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

174 lines
6.2 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::LoroCore;
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(|| {
2023-03-02 02:37:50 +00:00
let mut c1 = LoroCore::new(Default::default(), Some(0));
let mut c2 = LoroCore::new(Default::default(), Some(1));
let mut t1 = c1.get_text("text");
let mut 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 {
2023-02-27 12:55:52 +00:00
t1.delete(&c1, *pos, *del).unwrap();
t1.insert(&c1, *pos, ins).unwrap();
let update = c1.encode_from(c2.vv_cloned());
c2.decode(&update).unwrap();
} else {
2023-02-27 12:55:52 +00:00
t2.delete(&c2, *pos, *del).unwrap();
t2.insert(&c2, *pos, ins).unwrap();
let update = c2.encode_from(c1.vv_cloned());
c1.decode(&update).unwrap();
}
}
})
});
b.bench_function("rle update", |b| {
b.iter(|| {
2023-03-02 02:37:50 +00:00
let mut c1 = LoroCore::new(Default::default(), Some(0));
let mut c2 = LoroCore::new(Default::default(), Some(1));
let mut t1 = c1.get_text("text");
let mut 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 {
2023-02-27 12:55:52 +00:00
t1.delete(&c1, *pos, *del).unwrap();
t1.insert(&c1, *pos, ins).unwrap();
let update = c1.encode_from(c2.vv_cloned());
c2.decode(&update).unwrap();
} else {
2023-02-27 12:55:52 +00:00
t2.delete(&c2, *pos, *del).unwrap();
t2.insert(&c2, *pos, ins).unwrap();
let update = c2.encode_from(c1.vv_cloned());
c1.decode(&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;
2023-03-20 03:27:59 +00:00
use loro_internal::log_store::EncodeMode;
2023-03-02 02:37:50 +00:00
use loro_internal::{LoroCore, Transact, VersionVector};
2022-11-17 03:34:48 +00:00
pub fn b4(c: &mut Criterion) {
2022-12-27 06:18:46 +00:00
let actions = bench_utils::get_automerge_actions();
2022-11-17 03:34:48 +00:00
let mut loro = LoroCore::default();
2023-02-27 12:55:52 +00:00
let mut text = loro.get_text("text");
2023-03-02 02:37:50 +00:00
let txn = loro.transact();
2023-02-27 12:55:52 +00:00
for TextAction { pos, ins, del } in actions.iter() {
2023-03-02 02:37:50 +00:00
text.delete(&txn, *pos, *del).unwrap();
text.insert(&txn, *pos, ins).unwrap();
2023-02-27 12:55:52 +00:00
}
2023-03-02 02:37:50 +00:00
drop(txn);
2022-12-27 06:18:46 +00:00
2022-11-17 03:34:48 +00:00
let mut b = c.benchmark_group("encode");
b.bench_function("B4_encode_updates", |b| {
2022-12-02 06:03:29 +00:00
b.iter(|| {
2023-03-20 03:27:59 +00:00
let _ = loro.encode_with_cfg(EncodeMode::Updates(VersionVector::new()));
2022-12-02 06:03:29 +00:00
})
});
b.bench_function("B4_decode_updates", |b| {
2023-03-20 03:27:59 +00:00
let buf = loro.encode_with_cfg(EncodeMode::Updates(VersionVector::new()));
2023-03-02 02:37:50 +00:00
2022-12-02 06:03:29 +00:00
b.iter(|| {
2023-03-02 02:37:50 +00:00
let mut store2 = LoroCore::default();
2022-12-14 13:02:01 +00:00
store2.decode(&buf).unwrap();
2022-12-02 06:03:29 +00:00
})
});
b.bench_function("B4_encode_rle_updates", |b| {
b.iter(|| {
2023-03-20 03:27:59 +00:00
let _ = loro.encode_with_cfg(EncodeMode::RleUpdates(VersionVector::new()));
})
});
b.bench_function("B4_decode_rle_updates", |b| {
2023-03-20 03:27:59 +00:00
let buf = loro.encode_with_cfg(EncodeMode::RleUpdates(VersionVector::new()));
b.iter(|| {
2023-03-02 02:37:50 +00:00
let mut store2 = LoroCore::default();
store2.decode(&buf).unwrap();
})
});
b.bench_function("B4_encode_snapshot", |b| {
2022-12-02 06:03:29 +00:00
b.iter(|| {
2023-03-20 03:27:59 +00:00
let _ = loro.encode_all();
2022-12-02 06:03:29 +00:00
})
});
b.bench_function("B4_decode_snapshot", |b| {
2023-03-20 03:27:59 +00:00
let buf = loro.encode_all();
2022-12-02 06:03:29 +00:00
b.iter(|| {
2023-03-02 02:37:50 +00:00
let mut store2 = LoroCore::default();
2022-12-27 07:15:07 +00:00
store2.decode(&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;
2023-02-17 01:52:14 +00:00
use loro_internal::{change::ChangeMergeCfg, configure::Configure, LoroCore};
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 mut c1 = LoroCore::new(
Configure {
change: ChangeMergeCfg {
max_change_length: 0,
max_change_interval: 0,
},
..Default::default()
2022-12-27 06:25:44 +00:00
},
2023-02-22 08:54:13 +00:00
Some(1),
);
let mut c2 = LoroCore::new(
Configure {
change: ChangeMergeCfg {
max_change_length: 0,
max_change_interval: 0,
},
..Default::default()
2022-12-27 06:25:44 +00:00
},
2023-02-22 08:54:13 +00:00
Some(2),
);
let mut text1 = c1.get_text("text");
let mut text2 = c2.get_text("text");
for _ in 0..500 {
text1.insert(&c1, 0, "1").unwrap();
text2.insert(&c2, 0, "2").unwrap();
}
c1.decode(&c2.encode_from(c1.vv_cloned())).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);