diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 64feea9..4493d30 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,7 +46,7 @@ jobs: run: cargo check -Z features=dev_dep - run: cargo test --all-features - name: Test wasm - run: wasm-pack test --headless --chrome --firefox -- --all-features + run: wasm-pack test --headless --chrome --firefox if: startsWith(matrix.os, 'ubuntu') miri: name: "Miri" @@ -79,6 +79,33 @@ jobs: # -Zmiri-permissive-provenance disables warnings against int2ptr casts (since those are used by once_cell) # -Zmiri-ignore-leaks is necessary because a bunch of tests don't join all threads before finishing. MIRIFLAGS: -Zmiri-ignore-leaks -Zmiri-disable-isolation -Zmiri-permissive-provenance + benches: + name: "Benches" + runs-on: ${{ matrix.os }} + timeout-minutes: 15 + needs: test + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + steps: + - uses: actions/checkout@v3 + - uses: actions/cache@v3 + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target/ + key: ${{ runner.os }}-cargo-benches-${{ hashFiles('**/Cargo.toml') }} + - name: Install Rust + # --no-self-update is necessary because the windows environment cannot self-update rustup.exe. + run: rustup update stable --no-self-update && rustup default stable + - name: Install alsa and udev + run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev + if: startsWith(matrix.os, 'ubuntu') + - name: Run benchmarks + run: cargo bench msrv: runs-on: ubuntu-latest timeout-minutes: 15 diff --git a/Cargo.toml b/Cargo.toml index cf368bd..f55cfe4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,7 @@ rand_chacha = ["bevy_prng/rand_chacha"] rand_pcg = ["bevy_prng/rand_pcg"] rand_xoshiro = ["bevy_prng/rand_xoshiro"] wyrand = ["bevy_prng/wyrand"] +internal_benchmarks = ["dep:divan"] [dependencies] # bevy @@ -48,6 +49,7 @@ bevy.workspace = true bevy_prng = { path = "bevy_prng", version = "0.5" } # others +divan = { version = "0.1", optional = true } rand_core.workspace = true rand_chacha = { workspace = true, optional = true } serde = { workspace = true, optional = true } @@ -65,6 +67,13 @@ bevy_prng = { path = "bevy_prng", version = "0.5", features = ["rand_chacha", "w rand = "0.8" ron = { version = "0.8.0", features = ["integer128"] } +[[bench]] +name = "internal" +harness = false + +[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] +bevy_rand = { path = ".", version = "0.5", features = ["internal_benchmarks"] } + [target.'cfg(target_arch = "wasm32")'.dev-dependencies] wasm-bindgen-test = "0.3" @@ -76,4 +85,4 @@ name = "turn_based_game" path = "examples/turn_based_game.rs" [package.metadata.docs.rs] -all-features = true +features = ["rand_chacha", "rand_pcg", "rand_xoshiro", "wyrand"] diff --git a/benches/internal.rs b/benches/internal.rs new file mode 100644 index 0000000..f731863 --- /dev/null +++ b/benches/internal.rs @@ -0,0 +1,5 @@ +extern crate bevy_rand; + +fn main() { + divan::main(); +} diff --git a/src/thread_local_entropy.rs b/src/thread_local_entropy.rs index 15e2a76..e19bd60 100644 --- a/src/thread_local_entropy.rs +++ b/src/thread_local_entropy.rs @@ -78,6 +78,23 @@ impl RngCore for ThreadLocalEntropy { impl CryptoRng for ThreadLocalEntropy {} +#[cfg(feature = "internal_benchmarks")] +mod benchmarks { + use super::*; + + #[divan::bench(sample_size = 1024, sample_count = 128)] + fn local_access(bencher: divan::Bencher) { + use divan::counter::BytesCount; + + bencher + .with_inputs(|| [0u8; 64]) + .input_counter(BytesCount::of_slice) + .bench_refs(|bytes| { + ThreadLocalEntropy::new().fill_bytes(bytes); + }); + } +} + #[cfg(test)] mod tests { use super::*;