|
| 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