VOOZH about

URL: https://www.javacodegeeks.com/2015/05/simple-class-to-measure-latency.html

⇱ Simple Class to Measure Latency - Java Code Geeks


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.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Daniel Shaya
Daniel Shaya
May 4th, 2015Last Updated: May 4th, 2015
0 731 2 minutes read

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

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

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz