Open JaapWijnen opened 10 months ago
Hi,
There's nothing built in for that, but there are a couple of things you could try:
Generate two benchmarks runs (just parameterize the benchmark) and use start/stop measuring to get eg. A/B measurements - possibly ok if you runtime isn't too bad - statistically it'll give you something useful. Then export this in one of the supported formats and calculate the ratio
You could also try with custom measurements using the BenchmarkClock.now and register custom metrics for A/B/ratio as you suggest - that's probably cleaner and what I'd try first. Then you measure the time yourself basically.
I've taken the second approach and have the following extension on Benchmark now:
extension Benchmark {
@discardableResult
convenience init?(_ name: String, forward: @escaping (Benchmark) -> (), reverse: @escaping (Benchmark) -> ()) {
self.init(name, configuration: .init(metrics: [CustomMeasurement.forward, CustomMeasurement.reverse, CustomMeasurement.ratio])) { benchmark in
let startForward = BenchmarkClock.now
forward(benchmark)
let endForward = BenchmarkClock.now
let startReverse = BenchmarkClock.now
reverse(benchmark)
let endReverse = BenchmarkClock.now
let forward = Int((endForward - startForward).nanoseconds())
let reverse = Int((endReverse - startReverse).nanoseconds())
benchmark.measurement(CustomMeasurement.forward, forward)
benchmark.measurement(CustomMeasurement.reverse, reverse)
benchmark.measurement(CustomMeasurement.ratio, reverse / forward)
}
}
}
Only downside here is that we can only measure integer values so can't turn the ratio into a display of factors of say 1.5 etc. Any ideas and/or tips on that? (or the implementation itself)
Edit: removed the scaled iterations loop around the forward, reverse closure calls from the implementation since that was optimising out the entire closure
How about keeping microseconds for forward/reverse, but nanoseconds for the ratio?
Need to tweak the runtime of the measurement such that microseconds (or more) are suitable though. (On mobile device, but seems ChatGPT helped format reasonably)
Something like:
extension Benchmark {
@discardableResult
convenience init?(_ name: String, forward: @escaping (Benchmark) -> (), reverse: @escaping (Benchmark) -> ()) {
self.init(name, configuration: .init(metrics: [CustomMeasurement.forward, CustomMeasurement.reverse, CustomMeasurement.ratio])) { benchmark in
let startForward = BenchmarkClock.now
forward(benchmark)
let endForward = BenchmarkClock.now
let startReverse = BenchmarkClock.now
reverse(benchmark)
let endReverse = BenchmarkClock.now
// Calculate times in nanoseconds
let forwardTimeNanos = (endForward - startForward).nanoseconds()
let reverseTimeNanos = (endReverse - startReverse).nanoseconds()
// Convert times to microseconds for storage
let forwardTimeMicros = forwardTimeNanos / 1000
let reverseTimeMicros = reverseTimeNanos / 1000
// Ensure forwardTimeNanos is not zero to avoid division by zero
guard forwardTimeNanos != 0 else {
print("Forward time in nanoseconds is zero, cannot compute ratio.")
return
}
// Calculate ratio using nanosecond precision
let ratio = reverseTimeNanos / forwardTimeNanos
benchmark.measurement(CustomMeasurement.forward, forwardTimeMicros)
benchmark.measurement(CustomMeasurement.reverse, reverseTimeMicros)
benchmark.measurement(CustomMeasurement.ratio, ratio)
}
}
}
That still doesn't allow to display a ratio of 1.5 right? So in terms of precision if execution time is 2,000,000 ns vs 3,000,000 the ratio will still be 1.5 regardless of precision
Right, only integer samples - but in that case you would store 2,000(us) vs 3,000(us) with 1,500,000ns for your custom measurements, so you'd see the fractional part in that case.
Another approach is to just export the raw data in one of the output formats and do your own post processing for that specific case.
Hey there! I was hoping to use the package to measure the wall clock time of two related operations in a single benchmark. So that I can report on both their execution times and also return the ratio between them as a result of that single benchmark. What is the best way to go around this? I know I can make custom metrics but I'm not sure if it's possible to do two time measurements within one benchmark?