Skip to content

Commit d36e6a6

Browse files
authored
Revert "Add more benchmarks and benchmarker functionality (#505)"
This reverts commit e0af639.
1 parent e0af639 commit d36e6a6

File tree

11 files changed

+82
-2931
lines changed

11 files changed

+82
-2931
lines changed

Sources/RegexBenchmark/Benchmark.swift

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import Foundation
44
public protocol RegexBenchmark {
55
var name: String { get }
66
func run()
7-
func debug()
87
}
98

109
public struct Benchmark: RegexBenchmark {
1110
public let name: String
12-
let regex: Regex<AnyRegexOutput>
11+
let regex: Regex<Substring>
1312
let type: MatchType
1413
let target: String
1514

@@ -51,6 +50,66 @@ public struct NSBenchmark: RegexBenchmark {
5150
}
5251
}
5352

53+
public struct BenchmarkRunner {
54+
// Register instances of Benchmark and run them
55+
let suiteName: String
56+
var suite: [any RegexBenchmark]
57+
let samples: Int
58+
59+
public init(_ suiteName: String) {
60+
self.suiteName = suiteName
61+
self.suite = []
62+
self.samples = 20
63+
}
64+
65+
public init(_ suiteName: String, _ n: Int) {
66+
self.suiteName = suiteName
67+
self.suite = []
68+
self.samples = n
69+
}
70+
71+
public mutating func register(_ new: some RegexBenchmark) {
72+
suite.append(new)
73+
}
74+
75+
func measure(benchmark: some RegexBenchmark) -> Time {
76+
var times: [Time] = []
77+
78+
// initial run to make sure the regex has been compiled
79+
benchmark.run()
80+
81+
// fixme: use suspendingclock?
82+
for _ in 0..<samples {
83+
let start = Tick.now
84+
benchmark.run()
85+
let end = Tick.now
86+
let time = end.elapsedTime(since: start)
87+
times.append(time)
88+
}
89+
// todo: compute stdev and warn if it's too large
90+
91+
// return median time
92+
times.sort()
93+
return times[samples/2]
94+
}
95+
96+
public func run() {
97+
print("Running")
98+
for b in suite {
99+
print("- \(b.name) \(measure(benchmark: b))")
100+
}
101+
}
102+
103+
public func profile() {
104+
print("Starting")
105+
for b in suite {
106+
print("- \(b.name)")
107+
b.run()
108+
print("- done")
109+
}
110+
}
111+
}
112+
54113
/// A benchmark meant to be ran across multiple engines
55114
struct CrossBenchmark {
56115
/// The base name of the benchmark
@@ -71,7 +130,7 @@ struct CrossBenchmark {
71130
var isWhole: Bool = false
72131

73132
func register(_ runner: inout BenchmarkRunner) {
74-
let swiftRegex = try! Regex(regex)
133+
let swiftRegex = try! Regex(regex, as: Substring.self)
75134

76135
let nsPattern = isWhole ? "^" + regex + "$" : regex
77136
let nsRegex: NSRegularExpression

Sources/RegexBenchmark/BenchmarkRunner.swift

Lines changed: 0 additions & 178 deletions
This file was deleted.

Sources/RegexBenchmark/CLI.swift

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,30 @@ struct Runner: ParsableCommand {
55
@Argument(help: "Names of benchmarks to run")
66
var specificBenchmarks: [String] = []
77

8-
@Flag(help: "Run only once for profiling purposes")
8+
@Option(help: "Run only once for profiling purposes")
99
var profile = false
1010

1111
@Option(help: "How many samples to collect for each benchmark")
1212
var samples = 20
13-
14-
@Flag(help: "Debug benchmark regexes")
15-
var debug = false
16-
17-
@Option(help: "Output folder")
18-
var outputPath = "./results/"
19-
20-
@Flag(help: "Should the results be saved")
21-
var save = false
22-
23-
@Flag(help: "Compare this result with the latest saved result")
24-
var compare = false
2513

2614
func makeRunner() -> BenchmarkRunner {
27-
var benchmark = BenchmarkRunner("RegexBench", samples, outputPath)
15+
var benchmark = BenchmarkRunner("RegexBench", samples)
2816
benchmark.addReluctantQuant()
2917
benchmark.addCSS()
3018
benchmark.addNotFound()
3119
benchmark.addGraphemeBreak()
3220
benchmark.addHangulSyllable()
33-
benchmark.addHTML()
34-
benchmark.addEmail()
3521
return benchmark
3622
}
37-
3823
mutating func run() throws {
3924
var runner = makeRunner()
4025
if !self.specificBenchmarks.isEmpty {
4126
runner.suite = runner.suite.filter { b in specificBenchmarks.contains(b.name) }
4227
}
43-
switch (profile, debug) {
44-
case (true, true): print("Cannot run both profile and debug")
45-
case (true, false): runner.profile()
46-
case (false, true): runner.debug()
47-
case (false, false):
28+
if profile {
29+
runner.profile()
30+
} else {
4831
runner.run()
49-
if compare {
50-
try runner.compare()
51-
}
52-
if save {
53-
try runner.save()
54-
}
5532
}
5633
}
5734
}

0 commit comments

Comments
 (0)