From 47f3758721863568b64331f9572b32f2faba80cd Mon Sep 17 00:00:00 2001
From: Luca Barbato <lu_zero@gentoo.org>
Date: Sun, 26 Aug 2018 14:30:35 +0200
Subject: [PATCH] Move the benchmarks to criterion

---
 Cargo.toml                     |  2 +-
 benches/bench.rs               | 43 ++++-----------
 benches/comparative/mod.rs     | 21 ++------
 benches/comparative/predict.rs | 99 +++++++++++++---------------------
 benches/predict.rs             | 13 ++++-
 5 files changed, 64 insertions(+), 114 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index a6a2aa76..75344f53 100755
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -32,7 +32,7 @@ pkg-config = "0.3.12"
 bindgen = { version = "0.37", optional = true }
 
 [dev-dependencies]
-bencher = "0.1.5"
+criterion = "0.2"
 
 [[bin]]
 name = "rav1e"
diff --git a/benches/bench.rs b/benches/bench.rs
index 62c9af9d..dad2061c 100755
--- a/benches/bench.rs
+++ b/benches/bench.rs
@@ -8,13 +8,13 @@
 // PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 
 #[macro_use]
-extern crate bencher;
+extern crate criterion;
 extern crate rand;
 extern crate rav1e;
 
 mod predict;
 
-use bencher::*;
+use criterion::*;
 use rav1e::*;
 use rav1e::context::*;
 use rav1e::ec;
@@ -24,31 +24,13 @@ use rav1e::predict::*;
 #[cfg(feature = "comparative_bench")]
 mod comparative;
 
-struct WriteB {
-  tx_size: TxSize,
-  qi: usize
-}
-
-impl TDynBenchFn for WriteB {
-  fn run(&self, b: &mut Bencher) {
-    write_b_bench(b, self.tx_size, self.qi);
-  }
-}
-
-pub fn write_b() -> Vec<TestDescAndFn> {
-  use std::borrow::Cow;
-  let mut benches = ::std::vec::Vec::new();
+fn write_b(c: &mut Criterion) {
   for &tx_size in &[TxSize::TX_4X4, TxSize::TX_8X8] {
     for &qi in &[20, 55] {
-      let w = WriteB { tx_size, qi };
       let n = format!("write_b_bench({:?}, {})", tx_size, qi);
-      benches.push(TestDescAndFn {
-        desc: TestDesc { name: Cow::from(n), ignore: false },
-        testfn: TestFn::DynBenchFn(Box::new(w))
-      });
+      c.bench_function(&n, move |b| write_b_bench(b, tx_size, qi));
     }
   }
-  benches
 }
 
 fn write_b_bench(b: &mut Bencher, tx_size: TxSize, qindex: usize) {
@@ -108,20 +90,15 @@ fn write_b_bench(b: &mut Bencher, tx_size: TxSize, qindex: usize) {
   });
 }
 
