112f440587039ab52adfedf245a6f9d81eb889ec5Eric Fiselier
212f440587039ab52adfedf245a6f9d81eb889ec5Eric Fiselier#include "benchmark/benchmark.h"
312f440587039ab52adfedf245a6f9d81eb889ec5Eric Fiselier
49c25a67c1ab4e49cd813fe6afd28ca0d9e1b0c2dEric Fiselier#include <cassert>
553068f974c0d69849b6437ffe0d545aec2932309Dominic Hamon#include <memory>
69c25a67c1ab4e49cd813fe6afd28ca0d9e1b0c2dEric Fiselier
753068f974c0d69849b6437ffe0d545aec2932309Dominic Hamonclass MyFixture : public ::benchmark::Fixture {
853068f974c0d69849b6437ffe0d545aec2932309Dominic Hamon public:
953aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI  void SetUp(const ::benchmark::State& state) {
1053aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI    if (state.thread_index == 0) {
1153aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI      assert(data.get() == nullptr);
1253aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI      data.reset(new int(42));
1353aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI    }
1453068f974c0d69849b6437ffe0d545aec2932309Dominic Hamon  }
159c25a67c1ab4e49cd813fe6afd28ca0d9e1b0c2dEric Fiselier
1653aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI  void TearDown(const ::benchmark::State& state) {
1753aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI    if (state.thread_index == 0) {
1853aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI      assert(data.get() != nullptr);
1953aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI      data.reset();
2053aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI    }
2153068f974c0d69849b6437ffe0d545aec2932309Dominic Hamon  }
229c25a67c1ab4e49cd813fe6afd28ca0d9e1b0c2dEric Fiselier
231100e9190709a48b0819c84b3cebeaf2bf904d65Dominic Hamon  ~MyFixture() { assert(data == nullptr); }
249c25a67c1ab4e49cd813fe6afd28ca0d9e1b0c2dEric Fiselier
2553068f974c0d69849b6437ffe0d545aec2932309Dominic Hamon  std::unique_ptr<int> data;
2612f440587039ab52adfedf245a6f9d81eb889ec5Eric Fiselier};
2712f440587039ab52adfedf245a6f9d81eb889ec5Eric Fiselier
281100e9190709a48b0819c84b3cebeaf2bf904d65Dominic HamonBENCHMARK_F(MyFixture, Foo)(benchmark::State &st) {
2953068f974c0d69849b6437ffe0d545aec2932309Dominic Hamon  assert(data.get() != nullptr);
3053068f974c0d69849b6437ffe0d545aec2932309Dominic Hamon  assert(*data == 42);
3153068f974c0d69849b6437ffe0d545aec2932309Dominic Hamon  while (st.KeepRunning()) {
3253068f974c0d69849b6437ffe0d545aec2932309Dominic Hamon  }
3312f440587039ab52adfedf245a6f9d81eb889ec5Eric Fiselier}
3412f440587039ab52adfedf245a6f9d81eb889ec5Eric Fiselier
3512f440587039ab52adfedf245a6f9d81eb889ec5Eric FiselierBENCHMARK_DEFINE_F(MyFixture, Bar)(benchmark::State& st) {
3653aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI  if (st.thread_index == 0) {
3753aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI    assert(data.get() != nullptr);
3853aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI    assert(*data == 42);
3953aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI  }
4012f440587039ab52adfedf245a6f9d81eb889ec5Eric Fiselier  while (st.KeepRunning()) {
4153aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI    assert(data.get() != nullptr);
4253aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRI    assert(*data == 42);
4312f440587039ab52adfedf245a6f9d81eb889ec5Eric Fiselier  }
44dfe026075480f117f424d254c2f701ac97ea4cddMarcin Kolny  st.SetItemsProcessed(st.range(0));
4512f440587039ab52adfedf245a6f9d81eb889ec5Eric Fiselier}
4612f440587039ab52adfedf245a6f9d81eb889ec5Eric FiselierBENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42);
4753aca9bc7128b7c0cd513163c5432481f12025eeMohamed Amin JABRIBENCHMARK_REGISTER_F(MyFixture, Bar)->Arg(42)->ThreadPerCpu();
4812f440587039ab52adfedf245a6f9d81eb889ec5Eric Fiselier
4912f440587039ab52adfedf245a6f9d81eb889ec5Eric FiselierBENCHMARK_MAIN()
50