Home | History | Annotate | Download | only in internal
      1 /*
      2  *  Created by Phil on 04/07/2017.
      3  *  Copyright 2017 Two Blue Cubes Ltd. All rights reserved.
      4  *
      5  *  Distributed under the Boost Software License, Version 1.0. (See accompanying
      6  *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
      7  */
      8 #ifndef TWOBLUECUBES_CATCH_BENCHMARK_H_INCLUDED
      9 #define TWOBLUECUBES_CATCH_BENCHMARK_H_INCLUDED
     10 
     11 #include "catch_stringref.h"
     12 #include "catch_timer.h"
     13 
     14 #include <cstdint>
     15 #include <string>
     16 
     17 namespace Catch {
     18 
     19     class BenchmarkLooper {
     20 
     21         std::string m_name;
     22         std::size_t m_count = 0;
     23         std::size_t m_iterationsToRun = 1;
     24         uint64_t m_resolution;
     25         Timer m_timer;
     26 
     27         static auto getResolution() -> uint64_t;
     28     public:
     29         // Keep most of this inline as it's on the code path that is being timed
     30         BenchmarkLooper( StringRef name )
     31         :   m_name( name ),
     32             m_resolution( getResolution() )
     33         {
     34             reportStart();
     35             m_timer.start();
     36         }
     37 
     38         explicit operator bool() {
     39             if( m_count < m_iterationsToRun )
     40                 return true;
     41             return needsMoreIterations();
     42         }
     43 
     44         void increment() {
     45             ++m_count;
     46         }
     47 
     48         void reportStart();
     49         auto needsMoreIterations() -> bool;
     50     };
     51 
     52 } // end namespace Catch
     53 
     54 #define BENCHMARK( name ) \
     55     for( Catch::BenchmarkLooper looper( name ); looper; looper.increment() )
     56 
     57 #endif // TWOBLUECUBES_CATCH_BENCHMARK_H_INCLUDED
     58