|
| 1 | +#include <vector> |
| 2 | + |
| 3 | +#include "../src/benchmark_runner.h" |
| 4 | +#include "benchmark/benchmark.h" |
| 5 | +#include "gtest/gtest.h" |
| 6 | + |
| 7 | +#define N_REPETITIONS 100 |
| 8 | +#define N_ITERATIONS 1 |
| 9 | + |
| 10 | +using benchmark::ClearRegisteredBenchmarks; |
| 11 | +using benchmark::ConsoleReporter; |
| 12 | +using benchmark::MemoryManager; |
| 13 | +using benchmark::RegisterBenchmark; |
| 14 | +using benchmark::RunSpecifiedBenchmarks; |
| 15 | +using benchmark::State; |
| 16 | +using benchmark::internal::Benchmark; |
| 17 | + |
| 18 | +namespace counts { |
| 19 | +int num_allocs = 0; |
| 20 | +int max_bytes_used = 0; |
| 21 | +int total_allocated_bytes = 0; |
| 22 | +int net_heap_growth = 0; |
| 23 | +void reset() { |
| 24 | + num_allocs = 0; |
| 25 | + max_bytes_used = 0; |
| 26 | + total_allocated_bytes = 0; |
| 27 | + net_heap_growth = 0; |
| 28 | +} |
| 29 | +} // namespace counts |
| 30 | + |
| 31 | +class TestMemoryManager : public MemoryManager { |
| 32 | + void Start() override {} |
| 33 | + void Stop(Result& result) override { |
| 34 | + result.num_allocs = counts::num_allocs; |
| 35 | + result.net_heap_growth = counts::net_heap_growth; |
| 36 | + result.max_bytes_used = counts::max_bytes_used; |
| 37 | + result.total_allocated_bytes = counts::total_allocated_bytes; |
| 38 | + |
| 39 | + counts::num_allocs += 1; |
| 40 | + counts::max_bytes_used += 2; |
| 41 | + counts::net_heap_growth += 4; |
| 42 | + counts::total_allocated_bytes += 10; |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +class TestReporter : public ConsoleReporter { |
| 47 | + public: |
| 48 | + TestReporter() = default; |
| 49 | + virtual ~TestReporter() = default; |
| 50 | + |
| 51 | + bool ReportContext(const Context& /*unused*/) override { return true; } |
| 52 | + |
| 53 | + void PrintHeader(const Run&) override {} |
| 54 | + void PrintRunData(const Run& run) override { |
| 55 | + if (run.repetition_index == -1) return; |
| 56 | + if (!run.memory_result.is_valid) return; |
| 57 | + |
| 58 | + store.push_back(run.memory_result); |
| 59 | + } |
| 60 | + |
| 61 | + std::vector<MemoryManager::Result> store; |
| 62 | +}; |
| 63 | + |
| 64 | +class MemoryResultsTest : public testing::Test { |
| 65 | + public: |
| 66 | + Benchmark* bm; |
| 67 | + TestReporter reporter; |
| 68 | + |
| 69 | + void SetUp() override { |
| 70 | + bm = RegisterBenchmark("BM", [](State& st) { |
| 71 | + for (auto _ : st) { |
| 72 | + } |
| 73 | + }); |
| 74 | + bm->Repetitions(N_REPETITIONS); |
| 75 | + bm->Iterations(N_ITERATIONS); |
| 76 | + } |
| 77 | + void TearDown() override { ClearRegisteredBenchmarks(); } |
| 78 | +}; |
| 79 | + |
| 80 | +TEST_F(MemoryResultsTest, NoMMTest) { |
| 81 | + RunSpecifiedBenchmarks(&reporter); |
| 82 | + EXPECT_EQ(reporter.store.size(), 0); |
| 83 | +} |
| 84 | + |
| 85 | +TEST_F(MemoryResultsTest, ResultsTest) { |
| 86 | + counts::reset(); |
| 87 | + MemoryManager* mm = new TestMemoryManager; |
| 88 | + RegisterMemoryManager(mm); |
| 89 | + EXPECT_EQ(benchmark::internal::memory_manager, mm); |
| 90 | + |
| 91 | + RunSpecifiedBenchmarks(&reporter); |
| 92 | + EXPECT_EQ(reporter.store.size(), N_REPETITIONS); |
| 93 | + |
| 94 | + for (size_t i = 0; i < reporter.store.size(); i++) { |
| 95 | + EXPECT_EQ(reporter.store[i].num_allocs, i); |
| 96 | + EXPECT_EQ(reporter.store[i].max_bytes_used, i * 2); |
| 97 | + EXPECT_EQ(reporter.store[i].net_heap_growth, i * 4); |
| 98 | + EXPECT_EQ(reporter.store[i].total_allocated_bytes, i * 10); |
| 99 | + } |
| 100 | + |
| 101 | + delete mm; |
| 102 | +} |
0 commit comments