|
| 1 | +import * as d3 from "d3"; |
| 2 | +import React, { useEffect, useRef } from "react"; |
| 3 | +import * as zlib from "zlib"; |
| 4 | +import { BenchmarkResult, MemoryBenchmarkCategory } from "./benchmark-types"; |
| 5 | +import { joinOnProperty, JoinResult } from "./util"; |
| 6 | +import { barComparisonGraph } from "./visualizations/bar-comparison-graph"; |
| 7 | +import { positiveNegativeBarGraph } from "./visualizations/positive-negative-bar-graph"; |
| 8 | + |
| 9 | +const garbageCreatedComparisonGraphWidth = 1000; |
| 10 | +const garbageCreatedComparisonGraphHeight = 300; |
| 11 | + |
| 12 | +const garbageCreatedChangeGraphWidth = 1000; |
| 13 | +const garbageCreatedChangeGraphHeight = 300; |
| 14 | + |
| 15 | +// Utility functions |
| 16 | +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), |
| 31 | + ); |
| 32 | + |
| 33 | + // Populate graph with benchmark results |
| 34 | + const benchmarkResultsTable = benchmarksSortedByPercentDifference.map((bm, i) => { |
| 35 | + const change = garbagePercentChange(bm); |
| 36 | + const rowColor = change === 0 ? "currentColor" : change > 0 ? "red" : "green"; |
| 37 | + |
| 38 | + return ( |
| 39 | + <tr key={i} style={{ color: rowColor }}> |
| 40 | + <td>{bm.left!.benchmarkName}</td> |
| 41 | + <td>{formatMemory(benchmarkGarbage(bm.left!))}</td> |
| 42 | + <td>{formatMemory(benchmarkGarbage(bm.right!))}</td> |
| 43 | + <td>{change}</td> |
| 44 | + </tr> |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 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 | + }); |
| 58 | + |
| 59 | + // Comparison data master garbage created vs commit garbate created (ABSOLUTE) |
| 60 | + const generatedGarbageData = benchmarksSortedByPercentDifference.map((bm) => ({ |
| 61 | + name: formatBenchmarkName(bm.left!.benchmarkName || bm.right!.benchmarkName!), |
| 62 | + oldValue: bm.left!.categories[MemoryBenchmarkCategory.Garbage] || 0, |
| 63 | + newValue: bm.right!.categories[MemoryBenchmarkCategory.Garbage] || 0, |
| 64 | + })); |
| 65 | + |
| 66 | + useEffect(() => { |
| 67 | + // Populate graph showing percentual change in garbage created |
| 68 | + positiveNegativeBarGraph( |
| 69 | + d3.select(garbageCreatedChangeSvgRef.current), |
| 70 | + generatedGarbageChangeData, |
| 71 | + garbageCreatedChangeGraphWidth, |
| 72 | + garbageCreatedChangeGraphHeight, |
| 73 | + ); |
| 74 | + |
| 75 | + // Populate graph showing absolute garbage created numbers |
| 76 | + barComparisonGraph( |
| 77 | + d3.select(garbageCreatedComparisonSvgRef.current), |
| 78 | + generatedGarbageData, |
| 79 | + garbageCreatedComparisonGraphWidth, |
| 80 | + garbageCreatedComparisonGraphHeight, |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + return ( |
| 85 | + <> |
| 86 | + <h2>Benchmark results</h2> |
| 87 | + {/* Results table */} |
| 88 | + <table> |
| 89 | + <thead> |
| 90 | + <tr style={{ fontWeight: "bold" }}> |
| 91 | + <td>Benchmark</td> |
| 92 | + <td>Garbage Master</td> |
| 93 | + <td>Garbage Commit</td> |
| 94 | + <td>% Change</td> |
| 95 | + </tr> |
| 96 | + </thead> |
| 97 | + <tbody>{benchmarkResultsTable}</tbody> |
| 98 | + </table> |
| 99 | + |
| 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> |
| 115 | + </> |
| 116 | + ); |
| 117 | +} |
| 118 | + |
| 119 | +function decodeBenchmarkData(encodedData: string) { |
| 120 | + const results = JSON.parse(zlib.inflateSync(Buffer.from(encodedData, "base64")).toString()); |
| 121 | + |
| 122 | + const dataMaster = results.old as BenchmarkResult[]; |
| 123 | + const dataCommit = results.new as BenchmarkResult[]; |
| 124 | + |
| 125 | + // Match old/new results by name |
| 126 | + return joinOnProperty(dataMaster, dataCommit, (bm) => bm.benchmarkName); |
| 127 | +} |
0 commit comments