-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUG] Run external profiler (ProfilerManager) same number of iterations
#1913 (#1914) Run the external profiler the same number of iterations as the benchmark was run normally. This makes, for example, a trace collected via ProfilerManager consistent with collected PMU data.
- Loading branch information
Showing
4 changed files
with
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include <cassert> | ||
#include <cstdlib> | ||
#include <memory> | ||
#include <vector> | ||
|
||
#include "benchmark/benchmark.h" | ||
|
||
// Tests that we can specify the number of profiler iterations with | ||
// --benchmark_min_time=<NUM>x. | ||
namespace { | ||
|
||
int iteration_count = 0; | ||
int end_profiler_iteration_count = 0; | ||
|
||
class TestProfilerManager : public benchmark::ProfilerManager { | ||
void AfterSetupStart() override { iteration_count = 0; } | ||
void BeforeTeardownStop() override { | ||
end_profiler_iteration_count = iteration_count; | ||
} | ||
}; | ||
|
||
class NullReporter : public benchmark::BenchmarkReporter { | ||
public: | ||
bool ReportContext(const Context& /*context*/) override { return true; } | ||
void ReportRuns(const std::vector<Run>& /* report */) override {} | ||
}; | ||
|
||
} // end namespace | ||
|
||
static void BM_MyBench(benchmark::State& state) { | ||
for (auto s : state) { | ||
++iteration_count; | ||
} | ||
} | ||
BENCHMARK(BM_MyBench); | ||
|
||
int main(int argc, char** argv) { | ||
// Make a fake argv and append the new --benchmark_profiler_iterations=<foo> | ||
// to it. | ||
int fake_argc = argc + 1; | ||
const char** fake_argv = new const char*[static_cast<size_t>(fake_argc)]; | ||
for (int i = 0; i < argc; ++i) fake_argv[i] = argv[i]; | ||
fake_argv[argc] = "--benchmark_min_time=4x"; | ||
|
||
std::unique_ptr<benchmark::ProfilerManager> pm(new TestProfilerManager()); | ||
benchmark::RegisterProfilerManager(pm.get()); | ||
|
||
benchmark::Initialize(&fake_argc, const_cast<char**>(fake_argv)); | ||
|
||
NullReporter null_reporter; | ||
const size_t returned_count = | ||
benchmark::RunSpecifiedBenchmarks(&null_reporter, "BM_MyBench"); | ||
assert(returned_count == 1); | ||
|
||
// Check the executed iters. | ||
assert(end_profiler_iteration_count == 4); | ||
|
||
benchmark::RegisterProfilerManager(nullptr); | ||
delete[] fake_argv; | ||
return 0; | ||
} |