虚拟机间延迟测量_简单的类来测量延迟
生活随笔
收集整理的這篇文章主要介紹了
虚拟机间延迟测量_简单的类来测量延迟
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
虛擬機間延遲測量
這是我編寫的用于測量延遲的非常簡單的類。 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();d+=Math.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種方法:
- 構(gòu)造函數(shù):這需要一個int表示要測量的最大次數(shù)。 除了對內(nèi)存的影響外,過大的大小也不是問題。 在此實現(xiàn)中,您至少需要進行10,000次測量才能使代碼正常工作。 如果您想花更少的錢,只需在printStats()中適當調(diào)整代碼即可。
- 在要測量的代碼的任一側(cè)調(diào)用startMeasure()和endMeasure()。
- printStats()打印出結(jié)果。
實施如下:
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);} }翻譯自: https://www.javacodegeeks.com/2015/05/simple-class-to-measure-latency.html
虛擬機間延遲測量
總結(jié)
以上是生活随笔為你收集整理的虚拟机间延迟测量_简单的类来测量延迟的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linuxvi命令是什么(linux v
- 下一篇: 安卓窃听猫(安卓窃听)