Prefix is a lightweight profiler for .NET and Java developers. It enables developers to easily see what their code is doing. Including SQL queries, HTTP calls, errors, logs, and much more. Because it is a profiler, it can also be customized to track any method in your code or third party libraries! In this article we explore why that is useful and how to do it.
Custom method profiling is feature of Prefix Premium. Learn more about Prefix
Prefix tracks what your code is doing by web request. Prefix is very lightweight and designed to be your daily sidekick. It only tracks key methods in common .NET & Java libraries which enables it to understand what your code is doing at a high level. It can track SQL queries, HTTP calls, caching, queuing, errors, logs, and much more. Some example technologies that are supported are things like SQL, mongodb, redis, elasticsearch, etc.
Traditional profilers report how long and often methods are being called with no relation to the web requests. They track every method and sometimes every line of code in your application. This can be very slow and isn’t something you typically use very often unless you are doing some low level code performance tuning.
By default, Prefix tracks a lot of common things, but it doesn’t track everything. Tracking your own methods can be helpful to figure out if a method is being called, how often it is being called, and how long it takes within a specific web request or transaction. This is different than what you would normally see from a traditional profiler.
Good use cases:
For this example I have a simple web request that is taking 40-50ms but Prefix doesn’t show why. No SQL queries, HTTP calls or anything are showing up. Why does it take so long then?
If I look at the code, I can see it gets a list of users, serializes them to json, and then calculates an MD5 hash which is returned by the web request.
What is taking all the time?
One option would be to add some logging in my code and use that to estimate where the time is being spent. Instead, I’m going to tell Prefix to track all of these methods!
I do this by creating a little json file that contains the classes and method names I want to track. Please see our docs for how to do this: .NET Docs, Java Docs
I quickly changed my custom profiling config to include these methods and then restarted my application.
Here is what Prefix now shows with my custom methods being tracked. Success! Now I can tell that serializing the JSON is taking most of the time and the MD5 checksum takes a little time.
It is possible to use wildcards in the configuration to track multiple methods in a class. For example, I added {“Class” : “System.Web.Http.ApiController”, “Method” : “*”} to my config and restarted my application.
NOTE: Warning! Wild cards can cause a massive number of methods to be tracked which can greatly slow down your application and potentially cause it become unresponsive. Trying to track something like Newtonsoft.Json.* is a good example of what not to do! If you really want to track that many methods, you may want to use a traditional profiler, not Prefix.
Now I can see every method and property accessor call for ApiController.
If you would like to be a guest contributor to the Stackify blog please reach out to stackify@stackify.com