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

300 lines
10 KiB
Rust
Raw Normal View History

2023-07-17 13:24:02 +00:00
use criterion::{criterion_group, criterion_main, Criterion};
#[cfg(feature = "test_utils")]
mod run {
2023-07-28 05:38:52 +00:00
use std::sync::Arc;
2023-07-26 10:56:03 +00:00
2023-07-17 13:24:02 +00:00
use super::*;
use bench_utils::TextAction;
2023-07-26 10:56:03 +00:00
use criterion::black_box;
2023-09-02 15:23:52 +00:00
use loro_common::LoroValue;
use loro_internal::loro::LoroDoc;
2023-07-17 13:24:02 +00:00
pub fn b4(c: &mut Criterion) {
let actions = bench_utils::get_automerge_actions();
let mut b = c.benchmark_group("refactored direct_apply");
b.sample_size(10);
b.bench_function("B4", |b| {
b.iter(|| {
2023-07-19 13:21:37 +00:00
let loro = LoroDoc::default();
2023-07-17 13:24:02 +00:00
let text = loro.get_text("text");
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
2023-07-28 18:03:51 +00:00
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
2023-07-17 13:24:02 +00:00
}
})
});
2023-09-02 15:23:52 +00:00
b.bench_function("B4 with 100K actors history", |b| {
let store = LoroDoc::default();
for i in 0..100_000 {
store.set_peer_id(i);
let list = store.get_list("list");
let value: LoroValue = i.to_string().into();
let mut txn = store.txn().unwrap();
list.insert(&mut txn, 0, value).unwrap();
txn.commit().unwrap();
}
let update = store.export_snapshot();
drop(store);
b.iter_batched(
|| {
let loro = LoroDoc::default();
loro.import(&update).unwrap();
loro
},
|loro| {
let text = loro.get_text("text");
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
}
},
criterion::BatchSize::SmallInput,
)
});
2023-07-26 10:56:03 +00:00
b.bench_function("B4 Obs", |b| {
b.iter(|| {
let loro = LoroDoc::default();
let text = loro.get_text("text");
loro.subscribe_deep(Arc::new(move |event| {
black_box(event);
}));
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
2023-07-28 18:03:51 +00:00
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
2023-07-26 10:56:03 +00:00
}
})
});
2023-07-17 15:18:18 +00:00
b.bench_function("B4 encode snapshot", |b| {
2023-07-19 13:21:37 +00:00
let loro = LoroDoc::default();
2023-07-17 15:18:18 +00:00
let text = loro.get_text("text");
let mut n = 0;
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
if n == 10 {
n = 0;
drop(txn);
txn = loro.txn().unwrap();
}
n += 1;
2023-07-28 18:03:51 +00:00
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
2023-07-17 15:18:18 +00:00
}
txn.commit().unwrap();
b.iter(|| {
loro.export_snapshot();
});
});
b.bench_function("B4 encode updates", |b| {
2023-07-19 13:21:37 +00:00
let loro = LoroDoc::default();
2023-07-17 15:18:18 +00:00
let text = loro.get_text("text");
let mut n = 0;
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
if n == 10 {
n = 0;
drop(txn);
txn = loro.txn().unwrap();
}
n += 1;
2023-07-28 18:03:51 +00:00
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
2023-07-17 15:18:18 +00:00
}
txn.commit().unwrap();
b.iter(|| {
loro.export_from(&Default::default());
});
});
b.bench_function("B4 decode snapshot", |b| {
2023-07-19 13:21:37 +00:00
let loro = LoroDoc::default();
2023-07-17 15:18:18 +00:00
let text = loro.get_text("text");
let mut n = 0;
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
if n == 10 {
n = 0;
drop(txn);
txn = loro.txn().unwrap();
}
n += 1;
2023-07-28 18:03:51 +00:00
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
2023-07-17 15:18:18 +00:00
}
txn.commit().unwrap();
let data = loro.export_snapshot();
b.iter(|| {
2023-07-19 13:21:37 +00:00
let l = LoroDoc::new();
2023-07-17 15:18:18 +00:00
l.import(&data).unwrap();
});
});
b.bench_function("B4 import updates", |b| {
2023-07-19 13:21:37 +00:00
let loro = LoroDoc::default();
2023-07-17 15:18:18 +00:00
let text = loro.get_text("text");
let mut n = 0;
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
if n == 10 {
n = 0;
drop(txn);
txn = loro.txn().unwrap();
}
n += 1;
2023-07-28 18:03:51 +00:00
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
2023-07-17 15:18:18 +00:00
}
txn.commit().unwrap();
let data = loro.export_from(&Default::default());
b.iter(|| {
2023-07-19 13:21:37 +00:00
let l = LoroDoc::new();
2023-07-17 15:18:18 +00:00
l.import(&data).unwrap();
});
});
2023-07-17 13:24:02 +00:00
b.bench_function("B4 utf16", |b| {
b.iter(|| {
2023-07-19 13:21:37 +00:00
let loro = LoroDoc::new();
2023-07-17 13:24:02 +00:00
let text = loro.get_text("text");
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
2023-07-28 18:03:51 +00:00
text.delete_utf16(&mut txn, *pos, *del).unwrap();
text.insert_utf16(&mut txn, *pos, ins).unwrap();
2023-07-17 13:24:02 +00:00
}
})
});
b.bench_function("B4_Per100_Txn", |b| {
b.iter(|| {
2023-07-19 13:21:37 +00:00
let loro = LoroDoc::default();
2023-07-17 13:24:02 +00:00
let text = loro.get_text("text");
let mut n = 0;
let mut txn = loro.txn().unwrap();
for TextAction { pos, ins, del } in actions.iter() {
if n == 100 {
n = 0;
drop(txn);
txn = loro.txn().unwrap();
}
n += 1;
2023-07-28 18:03:51 +00:00
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
2023-07-17 13:24:02 +00:00
}
})
});
b.bench_function("B4 One Op One Txn", |b| {
b.iter(|| {
2023-07-19 13:21:37 +00:00
let loro = LoroDoc::default();
2023-07-17 13:24:02 +00:00
let text = loro.get_text("text");
{
for TextAction { pos, ins, del } in actions.iter() {
let mut txn = loro.txn().unwrap();
2023-07-28 18:03:51 +00:00
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
2023-07-17 13:24:02 +00:00
txn.commit().unwrap();
}
}
})
});
2023-07-26 10:56:03 +00:00
b.bench_function("B4 One Op One Txn Obs", |b| {
b.iter(|| {
let loro = LoroDoc::default();
let text = loro.get_text("text");
loro.subscribe_deep(Arc::new(move |event| {
black_box(event);
}));
{
for TextAction { pos, ins, del } in actions.iter() {
let mut txn = loro.txn().unwrap();
2023-07-28 18:03:51 +00:00
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
2023-07-26 10:56:03 +00:00
txn.commit().unwrap();
}
}
})
});
2023-07-17 13:24:02 +00:00
b.bench_function("B4DirectSync", |b| {
b.iter(|| {
2023-07-19 13:21:37 +00:00
let loro = LoroDoc::default();
let loro_b = LoroDoc::default();
2023-07-17 13:24:02 +00:00
let text = loro.get_text("text");
for TextAction { pos, ins, del } in actions.iter() {
{
let mut txn = loro.txn().unwrap();
2023-07-28 18:03:51 +00:00
text.delete(&mut txn, *pos, *del).unwrap();
text.insert(&mut txn, *pos, ins).unwrap();
2023-07-17 13:24:02 +00:00
}
loro_b
.import(&loro.export_from(&loro_b.oplog_vv()))
2023-07-17 13:24:02 +00:00
.unwrap();
}
})
});
drop(b);
let mut b = c.benchmark_group("refactored-sync");
b.bench_function("B4Parallel", |b| {
b.iter(|| {
2023-07-19 13:21:37 +00:00
let loro = LoroDoc::default();
let loro_b = LoroDoc::default();
2023-07-17 13:24:02 +00:00
let text = loro.get_text("text");
let text2 = loro_b.get_text("text");
let mut i = 0;
for TextAction { pos, ins, del } in actions.iter() {
let pos = *pos;
let del = *del;
i += 1;
if i > 1000 {
break;
}
{
let mut txn = loro.txn().unwrap();
2023-07-28 18:03:51 +00:00
text.delete(&mut txn, pos, del).unwrap();
text.insert(&mut txn, pos, ins).unwrap();
2023-07-17 13:24:02 +00:00
}
{
let mut txn = loro_b.txn().unwrap();
2023-07-28 18:03:51 +00:00
text2.delete(&mut txn, pos, del).unwrap();
text2.insert(&mut txn, pos, ins).unwrap();
2023-07-17 13:24:02 +00:00
}
loro_b
.import(&loro.export_from(&loro_b.oplog_vv()))
2023-07-17 13:24:02 +00:00
.unwrap();
loro.import(&loro_b.export_from(&loro.oplog_vv())).unwrap();
2023-07-17 13:24:02 +00:00
}
})
});
}
}
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);