Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Add internal benchmarks #16

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ 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
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 }
Expand All @@ -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"

Expand All @@ -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"]
5 changes: 5 additions & 0 deletions benches/internal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern crate bevy_rand;

fn main() {
divan::main();
}
17 changes: 17 additions & 0 deletions src/thread_local_entropy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
Loading