| 1 |
extern crate criterion; |
| 2 |
extern crate diff; |
| 3 |
|
| 4 |
use criterion::Criterion; |
| 5 |
|
| 6 |
criterion::criterion_group!(benches, bench_slice, bench_chars, bench_real_world); |
| 7 |
criterion::criterion_main!(benches); |
| 8 |
|
| 9 |
fn bench_slice(c: &mut Criterion) { |
| 10 |
c.bench_function("empty", |b| { |
| 11 |
let slice = [0u8; 0]; |
| 12 |
b.iter(|| ::diff::slice(&slice, &slice)); |
| 13 |
}); |
| 14 |
|
| 15 |
c.bench_function("10 equal items", |b| { |
| 16 |
let slice = [0u8; 10]; |
| 17 |
b.iter(|| ::diff::slice(&slice, &slice)); |
| 18 |
}); |
| 19 |
|
| 20 |
c.bench_function("10 non-equal items", |b| { |
| 21 |
let (left, right) = ([0u8; 10], [1u8; 10]); |
| 22 |
b.iter(|| ::diff::slice(&left, &right)); |
| 23 |
}); |
| 24 |
|
| 25 |
c.bench_function("100 equal items", |b| { |
| 26 |
let slice = [0u8; 100]; |
| 27 |
b.iter(|| ::diff::slice(&slice, &slice)); |
| 28 |
}); |
| 29 |
|
| 30 |
c.bench_function("100 non-equal items", |b| { |
| 31 |
let (left, right) = ([0u8; 100], [1u8; 100]); |
| 32 |
b.iter(|| ::diff::slice(&left, &right)); |
| 33 |
}); |
| 34 |
|
| 35 |
c.bench_function("1000 equal items", |b| { |
| 36 |
let slice = [0u8; 1000]; |
| 37 |
b.iter(|| ::diff::slice(&slice, &slice)); |
| 38 |
}); |
| 39 |
|
| 40 |
c.bench_function("1000 non-equal items", |b| { |
| 41 |
let (left, right) = ([0u8; 1000], [1u8; 1000]); |
| 42 |
b.iter(|| ::diff::slice(&left, &right)); |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
fn bench_chars(c: &mut Criterion) { |
| 47 |
c.bench_function("1024 byte string, last 256 different", |b| { |
| 48 |
let left = "?".repeat(768) + &"_".repeat(256); |
| 49 |
let right = "?".repeat(768) + &"!".repeat(256); |
| 50 |
assert_eq!(left.len(), right.len()); |
| 51 |
b.iter(|| ::diff::chars(&left, &right)); |
| 52 |
}); |
| 53 |
} |
| 54 |
|
| 55 |
fn bench_real_world(c: &mut Criterion) { |
| 56 |
let gitignores = std::fs::read_to_string("tests/data/gitignores.txt") |
| 57 |
.unwrap() |
| 58 |
.split("!!!") |
| 59 |
.filter_map(|str| (!str.is_empty()).then(|| str.into())) |
| 60 |
.collect::<Vec<String>>(); |
| 61 |
|
| 62 |
c.bench_function("diff::lines on gitignore files from rust-lang/rust", |b| { |
| 63 |
b.iter(|| { |
| 64 |
for (i, left) in gitignores.iter().enumerate() { |
| 65 |
// diff with previous 3, itself, and next 3 |
| 66 |
for right in gitignores[i.saturating_sub(3)..(i + 3).min(gitignores.len())].iter() { |
| 67 |
::diff::lines(&left, &right); |
| 68 |
} |
| 69 |
} |
| 70 |
}) |
| 71 |
}); |
| 72 |
|
| 73 |
c.bench_function("diff::chars on gitignore files from rust-lang/rust", |b| { |
| 74 |
b.iter(|| { |
| 75 |
for (i, left) in gitignores.iter().enumerate() { |
| 76 |
// diff with previous 2, itself, and next 2 |
| 77 |
for right in gitignores[i.saturating_sub(2)..(i + 2).min(gitignores.len())].iter() { |
| 78 |
::diff::chars(&left, &right); |
| 79 |
} |
| 80 |
} |
| 81 |
}) |
| 82 |
}); |
| 83 |
} |