Skip to content

Commit

Permalink
Improve benches (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
UkoeHB authored Sep 20, 2023
1 parent cbc6825 commit 6bcf4b0
Showing 1 changed file with 84 additions and 3 deletions.
87 changes: 84 additions & 3 deletions benches/replication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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);
}
Expand All @@ -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();
Expand Down

0 comments on commit 6bcf4b0

Please sign in to comment.