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

74 lines
2.6 KiB
Rust
Raw Normal View History

2022-12-27 06:18:46 +00:00
use std::{io::Write, time::Instant};
2022-12-27 06:18:46 +00:00
use bench_utils::TextAction;
use flate2::write::GzEncoder;
use loro_internal::VersionVector;
use loro_internal::{container::registry::ContainerWrapper, log_store::EncodeConfig, LoroCore};
fn main() {
2022-12-27 06:18:46 +00:00
let actions = bench_utils::get_automerge_actions();
let mut loro = LoroCore::default();
let text = loro.get_text("text");
text.with_container(|text| {
2022-12-27 06:18:46 +00:00
for TextAction { pos, ins, del } in actions.iter() {
text.delete(&loro, *pos, *del);
text.insert(&loro, *pos, ins);
}
});
2022-12-27 06:18:46 +00:00
2022-11-14 11:31:56 +00:00
let start = Instant::now();
let buf =
loro.encode_with_cfg(EncodeConfig::rle_update(VersionVector::new()).without_compress());
2022-11-14 11:31:56 +00:00
println!(
2022-12-11 12:55:22 +00:00
"encode changes {} bytes, used {}ms",
2022-11-14 11:31:56 +00:00
buf.len(),
2022-12-09 12:58:26 +00:00
start.elapsed().as_millis()
);
2023-02-17 01:52:14 +00:00
let json_ori = loro.to_json();
2022-12-09 12:58:26 +00:00
let start = Instant::now();
2023-02-16 01:09:24 +00:00
let buf_snapshot = loro.encode_with_cfg(EncodeConfig::snapshot().without_compress());
2022-12-11 12:55:22 +00:00
let json_snapshot = loro.to_json();
2022-12-09 12:58:26 +00:00
println!(
2022-12-11 12:55:22 +00:00
"encode snapshot {} bytes, used {}ms",
2022-12-09 12:58:26 +00:00
buf_snapshot.len(),
2022-11-14 11:31:56 +00:00
start.elapsed().as_millis()
);
2023-02-17 01:52:14 +00:00
let json_snapshot = loro.to_json();
2022-12-07 12:56:10 +00:00
let mut loro = LoroCore::default();
2022-12-11 12:55:22 +00:00
let start = Instant::now();
2022-12-14 13:02:01 +00:00
loro.decode(&buf).unwrap();
2023-01-03 09:40:52 +00:00
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();
2023-01-03 09:40:52 +00:00
assert_eq!(json_ori, json2);
2022-12-11 12:55:22 +00:00
let start = Instant::now();
2022-12-14 13:02:01 +00:00
let mut loro2 = LoroCore::default();
loro2.decode(&buf_snapshot).unwrap();
2022-12-11 12:55:22 +00:00
println!("decode snapshot used {}ms", start.elapsed().as_millis());
let json3 = loro2.to_json();
assert_eq!(json_snapshot, json3);
2023-01-03 09:40:52 +00:00
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());
2023-01-03 09:40:52 +00:00
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);
}