Last active
December 19, 2015 08:59
-
-
Save cosenary/5929458 to your computer and use it in GitHub Desktop.
Simple Benchmark chart class in Java.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Benchmark chart. | |
* Fancy benchmark chart generator. | |
* | |
* @since 4.04.2013 | |
* @author Christian Metz | [email protected] | |
* @version 1.0 | |
*/ | |
class Benchmark { | |
private final String CHART_API = "https://chart.googleapis.com/chart"; | |
private final LinkedHashMap<String, Long> benchmarks = new LinkedHashMap<>(); | |
private int width; | |
private int height; | |
/** | |
* Basic custom constructor. | |
*/ | |
public Benchmark() { | |
this(400, 250); | |
} | |
/** | |
* Size custom constructor. | |
* | |
* @param width | |
* @param height | |
*/ | |
public Benchmark(int width, int height) { | |
this.width = width; | |
this.height = height; | |
} | |
/** | |
* Add benchmark. | |
* | |
* @param title Description of the benchmark. | |
* @param value Measured value (e.g. time in ms.) | |
*/ | |
public void add(String title, long value) { | |
benchmarks.put(title, value); | |
} | |
/** | |
* Get chart image. | |
* | |
* @return Chart image icon. | |
*/ | |
public ImageIcon getChart() { | |
// assemble API call | |
String apiCall = CHART_API; | |
apiCall += "?cht=bhg&chs=" + width + "x" + height; | |
String titles = ""; | |
StringBuilder measurements = new StringBuilder(); | |
for (Map.Entry<String, Long> entry : benchmarks.entrySet()) { | |
titles += entry.getKey() + "|"; | |
measurements.insert(0, entry.getValue() + ","); | |
} | |
apiCall += "&chd=t:" + measurements.substring(0, measurements.length() - 1); | |
apiCall += "&chds=0,10000&chco=4d89f9,c6d9fd,00B88A&chds=0,10000&chbh=a&chxs=0,000000,0,0,_&chxt=y&chm=N,000000,0,,10|N,000000,1,,10|N,000000,2,,10&chxt=x,y"; | |
apiCall += "&chxl=1:|" + titles; | |
try { | |
URL url = new URL(apiCall); | |
BufferedImage image = ImageIO.read(url); | |
return new ImageIcon(image); | |
} catch (Exception e) { | |
System.err.println("Sorry - Chart creation failed :("); | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Test class. | |
*/ | |
class Main { | |
public static void main(String[] args) { | |
// create new instance | |
Benchmark benchmark = new Benchmark(); | |
// add some data | |
benchmark.add("Pizza", 10000); | |
benchmark.add("Cake", 3000); | |
benchmark.add("Chocolat", 8000); | |
// generate chart | |
ImageIcon chart = benchmark.getChart(); | |
// display it with a JOptionPane | |
JOptionPane.showMessageDialog(null, new JLabel(chart), "Elvira's Benchmark chart!", JOptionPane.INFORMATION_MESSAGE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment