Skip to content

Commit 6d9e5ac

Browse files
authored
Merge deae1d9 into eb8cbec
2 parents eb8cbec + deae1d9 commit 6d9e5ac

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

include/benchmark/benchmark.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ struct Statistics {
430430
};
431431

432432
namespace internal {
433+
struct BenchmarkInstance;
433434
class ThreadTimer;
434435
class ThreadManager;
435436

@@ -664,20 +665,20 @@ class State {
664665
// Number of threads concurrently executing the benchmark.
665666
const int threads;
666667

667-
// TODO(EricWF) make me private
668+
private:
668669
State(size_t max_iters, const std::vector<int64_t>& ranges, int thread_i,
669670
int n_threads, internal::ThreadTimer* timer,
670671
internal::ThreadManager* manager);
671672

672-
private:
673673
void StartKeepRunning();
674674
// Implementation of KeepRunning() and KeepRunningBatch().
675675
// is_batch must be true unless n is 1.
676676
bool KeepRunningInternal(size_t n, bool is_batch);
677677
void FinishKeepRunning();
678678
internal::ThreadTimer* timer_;
679679
internal::ThreadManager* manager_;
680-
BENCHMARK_DISALLOW_COPY_AND_ASSIGN(State);
680+
681+
friend struct internal::BenchmarkInstance;
681682
};
682683

683684
inline BENCHMARK_ALWAYS_INLINE bool State::KeepRunning() {
@@ -931,9 +932,6 @@ class Benchmark {
931932

932933
virtual void Run(State& state) = 0;
933934

934-
// Used inside the benchmark implementation
935-
struct Instance;
936-
937935
protected:
938936
explicit Benchmark(const char* name);
939937
Benchmark(Benchmark const&);

src/benchmark.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void UseCharPointer(char const volatile*) {}
126126
namespace {
127127

128128
BenchmarkReporter::Run CreateRunReport(
129-
const benchmark::internal::Benchmark::Instance& b,
129+
const benchmark::internal::BenchmarkInstance& b,
130130
const internal::ThreadManager::Result& results, size_t memory_iterations,
131131
const MemoryManager::Result& memory_result, double seconds) {
132132
// Create report about this benchmark run.
@@ -169,12 +169,10 @@ BenchmarkReporter::Run CreateRunReport(
169169

170170
// Execute one thread of benchmark b for the specified number of iterations.
171171
// Adds the stats collected for the thread into *total.
172-
void RunInThread(const benchmark::internal::Benchmark::Instance* b,
173-
size_t iters, int thread_id,
174-
internal::ThreadManager* manager) {
172+
void RunInThread(const BenchmarkInstance* b, size_t iters, int thread_id,
173+
ThreadManager* manager) {
175174
internal::ThreadTimer timer;
176-
State st(iters, b->arg, thread_id, b->threads, &timer, manager);
177-
b->benchmark->Run(st);
175+
State st = b->Run(iters, thread_id, &timer, manager);
178176
CHECK(st.iterations() >= st.max_iterations)
179177
<< "Benchmark returned before State::KeepRunning() returned false!";
180178
{
@@ -199,7 +197,7 @@ struct RunResults {
199197
};
200198

201199
RunResults RunBenchmark(
202-
const benchmark::internal::Benchmark::Instance& b,
200+
const benchmark::internal::BenchmarkInstance& b,
203201
std::vector<BenchmarkReporter::Run>* complexity_reports) {
204202
RunResults run_results;
205203

@@ -437,7 +435,7 @@ void State::FinishKeepRunning() {
437435
namespace internal {
438436
namespace {
439437

440-
void RunBenchmarks(const std::vector<Benchmark::Instance>& benchmarks,
438+
void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
441439
BenchmarkReporter* display_reporter,
442440
BenchmarkReporter* file_reporter) {
443441
// Note the file_reporter can be null.
@@ -447,7 +445,7 @@ void RunBenchmarks(const std::vector<Benchmark::Instance>& benchmarks,
447445
bool has_repetitions = FLAGS_benchmark_repetitions > 1;
448446
size_t name_field_width = 10;
449447
size_t stat_field_width = 0;
450-
for (const Benchmark::Instance& benchmark : benchmarks) {
448+
for (const BenchmarkInstance& benchmark : benchmarks) {
451449
name_field_width =
452450
std::max<size_t>(name_field_width, benchmark.name.size());
453451
has_repetitions |= benchmark.repetitions > 1;
@@ -595,7 +593,7 @@ size_t RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter,
595593
file_reporter->SetErrorStream(&output_file);
596594
}
597595

598-
std::vector<internal::Benchmark::Instance> benchmarks;
596+
std::vector<internal::BenchmarkInstance> benchmarks;
599597
if (!FindBenchmarksInternal(spec, &benchmarks, &Err)) return 0;
600598

601599
if (benchmarks.empty()) {

src/benchmark_api_internal.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "benchmark_api_internal.h"
2+
3+
namespace benchmark {
4+
namespace internal {
5+
6+
State BenchmarkInstance::Run(
7+
size_t iters, int thread_id, internal::ThreadTimer* timer,
8+
internal::ThreadManager* manager) const {
9+
State st(iters, arg, thread_id, threads, timer, manager);
10+
benchmark->Run(st);
11+
return st;
12+
}
13+
14+
} // internal
15+
} // benchmark

src/benchmark_api_internal.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
#include <cmath>
77
#include <iosfwd>
88
#include <limits>
9+
#include <memory>
910
#include <string>
1011
#include <vector>
1112

1213
namespace benchmark {
1314
namespace internal {
1415

1516
// Information kept per benchmark we may want to run
16-
struct Benchmark::Instance {
17+
struct BenchmarkInstance {
1718
std::string name;
1819
Benchmark* benchmark;
1920
AggregationReportMode aggregation_report_mode;
@@ -31,10 +32,13 @@ struct Benchmark::Instance {
3132
double min_time;
3233
size_t iterations;
3334
int threads; // Number of concurrent threads to us
35+
36+
State Run(size_t iters, int thread_id, internal::ThreadTimer* timer,
37+
internal::ThreadManager* manager) const;
3438
};
3539

3640
bool FindBenchmarksInternal(const std::string& re,
37-
std::vector<Benchmark::Instance>* benchmarks,
41+
std::vector<BenchmarkInstance>* benchmarks,
3842
std::ostream* Err);
3943

4044
bool IsZero(double n);

src/benchmark_register.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class BenchmarkFamilies {
7878
// Extract the list of benchmark instances that match the specified
7979
// regular expression.
8080
bool FindBenchmarks(std::string re,
81-
std::vector<Benchmark::Instance>* benchmarks,
81+
std::vector<BenchmarkInstance>* benchmarks,
8282
std::ostream* Err);
8383

8484
private:
@@ -107,7 +107,7 @@ void BenchmarkFamilies::ClearBenchmarks() {
107107
}
108108

109109
bool BenchmarkFamilies::FindBenchmarks(
110-
std::string spec, std::vector<Benchmark::Instance>* benchmarks,
110+
std::string spec, std::vector<BenchmarkInstance>* benchmarks,
111111
std::ostream* ErrStream) {
112112
CHECK(ErrStream);
113113
auto& Err = *ErrStream;
@@ -152,7 +152,7 @@ bool BenchmarkFamilies::FindBenchmarks(
152152

153153
for (auto const& args : family->args_) {
154154
for (int num_threads : *thread_counts) {
155-
Benchmark::Instance instance;
155+
BenchmarkInstance instance;
156156
instance.name = family->name_;
157157
instance.benchmark = family.get();
158158
instance.aggregation_report_mode = family->aggregation_report_mode_;
@@ -225,7 +225,7 @@ Benchmark* RegisterBenchmarkInternal(Benchmark* bench) {
225225
// FIXME: This function is a hack so that benchmark.cc can access
226226
// `BenchmarkFamilies`
227227
bool FindBenchmarksInternal(const std::string& re,
228-
std::vector<Benchmark::Instance>* benchmarks,
228+
std::vector<BenchmarkInstance>* benchmarks,
229229
std::ostream* Err) {
230230
return BenchmarkFamilies::GetInstance()->FindBenchmarks(re, benchmarks, Err);
231231
}

0 commit comments

Comments
 (0)