|
| 1 | +import { Buffer } from "buffer/"; |
1 | 2 | import * as d3 from "d3"; |
2 | 3 | import React, { useEffect, useRef } from "react"; |
3 | 4 | import * as zlib from "zlib"; |
4 | | -import { BenchmarkResult, MemoryBenchmarkCategory } from "./benchmark-types"; |
| 5 | +import { |
| 6 | + BenchmarkKind, |
| 7 | + BenchmarkResult, |
| 8 | + isMemoryBenchmarkResult, |
| 9 | + isRuntimeBenchmarkResult, |
| 10 | + MemoryBenchmarkCategory, |
| 11 | + MemoryBenchmarkResult, |
| 12 | + RuntimeBenchmarkResult, |
| 13 | +} from "./benchmark-types"; |
5 | 14 | import { joinOnProperty, JoinResult } from "./util"; |
6 | 15 | import { barComparisonGraph } from "./visualizations/bar-comparison-graph"; |
7 | 16 | import { positiveNegativeBarGraph } from "./visualizations/positive-negative-bar-graph"; |
8 | 17 |
|
9 | | -const garbageCreatedComparisonGraphWidth = 1000; |
10 | | -const garbageCreatedComparisonGraphHeight = 300; |
| 18 | +const comparisonGraphWidth = 1000; |
| 19 | +const comparisonGraphHeight = 300; |
11 | 20 |
|
12 | | -const garbageCreatedChangeGraphWidth = 1000; |
13 | | -const garbageCreatedChangeGraphHeight = 300; |
| 21 | +const changeGraphWidth = 1000; |
| 22 | +const changeGraphHeight = 300; |
14 | 23 |
|
15 | 24 | // Utility functions |
16 | 25 | const formatBenchmarkName = (benchmarkName: string) => benchmarkName.replace(".lua", "").split("/").pop()!; |
17 | | -const formatMemory = (value: number) => `${Math.round(value / 10) / 100} Mb`; |
18 | | - |
19 | | -const benchmarkGarbage = (bm: BenchmarkResult) => bm.categories[MemoryBenchmarkCategory.Garbage]; |
20 | | -const garbagePercentChange = (result: JoinResult<BenchmarkResult>) => |
21 | | - (benchmarkGarbage(result.right!) - benchmarkGarbage(result.left!)) / benchmarkGarbage(result.left!); |
22 | | - |
23 | | -export default function Benchmark() { |
24 | | - let garbageCreatedChangeSvgRef = useRef<SVGSVGElement>(null!); |
25 | | - let garbageCreatedComparisonSvgRef = useRef<SVGSVGElement>(null!); |
26 | | - |
27 | | - const benchmarkData = decodeBenchmarkData(window.location.search.split("?d=")[1]); |
28 | | - // Sort by percentage change of garbage created |
29 | | - const benchmarksSortedByPercentDifference = benchmarkData.sort( |
30 | | - (a, b) => garbagePercentChange(a) - garbagePercentChange(b), |
| 26 | +const percentChange = (oldValue: number, newValue: number) => ((newValue - oldValue) / oldValue) * 100; |
| 27 | +const formatMemory = (value: number) => `${(value / 1000).toFixed(2)} Mb`; |
| 28 | +const formatTime = (value: number) => `${(value * 1000).toFixed(2)} ms`; |
| 29 | +const formatPercent = (value: number) => `${value.toFixed(2)}%`; |
| 30 | + |
| 31 | +function BenchmarkCategory<T extends BenchmarkResult>({ |
| 32 | + name, |
| 33 | + benchmarks, |
| 34 | + extractValue, |
| 35 | + formatValue, |
| 36 | +}: { |
| 37 | + name: string; |
| 38 | + benchmarks: JoinResult<T>[]; |
| 39 | + extractValue: (result: T) => number; |
| 40 | + formatValue: (value: number) => string; |
| 41 | +}) { |
| 42 | + let changeSvgRef = useRef<SVGSVGElement>(null!); |
| 43 | + let comparisonSvgRef = useRef<SVGSVGElement>(null!); |
| 44 | + |
| 45 | + const percentChangeForResult = (result: JoinResult<T>) => |
| 46 | + percentChange(extractValue(result.left!), extractValue(result.right!)); |
| 47 | + |
| 48 | + // Sort by percentage change |
| 49 | + const benchmarksSortedByPercentageDiff = benchmarks.sort( |
| 50 | + (a, b) => percentChangeForResult(a) - percentChangeForResult(b), |
31 | 51 | ); |
32 | 52 |
|
33 | 53 | // Populate graph with benchmark results |
34 | | - const benchmarkResultsTable = benchmarksSortedByPercentDifference.map((bm, i) => { |
35 | | - const change = garbagePercentChange(bm); |
| 54 | + const memoryBenchmarksResultTable = benchmarksSortedByPercentageDiff.map((bm, i) => { |
| 55 | + const change = percentChangeForResult(bm); |
36 | 56 | const rowColor = change === 0 ? "currentColor" : change > 0 ? "red" : "green"; |
37 | 57 |
|
38 | 58 | return ( |
39 | 59 | <tr key={i} style={{ color: rowColor }}> |
40 | 60 | <td>{bm.left!.benchmarkName}</td> |
41 | | - <td>{formatMemory(benchmarkGarbage(bm.left!))}</td> |
42 | | - <td>{formatMemory(benchmarkGarbage(bm.right!))}</td> |
43 | | - <td>{change}</td> |
| 61 | + <td>{formatValue(extractValue(bm.left!))}</td> |
| 62 | + <td>{formatValue(extractValue(bm.right!))}</td> |
| 63 | + <td>{formatPercent(change)}</td> |
44 | 64 | </tr> |
45 | 65 | ); |
46 | 66 | }); |
47 | 67 |
|
48 | | - // Comparison data master garbage created vs commit garbate created (PERCENTAGE CHANGE) |
49 | | - const generatedGarbageChangeData = benchmarksSortedByPercentDifference.map((bm) => { |
50 | | - const oldValue = bm.left!.categories[MemoryBenchmarkCategory.Garbage]!; |
51 | | - const newValue = bm.right!.categories[MemoryBenchmarkCategory.Garbage]!; |
52 | | - |
53 | | - return { |
54 | | - name: formatBenchmarkName(bm.left!.benchmarkName || bm.right!.benchmarkName!), |
55 | | - value: (100 * (newValue - oldValue)) / oldValue, |
56 | | - }; |
57 | | - }); |
| 68 | + // Comparison data master vs commit (PERCENTAGE CHANGE) |
| 69 | + const changeData = benchmarksSortedByPercentageDiff.map((bm) => ({ |
| 70 | + name: formatBenchmarkName(bm.left!.benchmarkName || bm.right!.benchmarkName!), |
| 71 | + value: percentChangeForResult(bm), |
| 72 | + })); |
58 | 73 |
|
59 | | - // Comparison data master garbage created vs commit garbate created (ABSOLUTE) |
60 | | - const generatedGarbageData = benchmarksSortedByPercentDifference.map((bm) => ({ |
| 74 | + // Comparison data master vs commit (ABSOLUTE) |
| 75 | + const absoluteData = benchmarksSortedByPercentageDiff.map((bm) => ({ |
61 | 76 | name: formatBenchmarkName(bm.left!.benchmarkName || bm.right!.benchmarkName!), |
62 | | - oldValue: bm.left!.categories[MemoryBenchmarkCategory.Garbage] || 0, |
63 | | - newValue: bm.right!.categories[MemoryBenchmarkCategory.Garbage] || 0, |
| 77 | + oldValue: extractValue(bm.left!) || 0, |
| 78 | + newValue: extractValue(bm.right!) || 0, |
64 | 79 | })); |
65 | 80 |
|
66 | 81 | useEffect(() => { |
67 | | - // Populate graph showing percentual change in garbage created |
68 | | - positiveNegativeBarGraph( |
69 | | - d3.select(garbageCreatedChangeSvgRef.current), |
70 | | - generatedGarbageChangeData, |
71 | | - garbageCreatedChangeGraphWidth, |
72 | | - garbageCreatedChangeGraphHeight, |
73 | | - ); |
| 82 | + // Populate graph showing percent change |
| 83 | + positiveNegativeBarGraph(d3.select(changeSvgRef.current), changeData, changeGraphWidth, changeGraphHeight); |
74 | 84 |
|
75 | | - // Populate graph showing absolute garbage created numbers |
| 85 | + // Populate graph showing absolute numbers |
76 | 86 | barComparisonGraph( |
77 | | - d3.select(garbageCreatedComparisonSvgRef.current), |
78 | | - generatedGarbageData, |
79 | | - garbageCreatedComparisonGraphWidth, |
80 | | - garbageCreatedComparisonGraphHeight, |
| 87 | + d3.select(comparisonSvgRef.current), |
| 88 | + absoluteData, |
| 89 | + comparisonGraphWidth, |
| 90 | + comparisonGraphHeight, |
81 | 91 | ); |
82 | 92 | }); |
83 | 93 |
|
84 | 94 | return ( |
85 | 95 | <> |
86 | | - <h2>Benchmark results</h2> |
| 96 | + <h1>{name} results</h1> |
87 | 97 | {/* Results table */} |
88 | 98 | <table> |
89 | 99 | <thead> |
90 | 100 | <tr style={{ fontWeight: "bold" }}> |
91 | 101 | <td>Benchmark</td> |
92 | | - <td>Garbage Master</td> |
93 | | - <td>Garbage Commit</td> |
| 102 | + <td>Master</td> |
| 103 | + <td>Commit</td> |
94 | 104 | <td>% Change</td> |
95 | 105 | </tr> |
96 | 106 | </thead> |
97 | | - <tbody>{benchmarkResultsTable}</tbody> |
| 107 | + <tbody>{memoryBenchmarksResultTable}</tbody> |
98 | 108 | </table> |
99 | 109 |
|
100 | | - <h2>Garbage created change</h2> |
101 | | - {/* [% Delta] Gerbage created */} |
102 | | - <svg |
103 | | - ref={garbageCreatedChangeSvgRef} |
104 | | - width={garbageCreatedChangeGraphWidth} |
105 | | - height={garbageCreatedChangeGraphHeight} |
106 | | - ></svg> |
107 | | - |
108 | | - <h2>Garbage created</h2> |
109 | | - {/* [Absolute] Garbage created comparison */} |
110 | | - <svg |
111 | | - ref={garbageCreatedComparisonSvgRef} |
112 | | - width={garbageCreatedComparisonGraphWidth} |
113 | | - height={garbageCreatedComparisonGraphHeight} |
114 | | - ></svg> |
| 110 | + <h2>{name} change</h2> |
| 111 | + {/* [% Delta] */} |
| 112 | + <svg ref={changeSvgRef} width={changeGraphWidth} height={changeGraphHeight} /> |
| 113 | + |
| 114 | + <h2>{name}</h2> |
| 115 | + {/* [Absolute] comparison */} |
| 116 | + <svg ref={comparisonSvgRef} width={comparisonGraphWidth} height={comparisonGraphHeight} /> |
| 117 | + </> |
| 118 | + ); |
| 119 | +} |
| 120 | + |
| 121 | +export default function Benchmark() { |
| 122 | + const benchmarkData = decodeBenchmarkData(window.location.search.split("?d=")[1]); |
| 123 | + return ( |
| 124 | + <> |
| 125 | + {/* Results table */} |
| 126 | + <BenchmarkCategory |
| 127 | + name={"Garbage created"} |
| 128 | + benchmarks={benchmarkData.memory} |
| 129 | + extractValue={(bm) => bm.categories[MemoryBenchmarkCategory.Garbage]} |
| 130 | + formatValue={formatMemory} |
| 131 | + /> |
| 132 | + |
| 133 | + <BenchmarkCategory |
| 134 | + name={"Runtime"} |
| 135 | + benchmarks={benchmarkData.runtime} |
| 136 | + extractValue={(bm) => bm.time} |
| 137 | + formatValue={formatTime} |
| 138 | + /> |
115 | 139 | </> |
116 | 140 | ); |
117 | 141 | } |
118 | 142 |
|
119 | | -function decodeBenchmarkData(encodedData: string) { |
| 143 | +interface BenchmarkData { |
| 144 | + [BenchmarkKind.Memory]: JoinResult<MemoryBenchmarkResult>[]; |
| 145 | + [BenchmarkKind.Runtime]: JoinResult<RuntimeBenchmarkResult>[]; |
| 146 | +} |
| 147 | + |
| 148 | +function isMemoryBenchmarkJoinResult(r: JoinResult<BenchmarkResult>): r is JoinResult<MemoryBenchmarkResult> { |
| 149 | + return isMemoryBenchmarkResult(r.left || r.right!); |
| 150 | +} |
| 151 | + |
| 152 | +function isRuntimeBenchmarkJoinResult(r: JoinResult<BenchmarkResult>): r is JoinResult<RuntimeBenchmarkResult> { |
| 153 | + return isRuntimeBenchmarkResult(r.left || r.right!); |
| 154 | +} |
| 155 | + |
| 156 | +function decodeBenchmarkData(encodedData: string): BenchmarkData { |
120 | 157 | const results = JSON.parse(zlib.inflateSync(Buffer.from(encodedData, "base64")).toString()); |
121 | 158 |
|
122 | 159 | const dataMaster = results.old as BenchmarkResult[]; |
123 | 160 | const dataCommit = results.new as BenchmarkResult[]; |
124 | 161 |
|
125 | 162 | // Match old/new results by name |
126 | | - return joinOnProperty(dataMaster, dataCommit, (bm) => bm.benchmarkName); |
| 163 | + const joined = joinOnProperty(dataMaster, dataCommit, (bm) => bm.benchmarkName); |
| 164 | + return { |
| 165 | + [BenchmarkKind.Memory]: joined.filter(isMemoryBenchmarkJoinResult), |
| 166 | + [BenchmarkKind.Runtime]: joined.filter(isRuntimeBenchmarkJoinResult), |
| 167 | + }; |
127 | 168 | } |
0 commit comments