-benchmark_group!(
+criterion_group!(
   intra_prediction,
-  predict::intra_dc_4x4,
-  predict::intra_h_4x4,
-  predict::intra_v_4x4,
-  predict::intra_paeth_4x4,
-  predict::intra_smooth_4x4,
-  predict::intra_smooth_h_4x4,
-  predict::intra_smooth_v_4x4,
-  predict::intra_cfl_4x4
+  predict::pred_bench,
 );
 
+criterion_group!(write_block, write_b);
+
 #[cfg(feature = "comparative_bench")]
-benchmark_main!(comparative::intra_prediction);
+criterion_main!(comparative::intra_prediction);
 
 #[cfg(not(feature = "comparative_bench"))]
-benchmark_main!(write_b, intra_prediction);
+criterion_main!(write_block, intra_prediction);
diff --git a/benches/comparative/mod.rs b/benches/comparative/mod.rs
index 23d7ebfb..99f06028 100755
--- a/benches/comparative/mod.rs
+++ b/benches/comparative/mod.rs
@@ -11,22 +11,9 @@ extern crate libc;
 
 mod predict;
 
-benchmark_group!(
+use criterion::Criterion;
+
+criterion_group!(
   intra_prediction,
-  predict::intra_dc_4x4_native,
-  predict::intra_dc_4x4_aom,
-  predict::intra_h_4x4_native,
-  predict::intra_h_4x4_aom,
-  predict::intra_v_4x4_native,
-  predict::intra_v_4x4_aom,
-  predict::intra_paeth_4x4_native,
-  predict::intra_paeth_4x4_aom,
-  predict::intra_smooth_4x4_native,
-  predict::intra_smooth_4x4_aom,
-  predict::intra_smooth_h_4x4_native,
-  predict::intra_smooth_h_4x4_aom,
-  predict::intra_smooth_v_4x4_native,
-  predict::intra_smooth_v_4x4_aom,
-  predict::intra_cfl_4x4_native,
-  predict::intra_cfl_4x4_aom
+  predict::intra_bench,
 );
diff --git a/benches/comparative/predict.rs b/benches/comparative/predict.rs
index a07e03a3..585b8197 100755
--- a/benches/comparative/predict.rs
+++ b/benches/comparative/predict.rs
@@ -7,7 +7,7 @@
 // Media Patent License 1.0 was not distributed with this source code in the
 // PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 
-use bencher::*;
+use criterion::*;
 use comparative::libc;
 use predict as predict_native;
 use predict::*;
@@ -56,8 +56,8 @@ extern {
 }
 
 fn predict_intra_4x4_aom(
-  b: &mut Bencher, 
-  predictor: unsafe extern "C" fn(*mut u16, libc::ptrdiff_t, libc::c_int, libc::c_int, *const u16, *const u16, libc::c_int)) 
+  b: &mut Bencher,
+  predictor: unsafe extern "C" fn(*mut u16, libc::ptrdiff_t, libc::c_int, libc::c_int, *const u16, *const u16, libc::c_int))
 {
   let mut rng = ChaChaRng::from_seed([0; 32]);
   let (mut block, above_context, left_context) = generate_block(&mut rng);
@@ -65,71 +65,46 @@ fn predict_intra_4x4_aom(
   b.iter(|| {
     for _ in 0..MAX_ITER {
       unsafe {
-        predictor(block.as_mut_ptr(), BLOCK_SIZE.width() as libc::ptrdiff_t, 
+        predictor(block.as_mut_ptr(), BLOCK_SIZE.width() as libc::ptrdiff_t,
           4, 4, above_context.as_ptr(), left_context.as_ptr(), 8);
       }
     }
   })
 }
 
-pub fn intra_dc_4x4_native(b: &mut Bencher) {
-  predict_native::intra_dc_4x4(b);
-}
-
-pub fn intra_dc_4x4_aom(b: &mut Bencher) {
-  predict_intra_4x4_aom(b, highbd_dc_predictor);
-}
-
-pub fn intra_h_4x4_native(b: &mut Bencher) {
-  predict_native::intra_h_4x4(b);
-}
-
-pub fn intra_h_4x4_aom(b: &mut Bencher) {
-  predict_intra_4x4_aom(b, highbd_h_predictor);
-}
-
-pub fn intra_v_4x4_native(b: &mut Bencher) {
-  predict_native::intra_v_4x4(b);
-}
-
-pub fn intra_v_4x4_aom(b: &mut Bencher) {
-  predict_intra_4x4_aom(b, highbd_v_predictor);
-}
-
-pub fn intra_paeth_4x4_native(b: &mut Bencher) {
-  predict_native::intra_paeth_4x4(b);
-}
-
-pub fn intra_paeth_4x4_aom(b: &mut Bencher) {
-  predict_intra_4x4_aom(b, highbd_paeth_predictor);
-}
-
-pub fn intra_smooth_4x4_native(b: &mut Bencher) {
-  predict_native::intra_smooth_4x4(b);
-}
-
-pub fn intra_smooth_4x4_aom(b: &mut Bencher) {
-  predict_intra_4x4_aom(b, highbd_smooth_predictor);
-}
-
-pub fn intra_smooth_h_4x4_native(b: &mut Bencher) {
-  predict_native::intra_smooth_h_4x4(b);
-}
-
-pub fn intra_smooth_h_4x4_aom(b: &mut Bencher) {
-  predict_intra_4x4_aom(b, highbd_smooth_h_predictor);
-}
-
-pub fn intra_smooth_v_4x4_native(b: &mut Bencher) {
-  predict_native::intra_smooth_v_4x4(b);
-}
-
-pub fn intra_smooth_v_4x4_aom(b: &mut Bencher) {
-  predict_intra_4x4_aom(b, highbd_smooth_v_predictor);
-}
-
-pub fn intra_cfl_4x4_native(b: &mut Bencher) {
-  predict_native::intra_cfl_4x4(b);
+pub fn intra_bench(c: &mut Criterion) {
+    c.bench_functions("intra_dc_4x4", vec![
+        Fun::new("native", |b, _: &Option<usize>| { predict_native::intra_dc_4x4(b) }),
+        Fun::new("aom", |b, _: &Option<usize>| { predict_intra_4x4_aom(b, highbd_dc_predictor) })
+    ], None);
+    c.bench_functions("intra_h_4x4", vec![
+        Fun::new("native", |b, _: &Option<usize>| { predict_native::intra_h_4x4(b) }),
+        Fun::new("aom", |b, _: &Option<usize>| { predict_intra_4x4_aom(b, highbd_h_predictor) })
+    ], None);
+    c.bench_functions("intra_v_4x4", vec![
+        Fun::new("native", |b, _: &Option<usize>| { predict_native::intra_v_4x4(b) }),
+        Fun::new("aom", |b, _: &Option<usize>| { predict_intra_4x4_aom(b, highbd_v_predictor) })
+    ], None);
+    c.bench_functions("intra_paeth_4x4", vec![
+        Fun::new("native", |b, _: &Option<usize>| { predict_native::intra_paeth_4x4(b) }),
+        Fun::new("aom", |b, _: &Option<usize>| { predict_intra_4x4_aom(b, highbd_paeth_predictor) })
+    ], None);
+    c.bench_functions("intra_smooth_4x4", vec![
+        Fun::new("native", |b, _: &Option<usize>| { predict_native::intra_smooth_4x4(b) }),
+        Fun::new("aom", |b, _: &Option<usize>| { predict_intra_4x4_aom(b, highbd_smooth_predictor) })
+    ], None);
+    c.bench_functions("intra_smooth_h_4x4", vec![
+        Fun::new("native", |b, _: &Option<usize>| { predict_native::intra_smooth_h_4x4(b) }),
+        Fun::new("aom", |b, _: &Option<usize>| { predict_intra_4x4_aom(b, highbd_smooth_h_predictor) })
+    ], None);
+    c.bench_functions("intra_smooth_v_4x4", vec![
+        Fun::new("native", |b, _: &Option<usize>| { predict_native::intra_smooth_v_4x4(b) }),
+        Fun::new("aom", |b, _: &Option<usize>| { predict_intra_4x4_aom(b, highbd_smooth_v_predictor) })
+    ], None);
+    c.bench_functions("intra_cfl_4x4", vec![
+        Fun::new("native", |b, _: &Option<usize>| { predict_native::intra_cfl_4x4(b) }),
+        Fun::new("aom", |b, _: &Option<usize>| { intra_cfl_4x4_aom(b) })
+    ], None);
 }
 
 pub fn intra_cfl_4x4_aom(b: &mut Bencher) {
diff --git a/benches/predict.rs b/benches/predict.rs
index 8e030911..ede33611 100755
--- a/benches/predict.rs
+++ b/benches/predict.rs
@@ -7,7 +7,7 @@
 // Media Patent License 1.0 was not distributed with this source code in the
 // PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 
-use bencher::*;
+use criterion::*;
 use rand::{ChaChaRng, Rng, SeedableRng};
 use rav1e::partition::BlockSize;
 use rav1e::predict::{Block4x4, Intra};
@@ -23,6 +23,17 @@ pub fn generate_block(rng: &mut ChaChaRng) -> (Vec<u16>, Vec<u16>, Vec<u16>) {
   (block, above_context, left_context)
 }
 
+pub fn pred_bench(c: &mut Criterion) {
+    c.bench_function("intra_dc_4x4", |b| intra_dc_4x4(b));
+    c.bench_function("intra_h_4x4", |b| intra_h_4x4(b));
+    c.bench_function("intra_v_4x4", |b| intra_v_4x4(b));
+    c.bench_function("intra_paeth_4x4", |b| intra_paeth_4x4(b));
+    c.bench_function("intra_smooth_4x4", |b| intra_smooth_4x4(b));
+    c.bench_function("intra_smooth_h_4x4", |b| intra_smooth_h_4x4(b));
+    c.bench_function("intra_smooth_v_4x4", |b| intra_smooth_v_4x4(b));
+    c.bench_function("intra_cfl_4x4", |b| intra_cfl_4x4(b));
+}
+
 pub fn intra_dc_4x4(b: &mut Bencher) {
   let mut ra = ChaChaRng::from_seed([0; 32]);
   let (mut block, above, left) = generate_block(&mut ra);
-- 
GitLab