Skip to content

Commit b0b5359

Browse files
committed
Add benchmarks and CI tests
1 parent 0e2f280 commit b0b5359

File tree

4 files changed

+131
-2
lines changed

4 files changed

+131
-2
lines changed

.github/workflows/ci.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Test nightly
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
tags:
8+
- v*
9+
pull_request:
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v2
18+
19+
- name: Install toolchain
20+
uses: actions-rs/toolchain@v1
21+
with:
22+
profile: minimal
23+
toolchain: nightly
24+
override: true
25+
26+
- name: Install rustfmt
27+
run: rustup component add rustfmt
28+
29+
- name: Check formatting
30+
uses: actions-rs/cargo@v1
31+
with:
32+
command: fmt
33+
args: --all --verbose -- --check
34+
35+
- name: Run tests
36+
uses: actions-rs/cargo@v1
37+
with:
38+
command: test
39+
args: --verbose
40+
41+
- name: Docs
42+
uses: actions-rs/cargo@v1
43+
with:
44+
command: doc
45+
args: --no-deps

Cargo.toml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "msgpacker"
3-
version = "0.1.1"
3+
version = "0.1.3"
44
authors = ["Victor Lopez <victor@codx.io>"]
55
categories = ["compression", "encoding", "parser-implementations"]
66
edition = "2021"
@@ -11,3 +11,15 @@ repository = "https://github.com/codx-dev/msgpacker"
1111
description = "MessagePack protocol implementation for Rust."
1212

1313
[dependencies]
14+
15+
[dev-dependencies]
16+
criterion = "0.3"
17+
18+
[profile.bench]
19+
lto = true
20+
incremental = false
21+
codegen-units = 1
22+
23+
[[bench]]
24+
name = "pack"
25+
harness = false

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ message.pack(&mut cursor).expect("Message pack failed");
7575

7676
cursor.rewind().expect("Reset the cursor to the beginning");
7777

78-
// The consumer need to guarantee himself the cursor source will live long enough to satify the
78+
// The consumer need to guarantee himself the cursor source will live long enough to satisfy the
7979
// lifetime of the message reference.
8080
//
8181
// If this is guaranteed, then the function is safe.
@@ -109,3 +109,20 @@ let value = restored["some-key"]
109109

110110
assert_eq!(value, -15);
111111
```
112+
113+
## Benchmarks
114+
115+
Results obtained with `Intel(R) Core(TM) i9-9900X CPU @ 3.50GHz`
116+
117+
```ignore,no_run
118+
$ cargo bench
119+
msgpack nil time: [3.3648 ns 3.3783 ns 3.3928 ns]
120+
msgunpack nil time: [25.925 ns 26.008 ns 26.097 ns]
121+
msgunpack ref nil time: [22.632 ns 22.709 ns 22.789 ns]
122+
msgpack int time: [5.9986 ns 6.0216 ns 6.0525 ns]
123+
msgunpack int time: [25.481 ns 25.579 ns 25.680 ns]
124+
msgunpack ref int time: [22.635 ns 22.727 ns 22.830 ns]
125+
msgpack map time: [1.1588 us 1.1626 us 1.1667 us]
126+
msgunpack map time: [25.955 ns 26.045 ns 26.141 ns]
127+
msgunpack ref map time: [22.626 ns 22.716 ns 22.810 ns]
128+
```

benches/pack.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2+
3+
use msgpacker::prelude::*;
4+
5+
pub fn pack(c: &mut Criterion) {
6+
let message_nil = Message::Nil;
7+
let message_int = Message::from(i64::MIN);
8+
9+
let m = (0..100)
10+
.map(|i| MapEntry::new("some-key".into(), i.into()))
11+
.collect::<Vec<MapEntry>>();
12+
13+
let message_map = Message::map(m);
14+
15+
let mut buffer = vec![0u8; 4096];
16+
17+
c.bench_function("msgpack nil", |b| {
18+
b.iter(|| message_nil.pack(black_box(&mut buffer)).unwrap())
19+
});
20+
21+
c.bench_function("msgunpack nil", |b| {
22+
b.iter(|| Message::unpack(black_box(&mut buffer.as_slice())).unwrap())
23+
});
24+
25+
c.bench_function("msgunpack ref nil", |b| {
26+
b.iter(|| unsafe { MessageRef::unpack(black_box(&mut buffer.as_slice())).unwrap() })
27+
});
28+
29+
c.bench_function("msgpack int", |b| {
30+
b.iter(|| message_int.pack(black_box(&mut buffer)).unwrap())
31+
});
32+
33+
c.bench_function("msgunpack int", |b| {
34+
b.iter(|| Message::unpack(black_box(&mut buffer.as_slice())).unwrap())
35+
});
36+
37+
c.bench_function("msgunpack ref int", |b| {
38+
b.iter(|| unsafe { MessageRef::unpack(black_box(&mut buffer.as_slice())).unwrap() })
39+
});
40+
41+
c.bench_function("msgpack map", |b| {
42+
b.iter(|| message_map.pack(black_box(&mut buffer)).unwrap())
43+
});
44+
45+
c.bench_function("msgunpack map", |b| {
46+
b.iter(|| Message::unpack(black_box(&mut buffer.as_slice())).unwrap())
47+
});
48+
49+
c.bench_function("msgunpack ref map", |b| {
50+
b.iter(|| unsafe { MessageRef::unpack(black_box(&mut buffer.as_slice())).unwrap() })
51+
});
52+
}
53+
54+
criterion_group!(benches, pack);
55+
criterion_main!(benches);

0 commit comments

Comments
 (0)