This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcompute_bytes_benchmark.rs
67 lines (56 loc) · 2.05 KB
/
compute_bytes_benchmark.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use arklib::resource::{ResourceId, ResourceIdTrait};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand::prelude::*;
use std::fs;
const FILE_PATHS: [&str; 2] = ["tests/lena.jpg", "tests/test.pdf"]; // Add files to benchmark here
fn generate_random_data(size: usize) -> Vec<u8> {
let mut rng = rand::thread_rng();
(0..size).map(|_| rng.gen()).collect()
}
fn compute_bytes_on_raw_data(c: &mut Criterion) {
let inputs = [
("compute_bytes_small", 1024),
("compute_bytes_medium", 65536),
("compute_bytes_large", 1048576),
];
for (name, size) in inputs.iter() {
let input_data = generate_random_data(*size);
let mut group = c.benchmark_group(name.to_string());
group.measurement_time(std::time::Duration::from_secs(5)); // Set the measurement time here
group.bench_function("compute_bytes", move |b| {
b.iter(|| {
ResourceId::compute_bytes(black_box(&input_data))
.expect("compute_bytes returned an error")
});
});
group.finish();
}
}
fn compute_bytes_on_files_benchmark(c: &mut Criterion) {
// assert the files exist and are files
for file_path in FILE_PATHS.iter() {
assert!(
std::path::Path::new(file_path).is_file(),
"The file: {} does not exist or is not a file",
file_path
);
}
for file_path in FILE_PATHS.iter() {
let raw_bytes = fs::read(file_path).unwrap();
let mut group = c.benchmark_group(file_path.to_string());
group.measurement_time(std::time::Duration::from_secs(10)); // Set the measurement time here
group.bench_function("compute_bytes", move |b| {
b.iter(|| {
ResourceId::compute_bytes(black_box(&raw_bytes))
.expect("compute_bytes returned an error")
});
});
group.finish();
}
}
criterion_group!(
benches,
compute_bytes_on_raw_data,
compute_bytes_on_files_benchmark
);
criterion_main!(benches);