Friday 1 May 2015

Simple Class to Measure Latency

This is a very simple class I wrote to measure latency.  It's not the Rolls Royce solution that is HDRHistogram but if you want to add just one class to your project this does the trick quite nicely.

Here's a simple test program to show you how it's used:


This is some sample output:

Thread.sleep() random
Latency measured:
 0.32 us for 50 percentile
 0.44 us for 90 percentile
 0.68 us for 99 percentile
 26.82 us for 99.9 percentile
 582.66 us for 99.99 percentile
 2024.92 us worst percentile
Math.sqrt
Latency measured:
 0.04 us for 50 percentile
 0.06 us for 90 percentile
 0.09 us for 99 percentile
 0.12 us for 99.9 percentile
 0.20 us for 99.99 percentile
 28.17 us worst percentile

There are only 4 methods:
  • The constructor: This takes an int for the maximum number of times you want to measure. Apart from memory implications, oversizing, is not a problem.  In this implementation you need to take at least 10,000 measurements for the code to work. If you want to take less just adapt the code appropriately in printStats().
  • startMeasure() and endMeasure() are called on either side of the code to be measured.
  • printStats() prints out the results.
Implementation below:

No comments:

Post a Comment