Skip to content

Commit

Permalink
Criterion to divan (#622)
Browse files Browse the repository at this point in the history
As discussed in #620.
  • Loading branch information
JackCrumpLeys authored Jun 19, 2024
1 parent 9f9d284 commit 83e3e29
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 325 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ valence_world_border = { workspace = true, optional = true }
[dev-dependencies]
anyhow.workspace = true
clap.workspace = true
criterion.workspace = true
divan.workspace = true
flume.workspace = true
noise.workspace = true # For the terrain example.
tracing.workspace = true
Expand Down Expand Up @@ -129,9 +129,9 @@ bytes = "1.2.1"
cesu8 = "1.1.0"
cfb8 = "0.8.1"
clap = { version = "4.0.30", features = ["derive"] }
criterion = "0.5.1"
derive_more = "1.0.0-beta.3"
directories = "5.0.0"
divan = "0.1.14"
eframe = { version = "0.22.0", default-features = false }
egui = "0.22.0"
egui_dock = "0.6"
Expand Down
152 changes: 77 additions & 75 deletions benches/block.rs
Original file line number Diff line number Diff line change
@@ -1,91 +1,93 @@
use std::hint::black_box;

use criterion::Criterion;
use divan::Bencher;
use valence::block::{BlockKind, BlockState, PropName, PropValue};
use valence::ItemKind;

pub(crate) fn block(c: &mut Criterion) {
let mut group = c.benchmark_group("block");

let states = BlockKind::ALL.map(BlockKind::to_state);

group.bench_function("BlockState::from_kind", |b| {
b.iter(|| {
for kind in black_box(BlockKind::ALL) {
black_box(BlockState::from_kind(kind));
}
});
#[divan::bench]
pub fn from_kind(bencher: Bencher) {
bencher.bench(|| {
for kind in black_box(BlockKind::ALL) {
black_box(BlockState::from_kind(kind));
}
});

group.bench_function("BlockState::to_kind", |b| {
b.iter(|| {
for state in black_box(states) {
black_box(state.to_kind());
}
});
}
#[divan::bench]
pub fn to_kind(bencher: Bencher) {
let states = BlockKind::ALL.map(BlockKind::to_state);
bencher.bench(|| {
for state in black_box(states) {
black_box(state.to_kind());
}
});

group.bench_function("BlockState::get", |b| {
b.iter(|| {
for state in black_box(states) {
black_box(state.get(PropName::Note));
}
});
}
#[divan::bench]
pub fn get_prop(bencher: Bencher) {
let states = BlockKind::ALL.map(BlockKind::to_state);
bencher.bench(|| {
for state in black_box(states) {
black_box(state.get(PropName::Note));
}
});

group.bench_function("BlockState::set", |b| {
b.iter(|| {
for state in black_box(states) {
black_box(state.set(PropName::Note, PropValue::Didgeridoo));
}
});
}
#[divan::bench]
pub fn set_prop(bencher: Bencher) {
let states = BlockKind::ALL.map(BlockKind::to_state);
bencher.bench(|| {
for state in black_box(states) {
black_box(state.set(PropName::Note, PropValue::Didgeridoo));
}
});

group.bench_function("BlockState::is_liquid", |b| {
b.iter(|| {
for state in black_box(states) {
black_box(state.is_liquid());
}
});
}
#[divan::bench]
pub fn is_liquid(bencher: Bencher) {
let states = BlockKind::ALL.map(BlockKind::to_state);
bencher.bench(|| {
for state in black_box(states) {
black_box(state.is_liquid());
}
});

group.bench_function("BlockState::is_opaque", |b| {
b.iter(|| {
for state in black_box(states) {
black_box(state.is_opaque());
}
})
}
#[divan::bench]
pub fn is_opaque(bencher: Bencher) {
let states = BlockKind::ALL.map(BlockKind::to_state);
bencher.bench(|| {
for state in black_box(states) {
black_box(state.is_opaque());
}
});

group.bench_function("BlockState::is_replaceable", |b| {
b.iter(|| {
for state in black_box(states) {
black_box(state.is_replaceable());
}
})
}
#[divan::bench]
pub fn is_replaceable(bencher: Bencher) {
let states = BlockKind::ALL.map(BlockKind::to_state);
bencher.bench(|| {
for state in black_box(states) {
black_box(state.is_replaceable());
}
});

group.bench_function("BlockState::luminance", |b| {
b.iter(|| {
for state in black_box(states) {
black_box(state.luminance());
}
})
}
#[divan::bench]
pub fn luminance(bencher: Bencher) {
let states = BlockKind::ALL.map(BlockKind::to_state);
bencher.bench(|| {
for state in black_box(states) {
black_box(state.luminance());
}
});

group.bench_function("BlockKind::to_item_kind", |b| {
b.iter(|| {
for kind in black_box(BlockKind::ALL) {
black_box(kind.to_item_kind());
}
});
}
#[divan::bench]
pub fn to_item_kind(bencher: Bencher) {
bencher.bench(|| {
for kind in black_box(BlockKind::ALL) {
black_box(kind.to_item_kind());
}
});

group.bench_function("BlockKind::from_item_kind", |b| {
b.iter(|| {
for kind in black_box(ItemKind::ALL) {
black_box(BlockKind::from_item_kind(kind));
}
});
}
#[divan::bench]
pub fn from_item_kind(bencher: Bencher) {
bencher.bench(|| {
for kind in black_box(ItemKind::ALL) {
black_box(BlockKind::from_item_kind(kind));
}
});
}
26 changes: 12 additions & 14 deletions benches/decode_array.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
use std::hint::black_box;

use criterion::Criterion;
use divan::Bencher;
use valence::protocol::{Decode, Encode};

pub(crate) fn decode_array(c: &mut Criterion) {
let mut group = c.benchmark_group("decode_array");

#[divan::bench]
pub fn decode_small_array(bencher: Bencher) {
let floats = [123.0, 456.0, 789.0];
let mut buf = [0_u8; 24];

floats.encode(buf.as_mut_slice()).unwrap();

group.bench_function("<[f64; 3]>::decode", |b| {
b.iter(|| {
let mut r = black_box(buf.as_slice());
let _ = black_box(<[f64; 3]>::decode(&mut r));
});
bencher.bench(|| {
let mut r = black_box(buf.as_slice());
let _ = black_box(<[f64; 3]>::decode(&mut r));
});
}

#[divan::bench]
pub fn decode_large_array(bencher: Bencher) {
let bytes = [42; 4096];

group.bench_function("<[u8; 4096]>::decode", |b| {
b.iter(|| {
let mut r = black_box(bytes.as_slice());
let _ = black_box(<[u8; 4096]>::decode(&mut r));
})
bencher.bench(|| {
let mut r = black_box(bytes.as_slice());
let _ = black_box(<[u8; 4096]>::decode(&mut r));
});
}
11 changes: 5 additions & 6 deletions benches/idle.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use criterion::Criterion;
use divan::Bencher;
use valence::prelude::*;

/// Benches the performance of a single server tick while nothing much is
/// happening.
pub(crate) fn idle_update(c: &mut Criterion) {
#[divan::bench]
pub fn idle_update(bencher: Bencher) {
let mut app = App::new();

app.add_plugins(DefaultPlugins);
Expand All @@ -12,10 +13,8 @@ pub(crate) fn idle_update(c: &mut Criterion) {
// Run startup schedule.
app.update();

c.bench_function("idle_update", |b| {
b.iter(|| {
app.update();
});
bencher.bench_local(move || {
app.update();
});
}

Expand Down
15 changes: 2 additions & 13 deletions benches/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use criterion::{criterion_group, criterion_main};

mod anvil;
mod block;
mod decode_array;
Expand All @@ -9,15 +7,6 @@ mod packet;
mod var_int;
mod var_long;

criterion_group! {
benches,
block::block,
decode_array::decode_array,
idle::idle_update,
packet::packet,
var_int::var_int,
var_long::var_long,
many_players::many_players,
fn main() {
divan::main();
}

criterion_main!(benches);
63 changes: 30 additions & 33 deletions benches/many_players.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::time::Duration;

use bevy_app::prelude::*;
use criterion::Criterion;
use divan::Bencher;
use rand::Rng;
use valence::entity::Position;
use valence::keepalive::KeepaliveSettings;
Expand All @@ -15,18 +15,17 @@ use valence::testing::create_mock_client;
use valence::{ident, ChunkPos, DefaultPlugins, Hand, Server, ServerSettings};
use valence_server::CompressionThreshold;

pub(crate) fn many_players(c: &mut Criterion) {
run_many_players(c, "many_players", 3000, 16, 16);
run_many_players(c, "many_players_spread_out", 3000, 8, 200);
#[divan::bench]
fn many_players(bencher: Bencher) {
run_many_players(bencher, 3000, 16, 16);
}

fn run_many_players(
c: &mut Criterion,
func_name: &str,
client_count: usize,
view_dist: u8,
world_size: i32,
) {
#[divan::bench]
fn many_players_spread_out(bencher: Bencher) {
run_many_players(bencher, 3000, 8, 200);
}

fn run_many_players(bencher: Bencher, client_count: usize, view_dist: u8, world_size: i32) {
let mut app = App::new();

app.insert_resource(ServerSettings {
Expand Down Expand Up @@ -91,34 +90,32 @@ fn run_many_players(

app.update();

c.bench_function(func_name, |b| {
b.iter(|| {
let mut rng = rand::thread_rng();
bencher.bench_local(|| {
let mut rng = rand::thread_rng();

// Move the clients around randomly. They'll cross chunk borders and cause
// interesting things to happen.
for (id, helper) in &mut clients {
let pos = query.get(&app.world, *id).unwrap().get();
// Move the clients around randomly. They'll cross chunk borders and cause
// interesting things to happen.
for (id, helper) in &mut clients {
let pos = query.get(&app.world, *id).unwrap().get();

let offset = DVec3::new(rng.gen_range(-1.0..=1.0), 0.0, rng.gen_range(-1.0..=1.0));
let offset = DVec3::new(rng.gen_range(-1.0..=1.0), 0.0, rng.gen_range(-1.0..=1.0));

helper.send(&FullC2s {
position: pos + offset,
yaw: rng.gen_range(0.0..=360.0),
pitch: rng.gen_range(0.0..=360.0),
on_ground: rng.gen(),
});
helper.send(&FullC2s {
position: pos + offset,
yaw: rng.gen_range(0.0..=360.0),
pitch: rng.gen_range(0.0..=360.0),
on_ground: rng.gen(),
});

helper.send(&HandSwingC2s { hand: Hand::Main });
}
helper.send(&HandSwingC2s { hand: Hand::Main });
}

drop(rng);
drop(rng);

app.update(); // The important part.
app.update(); // The important part.

for (_, helper) in &mut clients {
helper.clear_received();
}
});
for (_, helper) in &mut clients {
helper.clear_received();
}
});
}
Loading

0 comments on commit 83e3e29

Please sign in to comment.