Skip to content

Commit

Permalink
Allow AddRange to work with int64_t. (#548)
Browse files Browse the repository at this point in the history
* Allow AddRange to work with int64_t.

Fixes #516

Also, tweak how we manage per-test build needs, and create a standard
_gtest suffix for googletest to differentiate from non-googletest tests.

I also ran clang-format on the files that I changed (but not the
benchmark include or main src as they have too many clang-format
issues).

* Add benchmark_gtest to cmake

* Set(Items|Bytes)Processed now take int64_t
  • Loading branch information
dominichamon authored Apr 3, 2018
1 parent e7eb54b commit 9913418
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 116 deletions.
50 changes: 24 additions & 26 deletions include/benchmark/benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,21 +514,21 @@ class State {
//
// REQUIRES: a benchmark has exited its benchmarking loop.
BENCHMARK_ALWAYS_INLINE
void SetBytesProcessed(size_t bytes) { bytes_processed_ = bytes; }
void SetBytesProcessed(int64_t bytes) { bytes_processed_ = bytes; }

BENCHMARK_ALWAYS_INLINE
size_t bytes_processed() const { return bytes_processed_; }
int64_t bytes_processed() const { return bytes_processed_; }

// If this routine is called with complexity_n > 0 and complexity report is
// requested for the
// family benchmark, then current benchmark will be part of the computation
// and complexity_n will
// represent the length of N.
BENCHMARK_ALWAYS_INLINE
void SetComplexityN(int complexity_n) { complexity_n_ = complexity_n; }
void SetComplexityN(int64_t complexity_n) { complexity_n_ = complexity_n; }

BENCHMARK_ALWAYS_INLINE
int complexity_length_n() { return complexity_n_; }
int64_t complexity_length_n() { return complexity_n_; }

// If this routine is called with items > 0, then an items/s
// label is printed on the benchmark report line for the currently
Expand All @@ -537,10 +537,10 @@ class State {
//
// REQUIRES: a benchmark has exited its benchmarking loop.
BENCHMARK_ALWAYS_INLINE
void SetItemsProcessed(size_t items) { items_processed_ = items; }
void SetItemsProcessed(int64_t items) { items_processed_ = items; }

BENCHMARK_ALWAYS_INLINE
size_t items_processed() const { return items_processed_; }
int64_t items_processed() const { return items_processed_; }

// If this routine is called, the specified label is printed at the
// end of the benchmark report line for the currently executing
Expand All @@ -562,16 +562,16 @@ class State {

// Range arguments for this run. CHECKs if the argument has been set.
BENCHMARK_ALWAYS_INLINE
int range(std::size_t pos = 0) const {
int64_t range(std::size_t pos = 0) const {
assert(range_.size() > pos);
return range_[pos];
}

BENCHMARK_DEPRECATED_MSG("use 'range(0)' instead")
int range_x() const { return range(0); }
int64_t range_x() const { return range(0); }

BENCHMARK_DEPRECATED_MSG("use 'range(1)' instead")
int range_y() const { return range(1); }
int64_t range_y() const { return range(1); }

BENCHMARK_ALWAYS_INLINE
size_t iterations() const {
Expand All @@ -598,12 +598,12 @@ class State {
bool error_occurred_;

private: // items we don't need on the first cache line
std::vector<int> range_;
std::vector<int64_t> range_;

size_t bytes_processed_;
size_t items_processed_;
int64_t bytes_processed_;
int64_t items_processed_;

int complexity_n_;
int64_t complexity_n_;

public:
// Container for user-defined counters.
Expand All @@ -615,7 +615,7 @@ class State {


// TODO(EricWF) make me private
State(size_t max_iters, const std::vector<int>& ranges, int thread_i,
State(size_t max_iters, const std::vector<int64_t>& ranges, int thread_i,
int n_threads, internal::ThreadTimer* timer,
internal::ThreadManager* manager);

Expand Down Expand Up @@ -736,31 +736,31 @@ class Benchmark {
// Run this benchmark once with "x" as the extra argument passed
// to the function.
// REQUIRES: The function passed to the constructor must accept an arg1.
Benchmark* Arg(int x);
Benchmark* Arg(int64_t x);

// Run this benchmark with the given time unit for the generated output report
Benchmark* Unit(TimeUnit unit);

// Run this benchmark once for a number of values picked from the
// range [start..limit]. (start and limit are always picked.)
// REQUIRES: The function passed to the constructor must accept an arg1.
Benchmark* Range(int start, int limit);
Benchmark* Range(int64_t start, int64_t limit);

// Run this benchmark once for all values in the range [start..limit] with
// specific step
// REQUIRES: The function passed to the constructor must accept an arg1.
Benchmark* DenseRange(int start, int limit, int step = 1);
Benchmark* DenseRange(int64_t start, int64_t limit, int step = 1);

// Run this benchmark once with "args" as the extra arguments passed
// to the function.
// REQUIRES: The function passed to the constructor must accept arg1, arg2 ...
Benchmark* Args(const std::vector<int>& args);
Benchmark* Args(const std::vector<int64_t>& args);

// Equivalent to Args({x, y})
// NOTE: This is a legacy C++03 interface provided for compatibility only.
// New code should use 'Args'.
Benchmark* ArgPair(int x, int y) {
std::vector<int> args;
Benchmark* ArgPair(int64_t x, int64_t y) {
std::vector<int64_t> args;
args.push_back(x);
args.push_back(y);
return Args(args);
Expand All @@ -769,7 +769,7 @@ class Benchmark {
// Run this benchmark once for a number of values picked from the
// ranges [start..limit]. (starts and limits are always picked.)
// REQUIRES: The function passed to the constructor must accept arg1, arg2 ...
Benchmark* Ranges(const std::vector<std::pair<int, int> >& ranges);
Benchmark* Ranges(const std::vector<std::pair<int64_t, int64_t> >& ranges);

// Equivalent to ArgNames({name})
Benchmark* ArgName(const std::string& name);
Expand All @@ -781,8 +781,8 @@ class Benchmark {
// Equivalent to Ranges({{lo1, hi1}, {lo2, hi2}}).
// NOTE: This is a legacy C++03 interface provided for compatibility only.
// New code should use 'Ranges'.
Benchmark* RangePair(int lo1, int hi1, int lo2, int hi2) {
std::vector<std::pair<int, int> > ranges;
Benchmark* RangePair(int64_t lo1, int64_t hi1, int64_t lo2, int64_t hi2) {
std::vector<std::pair<int64_t, int64_t> > ranges;
ranges.push_back(std::make_pair(lo1, hi1));
ranges.push_back(std::make_pair(lo2, hi2));
return Ranges(ranges);
Expand Down Expand Up @@ -889,15 +889,13 @@ class Benchmark {

int ArgsCnt() const;

static void AddRange(std::vector<int>* dst, int lo, int hi, int mult);

private:
friend class BenchmarkFamilies;

std::string name_;
ReportMode report_mode_;
std::vector<std::string> arg_names_; // Args for all benchmark runs
std::vector<std::vector<int> > args_; // Args for all benchmark runs
std::vector<std::vector<int64_t> > args_; // Args for all benchmark runs
TimeUnit time_unit_;
int range_multiplier_;
double min_time_;
Expand Down
2 changes: 1 addition & 1 deletion src/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ std::vector<BenchmarkReporter::Run> RunBenchmark(
} // namespace
} // namespace internal

State::State(size_t max_iters, const std::vector<int>& ranges, int thread_i,
State::State(size_t max_iters, const std::vector<int64_t>& ranges, int thread_i,
int n_threads, internal::ThreadTimer* timer,
internal::ThreadManager* manager)
: total_iterations_(0),
Expand Down
2 changes: 1 addition & 1 deletion src/benchmark_api_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct Benchmark::Instance {
std::string name;
Benchmark* benchmark;
ReportMode report_mode;
std::vector<int> arg;
std::vector<int64_t> arg;
TimeUnit time_unit;
int range_multiplier;
bool use_real_time;
Expand Down
57 changes: 17 additions & 40 deletions src/benchmark_register.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "benchmark/benchmark.h"
#include "benchmark_api_internal.h"
#include "internal_macros.h"
#include "benchmark_register.h"

#ifndef BENCHMARK_OS_WINDOWS
#ifndef BENCHMARK_OS_FUCHSIA
Expand All @@ -36,13 +34,16 @@
#include <sstream>
#include <thread>

#include "benchmark/benchmark.h"
#include "benchmark_api_internal.h"
#include "check.h"
#include "commandlineflags.h"
#include "complexity.h"
#include "statistics.h"
#include "internal_macros.h"
#include "log.h"
#include "mutex.h"
#include "re.h"
#include "statistics.h"
#include "string_util.h"
#include "timers.h"

Expand Down Expand Up @@ -175,7 +176,7 @@ bool BenchmarkFamilies::FindBenchmarks(
StrFormat("%s:", family->arg_names_[arg_i].c_str());
}
}

instance.name += StrFormat("%d", arg);
++arg_i;
}
Expand Down Expand Up @@ -246,30 +247,7 @@ Benchmark::Benchmark(const char* name)

Benchmark::~Benchmark() {}

void Benchmark::AddRange(std::vector<int>* dst, int lo, int hi, int mult) {
CHECK_GE(lo, 0);
CHECK_GE(hi, lo);
CHECK_GE(mult, 2);

// Add "lo"
dst->push_back(lo);

static const int kint32max = std::numeric_limits<int32_t>::max();

// Now space out the benchmarks in multiples of "mult"
for (int32_t i = 1; i < kint32max / mult; i *= mult) {
if (i >= hi) break;
if (i > lo) {
dst->push_back(i);
}
}
// Add "hi" (if different from "lo")
if (hi != lo) {
dst->push_back(hi);
}
}

Benchmark* Benchmark::Arg(int x) {
Benchmark* Benchmark::Arg(int64_t x) {
CHECK(ArgsCnt() == -1 || ArgsCnt() == 1);
args_.push_back({x});
return this;
Expand All @@ -280,20 +258,21 @@ Benchmark* Benchmark::Unit(TimeUnit unit) {
return this;
}

Benchmark* Benchmark::Range(int start, int limit) {
Benchmark* Benchmark::Range(int64_t start, int64_t limit) {
CHECK(ArgsCnt() == -1 || ArgsCnt() == 1);
std::vector<int> arglist;
std::vector<int64_t> arglist;
AddRange(&arglist, start, limit, range_multiplier_);

for (int i : arglist) {
for (int64_t i : arglist) {
args_.push_back({i});
}
return this;
}

Benchmark* Benchmark::Ranges(const std::vector<std::pair<int, int>>& ranges) {
Benchmark* Benchmark::Ranges(
const std::vector<std::pair<int64_t, int64_t>>& ranges) {
CHECK(ArgsCnt() == -1 || ArgsCnt() == static_cast<int>(ranges.size()));
std::vector<std::vector<int>> arglists(ranges.size());
std::vector<std::vector<int64_t>> arglists(ranges.size());
std::size_t total = 1;
for (std::size_t i = 0; i < ranges.size(); i++) {
AddRange(&arglists[i], ranges[i].first, ranges[i].second,
Expand All @@ -304,7 +283,7 @@ Benchmark* Benchmark::Ranges(const std::vector<std::pair<int, int>>& ranges) {
std::vector<std::size_t> ctr(arglists.size(), 0);

for (std::size_t i = 0; i < total; i++) {
std::vector<int> tmp;
std::vector<int64_t> tmp;
tmp.reserve(arglists.size());

for (std::size_t j = 0; j < arglists.size(); j++) {
Expand Down Expand Up @@ -336,17 +315,17 @@ Benchmark* Benchmark::ArgNames(const std::vector<std::string>& names) {
return this;
}

Benchmark* Benchmark::DenseRange(int start, int limit, int step) {
Benchmark* Benchmark::DenseRange(int64_t start, int64_t limit, int step) {
CHECK(ArgsCnt() == -1 || ArgsCnt() == 1);
CHECK_GE(start, 0);
CHECK_LE(start, limit);
for (int arg = start; arg <= limit; arg += step) {
for (int64_t arg = start; arg <= limit; arg += step) {
args_.push_back({arg});
}
return this;
}

Benchmark* Benchmark::Args(const std::vector<int>& args) {
Benchmark* Benchmark::Args(const std::vector<int64_t>& args) {
CHECK(ArgsCnt() == -1 || ArgsCnt() == static_cast<int>(args.size()));
args_.push_back(args);
return this;
Expand All @@ -363,15 +342,13 @@ Benchmark* Benchmark::RangeMultiplier(int multiplier) {
return this;
}


Benchmark* Benchmark::MinTime(double t) {
CHECK(t > 0.0);
CHECK(iterations_ == 0);
min_time_ = t;
return this;
}


Benchmark* Benchmark::Iterations(size_t n) {
CHECK(n > 0);
CHECK(IsZero(min_time_));
Expand Down
33 changes: 33 additions & 0 deletions src/benchmark_register.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef BENCHMARK_REGISTER_H
#define BENCHMARK_REGISTER_H

#include <vector>

#include "check.h"

template <typename T>
void AddRange(std::vector<T>* dst, T lo, T hi, int mult) {
CHECK_GE(lo, 0);
CHECK_GE(hi, lo);
CHECK_GE(mult, 2);

// Add "lo"
dst->push_back(lo);

static const T kmax = std::numeric_limits<T>::max();

// Now space out the benchmarks in multiples of "mult"
for (T i = 1; i < kmax / mult; i *= mult) {
if (i >= hi) break;
if (i > lo) {
dst->push_back(i);
}
}

// Add "hi" (if different from "lo")
if (hi != lo) {
dst->push_back(hi);
}
}

#endif // BENCHMARK_REGISTER_H
Loading

0 comments on commit 9913418

Please sign in to comment.