Skip to content

Commit fb7459d

Browse files
committed
Add basic CLI
1 parent 1384a69 commit fb7459d

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

Sources/RegexBenchmark/Benchmark.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,18 @@ public struct BenchmarkRunner {
5454
// Register instances of Benchmark and run them
5555
let suiteName: String
5656
var suite: [any RegexBenchmark]
57-
let samples: Int = 20
57+
let samples: Int
5858

59-
public init(suiteName: String) {
59+
public init(_ suiteName: String) {
6060
self.suiteName = suiteName
6161
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
6269
}
6370

6471
public mutating func register(_ new: some RegexBenchmark) {
@@ -101,6 +108,15 @@ public struct BenchmarkRunner {
101108
print("- \(b.name) \(measure(benchmark: b))")
102109
}
103110
}
111+
112+
public func profile() {
113+
print("Starting")
114+
for b in suite {
115+
print("- \(b.name)")
116+
b.run()
117+
print("- done")
118+
}
119+
}
104120
}
105121

106122
// nom nom nom, consume the argument

Sources/RegexBenchmark/CLI.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import ArgumentParser
2+
3+
@main
4+
struct Runner: ParsableCommand {
5+
@Argument(help: "Names of benchmarks to run")
6+
var specificBenchmarks: [String] = []
7+
8+
@Option(help: "Run only once for profiling purposes")
9+
var profile = false
10+
11+
@Option(help: "How many samples to collect for each benchmark")
12+
var samples = 20
13+
14+
func makeRunner() -> BenchmarkRunner {
15+
var benchmark = BenchmarkRunner("RegexBench", samples)
16+
benchmark.addReluctantQuant()
17+
benchmark.addBacktracking()
18+
benchmark.addCSS()
19+
benchmark.addFirstMatch()
20+
return benchmark
21+
}
22+
mutating func run() throws {
23+
var runner = makeRunner()
24+
if !self.specificBenchmarks.isEmpty {
25+
runner.suite = runner.suite.filter { b in specificBenchmarks.contains(b.name) }
26+
}
27+
if profile {
28+
runner.profile()
29+
} else {
30+
runner.run()
31+
}
32+
}
33+
}

Sources/RegexBenchmark/main.swift

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

0 commit comments

Comments
 (0)