/ concept-collection / benchcompress
Sign in
concept-collection / benchcompress
benchcompress / web-ui / src / components / benchmark / charts / BenchmarkCharts.tsx
187 lines · 5.6 KBCodeBlameHistory
70a7a71code formattingJeremy Magland 1import { useState } from "react";
a2daa82reference compression ratiosJeremy Magland 2import Plot from "react-plotly.js";
eacabc8test real dataJeremy Magland 3
05ee84fimprove bar graphsJeremy Magland 4interface BenchmarkBarChartProps {
5 title: string;
6 data: ChartData[];
7 dataKey: keyof Pick<
8 ChartData,
9 "compression_ratio" | "encode_speed" | "decode_speed"
10 >;
11 color: string;
12 xAxisTitle: string;
7245151normalize to best compressionJeremy Magland 13 normalize?: boolean;
05ee84fimprove bar graphsJeremy Magland 14}
16function BenchmarkBarChart({
17 title,
18 data,
19 dataKey,
20 color,
21 xAxisTitle,
7245151normalize to best compressionJeremy Magland 22 normalize,
05ee84fimprove bar graphsJeremy Magland 23}: BenchmarkBarChartProps) {
7245151normalize to best compressionJeremy Magland 24 const normalizedData =
25 normalize && dataKey === "compression_ratio"
26 ? data.map((d) => ({
27 ...d,
28 compression_ratio: d.reference_compression_ratio
29 ? d.compression_ratio / d.reference_compression_ratio
30 : d.compression_ratio,
31 reference_compression_ratio: d.reference_compression_ratio ? 1 : null,
32 }))
33 : data;
05ee84fimprove bar graphsJeremy Magland 35 return (
36 <div style={{ margin: "0 20px 20px 0" }}>
37 <h3 style={{ marginBottom: "10px" }}>{title}</h3>
38 <Plot
39 data={[
40 {
41 type: "bar",
42 orientation: "h",
7245151normalize to best compressionJeremy Magland 43 y: normalizedData.map((d) => d.algorithmOrDataset),
44 x: normalizedData.map((d) => d[dataKey]),
05ee84fimprove bar graphsJeremy Magland 45 marker: { color },
a2daa82reference compression ratiosJeremy Magland 46 name: title,
7245151normalize to best compressionJeremy Magland 47 hovertemplate:
48 normalize && dataKey === "compression_ratio"
49 ? "%{x:.3f}×<extra></extra>"
50 : "%{x:.2f}<extra></extra>",
05ee84fimprove bar graphsJeremy Magland 51 },
a2daa82reference compression ratiosJeremy Magland 52 ...(dataKey === "compression_ratio" &&
7245151normalize to best compressionJeremy Magland 53 normalizedData.some((d) => d.reference_compression_ratio !== null)
a2daa82reference compression ratiosJeremy Magland 54 ? [
7245151normalize to best compressionJeremy Magland 55 ...normalizedData
a2daa82reference compression ratiosJeremy Magland 56 .filter((d) => d.reference_compression_ratio !== null)
57 .flatMap((d) => [
58 {
59 type: "scatter" as const,
60 mode: "lines" as const,
61 y: [d.algorithmOrDataset, d.algorithmOrDataset],
62 x: [0, d.reference_compression_ratio],
7245151normalize to best compressionJeremy Magland 63 line: { color, width: 1 },
a2daa82reference compression ratiosJeremy Magland 64 showlegend: false,
65 hoverinfo: "skip" as const,
66 },
67 {
68 type: "scatter" as const,
69 mode: "markers" as const,
70 y: [d.algorithmOrDataset],
71 x: [d.reference_compression_ratio],
72 marker: { color: "#aaaaaa", size: 8 },
73 name: "Best Compression",
7245151normalize to best compressionJeremy Magland 74 hovertemplate: normalize
75 ? "Best: 1.000×<extra></extra>"
76 : "Best: %{x:.2f}<extra></extra>",
a2daa82reference compression ratiosJeremy Magland 77 showlegend:
7245151normalize to best compressionJeremy Magland 78 d.algorithmOrDataset ===
79 normalizedData[0].algorithmOrDataset,
a2daa82reference compression ratiosJeremy Magland 80 },
81 ]),
82 ]
83 : []),
05ee84fimprove bar graphsJeremy Magland 84 ]}
85 layout={{
86 width: 700,
87 height: Math.max(300, data.length * 23 + 40),
88 margin: { t: 5, r: 30, l: 200, b: 30 },
89 xaxis: { title: xAxisTitle },
90 yaxis: { automargin: true, ticksuffix: " " },
91 dragmode: false,
92 }}
93 config={{ displayModeBar: false }}
94 />
95 </div>
96 );
eacabc8test real dataJeremy Magland 99interface ChartData {
a2daa82reference compression ratiosJeremy Magland 100 algorithmOrDataset: string;
eacabc8test real dataJeremy Magland 101 compression_ratio: number;
a2daa82reference compression ratiosJeremy Magland 102 reference_compression_ratio: number | null; // the highest compression ratio for the dataset (if algorithmOrDataset is a dataset)
eacabc8test real dataJeremy Magland 103 encode_speed: number;
104 decode_speed: number;
107interface BenchmarkChartsProps {
108 chartData: ChartData[];
a2daa82reference compression ratiosJeremy Magland 109 showSortByCompressionRatio?: boolean;
7245151normalize to best compressionJeremy Magland 110 showNormalizeByReference?: boolean;
eacabc8test real dataJeremy Magland 111}
a2daa82reference compression ratiosJeremy Magland 113export function BenchmarkCharts({
114 chartData,
115 showSortByCompressionRatio,
7245151normalize to best compressionJeremy Magland 116 showNormalizeByReference,
a2daa82reference compression ratiosJeremy Magland 117}: BenchmarkChartsProps) {
118 const [sortByRatio, setSortByRatio] = useState(
119 showSortByCompressionRatio ? true : false,
120 );
7245151normalize to best compressionJeremy Magland 121 const [normalize, setNormalize] = useState(false);
eacabc8test real dataJeremy Magland 123 if (!chartData.length) return null;
8a6e869delta encoding for other algsJeremy Magland 125 const sortedData = sortByRatio
126 ? [...chartData].sort((a, b) => a.compression_ratio - b.compression_ratio)
127 : chartData;
eacabc8test real dataJeremy Magland 129 return (
8a6e869delta encoding for other algsJeremy Magland 130 <div>
a2daa82reference compression ratiosJeremy Magland 131 {showSortByCompressionRatio && (
132 <div style={{ marginBottom: "10px" }}>
133 <label style={{ display: "flex", alignItems: "center", gap: "8px" }}>
134 <input
135 type="checkbox"
136 checked={sortByRatio}
137 onChange={(e) => setSortByRatio(e.target.checked)}
138 />
139 Sort by compression ratio
140 </label>
141 </div>
142 )}
7245151normalize to best compressionJeremy Magland 143 {showNormalizeByReference && (
144 <div style={{ marginBottom: "10px" }}>
145 <label style={{ display: "flex", alignItems: "center", gap: "8px" }}>
146 <input
147 type="checkbox"
148 checked={normalize}
149 onChange={(e) => setNormalize(e.target.checked)}
150 />
151 Normalize to best compression
152 </label>
153 </div>
154 )}
05ee84fimprove bar graphsJeremy Magland 155 <div
156 style={{
157 display: "flex",
158 flexWrap: "wrap",
159 gap: "20px",
160 }}
161 >
162 <BenchmarkBarChart
163 title="Compression Ratio"
164 data={sortedData}
165 dataKey="compression_ratio"
166 color="#8884d8"
7245151normalize to best compressionJeremy Magland 167 xAxisTitle={normalize ? "Fraction of Best Compression" : "Ratio"}
168 normalize={normalize}
05ee84fimprove bar graphsJeremy Magland 169 />
170 <BenchmarkBarChart
171 title="Encode Speed (MB/s)"
172 data={sortedData}
173 dataKey="encode_speed"
174 color="#82ca9d"
175 xAxisTitle="MB/s"
176 />
177 <BenchmarkBarChart
178 title="Decode Speed (MB/s)"
179 data={sortedData}
180 dataKey="decode_speed"
181 color="#ff7300"
182 xAxisTitle="MB/s"
183 />
8a6e869delta encoding for other algsJeremy Magland 184 </div>
eacabc8test real dataJeremy Magland 185 </div>
186 );
moveopenescclose