commit cd851adc7eb58ede317df59572381f637d1db41c Author: abbycin Date: Sat Aug 2 16:14:27 2025 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1861325 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +/target +/scripts/bin +/scripts/include +/scripts/lib +/scripts/lib64 +/scripts/shares/ +/scripts/.gitignore +*.png +*.csv +pyvenv.cfg diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..662c2f9 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,547 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "anstream" +version = "0.6.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "301af1932e46185686725e0fad2f8f2aa7da69dd70bf6ecc44d6b703844a3933" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8bdeb6047d8983be085bab0ba1472e6dc604e7041dbf6fcd5e71523014fae9" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "403f75924867bb1033c59fbf0797484329750cfbe3c4325cd33127941fabc882" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "bitflags" +version = "2.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" + +[[package]] +name = "cfg-if" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268" + +[[package]] +name = "clap" +version = "4.5.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed87a9d530bb41a67537289bafcac159cb3ee28460e0a4571123d2a778a6a882" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64f4f3f3c77c94aff3c7e9aac9a2ca1974a5adf392a8bb751e827d6d127ab966" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef4f52386a59ca4c860f7393bcf8abd8dfd91ecccc0f774635ff68e92eeef491" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + +[[package]] +name = "crc32c" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a47af21622d091a8f0fb295b88bc886ac74efcc613efc19f5d0b21de5c89e47" +dependencies = [ + "rustc_version", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + +[[package]] +name = "dashmap" +version = "6.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf" +dependencies = [ + "cfg-if", + "crossbeam-utils", + "hashbrown", + "lock_api", + "once_cell", + "parking_lot_core", +] + +[[package]] +name = "getrandom" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.11.1+wasi-snapshot-preview1", +] + +[[package]] +name = "getrandom" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasi 0.14.2+wasi-0.2.4", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "io" +version = "0.0.2" +source = "git+https://git.o2c.fun/abby/mace.git?branch=task-63#cb9664d141a7254d3d036de8852b76f54b2b8109" +dependencies = [ + "libc", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "kv_bench" +version = "0.1.0" +dependencies = [ + "clap", + "mace", + "rand 0.9.2", +] + +[[package]] +name = "libc" +version = "0.2.174" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" + +[[package]] +name = "lock_api" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" + +[[package]] +name = "mace" +version = "0.0.8" +source = "git+https://git.o2c.fun/abby/mace.git?branch=task-63#cb9664d141a7254d3d036de8852b76f54b2b8109" +dependencies = [ + "crc32c", + "crossbeam-epoch", + "dashmap", + "io", + "log", + "rand 0.8.5", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" + +[[package]] +name = "parking_lot_core" +version = "0.9.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc838d2a56b5b1a6c25f55575dfc605fabb63bb2365f6c2353ef9159aa69e4a5" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + +[[package]] +name = "rand" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" +dependencies = [ + "rand_chacha 0.9.0", + "rand_core 0.9.3", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.3", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.16", +] + +[[package]] +name = "rand_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" +dependencies = [ + "getrandom 0.3.3", +] + +[[package]] +name = "redox_syscall" +version = "0.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5407465600fb0548f1442edf71dd20683c6ed326200ace4b1ef0763521bb3b77" +dependencies = [ + "bitflags", +] + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "semver" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56e6fa9c48d24d85fb3de5ad847117517440f6beceb7798af16b4a87d616b8d0" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasi" +version = "0.14.2+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" +dependencies = [ + "wit-bindgen-rt", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen-rt" +version = "0.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" +dependencies = [ + "bitflags", +] + +[[package]] +name = "zerocopy" +version = "0.8.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..6330fa4 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "kv_bench" +version = "0.1.0" +edition = "2024" + +[dependencies] +mace = { git = "https://git.o2c.fun/abby/mace.git", branch = "task-63" } +clap = { version = "4.5.42", features = ["derive"] } +rand = "0.9.2" diff --git a/maritx.md b/maritx.md new file mode 100644 index 0000000..f2ab6ee --- /dev/null +++ b/maritx.md @@ -0,0 +1,9 @@ +| 组合类型 | Key 大小 | Value 大小 | 典型场景 | 测试目标 | +|------------------|-------------------|---------------------------|------------------------------|--------------------------------------| +| 小 Key + 小 Value | 10-100 字节 | 100 字节 - 1 KB | 缓存用户信息、配置、热点数据 | 高吞吐量(QPS)、低延迟、内存效率 | +| 小 Key + 大 Value | 10-100 字节 | 1 KB - 数 MB | 存储文档、图片缩略图、日志 | 网络带宽利用率、持久化性能、压缩效率 | +| 大 Key + 小 Value | 1 KB - 10 KB | 100 字节 - 1 KB | 分布式锁、复杂命名空间 | Key 哈希性能、分片均衡性、元数据内存占用 | +| 大 Key + 大 Value | 1 KB - 10 KB | 数 MB - 100 MB | 极端负载测试、BLOB 存储 | 系统极限性能、I/O 吞吐、GC 压力 | +| 可变大小组合 | 10 字节 - 1 KB | 100 字节 - 10 MB | 混合业务负载(如电商系统) | 稳定性、缓存命中率、碎片化影响 | +| 超小 Value | 10-50 字节 | 1-8 字节 | 分布式锁、计数器(如 Redis 原子操作) | 高并发竞争性能、CAS 效率 | +| 超大 Value | 10-100 字节 | 接近系统上限(如 512 MB) | 单 Value 极限测试 | 单线程阻塞风险、序列化/反序列化瓶颈 | \ No newline at end of file diff --git a/scripts/init.sh b/scripts/init.sh new file mode 100755 index 0000000..4ea95db --- /dev/null +++ b/scripts/init.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +python3 -m venv . +./bin/pip3 install pandas matplotlib \ No newline at end of file diff --git a/scripts/plot.py b/scripts/plot.py new file mode 100644 index 0000000..d02dc6a --- /dev/null +++ b/scripts/plot.py @@ -0,0 +1,28 @@ +import pandas as pd +import matplotlib.pyplot as plt + +# 读取数据 +df = pd.read_csv("./x.csv") + +# group by mode +modes = df["mode"].unique() + +for mode in modes: + plt.figure(figsize=(12, 6)) + subset = df[df["mode"] == mode] + + # group by key_size/value_size + key_value_combinations = subset.groupby(["key_size", "value_size"]) + + for (key_size, value_size), group in key_value_combinations: + label = f"key={key_size}B, val={value_size}B" + plt.plot(group["threads"], group["ops"], marker="o", label=label) + + plt.title(f"Performance: {mode.upper()}") + plt.xlabel("Threads") + plt.ylabel("OPS") + plt.grid(True) + plt.legend() + plt.tight_layout() + plt.savefig(f"{mode}.png") + plt.close() diff --git a/scripts/x.sh b/scripts/x.sh new file mode 100755 index 0000000..d1fe0de --- /dev/null +++ b/scripts/x.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +cd .. +cargo build --release 1>/dev/null 2> /dev/null + +function samples() { + kv_sz=(16 16 100 1024 1024 1024) + # set -x + + 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 100000 --mode insert --key-size ${kv_sz[j]} --value-size ${kv_sz[j+1]} + if test $? -ne 0 + then + exit 1 + fi + ./target/release/kv_bench --path /home/abby/mace_bench --threads $i --iterations 100000 --mode get --key-size ${kv_sz[j]} --value-size ${kv_sz[j+1]} + if test $? -ne 0 + then + exit 1 + fi + ./target/release/kv_bench --path /home/abby/mace_bench --threads $i --iterations 100000 --mode mixed --key-size ${kv_sz[j]} --value-size ${kv_sz[j+1]} --insert-ratio 70 + if test $? -ne 0 + then + exit 1 + fi + done + done +} + +echo mode,threads,key_size,value_size,insert_ratio,ops > x.csv +samples 2>> x.csv + diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..83d6aae --- /dev/null +++ b/src/main.rs @@ -0,0 +1,185 @@ +use clap::Parser; +use mace::{Mace, Options}; +use rand::prelude::*; +use std::path::Path; +use std::sync::Arc; +use std::thread::JoinHandle; +use std::time::Instant; + +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct Args { + #[arg(short = 'p', long, default_value = "/tmp/mace")] + path: String, + + #[arg(short = 'm', long, default_value = "insert")] + mode: String, + + #[arg(short = 'k', long, default_value = "16")] + key_size: usize, + + #[arg(short = 'v', long, default_value = "1024")] + value_size: usize, + + #[arg(short = 't', long, default_value = "4")] + threads: usize, + + #[arg(short = 'i', long, default_value = "10000")] + iterations: usize, + + #[arg(short = 'r', long, default_value = "50")] + insert_ratio: u8, + + #[arg(long, default_value = "false")] + random: bool, +} + +fn main() { + let args = Args::parse(); + + let path = Path::new(&args.path); + + if args.path.is_empty() { + eprintln!("path is empty"); + return; + } + + if path.exists() { + eprintln!("path {:?} already exists", args.path); + return; + } + + if args.key_size < 16 || args.value_size < 16 { + eprintln!("Error: key_size or value_size too small, must >= 16"); + return; + } + + if args.insert_ratio > 100 { + eprintln!("Error: Insert ratio must be between 0 and 100"); + return; + } + + let mut opt = Options::new(path); + opt.sync_on_write = false; + opt.tmp_store = true; + let db = Mace::new(opt.validate().unwrap()).unwrap(); + + let value = Arc::new(vec![b'0'; args.value_size]); + if args.mode == "get" { + for tid in 0..args.threads { + for i in 0..args.iterations { + let key = format!("key_{tid}_{i}"); + let mut tmp = key.into_bytes(); + tmp.resize(args.key_size, 0); + let pre_tx = db.begin().unwrap(); + pre_tx.put(&tmp, &*value).unwrap(); + pre_tx.commit().unwrap(); + } + } + } + + let barrier = Arc::new(std::sync::Barrier::new(args.threads)); + 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> = (0..args.threads) + .map(|tid| { + let db = db.clone(); + let total_ops = total_ops.clone(); + let barrier = Arc::clone(&barrier); + let mode = args.mode.clone(); + let insert_ratio = args.insert_ratio; + let st = start_time.clone(); + let val = value.clone(); + + std::thread::spawn(move || { + let mut keys: Vec> = Vec::with_capacity(args.iterations); + for i in 0..args.iterations { + let key = format!("key_{tid}_{i}"); + let mut tmp = key.into_bytes(); + tmp.resize(args.key_size, 0); + keys.push(tmp); + } + + let mut rng = rand::rng(); + if args.random { + keys.shuffle(&mut rng); + } + barrier.wait(); + + { + if let Ok(mut guard) = st.try_lock() { + *guard = Instant::now(); + } + } + + match mode.as_str() { + "insert" => { + for key in &keys { + let tx = db.begin().unwrap(); + tx.put(key.as_slice(), val.as_slice()).unwrap(); + tx.commit().unwrap(); + } + } + "get" => { + for key in &keys { + let tx = db.view().unwrap(); + tx.get(key).unwrap(); + } + } + "mixed" => { + for key in &keys { + let is_insert = rng.random_range(0..100) < insert_ratio; + + if is_insert { + let tx = db.begin().unwrap(); + tx.put(key, &*val).unwrap(); + tx.commit().unwrap(); + } else { + let tx = db.view().unwrap(); + let _ = tx.get(key); // may not insert + } + } + } + _ => panic!("Invalid mode"), + } + + total_ops.fetch_add(args.iterations, std::sync::atomic::Ordering::Relaxed); + }) + }) + .collect(); + + for x in h { + x.join().unwrap(); + } + + let test_start = start_time.lock().unwrap(); + let duration = test_start.elapsed(); + let total = total_ops.load(std::sync::atomic::Ordering::Relaxed); + let ops = total as f64 / duration.as_secs_f64(); + + // println!("{:<20} {}", "Test Mode:", args.mode); + // println!("{:<20} {}", "Threads:", args.threads); + // println!("{:<20} {total}", "Total Ops:"); + // println!("{:<20} {:.2}s", "Duration:", duration.as_secs_f64()); + // println!("{:<20} {ops:.2}", "OPS:"); + // println!("{:<20} {}B", "Key Size:", args.key_size); + // println!("{:<20} {}B", "Value Size:", args.value_size); + + // 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" { + 100 + } else { + 0 + }; + // eprintln!("mode,threads,key_size,value_size,insert_ratio,ops"); + eprintln!( + "{},{},{},{},{},{:.2}", + args.mode, args.threads, args.key_size, args.value_size, ratio, ops + ); +}