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();