Compare commits

13 Commits

Author SHA1 Message Date
abby fe6b1de1f6 refine test & support mace-0.0.27 2026-02-20 21:41:22 +08:00
abby 7bd02bb652 0.0.25 support 2026-02-05 10:05:53 +08:00
abby 36c15dc0d0 support mace 0.0.24 2026-01-19 11:53:17 +08:00
abby da89a88b40 support mace 0.0.23 2026-01-02 15:07:48 +08:00
abby 5d92699980 support mace 0.0.22 2025-12-26 16:11:07 +08:00
abby 169fe3871a support mace 0.0.21 2025-12-21 13:24:32 +08:00
abby 09345dc029 update 2025-12-21 10:06:12 +08:00
abby 5b513c4bdb update readme 2025-12-20 16:15:48 +08:00
abby ccb108c33d fix test 2025-12-20 16:13:03 +08:00
abby 75e8b90cb9 fix readme 2025-11-21 12:09:40 +08:00
abby 6c1ea2f56a add scan test 2025-11-21 12:08:04 +08:00
abby 9f6434dba1 add readme 2025-11-16 10:48:41 +08:00
abby 08b1fcec9a merge blodb 2025-11-16 10:43:06 +08:00
18 changed files with 340 additions and 123 deletions
-1
View File
@@ -5,7 +5,6 @@
/scripts/lib64
/scripts/share/
/scripts/.gitignore
*.png
*.csv
pyvenv.cfg
Cargo.lock
+1 -1
View File
@@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2024"
[dependencies]
mace = { git = "https://github.com/abbycin/mace" }
mace-kv = "0.0.27"
clap = { version = "4.5.48", features = ["derive"] }
rand = "0.9.2"
log = "0.4.22"
+33
View File
@@ -0,0 +1,33 @@
# mace 0.0.27 vs rocksdb 10.4.2
## sequential insert
![mace_sequential_insert](./scripts/mace_sequential_insert.png)
![rocksdb_sequential_insert](./scripts/rocksdb_sequential_insert.png)
## random insert
![mace_random_insert](./scripts/mace_random_insert.png)
![rocksdb_random_insert](./scripts/rocksdb_random_insert.png)
---
## random get (warm get)
![mace_get](./scripts/mace_get.png)
![rocksdb_get](./scripts/rocksdb_get.png)
---
# mixed perfomance (hot get)
![mace_mixed](./scripts/mace_mixed.png)
![rockdb_mixed](./scripts/rocksdb_mixed.png)
# sequential scan (warm scan)
![mace_scan](./scripts/mace_scan.png)
![rocksdb_scan](./scripts/rocksdb_scan.png)
+143 -27
View File
@@ -1,6 +1,9 @@
#include <algorithm>
#include <atomic>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <fmt/base.h>
#include <fmt/format.h>
#include <memory>
#include <random>
@@ -8,6 +11,7 @@
#include <rocksdb/db.h>
#include <rocksdb/env.h>
#include <rocksdb/options.h>
#include <rocksdb/slice.h>
#include <rocksdb/table.h>
#include <rocksdb/utilities/optimistic_transaction_db.h>
#include <rocksdb/utilities/transaction.h>
@@ -18,14 +22,37 @@
#include <format>
#include <string>
#include <pthread.h>
#include <sched.h>
#include <unistd.h>
#include "CLI/CLI.hpp"
#include "instant.h"
template<class T>
static void black_box(const T &t) {
asm volatile("" ::"m"(t) : "memory");
}
static size_t cores_online() {
auto n = ::sysconf(_SC_NPROCESSORS_ONLN);
return n > 0 ? static_cast<size_t>(n) : 1;
}
static void bind_core(size_t tid) {
cpu_set_t set;
CPU_ZERO(&set);
auto core = static_cast<int>(tid % cores_online());
CPU_SET(core, &set);
(void) pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &set);
}
struct Args {
size_t threads;
size_t iterations;
size_t key_size;
size_t value_size;
size_t blob_size;
size_t insert_ratio;
bool random;
std::string mode;
@@ -39,19 +66,21 @@ int main(int argc, char *argv[]) {
.iterations = 100000,
.key_size = 16,
.value_size = 1024,
.blob_size = 8192,
.insert_ratio = 30,
.mode = "insert",
.path = "/tmp/rocksdb_tmp",
};
app.add_option("-m,--mode", args.mode, "Mode: insert, get, mixed");
app.add_option("-m,--mode", args.mode, "Mode: insert, get, mixed, scan");
app.add_option("-t,--threads", args.threads, "Threads");
app.add_option("-k,--key-size", args.key_size, "Key Size");
app.add_option("-v,--value-size", args.value_size, "Value Size");
app.add_option("-b,--blob-size", args.blob_size, "Blob Size");
app.add_option("-i,--iterations", args.iterations, "Iterations");
app.add_option("-r,--insert-ratio", args.insert_ratio, "Insert Ratio for mixed mode");
app.add_option("-p,--path", args.path, "DataBase Home");
app.add_option("--random", args.random, "Shuffle insert keys");
app.add_flag("--random", args.random, "Shuffle insert keys");
CLI11_PARSE(app, argc, argv);
@@ -65,7 +94,12 @@ int main(int argc, char *argv[]) {
return 1;
}
if (args.mode != "insert" && args.mode != "get" && args.mode != "mixed") {
if (args.threads == 0) {
fmt::println("Error: threads must be greater than 0");
return 1;
}
if (args.mode != "insert" && args.mode != "get" && args.mode != "mixed" && args.mode != "scan") {
fmt::println("Error: Invalid mode");
return 1;
}
@@ -80,11 +114,31 @@ int main(int argc, char *argv[]) {
return 1;
}
auto find_upper_bound = [](std::string prefix) {
std::string upper_bound_key = prefix;
for (int i = upper_bound_key.length() - 1; i >= 0; --i) {
if ((unsigned char) upper_bound_key[i] != 0xff) {
upper_bound_key[i] = (unsigned char) upper_bound_key[i] + 1;
upper_bound_key.resize(i + 1);
break;
}
if (i == 0) {
upper_bound_key = "";
break;
}
}
return upper_bound_key;
};
rocksdb::ColumnFamilyOptions cfo{};
cfo.enable_blob_files = true;
cfo.min_blob_size = 8192;
// use 1GB block cache
auto cache = rocksdb::NewLRUCache(1 << 30);
cfo.min_blob_size = args.blob_size;
// rocksdb::BlockBasedTableOptions top{};
// top.use_delta_encoding = false;
// cfo.table_factory.reset(rocksdb::NewBlockBasedTableFactory(top));
// use 3GB block cache
auto cache = rocksdb::NewLRUCache(3 << 30);
rocksdb::BlockBasedTableOptions table_options{};
table_options.block_cache = cache;
cfo.table_factory.reset(NewBlockBasedTableFactory(table_options));
@@ -102,7 +156,6 @@ int main(int argc, char *argv[]) {
options.enable_pipelined_write = true;
options.env->SetBackgroundThreads(4, rocksdb::Env::Priority::HIGH);
auto ropt = rocksdb::ReadOptions();
auto wopt = rocksdb::WriteOptions();
wopt.no_slowdown = true;
// wopt.disableWAL = true;
@@ -111,34 +164,38 @@ int main(int argc, char *argv[]) {
std::atomic<uint64_t> total_op{0};
rocksdb::OptimisticTransactionDB *db;
auto b = nm::Instant::now();
std::mutex mtx{};
std::vector<rocksdb::ColumnFamilyHandle *> handles{};
auto s = rocksdb::OptimisticTransactionDB::Open(options, args.path, cfd, &handles, &db);
assert(s.ok());
std::barrier barrier{static_cast<ptrdiff_t>(args.threads)};
std::barrier ready_barrier{static_cast<ptrdiff_t>(args.threads + 1)};
std::barrier start_barrier{static_cast<ptrdiff_t>(args.threads + 1)};
std::random_device rd{};
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist(0, 100);
std::string val(args.value_size, 'x');
std::vector<size_t> key_counts(args.threads, args.iterations / args.threads);
for (size_t i = 0; i < args.iterations % args.threads; ++i) {
key_counts[i] += 1;
}
keys.reserve(args.threads);
for (size_t tid = 0; tid < args.threads; ++tid) {
std::vector<std::string> key{};
for (size_t i = 0; i < args.iterations; ++i) {
key.reserve(key_counts[tid]);
for (size_t i = 0; i < key_counts[tid]; ++i) {
auto tmp = std::format("key_{}_{}", tid, i);
tmp.resize(args.key_size, 'x');
key.emplace_back(std::move(tmp));
}
if (args.random) {
std::shuffle(keys.begin(), keys.end(), gen);
if (args.mode == "get" || args.random) {
std::shuffle(key.begin(), key.end(), gen);
}
keys.emplace_back(std::move(key));
}
auto *handle = handles[0];
if (args.mode == "get") {
if (args.mode == "get" || args.mode == "scan") {
auto *kv = db->BeginTransaction(wopt);
for (size_t tid = 0; tid < args.threads; ++tid) {
auto *tk = &keys[tid];
@@ -154,21 +211,51 @@ int main(int argc, char *argv[]) {
// re-open db
s = rocksdb::OptimisticTransactionDB::Open(options, args.path, cfd, &handles, &db);
assert(s.ok());
}
handle = handles[0];
for (size_t tid = 0; tid < args.threads; ++tid) {
auto *tk = &keys[tid];
wg.emplace_back([&] {
std::string rval(args.value_size, '0');
barrier.arrive_and_wait();
if (mtx.try_lock()) {
b = nm::Instant::now();
mtx.unlock();
// simulate common use cases
std::uniform_int_distribution<size_t> tid_dist(0, args.threads - 1);
for (size_t i = 0; i < args.iterations; ++i) {
auto tid = tid_dist(gen);
if (keys[tid].empty()) {
continue;
}
std::uniform_int_distribution<size_t> key_dist(0, keys[tid].size() - 1);
const auto &k = keys[tid][key_dist(gen)];
auto s = db->Get(rocksdb::ReadOptions(), k, &val);
if (!s.ok()) {
std::terminate();
}
}
}
auto *snapshot = db->GetSnapshot();
auto base_seed = rd();
for (size_t tid = 0; tid < args.threads; ++tid) {
wg.emplace_back([&, tid] {
bind_core(tid);
std::string rval(args.value_size, '0');
auto prefix = std::format("key_{}_", tid);
auto ropt = rocksdb::ReadOptions();
auto upper_bound = find_upper_bound(prefix);
auto upper_bound_slice = rocksdb::Slice(upper_bound);
if (!upper_bound.empty()) {
ropt.iterate_upper_bound = &upper_bound_slice;
}
auto *tk = &keys[tid];
ropt.prefix_same_as_start = true;
ropt.snapshot = snapshot;
size_t round = 0;
std::mt19937 mixed_gen(static_cast<uint32_t>(base_seed) ^ static_cast<uint32_t>(0x9e3779b9U * (tid + 1)));
std::uniform_int_distribution<int> mixed_dist(0, 99);
ready_barrier.arrive_and_wait();
start_barrier.arrive_and_wait();
if (args.mode == "insert") {
for (auto &key: *tk) {
round += 1;
auto *kv = db->BeginTransaction(wopt);
kv->Put(handle, key, val);
kv->Commit();
@@ -177,6 +264,7 @@ int main(int argc, char *argv[]) {
} else if (args.mode == "get") {
for (auto &key: *tk) {
round += 1;
auto *kv = db->BeginTransaction(wopt);
kv->Get(ropt, handle, key, &rval);
kv->Commit();
@@ -184,7 +272,8 @@ int main(int argc, char *argv[]) {
}
} else if (args.mode == "mixed") {
for (auto &key: *tk) {
auto is_insert = dist(gen) < args.insert_ratio;
round += 1;
auto is_insert = mixed_dist(mixed_gen) < static_cast<int>(args.insert_ratio);
auto *kv = db->BeginTransaction(wopt);
if (is_insert) {
kv->Put(handle, key, val);
@@ -194,11 +283,30 @@ int main(int argc, char *argv[]) {
kv->Commit();
delete kv;
}
} else if (args.mode == "scan") {
// ropt.pin_data = true;
auto *iter = db->NewIterator(ropt);
iter->Seek(prefix);
size_t n = 0;
while (iter->Valid()) {
round += 1;
auto k = iter->key();
auto v = iter->value();
black_box(k);
black_box(v);
iter->Next();
n += 1;
}
total_op.fetch_add(args.iterations, std::memory_order::relaxed);
delete iter;
}
total_op.fetch_add(round, std::memory_order::relaxed);
});
}
ready_barrier.arrive_and_wait();
b = nm::Instant::now();
start_barrier.arrive_and_wait();
for (auto &w: wg) {
w.join();
}
@@ -208,8 +316,16 @@ int main(int argc, char *argv[]) {
return args.mode == "insert" ? 100 : 0;
}();
uint64_t ops = total_op.load(std::memory_order_relaxed) / b.elapse_sec();
if (args.mode == "insert") {
if (args.random) {
args.mode = "random_insert";
} else {
args.mode = "sequential_insert";
}
}
fmt::println("{},{},{},{},{},{},{}", args.mode, args.threads, args.key_size, args.value_size, ratio, (uint64_t) ops,
(uint64_t) b.elapse_ms());
db->ReleaseSnapshot(snapshot);
delete handle;
delete db;
std::filesystem::remove_all(args.path);
+32 -18
View File
@@ -1,42 +1,56 @@
#!/usr/bin/env bash
pushd .
cd ..
cargo build --release 1>/dev/null 2> /dev/null
set -euo pipefail
if [ "$#" -ne 1 ]
then
printf "\033[m$0 path\033[0m\n"
exit 1
fi
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
root_dir="$(cd -- "${script_dir}/.." && pwd)"
cargo build --release --manifest-path "${root_dir}/Cargo.toml" 1>/dev/null 2>/dev/null
function samples() {
export RUST_BACKTRACE=full
kv_sz=(16 16 100 1024 1024 1024 16 10240)
mode=(insert get mixed scan)
# set -x
db_root="$1"
cnt=10000
cnt=100000
for ((i = 1; i <= $(nproc); i *= 2))
do
for ((j = 0; j < ${#kv_sz[@]}; j += 2))
do
./target/release/kv_bench --path /home/abby/mace_bench --threads $i --iterations $cnt --mode insert --key-size ${kv_sz[j]} --value-size ${kv_sz[j+1]}
for ((k = 0; k < ${#mode[@]}; k += 1))
do
if [ "${mode[k]}" == "insert" ]
then
"${root_dir}/target/release/kv_bench" --path "${db_root}" --threads "${i}" --iterations "${cnt}" --mode "${mode[k]}" --key-size "${kv_sz[j]}" --value-size "${kv_sz[j+1]}" --random
if test $? -ne 0
then
echo "insert threads $i ksz ${kv_sz[j]} vsz ${kv_sz[j+1]} fail"
echo "${mode[k]} threads $i ksz ${kv_sz[j]} vsz ${kv_sz[j+1]} random fail"
exit 1
fi
./target/release/kv_bench --path /home/abby/mace_bench --threads $i --iterations $cnt --mode get --key-size ${kv_sz[j]} --value-size ${kv_sz[j+1]}
fi
"${root_dir}/target/release/kv_bench" --path "${db_root}" --threads "${i}" --iterations "${cnt}" --mode "${mode[k]}" --key-size "${kv_sz[j]}" --value-size "${kv_sz[j+1]}"
if test $? -ne 0
then
echo "get threads $i ksz ${kv_sz[j]} vsz ${kv_sz[j+1]} fail"
exit 1
fi
./target/release/kv_bench --path /home/abby/mace_bench --threads $i --iterations $cnt --mode mixed --key-size ${kv_sz[j]} --value-size ${kv_sz[j+1]} --insert-ratio 30
if test $? -ne 0
then
echo "mixed threads $i ksz ${kv_sz[j]} vsz ${kv_sz[j+1]} fail"
echo "${mode[k]} threads $i ksz ${kv_sz[j]} vsz ${kv_sz[j+1]} fail"
exit 1
fi
done
done
done
}
echo mode,threads,key_size,value_size,insert_ratio,ops,elasped > scripts/mace.csv
samples 2>> scripts/mace.csv
popd
./bin/python plot.py mace.csv
echo mode,threads,key_size,value_size,insert_ratio,ops,elasped > "${script_dir}/mace.csv"
samples "$1" 2>> "${script_dir}/mace.csv"
if [ -x "${script_dir}/bin/python" ]; then
(cd "${script_dir}" && "${script_dir}/bin/python" plot.py mace.csv)
else
(cd "${script_dir}" && python3 plot.py mace.csv)
fi
Binary file not shown.

After

Width:  |  Height:  |  Size: 133 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 135 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 143 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 125 KiB

+6 -2
View File
@@ -5,8 +5,12 @@ import sys
def real_mode(m):
if m == "mixed":
return "MIXED (70% Get, 30% Insert)"
return m.upper()
return "Mixed (70% Get, 30% Insert)"
elif m == "get":
return "Random Get"
elif m == "scan":
return "Sequential Scan"
return m.capitalize()
name = sys.argv[1]
prefix = name.split(".")[0]
+34 -19
View File
@@ -1,41 +1,56 @@
#!/usr/bin/env bash
pushd .
cd ../rocksdb
cmake --preset release 1>/dev/null 2>/dev/null
cmake --build --preset release 1>/dev/null 2>/dev/null
set -euo pipefail
if [ "$#" -ne 1 ]
then
printf "\033[m$0 path\033[0m\n"
exit 1
fi
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
root_dir="$(cd -- "${script_dir}/.." && pwd)"
rocksdb_dir="${root_dir}/rocksdb"
(cd "${rocksdb_dir}" && cmake --preset release 1>/dev/null 2>/dev/null)
(cd "${rocksdb_dir}" && cmake --build --preset release 1>/dev/null 2>/dev/null)
function samples() {
kv_sz=(16 16 100 1024 1024 1024 16 10240)
mode=(insert get mixed scan)
# set -x
cnt=10000
db_root="$1"
cnt=100000
for ((i = 1; i <= $(nproc); i *= 2))
do
for ((j = 0; j < ${#kv_sz[@]}; j += 2))
do
./build/release/rocksdb_bench --path /home/abby/rocksdb_tmp --threads $i --iterations $cnt --mode insert --key-size ${kv_sz[j]} --value-size ${kv_sz[j+1]}
for ((k = 0; k < ${#mode[@]}; k += 1))
do
if [ "${mode[k]}" == "insert" ]
then
"${rocksdb_dir}/build/release/rocksdb_bench" --path "${db_root}" --threads "${i}" --iterations "${cnt}" --mode "${mode[k]}" --key-size "${kv_sz[j]}" --value-size "${kv_sz[j+1]}" --random
if test $? -ne 0
then
echo "insert threads $i ksz ${kv_sz[j]} vsz ${kv_sz[j+1]} fail"
echo "${mode[k]} threads $i ksz ${kv_sz[j]} vsz ${kv_sz[j+1]} random fail"
exit 1
fi
./build/release/rocksdb_bench --path /home/abby/rocksdb_tmp --threads $i --iterations $cnt --mode get --key-size ${kv_sz[j]} --value-size ${kv_sz[j+1]}
fi
"${rocksdb_dir}/build/release/rocksdb_bench" --path "${db_root}" --threads "${i}" --iterations "${cnt}" --mode "${mode[k]}" --key-size "${kv_sz[j]}" --value-size "${kv_sz[j+1]}"
if test $? -ne 0
then
echo "get threads $i ksz ${kv_sz[j]} vsz ${kv_sz[j+1]} fail"
exit 1
fi
./build/release/rocksdb_bench --path /home/abby/rocksdb_tmp --threads $i --iterations $cnt --mode mixed --key-size ${kv_sz[j]} --value-size ${kv_sz[j+1]} --insert-ratio 30
if test $? -ne 0
then
echo "mixed threads $i ksz ${kv_sz[j]} vsz ${kv_sz[j+1]} fail"
echo "${mode[k]} threads $i ksz ${kv_sz[j]} vsz ${kv_sz[j+1]} fail"
exit 1
fi
done
done
done
}
echo mode,threads,key_size,value_size,insert_ratio,ops,elapsed > ../scripts/rocksdb.csv
samples 1>> ../scripts/rocksdb.csv
popd
./bin/python plot.py rocksdb.csv
echo mode,threads,key_size,value_size,insert_ratio,ops,elapsed > "${script_dir}/rocksdb.csv"
samples "$1" 1>> "${script_dir}/rocksdb.csv"
if [ -x "${script_dir}/bin/python" ]; then
(cd "${script_dir}" && "${script_dir}/bin/python" plot.py rocksdb.csv)
else
(cd "${script_dir}" && python3 plot.py rocksdb.csv)
fi
Binary file not shown.

After

Width:  |  Height:  |  Size: 143 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 127 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

+77 -41
View File
@@ -49,10 +49,10 @@ struct Args {
fn main() {
#[cfg(target_os = "linux")]
{
Logger::init().add_file("/Data/x.log", true);
Logger::init().add_file("/tmp/x.log", true);
log::set_max_level(log::LevelFilter::Info);
}
let args = Args::parse();
let mut args = Args::parse();
let path = Path::new(&args.path);
@@ -66,6 +66,16 @@ fn main() {
exit(1);
}
if args.threads == 0 {
eprintln!("Error: threads must be greater than 0");
exit(1);
}
if !matches!(args.mode.as_str(), "insert" | "get" | "mixed" | "scan") {
eprintln!("Error: Invalid mode");
exit(1);
}
if args.key_size < 16 || args.value_size < 16 {
eprintln!("Error: key_size or value_size too small, must >= 16");
exit(1);
@@ -81,69 +91,84 @@ fn main() {
opt.sync_on_write = false;
opt.over_provision = true; // large value will use lots of memeory
opt.inline_size = args.blob_size;
opt.tmp_store = args.mode != "get";
opt.tmp_store = args.mode != "get" && args.mode != "scan";
opt.cache_capacity = 3 << 30;
let mut saved = opt.clone();
saved.tmp_store = false;
let mut db = Mace::new(opt.validate().unwrap()).unwrap();
db.disable_gc();
let mut bkt = db.new_bucket("default").unwrap();
let mut rng = rand::rng();
let value = Arc::new(vec![b'0'; args.value_size]);
let mut key_counts = vec![args.iterations / args.threads; args.threads];
for cnt in key_counts.iter_mut().take(args.iterations % args.threads) {
*cnt += 1;
}
for tid in 0..args.threads {
let mut tk = Vec::with_capacity(args.iterations);
for i in 0..args.iterations {
let mut tk = Vec::with_capacity(key_counts[tid]);
for i in 0..key_counts[tid] {
let mut key = format!("key_{tid}_{i}").into_bytes();
key.resize(args.key_size, b'x');
tk.push(key);
}
if args.random {
if args.random || args.mode == "get" {
tk.shuffle(&mut rng);
}
keys.push(tk);
}
if args.mode == "get" {
let pre_tx = db.begin().unwrap();
if args.mode == "get" || args.mode == "scan" {
let pre_tx = bkt.begin().unwrap();
(0..args.threads).for_each(|tid| {
for i in 0..args.iterations {
pre_tx.put(&keys[tid][i], &*value).unwrap();
for k in &keys[tid] {
pre_tx.put(k, &*value).unwrap();
}
});
pre_tx.commit().unwrap();
drop(bkt);
drop(db);
// re-open db
saved.tmp_store = true;
db = Mace::new(saved.validate().unwrap()).unwrap();
bkt = db.get_bucket("default").unwrap();
// simulate common use cases
for _ in 0..args.iterations {
let tid = rng.random_range(0..args.threads);
let Some(k) = keys[tid].choose(&mut rng) else {
continue;
};
let view = bkt.view().unwrap();
view.get(k).unwrap();
}
}
let barrier = Arc::new(std::sync::Barrier::new(args.threads));
let ready_barrier = Arc::new(std::sync::Barrier::new(args.threads + 1));
let start_barrier = Arc::new(std::sync::Barrier::new(args.threads + 1));
let total_ops = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let start_time = Arc::new(std::sync::Mutex::new(Instant::now()));
let h: Vec<JoinHandle<()>> = (0..args.threads)
.map(|tid| {
let db = db.clone();
let db = bkt.clone();
let tk: &Vec<Vec<u8>> = unsafe { std::mem::transmute(&keys[tid]) };
let total_ops = total_ops.clone();
let barrier = Arc::clone(&barrier);
let ready_barrier = Arc::clone(&ready_barrier);
let start_barrier = Arc::clone(&start_barrier);
let mode = args.mode.clone();
let insert_ratio = args.insert_ratio;
let st = start_time.clone();
let val = value.clone();
let prefix = format!("key_{tid}_");
std::thread::spawn(move || {
// coreid::bind_core(tid);
barrier.wait();
{
if let Ok(mut guard) = st.try_lock() {
*guard = Instant::now();
}
}
coreid::bind_core(tid);
let mut round = 0;
ready_barrier.wait();
start_barrier.wait();
match mode.as_str() {
"insert" => {
for key in tk {
round += 1;
let tx = db.begin().unwrap();
tx.put(key.as_slice(), val.as_slice()).unwrap();
tx.commit().unwrap();
@@ -151,13 +176,16 @@ fn main() {
}
"get" => {
for key in tk {
round += 1;
let tx = db.view().unwrap();
tx.get(key).unwrap();
let x = tx.get(key).unwrap();
std::hint::black_box(x);
}
}
"mixed" => {
for key in tk {
let is_insert = rand::random_range(0..100) < insert_ratio;
round += 1;
if is_insert {
let tx = db.begin().unwrap();
@@ -165,37 +193,39 @@ fn main() {
tx.commit().unwrap();
} else {
let tx = db.view().unwrap();
let _ = tx.get(key); // not found
let x = tx.get(key); // not found
let _ = std::hint::black_box(x);
}
}
}
"scan" => {
let view = db.view().unwrap();
let iter = view.seek(prefix);
for x in iter {
round += 1;
std::hint::black_box(x);
}
}
_ => panic!("Invalid mode"),
}
total_ops.fetch_add(args.iterations, std::sync::atomic::Ordering::Relaxed);
total_ops.fetch_add(round, std::sync::atomic::Ordering::Relaxed);
})
})
.collect();
ready_barrier.wait();
let start_time = Instant::now();
start_barrier.wait();
for x in h {
x.join().unwrap();
}
let test_start = start_time.lock().unwrap();
let duration = test_start.elapsed();
let duration = start_time.elapsed();
let total = total_ops.load(std::sync::atomic::Ordering::Relaxed);
let ops = (total as f64 / duration.as_secs_f64()) as usize;
// println!("{:<20} {}", "Test Mode:", args.mode);
// println!("{:<20} {}", "Threads:", args.threads);
// println!("{:<20} {}", "Iterations", args.iterations);
// println!("{:<20} {}B", "Key Size:", args.key_size);
// println!("{:<20} {}B", "Value Size:", args.value_size);
// println!("{:<20} {ops}", "Total Ops:");
// if args.mode == "mixed" {
// println!("{:<20} {}%", "Insert Ratio:", args.insert_ratio);
// }
let ratio = if args.mode == "mixed" {
args.insert_ratio
} else if args.mode == "insert" {
@@ -203,9 +233,15 @@ fn main() {
} else {
0
};
// eprintln!("mode,threads,key_size,value_size,insert_ratio,ops");
if args.mode == "insert" {
if args.random {
args.mode = "random_insert".into();
} else {
args.mode = "sequential_insert".into();
}
}
eprintln!(
"{},{},{},{},{},{:.2},{}",
"{},{},{},{},{},{},{}",
args.mode,
args.threads,
args.key_size,