• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <algorithm>
2 #include <cassert>
3 #include <cstdint>
4 #include <cstdlib>
5 #include <cstring>
6 #include <iostream>
7 #include <limits>
8 #include <string>
9 #include <vector>
10 
11 #include "benchmark/benchmark.h"
12 
13 // Tests that we can override benchmark-spec value from FLAGS_benchmark_filter
14 // with argument to RunSpecifiedBenchmarks(...).
15 
16 namespace {
17 
18 class TestReporter : public benchmark::ConsoleReporter {
19  public:
ReportContext(const Context & context)20   bool ReportContext(const Context& context) override {
21     return ConsoleReporter::ReportContext(context);
22   };
23 
ReportRuns(const std::vector<Run> & report)24   void ReportRuns(const std::vector<Run>& report) override {
25     assert(report.size() == 1);
26     matched_functions.push_back(report[0].run_name.function_name);
27     ConsoleReporter::ReportRuns(report);
28   };
29 
TestReporter()30   TestReporter() {}
31 
~TestReporter()32   ~TestReporter() override {}
33 
GetMatchedFunctions() const34   const std::vector<std::string>& GetMatchedFunctions() const {
35     return matched_functions;
36   }
37 
38  private:
39   std::vector<std::string> matched_functions;
40 };
41 
42 }  // end namespace
43 
BM_NotChosen(benchmark::State & state)44 static void BM_NotChosen(benchmark::State& state) {
45   assert(false && "SHOULD NOT BE CALLED");
46   for (auto _ : state) {
47   }
48 }
49 BENCHMARK(BM_NotChosen);
50 
BM_Chosen(benchmark::State & state)51 static void BM_Chosen(benchmark::State& state) {
52   for (auto _ : state) {
53   }
54 }
55 BENCHMARK(BM_Chosen);
56 
main(int argc,char ** argv)57 int main(int argc, char** argv) {
58   const std::string flag = "BM_NotChosen";
59 
60   // Verify that argv specify --benchmark_filter=BM_NotChosen.
61   bool found = false;
62   for (int i = 0; i < argc; ++i) {
63     if (strcmp("--benchmark_filter=BM_NotChosen", argv[i]) == 0) {
64       found = true;
65       break;
66     }
67   }
68   assert(found);
69 
70   benchmark::Initialize(&argc, argv);
71 
72   // Check that the current flag value is reported accurately via the
73   // GetBenchmarkFilter() function.
74   if (flag != benchmark::GetBenchmarkFilter()) {
75     std::cerr
76         << "Seeing different value for flags. GetBenchmarkFilter() returns ["
77         << benchmark::GetBenchmarkFilter() << "] expected flag=[" << flag
78         << "]\n";
79     return 1;
80   }
81   TestReporter test_reporter;
82   const char* const spec = "BM_Chosen";
83   const size_t returned_count =
84       benchmark::RunSpecifiedBenchmarks(&test_reporter, spec);
85   assert(returned_count == 1);
86   const std::vector<std::string> matched_functions =
87       test_reporter.GetMatchedFunctions();
88   assert(matched_functions.size() == 1);
89   if (strcmp(spec, matched_functions.front().c_str()) != 0) {
90     std::cerr << "Expected benchmark [" << spec << "] to run, but got ["
91               << matched_functions.front() << "]\n";
92     return 2;
93   }
94 
95   // Test that SetBenchmarkFilter works.
96   const std::string golden_value = "golden_value";
97   benchmark::SetBenchmarkFilter(golden_value);
98   std::string current_value = benchmark::GetBenchmarkFilter();
99   if (golden_value != current_value) {
100     std::cerr << "Expected [" << golden_value
101               << "] for --benchmark_filter but got [" << current_value << "]\n";
102     return 3;
103   }
104   return 0;
105 }
106