test: add B4 bench

This commit is contained in:
Zixuan Chen 2022-10-31 16:16:44 +08:00
parent fa2db1be7e
commit 22465a5e97
4 changed files with 61 additions and 1 deletions

21
Cargo.lock generated
View file

@ -236,6 +236,15 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "crc32fast"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d"
dependencies = [
"cfg-if",
]
[[package]] [[package]]
name = "crdt-list" name = "crdt-list"
version = "0.3.0" version = "0.3.0"
@ -409,6 +418,16 @@ dependencies = [
"instant", "instant",
] ]
[[package]]
name = "flate2"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f82b0f4c27ad9f8bfd1f3208d882da2b09c301bc1c828fd3a00d0216d2fbbff6"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -579,6 +598,7 @@ dependencies = [
"ctor", "ctor",
"dhat", "dhat",
"enum-as-inner", "enum-as-inner",
"flate2",
"fxhash", "fxhash",
"im", "im",
"js-sys", "js-sys",
@ -591,6 +611,7 @@ dependencies = [
"ring", "ring",
"rle", "rle",
"serde", "serde",
"serde_json",
"smallvec", "smallvec",
"smartstring", "smartstring",
"static_assertions", "static_assertions",

View file

@ -38,6 +38,8 @@ tabled = "0.10.0"
color-backtrace = { version = "0.5" } color-backtrace = { version = "0.5" }
ctor = "0.1.23" ctor = "0.1.23"
criterion = "0.4.0" criterion = "0.4.0"
serde_json = "1.0.87"
flate2 = "1.0.24"
# See https://matklad.github.io/2021/02/27/delete-cargo-integration-tests.html # See https://matklad.github.io/2021/02/27/delete-cargo-integration-tests.html
[lib] [lib]

Binary file not shown.

View file

@ -1,12 +1,19 @@
use criterion::{criterion_group, criterion_main, Criterion}; use criterion::{criterion_group, criterion_main, Criterion};
const RAW_DATA: &[u8; 901823] = include_bytes!("automerge-paper.json.gz");
#[cfg(feature = "fuzzing")] #[cfg(feature = "fuzzing")]
mod run { mod run {
use std::io::Read;
use super::*; use super::*;
use arbitrary::Unstructured; use arbitrary::Unstructured;
use flate2::read::GzDecoder;
use loro_core::fuzz::test_multi_sites; use loro_core::fuzz::test_multi_sites;
use loro_core::fuzz::Action; use loro_core::fuzz::Action;
use loro_core::LoroCore;
use rand::Rng; use rand::Rng;
use rand::SeedableRng; use rand::SeedableRng;
use serde_json::Value;
pub fn criterion_benchmark(c: &mut Criterion) { pub fn criterion_benchmark(c: &mut Criterion) {
let mut rgn = rand::rngs::StdRng::seed_from_u64(0); let mut rgn = rand::rngs::StdRng::seed_from_u64(0);
@ -21,11 +28,41 @@ mod run {
b.iter(|| test_multi_sites(2, actions.clone().into())) b.iter(|| test_multi_sites(2, actions.clone().into()))
}); });
} }
pub fn b4(c: &mut Criterion) {
let mut d = GzDecoder::new(&RAW_DATA[..]);
let mut s = String::new();
d.read_to_string(&mut s).unwrap();
let json: Value = serde_json::from_str(&s).unwrap();
let txns = json.as_object().unwrap().get("txns");
c.bench_function("B4", |b| {
b.iter(|| {
let mut loro = LoroCore::default();
let mut text = loro.get_or_create_root_text("text").unwrap();
for txn in txns.unwrap().as_array().unwrap() {
let patches = txn
.as_object()
.unwrap()
.get("patches")
.unwrap()
.as_array()
.unwrap();
for patch in patches {
let pos = patch[0].as_u64().unwrap() as usize;
let del_here = patch[1].as_u64().unwrap() as usize;
let ins_content = patch[2].as_str().unwrap();
text.delete(pos, del_here);
text.insert(pos, ins_content);
}
}
})
});
}
} }
pub fn dumb(_c: &mut Criterion) {} pub fn dumb(_c: &mut Criterion) {}
#[cfg(feature = "fuzzing")] #[cfg(feature = "fuzzing")]
criterion_group!(benches, run::criterion_benchmark); criterion_group!(benches, run::criterion_benchmark, run::b4);
#[cfg(not(feature = "fuzzing"))] #[cfg(not(feature = "fuzzing"))]
criterion_group!(benches, dumb); criterion_group!(benches, dumb);
criterion_main!(benches); criterion_main!(benches);