Skip to content

Commit 743967d

Browse files
committed
test: Add image reducer benchmark
1 parent 8d82b83 commit 743967d

File tree

4 files changed

+252
-3
lines changed

4 files changed

+252
-3
lines changed

Cargo.lock

+193-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ default-run = "hyperiond"
99
name = "hyperiond"
1010
path = "src/main.rs"
1111

12+
[[bench]]
13+
name = "reducer"
14+
harness = false
15+
1216
[dependencies]
1317
ambassador = "0.2"
1418
async-trait = "0.1"
@@ -51,3 +55,7 @@ validator = { version = "0.14", features = ["derive"] }
5155

5256
[build-dependencies]
5357
prost-build = "0.7"
58+
59+
[dev-dependencies]
60+
criterion = "0.3"
61+
rand = "0.8"

benches/reducer.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::convert::TryFrom;
2+
3+
use criterion::{criterion_group, criterion_main, Criterion};
4+
use rand::prelude::*;
5+
6+
use hyperion::{
7+
image::*,
8+
models::{ClassicLedConfig, Color16, Leds, ToLeds},
9+
};
10+
11+
fn random_image(width: u32, height: u32) -> RawImage {
12+
let mut data = vec![0u8; (width * height * RawImage::CHANNELS) as usize];
13+
14+
let mut rng = rand::thread_rng();
15+
rng.fill_bytes(&mut data);
16+
17+
RawImage::try_from((data, width, height)).unwrap()
18+
}
19+
20+
fn classic_led_config(leds: u32) -> Leds {
21+
let classic_led_config = ClassicLedConfig {
22+
top: leds / 4,
23+
bottom: leds / 4,
24+
left: leds / 4,
25+
right: leds / 4,
26+
..Default::default()
27+
};
28+
29+
classic_led_config.to_leds()
30+
}
31+
32+
pub fn criterion_benchmark(c: &mut Criterion) {
33+
let width = 1920 / 16;
34+
let height = 1080 / 16;
35+
let leds = classic_led_config(40);
36+
let mut colors = vec![Color16::default(); leds.leds.len()];
37+
38+
c.bench_function(
39+
&format!("{} px {} leds", width * height, leds.leds.len()),
40+
|b| {
41+
let mut reducer = Reducer::default();
42+
let image = random_image(width, height);
43+
44+
b.iter(|| reducer.reduce(&image, &leds.leds, &mut colors))
45+
},
46+
);
47+
}
48+
49+
criterion_group!(benches, criterion_benchmark);
50+
criterion_main!(benches);

0 commit comments

Comments
 (0)