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

bench: add btree and dash benchmark #16

Merged
merged 4 commits into from
Oct 30, 2024
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
11 changes: 11 additions & 0 deletions .git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

# Run cargo fmt and check if there are any changes
cargo fmt --all -- --check
if [ $? -ne 0 ]; then
echo "❌ Formatting check failed. Please run 'cargo fmt' to format your code."
exit 1
fi

echo "✅ Formatting check passed."
exit 0
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ shumai = "0.2.15"
serde = "1.0.210"
serde_json = "1.0.128"
flurry = "0.5.1"
dashmap = "6.1.0"
mimalloc = { version = "0.1.43", default-features = false }
selfsimilar = "0.1.0"
shuttle = "0.8.0"
Expand Down
126 changes: 125 additions & 1 deletion bench/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use congee::Art;
use rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
use shumai::{config, ShumaiBench};
use std::{cell::UnsafeCell, fmt::Display};
use std::{cell::UnsafeCell, collections::BTreeMap, fmt::Display};

use mimalloc::MiMalloc;

Expand All @@ -26,6 +26,8 @@ impl Display for Workload {
#[derive(Serialize, Clone, Copy, Debug, Deserialize)]
pub enum IndexType {
SingleHashMap,
BTree,
Dash,
Flurry,
ART,
}
Expand Down Expand Up @@ -75,6 +77,76 @@ trait DBIndex: Send + Sync {
) -> usize;
}

struct BTreeMapWrapper {
map: UnsafeCell<BTreeMap<usize, usize>>, // only allow single thread access
}

impl BTreeMapWrapper {
fn new() -> Self {
Self {
map: UnsafeCell::new(BTreeMap::new()),
}
}
}

unsafe impl Send for BTreeMapWrapper {}
unsafe impl Sync for BTreeMapWrapper {}

impl DBIndex for BTreeMapWrapper {
type Guard<'a> = ();

fn pin(&self) -> Self::Guard<'_> {
()
}

fn insert<'a>(&'a self, key: usize, v: usize, _: &Self::Guard<'a>) {
unsafe {
(*self.map.get()).insert(key, v);
}
}

fn get<'a>(&'a self, key: &usize, _: &Self::Guard<'a>) -> Option<usize> {
unsafe { (*self.map.get()).get(key).cloned() }
}

fn update<'a>(
&'a self,
key: &usize,
new: usize,
_: &Self::Guard<'a>,
) -> Option<(usize, Option<usize>)> {
unsafe {
(*self.map.get())
.entry(*key)
.and_modify(|v| *v = new)
.or_insert(new);
}
Some((*key, Some(*key)))
}

fn scan<'a>(
&'a self,
low_key: &usize,
high_key: &usize,
results: &mut [(usize, usize)],
_: &Self::Guard<'a>,
) -> usize {
unsafe {
let map = &*self.map.get();
let range = map.range(low_key..=high_key);
let mut count = 0;
for (k, v) in range {
if count >= results.len() {
break;
}
results[count] = (*k, *v);
count += 1;
}
count
}
}
}

impl DBIndex for Art<usize, usize> {
type Guard<'a> = crossbeam_epoch::Guard;

Expand Down Expand Up @@ -211,6 +283,42 @@ impl DBIndex for flurry::HashMap<usize, usize> {
}
}

impl DBIndex for dashmap::DashMap<usize, usize> {
type Guard<'a> = ();

fn pin(&self) -> Self::Guard<'_> {
()
}

fn insert<'a>(&'a self, key: usize, v: usize, _: &Self::Guard<'a>) {
self.insert(key, v);
}

fn get<'a>(&'a self, key: &usize, _: &Self::Guard<'a>) -> Option<usize> {
self.get(key).map(|v| *v)
}

fn update<'a>(
&'a self,
key: &usize,
new: usize,
_: &Self::Guard<'a>,
) -> Option<(usize, Option<usize>)> {
self.alter(key, |_, _| new);
Some((*key, Some(*key)))
}

fn scan<'a>(
&'a self,
low_key: &usize,
high_key: &usize,
results: &mut [(usize, usize)],
_: &Self::Guard<'a>,
) -> usize {
unimplemented!("DashMap can't scan")
}
}

impl<Index: DBIndex> ShumaiBench for TestBench<Index> {
type Config = Basic;
type Result = usize;
Expand Down Expand Up @@ -280,6 +388,22 @@ fn main() {

for c in config.iter() {
match c.index_type {
IndexType::BTree => {
let mut test_bench = TestBench {
index: BTreeMapWrapper::new(),
initial_cnt: 50_000_000,
};
let result = shumai::run(&mut test_bench, c, repeat);
result.write_json().unwrap();
}
IndexType::Dash => {
let mut test_bench = TestBench {
index: dashmap::DashMap::new(),
initial_cnt: 50_000_000,
};
let result = shumai::run(&mut test_bench, c, repeat);
result.write_json().unwrap();
}
IndexType::Flurry => {
let mut test_bench = TestBench {
index: flurry::HashMap::new(),
Expand Down
17 changes: 15 additions & 2 deletions bench/benchmark.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@ name = "basic"
threads = [1, 2, 4, 8, 32]
time = 3
workload = ["ReadOnly", "InsertOnly", "UpdateOnly", "ScanOnly"]
index_type = ["Flurry", "ART"]
index_type = ["Flurry", "ART", "Dash", "BTree"]

[[Basic]]
name = "single-thread"
threads = [1]
time = 3
workload = ["ReadOnly"]
index_type = ["ART", "SingleHashMap"]
index_type = ["ART", "SingleHashMap", "BTree", "Dash"]

[[Basic]]
name = "dash"
threads = [1, 2, 4, 8, 32]
time = 3
workload = ["ReadOnly", "InsertOnly", "UpdateOnly"]
index_type = ["Dash"]

[[Basic]]
name = "btree"
threads = [1]
time = 3
workload = ["ReadOnly", "InsertOnly", "UpdateOnly"]
index_type = ["BTree"]

[[Scan]]
name = "scan"
Expand Down
Loading