You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.8 KiB

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