7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | # Benchmarks for the rust-sdk crypto layer |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | This directory contains various benchmarks that test critical functionality in |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | the crypto layer in the rust-sdk. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | We're using [Criterion] for the benchmarks, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | [the full documentation for Criterion can be found here](https://bheisler.github.io/criterion.rs/book/criterion_rs.html). |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | ## Running the benchmarks |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | The benchmark can be run by using the `bench` command of `cargo`: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | ```bash |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | $ cargo bench |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | This will work from the workspace directory of the rust-sdk. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | To lower compile times, you might be interested in using the `profiling` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | profile, that's optimized for a fair tradeoff between compile times and runtime |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | performance: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | ```bash |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | $ cargo bench --profile profiling |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | If you want to pass options to the benchmark |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | [you'll need to specify the name of the benchmark](https://bheisler.github.io/criterion.rs/book/faq.html#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options): |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | ```bash |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | $ cargo bench --bench crypto_bench -- # Your options go here |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | If you want to run only a specific benchmark, pass the name of the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | benchmark as an argument: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | ```bash |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | $ cargo bench --bench crypto_bench "Room key sharing/" |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | After the benchmarks are done, a HTML report can be found in |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | `target/criterion/report/index.html`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | ### Using a baseline for the benchmark |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | The benchmarks will by default compare the results to the previous run of the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | benchmark. If you are improving the performance of a specific feature and run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | the benchmark many times, it may be useful to store a baseline to compare |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | against instead. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | The `--save-baseline` switch can be used to create a baseline for the benchmark. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | ```bash |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | $ cargo bench --bench crypto_bench -- --save-baseline libolm |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | After you make your changes you can use the baseline to compare the results like |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | so: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | ```bash |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | $ cargo bench --bench crypto_bench -- --baseline libolm |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | ### Generating flame graphs for the benchmarks |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | The benchmarks support profiling and generating [Flame Graphs] while they run in |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | profiling mode using [pprof]. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | Profiling usually requires root permissions, to avoid the need for root |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | permissions you can adjust the value of `perf_event_paranoid`, e.g. the most |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | permisive value is `-1`: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | ```bash |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | $ echo -1 | sudo tee /proc/sys/kernel/perf_event_paranoid |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 75 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 76 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 77 | To generate flame graphs feature, enable the profiling mode using the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 78 | `--profile-time` command line flag: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 79 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 80 | ```bash |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 81 | $ cargo bench --bench crypto_bench -- --profile-time=5 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 82 | ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 83 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 84 | After the benchmarks are done, a flame graph for each individual benchmark can |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 85 | be found in `target/criterion/<name-of-benchmark>/profile/flamegraph.svg`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 86 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 87 | [pprof]: https://docs.rs/pprof/0.5.0/pprof/index.html# |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 88 | [Criterion]: https://docs.rs/criterion/0.3.5/criterion/ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 89 | [Flame Graphs]: https://www.brendangregg.com/flamegraphs.html |