如何在测量性能时获得直方图数据

时间:2013-01-31 01:38:54

标签: java histogram

我试图通过将数据放入HashMap中来衡量我们服务的性能 - X毫秒来回Y次呼叫。下面是我的代码,非常简单。它将在点击服务之前设置计时器,在响应返回后,它将测量时间。

private static void serviceCall() {

    histogram = new HashMap<Long, Long>();
    keys = histogram.keySet();
    long total = 10;
    long runs = total;

    while (runs > 0) {

        long start_time = System.currentTimeMillis();

        // hitting the service      
        result = restTemplate
                .getForObject("Some URL",String.class);

        long difference = (System.currentTimeMillis() - start_time);

        Long count = histogram.get(difference);
        if (count != null) {
            count++;
            histogram.put(Long.valueOf(difference), count);
        } else {
            histogram.put(Long.valueOf(difference), Long.valueOf(1L));
        }
        runs--;
    }
    for (Long key : keys) {
        Long value = histogram.get(key);
        System.out.println("SERVICE MEASUREMENT, HG data, " + key + ":" + value);
    }
}

目前我得到的输出是这样的 -

SERVICE MEASUREMENT, HG data, 166:1
SERVICE MEASUREMENT, HG data, 40:2
SERVICE MEASUREMENT, HG data, 41:4
SERVICE MEASUREMENT, HG data, 42:1
SERVICE MEASUREMENT, HG data, 43:1
SERVICE MEASUREMENT, HG data, 44:1

表示1 call came back in 166 ms2 calls came back in 40 ms,与其他输出相同。

问题陈述: -

我现在正在寻找的是这样的。我应该像这样设置范围 -

X Number of calls came back in between 1 and 10 ms
Y Number of calls came back in between 11 and 20 ms
Z Number of calls came back in between 21 and 30 ms
P Number of calls came back in between 31 and 40 ms
T number of calls came back in between 41 and 50 ms

....
....

I number of calls came back in more than 100 ms

还有任何配置范围的方法。假设将来我需要调整范围,我应该能够做到。我如何在当前的程序中实现这一点?任何建议都会有很大的帮助。

1 个答案:

答案 0 :(得分:1)

直方图是一组排列在相同大小的“箱”中的数据。您应该将时间测量值转换为bin,并将该bin用作映射键。这可以通过将时间值除以箱尺寸来完成。例如:time / 10L