Skip to content

Commit

Permalink
bench: black box integer test, use macros (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicceboy authored Oct 28, 2024
1 parent b8d294e commit f37cb69
Showing 1 changed file with 36 additions and 50 deletions.
86 changes: 36 additions & 50 deletions benches/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
// Modifications:
// Adds other rasn codecs to the benchmark
// Adds optional and extended fields for `Color`
// Use macros to generate the tests
// Wrap with black_box to prevent the compiler from optimizing out the code

use criterion::{criterion_group, criterion_main, Criterion};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rasn::{ber, oer, uper};

#[allow(non_camel_case_types, non_snake_case, non_upper_case_globals, unused)]
Expand Down Expand Up @@ -117,61 +119,45 @@ pub fn build_sample_rasn() -> world3d::World {
World { depth }
}

fn uper_rasn_enc(c: &mut Criterion) {
c.bench_function("RASN/encode UPER - sample.asn", |b| {
b.iter(|| {
let w = build_sample_rasn();
let _ = uper::encode(&w).unwrap();
})
});
}
fn oer_rasn_enc(c: &mut Criterion) {
c.bench_function("RASN/encode OER - sample.asn", |b| {
b.iter(|| {
let w = build_sample_rasn();
let _ = oer::encode(&w).unwrap();
})
});
}
fn ber_rasn_enc(c: &mut Criterion) {
c.bench_function("RASN/encode BER - sample.asn", |b| {
b.iter(|| {
let w = build_sample_rasn();
let _ = ber::encode(&w).unwrap();
})
});
macro_rules! rasn_enc_fn {
($fn_name:ident, $codec:ident) => {
fn $fn_name(c: &mut Criterion) {
let items = build_sample_rasn();
c.bench_function(
&format!(
"RASN/encode {} - extended integer sample",
stringify!($codec).to_uppercase()
),
|b| b.iter(|| black_box($codec::encode(&items).unwrap())),
);
}
};
}

fn uper_rasn_dec(c: &mut Criterion) {
let w = build_sample_rasn();
let encoded = uper::encode(&w).unwrap();
macro_rules! rasn_dec_fn {
($fn_name:ident, $codec:ident) => {
fn $fn_name(c: &mut Criterion) {
let w = build_sample_rasn();
let encoded = $codec::encode(&w).unwrap();

c.bench_function("RASN/decode UPER - sample.asn", |b| {
b.iter(|| {
let _ = uper::decode::<world3d::World>(&encoded).unwrap();
})
});
c.bench_function(
&format!(
"RASN/decode {} - extended integer sample",
stringify!($codec).to_uppercase()
),
|b| b.iter(|| black_box($codec::decode::<world3d::World>(&encoded).unwrap())),
);
}
};
}
fn oer_rasn_dec(c: &mut Criterion) {
let w = build_sample_rasn();
let encoded = oer::encode(&w).unwrap();

c.bench_function("RASN/decode OER - sample.asn", |b| {
b.iter(|| {
let _ = oer::decode::<world3d::World>(&encoded).unwrap();
})
});
}
fn ber_rasn_dec(c: &mut Criterion) {
let w = build_sample_rasn();
let encoded = ber::encode(&w).unwrap();
rasn_enc_fn!(uper_rasn_enc, uper);
rasn_enc_fn!(oer_rasn_enc, oer);
rasn_enc_fn!(ber_rasn_enc, ber);

c.bench_function("RASN/decode BER - sample.asn", |b| {
b.iter(|| {
let _ = ber::decode::<world3d::World>(&encoded).unwrap();
})
});
}
rasn_dec_fn!(uper_rasn_dec, uper);
rasn_dec_fn!(oer_rasn_dec, oer);
rasn_dec_fn!(ber_rasn_dec, ber);

criterion_group!(
benches,
Expand Down

0 comments on commit f37cb69

Please sign in to comment.