Skip to content

Commit

Permalink
adding bench stub
Browse files Browse the repository at this point in the history
  • Loading branch information
maebli committed Nov 15, 2023
1 parent 87b000a commit 3406324
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 42 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
[package]
name = "m-bus-decoder"
name = "m-bus-parser"
version = "0.1.0"
edition = "2021"


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[dev-dependencies]
criterion = "0.3"
13 changes: 13 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use m_bus_parser::parse_frame;

fn frame_parse_benchmark(c: &mut Criterion) {
let data = [0x68, 0x04, 0x04, 0x68, 0x53, 0x01, 0x00, 0x00, 0x54, 0x16];
c.bench_function("parse_frame", |b| b.iter(|| {
// Use black_box to prevent compiler optimizations from skipping the computation
parse_frame(black_box(&data)).unwrap();
}));
}

criterion_group!(benches, frame_parse_benchmark);
criterion_main!(benches);
8 changes: 7 additions & 1 deletion examples/example.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
use m_bus_parser::parse_frame;

fn main() {}
fn main() {
let data = [0x68, 0x04, 0x04, 0x68, 0x53, 0x01, 0x00, 0x00, 0x54, 0x16];
let frame = parse_frame(&data).unwrap();
println!("{:?}", frame);

}
10 changes: 0 additions & 10 deletions src/frames/control_frame.rs

This file was deleted.

4 changes: 0 additions & 4 deletions src/frames/extended_link_layer_frame.rs

This file was deleted.

14 changes: 0 additions & 14 deletions src/frames/long_frame.rs

This file was deleted.

5 changes: 4 additions & 1 deletion src/frames/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ pub trait Frame {
}

pub fn parse(data: &[u8]) {

match parse_frame(data) {
Ok(frame) => println!("Frame: {:?}", frame),
Err(e) => println!("Error: {:?}", e),
}
}

pub fn parse_frame(data: &[u8]) -> Result<FrameType, FrameError> {
Expand Down
7 changes: 0 additions & 7 deletions src/frames/short_frame.rs

This file was deleted.

4 changes: 0 additions & 4 deletions src/frames/single_character_frame.rs

This file was deleted.

4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
mod frames;

pub fn parse_frame(data: &[u8]) -> Result<frames::FrameType, frames::FrameError> {
frames::parse_frame(data)
}

0 comments on commit 3406324

Please sign in to comment.