Skip to content

Commit

Permalink
fix bench mark
Browse files Browse the repository at this point in the history
  • Loading branch information
kralverde committed Mar 1, 2025
1 parent ae98f55 commit 7b37074
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
32 changes: 20 additions & 12 deletions pumpkin-world/benches/chunk_noise_populate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs, sync::Arc};
use std::{fs, path::PathBuf, sync::Arc};

use criterion::{Criterion, criterion_group, criterion_main};
use pumpkin_util::math::vector2::Vector2;
Expand All @@ -19,20 +19,23 @@ fn bench_populate_noise(c: &mut Criterion) {
});
}

const MIN_POS: i32 = -32;
const MAX_POS: i32 = 32;
const MIN_POS: i32 = -4;
const MAX_POS: i32 = 4;

async fn test_reads(level: &Level) {
let chunk_positions =
(MIN_POS..=MAX_POS).flat_map(|x| (MIN_POS..=MAX_POS).map(move |z| Vector2::new(x, z)));
async fn test_reads(root_dir: PathBuf, positions: &[Vector2<i32>]) {
let level = Level::from_root_folder(root_dir);

let rt = tokio::runtime::Handle::current();
let (send, mut recv) = tokio::sync::mpsc::channel(10);
level.fetch_chunks(&chunk_positions.collect::<Vec<_>>(), send, &rt);
while recv.recv().await.is_some() {}
level.fetch_chunks(positions, send, &rt);
while let Some(x) = recv.recv().await {
// Don't compile me away!
let _ = x;
}
}

async fn test_writes(level: &Level, chunks: &[(Vector2<i32>, Arc<RwLock<ChunkData>>)]) {
async fn test_writes(root_dir: PathBuf, chunks: &[(Vector2<i32>, Arc<RwLock<ChunkData>>)]) {
let level = Level::from_root_folder(root_dir);
for (pos, chunk) in chunks {
level.write_chunk((*pos, chunk.clone())).await;
}
Expand All @@ -43,7 +46,6 @@ fn bench_chunk_io(c: &mut Criterion) {
// System temp dirs are in-memory, so we cant use temp_dir
let root_dir = global_path!("./bench_root");
fs::create_dir(&root_dir).unwrap();
let level = Level::from_root_folder(root_dir.clone());

let chunk_positions =
(MIN_POS..=MAX_POS).flat_map(|x| (MIN_POS..=MAX_POS).map(move |z| Vector2::new(x, z)));
Expand All @@ -54,24 +56,30 @@ fn bench_chunk_io(c: &mut Criterion) {
println!("Initializing data...");
// Initial writes
let mut chunks = Vec::new();
let mut positions = Vec::new();
async_handler.block_on(async {
let rt = tokio::runtime::Handle::current();
let (send, mut recv) = tokio::sync::mpsc::channel(10);
// Our data dir is empty, so we're generating new chunks here
let level = Level::from_root_folder(root_dir.clone());
level.fetch_chunks(&chunk_positions.collect::<Vec<_>>(), send, &rt);
while let Some((chunk, _)) = recv.recv().await {
let pos = chunk.read().await.position;
chunks.push((pos, chunk));
positions.push(pos);
}
});
println!("Testing with {} chunks", chunks.len());

// These test worst case: no caching done by `Level`
c.bench_function("write chunks", |b| {
b.to_async(&async_handler)
.iter(|| test_writes(&level, &chunks))
.iter(|| test_writes(root_dir.clone(), &chunks))
});

c.bench_function("read chunks", |b| {
b.to_async(&async_handler).iter(|| test_reads(&level))
b.to_async(&async_handler)
.iter(|| test_reads(root_dir.clone(), &positions))
});

fs::remove_dir_all(&root_dir).unwrap();
Expand Down
11 changes: 4 additions & 7 deletions pumpkin-world/src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,10 @@ impl Level {
let chunk_writer = self.chunk_writer.clone();
let level_folder = self.level_folder.clone();

// TODO: Save the join handles to await them when stopping the server
tokio::spawn(async move {
let data = chunk_to_write.1.read().await;
if let Err(error) = chunk_writer.write_chunk(&data, &level_folder, &chunk_to_write.0) {
log::error!("Failed writing Chunk to disk {}", error.to_string());
}
});
let data = chunk_to_write.1.read().await;
if let Err(error) = chunk_writer.write_chunk(&data, &level_folder, &chunk_to_write.0) {
log::error!("Failed writing Chunk to disk {}", error.to_string());
}
}

fn load_chunk_from_save(
Expand Down

0 comments on commit 7b37074

Please sign in to comment.