Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Track the Performance of Any Method in Your Code With Prefix

DZone's Guide to

How to Track the Performance of Any Method in Your Code With Prefix

Prefix tracks what your code does by web request, enables developers to easily see what their code is doing, and is good when you want to isolate performance problems.

· Performance Zone
Free Resource

Download our Introduction to API Performance Testing and learn why testing your API is just as important as testing your website, and how to start today.

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.

Prefix tracks what your code is doing by web request. It's very lightweight and designed to be your daily sidekick. It only tracks key methods in common .NET and 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 for 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.

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 shows 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 that is returned by the web request.

What is taking up 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 .NET docs and Java docs to see how to do this.)

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 and 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.

Image title

Find scaling and performance issues before your customers do with our Introduction to High-Capacity Load Testing guide.

Topics:
performance ,prefix ,tutorial ,methods

Published at DZone with permission of Matt Watson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}