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

110 lines
3.2 KiB
Rust
Raw Normal View History

2023-07-17 15:18:18 +00:00
use bench_utils::TextAction;
use criterion::black_box;
use loro_internal::LoroDoc;
2023-07-17 15:18:18 +00:00
fn main() {
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() {
2023-11-28 08:22:43 +00:00
text.delete_with_txn(&mut txn, *pos, *del);
text.insert_with_txn(&mut txn, *pos, ins);
2023-07-18 06:39:17 +00:00
}
txn.commit().unwrap();
let snapshot = loro.export_snapshot().unwrap();
2023-07-18 06:39:17 +00:00
let updates = loro.export_from(&Default::default());
let json_updates = serde_json::to_string(&loro.export_json_updates(
&Default::default(),
&loro.oplog_vv(),
true,
))
.unwrap();
2023-07-18 06:39:17 +00:00
println!("\n");
println!("Snapshot size={}", snapshot.len());
println!("Updates size={}", updates.len());
println!("Json Updates size={}", json_updates.len());
2023-07-18 06:39:17 +00:00
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();
2023-11-28 08:22:43 +00:00
text.delete_with_txn(&mut txn, *pos, *del);
text.insert_with_txn(&mut txn, *pos, ins);
2023-07-18 06:39:17 +00:00
txn.commit().unwrap();
}
let snapshot = loro.export_snapshot().unwrap();
2023-07-18 06:39:17 +00:00
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() {
println!("Bench decode");
2023-07-17 15:18:18 +00:00
let actions = bench_utils::get_automerge_actions();
{
let mut loro = LoroDoc::default();
2023-07-17 15:18:18 +00:00
let text = loro.get_text("text");
loro.start_auto_commit();
2023-07-17 15:18:18 +00:00
for _ in 0..10 {
for TextAction { pos, ins, del } in actions.iter() {
2023-11-28 08:22:43 +00:00
text.delete(*pos, *del);
text.insert(*pos, ins);
}
2023-07-17 15:18:18 +00:00
}
let snapshot = loro.export_snapshot().unwrap();
// 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();
2023-11-28 08:22:43 +00:00
text.delete_with_txn(&mut txn, *pos, *del);
text.insert_with_txn(&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();
}
}