From 6bcf4b03a9f602c5a9784cbbf794dac5109c4cdd Mon Sep 17 00:00:00 2001 From: UkoeHB <37489173+UkoeHB@users.noreply.github.com> Date: Wed, 20 Sep 2023 17:03:43 -0500 Subject: [PATCH] Improve benches (#41) --- benches/replication.rs | 87 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 3 deletions(-) diff --git a/benches/replication.rs b/benches/replication.rs index eff46933..7e0d213f 100644 --- a/benches/replication.rs +++ b/benches/replication.rs @@ -9,9 +9,10 @@ use criterion::{criterion_group, criterion_main, Criterion}; use serde::{Deserialize, Serialize}; #[derive(Component, Clone, Copy, Serialize, Deserialize)] -struct DummyComponent; +struct DummyComponent(usize); const ENTITIES: u32 = 900; +const SOCKET_WAIT: Duration = Duration::from_millis(5); // Sometimes it takes time for socket to receive all data. fn replication(c: &mut Criterion) { c.bench_function("entities send", |b| { @@ -27,12 +28,13 @@ fn replication(c: &mut Criterion) { server_app .world - .spawn_batch([(Replication, DummyComponent); ENTITIES as usize]); + .spawn_batch([(Replication, DummyComponent(0)); ENTITIES as usize]); let instant = Instant::now(); server_app.update(); elapsed += instant.elapsed(); + std::thread::sleep(SOCKET_WAIT); client_app.update(); assert_eq!(client_app.world.entities().len(), ENTITIES); } @@ -54,9 +56,88 @@ fn replication(c: &mut Criterion) { server_app .world - .spawn_batch([(Replication, DummyComponent); ENTITIES as usize]); + .spawn_batch([(Replication, DummyComponent(0)); ENTITIES as usize]); server_app.update(); + std::thread::sleep(SOCKET_WAIT); + + let instant = Instant::now(); + client_app.update(); + elapsed += instant.elapsed(); + assert_eq!(client_app.world.entities().len(), ENTITIES); + } + + elapsed + }) + }); + + c.bench_function("entities update send", |b| { + b.iter_custom(|iter| { + let mut elapsed = Duration::ZERO; + let mut server_app = App::new(); + let mut client_app = App::new(); + for app in [&mut server_app, &mut client_app] { + setup_app(app); + } + common::connect(&mut server_app, &mut client_app); + + server_app + .world + .spawn_batch([(Replication, DummyComponent(0)); ENTITIES as usize]); + let mut query = server_app.world.query::<&mut DummyComponent>(); + + server_app.update(); + std::thread::sleep(SOCKET_WAIT); + client_app.update(); + assert_eq!(client_app.world.entities().len(), ENTITIES); + + for _ in 0..iter { + for mut dummy_component in query.iter_mut(&mut server_app.world) { + dummy_component.0 += 1; + } + + std::thread::sleep(SOCKET_WAIT); + let instant = Instant::now(); + server_app.update(); + elapsed += instant.elapsed(); + + std::thread::sleep(SOCKET_WAIT); + client_app.update(); + assert_eq!(client_app.world.entities().len(), ENTITIES); + } + + elapsed + }) + }); + + c.bench_function("entities update receive", |b| { + b.iter_custom(|iter| { + let mut elapsed = Duration::ZERO; + let mut server_app = App::new(); + let mut client_app = App::new(); + for app in [&mut server_app, &mut client_app] { + setup_app(app); + } + common::connect(&mut server_app, &mut client_app); + + server_app + .world + .spawn_batch([(Replication, DummyComponent(0)); ENTITIES as usize]); + let mut query = server_app.world.query::<&mut DummyComponent>(); + + server_app.update(); + std::thread::sleep(SOCKET_WAIT); + client_app.update(); + assert_eq!(client_app.world.entities().len(), ENTITIES); + + for _ in 0..iter { + for mut dummy_component in query.iter_mut(&mut server_app.world) { + dummy_component.0 += 1; + } + + std::thread::sleep(SOCKET_WAIT); + server_app.update(); + std::thread::sleep(SOCKET_WAIT); let instant = Instant::now(); client_app.update();