automerge/automerge-frontend/benches/change.rs
Andrew Jeffery fc45ff09a3
Allow change to return value from users closure (#84)
* Allow change to return value from users closure

* Add closure_result field to OptimisticChangeResult
2021-04-15 10:49:04 +01:00

40 lines
1.4 KiB
Rust

use automerge_frontend::{Frontend, InvalidChangeRequest, LocalChange, Path, Value};
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
use rand::{thread_rng, Rng};
use unicode_segmentation::UnicodeSegmentation;
pub fn insert_long_string(c: &mut Criterion) {
c.bench_function("Frontend::change insert long string", move |b| {
b.iter_batched(
|| {
let doc = Frontend::new();
let random_string: String = thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.take(6000)
.map(char::from)
.collect();
(doc, random_string)
},
|(mut doc, string)| {
#[allow(clippy::unit_arg)]
black_box({
doc.change::<_, _, InvalidChangeRequest>(None, |d| {
d.add_change(LocalChange::set(
Path::root().key("text"),
Value::Text(string.graphemes(true).map(|s| s.to_owned()).collect()),
))
})
.unwrap()
})
},
BatchSize::SmallInput,
)
});
}
criterion_group! {
name = frontend_benches;
config = Criterion::default().sample_size(10);
targets = insert_long_string,
}
criterion_main!(frontend_benches);