loro/crates/loro-internal/examples/encoding.rs
2023-03-02 10:37:50 +08:00

74 lines
2.5 KiB
Rust

use std::{io::Write, time::Instant};
use bench_utils::TextAction;
use flate2::write::GzEncoder;
use loro_internal::{log_store::EncodeConfig, LoroCore};
use loro_internal::{Transact, VersionVector};
fn main() {
let actions = bench_utils::get_automerge_actions();
let mut loro = LoroCore::default();
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();
}
drop(txn);
let start = Instant::now();
let buf =
loro.encode_with_cfg(EncodeConfig::rle_update(VersionVector::new()).without_compress());
println!(
"encode changes {} bytes, used {}ms",
buf.len(),
start.elapsed().as_millis()
);
let json_ori = loro.to_json();
let start = Instant::now();
let buf_snapshot = loro.encode_with_cfg(EncodeConfig::snapshot().without_compress());
let json_snapshot = loro.to_json();
println!(
"encode snapshot {} bytes, used {}ms",
buf_snapshot.len(),
start.elapsed().as_millis()
);
let json_snapshot = loro.to_json();
let mut loro = LoroCore::default();
let start = Instant::now();
loro.decode(&buf).unwrap();
println!("decode rle_updates used {}ms", start.elapsed().as_millis());
let buf2 =
loro.encode_with_cfg(EncodeConfig::rle_update(VersionVector::new()).without_compress());
assert_eq!(buf, buf2);
let json2 = loro.to_json();
assert_eq!(json_ori, json2);
let start = Instant::now();
let mut loro2 = LoroCore::default();
loro2.decode(&buf_snapshot).unwrap();
println!("decode snapshot used {}ms", start.elapsed().as_millis());
let json3 = loro2.to_json();
assert_eq!(json_snapshot, json3);
let start = Instant::now();
let update_buf =
loro.encode_with_cfg(EncodeConfig::update(VersionVector::new()).without_compress());
println!(
"encode updates {} bytes, used {}ms",
update_buf.len(),
start.elapsed().as_millis()
);
let mut encoder = GzEncoder::new(Vec::new(), flate2::Compression::default());
encoder.write_all(&update_buf).unwrap();
let data = encoder.finish().unwrap();
println!("After compress updates have {} bytes", data.len());
let mut loro3 = LoroCore::default();
let start = Instant::now();
loro3.decode(&update_buf).unwrap();
println!("decode updates used {}ms", start.elapsed().as_millis());
let json_update = loro3.to_json();
assert_eq!(json_ori, json_update);
}