Skip to content
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

ratio between two related operations #226

Open
JaapWijnen opened this issue Jan 19, 2024 · 5 comments
Open

ratio between two related operations #226

JaapWijnen opened this issue Jan 19, 2024 · 5 comments

Comments

@JaapWijnen
Copy link

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?

@hassila
Copy link
Contributor

hassila commented Jan 20, 2024

Hi,

There's nothing built in for that, but there are a couple of things you could try:

  1. 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

  2. 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.

@JaapWijnen
Copy link
Author

JaapWijnen commented Feb 2, 2024

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

@hassila
Copy link
Contributor

hassila commented Feb 2, 2024

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)
        }
    }
}

@JaapWijnen
Copy link
Author

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

@hassila
Copy link
Contributor

hassila commented Feb 5, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants