Skip to content

Commit e8d273a

Browse files
committed
Change chart to be a normalized performance graph
A bad one because I have no idea how to use Swift Charts
1 parent 1b761be commit e8d273a

File tree

1 file changed

+22
-45
lines changed

1 file changed

+22
-45
lines changed

Sources/RegexBenchmark/BenchmarkChart.swift

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,30 @@ import SwiftUI
1717
struct BenchmarkChart: View {
1818
var comparisons: [BenchmarkResult.Comparison]
1919

20+
var sortedComparisons: [BenchmarkResult.Comparison] {
21+
comparisons.sorted { a, b in
22+
a.latest.median.seconds/a.baseline.median.seconds <
23+
b.latest.median.seconds/b.baseline.median.seconds
24+
}
25+
}
2026
var body: some View {
2127
VStack(alignment: .leading) {
22-
ForEach(comparisons) { comparison in
23-
let new = comparison.latest.median.seconds
24-
let old = comparison.baseline.median.seconds
25-
Chart {
28+
Chart {
29+
ForEach(sortedComparisons) { comparison in
30+
let new = comparison.latest.median.seconds
31+
let old = comparison.baseline.median.seconds
2632
chartBody(
2733
name: comparison.name,
2834
new: new,
2935
old: old,
3036
sampleCount: comparison.latest.samples)
3137
}
32-
.chartXAxis {
33-
AxisMarks { value in
34-
AxisTick()
35-
AxisValueLabel {
36-
Text(String(format: "%.5fs", value.as(Double.self)!))
37-
}
38-
}
39-
}
40-
.chartYAxis {
41-
AxisMarks { value in
42-
AxisGridLine()
43-
AxisValueLabel {
44-
HStack {
45-
Text(value.as(String.self)!)
46-
let delta = (new - old) / old * 100
47-
Text(String(format: "%+.2f%%", delta))
48-
.foregroundColor(delta <= 0 ? .green : .yellow)
49-
}
50-
}
51-
}
52-
}
53-
.frame(idealHeight: 60)
54-
}
38+
// Baseline
39+
RuleMark(y: .value("Time", 1.0))
40+
.foregroundStyle(.red)
41+
.lineStyle(.init(lineWidth: 1, dash: [2]))
42+
43+
}.frame(idealHeight: 400)
5544
}
5645
}
5746

@@ -62,27 +51,15 @@ struct BenchmarkChart: View {
6251
old: TimeInterval,
6352
sampleCount: Int
6453
) -> some ChartContent {
65-
// Baseline bar
66-
BarMark(
67-
x: .value("Time", old),
68-
y: .value("Name", "\(name) (\(sampleCount) samples)"))
69-
.position(by: .value("Kind", "Baseline"))
70-
.foregroundStyle(.gray)
71-
72-
// Latest result bar
54+
// Normalized runtime
7355
BarMark(
74-
x: .value("Time", new),
75-
y: .value("Name", "\(name) (\(sampleCount) samples)"))
76-
.position(by: .value("Kind", "Latest"))
56+
x: .value("Name", name),
57+
y: .value("Normalized runtime", new / old))
58+
7759
.foregroundStyle(LinearGradient(
7860
colors: [.accentColor, new - old <= 0 ? .green : .yellow],
79-
startPoint: .leading,
80-
endPoint: .trailing))
81-
82-
// Comparison
83-
RuleMark(x: .value("Time", new))
84-
.foregroundStyle(.gray)
85-
.lineStyle(.init(lineWidth: 0.5, dash: [2]))
61+
startPoint: .bottom,
62+
endPoint: .top))
8663
}
8764
}
8865

0 commit comments

Comments
 (0)