html5开发微网站,宁波网站推广公司报价,wordpress 结构化数据,wordpress开场动画虚拟机间延迟测量这是我编写的用于测量延迟的非常简单的类。 HDRHistogram不是劳斯莱斯解决方案#xff0c;但是如果您只想在项目中添加一个类#xff0c;那么效果就很好。 这是一个简单的测试程序#xff0c;向您展示其用法#xff1a; package util;public class Laten… 虚拟机间延迟测量 这是我编写的用于测量延迟的非常简单的类。 HDRHistogram不是劳斯莱斯解决方案但是如果您只想在项目中添加一个类那么效果就很好。 这是一个简单的测试程序向您展示其用法 package util;public class LatencyMeasureExample {public static void main(String[] args) throws InterruptedException{//Below are a couple of examplesLatencyMeasure 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();dMath.sqrt(i);lm.endMeasure();}lm.printStats();}
} 这是一些示例输出 Thread.sleep() random
Latency measured:0.32 us for 50 percentile0.44 us for 90 percentile0.68 us for 99 percentile26.82 us for 99.9 percentile582.66 us for 99.99 percentile2024.92 us worst percentile
Math.sqrt
Latency measured:0.04 us for 50 percentile0.06 us for 90 percentile0.09 us for 99 percentile0.12 us for 99.9 percentile0.20 us for 99.99 percentile28.17 us worst percentile 只有4种方法 构造函数这需要一个int表示要测量的最大次数。 除了对内存的影响外过大的大小也不是问题。 在此实现中您至少需要进行10,000次测量才能使代码正常工作。 如果您想花更少的钱只需在printStats中适当调整代码即可。 在要测量的代码的任一侧调用startMeasure和endMeasure。 printStats打印出结果。 实施如下 package util;import java.util.Arrays;public class LatencyMeasure {private long[] times;private long time;private int index0;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);}
}翻译自: https://www.javacodegeeks.com/2015/05/simple-class-to-measure-latency.html虚拟机间延迟测量