forked from google/benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'remotes/varshneydevansh/benchmark_list_…
…tests-work-with-benchmark-format=json-google#1642'
- Loading branch information
Showing
8 changed files
with
173 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2015 Google Inc. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <gtest/gtest.h> | ||
#include <gmock/gmock.h> | ||
#include <sstream> | ||
#include "benchmark/benchmark.h" | ||
|
||
namespace benchmark { | ||
namespace internal { | ||
|
||
// Helper function to register benchmarks | ||
void RegisterBenchmarks() { | ||
benchmark::RegisterBenchmark("BM_simple", [](benchmark::State& state) { | ||
for (auto _ : state) {} | ||
}); | ||
benchmark::RegisterBenchmark("BM_complex", [](benchmark::State& state) { | ||
for (auto _ : state) { | ||
// Simulating a complex operation | ||
for (int i = 0; i < 1000; ++i) { | ||
benchmark::DoNotOptimize(i * i); | ||
} | ||
} | ||
}); | ||
benchmark::RegisterBenchmark("BM_special_chars", [](benchmark::State& state) { | ||
for (auto _ : state) {} | ||
})->Name("BM_special!@#"); | ||
} | ||
|
||
class ReporterListTest : public ::testing::Test { | ||
protected: | ||
std::stringstream ss; | ||
BenchmarkReporter::Context context; | ||
|
||
void SetUp() override { | ||
benchmark::ClearRegisteredBenchmarks(); | ||
ss.str(""); | ||
ss.clear(); | ||
} | ||
|
||
void RunList(BenchmarkReporter& reporter) { | ||
RegisterBenchmarks(); // Register all benchmarks | ||
benchmark::RunSpecifiedBenchmarks(&reporter); | ||
} | ||
}; | ||
|
||
// Tests the behavior of reporters when no benchmarks are registered | ||
TEST_F(ReporterListTest, HandlesEmptyBenchmarkList) { | ||
benchmark::ConsoleReporter console_reporter(&ss); | ||
benchmark::JSONReporter json_reporter(&ss); | ||
benchmark::CSVReporter csv_reporter(&ss); | ||
|
||
// Expect no output as no benchmarks are registered | ||
RunList(console_reporter); | ||
EXPECT_TRUE(ss.str().empty()); | ||
|
||
ss.str(""); | ||
RunList(json_reporter); | ||
EXPECT_TRUE(ss.str().empty()); | ||
|
||
ss.str(""); | ||
RunList(csv_reporter); | ||
EXPECT_TRUE(ss.str().empty()); | ||
} | ||
|
||
// Tests the output of reporters when benchmarks are listed | ||
TEST_F(ReporterListTest, ListsBenchmarksWithDifferentReporters) { | ||
benchmark::ConsoleReporter console_reporter(&ss); | ||
benchmark::JSONReporter json_reporter(&ss); | ||
benchmark::CSVReporter csv_reporter(&ss); | ||
|
||
RegisterBenchmarks(); // Ensure some benchmarks are registered | ||
|
||
RunList(console_reporter); | ||
EXPECT_THAT(ss.str(), ::testing::HasSubstr("BM_simple")); | ||
EXPECT_THAT(ss.str(), ::testing::HasSubstr("BM_complex")); | ||
EXPECT_THAT(ss.str(), ::testing::HasSubstr("BM_special!@#")); | ||
|
||
ss.str(""); | ||
RunList(json_reporter); | ||
EXPECT_THAT(ss.str(), ::testing::HasSubstr("\"name\": \"BM_simple\"")); | ||
EXPECT_THAT(ss.str(), ::testing::HasSubstr("\"name\": \"BM_complex\"")); | ||
EXPECT_THAT(ss.str(), ::testing::HasSubstr("\"name\": \"BM_special!@#\"")); | ||
|
||
// Check JSON structure | ||
std::string json_output = ss.str(); | ||
EXPECT_THAT(json_output.front(), testing::Eq('[')); | ||
EXPECT_THAT(json_output.back(), testing::Eq(']')) | ||
|
||
ss.str(""); | ||
RunList(csv_reporter); | ||
EXPECT_THAT(ss.str(), ::testing::HasSubstr("BM_simple")); | ||
EXPECT_THAT(ss.str(), ::testing::HasSubstr("BM_complex")); | ||
EXPECT_THAT(ss.str(), ::testing::HasSubstr("BM_special!@#")); | ||
} | ||
|
||
} // namespace internal | ||
} // namespace benchmark |
c08727e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What am I missing, exactly. I did try to look into this but was missing information on the direction which I took.
c08727e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just pulling your edit there into my experimental fork, that's all.