I am trying to measure how much time each thread takes in inserting to database. I have captured all those performance numbers in a ConcurrentHashMap named map like how much time each thread is taking in inserting. In that concurrent hash map it will be something like this.
Key- 10
Value- 2
So that means, 2 Calls came back in 10 ms. Another example below
Key - 20
Value -1
which means, 1 Call came back in 20 ms.
And that map will contain lot more data means lot more key value pair.
So now I am trying to do something like below by using the same map above, that means I need to iterate the above map to get the below numbers in that particular range. Is that possible to do?
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
Some code that I thought of initially.
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();
}
Can anyone help me here?
Update:-
My map sample value-
{31=3, 48=1, 33=1, 30=12, 43=1, 38=1, 32=1}
It means total call was 3+1+1+12+1+1+1 = 20 by adding value from the map
And out of that I need to figure out the above scenario means something like this
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
Below is my code that I have tried with the below suggestion-
private static void drawHistogram(Map map) {
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");
}
}
As you can see, I have 20 calls made but this is showing only 7 calls. Anything wrong I did? This is the output I got-
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
which shows only 7 calls. But there are 20 calls.
Anticipating the need to easily redefine bucket size (and indeed the number of buckets you aggregate into) I propose:
Configurability is managed by changing
definition. I used the following code to test this: