To start, I want to thank @bheisler for all of the effort you've put into criterion and iai!!
I've been experimenting with iai and really like the notion of "one-shot" measuring for low level benchmarks. I've played round with it, but sometimes I get unexpected results. This could definitely be an error on my part, that is usually the case, but I've been unable to track it down and thus I've created this issue.
Of note, I get very consistent results if I do multiple runs of a single configuration. But sometimes I run into problems when I change something or run a slightly different command. I then can get results that look wrong to me.
First off, I use an Arch Linux system for development:
And here are two more runs of just bench_iai showing the expected consistency:
$ cargo bench --bench bench_iai
Finished bench [optimized] target(s) in 0.02s
Running benches/bench_iai.rs (target/release/deps/bench_iai-e75a6910d1576500)
bench_iai_add
Instructions: 8 (No change)
L1 Accesses: 12 (No change)
L2 Accesses: 2 (No change)
RAM Accesses: 2 (No change)
Estimated Cycles: 92 (No change)
$ cargo bench --bench bench_iai
Finished bench [optimized] target(s) in 0.02s
Running benches/bench_iai.rs (target/release/deps/bench_iai-e75a6910d1576500)
bench_iai_add
Instructions: 8 (No change)
L1 Accesses: 12 (No change)
L2 Accesses: 2 (No change)
RAM Accesses: 2 (No change)
Estimated Cycles: 92 (No change)
Here is a my first unexpected result, if I change the command line, adding taskset -c 0, I wouldn't expect significantly different results, but Instructions is 0, that is unexpected:
$ taskset -c 0 cargo bench --bench bench_iai
Finished bench [optimized] target(s) in 0.02s
Running benches/bench_iai.rs (target/release/deps/bench_iai-e75a6910d1576500)
bench_iai_add
Instructions: 0 (-100.0000%)
L1 Accesses: 18446744073709551615 (+153722867280912908288%)
L2 Accesses: 2 (No change)
RAM Accesses: 3 (+50.00000%)
Estimated Cycles: 114 (+23.91304%)
$ taskset -c 0 cargo bench --bench bench_iai
Finished bench [optimized] target(s) in 0.02s
Running benches/bench_iai.rs (target/release/deps/bench_iai-e75a6910d1576500)
bench_iai_add
Instructions: 0 (No change)
L1 Accesses: 18446744073709551615 (No change)
L2 Accesses: 2 (No change)
RAM Accesses: 3 (No change)
Estimated Cycles: 114 (No change)
$ taskset -c 0 cargo bench --bench bench_iai
Finished bench [optimized] target(s) in 0.02s
Running benches/bench_iai.rs (target/release/deps/bench_iai-e75a6910d1576500)
bench_iai_add
Instructions: 0 (No change)
L1 Accesses: 18446744073709551615 (No change)
L2 Accesses: 2 (No change)
RAM Accesses: 3 (No change)
Estimated Cycles: 114 (No change)
But a bigger problem is if rename bench_iai.rs to iai.rs and the bench within that file from
bench_iai_add to iai_add:
And then I make the necessary changes to get things working again, see exper-iai branch rename-bench_iai_add-to-iai_add. In my opinion only "labels" have changed and none of the actual assembler code has changed.
But now I get really unexpected results, I switch branches and then clean and rerun bench and now Instructions has a value of 22:
$ git switch main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
$ git switch rename-bench_iai_add-to-iai-add
Switched to branch 'rename-bench_iai_add-to-iai-add'
$ cargo clean
$ cargo bench
Compiling autocfg v1.1.0
Compiling proc-macro2 v1.0.47
...
Compiling tinytemplate v1.2.1
Compiling criterion v0.4.0
Finished bench [optimized] target(s) in 20.60s
Running unittests src/lib.rs (target/release/deps/exper_iai-e0c596df81667934)
running 1 test
test tests::it_works ... ignored
test result: ok. 0 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running unittests src/main.rs (target/release/deps/exper_iai-bbf641b3842b4eea)
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Running benches/iai.rs (target/release/deps/iai-1d6df879cc9849e1)
iai_add
Instructions: 22
L1 Accesses: 34
L2 Accesses: 2
RAM Accesses: 2
Estimated Cycles: 114
$ cargo bench --bench iai
Finished bench [optimized] target(s) in 0.02s
Running benches/iai.rs (target/release/deps/iai-1d6df879cc9849e1)
iai_add
Instructions: 22 (No change)
L1 Accesses: 34 (No change)
L2 Accesses: 2 (No change)
RAM Accesses: 2 (No change)
Estimated Cycles: 114 (No change)
$ cargo bench --bench iai
Finished bench [optimized] target(s) in 0.02s
Running benches/iai.rs (target/release/deps/iai-1d6df879cc9849e1)
iai_add
Instructions: 22 (No change)
L1 Accesses: 34 (No change)
L2 Accesses: 2 (No change)
RAM Accesses: 2 (No change)
Estimated Cycles: 114 (No change)
To start, I want to thank @bheisler for all of the effort you've put into
criterion
andiai
!!I've been experimenting with
iai
and really like the notion of "one-shot" measuring for low level benchmarks. I've played round with it, but sometimes I get unexpected results. This could definitely be an error on my part, that is usually the case, but I've been unable to track it down and thus I've created this issue.Of note, I get very consistent results if I do multiple runs of a single configuration. But sometimes I run into problems when I change something or run a slightly different command. I then can get results that look wrong to me.
First off, I use an Arch Linux system for development:
I've created exper-iai with
cargo new --lib
which creates a lib with anfn add
and testit_works
:I then added a simple
fn main
:And the iai benchmark is:
I also created
gen_asm.sh
so I could see the generated assembler.Also in Cargo.toml I added
[profile.dev]
and[profile.release]
as well as I addedrust-toolchain.toml
to keep the toolchain consistent:Running main and test work as expected:
And running
cargo bench
and yields a more or less expected result:And here are two more runs of just bench_iai showing the expected consistency:
Here is a my first unexpected result, if I change the command line, adding
taskset -c 0
, I wouldn't expect significantly different results, butInstructions
is0
, that is unexpected:But a bigger problem is if rename
bench_iai.rs
toiai.rs
and the bench within that file frombench_iai_add
toiai_add
:And then I make the necessary changes to get things working again, see exper-iai branch
rename-bench_iai_add-to-iai_add
. In my opinion only "labels" have changed and none of the actual assembler code has changed.But now I get really unexpected results, I switch branches and then
clean
and rerunbench
and nowInstructions
has a value of22
: