loro/crates/loro-internal/examples/encoding_refactored.rs

102 lines
2.9 KiB
Rust
Raw Normal View History

2023-07-17 15:18:18 +00:00
use bench_utils::TextAction;
use criterion::black_box;
2023-07-19 13:21:37 +00:00
use loro_internal::refactor::loro::LoroDoc;
2023-07-17 15:18:18 +00:00
fn main() {
2023-07-18 06:39:17 +00:00
log_size();
// bench_decode();
// bench_decode_updates();
2023-07-18 06:39:17 +00:00
}
#[allow(unused)]
2023-07-18 06:39:17 +00:00
fn log_size() {
let actions = bench_utils::get_automerge_actions();
{
2023-07-19 13:21:37 +00:00
let loro = LoroDoc::default();
2023-07-18 06:39:17 +00:00
let text = loro.get_text("text");
let mut txn = loro.txn().unwrap();
2023-07-18 06:39:17 +00:00
for TextAction { pos, ins, del } in actions.iter() {
text.delete(&mut txn, *pos, *del);
text.insert(&mut txn, *pos, ins);
}
txn.commit().unwrap();
let snapshot = loro.export_snapshot();
let updates = loro.export_from(&Default::default());
println!("\n");
println!("Snapshot size={}", snapshot.len());
println!("Updates size={}", updates.len());
println!("\n");
loro.diagnose_size();
}
println!("\n");
println!("\n");
println!("\n");
{
println!("One Transaction Per Action");
2023-07-19 13:21:37 +00:00
let loro = LoroDoc::default();
2023-07-18 06:39:17 +00:00
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);
text.insert(&mut txn, *pos, ins);
txn.commit().unwrap();
}
let snapshot = loro.export_snapshot();
let updates = loro.export_from(&Default::default());
println!("\n");
println!("Snapshot size={}", snapshot.len());
println!("Updates size={}", updates.len());
println!("\n");
loro.diagnose_size();
}
}
#[allow(unused)]
fn bench_decode() {
2023-07-17 15:18:18 +00:00
let actions = bench_utils::get_automerge_actions();
{
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");
2023-07-18 02:10:52 +00:00
#[allow(warnings)]
2023-07-17 15:18:18 +00:00
for TextAction { pos, ins, del } in actions.iter() {
2023-07-18 06:39:17 +00:00
let mut txn = loro.txn().unwrap();
2023-07-17 15:18:18 +00:00
text.delete(&mut txn, *pos, *del);
text.insert(&mut txn, *pos, ins);
2023-07-18 06:39:17 +00:00
txn.commit().unwrap();
2023-07-17 15:18:18 +00:00
}
let snapshot = loro.export_snapshot();
// for _ in 0..100 {
// black_box(loro.export_snapshot());
// }
2023-07-18 08:25:29 +00:00
for _ in 0..100 {
2023-07-19 13:21:37 +00:00
let loro = LoroDoc::new();
loro.import(black_box(&snapshot)).unwrap();
2023-07-17 15:18:18 +00:00
}
}
}
#[allow(unused)]
fn bench_decode_updates() {
let actions = bench_utils::get_automerge_actions();
2023-07-19 13:21:37 +00:00
let loro = LoroDoc::default();
let text = loro.get_text("text");
#[allow(warnings)]
for TextAction { pos, ins, del } in actions.iter() {
let mut txn = loro.txn().unwrap();
text.delete(&mut txn, *pos, *del);
text.insert(&mut txn, *pos, ins);
txn.commit().unwrap();
}
let updates = loro.export_from(&Default::default());
for _ in 0..10 {
2023-07-19 13:21:37 +00:00
let loro = LoroDoc::new();
loro.import(black_box(&updates)).unwrap();
}
}