Skip to content

Commit 21495fc

Browse files
committed
chore: write a benchmark for measuring performance of dex module.
1 parent 5e89e5b commit 21495fc

4 files changed

Lines changed: 212 additions & 8 deletions

File tree

Cargo.lock

Lines changed: 141 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,15 @@ protobuf-parse = { workspace = true, optional = true }
348348
yara-x-proto = { workspace = true, optional = true }
349349

350350
[dev-dependencies]
351+
criterion = "0.5"
351352
globwalk = { workspace = true }
352353
goldenfile = { workspace = true }
353354
ihex = { workspace = true }
354355
pretty_assertions = { workspace = true }
355356
rayon = { workspace = true }
356357
yara-x-proto-yaml = { workspace = true }
357-
zip = { workspace = true }
358+
zip = { workspace = true }
359+
360+
[[bench]]
361+
name = "dex"
362+
harness = false

lib/benches/commons/mod.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::fs::File;
2+
use std::io::Read;
3+
use std::path::Path;
4+
5+
pub fn create_binary_from_ihex(ihex: &str) -> Vec<u8> {
6+
let mut reader = ihex::Reader::new(ihex);
7+
let mut data = Vec::new();
8+
while let Some(Ok(record)) = reader.next() {
9+
if let ihex::Record::Data { value, .. } = record {
10+
data.extend(value);
11+
}
12+
}
13+
data
14+
}
15+
16+
pub fn create_binary_from_zipped_ihex<P: AsRef<Path>>(path: P) -> Vec<u8> {
17+
let path = path.as_ref();
18+
let f = File::open(path)
19+
.unwrap_or_else(|_| panic!("can not open file: {:?}", &path));
20+
21+
let mut zip = zip::ZipArchive::new(f)
22+
.unwrap_or_else(|_| panic!("can not unzip file: {:?}", &path));
23+
24+
let path_without_zip_ext = path.with_extension("");
25+
let inner_file_name =
26+
path_without_zip_ext.file_name().unwrap().to_str().unwrap();
27+
28+
let mut inner_file = zip.by_name(inner_file_name).unwrap_or_else(|_| {
29+
panic!(
30+
"ZIP archive {:?} doesn't contain file: {:?}",
31+
&path, &inner_file_name
32+
)
33+
});
34+
35+
let mut ihex = String::new();
36+
inner_file.read_to_string(&mut ihex).unwrap_or_else(|_| {
37+
panic!("can not read ihex content from : {:?}", &path)
38+
});
39+
40+
create_binary_from_ihex(ihex.as_str())
41+
}

lib/benches/dex.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2+
3+
mod commons;
4+
5+
use commons::create_binary_from_zipped_ihex;
6+
7+
fn bench_dex(c: &mut Criterion) {
8+
let data = create_binary_from_zipped_ihex(
9+
"src/modules/dex/tests/testdata/c14c75d58399825287e0ee0fcfede6ec06f93489fb52f70bca2736fae5fceab2.in.zip",
10+
);
11+
12+
let mut group = c.benchmark_group("dex");
13+
14+
group.bench_function("parse", |b| {
15+
b.iter(|| {
16+
let _ = black_box(yara_x::mods::invoke::<yara_x::mods::Dex>(black_box(&data)));
17+
});
18+
});
19+
20+
group.finish();
21+
}
22+
23+
criterion_group!(benches, bench_dex);
24+
criterion_main!(benches);

0 commit comments

Comments
 (0)