2022-11-17 02:22:28 +00:00
|
|
|
use criterion::{criterion_group, criterion_main, Criterion};
|
|
|
|
#[cfg(feature = "test_utils")]
|
|
|
|
mod run {
|
|
|
|
use super::*;
|
|
|
|
use arbitrary::Arbitrary;
|
|
|
|
use arbitrary::Unstructured;
|
2023-07-31 03:49:55 +00:00
|
|
|
use loro_internal::LoroDoc;
|
2023-01-16 10:44:19 +00:00
|
|
|
use loro_internal::LoroValue;
|
2022-11-17 02:22:28 +00:00
|
|
|
use rand::Rng;
|
|
|
|
use rand::SeedableRng;
|
|
|
|
|
|
|
|
pub fn many_list_containers(c: &mut Criterion) {
|
|
|
|
#[derive(Arbitrary)]
|
|
|
|
struct Action {
|
|
|
|
container: u8,
|
|
|
|
actor: u8,
|
|
|
|
pos: u8,
|
|
|
|
value: u8,
|
|
|
|
sync: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut rgn = rand::rngs::StdRng::seed_from_u64(0);
|
|
|
|
let mut bytes = Vec::new();
|
|
|
|
for _ in 0..10000 {
|
|
|
|
bytes.push(rgn.gen::<u8>());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut gen = Unstructured::new(&bytes);
|
|
|
|
let actions: [Action; 200] = gen.arbitrary().unwrap();
|
|
|
|
let mut b = c.benchmark_group("10 list containers");
|
|
|
|
b.sample_size(10);
|
|
|
|
b.bench_function("sync random inserts to 10 list containers", |b| {
|
|
|
|
b.iter(|| {
|
2023-07-31 03:49:55 +00:00
|
|
|
let mut actors: Vec<_> = (0..10).map(|_| LoroDoc::default()).collect();
|
2022-11-17 02:22:28 +00:00
|
|
|
for action in actions.iter() {
|
|
|
|
let len = actors.len();
|
|
|
|
let actor = &mut actors[action.actor as usize % len];
|
|
|
|
let container = action.container % 10;
|
|
|
|
if container % 2 == 0 {
|
2023-07-31 03:49:55 +00:00
|
|
|
let text = actor.get_text(container.to_string().as_str());
|
|
|
|
let mut txn = actor.txn().unwrap();
|
2023-11-28 08:22:43 +00:00
|
|
|
text.insert_with_txn(
|
2023-07-31 03:49:55 +00:00
|
|
|
&mut txn,
|
2023-08-04 02:45:23 +00:00
|
|
|
(action.pos as usize) % text.len_unicode().max(1),
|
2022-11-17 02:22:28 +00:00
|
|
|
action.value.to_string().as_str(),
|
2022-11-22 03:51:55 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-11-17 02:22:28 +00:00
|
|
|
} else {
|
2023-07-31 03:49:55 +00:00
|
|
|
let list = actor.get_list(container.to_string().as_str());
|
|
|
|
let mut txn = actor.txn().unwrap();
|
2023-11-28 08:22:43 +00:00
|
|
|
list.insert_with_txn(
|
2023-07-31 03:49:55 +00:00
|
|
|
&mut txn,
|
2022-11-18 08:19:35 +00:00
|
|
|
(action.pos as usize) % list.len().max(1),
|
2023-07-31 03:49:55 +00:00
|
|
|
action.value.to_string().as_str().into(),
|
2022-11-22 03:51:55 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-11-17 02:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let a = (action.actor as usize) % len;
|
|
|
|
let b = (action.sync as usize) % len;
|
|
|
|
if a != b {
|
|
|
|
let (a, b) = arref::array_mut_ref!(&mut actors, [a, b]);
|
2023-08-04 02:45:23 +00:00
|
|
|
a.import(&b.export_from(&a.oplog_vv())).unwrap();
|
2022-11-17 02:22:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 1..actors.len() {
|
|
|
|
let (a, b) = arref::array_mut_ref!(&mut actors, [0, i]);
|
2023-08-04 02:45:23 +00:00
|
|
|
a.import(&b.export_from(&a.oplog_vv())).unwrap();
|
2022-11-17 02:22:28 +00:00
|
|
|
}
|
|
|
|
for i in 1..actors.len() {
|
|
|
|
let (a, b) = arref::array_mut_ref!(&mut actors, [i, 0]);
|
2023-08-04 02:45:23 +00:00
|
|
|
a.import(&b.export_from(&a.oplog_vv())).unwrap();
|
2022-11-17 02:22:28 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
2022-12-08 15:02:44 +00:00
|
|
|
|
|
|
|
pub fn many_actors(c: &mut Criterion) {
|
|
|
|
let mut b = c.benchmark_group("many_actors");
|
|
|
|
b.sample_size(10);
|
|
|
|
b.bench_function("100 actors", |b| {
|
|
|
|
b.iter(|| {
|
2023-07-31 03:49:55 +00:00
|
|
|
let mut actors: Vec<_> = (0..100).map(|_| LoroDoc::default()).collect();
|
2022-12-08 15:02:44 +00:00
|
|
|
for (i, actor) in actors.iter_mut().enumerate() {
|
2023-07-31 03:49:55 +00:00
|
|
|
let list = actor.get_list("list");
|
2022-12-08 15:02:44 +00:00
|
|
|
let value: LoroValue = i.to_string().into();
|
2023-07-31 03:49:55 +00:00
|
|
|
let mut txn = actor.txn().unwrap();
|
2023-11-28 08:22:43 +00:00
|
|
|
list.insert_with_txn(&mut txn, 0, value).unwrap();
|
2022-12-08 15:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i in 1..actors.len() {
|
|
|
|
let (a, b) = arref::array_mut_ref!(&mut actors, [0, i]);
|
2023-08-04 02:45:23 +00:00
|
|
|
a.import(&b.export_from(&a.oplog_vv())).unwrap();
|
2022-12-08 15:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i in 1..actors.len() {
|
|
|
|
let (a, b) = arref::array_mut_ref!(&mut actors, [0, i]);
|
2023-08-04 02:45:23 +00:00
|
|
|
b.import(&a.export_from(&b.oplog_vv())).unwrap();
|
2022-12-08 15:02:44 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
2022-11-17 02:22:28 +00:00
|
|
|
}
|
|
|
|
pub fn dumb(_c: &mut Criterion) {}
|
|
|
|
|
|
|
|
#[cfg(feature = "test_utils")]
|
2022-12-08 15:02:44 +00:00
|
|
|
criterion_group!(benches, run::many_list_containers, run::many_actors);
|
2022-11-17 02:22:28 +00:00
|
|
|
#[cfg(not(feature = "test_utils"))]
|
|
|
|
criterion_group!(benches, dumb);
|
|
|
|
criterion_main!(benches);
|