loro/crates/loro-internal/benches/encode.rs

148 lines
4.8 KiB
Rust
Raw Normal View History

2022-11-17 03:34:48 +00:00
use criterion::{criterion_group, criterion_main, Criterion};
#[cfg(feature = "test_utils")]
mod sync {
use super::*;
2022-12-27 06:18:46 +00:00
use bench_utils::{get_automerge_actions, TextAction};
use loro_internal::LoroDoc;
pub fn b4(c: &mut Criterion) {
2022-12-27 06:18:46 +00:00
let actions = get_automerge_actions();
let mut b = c.benchmark_group("encode_with_sync");
b.sample_size(10);
b.bench_function("update", |b| {
b.iter(|| {
let c1 = LoroDoc::new();
c1.set_peer_id(1);
let c2 = LoroDoc::new();
c1.set_peer_id(2);
let t1 = c1.get_text("text");
let t2 = c2.get_text("text");
for (i, action) in actions.iter().enumerate() {
2023-02-20 12:02:38 +00:00
if i > 2000 {
break;
}
2022-12-27 06:18:46 +00:00
let TextAction { pos, ins, del } = action;
if i % 2 == 0 {
let mut txn = c1.txn().unwrap();
t1.delete(&mut txn, *pos, *del).unwrap();
t1.insert(&mut txn, *pos, ins).unwrap();
txn.commit().unwrap();
2023-02-27 12:55:52 +00:00
let update = c1.export_from(&c2.oplog_vv());
c2.import(&update).unwrap();
} else {
let mut txn = c2.txn().unwrap();
t2.delete(&mut txn, *pos, *del).unwrap();
t2.insert(&mut txn, *pos, ins).unwrap();
txn.commit().unwrap();
let update = c2.export_from(&c1.oplog_vv());
c1.import(&update).unwrap();
}
}
})
});
}
}
2022-11-17 03:34:48 +00:00
#[cfg(feature = "test_utils")]
mod run {
use super::*;
2022-12-27 06:18:46 +00:00
use bench_utils::TextAction;
use loro_internal::LoroDoc;
2022-11-17 03:34:48 +00:00
pub fn b4(c: &mut Criterion) {
let loro = LoroDoc::default();
let mut ran = false;
let mut ensure_ran = || {
if !ran {
let actions = bench_utils::get_automerge_actions();
let text = loro.get_text("text");
for TextAction { pos, ins, del } in actions.iter() {
let mut txn = loro.txn().unwrap();
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
}
ran = true;
}
};
2022-12-27 06:18:46 +00:00
2022-11-17 03:34:48 +00:00
let mut b = c.benchmark_group("encode");
b.sample_size(10);
b.bench_function("B4_encode_updates", |b| {
ensure_ran();
2022-12-02 06:03:29 +00:00
b.iter(|| {
let _ = loro.export_from(&Default::default());
2022-12-02 06:03:29 +00:00
})
});
b.bench_function("B4_decode_updates", |b| {
ensure_ran();
let buf = loro.export_from(&Default::default());
2023-03-02 02:37:50 +00:00
2022-12-02 06:03:29 +00:00
b.iter(|| {
let store2 = LoroDoc::default();
store2.import(&buf).unwrap();
})
});
b.bench_function("B4_decode_updates detached mode", |b| {
ensure_ran();
let buf = loro.export_from(&Default::default());
b.iter(|| {
let mut store2 = LoroDoc::default();
store2.detach();
store2.import(&buf).unwrap();
})
});
b.bench_function("B4_encode_snapshot", |b| {
ensure_ran();
2022-12-02 06:03:29 +00:00
b.iter(|| {
let _ = loro.export_snapshot();
2022-12-02 06:03:29 +00:00
})
});
b.bench_function("B4_decode_snapshot", |b| {
ensure_ran();
let buf = loro.export_snapshot();
2022-12-02 06:03:29 +00:00
b.iter(|| {
let store2 = LoroDoc::default();
store2.import(&buf).unwrap();
2022-12-02 06:03:29 +00:00
})
});
2022-11-17 03:34:48 +00:00
}
}
2022-12-27 06:25:44 +00:00
mod import {
use criterion::Criterion;
use loro_internal::LoroDoc;
2022-12-27 06:25:44 +00:00
#[allow(dead_code)]
2022-12-27 06:25:44 +00:00
pub fn causal_iter(c: &mut Criterion) {
let mut b = c.benchmark_group("causal_iter");
b.sample_size(10);
b.bench_function("parallel_500", |b| {
2023-02-22 08:54:13 +00:00
b.iter(|| {
let c1 = LoroDoc::new();
c1.set_peer_id(1);
let c2 = LoroDoc::new();
c1.set_peer_id(2);
let text1 = c1.get_text("text");
let text2 = c2.get_text("text");
2023-02-22 08:54:13 +00:00
for _ in 0..500 {
text1.insert(&mut c1.txn().unwrap(), 0, "1").unwrap();
text2.insert(&mut c2.txn().unwrap(), 0, "2").unwrap();
2023-02-22 08:54:13 +00:00
}
c1.import(&c2.export_from(&c1.oplog_vv())).unwrap();
2022-12-27 06:25:44 +00:00
})
});
}
}
2022-11-17 03:34:48 +00:00
pub fn dumb(_c: &mut Criterion) {}
#[cfg(feature = "test_utils")]
2022-12-27 06:25:44 +00:00
criterion_group!(benches, run::b4, sync::b4, import::causal_iter);
2022-11-17 03:34:48 +00:00
#[cfg(not(feature = "test_utils"))]
criterion_group!(benches, dumb);
criterion_main!(benches);