Skip to content

Commit 18d5ebb

Browse files
authored
Fix benchmark visualizer (#104)
1 parent e998c4c commit 18d5ebb

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

src/pages/benchviz/Benchmark.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import { joinOnProperty, JoinResult } from "./util";
1515
import { barComparisonGraph } from "./visualizations/bar-comparison-graph";
1616
import { positiveNegativeBarGraph } from "./visualizations/positive-negative-bar-graph";
1717

18-
const comparisonGraphWidth = 1000;
18+
const comparisonGraphWidth = 1200;
1919
const comparisonGraphHeight = 300;
2020

21-
const changeGraphWidth = 1000;
21+
const changeGraphWidth = 1200;
2222
const changeGraphHeight = 300;
2323

2424
// Utility functions
@@ -53,7 +53,7 @@ function BenchmarkCategory<T extends BenchmarkResult>({
5353
// Populate graph with benchmark results
5454
const memoryBenchmarksResultTable = benchmarksSortedByPercentageDiff.map((bm, i) => {
5555
const change = percentChangeForResult(bm);
56-
const rowColor = change === 0 ? "currentColor" : change > 0 ? "red" : "green";
56+
const rowColor = change <= -0.01 ? "green" : change >= 0.01 ? "red" : "currentColor";
5757

5858
return (
5959
<tr key={i} style={{ color: rowColor }}>
@@ -133,7 +133,7 @@ export default function Benchmark() {
133133
<BenchmarkCategory
134134
name={"Runtime"}
135135
benchmarks={benchmarkData.runtime}
136-
extractValue={(bm) => bm.time}
136+
extractValue={(bm) => bm.time * 1000}
137137
formatValue={formatTime}
138138
/>
139139
</>

src/pages/benchviz/visualizations/bar-comparison-graph.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function barComparisonGraph(
2727
.range([GRAPH_MARGIN.left, width - GRAPH_MARGIN.right]);
2828

2929
const bandWidth = xScale.bandwidth();
30-
const barWidth = 25;
30+
const barWidth = Math.min(25, (0.3 * width) / data.length);
3131

3232
const xAxis = d3.axisBottom(xScale);
3333
selection.append("g").attr("transform", `translate(0, ${barMaxHeight})`).call(xAxis);
@@ -47,23 +47,32 @@ export function barComparisonGraph(
4747
// Create bars for each entry
4848
const entries = selection.selectAll("rect").data(data).enter();
4949

50+
const formatTooltip = (d: ComparisonData) =>
51+
`${d.name}\nOld: ${d.oldValue.toFixed(2)}\nNew: ${d.newValue.toFixed(2)}`;
52+
5053
entries
5154
.append("rect")
5255
.attr("width", barWidth)
5356
.attr("x", (d) => xScale(d.name)! + 0.5 * bandWidth - barWidth - 1)
5457
.attr("height", (d) => barMaxHeight - yScale(d.oldValue)!)
5558
.attr("y", (d) => yScale(d.oldValue)!)
56-
.style("fill", COLOR_OLD);
57-
//.style("stroke", "currentColor");
59+
.style("fill", COLOR_OLD)
60+
.style("stroke", "currentColor")
61+
// Add hover tooltip
62+
.append("svg:title")
63+
.text(formatTooltip);
5864

5965
entries
6066
.append("rect")
6167
.attr("width", barWidth)
6268
.attr("x", (d) => xScale(d.name)! + 0.5 * bandWidth + 1)
6369
.attr("height", (d) => barMaxHeight - yScale(d.newValue)!)
6470
.attr("y", (d) => yScale(d.newValue)!)
65-
.style("fill", COLOR_NEW);
66-
//.style("stroke", "currentColor");
71+
.style("fill", COLOR_NEW)
72+
.style("stroke", "currentColor")
73+
// Add hover tooltip
74+
.append("svg:title")
75+
.text(formatTooltip);
6776

6877
// Add legend
6978
const legendEntries: Array<[string, string]> = [

src/pages/benchviz/visualizations/positive-negative-bar-graph.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ export function positiveNegativeBarGraph(
1616
const minValue = d3.min(data.map((bm) => bm.value))!;
1717
const maxValue = d3.max(data.map((bm) => bm.value))!;
1818

19+
const range = maxValue - minValue;
20+
1921
const yScale = d3
2022
.scaleLinear()
21-
.domain([minValue * 1.2, maxValue * 1.2])
23+
.domain([minValue - 0.2 * range, maxValue + 0.2 * range])
2224
.range([0, height - 10]);
2325

2426
const yAxis = d3.axisLeft(yScale);
@@ -29,7 +31,7 @@ export function positiveNegativeBarGraph(
2931
.range([GRAPH_MARGIN.left, width - GRAPH_MARGIN.right]);
3032

3133
const bandWidth = xScale.bandwidth();
32-
const barWidth = 25;
34+
const barWidth = Math.min(25, (0.5 * width) / data.length);
3335

3436
const xAxis = d3.axisBottom(xScale);
3537

@@ -40,25 +42,27 @@ export function positiveNegativeBarGraph(
4042

4143
selection.append("g").attr("transform", `translate(${GRAPH_MARGIN.left}, ${GRAPH_MARGIN.top})`).call(yAxis);
4244

43-
const barSize = (val: number) => Math.abs(height / 2 - yScale(val)!);
45+
const barSize = (val: number) => Math.abs(yScale(0)! - yScale(val)!);
4446

4547
const bars = selection.selectAll("rect").data(data).enter();
4648

4749
bars.append("rect")
4850
.attr("width", barWidth)
4951
.attr("x", (d) => xScale(d.name)! + 0.5 * bandWidth - 0.5 * barWidth)
50-
.attr("height", (d) => barSize(d.value) - 1)
51-
.attr("y", (d) => (d.value > 0 ? height / 2 + 10 : height / 2 + 10 - barSize(d.value)))
52-
.style("fill", (d) => (d.value > 0 ? "red" : "green"));
53-
//.style("stroke", "currentColor");
52+
.attr("height", (d) => barSize(d.value))
53+
.attr("y", (d) => GRAPH_MARGIN.top + (d.value > 0 ? yScale(0)! : yScale(0)! - barSize(d.value)))
54+
.style("fill", (d) => (d.value > 0 ? "red" : "green"))
55+
.style("stroke", "currentColor")
56+
// Add hover tooltip
57+
.append("svg:title")
58+
.text((d) => `${d.name}: ${d.value.toFixed(2)}%`);
5459

5560
bars.append("text")
5661
.text((d) => `${d.value > 0 ? "+" : ""}${d.value.toFixed(2)}%`)
5762
.attr("x", (d) => xScale(d.name)! + 0.5 * bandWidth)
58-
.attr("y", (d) => (d.value > 0 ? height / 2 + barSize(d.value) + 30 : height / 2 - barSize(d.value)))
63+
.attr("y", (d) => (d.value > 0 ? Math.max(yScale(0)! + 40, yScale(d.value)! + 20) : yScale(d.value)!))
5964
.style("fill", "currentColor")
6065
.style("text-anchor", "middle");
61-
//.style("stroke", "currentColor");
6266

6367
return selection;
6468
}

src/pages/index.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ interface Feature {
1212

1313
const features: Feature[] = [
1414
{
15-
title: "Extend existing APIs",
15+
title: "Declare and use existing APIs",
1616
description: (
1717
<>
18-
This project is useful in any environment where Lua code is accepted, with the powerful option of simply
19-
declaring any existing API using TypeScript declaration files.
18+
This project is useful in any environment where Lua code is accepted, with the powerful option of
19+
declaring types for any existing API using TypeScript declaration files.
2020
</>
2121
),
2222
},
@@ -43,7 +43,7 @@ const features: Feature[] = [
4343
];
4444

4545
const exampleSource = `
46-
function onAbilityCast(this: void, caster: Unit, targetPos: Vector) {
46+
function onAbilityCast(caster: Unit, targetPos: Vector) {
4747
const units = findUnitsInRadius(targetPos, 500);
4848
4949
const enemies = units.filter(unit => caster.isEnemy(unit));

0 commit comments

Comments
 (0)