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

Spin in benches instead of yielding #45

Merged
merged 3 commits into from
Sep 21, 2023
Merged
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ serde_test = "1.0"
criterion = { version = "0.5", default-features = false, features = [
"cargo_bench_support",
] }
spin_sleep = "1.1"
anyhow = "1.0"
clap = { version = "4.1", features = ["derive"] }
bevy = { version = "0.11", default-features = false, features = [
Expand Down
26 changes: 15 additions & 11 deletions benches/replication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ use bevy::{app::MainScheduleOrder, ecs::schedule::ExecutorKind, prelude::*};
use bevy_replicon::prelude::*;
use criterion::{criterion_group, criterion_main, Criterion};
use serde::{Deserialize, Serialize};
use spin_sleep::{SpinSleeper, SpinStrategy};

#[derive(Component, Clone, Copy, Serialize, Deserialize)]
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) {
const ENTITIES: u32 = 900;
const SOCKET_WAIT: Duration = Duration::from_millis(5); // Sometimes it takes time for socket to receive all data.

// Use spinner to keep CPU hot in the schedule for stable benchmark results.
let sleeper = SpinSleeper::new(1_000_000_000).with_spin_strategy(SpinStrategy::SpinLoopHint);

c.bench_function("entities send", |b| {
b.iter_custom(|iter| {
let mut elapsed = Duration::ZERO;
Expand All @@ -34,7 +38,7 @@ fn replication(c: &mut Criterion) {
server_app.update();
elapsed += instant.elapsed();

std::thread::sleep(SOCKET_WAIT);
sleeper.sleep(SOCKET_WAIT);
client_app.update();
assert_eq!(client_app.world.entities().len(), ENTITIES);
}
Expand All @@ -59,7 +63,7 @@ fn replication(c: &mut Criterion) {
.spawn_batch([(Replication, DummyComponent(0)); ENTITIES as usize]);

server_app.update();
std::thread::sleep(SOCKET_WAIT);
sleeper.sleep(SOCKET_WAIT);

let instant = Instant::now();
client_app.update();
Expand Down Expand Up @@ -87,7 +91,7 @@ fn replication(c: &mut Criterion) {
let mut query = server_app.world.query::<&mut DummyComponent>();

server_app.update();
std::thread::sleep(SOCKET_WAIT);
sleeper.sleep(SOCKET_WAIT);
client_app.update();
assert_eq!(client_app.world.entities().len(), ENTITIES);

Expand All @@ -96,12 +100,12 @@ fn replication(c: &mut Criterion) {
dummy_component.0 += 1;
}

std::thread::sleep(SOCKET_WAIT);
sleeper.sleep(SOCKET_WAIT);
let instant = Instant::now();
server_app.update();
elapsed += instant.elapsed();

std::thread::sleep(SOCKET_WAIT);
sleeper.sleep(SOCKET_WAIT);
client_app.update();
assert_eq!(client_app.world.entities().len(), ENTITIES);
}
Expand All @@ -126,7 +130,7 @@ fn replication(c: &mut Criterion) {
let mut query = server_app.world.query::<&mut DummyComponent>();

server_app.update();
std::thread::sleep(SOCKET_WAIT);
sleeper.sleep(SOCKET_WAIT);
client_app.update();
assert_eq!(client_app.world.entities().len(), ENTITIES);

Expand All @@ -135,9 +139,9 @@ fn replication(c: &mut Criterion) {
dummy_component.0 += 1;
}

std::thread::sleep(SOCKET_WAIT);
sleeper.sleep(SOCKET_WAIT);
server_app.update();
std::thread::sleep(SOCKET_WAIT);
sleeper.sleep(SOCKET_WAIT);

let instant = Instant::now();
client_app.update();
Expand Down