-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathBenchmarkRunner.swift
263 lines (237 loc) · 6.72 KB
/
BenchmarkRunner.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import Foundation
@_spi(RegexBenchmark) import _StringProcessing
/// The number of times to re-run the benchmark if results are too varying
private var rerunCount: Int { 3 }
extension Benchmark.MatchType {
fileprivate var nameSuffix: String {
switch self {
case .whole: return "_Whole"
case .first: return "_First"
case .allMatches: return "_All"
}
}
}
struct BenchmarkRunner {
let suiteName: String
var suite: [any RegexBenchmark] = []
let samples: Int
var results: SuiteResult = SuiteResult()
let quiet: Bool
let enableTracing: Bool
let enableMetrics: Bool
// Forcibly include firstMatch benchmarks for all CrossBenchmarks
let includeFirstOverride: Bool
// Register a cross-benchmark
mutating func registerCrossBenchmark(
nameBase: String,
input: String,
pattern: String,
_ type: Benchmark.MatchType,
alsoRunScalarSemantic: Bool = true
) {
let swiftRegex = try! Regex(pattern)
let nsRegex: NSRegularExpression
if type == .whole {
nsRegex = try! NSRegularExpression(pattern: "^" + pattern + "$")
} else {
nsRegex = try! NSRegularExpression(pattern: pattern)
}
let nameSuffix = type.nameSuffix
register(
Benchmark(
name: nameBase + nameSuffix,
regex: swiftRegex,
pattern: pattern,
type: type,
target: input))
register(
NSBenchmark(
name: nameBase + nameSuffix + CrossBenchmark.nsSuffix,
regex: nsRegex,
type: .init(type),
target: input))
if alsoRunScalarSemantic {
register(
Benchmark(
name: nameBase + nameSuffix + "_Scalar",
regex: swiftRegex.matchingSemantics(.unicodeScalar),
pattern: pattern,
type: type,
target: input))
register(
NSBenchmark(
name: nameBase + nameSuffix + "_Scalar" + CrossBenchmark.nsSuffix,
regex: nsRegex,
type: .init(type),
target: input))
}
}
// Register a cross-benchmark list
mutating func registerCrossBenchmark(
name: String,
inputList: [String],
pattern: String,
alsoRunScalarSemantic: Bool = true
) {
let swiftRegex = try! Regex(pattern)
register(InputListBenchmark(
name: name,
regex: swiftRegex,
pattern: pattern,
targets: inputList
))
register(InputListNSBenchmark(
name: name + CrossBenchmark.nsSuffix,
regex: pattern,
targets: inputList
))
if alsoRunScalarSemantic {
register(InputListBenchmark(
name: name + "_Scalar",
regex: swiftRegex.matchingSemantics(.unicodeScalar),
pattern: pattern,
targets: inputList
))
register(InputListNSBenchmark(
name: name + "_Scalar" + CrossBenchmark.nsSuffix,
regex: pattern,
targets: inputList
))
}
}
// Register a swift-only benchmark
mutating func register(
nameBase: String,
input: String,
pattern: String,
_ swiftRegex: Regex<AnyRegexOutput>,
_ type: Benchmark.MatchType,
alsoRunScalarSemantic: Bool = true
) {
let nameSuffix = type.nameSuffix
register(
Benchmark(
name: nameBase + nameSuffix,
regex: swiftRegex,
pattern: pattern,
type: type,
target: input))
if alsoRunScalarSemantic {
register(
Benchmark(
name: nameBase + nameSuffix + "_Scalar",
regex: swiftRegex,
pattern: pattern,
type: type,
target: input))
}
}
private mutating func register(_ benchmark: NSBenchmark) {
suite.append(benchmark)
}
private mutating func register(_ benchmark: Benchmark) {
var benchmark = benchmark
if enableTracing {
benchmark.enableTracing()
}
if enableMetrics {
benchmark.enableMetrics()
}
suite.append(benchmark)
}
private mutating func register(_ benchmark: InputListNSBenchmark) {
suite.append(benchmark)
}
private mutating func register(_ benchmark: InputListBenchmark) {
var benchmark = benchmark
if enableTracing {
benchmark.enableTracing()
}
if enableMetrics {
benchmark.enableMetrics()
}
suite.append(benchmark)
}
func medianMeasure(
samples: Int,
closure: () -> Void
) -> Measurement {
// FIXME: use suspendingclock?
var times: [Time] = []
for _ in 0..<samples {
let start = Tick.now
closure()
let end = Tick.now
let time = end.elapsedTime(since: start)
times.append(time)
}
return Measurement(results: times)
}
func measure(
benchmark: some RegexBenchmark,
samples: Int
) -> BenchmarkResult {
// Initial run to make sure the regex has been compiled
benchmark.run()
// Measure compilataion time for Swift regex
let compileTime: Measurement?
let parseTime: Measurement?
if benchmark is SwiftRegexBenchmark {
var benchmark = benchmark as! SwiftRegexBenchmark
compileTime = medianMeasure(samples: samples) { benchmark.compile() }
// Can't parse if we don't have an input string (ie a builder regex)
if benchmark.pattern != nil {
parseTime = medianMeasure(samples: samples) { let _ = benchmark.parse() }
} else {
parseTime = nil
}
} else {
compileTime = nil
parseTime = nil
}
let runtime = medianMeasure(samples: samples) { benchmark.run() }
return BenchmarkResult(
runtime: runtime,
compileTime: compileTime,
parseTime: parseTime)
}
mutating func run() {
print("Running")
for b in suite {
var result = measure(benchmark: b, samples: samples)
if result.runtimeIsTooVariant {
for _ in 0..<rerunCount {
print("Warning: Standard deviation > \(Stats.maxAllowedStdev*100)% for \(b.name)")
print(result.runtime)
print("Rerunning \(b.name)")
result = measure(benchmark: b, samples: result.runtime.samples*2)
print(result.runtime)
if !result.runtimeIsTooVariant {
break
}
}
if result.runtimeIsTooVariant {
fatalError("Benchmark \(b.name) is too variant")
}
}
if result.compileTime?.median ?? .zero > Time.millisecond {
print("Warning: Abnormally high compilation time, what happened?")
}
if result.parseTime?.median ?? .zero > Time.millisecond {
print("Warning: Abnormally high parse time, what happened?")
}
if !quiet {
print("- \(b.name)\n\(result)")
}
self.results.add(name: b.name, result: result)
}
}
mutating func debug() {
print("Debugging")
print("========================")
for b in suite {
b.debug()
print("========================")
}
}
}