-
-
Save ArtemGr/2aa2bd674b41b3af6eb9a1f191a01f59 to your computer and use it in GitHub Desktop.
Rocksdb experiment, watching consumed disk space
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/target | |
/db | |
/Cargo.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import subprocess | |
rc = os.system('cargo build --release') | |
if rc != 0: | |
exit(rc) | |
rc = subprocess.call('target/release/rocks.exe') | |
if rc != 0: | |
exit(rc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "rocks" | |
version = "0.1.0" | |
edition = "2021" | |
[[bin]] | |
name = "rocks" | |
path = "rocks.rs" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
glob = "0.3" | |
gstuff = {version = "0.7", features = ["nightly", "rdtsc"]} | |
rocksdb = {version = "0.17", default-features = false, features = ["zstd"]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use glob::glob; | |
use gstuff::rdtsc; | |
use rocksdb::{DB, DBCompressionType, Options}; | |
fn size_on_disk() -> u64 { | |
let mut size = 0; | |
for file in glob (r"db/*") .unwrap() { | |
if let Ok (file) = file { | |
if let Ok (meta) = file.metadata() { | |
if !meta.is_file() {continue} | |
size += meta.len() as u64 | |
} | |
} | |
} | |
size | |
} | |
fn main() { | |
// https://docs.rs/rocksdb/latest/rocksdb/struct.Options.html | |
// https://github.com/facebook/rocksdb/wiki/Universal-Compaction | |
let mut opts = Options::default(); | |
opts.create_if_missing(true); | |
opts.set_compression_type(DBCompressionType::Zstd); | |
//opts.set_disable_auto_compactions(true); | |
let db = DB::open(&opts, "db").unwrap(); | |
let mut cnt = 0_u64; | |
let mut size = 0_u64; | |
let mut last_id: Option<u64> = None; | |
while cnt < 1_000_000_000 { | |
let id = rdtsc(); | |
let write: [u8; 8] = unsafe { std::mem::transmute(id.to_be()) }; | |
let payload = vec![100; 10000]; | |
db.put(write, payload).unwrap(); | |
if let Some(last) = last_id { | |
let last_key: [u8; 8] = unsafe { std::mem::transmute(last.to_be()) }; | |
db.delete(last_key).unwrap(); | |
} | |
last_id = Some(id); | |
if cnt % 1_000 == 0 { | |
let cur_size = size_on_disk(); | |
if cnt % 1_000_000 == 0 { | |
println!("{}", cur_size); | |
} | |
if cur_size < size { | |
println!("db shrunk! From {} to {} MiB", size / 1024 / 1024, cur_size / 1024 / 1024); | |
} | |
size = cur_size; | |
} | |
cnt += 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment