Core Java

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:

package util;
 
public class LatencyMeasureExample {
    public static void main(String[] args) throws InterruptedException{
        //Below are a couple of examples
        LatencyMeasure lm = new LatencyMeasure(1000000);
 
        System.out.println("Thread.sleep() random");
        for (int i = 0; i < 100000; i++) {
            lm.startMeasure();
            Thread.sleep((long)Math.random()*10);
            lm.endMeasure();
        }
        lm.printStats();
 
        lm = new LatencyMeasure(1000000);
 
        double d = 0;
        System.out.println("\nMath.sqrt");
        for (int i = 0; i < 100000; i++) {
            lm.startMeasure();
            d+=Math.sqrt(i);
            lm.endMeasure();
        }
 
        lm.printStats();
    }
}

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:

package util;
 
import java.util.Arrays;
 
public class LatencyMeasure {
 
    private long[] times;
    private long time;
    private int index=0;
 
    public LatencyMeasure(int maxCapacity) {
        times = new long[maxCapacity];
 
        for (int i = 0; i < times.length; i++) {
            times[i] = -1;
        }
    }
 
    public void startMeasure(){
        time = System.nanoTime();
    }
 
    public void endMeasure() {
        times[index++] = System.nanoTime()-time;
    }
 
    public void printStats() {
        int filled = 0;
        for (int i = 0; i < times.length; i++) {
            if (times[i] == -1) {
                filled = i;
                break;
            }
        }
 
        long[] popTimes = new long[filled];
        System.arraycopy(times, 0, popTimes, 0, filled);
 
        Arrays.sort(popTimes);
        System.out.printf("Latency measured: \n" +
                        " %.2f us for 50 percentile\n" +
                        " %.2f us for 90 percentile\n" +
                        " %.2f us for 99 percentile\n" +
                        " %.2f us for 99.9 percentile\n" +
                        " %.2f us for 99.99 percentile\n" +
                        " %.2f us worst percentile\n",
                popTimes[popTimes.length / 2] / 1e3,
                popTimes[popTimes.length * 9 / 10] / 1e3,
                popTimes[popTimes.length - popTimes.length / 100] / 1e3,
                popTimes[popTimes.length - popTimes.length / 1000] / 1e3,
                popTimes[popTimes.length - popTimes.length / 10000] / 1e3,
                popTimes[popTimes.length - 1] / 1e3
        );
    }
}
Reference: Simple Class to Measure Latency from our JCG partner Daniel Shaya at the Rational Java blog.

Daniel Shaya

Daniel has been programming in Java since it was in beta. Working predominantly in the finance industry he has created real time trading and margin risk applications. He is currently a director at OpenHFT where we are building next generation Java low latency products.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button