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

184 lines
6.6 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::container::registry::ContainerWrapper;
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| {
let mut c1 = LoroCore::new(Default::default(), Some(0));
let mut c2 = LoroCore::new(Default::default(), Some(1));
let t1 = c1.get_text("text");
let t2 = c2.get_text("text");
b.iter(|| {
for (i, action) in actions.iter().enumerate() {
2022-12-27 06:18:46 +00:00
let TextAction { pos, ins, del } = action;
if i % 2 == 0 {
t1.with_container(|text| {
text.delete(&c1, *pos, *del);
2022-12-27 06:18:46 +00:00
text.insert(&c1, *pos, ins);
});
let update = c1.encode_from(c2.vv_cloned());
c2.decode(&update).unwrap();
} else {
t2.with_container(|text| {
text.delete(&c2, *pos, *del);
2022-12-27 06:18:46 +00:00
text.insert(&c2, *pos, ins);
});
let update = c2.encode_from(c1.vv_cloned());
c1.decode(&update).unwrap();
}
}
})
});
b.bench_function("rle update", |b| {
let mut c1 = LoroCore::new(Default::default(), Some(0));
let mut c2 = LoroCore::new(Default::default(), Some(1));
let t1 = c1.get_text("text");
let t2 = c2.get_text("text");
b.iter(|| {
for (i, action) in actions.iter().enumerate() {
2022-12-27 06:18:46 +00:00
let TextAction { pos, ins, del } = action;
if i % 2 == 0 {
t1.with_container(|text| {
text.delete(&c1, *pos, *del);
2022-12-27 06:18:46 +00:00
text.insert(&c1, *pos, ins);
});
let update = c1.encode_from(c2.vv_cloned());
c2.decode(&update).unwrap();
} else {
t2.with_container(|text| {
text.delete(&c2, *pos, *del);
2022-12-27 06:18:46 +00:00
text.insert(&c2, *pos, ins);
});
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;
use loro_internal::container::registry::ContainerWrapper;
use loro_internal::log_store::{EncodeConfig, EncodeMode};
use loro_internal::{LoroCore, 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();
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-11-17 03:34:48 +00:00
}
});
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(|| {
let _ = loro.encode_with_cfg(
EncodeConfig::new(EncodeMode::Updates(VersionVector::new())).without_compress(),
);
2022-12-02 06:03:29 +00:00
})
});
b.bench_function("B4_decode_updates", |b| {
let buf = loro.encode_with_cfg(
EncodeConfig::new(EncodeMode::Updates(VersionVector::new())).without_compress(),
);
2022-12-02 06:03:29 +00:00
let mut store2 = LoroCore::default();
b.iter(|| {
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(|| {
let _ = loro.encode_with_cfg(
EncodeConfig::new(EncodeMode::RleUpdates(VersionVector::new()))
.without_compress(),
);
})
});
b.bench_function("B4_decode_rle_updates", |b| {
let buf = loro.encode_with_cfg(
EncodeConfig::new(EncodeMode::RleUpdates(VersionVector::new())).without_compress(),
);
let mut store2 = LoroCore::default();
b.iter(|| {
store2.decode(&buf).unwrap();
})
});
b.bench_function("B4_encode_snapshot", |b| {
2022-12-02 06:03:29 +00:00
b.iter(|| {
let _ = loro.encode_all();
2022-12-02 06:03:29 +00:00
})
});
b.bench_function("B4_decode_snapshot", |b| {
let buf = loro.encode_all();
2022-12-14 13:02:01 +00:00
let mut store2 = LoroCore::default();
2022-12-02 06:03:29 +00:00
b.iter(|| {
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| {
let mut c1 = LoroCore::new(
Configure {
change: ChangeMergeCfg {
max_change_length: 0,
max_change_interval: 0,
},
..Default::default()
},
Some(1),
);
let mut c2 = LoroCore::new(
Configure {
change: ChangeMergeCfg {
max_change_length: 0,
max_change_interval: 0,
},
..Default::default()
},
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();
}
b.iter(|| {
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);