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

210 lines
7.3 KiB
Rust
Raw Normal View History

2022-10-27 08:36:04 +00:00
use criterion::{criterion_group, criterion_main, Criterion};
2022-10-31 08:16:44 +00:00
#[cfg(feature = "test_utils")]
2022-10-28 10:28:55 +00:00
mod run {
2022-10-31 08:16:44 +00:00
2022-10-28 10:28:55 +00:00
use super::*;
use arbitrary::Unstructured;
2022-12-27 06:18:46 +00:00
use bench_utils::TextAction;
use loro_internal::fuzz::test_multi_sites;
use loro_internal::fuzz::Action;
use loro_internal::LoroCore;
2023-03-02 09:54:57 +00:00
use loro_internal::Transact;
2022-10-28 10:28:55 +00:00
use rand::Rng;
use rand::SeedableRng;
2022-10-27 08:36:04 +00:00
2022-11-09 03:04:58 +00:00
pub fn two_client_edits(c: &mut Criterion) {
2022-10-28 10:28:55 +00:00
let mut rgn = rand::rngs::StdRng::seed_from_u64(0);
let mut bytes = Vec::new();
2022-11-10 01:30:52 +00:00
for _ in 0..8000 {
2022-10-28 10:28:55 +00:00
bytes.push(rgn.gen::<u8>());
}
2022-10-27 08:36:04 +00:00
2022-10-28 10:28:55 +00:00
let mut gen = Unstructured::new(&bytes);
2022-11-10 01:30:52 +00:00
let mut c = c.benchmark_group("sync");
2022-11-15 08:02:42 +00:00
let mut actions = gen.arbitrary::<[Action; 200]>().unwrap();
2022-10-28 10:28:55 +00:00
c.bench_function("random text edit 2 sites", |b| {
2022-11-15 08:02:42 +00:00
b.iter(|| test_multi_sites(2, &mut actions))
2022-10-28 10:28:55 +00:00
});
2022-11-09 03:04:58 +00:00
c.bench_function("random text edit 8 sites", |b| {
2022-11-15 08:02:42 +00:00
b.iter(|| test_multi_sites(8, &mut actions))
2022-11-09 03:04:58 +00:00
});
2022-11-15 08:02:42 +00:00
let mut actions = gen.arbitrary::<[Action; 4000]>().unwrap();
2022-11-10 01:30:52 +00:00
c.sample_size(10);
c.bench_function("random text edit 8 sites long", |b| {
2022-11-15 08:02:42 +00:00
b.iter(|| test_multi_sites(8, &mut actions))
2022-11-10 01:30:52 +00:00
});
2022-10-28 10:28:55 +00:00
}
2022-10-31 08:16:44 +00:00
pub fn b4(c: &mut Criterion) {
2022-12-27 06:18:46 +00:00
let actions = bench_utils::get_automerge_actions();
let mut b = c.benchmark_group("direct_apply");
2023-03-06 04:43:52 +00:00
b.sample_size(10);
2022-11-10 01:30:52 +00:00
b.bench_function("B4", |b| {
2022-10-31 08:16:44 +00:00
b.iter(|| {
let mut loro = LoroCore::default();
2023-02-27 12:55:52 +00:00
let mut text = loro.get_text("text");
2023-03-10 10:21:05 +00:00
for TextAction { pos, ins, del } in actions.iter() {
text.delete(&loro, *pos, *del).unwrap();
text.insert(&loro, *pos, ins).unwrap();
}
})
});
b.bench_function("B4 utf16", |b| {
b.iter(|| {
let mut loro = LoroCore::default();
let mut text = loro.get_text("text");
for TextAction { pos, ins, del } in actions.iter() {
text.delete_utf16(&loro, *pos, *del).unwrap();
text.insert_utf16(&loro, *pos, ins).unwrap();
}
})
});
2023-03-10 10:21:05 +00:00
b.bench_function("B4_Per100_Txn", |b| {
b.iter(|| {
let mut loro = LoroCore::default();
let mut text = loro.get_text("text");
let mut n = 0;
let mut txn = loro.transact();
for TextAction { pos, ins, del } in actions.iter() {
if n == 100 {
n = 0;
drop(txn);
txn = loro.transact();
}
n += 1;
text.delete(&txn, *pos, *del).unwrap();
text.insert(&txn, *pos, ins).unwrap();
}
})
});
b.bench_function("B4_All_Txn", |b| {
b.iter(|| {
let mut loro = LoroCore::default();
let mut text = loro.get_text("text");
2023-03-02 09:54:57 +00:00
{
let txn = loro.transact();
for TextAction { pos, ins, del } in actions.iter() {
text.delete(&txn, *pos, *del).unwrap();
text.insert(&txn, *pos, ins).unwrap();
}
2023-02-27 12:55:52 +00:00
}
2022-11-28 13:28:15 +00:00
})
});
b.bench_function("B4 Observed", |b| {
b.iter(|| {
let mut loro = LoroCore::default();
loro.subscribe_deep(Box::new(|_| {}));
2023-02-27 12:55:52 +00:00
let mut text = loro.get_text("text");
2023-03-02 09:54:57 +00:00
{
for TextAction { pos, ins, del } in actions.iter() {
text.delete(&loro, *pos, *del).unwrap();
text.insert(&loro, *pos, ins).unwrap();
2023-03-02 09:54:57 +00:00
}
2023-02-27 12:55:52 +00:00
}
2022-10-31 08:16:44 +00:00
})
});
2022-11-09 03:04:58 +00:00
2023-03-15 12:43:26 +00:00
b.bench_function("B4 Observed_Per100_Txn", |b| {
b.iter(|| {
let mut loro = LoroCore::default();
loro.subscribe_deep(Box::new(|_| {}));
let mut text = loro.get_text("text");
let mut n = 0;
let mut txn = loro.transact();
for TextAction { pos, ins, del } in actions.iter() {
if n == 100 {
n = 0;
drop(txn);
txn = loro.transact();
}
n += 1;
text.delete(&txn, *pos, *del).unwrap();
text.insert(&txn, *pos, ins).unwrap();
}
})
});
b.bench_function("B4 Observed_All_Txn", |b| {
b.iter(|| {
let mut loro = LoroCore::default();
loro.subscribe_deep(Box::new(|_| {}));
let mut text = loro.get_text("text");
{
let txn = loro.transact();
for TextAction { pos, ins, del } in actions.iter() {
text.delete(&txn, *pos, *del).unwrap();
text.insert(&txn, *pos, ins).unwrap();
}
}
})
});
2023-03-06 04:43:52 +00:00
// b.sample_size(10);
2022-11-10 01:30:52 +00:00
b.bench_function("B4DirectSync", |b| {
2022-11-09 03:04:58 +00:00
b.iter(|| {
let mut loro = LoroCore::default();
let mut loro_b = LoroCore::default();
2023-02-27 12:55:52 +00:00
let mut text = loro.get_text("text");
2022-12-27 06:18:46 +00:00
for TextAction { pos, ins, del } in actions.iter() {
2023-03-02 09:54:57 +00:00
{
let txn = loro.transact();
text.delete(&txn, *pos, *del).unwrap();
text.insert(&txn, *pos, ins).unwrap();
}
2022-11-09 03:04:58 +00:00
loro_b.import(loro.export(loro_b.vv_cloned()));
2022-11-09 03:04:58 +00:00
}
})
});
2022-11-09 13:41:32 +00:00
2022-11-10 01:30:52 +00:00
drop(b);
let mut b = c.benchmark_group("sync");
2022-11-09 13:41:32 +00:00
b.bench_function("B4Parallel", |b| {
b.iter(|| {
let mut loro = LoroCore::default();
let mut loro_b = LoroCore::default();
2023-02-27 12:55:52 +00:00
let mut text = loro.get_text("text");
let mut text2 = loro_b.get_text("text");
2022-11-09 13:41:32 +00:00
let mut i = 0;
2022-12-27 06:18:46 +00:00
for TextAction { pos, ins, del } in actions.iter() {
let pos = *pos;
let del = *del;
2022-11-09 13:41:32 +00:00
i += 1;
if i > 1000 {
break;
}
2023-03-02 09:54:57 +00:00
{
let txn = loro.transact();
text.delete(&txn, pos, del).unwrap();
text.insert(&txn, pos, ins).unwrap();
}
2023-02-27 12:55:52 +00:00
2023-03-02 09:54:57 +00:00
{
let txn = loro_b.transact();
text2.delete(&txn, pos, del).unwrap();
text2.insert(&txn, pos, ins).unwrap();
}
loro_b.import(loro.export(loro_b.vv_cloned()));
loro.import(loro_b.export(loro.vv_cloned()));
2022-11-09 13:41:32 +00:00
}
})
});
2022-10-31 08:16:44 +00:00
}
2022-10-27 08:36:04 +00:00
}
2022-10-28 10:28:55 +00:00
pub fn dumb(_c: &mut Criterion) {}
2022-10-27 08:36:04 +00:00
#[cfg(feature = "test_utils")]
2022-11-09 03:04:58 +00:00
criterion_group!(benches, run::two_client_edits, run::b4);
#[cfg(not(feature = "test_utils"))]
2022-10-28 10:28:55 +00:00
criterion_group!(benches, dumb);
2022-10-27 08:36:04 +00:00
criterion_main!(benches);