Skip to content

Commit

Permalink
Add ReDB benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiemh committed Sep 20, 2024
1 parent d830751 commit 7ab3508
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 4 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/crud-bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ jobs:
# MongoDB
- database: mongodb
description: MongoDB
# ReDB
- database: redb
description: ReDB
# RocksDB
- database: rocksdb
description: RocksDB
Expand Down Expand Up @@ -175,4 +178,7 @@ jobs:
run: ${{ github.workspace }}/artifacts/crud-bench -d ${{ matrix.database }} -s 30000 -t 32

- name: Run benchmarks (100,000 samples / 32 threads)
run: ${{ github.workspace }}/artifacts/crud-bench -d ${{ matrix.database }} -s 100000 -t 32
run: ${{ github.workspace }}/artifacts/crud-bench -d ${{ matrix.database }} -s 100000 -t 32

- name: Run benchmarks (250,000 samples / 128 threads)
run: ${{ github.workspace }}/artifacts/crud-bench -d ${{ matrix.database }} -s 250000 -t 128
8 changes: 5 additions & 3 deletions crud-bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ license = "Apache-2.0"
readme = "README.md"

[features]
default = ["redis", "rocksdb", "postgres", "mongodb", "surrealkv", "surrealdb"]
redis = ["dep:redis"]
default = ["redis", "redb", "rocksdb", "postgres", "mongodb", "surrealkv", "surrealdb"]
mongodb = ["dep:mongodb"]
rocksdb = ["dep:rocksdb"]
postgres = ["dep:tokio-postgres"]
redb = ["dep:redb"]
redis = ["dep:redis"]
rocksdb = ["dep:rocksdb"]
surrealdb = ["dep:surrealdb"]
surrealkv = ["dep:surrealkv"]

Expand All @@ -29,6 +30,7 @@ log = "0.4.22"
mongodb = { version = "2.8.2", optional = true }
rand = { version = "0.8.5", features = ["small_rng"] }
rayon = "1.10.0"
redb = { version = "2.1.3", optional = true }
redis = { version = "0.24.0", features = ["tokio-comp"], optional = true }
rocksdb = { git = "https://github.com/surrealdb/rust-rocksdb", features = ["lz4", "snappy"], optional = true }
serde = { version = "1.0.210", features = ["derive"] }
Expand Down
9 changes: 9 additions & 0 deletions crud-bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::dry::DryClientProvider;
use crate::mongodb::MongoDBClientProvider;
#[cfg(feature = "postgres")]
use crate::postgres::PostgresClientProvider;
#[cfg(feature = "rocksdb")]
use crate::redb::ReDBClientProvider;
#[cfg(feature = "redis")]
use crate::redis::RedisClientProvider;
#[cfg(feature = "rocksdb")]
Expand All @@ -24,6 +26,7 @@ mod docker;
mod dry;
mod mongodb;
mod postgres;
mod redb;
mod redis;
mod rocksdb;
mod surrealdb;
Expand Down Expand Up @@ -56,6 +59,8 @@ pub(crate) struct Args {
#[derive(ValueEnum, Debug, Clone)]
pub(crate) enum Database {
Dry,
#[cfg(feature = "redb")]
Redb,
#[cfg(feature = "rocksdb")]
Rocksdb,
#[cfg(feature = "surrealkv")]
Expand All @@ -80,6 +85,8 @@ impl Database {
fn start_docker(&self, image: Option<String>) -> Option<DockerContainer> {
let params: DockerParams = match self {
Database::Dry => return None,
#[cfg(feature = "redb")]
Database::Redb => return None,
#[cfg(feature = "rocksdb")]
Database::Rocksdb => return None,
#[cfg(feature = "surrealkv")]
Expand Down Expand Up @@ -107,6 +114,8 @@ impl Database {
async fn run(&self, benchmark: &Benchmark) -> Result<BenchmarkResult> {
match self {
Database::Dry => benchmark.run(DryClientProvider::default()).await,
#[cfg(feature = "redb")]
Database::Redb => benchmark.run(ReDBClientProvider::setup().await?).await,
#[cfg(feature = "rocksdb")]
Database::Rocksdb => benchmark.run(RocksDBClientProvider::setup().await?).await,
#[cfg(feature = "surrealkv")]
Expand Down
89 changes: 89 additions & 0 deletions crud-bench/src/redb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#![cfg(feature = "redb")]

use anyhow::Error;
use anyhow::Result;
use redb::{Database, TableDefinition};
use std::sync::Arc;

use crate::benchmark::{BenchmarkClient, BenchmarkEngine, Record};

const TABLE: TableDefinition<&[u8], Vec<u8>> = TableDefinition::new("test");

pub(crate) struct ReDBClientProvider {
db: Arc<Database>,
}

impl ReDBClientProvider {
pub(crate) async fn setup() -> Result<Self, Error> {
Ok(Self {
db: Arc::new(Database::create("redb")?),
})
}
}

impl BenchmarkEngine<ReDBClient> for ReDBClientProvider {
async fn create_client(&self, _: Option<String>) -> Result<ReDBClient> {
Ok(ReDBClient {
db: self.db.clone(),
})
}
}

pub(crate) struct ReDBClient {
db: Arc<Database>,
}

impl BenchmarkClient for ReDBClient {
async fn read(&mut self, key: i32) -> Result<()> {
let key = &key.to_ne_bytes();
// Create a new transaction
let txn = self.db.begin_read()?;
// Open the database table
let tab = txn.open_table(TABLE)?;
// Process the data
let read: Option<_> = tab.get(key.as_ref())?;
assert!(read.is_some());
Ok(())
}

async fn create(&mut self, key: i32, record: &Record) -> Result<()> {
let key = &key.to_ne_bytes();
let val = serde_json::to_vec(record)?;
// Create a new transaction
let txn = self.db.begin_write()?;
// Open the database table
let mut tab = txn.open_table(TABLE)?;
// Process the data
tab.insert(key.as_ref(), val)?;
drop(tab);
txn.commit()?;
Ok(())
}

async fn update(&mut self, key: i32, record: &Record) -> Result<()> {
let key = &key.to_ne_bytes();
let val = serde_json::to_vec(record)?;
// Create a new transaction
let txn = self.db.begin_write()?;
// Open the database table
let mut tab = txn.open_table(TABLE)?;
// Process the data
tab.insert(key.as_ref(), val)?;
drop(tab);
txn.commit()?;
Ok(())
}

async fn delete(&mut self, key: i32) -> Result<()> {
let key = &key.to_ne_bytes();
// Create a new transaction
let txn = self.db.begin_write()?;
// Open the database table
let mut tab = txn.open_table(TABLE)?;
// Process the data
tab.remove(key.as_ref())?;
drop(tab);
txn.commit()?;
Ok(())
}
}

0 comments on commit 7ab3508

Please sign in to comment.