Skip to content

Commit bef978a

Browse files
authored
Added basic benchmarks (#542)
1 parent 3df4d33 commit bef978a

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jobs:
6565
- VERSION: nightly
6666
FLAGS: "--no-default-features --features std"
6767
- VERSION: nightly
68-
FLAGS: "-Z minimal-versions"
68+
FLAGS: "-Z direct-minimal-versions"
6969

7070
steps:
7171
- uses: actions/[email protected]
@@ -80,12 +80,15 @@ jobs:
8080
with:
8181
path: |
8282
~/.cargo/bin/
83-
~/.cargo/registry/index/
84-
~/.cargo/registry/cache/
8583
~/.cargo/git/db/
8684
target/
8785
key: ${{ runner.os }}-${{ matrix.RUST.VERSION }}-cargo-2-${{ hashFiles('**/Cargo.toml') }}
8886

87+
- run: |
88+
cargo generate-lockfile
89+
cargo update -p half --precise 2.4.1
90+
if: matrix.RUST.VERSION == '1.74.0'
91+
8992
- run: cargo check --workspace --tests ${{ matrix.RUST.FLAGS }}
9093
- run: cargo test --workspace ${{ matrix.RUST.FLAGS }}
9194
if: "${{ !matrix.RUST.SKIP_TESTS }}"
@@ -110,6 +113,10 @@ jobs:
110113
path: target/
111114
key: ${{ runner.os }}-cargo-5-${{ hashFiles('**/Cargo.toml') }}
112115

116+
- run: |
117+
cargo generate-lockfile
118+
cargo update -p half --precise 2.4.1
119+
113120
- run: cargo test
114121
env:
115122
RUSTFLAGS: "-Cinstrument-coverage"

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ itoa = "1.0.11"
2222

2323
[dev-dependencies]
2424
libc = "0.2.11"
25+
# TODO: upgrade to 0.6 when we raise MSRV
26+
criterion = "0.5"
2527

2628
[workspace]
2729
members = ["asn1_derive", "asn1parse"]
30+
31+
[[bench]]
32+
name = "basic"
33+
harness = false

asn1_derive/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ proc-macro = true
1414

1515
[dependencies]
1616
proc-macro2 = "1.0.60"
17-
quote = "1.0"
18-
syn = "2.0"
17+
quote = "1.0.25"
18+
syn = "2.0.8"

benches/basic.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use criterion::{criterion_group, criterion_main, Criterion};
2+
use std::hint::black_box;
3+
4+
#[derive(asn1::Asn1Read, asn1::Asn1Write, Debug, PartialEq, Clone)]
5+
struct BasicStruct<'a> {
6+
integer_field: u64,
7+
bool_field: bool,
8+
octet_string_field: &'a [u8],
9+
}
10+
11+
fn bench_parse_basic_struct(c: &mut Criterion) {
12+
let mut group = c.benchmark_group("parse_basic_struct");
13+
14+
for size in [0, 100, 10000] {
15+
let octet_data = vec![0x42u8; size];
16+
let test_data = asn1::write_single(&BasicStruct {
17+
integer_field: 42,
18+
bool_field: true,
19+
octet_string_field: &octet_data,
20+
})
21+
.unwrap();
22+
23+
group.bench_with_input(format!("size_{size}"), &size, |b, _| {
24+
b.iter(|| {
25+
let result = asn1::parse_single::<BasicStruct>(black_box(&test_data)).unwrap();
26+
black_box(result)
27+
})
28+
});
29+
}
30+
group.finish();
31+
}
32+
33+
fn bench_serialize_basic_struct(c: &mut Criterion) {
34+
let mut group = c.benchmark_group("serialize_basic_struct");
35+
36+
for size in [0, 100, 10000] {
37+
let octet_data = vec![0x42u8; size];
38+
let test_struct = BasicStruct {
39+
integer_field: 42,
40+
bool_field: true,
41+
octet_string_field: &octet_data,
42+
};
43+
44+
group.bench_with_input(format!("size_{size}"), &size, |b, _| {
45+
b.iter(|| {
46+
let result = asn1::write_single(black_box(&test_struct)).unwrap();
47+
black_box(result)
48+
})
49+
});
50+
}
51+
group.finish();
52+
}
53+
criterion_group!(
54+
benches,
55+
bench_parse_basic_struct,
56+
bench_serialize_basic_struct,
57+
);
58+
criterion_main!(benches);

0 commit comments

Comments
 (0)