aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/benches
diff options
context:
space:
mode:
authorBernardo <[email protected]>2018-12-21 17:51:31 +0000
committerBernardo <[email protected]>2018-12-25 19:03:14 +0000
commitd6312085a1ac97030fa768366585b9cfb6c955cd (patch)
treebdd36cd3efd76fc3a1561847ed66ccece7b75303 /crates/ra_editor/benches
parenta005d2a614031a18c9a5bf6557789a41f1b25c31 (diff)
remove slower impl, add benchmarks
Diffstat (limited to 'crates/ra_editor/benches')
-rw-r--r--crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs88
1 files changed, 88 insertions, 0 deletions
diff --git a/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs b/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs
new file mode 100644
index 000000000..b345a91ae
--- /dev/null
+++ b/crates/ra_editor/benches/translate_offset_with_edit_benchmark.rs
@@ -0,0 +1,88 @@
1use criterion::{criterion_group, criterion_main};
2use criterion::Criterion;
3use criterion::Fun;
4use ra_text_edit::AtomTextEdit;
5use ra_text_edit::test_utils::{arb_edits_custom, arb_offset};
6use ra_editor::line_index_utils;
7use ra_editor::LineIndex;
8use ra_syntax::TextUnit;
9use proptest::test_runner;
10use proptest::string::string_regex;
11use proptest::strategy::{Strategy, ValueTree};
12use rand_xorshift::XorShiftRng;
13use rand::SeedableRng;
14use lazy_static::lazy_static;
15
16#[derive(Debug)]
17struct Data {
18 text: String,
19 line_index: LineIndex,
20 edits: Vec<AtomTextEdit>,
21 offset: TextUnit,
22}
23
24fn setup_data() -> Data {
25 let mut runner = test_runner::TestRunner::default();
26 {
27 struct TestRng {
28 rng: XorShiftRng,
29 }
30 // HACK to be able to manually seed the TestRunner
31 let rng: &mut TestRng = unsafe { std::mem::transmute(runner.rng()) };
32 rng.rng = XorShiftRng::seed_from_u64(0);
33 }
34
35 let text = {
36 let arb = string_regex("([a-zA-Z_0-9]{10,50}.{1,5}\n){100,500}").unwrap();
37 let tree = arb.new_tree(&mut runner).unwrap();
38 tree.current()
39 };
40
41 let edits = {
42 let arb = arb_edits_custom(&text, 99, 100);
43 let tree = arb.new_tree(&mut runner).unwrap();
44 tree.current()
45 };
46
47 let offset = {
48 let arb = arb_offset(&text);
49 let tree = arb.new_tree(&mut runner).unwrap();
50 tree.current()
51 };
52
53 let line_index = LineIndex::new(&text);
54
55 Data {
56 text,
57 line_index,
58 edits,
59 offset,
60 }
61}
62
63lazy_static! {
64 static ref DATA: Data = setup_data();
65}
66
67fn compare_translates(c: &mut Criterion) {
68 let f1 = Fun::new("translate_after_edit", |b, _| {
69 b.iter(|| {
70 let d = &*DATA;
71 line_index_utils::translate_after_edit(&d.text, d.offset, d.edits.clone());
72 })
73 });
74
75 let f2 = Fun::new("count_newlines", |b, _| {
76 b.iter(|| {
77 let d = &*DATA;
78 line_index_utils::count_newlines(d.offset, &d.line_index, &d.edits);
79 })
80 });
81
82 let functions = vec![f1, f2];
83
84 c.bench_functions("translate", functions, ());
85}
86
87criterion_group!(benches, compare_translates);
88criterion_main!(benches);