1

I am trying to measure the performance of Database Insert. I have captured all those performance numbers in a ConcurrentHashMap. I am trying to make a Histogram of those numbers.

In that concurrent hash map it will be something like this.I am quoting an example. But that map will have lot more data means lot more key value pair.

Key- 11
Value- 2

So that means, 2 Calls came back in 11 ms. Another example below

Key - 30
Value -1 

which means, 1 Call came back in 30 ms.

So basis on the above map, I am trying to do something like this-

Number of calls came back in between 1 and 10 ms
Number of calls came back in between 10 and 20 ms
Number of calls came back in between 20 and 30 ms
Number of calls came back in between 30 and 40 ms
Number of calls came back in between 40 and 50 ms
Number of calls came back in between 50 and 60 ms
Number of calls came back in between 60 and 70 ms
Number of calls came back in between 70 and 80 ms
Number of calls came back in between 80 and 90 ms
Number of calls came back in between 90 and 100 ms
Number of calls came back in greater than 100 ms

I am not able to find an easy approach to make a histogram like above from that map. Only thing I can think of is hard code the various counter and keep on incrementing those counter if it falls within that particular range. But this doesn't looks a clean way to do it. Any thoughts how can I solve this problem?

private static void logHistogramInfo() {

  // here histogram is the concurrenthashmap
    System.out.println(histogram);


}
AKIWEB
  • 19,008
  • 67
  • 180
  • 294
  • Here http://stackoverflow.com/questions/13105765/how-to-convert-numbers-into-symbols-in-java-for-example-instead-of-2-to-or – Apurv Feb 07 '13 at 07:20
  • That was my approach as well. But that code is just putting lot more if else for all the ranges possible. But in future suppose if that range got increased, then I need to go and add another if else loop which I want to avoid. That is the reason I mentioned any more cleaner solution? – AKIWEB Feb 07 '13 at 07:27

2 Answers2

1

A simple Histogram in Java:

import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
public class Histogram {
    public static void main(String[] args) {
        Map<Integer,Integer> data = new HashMap<Integer,Integer>();
        data.put(10, 2);
        data.put(20, 3);
        data.put(30, 5);
        data.put(40, 15);
        data.put(50, 4);
        drawHistogram(data);
    }

    private static void drawHistogram(Map<Integer,Integer> data){
        SortedSet<Integer> keys = new TreeSet<Integer>(data.keySet());
        for(Integer key : keys){
            System.out.print(key + " : ");
            for(int i = 0; i< data.get(key); i++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
Apurv
  • 3,723
  • 3
  • 30
  • 51
0

Here's a quick hack: Separate the concerns in the following way.

  1. Don't keep the histogram information using Java data structures. It clutters your code. Instead, just output to a log file the duration of each call, no pre-processing.
  2. Use any statistical application, such as Excel, R, or SciPy to import the data and produce the histogram. For example, follow these instructions to produce a histogram in Excel. R has a built-in function for plotting a histogram in much less hassle.

The advantage of this approach, IMHO, is that you can run any sort of statistical analysis on this data using specialized tools, without interfering with your main development effort. The downsize is that the log file can be rather lengthy. Use a gzipped output stream if this becomes a real issue.

Little Bobby Tables
  • 5,261
  • 2
  • 39
  • 49