How to track the performance of any method in your code with Prefix

By: mwatson
  |  March 14, 2024
How to track the performance of any method in your code with Prefix

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

How is Prefix different than a “normal” profiler?

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.

Why would I use custom method profiling with Prefix?

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:

  • Trying to isolate performance problems
  • Tracking common methods in your application architecture

Example: Isolating performance problems

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?

prefix-shows-nothing

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?

jsontest

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.

jsontest2

Tip & Warning: Tracking all methods in a class

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.

apicontroller


Get Prefix Power on a server!
Get Prefix Power on a server!


Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More

Want to contribute to the Stackify blog?

If you would like to be a guest contributor to the Stackify blog please reach out to [email protected]