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;
|
2023-07-31 03:49:55 +00:00
|
|
|
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-11-28 08:22:43 +00:00
|
|
|
text.delete_with_txn(&mut txn, *pos, *del).unwrap();
|
|
|
|
text.insert_with_txn(&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 {
|
2023-10-31 09:59:24 +00:00
|
|
|
store.set_peer_id(i).unwrap();
|
2023-09-02 15:23:52 +00:00
|
|
|
let list = store.get_list("list");
|
|
|
|
let value: LoroValue = i.to_string().into();
|
|
|
|
let mut txn = store.txn().unwrap();
|
2023-11-28 08:22:43 +00:00
|
|
|
list.insert_with_txn(&mut txn, 0, value).unwrap();
|
2023-09-02 15:23:52 +00:00
|
|
|
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() {
|
2023-11-28 08:22:43 +00:00
|
|
|
text.delete_with_txn(&mut txn, *pos, *del).unwrap();
|
|
|
|
text.insert_with_txn(&mut txn, *pos, ins).unwrap();
|
2023-09-02 15:23:52 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
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");
|
2023-11-01 12:02:05 +00:00
|
|
|
loro.subscribe_root(Arc::new(move |event| {
|
2023-07-26 10:56:03 +00:00
|
|
|
black_box(event);
|
|
|
|
}));
|
|
|
|
let mut txn = loro.txn().unwrap();
|
|
|
|
for TextAction { pos, ins, del } in actions.iter() {
|
2023-11-28 08:22:43 +00:00
|
|
|
text.delete_with_txn(&mut txn, *pos, *del).unwrap();
|
|
|
|
text.insert_with_txn(&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-11-28 08:22:43 +00:00
|
|
|
text.delete_with_txn(&mut txn, *pos, *del).unwrap();
|
|
|
|
text.insert_with_txn(&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-11-28 08:22:43 +00:00
|
|
|
text.delete_with_txn(&mut txn, *pos, *del).unwrap();
|
|
|
|
text.insert_with_txn(&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-11-28 08:22:43 +00:00
|
|
|
text.delete_with_txn(&mut txn, *pos, *del).unwrap();
|
|
|
|
text.insert_with_txn(&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-11-28 08:22:43 +00:00
|
|
|
text.delete_with_txn(&mut txn, *pos, *del).unwrap();
|
|
|
|
text.insert_with_txn(&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-10-29 06:02:13 +00:00
|
|
|
// b.bench_function("B4 utf16", |b| {
|
|
|
|
// b.iter(|| {
|
|
|
|
// let loro = LoroDoc::new();
|
|
|
|
// let text = loro.get_text("text");
|
|
|
|
// let mut txn = loro.txn().unwrap();
|
2023-07-17 13:24:02 +00:00
|
|
|
|
2023-10-29 06:02:13 +00:00
|
|
|
// for TextAction { pos, ins, del } in actions.iter() {
|
|
|
|
// 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-11-28 08:22:43 +00:00
|
|
|
text.delete_with_txn(&mut txn, *pos, *del).unwrap();
|
|
|
|
text.insert_with_txn(&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-11-28 08:22:43 +00:00
|
|
|
text.delete_with_txn(&mut txn, *pos, *del).unwrap();
|
|
|
|
text.insert_with_txn(&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");
|
2023-11-01 12:02:05 +00:00
|
|
|
loro.subscribe_root(Arc::new(move |event| {
|
2023-07-26 10:56:03 +00:00
|
|
|
black_box(event);
|
|
|
|
}));
|
|
|
|
{
|
|
|
|
for TextAction { pos, ins, del } in actions.iter() {
|
|
|
|
let mut txn = loro.txn().unwrap();
|
2023-11-28 08:22:43 +00:00
|
|
|
text.delete_with_txn(&mut txn, *pos, *del).unwrap();
|
|
|
|
text.insert_with_txn(&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-11-28 08:22:43 +00:00
|
|
|
text.delete_with_txn(&mut txn, *pos, *del).unwrap();
|
|
|
|
text.insert_with_txn(&mut txn, *pos, ins).unwrap();
|
2023-07-17 13:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loro_b
|
2023-08-04 02:45:23 +00:00
|
|
|
.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-11-28 08:22:43 +00:00
|
|
|
text.delete_with_txn(&mut txn, pos, del).unwrap();
|
|
|
|
text.insert_with_txn(&mut txn, pos, ins).unwrap();
|
2023-07-17 13:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut txn = loro_b.txn().unwrap();
|
2023-11-28 08:22:43 +00:00
|
|
|
text2.delete_with_txn(&mut txn, pos, del).unwrap();
|
|
|
|
text2.insert_with_txn(&mut txn, pos, ins).unwrap();
|
2023-07-17 13:24:02 +00:00
|
|
|
}
|
|
|
|
loro_b
|
2023-08-04 02:45:23 +00:00
|
|
|
.import(&loro.export_from(&loro_b.oplog_vv()))
|
2023-07-17 13:24:02 +00:00
|
|
|
.unwrap();
|
2023-08-04 02:45:23 +00:00
|
|
|
loro.import(&loro_b.export_from(&loro.oplog_vv())).unwrap();
|
2023-07-17 13:24:02 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
2023-10-29 06:02:13 +00:00
|
|
|
|
|
|
|
let b = b.sample_size(10);
|
|
|
|
b.bench_function("DecodeUpdates B4Parallel", |b| {
|
|
|
|
let loro = LoroDoc::default();
|
|
|
|
let loro_b = LoroDoc::default();
|
|
|
|
let text = loro.get_text("text");
|
|
|
|
let text2 = loro_b.get_text("text");
|
|
|
|
for TextAction { pos, ins, del } in actions.iter() {
|
|
|
|
let pos = *pos;
|
|
|
|
let del = *del;
|
|
|
|
{
|
|
|
|
let mut txn = loro.txn().unwrap();
|
2023-11-28 08:22:43 +00:00
|
|
|
text.delete_with_txn(&mut txn, pos, del).unwrap();
|
|
|
|
text.insert_with_txn(&mut txn, pos, ins).unwrap();
|
2023-10-29 06:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut txn = loro_b.txn().unwrap();
|
2023-11-28 08:22:43 +00:00
|
|
|
text2.delete_with_txn(&mut txn, pos, del).unwrap();
|
|
|
|
text2.insert_with_txn(&mut txn, pos, ins).unwrap();
|
2023-10-29 06:02:13 +00:00
|
|
|
}
|
|
|
|
loro_b
|
|
|
|
.import(&loro.export_from(&loro_b.oplog_vv()))
|
|
|
|
.unwrap();
|
|
|
|
loro.import(&loro_b.export_from(&loro.oplog_vv())).unwrap();
|
|
|
|
}
|
|
|
|
let data = loro.export_from(&Default::default());
|
|
|
|
b.iter(|| {
|
|
|
|
let loro = LoroDoc::default();
|
|
|
|
loro.import(&data).unwrap();
|
|
|
|
})
|
|
|
|
});
|
|
|
|
b.bench_function("DecodeSnapshot B4Parallel", |b| {
|
|
|
|
let loro = LoroDoc::default();
|
|
|
|
let loro_b = LoroDoc::default();
|
|
|
|
let text = loro.get_text("text");
|
|
|
|
let text2 = loro_b.get_text("text");
|
|
|
|
for TextAction { pos, ins, del } in actions.iter() {
|
|
|
|
let pos = *pos;
|
|
|
|
let del = *del;
|
|
|
|
{
|
|
|
|
let mut txn = loro.txn().unwrap();
|
2023-11-28 08:22:43 +00:00
|
|
|
text.delete_with_txn(&mut txn, pos, del).unwrap();
|
|
|
|
text.insert_with_txn(&mut txn, pos, ins).unwrap();
|
2023-10-29 06:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut txn = loro_b.txn().unwrap();
|
2023-11-28 08:22:43 +00:00
|
|
|
text2.delete_with_txn(&mut txn, pos, del).unwrap();
|
|
|
|
text2.insert_with_txn(&mut txn, pos, ins).unwrap();
|
2023-10-29 06:02:13 +00:00
|
|
|
}
|
|
|
|
loro_b
|
|
|
|
.import(&loro.export_from(&loro_b.oplog_vv()))
|
|
|
|
.unwrap();
|
|
|
|
loro.import(&loro_b.export_from(&loro.oplog_vv())).unwrap();
|
|
|
|
}
|
|
|
|
let data = loro.export_snapshot();
|
|
|
|
b.iter(|| {
|
|
|
|
let loro = LoroDoc::default();
|
|
|
|
loro.import(&data).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);
|