我正在尝试测量每个线程插入数据库所需的时间。我已经在ConcurrentHashMap
命名映射中捕获了所有这些性能数字,例如每个线程插入的时间。在那个并发哈希映射中,它将是这样的。
Key- 10
Value- 2
这意味着 2 个呼叫在 10 毫秒内返回。下面的另一个例子
Key - 20
Value -1
这意味着,1 个呼叫在 20 毫秒内返回。
而且该地图将包含更多数据,这意味着更多的键值对。
所以现在我正在尝试通过使用上面的相同地图来做类似下面的事情,这意味着我需要迭代上面的地图以获得该特定范围内的以下数字。那有可能吗?
How many calls(X number) came back in between 1 and 20 ms
How many calls(X number) came back in between 20 and 40 ms
How many calls(X number) came back in between 40 and 60 ms
How many calls(X number) came back in between 60 and 80 ms
How many calls(X number) came back in between 80 and 100 ms
How many calls(X number) came back after 100 ms
我最初想到的一些代码。
SortedSet<Long> keys = new TreeSet<Long>(map.keySet());
for (Long key : keys) {
System.out.print(key + " : ");
for (int i = 0; i < map.get(key); i++) {
// Not sure what I am supposed to do here?
}
System.out.println();
}
有人能帮我一下吗?
更新:-
我的地图样本值-
{31=3, 48=1, 33=1, 30=12, 43=1, 38=1, 32=1}
这意味着总调用是3+1+1+12+1+1+1 = 20
通过value
从map
除此之外,我需要弄清楚上述情况意味着这样的事情
How many calls(X number) came back in between 1 and 20 ms
How many calls(X number) came back in between 20 and 40 ms
How many calls(X number) came back in between 40 and 60 ms
How many calls(X number) came back in between 60 and 80 ms
How many calls(X number) came back in between 80 and 100 ms
How many calls(X number) came back after 100 ms
以下是我尝试使用以下建议的代码-
私人静态无效drawHistogram(地图地图){
int counter[] = new int[6];
for (Integer key : map.keySet()) {
System.out.println("" + key);
// add sample
int idx = key / 20;
idx = Math.min(idx, counter.length - 1);
counter[idx]++;
}
for (int i = 0; i < counter.length; i++) {
System.out.println(counter[i] + " came back in between " + i * 20 + " and " + (i + 1) * 20
+ " ms");
}
}
如您所见,我打了 20 个电话,但这仅显示 7 个电话。我做错了什么吗?这是我得到的输出-
0 came back in between 0 and 20 ms
5 came back in between 20 and 40 ms
2 came back in between 40 and 60 ms
0 came back in between 60 and 80 ms
0 came back in between 80 and 100 ms
0 came back in between 100 and 120 ms
仅显示 7 个电话。但是有20个电话。