Skip to content

Commit

Permalink
Benchmark internals
Browse files Browse the repository at this point in the history
  • Loading branch information
nvzqz committed Oct 30, 2023
1 parent b35fb04 commit 558176a
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,26 @@ jobs:
target
key: examples-bench-${{ runner.os }}
- run: cargo bench -p examples --all-features

# Run `internal_benches/` directory as benchmarks.
internals-bench:
name: Internals Bench
runs-on: ${{ matrix.os }}
env:
# Run each benchmark within 2 seconds.
DIVAN_MAX_TIME: 2
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest
steps:
- uses: actions/checkout@v4
- uses: actions/[email protected]
with:
path: |
${{ env.CARGO_HOME }}
target
key: internals-bench-${{ runner.os }}
- run: cargo bench -p internal_benches --all-features
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ default = ["wrap_help"]
help = ["clap/help"]
wrap_help = ["help", "clap/wrap_help"]

# Benchmark internals. Not meant for public use.
internal_benches = []

[workspace]
members = ["macros", "examples"]
members = ["macros", "examples", "internal_benches"]

[workspace.dependencies]
divan = { path = "." }
16 changes: 16 additions & 0 deletions internal_benches/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "internal_benches"
version = "0.0.0"
edition = "2021"
authors = ["Nikolai Vazquez"]
license = "MIT OR Apache-2.0"
description = "Internal benchmarks for Divan, a comfy benchmarking framework."
readme = "../README.md"
publish = false

[dependencies]
divan = { workspace = true, features = ["internal_benches"] }

[[bench]]
name = "internals"
harness = false
27 changes: 27 additions & 0 deletions internal_benches/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Divan Internal Benchmarks

This crate demonstrates how to use [Divan] to benchmark internals of a crate by
benchmarking the internals of Divan.

These can be benchmarked locally by running:

```sh
git clone https://github.com/nvzqz/divan.git
cd divan

cargo bench -q -p internal_benches
```

As of this writing, the output on my machine is:

```txt
divan fastest │ slowest │ median │ mean │ samples │ iters
╰─ time │ │ │ │ │
╰─ timer │ │ │ │ │
├─ get_tsc 0.158 ns │ 0.202 ns │ 0.161 ns │ 0.162 ns │ 100 │ 1638400
╰─ measure │ │ │ │ │
├─ precision 89.58 µs │ 221.5 µs │ 201.9 µs │ 184.5 µs │ 100 │ 100
╰─ sample_loop_overhead 314.2 µs │ 342.5 µs │ 314.5 µs │ 317.1 µs │ 100 │ 100
```

[divan]: https://github.com/nvzqz/divan
3 changes: 3 additions & 0 deletions internal_benches/benches/internals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
divan::main();
}
22 changes: 22 additions & 0 deletions src/time/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,25 @@ pub(crate) enum TimerKind {
/// CPU timestamp counter.
Tsc,
}

#[cfg(feature = "internal_benches")]
#[crate::bench(crate = crate)]
fn get_tsc() -> Result<Timer, TscUnavailable> {
Timer::get_tsc()
}

#[cfg(feature = "internal_benches")]
mod measure {
use super::*;

#[crate::bench(crate = crate)]
fn precision() -> FineDuration {
Timer::Os.measure_precision()
}

#[cfg(feature = "internal_benches")]
#[crate::bench(crate = crate)]
fn sample_loop_overhead() -> FineDuration {
Timer::Os.measure_sample_loop_overhead()
}
}

0 comments on commit 558176a

Please sign in to comment.