Skip to content

Commit

Permalink
feat: move compatibility test to utils (y-crdt/y-octo#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Sep 27, 2023
1 parent d34fe0f commit 90ba069
Show file tree
Hide file tree
Showing 54 changed files with 912 additions and 1,592 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ jobs:
run: |
cargo bench --bench codec_benchmarks --features bench -- --save-baseline master
cargo bench --bench array_ops_benchmarks --features bench -- --save-baseline master
# temp disable due to wired memory allocation issue in linux
# i cannot reproduce this issue in mac, but can reproduce in gce and ci
# cargo bench --bench map_ops_benchmarks --features bench -- --save-baseline master
cargo bench --bench map_ops_benchmarks --features bench -- --save-baseline master
cargo bench --bench text_ops_benchmarks --features bench -- --save-baseline master
cargo bench --bench update_benchmarks --features bench -- --save-baseline master
Expand All @@ -49,7 +47,7 @@ jobs:
run: |
cargo bench --bench codec_benchmarks --features bench -- --save-baseline pr
cargo bench --bench array_ops_benchmarks --features bench -- --save-baseline pr
# cargo bench --bench map_ops_benchmarks --features bench -- --save-baseline pr
cargo bench --bench map_ops_benchmarks --features bench -- --save-baseline pr
cargo bench --bench text_ops_benchmarks --features bench -- --save-baseline pr
cargo bench --bench update_benchmarks --features bench -- --save-baseline pr
Expand Down
14 changes: 10 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ opt-level = 3

[patch.crates-io]
lib0 = { git = "https://github.com/toeverything/y-crdt", rev = "a700f09" }
y-sync = { git = "https://github.com/toeverything/y-sync", rev = "5626851" }
yrs = { git = "https://github.com/toeverything/y-crdt", rev = "a700f09" }
8 changes: 4 additions & 4 deletions apps/keck/src/server/api/blocks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ pub async fn delete_block(
.await
.and_then(|mut ws| Ok(ws.get_blocks()?))
{
if space.remove(&block) {
return StatusCode::NO_CONTENT;
}
space.remove(&block);
StatusCode::NO_CONTENT
} else {
StatusCode::NOT_FOUND
}
StatusCode::NOT_FOUND
}

/// Get children in `Block`
Expand Down
2 changes: 1 addition & 1 deletion apps/keck/src/server/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Context {
.post(webhook)
.json(
&history
.into_iter()
.iter()
.map(|h| (ws_id.as_str(), h).into())
.collect::<Vec<BlockHistory>>(),
)
Expand Down
8 changes: 4 additions & 4 deletions libs/jwst-binding/jwst-jni/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ foreign_class!(
.join("\n");
fs::write(&in_temp, &template).unwrap();

let template_changed = fs::read_to_string(&in_src).unwrap() != template;
let template_changed = fs::read_to_string(in_src).unwrap() != template;

if template_changed || !in_temp.with_extension("").exists() || !jni_dir.exists() {
// delete the lib folder then create it again to prevent obsolete files
Expand All @@ -100,10 +100,10 @@ foreign_class!(
swig_gen.expand("android bindings", &in_temp, in_temp.with_extension("out"));

if !in_temp.with_extension("").exists()
|| fs::read_to_string(&in_temp.with_extension("out")).unwrap()
!= fs::read_to_string(&in_temp.with_extension("")).unwrap()
|| fs::read_to_string(in_temp.with_extension("out")).unwrap()
!= fs::read_to_string(in_temp.with_extension("")).unwrap()
{
fs::copy(&in_temp.with_extension("out"), &in_temp.with_extension("")).unwrap();
fs::copy(in_temp.with_extension("out"), in_temp.with_extension("")).unwrap();
}
}
}
5 changes: 1 addition & 4 deletions libs/jwst-binding/jwst-jni/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ impl Workspace {

#[generate_interface]
pub fn remove(&mut self, block_id: String) -> bool {
self.workspace
.get_blocks()
.map(|mut s| s.remove(&block_id))
.unwrap_or(false)
self.workspace.get_blocks().map(|mut s| s.remove(&block_id)).is_ok()
}

#[generate_interface]
Expand Down
46 changes: 43 additions & 3 deletions libs/jwst-codec-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,55 @@ edition = "2021"

[features]
bench = ["regex"]
fuzz = ["arbitrary", "jwst-codec", "phf", "yrs"]
fuzz = ["arbitrary", "phf"]

[dependencies]
arbitrary = { version = "1.3.0", features = ["derive"], optional = true }
jwst-codec = { workspace = true, optional = true }
phf = { version = "0.11", features = ["macros"], optional = true }
rand = "0.8"
rand_chacha = "0.3"
regex = { version = "1.5", optional = true }
yrs = { version = "=0.16.5", optional = true }

# ======= bench dependencies =======
jwst-codec = { workspace = true }

lib0 = { version = "=0.16.5", features = ["lib0-serde"] }
y-sync = "=0.3.1"
yrs = "=0.16.5"

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
path-ext = "0.1"
proptest = "1.2"
proptest-derive = "0.4"

[[bin]]
name = "bench_result_render"
path = "bin/bench_result_render.rs"

[[bin]]
name = "memory_leak_test"
path = "bin/memory_leak_test.rs"

[[bench]]
harness = false
name = "array_ops_benchmarks"

[[bench]]
harness = false
name = "codec_benchmarks"

[[bench]]
harness = false
name = "map_ops_benchmarks"

[[bench]]
harness = false
name = "text_ops_benchmarks"

[[bench]]
harness = false
name = "update_benchmarks"

[lib]
bench = true
79 changes: 79 additions & 0 deletions libs/jwst-codec-utils/benches/array_ops_benchmarks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use std::time::Duration;

use criterion::{criterion_group, criterion_main, Criterion};
use rand::{Rng, SeedableRng};

fn operations(c: &mut Criterion) {
let mut group = c.benchmark_group("ops/array");
group.measurement_time(Duration::from_secs(15));

group.bench_function("yrs/insert", |b| {
let base_text = "test1 test2 test3 test4 test5 test6 test7 test8 test9";
let mut rng = rand_chacha::ChaCha20Rng::seed_from_u64(1234);

let idxs = (0..99)
.map(|_| rng.gen_range(0..base_text.len() as u32))
.collect::<Vec<_>>();
b.iter(|| {
use yrs::*;
let doc = Doc::new();
let array = doc.get_or_insert_array("test");

let mut trx = doc.transact_mut();
for c in base_text.chars() {
array.push_back(&mut trx, c.to_string()).unwrap();
}
for idx in &idxs {
array.insert(&mut trx, *idx, "test").unwrap();
}
drop(trx);
});
});

group.bench_function("yrs/insert range", |b| {
let base_text = "test1 test2 test3 test4 test5 test6 test7 test8 test9";
let mut rng = rand_chacha::ChaCha20Rng::seed_from_u64(1234);

let idxs = (0..99)
.map(|_| rng.gen_range(0..base_text.len() as u32))
.collect::<Vec<_>>();
b.iter(|| {
use yrs::*;
let doc = Doc::new();
let array = doc.get_or_insert_array("test");

let mut trx = doc.transact_mut();
for c in base_text.chars() {
array.push_back(&mut trx, c.to_string()).unwrap();
}
for idx in &idxs {
array.insert_range(&mut trx, *idx, vec!["test1", "test2"]).unwrap();
}
drop(trx);
});
});

group.bench_function("yrs/remove", |b| {
let base_text = "test1 test2 test3 test4 test5 test6 test7 test8 test9";

b.iter(|| {
use yrs::*;
let doc = Doc::new();
let array = doc.get_or_insert_array("test");

let mut trx = doc.transact_mut();
for c in base_text.chars() {
array.push_back(&mut trx, c.to_string()).unwrap();
}
for idx in (base_text.len() as u32)..0 {
array.remove(&mut trx, idx).unwrap();
}
drop(trx);
});
});

group.finish();
}

criterion_group!(benches, operations);
criterion_main!(benches);
89 changes: 89 additions & 0 deletions libs/jwst-codec-utils/benches/codec_benchmarks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use criterion::{criterion_group, criterion_main, Criterion, SamplingMode};
use lib0::{
decoding::{Cursor, Read},
encoding::Write,
};

const BENCHMARK_SIZE: u32 = 100000;

fn codec(c: &mut Criterion) {
let mut codec_group = c.benchmark_group("codec");
codec_group.sampling_mode(SamplingMode::Flat);
{
codec_group.bench_function("lib0 encode var_int (64 bit)", |b| {
b.iter(|| {
let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8);
for i in 0..(BENCHMARK_SIZE as i64) {
encoder.write_var(i);
}
})
});
codec_group.bench_function("lib0 decode var_int (64 bit)", |b| {
let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8);
for i in 0..(BENCHMARK_SIZE as i64) {
encoder.write_var(i);
}

b.iter(|| {
let mut decoder = Cursor::from(&encoder);
for i in 0..(BENCHMARK_SIZE as i64) {
let num: i64 = decoder.read_var().unwrap();
assert_eq!(num, i);
}
})
});
}

{
codec_group.bench_function("lib0 encode var_uint (32 bit)", |b| {
b.iter(|| {
let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8);
for i in 0..BENCHMARK_SIZE {
encoder.write_var(i);
}
})
});
codec_group.bench_function("lib0 decode var_uint (32 bit)", |b| {
let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8);
for i in 0..BENCHMARK_SIZE {
encoder.write_var(i);
}

b.iter(|| {
let mut decoder = Cursor::from(&encoder);
for i in 0..BENCHMARK_SIZE {
let num: u32 = decoder.read_var().unwrap();
assert_eq!(num, i);
}
})
});
}

{
codec_group.bench_function("lib0 encode var_uint (64 bit)", |b| {
b.iter(|| {
let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8);
for i in 0..(BENCHMARK_SIZE as u64) {
encoder.write_var(i);
}
})
});
codec_group.bench_function("lib0 decode var_uint (64 bit)", |b| {
let mut encoder = Vec::with_capacity(BENCHMARK_SIZE as usize * 8);
for i in 0..(BENCHMARK_SIZE as u64) {
encoder.write_var(i);
}

b.iter(|| {
let mut decoder = Cursor::from(&encoder);
for i in 0..(BENCHMARK_SIZE as u64) {
let num: u64 = decoder.read_var().unwrap();
assert_eq!(num, i);
}
})
});
}
}

criterion_group!(benches, codec);
criterion_main!(benches);
Loading

0 comments on commit 90ba069

Please sign in to comment.