Skip to content

Commit

Permalink
Add lexer and reader binary benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbabcock committed Feb 2, 2025
1 parent 60a15ea commit 0216d9e
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions benches/jomini_bench.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use criterion::{
black_box, criterion_group, criterion_main, BenchmarkId, Criterion, SamplingMode, Throughput,
};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use flate2::read::GzDecoder;
use jomini::{
binary::{BinaryFlavor, BinaryTapeParser, TokenResolver},
Expand Down Expand Up @@ -177,15 +175,40 @@ pub fn binary_parse_benchmark(c: &mut Criterion) {
for game in &["eu4", "ck3", "v3"] {
let data = request(&format!("jomini/{game}-bin"));
group.throughput(Throughput::Bytes(data.len() as u64));
group.sampling_mode(SamplingMode::Flat);
group.bench_function(BenchmarkId::new("binary", game), |b| {
group.bench_function(BenchmarkId::new("tape", game), |b| {
let mut tape = BinaryTape::default();
b.iter(|| {
BinaryTapeParser
.parse_slice_into_tape(data.as_slice(), &mut tape)
.unwrap();
})
});

group.bench_function(BenchmarkId::new("lexer", game), |b| {
b.iter(|| {
let mut lexer = jomini::binary::Lexer::new(data.as_slice());
let mut counter = 0;
while let Ok(Some(token)) = lexer.next_token() {
if matches!(token, jomini::binary::Token::Id(_)) {
counter += 1;
}
}
black_box(counter);
})
});

group.bench_function(BenchmarkId::new("reader", game), |b| {
b.iter(|| {
let mut reader = jomini::binary::TokenReader::new(data.as_slice());
let mut counter = 0;
while let Ok(Some(token)) = reader.next() {
if matches!(token, jomini::binary::Token::Id(_)) {
counter += 1;
}
}
black_box(counter);
})
});
}

group.finish();
Expand Down

0 comments on commit 0216d9e

Please sign in to comment.