testing::Benchmark Class Reference

#include <benchmark.h>

Public Member Functions

 Benchmark (const char *name, void(*fn)(int))
 
 Benchmark (const char *name, void(*fn_range)(int, int))
 
BenchmarkArg (int x)
 
BenchmarkRange (int lo, int hi)
 
const charName ()
 
bool ShouldRun (int argc, char *argv[])
 
void Run ()
 

Private Member Functions

void Register (const char *name, void(*fn)(int), void(*fn_range)(int, int))
 
void RunRepeatedlyWithArg (int iterations, int arg)
 
void RunWithArg (int arg)
 

Private Attributes

const charname_
 
void(* fn_ )(int)
 
void(* fn_range_ )(int, int)
 
std::vector< intargs_
 

Constructor & Destructor Documentation

◆ Benchmark() [1/2]

testing::Benchmark::Benchmark ( const char name,
void(*)(int fn 
)
inline
23 { Register(name, fn, NULL); }
void Register(const char *name, void(*fn)(int), void(*fn_range)(int, int))
Definition: benchmark_main.cc:114
string name
Definition: plotDoE.py:33

References plotDoE::name, and Register().

◆ Benchmark() [2/2]

testing::Benchmark::Benchmark ( const char name,
void(*)(int, int fn_range 
)
inline
24 { Register(name, NULL, fn_range); }

References plotDoE::name, and Register().

Member Function Documentation

◆ Arg()

Benchmark * testing::Benchmark::Arg ( int  x)
72  {
73  args_.push_back(arg);
74  return this;
75 }
std::vector< int > args_
Definition: benchmark.h:35

References args_.

◆ Name()

const char * testing::Benchmark::Name ( )
93 { return name_; }
const char * name_
Definition: benchmark.h:32

References name_.

◆ Range()

Benchmark * testing::Benchmark::Range ( int  lo,
int  hi 
)
77  {
78  const int kRangeMultiplier = 8;
79  if (hi < lo) {
80  int temp = hi;
81  hi = lo;
82  lo = temp;
83  }
84  while (lo < hi) {
85  args_.push_back(lo);
86  lo *= kRangeMultiplier;
87  }
88  // We always run the hi number.
89  args_.push_back(hi);
90  return this;
91 }

References args_.

◆ Register()

void testing::Benchmark::Register ( const char name,
void(*)(int fn,
void(*)(int, int fn_range 
)
private
114  {
115  name_ = name;
116  fn_ = fn;
117  fn_range_ = fn_range;
118  if (fn_ == NULL && fn_range_ == NULL) {
119  fprintf(stderr, "%s: missing function\n", name_);
120  exit(EXIT_FAILURE);
121  }
122  gBenchmarks().insert(std::make_pair(name, this));
123 }
BenchmarkMap & gBenchmarks()
Definition: benchmark_main.cc:32
void(* fn_range_)(int, int)
Definition: benchmark.h:34
void(* fn_)(int)
Definition: benchmark.h:33

References fn_, fn_range_, gBenchmarks(), plotDoE::name, and name_.

Referenced by Benchmark().

◆ Run()

void testing::Benchmark::Run ( )
124  {
125  if (fn_ != NULL) {
126  RunWithArg(0);
127  } else {
128  if (args_.empty()) {
129  fprintf(stderr, "%s: no args!\n", name_);
130  exit(EXIT_FAILURE);
131  }
132  for (size_t i = 0; i < args_.size(); ++i) {
133  RunWithArg(args_[i]);
134  }
135  }
136 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
void RunWithArg(int arg)
Definition: benchmark_main.cc:150

References args_, fn_, i, name_, and RunWithArg().

◆ RunRepeatedlyWithArg()

void testing::Benchmark::RunRepeatedlyWithArg ( int  iterations,
int  arg 
)
private
137  {
138  g_flops_processed = 0;
141  if (fn_ != NULL) {
142  fn_(iterations);
143  } else {
144  fn_range_(iterations, arg);
145  }
146  if (g_benchmark_start_time_ns != 0) {
148  }
149 }
static int64_t g_flops_processed
Definition: benchmark_main.cc:26
static int64_t g_benchmark_total_time_ns
Definition: benchmark_main.cc:27
static int64_t g_benchmark_start_time_ns
Definition: benchmark_main.cc:28
static int64_t NanoTime()
Definition: benchmark_main.cc:59

References fn_, fn_range_, g_benchmark_start_time_ns, g_benchmark_total_time_ns, g_flops_processed, and NanoTime().

Referenced by RunWithArg().

◆ RunWithArg()

void testing::Benchmark::RunWithArg ( int  arg)
private
150  {
151  // run once in case it's expensive
152  int iterations = 1;
153  RunRepeatedlyWithArg(iterations, arg);
154  while (g_benchmark_total_time_ns < 1e9 && iterations < 1e9) {
155  int last = iterations;
156  if (g_benchmark_total_time_ns / iterations == 0) {
157  iterations = 1e9;
158  } else {
159  iterations = 1e9 / (g_benchmark_total_time_ns / iterations);
160  }
161  iterations = std::max(last + 1, std::min(iterations + iterations / 2, 100 * last));
162  iterations = Round(iterations);
163  RunRepeatedlyWithArg(iterations, arg);
164  }
165  char throughput[100];
166  throughput[0] = '\0';
168  double mflops_processed = static_cast<double>(g_flops_processed) / 1e6;
169  double seconds = static_cast<double>(g_benchmark_total_time_ns) / 1e9;
170  snprintf(throughput, sizeof(throughput), " %8.2f MFlops/s", mflops_processed / seconds);
171  }
172  char full_name[100];
173  if (fn_range_ != NULL) {
174  if (arg >= (1 << 20)) {
175  snprintf(full_name, sizeof(full_name), "%s/%dM", name_, arg / (1 << 20));
176  } else if (arg >= (1 << 10)) {
177  snprintf(full_name, sizeof(full_name), "%s/%dK", name_, arg / (1 << 10));
178  } else {
179  snprintf(full_name, sizeof(full_name), "%s/%d", name_, arg);
180  }
181  } else {
182  snprintf(full_name, sizeof(full_name), "%s", name_);
183  }
184  printf("%-*s %10d %10" PRId64 "%s\n", g_name_column_width, full_name, iterations,
185  g_benchmark_total_time_ns / iterations, throughput);
186  fflush(stdout);
187 }
static int Round(int n)
Definition: benchmark_main.cc:39
static int g_name_column_width
Definition: benchmark_main.cc:37
void RunRepeatedlyWithArg(int iterations, int arg)
Definition: benchmark_main.cc:137
#define min(a, b)
Definition: datatypes.h:22
#define max(a, b)
Definition: datatypes.h:23
static constexpr const last_t last
Definition: IndexedViewHelper.h:48
stdout
Definition: fix_broken_doxygen_formulae.py:258
double seconds(void)

References fn_range_, g_benchmark_total_time_ns, g_flops_processed, g_name_column_width, Eigen::placeholders::last, max, min, name_, Round(), RunRepeatedlyWithArg(), seconds(), and fix_broken_doxygen_formulae::stdout.

Referenced by Run().

◆ ShouldRun()

bool testing::Benchmark::ShouldRun ( int  argc,
char argv[] 
)
94  {
95  if (argc == 1) {
96  return true; // With no arguments, we run all benchmarks.
97  }
98  // Otherwise, we interpret each argument as a regular expression and
99  // see if any of our benchmarks match.
100  for (int i = 1; i < argc; i++) {
101  regex_t re;
102  if (regcomp(&re, argv[i], 0) != 0) {
103  fprintf(stderr, "couldn't compile \"%s\" as a regular expression!\n", argv[i]);
104  exit(EXIT_FAILURE);
105  }
106  int match = regexec(&re, name_, 0, NULL, 0);
107  regfree(&re);
108  if (match != REG_NOMATCH) {
109  return true;
110  }
111  }
112  return false;
113 }
bool match(const T &xpr, std::string ref, std::string str_xpr="")
Definition: indexed_view.cpp:29

References i, match(), and name_.

Member Data Documentation

◆ args_

std::vector<int> testing::Benchmark::args_
private

Referenced by Arg(), Range(), and Run().

◆ fn_

void(* testing::Benchmark::fn_) (int)
private

Referenced by Register(), Run(), and RunRepeatedlyWithArg().

◆ fn_range_

void(* testing::Benchmark::fn_range_) (int, int)
private

◆ name_

const char* testing::Benchmark::name_
private

The documentation for this class was generated from the following files: