chore: add encode bench

This commit is contained in:
leeeon233 2022-11-17 11:34:48 +08:00 committed by Zixuan Chen
parent c9f63b6594
commit c4995e7ba2
3 changed files with 67 additions and 2 deletions

View file

@ -63,3 +63,7 @@ harness = false
[[bench]] [[bench]]
name = "list" name = "list"
harness = false harness = false
[[bench]]
name = "encode"
harness = false

View file

@ -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);

View file

@ -39,10 +39,10 @@ fn main() {
0, 0,
start.elapsed().as_millis() start.elapsed().as_millis()
); );
let start = Instant::now();
let loro = LoroCore::decode_snapshot(&buf, None, Configure::default()); let loro = LoroCore::decode_snapshot(&buf, None, Configure::default());
println!("decode used {}ms", start.elapsed().as_millis());
let buf2 = loro.encode_snapshot(); let buf2 = loro.encode_snapshot();
println!("{} bytes", buf2.len());
assert_eq!(buf, buf2); assert_eq!(buf, buf2);
let mut last = 100; let mut last = 100;
let mut count = 0; let mut count = 0;