From 8ef6d3f6da238219278b97c1b657ab5f34ce5b33 Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Wed, 6 Sep 2023 18:27:01 +0300 Subject: [PATCH] Add benchmarks --- CHANGELOG.md | 2 +- Cargo.toml | 7 ++++ benches/replication.rs | 85 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 benches/replication.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fa21c64..7cd6ff9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- Derive `Clone, Copy` for `Replication`. +- Derive `Clone` and `Copy` for `Replication`. - Make `ServerPlugin` fields private and add `ServerPlugin::new`. - Make `AckedTicks` public. - Make `NetworkEntityMap` public. diff --git a/Cargo.toml b/Cargo.toml index 434019d3..219ab273 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,9 @@ derive_more = { version = "0.99", default-features = false, features = [ [dev-dependencies] serde_test = "1.0" +criterion = { version = "0.5", default-features = false, features = [ + "cargo_bench_support", +] } anyhow = "1.0" clap = { version = "4.1", features = ["derive"] } bevy = { version = "0.11", default-features = false, features = [ @@ -42,3 +45,7 @@ bevy = { version = "0.11", default-features = false, features = [ "x11", "default_font", ] } + +[[bench]] +name = "replication" +harness = false diff --git a/benches/replication.rs b/benches/replication.rs new file mode 100644 index 00000000..9209441b --- /dev/null +++ b/benches/replication.rs @@ -0,0 +1,85 @@ +#[path = "../tests/common/mod.rs"] +mod common; + +use std::time::{Duration, Instant}; + +use bevy::prelude::*; +use bevy_replicon::prelude::*; +use criterion::{criterion_group, criterion_main, Criterion}; + +#[derive(Component, Reflect, Default, Clone, Copy)] +#[reflect(Component)] +struct DummyComponent; + +const ENTITIES: u32 = 900; + +fn replication(c: &mut Criterion) { + c.bench_function("entities send", |b| { + b.iter_custom(|iter| { + let mut elapsed = Duration::ZERO; + for _ in 0..iter { + let mut server_app = App::new(); + let mut client_app = App::new(); + for app in [&mut server_app, &mut client_app] { + app.add_plugins(( + MinimalPlugins, + ReplicationPlugins.set(ServerPlugin::new(TickPolicy::Manual)), + )) + .replicate::(); + } + common::setup(&mut server_app, &mut client_app); + + server_app + .world + .spawn_batch([(Replication, DummyComponent); ENTITIES as usize]); + + let instant = Instant::now(); + server_app.update(); + elapsed += instant.elapsed(); + + client_app.update(); + assert_eq!(client_app.world.entities().len(), ENTITIES); + } + + elapsed + }) + }); + + c.bench_function("entities receive", |b| { + b.iter_custom(|iter| { + let mut elapsed = Duration::ZERO; + for _ in 0..iter { + let mut server_app = App::new(); + let mut client_app = App::new(); + for app in [&mut server_app, &mut client_app] { + app.add_plugins(( + MinimalPlugins, + ReplicationPlugins.set(ServerPlugin::new(TickPolicy::Manual)), + )) + .replicate::(); + } + common::setup(&mut server_app, &mut client_app); + + server_app + .world + .spawn_batch([(Replication, DummyComponent); ENTITIES as usize]); + + server_app.update(); + + let instant = Instant::now(); + client_app.update(); + elapsed += instant.elapsed(); + assert_eq!(client_app.world.entities().len(), ENTITIES); + } + + elapsed + }) + }); +} + +criterion_group! { + name = benches; + config = Criterion::default().sample_size(50); + targets = replication +} +criterion_main!(benches);