Skip to content

Revert "Add more benchmarks and benchmarker functionality" #507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 62 additions & 3 deletions Sources/RegexBenchmark/Benchmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import Foundation
public protocol RegexBenchmark {
var name: String { get }
func run()
func debug()
}

public struct Benchmark: RegexBenchmark {
public let name: String
let regex: Regex<AnyRegexOutput>
let regex: Regex<Substring>
let type: MatchType
let target: String

Expand Down Expand Up @@ -51,6 +50,66 @@ public struct NSBenchmark: RegexBenchmark {
}
}

public struct BenchmarkRunner {
// Register instances of Benchmark and run them
let suiteName: String
var suite: [any RegexBenchmark]
let samples: Int

public init(_ suiteName: String) {
self.suiteName = suiteName
self.suite = []
self.samples = 20
}

public init(_ suiteName: String, _ n: Int) {
self.suiteName = suiteName
self.suite = []
self.samples = n
}

public mutating func register(_ new: some RegexBenchmark) {
suite.append(new)
}

func measure(benchmark: some RegexBenchmark) -> Time {
var times: [Time] = []

// initial run to make sure the regex has been compiled
benchmark.run()

// fixme: use suspendingclock?
for _ in 0..<samples {
let start = Tick.now
benchmark.run()
let end = Tick.now
let time = end.elapsedTime(since: start)
times.append(time)
}
// todo: compute stdev and warn if it's too large

// return median time
times.sort()
return times[samples/2]
}

public func run() {
print("Running")
for b in suite {
print("- \(b.name) \(measure(benchmark: b))")
}
}

public func profile() {
print("Starting")
for b in suite {
print("- \(b.name)")
b.run()
print("- done")
}
}
}

/// A benchmark meant to be ran across multiple engines
struct CrossBenchmark {
/// The base name of the benchmark
Expand All @@ -71,7 +130,7 @@ struct CrossBenchmark {
var isWhole: Bool = false

func register(_ runner: inout BenchmarkRunner) {
let swiftRegex = try! Regex(regex)
let swiftRegex = try! Regex(regex, as: Substring.self)

let nsPattern = isWhole ? "^" + regex + "$" : regex
let nsRegex: NSRegularExpression
Expand Down
178 changes: 0 additions & 178 deletions Sources/RegexBenchmark/BenchmarkRunner.swift

This file was deleted.

33 changes: 5 additions & 28 deletions Sources/RegexBenchmark/CLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,30 @@ struct Runner: ParsableCommand {
@Argument(help: "Names of benchmarks to run")
var specificBenchmarks: [String] = []

@Flag(help: "Run only once for profiling purposes")
@Option(help: "Run only once for profiling purposes")
var profile = false

@Option(help: "How many samples to collect for each benchmark")
var samples = 20

@Flag(help: "Debug benchmark regexes")
var debug = false

@Option(help: "Output folder")
var outputPath = "./results/"

@Flag(help: "Should the results be saved")
var save = false

@Flag(help: "Compare this result with the latest saved result")
var compare = false

func makeRunner() -> BenchmarkRunner {
var benchmark = BenchmarkRunner("RegexBench", samples, outputPath)
var benchmark = BenchmarkRunner("RegexBench", samples)
benchmark.addReluctantQuant()
benchmark.addCSS()
benchmark.addNotFound()
benchmark.addGraphemeBreak()
benchmark.addHangulSyllable()
benchmark.addHTML()
benchmark.addEmail()
return benchmark
}

mutating func run() throws {
var runner = makeRunner()
if !self.specificBenchmarks.isEmpty {
runner.suite = runner.suite.filter { b in specificBenchmarks.contains(b.name) }
}
switch (profile, debug) {
case (true, true): print("Cannot run both profile and debug")
case (true, false): runner.profile()
case (false, true): runner.debug()
case (false, false):
if profile {
runner.profile()
} else {
runner.run()
if compare {
try runner.compare()
}
if save {
try runner.save()
}
}
}
}
Loading