Skip to content

Commit 74dd8e6

Browse files
committed
Add loading
1 parent 6ca5cfd commit 74dd8e6

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

Sources/RegexBenchmark/BenchmarkResults.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22

33
extension BenchmarkRunner {
4+
/// Attempts to save the results to the given path
45
func save(to savePath: String) throws {
56
let url = URL(fileURLWithPath: savePath, isDirectory: false)
67
let parent = url.deletingLastPathComponent()
@@ -10,7 +11,16 @@ extension BenchmarkRunner {
1011
print("Saving result to \(url.path)")
1112
try results.save(to: url)
1213
}
13-
14+
15+
/// Attempts to load the results from the given save file
16+
mutating func load(from savePath: String) throws {
17+
let url = URL(fileURLWithPath: savePath)
18+
let result = try SuiteResult.load(from: url)
19+
self.results = result
20+
print("Loaded results from \(url.path)")
21+
}
22+
23+
/// Compare this runner's results against the results stored in the given file path
1424
func compare(against compareFilePath: String, showChart: Bool, saveTo: String?) throws {
1525
let compareFileURL = URL(fileURLWithPath: compareFilePath)
1626
let compareResult = try SuiteResult.load(from: compareFileURL)
@@ -33,6 +43,7 @@ extension BenchmarkRunner {
3343
displayComparisons(compileTimeComparisons, false, against: "saved benchmark result " + compareFile)
3444
}
3545

46+
/// Compares Swift Regex benchmark results against NSRegularExpression
3647
func compareWithNS(showChart: Bool, saveTo: String?) throws {
3748
let comparisons = results.compareWithNS().filter({$0.diff != nil})
3849
displayComparisons(comparisons, showChart, against: "NSRegularExpression (via CrossBenchmark)")

Sources/RegexBenchmark/CLI.swift

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ struct Runner: ParsableCommand {
1010

1111
@Flag(help: "Debug benchmark regexes")
1212
var debug = false
13+
14+
@Option(help: "Load results from this file instead of rerunning")
15+
var load: String?
1316

1417
@Option(help: "The file results should be saved to")
1518
var save: String?
@@ -34,7 +37,7 @@ struct Runner: ParsableCommand {
3437

3538
mutating func run() throws {
3639
var runner = BenchmarkRunner.makeRunner(samples, quiet)
37-
40+
3841
if !self.specificBenchmarks.isEmpty {
3942
runner.suite = runner.suite.filter { b in
4043
specificBenchmarks.contains { pattern in
@@ -44,24 +47,29 @@ struct Runner: ParsableCommand {
4447
}
4548
if debug {
4649
runner.debug()
50+
return
51+
}
52+
53+
if let loadFile = load {
54+
try runner.load(from: loadFile)
4755
} else {
4856
if excludeNs {
4957
runner.suite = runner.suite.filter { b in !b.name.contains("NS") }
5058
}
5159
runner.run()
52-
if let saveFile = save {
53-
try runner.save(to: saveFile)
54-
}
55-
if saveComparison != nil && compareWithNS && compare != nil {
56-
print("Unable to save both comparison results, specify only one compare operation")
57-
return
58-
}
59-
if compareWithNS {
60-
try runner.compareWithNS(showChart: showChart, saveTo: saveComparison)
61-
}
62-
if let compareFile = compare {
63-
try runner.compare(against: compareFile, showChart: showChart, saveTo: saveComparison)
64-
}
60+
}
61+
if let saveFile = save {
62+
try runner.save(to: saveFile)
63+
}
64+
if saveComparison != nil && compareWithNS && compare != nil {
65+
print("Unable to save both comparison results, specify only one compare operation")
66+
return
67+
}
68+
if compareWithNS {
69+
try runner.compareWithNS(showChart: showChart, saveTo: saveComparison)
70+
}
71+
if let compareFile = compare {
72+
try runner.compare(against: compareFile, showChart: showChart, saveTo: saveComparison)
6573
}
6674
}
6775
}

0 commit comments

Comments
 (0)