Welcome to mirror list, hosted at ThFree Co, Russian Federation.

benchmark.hpp « testing - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dea74077149a202c90bc68a9f7e7147f1c4430f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#pragma once
#include "testing/testing.hpp"
#include "testing/testregister.hpp"
#include "base/timer.hpp"
#include "std/iostream.hpp"

namespace my
{
  class BenchmarkNTimes
  {
  public:
    BenchmarkNTimes(int repeatCount, double maxSecondsToSucceed)
      : m_RepeatCount(repeatCount), m_MaxSecondsToSucceed(maxSecondsToSucceed), m_Iteration(0)
    {
    }

    ~BenchmarkNTimes()
    {
      double const secondsElapsed = m_Timer.ElapsedSeconds();
      TEST_GREATER(m_RepeatCount, 0, ());
      TEST_LESS_OR_EQUAL(secondsElapsed, m_MaxSecondsToSucceed, (m_RepeatCount));
      cout << secondsElapsed << "s total";
      if (secondsElapsed > 0)
      {
        cout << ", " << static_cast<int>(m_RepeatCount / secondsElapsed) << "/s, ";
        /*
        if (secondsElapsed / m_RepeatCount * 1000 >= 10)
          cout << static_cast<int>(secondsElapsed / m_RepeatCount * 1000) << "ms each";
        else */ if (secondsElapsed / m_RepeatCount * 1000000 >= 10)
          cout << static_cast<int>(secondsElapsed / m_RepeatCount * 1000000) << "us each";
        else
          cout << static_cast<int>(secondsElapsed / m_RepeatCount * 1000000000) << "ns each";
      }
      cout << " ...";
    }

    inline int Iteration() const { return m_Iteration; }
    inline bool ContinueIterating() const { return m_Iteration < m_RepeatCount; }
    inline void NextIteration() { ++m_Iteration; }

  private:
    int const m_RepeatCount;
    double const m_MaxSecondsToSucceed;
    int m_Iteration;
    Timer m_Timer;
  };
}

#define BENCHMARK_TEST(name) \
    void Benchmark_##name();	\
        TestRegister g_BenchmarkRegister_##name("Benchmark::"#name, __FILE__, &Benchmark_##name); \
    void Benchmark_##name()

#define BENCHMARK_N_TIMES(times, maxTimeToSucceed) \
    for (::my::BenchmarkNTimes benchmark(times, maxTimeToSucceed); \
         benchmark.ContinueIterating(); benchmark.NextIteration())