从直方图计算平均值和百分位数?
2022-09-04 23:10:34
我编写了一个计时器,它将测量任何多线程应用程序中特定代码的性能。在下面的计时器中,它还将使用 x 毫秒的调用数填充地图。我将使用此映射作为直方图的一部分进行进一步分析,例如调用百分比花费了这么多毫秒等。
public static class StopWatch {
public static ConcurrentHashMap<Long, Long> histogram = new ConcurrentHashMap<Long, Long>();
/**
* Creates an instance of the timer and starts it running.
*/
public static StopWatch getInstance() {
return new StopWatch();
}
private long m_end = -1;
private long m_interval = -1;
private final long m_start;
private StopWatch() {
m_start = m_interval = currentTime();
}
/**
* Returns in milliseconds the amount of time that has elapsed since the timer was created. If the
* <code>stop</code> method has been invoked, then this returns instead the elapsed time between the creation of
* the timer and the moment when <code>stop</code> was invoked.
*
* @return duration it took
*/
public long getDuration() {
long result = 0;
final long startTime = m_start;
final long endTime = isStopWatchRunning() ? currentTime() : m_end;
result = convertNanoToMilliseconds(endTime - startTime);
boolean done = false;
while (!done) {
Long oldValue = histogram.putIfAbsent(result, 1L);
if (oldValue != null) {
done = histogram.replace(result, oldValue, oldValue + 1);
} else {
done = true;
}
}
return result;
}
/**
* Returns in milliseconds the amount of time that has elapsed since the last invocation of this same method. If
* this method has not previously been invoked, then it is the amount of time that has elapsed since the timer
* was created. <strong>Note</strong> that once the <code>stop</code> method has been invoked this will just
* return zero.
*
* @return interval period
*/
public long getInterval() {
long result = 0;
final long startTime = m_interval;
final long endTime;
if (isStopWatchRunning()) {
endTime = m_interval = currentTime();
} else {
endTime = m_end;
}
result = convertNanoToMilliseconds(endTime - startTime);
return result;
}
/**
* Stops the timer from advancing. This has an impact on the values returned by both the
* <code>getDuration</code> and the <code>getInterval</code> methods.
*/
public void stop() {
if (isStopWatchRunning()) {
m_end = currentTime();
}
}
/**
* What is the current time in nanoseconds?
*
* @return returns back the current time in nanoseconds
*/
private long currentTime() {
return System.nanoTime();
}
/**
* This is used to check whether the timer is alive or not
*
* @return checks whether the timer is running or not
*/
private boolean isStopWatchRunning() {
return (m_end <= 0);
}
/**
* This is used to convert NanoSeconds to Milliseconds
*
* @param nanoseconds
* @return milliseconds value of nanoseconds
*/
private long convertNanoToMilliseconds(final long nanoseconds) {
return nanoseconds / 1000000L;
}
}
例如,这就是我将使用上述计时器类来测量多线程应用程序中特定代码性能的方式:
StopWatch timer = StopWatch.getInstance();
//... some code here to measure
timer.getDuration();
现在我的问题是 - 从我的直方图地图中计算请求的平均,中位数,第95和第99百分位数的最佳方法是什么?我的意思是说,我只想在我的秒表类中添加某些方法,这些方法将完成所有计算,例如查找平均值,中位数,第95和第99百分位。
然后我可以通过使用实例直接获取它。StopWatch
我的直方图将如下所示:
键 - 表示毫秒数
value - 表示花费了那么多毫秒的调用次数。