Skip to content
Open
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
10 changes: 6 additions & 4 deletions src/app/runtime/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,18 @@ pub fn generate_spawn_chunks(state: GlobalState) -> Result<(), BinaryError> {
pub fn setup_db(state: GlobalState) -> Result<(), BinaryError> {
info!("Setting up database...");

state.world.storage_backend.create_table("metadata")?;

let chunk_key = string_to_u128("chunk-format-hash");
state.world.storage_backend.insert(
"metadata".to_string(),
state.world.storage_backend.upsert(
"metadata",
chunk_key,
Chunk::type_hash().to_be_bytes().to_vec(),
)?;

let player_key = string_to_u128("player-format-hash");
state.world.storage_backend.insert(
"metadata".to_string(),
state.world.storage_backend.upsert(
"metadata",
player_key,
OfflinePlayerData::type_hash().to_be_bytes().to_vec(),
)?;
Expand Down
2 changes: 1 addition & 1 deletion src/app/validate/src/check_chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::{error, warn};
use type_hash::TypeHash;

pub fn check_chunks(state: &ServerState) -> Result<(), String> {
let env = state.world.storage_backend.env.lock();
let env = &state.world.storage_backend.env;
let txn = env
.read_txn()
.map_err(|e| format!("Failed to create read transaction: {}", e))?;
Expand Down
2 changes: 1 addition & 1 deletion src/app/validate/src/check_players.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use type_hash::TypeHash;
use uuid::Uuid;

pub fn check_players(state: &ServerState) -> Result<(), String> {
let env = state.world.storage_backend.env.lock();
let env = &state.world.storage_backend.env;
let txn = env
.read_txn()
.map_err(|e| format!("Failed to create read transaction: {}", e))?;
Expand Down
2 changes: 2 additions & 0 deletions src/storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
thiserror = { workspace = true }
heed = { workspace = true }
page_size = { workspace = true }
dashmap = { workspace = true }
parking_lot = { workspace = true }


Expand All @@ -16,6 +17,7 @@ criterion = { workspace = true }
tempfile = { workspace = true }
wyhash = { workspace = true }
rand = { workspace = true }
rayon = { workspace = true }

[[bench]]
name = "storage_bench"
Expand Down
106 changes: 66 additions & 40 deletions src/storage/src/benches/db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashSet;
use std::sync::Arc;
use temper_storage::lmdb::StorageBackend;

fn generate_random_data(size: usize) -> Vec<u8> {
Expand All @@ -17,9 +18,9 @@ fn generate_random_key(used: &mut HashSet<u128>) -> u128 {
key
}

fn select_random<T: Clone + Copy>(choices: Vec<T>) -> T {
fn select_random<T: Clone + Copy>(choices: &[T]) -> T {
let index = rand::random::<u32>() as usize % choices.len();
*choices.get(index).unwrap()
choices[index]
}

pub(crate) fn db_benches(c: &mut criterion::Criterion) {
Expand All @@ -28,36 +29,36 @@ pub(crate) fn db_benches(c: &mut criterion::Criterion) {

let db = StorageBackend::initialize(Some(tempdir), 100 * 1024 * 1024 * 1024).unwrap();

db.create_table("insert_test".to_string()).unwrap();
db.create_table("insert_test").unwrap();

let mut insert_group = c.benchmark_group("Insert");

insert_group.bench_function("512b".to_string(), |b| {
insert_group.bench_function("512b", |b| {
b.iter(|| {
db.insert(
"insert_test".to_string(),
"insert_test",
generate_random_key(&mut used_keys),
generate_random_data(512),
)
.unwrap();
})
});

insert_group.bench_function("1kb".to_string(), |b| {
insert_group.bench_function("1kb", |b| {
b.iter(|| {
db.insert(
"insert_test".to_string(),
"insert_test",
generate_random_key(&mut used_keys),
generate_random_data(1024),
)
.unwrap();
})
});

insert_group.bench_function("4kb".to_string(), |b| {
insert_group.bench_function("4kb", |b| {
b.iter(|| {
db.insert(
"insert_test".to_string(),
"insert_test",
generate_random_key(&mut used_keys),
generate_random_data(4096),
)
Expand All @@ -69,55 +70,80 @@ pub(crate) fn db_benches(c: &mut criterion::Criterion) {

let mut read_group = c.benchmark_group("Read");

db.create_table("read_test".to_string()).unwrap();
db.create_table("read_test").unwrap();

let keys_512b = (0..1000)
let keys_512b: Vec<u128> = (0..1000)
.map(|_| generate_random_key(&mut used_keys))
.collect::<Vec<_>>();

.collect();
for key in keys_512b.iter() {
db.insert("read_test".to_string(), *key, generate_random_data(512))
db.insert("read_test", *key, generate_random_data(512))
.unwrap();
}

read_group.bench_function("512b".to_string(), |b| {
b.iter(|| {
db.get("read_test".to_string(), select_random(keys_512b.clone()))
.unwrap();
})
read_group.bench_function("512b", |b| {
b.iter(|| db.get("read_test", select_random(&keys_512b)).unwrap())
});

let keys_1kb = (0..1000)
let keys_1kb: Vec<u128> = (0..1000)
.map(|_| generate_random_key(&mut used_keys))
.collect::<Vec<_>>();

.collect();
for key in keys_1kb.iter() {
db.insert("read_test".to_string(), *key, generate_random_data(1024))
db.insert("read_test", *key, generate_random_data(1024))
.unwrap();
}

read_group.bench_function("1kb".to_string(), |b| {
b.iter(|| {
db.get("read_test".to_string(), select_random(keys_1kb.clone()))
.unwrap();
})
read_group.bench_function("1kb", |b| {
b.iter(|| db.get("read_test", select_random(&keys_1kb)).unwrap())
});

let keys_4kb = (0..1000)
let keys_4kb: Vec<u128> = (0..1000)
.map(|_| generate_random_key(&mut used_keys))
.collect::<Vec<_>>();

.collect();
for key in keys_4kb.iter() {
db.insert("read_test".to_string(), *key, generate_random_data(4096))
db.insert("read_test", *key, generate_random_data(4096))
.unwrap();
}

read_group.bench_function("4kb".to_string(), |b| {
b.iter(|| {
db.get("read_test".to_string(), select_random(keys_4kb.clone()))
.unwrap();
})
read_group.bench_function("4kb", |b| {
b.iter(|| db.get("read_test", select_random(&keys_4kb)).unwrap())
});

read_group.finish();

db.create_table("concurrent_test").unwrap();

let concurrent_keys: Vec<u128> = (0..1000)
.map(|_| generate_random_key(&mut used_keys))
.collect();
for key in concurrent_keys.iter() {
db.insert("concurrent_test", *key, generate_random_data(512))
.unwrap();
}

let db = Arc::new(db);

let mut pool_group = c.benchmark_group("ConcurrentReadPool");

for thread_count in [2usize, 4, 8, 16] {
let db = Arc::clone(&db);
let keys = concurrent_keys.clone();

let pool = rayon::ThreadPoolBuilder::new()
.num_threads(thread_count)
.build()
.unwrap();

pool_group.bench_function(format!("{thread_count}_threads_512b"), |b| {
b.iter(|| {
pool.scope(|s| {
for _ in 0..thread_count {
let db = Arc::clone(&db);
let keys = keys.clone();
s.spawn(move |_| {
db.get("concurrent_test", select_random(&keys)).unwrap();
});
}
});
})
});
}

pool_group.finish();
}
Loading
Loading