Skip to content

Commit 558176a

Browse files
committed
Benchmark internals
1 parent b35fb04 commit 558176a

File tree

6 files changed

+95
-1
lines changed

6 files changed

+95
-1
lines changed

.github/workflows/ci.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,26 @@ jobs:
138138
target
139139
key: examples-bench-${{ runner.os }}
140140
- run: cargo bench -p examples --all-features
141+
142+
# Run `internal_benches/` directory as benchmarks.
143+
internals-bench:
144+
name: Internals Bench
145+
runs-on: ${{ matrix.os }}
146+
env:
147+
# Run each benchmark within 2 seconds.
148+
DIVAN_MAX_TIME: 2
149+
strategy:
150+
matrix:
151+
os:
152+
- ubuntu-latest
153+
- macos-latest
154+
- windows-latest
155+
steps:
156+
- uses: actions/checkout@v4
157+
- uses: actions/[email protected]
158+
with:
159+
path: |
160+
${{ env.CARGO_HOME }}
161+
target
162+
key: internals-bench-${{ runner.os }}
163+
- run: cargo bench -p internal_benches --all-features

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ default = ["wrap_help"]
2929
help = ["clap/help"]
3030
wrap_help = ["help", "clap/wrap_help"]
3131

32+
# Benchmark internals. Not meant for public use.
33+
internal_benches = []
34+
3235
[workspace]
33-
members = ["macros", "examples"]
36+
members = ["macros", "examples", "internal_benches"]
3437

3538
[workspace.dependencies]
3639
divan = { path = "." }

internal_benches/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "internal_benches"
3+
version = "0.0.0"
4+
edition = "2021"
5+
authors = ["Nikolai Vazquez"]
6+
license = "MIT OR Apache-2.0"
7+
description = "Internal benchmarks for Divan, a comfy benchmarking framework."
8+
readme = "../README.md"
9+
publish = false
10+
11+
[dependencies]
12+
divan = { workspace = true, features = ["internal_benches"] }
13+
14+
[[bench]]
15+
name = "internals"
16+
harness = false

internal_benches/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Divan Internal Benchmarks
2+
3+
This crate demonstrates how to use [Divan] to benchmark internals of a crate by
4+
benchmarking the internals of Divan.
5+
6+
These can be benchmarked locally by running:
7+
8+
```sh
9+
git clone https://github.com/nvzqz/divan.git
10+
cd divan
11+
12+
cargo bench -q -p internal_benches
13+
```
14+
15+
As of this writing, the output on my machine is:
16+
17+
```txt
18+
divan fastest │ slowest │ median │ mean │ samples │ iters
19+
╰─ time │ │ │ │ │
20+
╰─ timer │ │ │ │ │
21+
├─ get_tsc 0.158 ns │ 0.202 ns │ 0.161 ns │ 0.162 ns │ 100 │ 1638400
22+
╰─ measure │ │ │ │ │
23+
├─ precision 89.58 µs │ 221.5 µs │ 201.9 µs │ 184.5 µs │ 100 │ 100
24+
╰─ sample_loop_overhead 314.2 µs │ 342.5 µs │ 314.5 µs │ 317.1 µs │ 100 │ 100
25+
```
26+
27+
[divan]: https://github.com/nvzqz/divan
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
divan::main();
3+
}

src/time/timer.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,25 @@ pub(crate) enum TimerKind {
172172
/// CPU timestamp counter.
173173
Tsc,
174174
}
175+
176+
#[cfg(feature = "internal_benches")]
177+
#[crate::bench(crate = crate)]
178+
fn get_tsc() -> Result<Timer, TscUnavailable> {
179+
Timer::get_tsc()
180+
}
181+
182+
#[cfg(feature = "internal_benches")]
183+
mod measure {
184+
use super::*;
185+
186+
#[crate::bench(crate = crate)]
187+
fn precision() -> FineDuration {
188+
Timer::Os.measure_precision()
189+
}
190+
191+
#[cfg(feature = "internal_benches")]
192+
#[crate::bench(crate = crate)]
193+
fn sample_loop_overhead() -> FineDuration {
194+
Timer::Os.measure_sample_loop_overhead()
195+
}
196+
}

0 commit comments

Comments
 (0)