From 261fb329ee70e9f9c508eb2843c999890d7d7bf6 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Mon, 31 Oct 2022 17:21:06 +0800 Subject: [PATCH] test: add mem profiling --- Cargo.lock | 47 +++++++++++++++++++++++++++ crates/loro-core/Cargo.toml | 9 ++++++ crates/loro-core/examples/mem.rs | 54 ++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 crates/loro-core/examples/mem.rs diff --git a/Cargo.lock b/Cargo.lock index bcf0af09..fd38dda8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -434,6 +434,12 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "fs_extra" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394" + [[package]] name = "fxhash" version = "0.2.1" @@ -545,6 +551,38 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" +[[package]] +name = "jemalloc-ctl" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1891c671f3db85d8ea8525dd43ab147f9977041911d24a03e5a36187a7bfde9" +dependencies = [ + "jemalloc-sys", + "libc", + "paste", +] + +[[package]] +name = "jemalloc-sys" +version = "0.5.2+5.3.0-patched" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134163979b6eed9564c98637b710b40979939ba351f59952708234ea11b5f3f8" +dependencies = [ + "cc", + "fs_extra", + "libc", +] + +[[package]] +name = "jemallocator" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16c2514137880c52b0b4822b563fadd38257c1f380858addb74a400889696ea6" +dependencies = [ + "jemalloc-sys", + "libc", +] + [[package]] name = "js-sys" version = "0.3.60" @@ -601,6 +639,9 @@ dependencies = [ "flate2", "fxhash", "im", + "jemalloc-ctl", + "jemalloc-sys", + "jemallocator", "js-sys", "num", "owning_ref", @@ -850,6 +891,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "paste" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1de2e551fb905ac83f73f7aedf2f0cb4a0da7e35efa24a202a936269f1f18e1" + [[package]] name = "phf_shared" version = "0.10.0" diff --git a/crates/loro-core/Cargo.toml b/crates/loro-core/Cargo.toml index 2d32c828..7f39247f 100644 --- a/crates/loro-core/Cargo.toml +++ b/crates/loro-core/Cargo.toml @@ -40,6 +40,15 @@ ctor = "0.1.23" criterion = "0.4.0" serde_json = "1.0.87" flate2 = "1.0.24" +jemalloc-ctl = "0.5.0" +jemalloc-sys = { version = "0.5.0+0.5.3", features = [ + "stats", + "profiling", + "unprefixed_malloc_on_supported_platforms", +] } + +[target.'cfg(not(target_env = "msvc"))'.dev-dependencies] +jemallocator = "0.5.0" # See https://matklad.github.io/2021/02/27/delete-cargo-integration-tests.html [lib] diff --git a/crates/loro-core/examples/mem.rs b/crates/loro-core/examples/mem.rs new file mode 100644 index 00000000..5d7aea48 --- /dev/null +++ b/crates/loro-core/examples/mem.rs @@ -0,0 +1,54 @@ +#[cfg(not(target_env = "msvc"))] +use jemallocator::Jemalloc; + +#[cfg(not(target_env = "msvc"))] +#[global_allocator] +static GLOBAL: Jemalloc = Jemalloc; + +use jemalloc_ctl::{epoch, stats}; +const RAW_DATA: &[u8; 901823] = include_bytes!("../benches/automerge-paper.json.gz"); + +use std::{io::Read, time::Instant}; + +use flate2::read::GzDecoder; +use loro_core::LoroCore; +use serde_json::Value; + +pub fn main() { + let alloc_stats = stats::allocated::mib().unwrap(); + let mut d = GzDecoder::new(&RAW_DATA[..]); + let mut s = String::new(); + d.read_to_string(&mut s).unwrap(); + let json: Value = serde_json::from_str(&s).unwrap(); + drop(s); + let txns = json.as_object().unwrap().get("txns"); + let e = epoch::mib().unwrap(); + let start = Instant::now(); + let mut loro = LoroCore::default(); + let mut text = loro.get_or_create_root_text("text").unwrap(); + for i in 0..10 { + for txn in txns.unwrap().as_array().unwrap() { + let patches = txn + .as_object() + .unwrap() + .get("patches") + .unwrap() + .as_array() + .unwrap(); + for patch in patches { + let pos = patch[0].as_u64().unwrap() as usize; + let del_here = patch[1].as_u64().unwrap() as usize; + let ins_content = patch[2].as_str().unwrap(); + text.delete(pos, del_here); + text.insert(pos, ins_content); + } + } + } + drop(json); + drop(d); + e.advance().unwrap(); + let new_new_heap = alloc_stats.read().unwrap(); + println!("Apply Automerge Dataset 10X"); + println!("Mem: {} MB", new_new_heap as f64 / 1024. / 1024.); + println!("Used: {} ms", start.elapsed().as_millis()); +}