Add criterion benchmarks for Rust ()

This commit is contained in:
Andrew Jeffery 2021-12-19 14:59:40 +00:00 committed by GitHub
parent fc89a26302
commit e6b806b458
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 111 additions and 24 deletions

View file

@ -9,3 +9,6 @@ members = [
debug = true
lto = true
opt-level = 3
[profile.bench]
debug = true

View file

@ -41,3 +41,7 @@ And finally to test the js library. This is where most of the tests reside.
## testing
$ yarn test
```
## Benchmarking
The `edit-trace` folder has the main code for running the edit trace benchmarking.

View file

@ -7,5 +7,10 @@ edition = "2018"
[dependencies]
automerge = { path = "../automerge" }
criterion = "0.3.5"
json = "0.12.4"
rand = "^0.8"
[[bench]]
name = "main"
harness = false

View file

@ -1,49 +1,52 @@
Try the different editing traces on different automerge implementations
### Automerge Experiement - pure rust
```code
# cargo --release run
```
```code
# cargo --release run
```
#### Benchmarks
There are some criterion benchmarks in the `benches` folder which can be run with `cargo bench` or `cargo criterion`.
For flamegraphing, `cargo flamegraph --bench main -- --bench "save" # or "load" or "replay" or nothing` can be useful.
### Automerge Experiement - wasm api
```code
# node automerge-wasm.js
```
```code
# node automerge-wasm.js
```
### Automerge Experiment - JS wrapper
```code
# node automerge-js.js
```
```code
# node automerge-js.js
```
### Automerge 1.0 pure javascript - new fast backend
This assume automerge has been checked out in a directory along side this repo
```code
# node automerge-1.0.js
```
```code
# node automerge-1.0.js
```
### Automerge 1.0 with rust backend
This assume automerge has been checked out in a directory along side this repo
```code
# node automerge-rs.js
```
```code
# node automerge-rs.js
```
### Automerge Experiment - JS wrapper
```code
# node automerge-js.js
```
```code
# node automerge-js.js
```
### Baseline Test. Javascript Array with no CRDT info
```code
# node baseline.js
```
### Baseline Test. Javascript Array with no CRDT info
```code
# node baseline.js
```

View file

@ -0,0 +1,71 @@
use automerge::{Automerge, Value, ROOT};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use std::fs;
fn replay_trace(commands: Vec<(usize, usize, Vec<Value>)>) -> Automerge {
let mut doc = Automerge::new();
let text = doc.set(ROOT, "text", Value::text()).unwrap();
for (pos, del, vals) in commands {
doc.splice(text, pos, del, vals).unwrap();
}
doc.commit(None, None);
doc
}
fn save_trace(mut doc: Automerge) {
doc.save().unwrap();
}
fn load_trace(bytes: &[u8]) {
Automerge::load(bytes).unwrap();
}
fn bench(c: &mut Criterion) {
let contents = fs::read_to_string("edits.json").expect("cannot read edits file");
let edits = json::parse(&contents).expect("cant parse edits");
let mut commands = vec![];
for i in 0..edits.len() {
let pos: usize = edits[i][0].as_usize().unwrap();
let del: usize = edits[i][1].as_usize().unwrap();
let mut vals = vec![];
for j in 2..edits[i].len() {
let v = edits[i][j].as_str().unwrap();
vals.push(Value::str(v));
}
commands.push((pos, del, vals));
}
let mut group = c.benchmark_group("edit trace");
group.throughput(Throughput::Elements(commands.len() as u64));
group.bench_with_input(
BenchmarkId::new("replay", commands.len()),
&commands,
|b, commands| {
b.iter_batched(
|| commands.clone(),
replay_trace,
criterion::BatchSize::LargeInput,
)
},
);
let commands_len = commands.len();
let mut doc = replay_trace(commands);
group.bench_with_input(BenchmarkId::new("save", commands_len), &doc, |b, doc| {
b.iter_batched(|| doc.clone(), save_trace, criterion::BatchSize::LargeInput)
});
let bytes = doc.save().unwrap();
group.bench_with_input(
BenchmarkId::new("load", commands_len),
&bytes,
|b, bytes| b.iter(|| load_trace(bytes)),
);
group.finish();
}
criterion_group!(benches, bench);
criterion_main!(benches);

View file

@ -46,6 +46,7 @@
wasm-pack
pkgconfig
openssl
gnuplot
nodejs
yarn