From c4995e7ba266f93b88841b563e1b09aed7bd5acb Mon Sep 17 00:00:00 2001 From: leeeon233 Date: Thu, 17 Nov 2022 11:34:48 +0800 Subject: [PATCH] chore: add encode bench --- crates/loro-core/Cargo.toml | 4 ++ crates/loro-core/benches/encode.rs | 61 +++++++++++++++++++++++++++ crates/loro-core/examples/encoding.rs | 4 +- 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 crates/loro-core/benches/encode.rs diff --git a/crates/loro-core/Cargo.toml b/crates/loro-core/Cargo.toml index 385af714..5f2d6929 100644 --- a/crates/loro-core/Cargo.toml +++ b/crates/loro-core/Cargo.toml @@ -63,3 +63,7 @@ harness = false [[bench]] name = "list" harness = false + +[[bench]] +name = "encode" +harness = false diff --git a/crates/loro-core/benches/encode.rs b/crates/loro-core/benches/encode.rs new file mode 100644 index 00000000..dfcfb4bd --- /dev/null +++ b/crates/loro-core/benches/encode.rs @@ -0,0 +1,61 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +const RAW_DATA: &[u8; 901823] = include_bytes!("automerge-paper.json.gz"); + +#[cfg(feature = "test_utils")] +mod run { + use std::io::Read; + + use super::*; + use flate2::read::GzDecoder; + use loro_core::configure::Configure; + use loro_core::container::registry::ContainerWrapper; + use loro_core::LoroCore; + use serde_json::Value; + + pub fn b4(c: &mut Criterion) { + 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(); + let txns = json.as_object().unwrap().get("txns"); + let mut loro = LoroCore::default(); + let text = loro.get_text("text"); + text.with_container(|text| { + 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(&loro, pos, del_here); + text.insert(&loro, pos, ins_content); + } + } + }); + let mut b = c.benchmark_group("encode"); + b.bench_function("B4_encode", |b| { + b.iter(|| { + let _ = loro.encode_snapshot(); + }) + }); + b.bench_function("B4_decode", |b| { + let buf = loro.encode_snapshot(); + b.iter(|| { + let _ = LoroCore::decode_snapshot(&buf, None, Configure::default()); + }) + }); + } +} +pub fn dumb(_c: &mut Criterion) {} + +#[cfg(feature = "test_utils")] +criterion_group!(benches, run::b4); +#[cfg(not(feature = "test_utils"))] +criterion_group!(benches, dumb); +criterion_main!(benches); diff --git a/crates/loro-core/examples/encoding.rs b/crates/loro-core/examples/encoding.rs index bef76433..d2abe59d 100644 --- a/crates/loro-core/examples/encoding.rs +++ b/crates/loro-core/examples/encoding.rs @@ -39,10 +39,10 @@ fn main() { 0, start.elapsed().as_millis() ); - + let start = Instant::now(); let loro = LoroCore::decode_snapshot(&buf, None, Configure::default()); + println!("decode used {}ms", start.elapsed().as_millis()); let buf2 = loro.encode_snapshot(); - println!("{} bytes", buf2.len()); assert_eq!(buf, buf2); let mut last = 100; let mut count = 0;