|
| 1 | +package graphql.performance.page.analyzer; |
| 2 | + |
| 3 | +import graphql.performance.page.model.BenchmarkResult; |
| 4 | +import graphql.performance.page.model.BenchmarkSeries; |
| 5 | +import graphql.performance.page.model.ResultFile; |
| 6 | + |
| 7 | +import java.util.Comparator; |
| 8 | +import java.util.LinkedHashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.TreeMap; |
| 12 | + |
| 13 | +public class BenchmarkAnalyzer { |
| 14 | + |
| 15 | + /** |
| 16 | + * Unit conversion factors to a common base unit. |
| 17 | + * For time units: base is nanoseconds. |
| 18 | + * For throughput units: base is ops/s. |
| 19 | + */ |
| 20 | + private static final Map<String, Double> TIME_UNIT_TO_NANOS = Map.of( |
| 21 | + "ns/op", 1.0, |
| 22 | + "us/op", 1_000.0, |
| 23 | + "ms/op", 1_000_000.0, |
| 24 | + "s/op", 1_000_000_000.0 |
| 25 | + ); |
| 26 | + |
| 27 | + private static final Map<String, Double> THROUGHPUT_UNIT_TO_OPS_PER_S = Map.of( |
| 28 | + "ops/s", 1.0, |
| 29 | + "ops/ms", 1_000.0, |
| 30 | + "ops/us", 1_000_000.0, |
| 31 | + "ops/ns", 1_000_000_000.0 |
| 32 | + ); |
| 33 | + |
| 34 | + /** |
| 35 | + * Groups all results from all files into benchmark series, sorted by timestamp. |
| 36 | + * Returns a map of benchmarkClassName -> list of series for that class. |
| 37 | + */ |
| 38 | + public Map<String, List<BenchmarkSeries>> analyze(List<ResultFile> files) { |
| 39 | + // Sort files by timestamp |
| 40 | + files.sort(Comparator.comparing(ResultFile::getTimestamp)); |
| 41 | + |
| 42 | + // Build series map: seriesKey -> BenchmarkSeries |
| 43 | + Map<String, BenchmarkSeries> seriesMap = new LinkedHashMap<>(); |
| 44 | + |
| 45 | + for (ResultFile file : files) { |
| 46 | + for (BenchmarkResult result : file.getResults()) { |
| 47 | + String key = result.getSeriesKey(); |
| 48 | + BenchmarkSeries series = seriesMap.computeIfAbsent(key, k -> |
| 49 | + new BenchmarkSeries( |
| 50 | + key, |
| 51 | + result.getBenchmark(), |
| 52 | + result.getBenchmarkClassName(), |
| 53 | + result.getBenchmarkMethodName(), |
| 54 | + result.getMode(), |
| 55 | + result.getParamsString() |
| 56 | + ) |
| 57 | + ); |
| 58 | + series.addDataPoint(new BenchmarkSeries.DataPoint( |
| 59 | + file.getTimestamp(), |
| 60 | + file.getCommitHash(), |
| 61 | + file.getJdkVersion(), |
| 62 | + result.getPrimaryMetric().getScore(), |
| 63 | + result.getPrimaryMetric().getScoreError(), |
| 64 | + result.getPrimaryMetric().getScoreUnit() |
| 65 | + )); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Normalize units within each series |
| 70 | + for (BenchmarkSeries series : seriesMap.values()) { |
| 71 | + normalizeUnits(series); |
| 72 | + } |
| 73 | + |
| 74 | + // Group by benchmark class name, sorted alphabetically |
| 75 | + Map<String, List<BenchmarkSeries>> grouped = new TreeMap<>(); |
| 76 | + for (BenchmarkSeries series : seriesMap.values()) { |
| 77 | + grouped.computeIfAbsent(series.getBenchmarkClassName(), k -> new java.util.ArrayList<>()) |
| 78 | + .add(series); |
| 79 | + } |
| 80 | + |
| 81 | + // Sort series within each class: by mode (thrpt first), then by display label |
| 82 | + for (List<BenchmarkSeries> seriesList : grouped.values()) { |
| 83 | + seriesList.sort(Comparator |
| 84 | + .comparing(BenchmarkSeries::getMode) |
| 85 | + .thenComparing(BenchmarkSeries::getDisplayLabel)); |
| 86 | + } |
| 87 | + |
| 88 | + return grouped; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Normalizes all data points in a series to the most recent unit. |
| 93 | + * This handles cases where a benchmark changed units over time (e.g. ns/op -> ms/op). |
| 94 | + */ |
| 95 | + private void normalizeUnits(BenchmarkSeries series) { |
| 96 | + List<BenchmarkSeries.DataPoint> points = series.getDataPoints(); |
| 97 | + if (points.size() < 2) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + // Target unit is the most recent data point's unit |
| 102 | + String targetUnit = points.getLast().scoreUnit(); |
| 103 | + |
| 104 | + // Check if all points already have the same unit |
| 105 | + boolean allSame = points.stream().allMatch(dp -> dp.scoreUnit().equals(targetUnit)); |
| 106 | + if (allSame) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + // Determine if these are time or throughput units |
| 111 | + boolean isTime = TIME_UNIT_TO_NANOS.containsKey(targetUnit); |
| 112 | + boolean isThroughput = THROUGHPUT_UNIT_TO_OPS_PER_S.containsKey(targetUnit); |
| 113 | + |
| 114 | + if (!isTime && !isThroughput) { |
| 115 | + // Unknown unit family, skip normalization |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + // Replace data points with normalized values |
| 120 | + for (int i = 0; i < points.size(); i++) { |
| 121 | + BenchmarkSeries.DataPoint dp = points.get(i); |
| 122 | + if (!dp.scoreUnit().equals(targetUnit)) { |
| 123 | + double factor = computeConversionFactor(dp.scoreUnit(), targetUnit, isTime); |
| 124 | + if (!Double.isNaN(factor)) { |
| 125 | + points.set(i, new BenchmarkSeries.DataPoint( |
| 126 | + dp.timestamp(), |
| 127 | + dp.commitHash(), |
| 128 | + dp.jdkVersion(), |
| 129 | + dp.score() * factor, |
| 130 | + dp.scoreError() * factor, |
| 131 | + targetUnit |
| 132 | + )); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private double computeConversionFactor(String fromUnit, String toUnit, boolean isTime) { |
| 139 | + if (isTime) { |
| 140 | + Double fromFactor = TIME_UNIT_TO_NANOS.get(fromUnit); |
| 141 | + Double toFactor = TIME_UNIT_TO_NANOS.get(toUnit); |
| 142 | + if (fromFactor == null || toFactor == null) { |
| 143 | + return Double.NaN; |
| 144 | + } |
| 145 | + return fromFactor / toFactor; |
| 146 | + } else { |
| 147 | + Double fromFactor = THROUGHPUT_UNIT_TO_OPS_PER_S.get(fromUnit); |
| 148 | + Double toFactor = THROUGHPUT_UNIT_TO_OPS_PER_S.get(toUnit); |
| 149 | + if (fromFactor == null || toFactor == null) { |
| 150 | + return Double.NaN; |
| 151 | + } |
| 152 | + return fromFactor / toFactor; |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments