| 1 | package org.mozilla.javascript.benchmarks; |
| 2 | |
| 3 | import com.google.caliper.api.ResultProcessor; |
| 4 | import com.google.caliper.model.Measurement; |
| 5 | import com.google.caliper.model.Trial; |
| 6 | import com.google.caliper.model.Value; |
| 7 | |
| 8 | import java.io.File; |
| 9 | import java.io.FileWriter; |
| 10 | import java.io.IOException; |
| 11 | import java.io.PrintWriter; |
| 12 | import java.util.HashMap; |
| 13 | import java.util.Map; |
| 14 | |
| 15 | public class ResultPlotter |
| 16 | implements ResultProcessor |
| 17 | |
| 18 | { |
| 19 | private PrintWriter out; |
| 20 | private final HashMap<String, Double> results = new HashMap<String, Double>(); |
| 21 | |
| 22 | public ResultPlotter() |
| 23 | throws IOException |
| 24 | { |
| 25 | out = new PrintWriter( |
| 26 | new FileWriter(new File(System.getProperty("rhino.benchmark.report"), "caliper-spider.csv")) |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public void close() |
| 32 | { |
| 33 | boolean once = false; |
| 34 | for (String n : results.keySet()) { |
| 35 | if (once) { |
| 36 | out.print(','); |
| 37 | } else { |
| 38 | once = true; |
| 39 | } |
| 40 | out.print(n); |
| 41 | } |
| 42 | out.println(); |
| 43 | |
| 44 | once = false; |
| 45 | for (Double v : results.values()) { |
| 46 | if (once) { |
| 47 | out.print(','); |
| 48 | } else { |
| 49 | once = true; |
| 50 | } |
| 51 | out.printf("%.2f", v); |
| 52 | } |
| 53 | out.println(); |
| 54 | |
| 55 | out.close(); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void processTrial(Trial trial) |
| 60 | { |
| 61 | if (trial.instrumentSpec().className().contains("RuntimeInstrument")) { |
| 62 | double runningAvg = 0.0; |
| 63 | for (Measurement m : trial.measurements()) { |
| 64 | runningAvg += (m.value().magnitude() / m.weight()); |
| 65 | } |
| 66 | double avg = runningAvg / (double) trial.measurements().size(); |
| 67 | results.put(trial.scenario().benchmarkSpec().methodName(), avg); |
| 68 | } |
| 69 | } |
| 70 | } |