Unverified Commit 9913418d by Dominic Hamon Committed by GitHub

Allow AddRange to work with int64_t. (#548)

* 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
parent e7eb54b5
...@@ -514,10 +514,10 @@ class State { ...@@ -514,10 +514,10 @@ class State {
// //
// REQUIRES: a benchmark has exited its benchmarking loop. // REQUIRES: a benchmark has exited its benchmarking loop.
BENCHMARK_ALWAYS_INLINE BENCHMARK_ALWAYS_INLINE
void SetBytesProcessed(size_t bytes) { bytes_processed_ = bytes; } void SetBytesProcessed(int64_t bytes) { bytes_processed_ = bytes; }
BENCHMARK_ALWAYS_INLINE 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 // If this routine is called with complexity_n > 0 and complexity report is
// requested for the // requested for the
...@@ -525,10 +525,10 @@ class State { ...@@ -525,10 +525,10 @@ class State {
// and complexity_n will // and complexity_n will
// represent the length of N. // represent the length of N.
BENCHMARK_ALWAYS_INLINE 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 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 // If this routine is called with items > 0, then an items/s
// label is printed on the benchmark report line for the currently // label is printed on the benchmark report line for the currently
...@@ -537,10 +537,10 @@ class State { ...@@ -537,10 +537,10 @@ class State {
// //
// REQUIRES: a benchmark has exited its benchmarking loop. // REQUIRES: a benchmark has exited its benchmarking loop.
BENCHMARK_ALWAYS_INLINE BENCHMARK_ALWAYS_INLINE
void SetItemsProcessed(size_t items) { items_processed_ = items; } void SetItemsProcessed(int64_t items) { items_processed_ = items; }
BENCHMARK_ALWAYS_INLINE 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 // If this routine is called, the specified label is printed at the
// end of the benchmark report line for the currently executing // end of the benchmark report line for the currently executing
...@@ -562,16 +562,16 @@ class State { ...@@ -562,16 +562,16 @@ class State {
// Range arguments for this run. CHECKs if the argument has been set. // Range arguments for this run. CHECKs if the argument has been set.
BENCHMARK_ALWAYS_INLINE BENCHMARK_ALWAYS_INLINE
int range(std::size_t pos = 0) const { int64_t range(std::size_t pos = 0) const {
assert(range_.size() > pos); assert(range_.size() > pos);
return range_[pos]; return range_[pos];
} }
BENCHMARK_DEPRECATED_MSG("use 'range(0)' instead") 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") 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 BENCHMARK_ALWAYS_INLINE
size_t iterations() const { size_t iterations() const {
...@@ -598,12 +598,12 @@ private: ...@@ -598,12 +598,12 @@ private:
bool error_occurred_; bool error_occurred_;
private: // items we don't need on the first cache line private: // items we don't need on the first cache line
std::vector<int> range_; std::vector<int64_t> range_;
size_t bytes_processed_; int64_t bytes_processed_;
size_t items_processed_; int64_t items_processed_;
int complexity_n_; int64_t complexity_n_;
public: public:
// Container for user-defined counters. // Container for user-defined counters.
...@@ -615,7 +615,7 @@ private: // items we don't need on the first cache line ...@@ -615,7 +615,7 @@ private: // items we don't need on the first cache line
// TODO(EricWF) make me private // 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, int n_threads, internal::ThreadTimer* timer,
internal::ThreadManager* manager); internal::ThreadManager* manager);
...@@ -736,7 +736,7 @@ class Benchmark { ...@@ -736,7 +736,7 @@ class Benchmark {
// Run this benchmark once with "x" as the extra argument passed // Run this benchmark once with "x" as the extra argument passed
// to the function. // to the function.
// REQUIRES: The function passed to the constructor must accept an arg1. // 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 // Run this benchmark with the given time unit for the generated output report
Benchmark* Unit(TimeUnit unit); Benchmark* Unit(TimeUnit unit);
...@@ -744,23 +744,23 @@ class Benchmark { ...@@ -744,23 +744,23 @@ class Benchmark {
// Run this benchmark once for a number of values picked from the // Run this benchmark once for a number of values picked from the
// range [start..limit]. (start and limit are always picked.) // range [start..limit]. (start and limit are always picked.)
// REQUIRES: The function passed to the constructor must accept an arg1. // 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 // Run this benchmark once for all values in the range [start..limit] with
// specific step // specific step
// REQUIRES: The function passed to the constructor must accept an arg1. // 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 // Run this benchmark once with "args" as the extra arguments passed
// to the function. // to the function.
// REQUIRES: The function passed to the constructor must accept arg1, arg2 ... // 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}) // Equivalent to Args({x, y})
// NOTE: This is a legacy C++03 interface provided for compatibility only. // NOTE: This is a legacy C++03 interface provided for compatibility only.
// New code should use 'Args'. // New code should use 'Args'.
Benchmark* ArgPair(int x, int y) { Benchmark* ArgPair(int64_t x, int64_t y) {
std::vector<int> args; std::vector<int64_t> args;
args.push_back(x); args.push_back(x);
args.push_back(y); args.push_back(y);
return Args(args); return Args(args);
...@@ -769,7 +769,7 @@ class Benchmark { ...@@ -769,7 +769,7 @@ class Benchmark {
// Run this benchmark once for a number of values picked from the // Run this benchmark once for a number of values picked from the
// ranges [start..limit]. (starts and limits are always picked.) // ranges [start..limit]. (starts and limits are always picked.)
// REQUIRES: The function passed to the constructor must accept arg1, arg2 ... // 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}) // Equivalent to ArgNames({name})
Benchmark* ArgName(const std::string& name); Benchmark* ArgName(const std::string& name);
...@@ -781,8 +781,8 @@ class Benchmark { ...@@ -781,8 +781,8 @@ class Benchmark {
// Equivalent to Ranges({{lo1, hi1}, {lo2, hi2}}). // Equivalent to Ranges({{lo1, hi1}, {lo2, hi2}}).
// NOTE: This is a legacy C++03 interface provided for compatibility only. // NOTE: This is a legacy C++03 interface provided for compatibility only.
// New code should use 'Ranges'. // New code should use 'Ranges'.
Benchmark* RangePair(int lo1, int hi1, int lo2, int hi2) { Benchmark* RangePair(int64_t lo1, int64_t hi1, int64_t lo2, int64_t hi2) {
std::vector<std::pair<int, int> > ranges; std::vector<std::pair<int64_t, int64_t> > ranges;
ranges.push_back(std::make_pair(lo1, hi1)); ranges.push_back(std::make_pair(lo1, hi1));
ranges.push_back(std::make_pair(lo2, hi2)); ranges.push_back(std::make_pair(lo2, hi2));
return Ranges(ranges); return Ranges(ranges);
...@@ -889,15 +889,13 @@ class Benchmark { ...@@ -889,15 +889,13 @@ class Benchmark {
int ArgsCnt() const; int ArgsCnt() const;
static void AddRange(std::vector<int>* dst, int lo, int hi, int mult);
private: private:
friend class BenchmarkFamilies; friend class BenchmarkFamilies;
std::string name_; std::string name_;
ReportMode report_mode_; ReportMode report_mode_;
std::vector<std::string> arg_names_; // Args for all benchmark runs 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_; TimeUnit time_unit_;
int range_multiplier_; int range_multiplier_;
double min_time_; double min_time_;
......
...@@ -290,7 +290,7 @@ std::vector<BenchmarkReporter::Run> RunBenchmark( ...@@ -290,7 +290,7 @@ std::vector<BenchmarkReporter::Run> RunBenchmark(
} // namespace } // namespace
} // namespace internal } // 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, int n_threads, internal::ThreadTimer* timer,
internal::ThreadManager* manager) internal::ThreadManager* manager)
: total_iterations_(0), : total_iterations_(0),
......
...@@ -17,7 +17,7 @@ struct Benchmark::Instance { ...@@ -17,7 +17,7 @@ struct Benchmark::Instance {
std::string name; std::string name;
Benchmark* benchmark; Benchmark* benchmark;
ReportMode report_mode; ReportMode report_mode;
std::vector<int> arg; std::vector<int64_t> arg;
TimeUnit time_unit; TimeUnit time_unit;
int range_multiplier; int range_multiplier;
bool use_real_time; bool use_real_time;
......
...@@ -12,9 +12,7 @@ ...@@ -12,9 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "benchmark/benchmark.h" #include "benchmark_register.h"
#include "benchmark_api_internal.h"
#include "internal_macros.h"
#ifndef BENCHMARK_OS_WINDOWS #ifndef BENCHMARK_OS_WINDOWS
#ifndef BENCHMARK_OS_FUCHSIA #ifndef BENCHMARK_OS_FUCHSIA
...@@ -36,13 +34,16 @@ ...@@ -36,13 +34,16 @@
#include <sstream> #include <sstream>
#include <thread> #include <thread>
#include "benchmark/benchmark.h"
#include "benchmark_api_internal.h"
#include "check.h" #include "check.h"
#include "commandlineflags.h" #include "commandlineflags.h"
#include "complexity.h" #include "complexity.h"
#include "statistics.h" #include "internal_macros.h"
#include "log.h" #include "log.h"
#include "mutex.h" #include "mutex.h"
#include "re.h" #include "re.h"
#include "statistics.h"
#include "string_util.h" #include "string_util.h"
#include "timers.h" #include "timers.h"
...@@ -246,30 +247,7 @@ Benchmark::Benchmark(const char* name) ...@@ -246,30 +247,7 @@ Benchmark::Benchmark(const char* name)
Benchmark::~Benchmark() {} Benchmark::~Benchmark() {}
void Benchmark::AddRange(std::vector<int>* dst, int lo, int hi, int mult) { Benchmark* Benchmark::Arg(int64_t x) {
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) {
CHECK(ArgsCnt() == -1 || ArgsCnt() == 1); CHECK(ArgsCnt() == -1 || ArgsCnt() == 1);
args_.push_back({x}); args_.push_back({x});
return this; return this;
...@@ -280,20 +258,21 @@ Benchmark* Benchmark::Unit(TimeUnit unit) { ...@@ -280,20 +258,21 @@ Benchmark* Benchmark::Unit(TimeUnit unit) {
return this; return this;
} }
Benchmark* Benchmark::Range(int start, int limit) { Benchmark* Benchmark::Range(int64_t start, int64_t limit) {
CHECK(ArgsCnt() == -1 || ArgsCnt() == 1); CHECK(ArgsCnt() == -1 || ArgsCnt() == 1);
std::vector<int> arglist; std::vector<int64_t> arglist;
AddRange(&arglist, start, limit, range_multiplier_); AddRange(&arglist, start, limit, range_multiplier_);
for (int i : arglist) { for (int64_t i : arglist) {
args_.push_back({i}); args_.push_back({i});
} }
return this; 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())); 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; std::size_t total = 1;
for (std::size_t i = 0; i < ranges.size(); i++) { for (std::size_t i = 0; i < ranges.size(); i++) {
AddRange(&arglists[i], ranges[i].first, ranges[i].second, AddRange(&arglists[i], ranges[i].first, ranges[i].second,
...@@ -304,7 +283,7 @@ Benchmark* Benchmark::Ranges(const std::vector<std::pair<int, int>>& ranges) { ...@@ -304,7 +283,7 @@ Benchmark* Benchmark::Ranges(const std::vector<std::pair<int, int>>& ranges) {
std::vector<std::size_t> ctr(arglists.size(), 0); std::vector<std::size_t> ctr(arglists.size(), 0);
for (std::size_t i = 0; i < total; i++) { for (std::size_t i = 0; i < total; i++) {
std::vector<int> tmp; std::vector<int64_t> tmp;
tmp.reserve(arglists.size()); tmp.reserve(arglists.size());
for (std::size_t j = 0; j < arglists.size(); j++) { for (std::size_t j = 0; j < arglists.size(); j++) {
...@@ -336,17 +315,17 @@ Benchmark* Benchmark::ArgNames(const std::vector<std::string>& names) { ...@@ -336,17 +315,17 @@ Benchmark* Benchmark::ArgNames(const std::vector<std::string>& names) {
return this; 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(ArgsCnt() == -1 || ArgsCnt() == 1);
CHECK_GE(start, 0); CHECK_GE(start, 0);
CHECK_LE(start, limit); 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}); args_.push_back({arg});
} }
return this; 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())); CHECK(ArgsCnt() == -1 || ArgsCnt() == static_cast<int>(args.size()));
args_.push_back(args); args_.push_back(args);
return this; return this;
...@@ -363,7 +342,6 @@ Benchmark* Benchmark::RangeMultiplier(int multiplier) { ...@@ -363,7 +342,6 @@ Benchmark* Benchmark::RangeMultiplier(int multiplier) {
return this; return this;
} }
Benchmark* Benchmark::MinTime(double t) { Benchmark* Benchmark::MinTime(double t) {
CHECK(t > 0.0); CHECK(t > 0.0);
CHECK(iterations_ == 0); CHECK(iterations_ == 0);
...@@ -371,7 +349,6 @@ Benchmark* Benchmark::MinTime(double t) { ...@@ -371,7 +349,6 @@ Benchmark* Benchmark::MinTime(double t) {
return this; return this;
} }
Benchmark* Benchmark::Iterations(size_t n) { Benchmark* Benchmark::Iterations(size_t n) {
CHECK(n > 0); CHECK(n > 0);
CHECK(IsZero(min_time_)); CHECK(IsZero(min_time_));
......
#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
NEEDS_GTEST_MAIN = [
"statistics_test.cc",
]
TEST_COPTS = [ TEST_COPTS = [
"-pedantic", "-pedantic",
"-pedantic-errors", "-pedantic-errors",
"-std=c++11", "-std=c++11",
"-Wall",
"-Wextra",
"-Wshadow",
# "-Wshorten-64-to-32",
"-Wfloat-equal",
"-fstrict-aliasing",
] ]
PER_SRC_COPTS = ({
"cxx03_test.cc": ["-std=c++03"],
# Some of the issues with DoNotOptimize only occur when optimization is enabled
"donotoptimize_test.cc": ["-O3"],
})
TEST_ARGS = ["--benchmark_min_time=0.01"] TEST_ARGS = ["--benchmark_min_time=0.01"]
PER_SRC_TEST_ARGS = ({
"user_counters_tabular_test.cc": ["--benchmark_counters_tabular=true"],
})
cc_library( cc_library(
name = "output_test_helper", name = "output_test_helper",
testonly = 1, testonly = 1,
...@@ -22,27 +35,23 @@ cc_library( ...@@ -22,27 +35,23 @@ cc_library(
], ],
) )
[cc_test( [
cc_test(
name = test_src[:-len(".cc")], name = test_src[:-len(".cc")],
size = "small", size = "small",
srcs = [test_src], srcs = [test_src],
args = TEST_ARGS + ({ args = TEST_ARGS + PER_SRC_TEST_ARGS.get(test_src, []),
"user_counters_tabular_test.cc": ["--benchmark_counters_tabular=true"], copts = TEST_COPTS + PER_SRC_COPTS.get(test_src, []),
}).get(test_src, []),
copts = TEST_COPTS + ({
"cxx03_test.cc": ["-std=c++03"],
# Some of the issues with DoNotOptimize only occur when optimization is enabled
"donotoptimize_test.cc": ["-O3"],
}).get(test_src, []),
deps = [ deps = [
":output_test_helper", ":output_test_helper",
"//:benchmark", "//:benchmark",
"//:benchmark_internal_headers", "//:benchmark_internal_headers",
"@com_google_googletest//:gtest", "@com_google_googletest//:gtest",
] + ( ] + (
["@com_google_googletest//:gtest_main"] if (test_src in NEEDS_GTEST_MAIN) else [] ["@com_google_googletest//:gtest_main"] if (test_src[-len("gtest.cc"):] == "gtest.cc") else []
), ),
# FIXME: Add support for assembly tests to bazel. # FIXME: Add support for assembly tests to bazel.
# See Issue #556 # See Issue #556
# https://github.com/google/benchmark/issues/556 # https://github.com/google/benchmark/issues/556
) for test_src in glob(["*_test.cc"], exclude = ["*_assembly_test.cc"])] ) for test_src in glob(["*test.cc"], exclude = ["*_assembly_test.cc"])
]
...@@ -162,7 +162,8 @@ if (BENCHMARK_ENABLE_GTEST_TESTS) ...@@ -162,7 +162,8 @@ if (BENCHMARK_ENABLE_GTEST_TESTS)
add_test(${name} ${name}) add_test(${name} ${name})
endmacro() endmacro()
add_gtest(statistics_test) add_gtest(benchmark_gtest)
add_gtest(statistics_gtest)
endif(BENCHMARK_ENABLE_GTEST_TESTS) endif(BENCHMARK_ENABLE_GTEST_TESTS)
############################################################################### ###############################################################################
......
#include <vector>
#include "../src/benchmark_register.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace {
TEST(AddRangeTest, Simple) {
std::vector<int> dst;
AddRange(&dst, 1, 2, 2);
EXPECT_THAT(dst, testing::ElementsAre(1, 2));
}
TEST(AddRangeTest, Simple64) {
std::vector<int64_t> dst;
AddRange(&dst, static_cast<int64_t>(1), static_cast<int64_t>(2), 2);
EXPECT_THAT(dst, testing::ElementsAre(1, 2));
}
TEST(AddRangeTest, Advanced) {
std::vector<int> dst;
AddRange(&dst, 5, 15, 2);
EXPECT_THAT(dst, testing::ElementsAre(5, 8, 15));
}
TEST(AddRangeTest, Advanced64) {
std::vector<int64_t> dst;
AddRange(&dst, static_cast<int64_t>(5), static_cast<int64_t>(15), 2);
EXPECT_THAT(dst, testing::ElementsAre(5, 8, 15));
}
} // end namespace
...@@ -40,8 +40,8 @@ double CalculatePi(int depth) { ...@@ -40,8 +40,8 @@ double CalculatePi(int depth) {
return (pi - 1.0) * 4; return (pi - 1.0) * 4;
} }
std::set<int> ConstructRandomSet(int size) { std::set<int64_t> ConstructRandomSet(int64_t size) {
std::set<int> s; std::set<int64_t> s;
for (int i = 0; i < size; ++i) s.insert(s.end(), i); for (int i = 0; i < size; ++i) s.insert(s.end(), i);
return s; return s;
} }
...@@ -64,7 +64,7 @@ BENCHMARK(BM_Factorial)->UseRealTime(); ...@@ -64,7 +64,7 @@ BENCHMARK(BM_Factorial)->UseRealTime();
static void BM_CalculatePiRange(benchmark::State& state) { static void BM_CalculatePiRange(benchmark::State& state) {
double pi = 0.0; double pi = 0.0;
for (auto _ : state) pi = CalculatePi(state.range(0)); for (auto _ : state) pi = CalculatePi(static_cast<int>(state.range(0)));
std::stringstream ss; std::stringstream ss;
ss << pi; ss << pi;
state.SetLabel(ss.str()); state.SetLabel(ss.str());
...@@ -74,7 +74,7 @@ BENCHMARK_RANGE(BM_CalculatePiRange, 1, 1024 * 1024); ...@@ -74,7 +74,7 @@ BENCHMARK_RANGE(BM_CalculatePiRange, 1, 1024 * 1024);
static void BM_CalculatePi(benchmark::State& state) { static void BM_CalculatePi(benchmark::State& state) {
static const int depth = 1024; static const int depth = 1024;
for (auto _ : state) { for (auto _ : state) {
benchmark::DoNotOptimize(CalculatePi(depth)); benchmark::DoNotOptimize(CalculatePi(static_cast<int>(depth)));
} }
} }
BENCHMARK(BM_CalculatePi)->Threads(8); BENCHMARK(BM_CalculatePi)->Threads(8);
...@@ -82,7 +82,7 @@ BENCHMARK(BM_CalculatePi)->ThreadRange(1, 32); ...@@ -82,7 +82,7 @@ BENCHMARK(BM_CalculatePi)->ThreadRange(1, 32);
BENCHMARK(BM_CalculatePi)->ThreadPerCpu(); BENCHMARK(BM_CalculatePi)->ThreadPerCpu();
static void BM_SetInsert(benchmark::State& state) { static void BM_SetInsert(benchmark::State& state) {
std::set<int> data; std::set<int64_t> data;
for (auto _ : state) { for (auto _ : state) {
state.PauseTiming(); state.PauseTiming();
data = ConstructRandomSet(state.range(0)); data = ConstructRandomSet(state.range(0));
...@@ -103,9 +103,9 @@ static void BM_Sequential(benchmark::State& state) { ...@@ -103,9 +103,9 @@ static void BM_Sequential(benchmark::State& state) {
ValueType v = 42; ValueType v = 42;
for (auto _ : state) { for (auto _ : state) {
Container c; Container c;
for (int i = state.range(0); --i;) c.push_back(v); for (int64_t i = state.range(0); --i;) c.push_back(v);
} }
const size_t items_processed = state.iterations() * state.range(0); const int64_t items_processed = state.iterations() * state.range(0);
state.SetItemsProcessed(items_processed); state.SetItemsProcessed(items_processed);
state.SetBytesProcessed(items_processed * sizeof(v)); state.SetBytesProcessed(items_processed * sizeof(v));
} }
...@@ -118,8 +118,9 @@ BENCHMARK_TEMPLATE(BM_Sequential, std::vector<int>, int)->Arg(512); ...@@ -118,8 +118,9 @@ BENCHMARK_TEMPLATE(BM_Sequential, std::vector<int>, int)->Arg(512);
#endif #endif
static void BM_StringCompare(benchmark::State& state) { static void BM_StringCompare(benchmark::State& state) {
std::string s1(state.range(0), '-'); size_t len = static_cast<size_t>(state.range(0));
std::string s2(state.range(0), '-'); std::string s1(len, '-');
std::string s2(len, '-');
for (auto _ : state) benchmark::DoNotOptimize(s1.compare(s2)); for (auto _ : state) benchmark::DoNotOptimize(s1.compare(s2));
} }
BENCHMARK(BM_StringCompare)->Range(1, 1 << 20); BENCHMARK(BM_StringCompare)->Range(1, 1 << 20);
...@@ -154,13 +155,13 @@ static void BM_LongTest(benchmark::State& state) { ...@@ -154,13 +155,13 @@ static void BM_LongTest(benchmark::State& state) {
BENCHMARK(BM_LongTest)->Range(1 << 16, 1 << 28); BENCHMARK(BM_LongTest)->Range(1 << 16, 1 << 28);
static void BM_ParallelMemset(benchmark::State& state) { static void BM_ParallelMemset(benchmark::State& state) {
int size = state.range(0) / static_cast<int>(sizeof(int)); int64_t size = state.range(0) / static_cast<int64_t>(sizeof(int));
int thread_size = size / state.threads; int thread_size = static_cast<int>(size) / state.threads;
int from = thread_size * state.thread_index; int from = thread_size * state.thread_index;
int to = from + thread_size; int to = from + thread_size;
if (state.thread_index == 0) { if (state.thread_index == 0) {
test_vector = new std::vector<int>(size); test_vector = new std::vector<int>(static_cast<size_t>(size));
} }
for (auto _ : state) { for (auto _ : state) {
...@@ -178,8 +179,8 @@ static void BM_ParallelMemset(benchmark::State& state) { ...@@ -178,8 +179,8 @@ static void BM_ParallelMemset(benchmark::State& state) {
BENCHMARK(BM_ParallelMemset)->Arg(10 << 20)->ThreadRange(1, 4); BENCHMARK(BM_ParallelMemset)->Arg(10 << 20)->ThreadRange(1, 4);
static void BM_ManualTiming(benchmark::State& state) { static void BM_ManualTiming(benchmark::State& state) {
size_t slept_for = 0; int64_t slept_for = 0;
int microseconds = state.range(0); int64_t microseconds = state.range(0);
std::chrono::duration<double, std::micro> sleep_duration{ std::chrono::duration<double, std::micro> sleep_duration{
static_cast<double>(microseconds)}; static_cast<double>(microseconds)};
......
...@@ -81,9 +81,9 @@ ADD_COMPLEXITY_CASES(big_o_1_test_name, rms_o_1_test_name, lambda_big_o_1); ...@@ -81,9 +81,9 @@ ADD_COMPLEXITY_CASES(big_o_1_test_name, rms_o_1_test_name, lambda_big_o_1);
// --------------------------- Testing BigO O(N) --------------------------- // // --------------------------- Testing BigO O(N) --------------------------- //
// ========================================================================= // // ========================================================================= //
std::vector<int> ConstructRandomVector(int size) { std::vector<int> ConstructRandomVector(int64_t size) {
std::vector<int> v; std::vector<int> v;
v.reserve(size); v.reserve(static_cast<int>(size));
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
v.push_back(std::rand() % size); v.push_back(std::rand() % size);
} }
...@@ -92,8 +92,8 @@ std::vector<int> ConstructRandomVector(int size) { ...@@ -92,8 +92,8 @@ std::vector<int> ConstructRandomVector(int size) {
void BM_Complexity_O_N(benchmark::State& state) { void BM_Complexity_O_N(benchmark::State& state) {
auto v = ConstructRandomVector(state.range(0)); auto v = ConstructRandomVector(state.range(0));
const int item_not_in_vector = // Test worst case scenario (item not in vector)
state.range(0) * 2; // Test worst case scenario (item not in vector) const int64_t item_not_in_vector = state.range(0) * 2;
for (auto _ : state) { for (auto _ : state) {
benchmark::DoNotOptimize(std::find(v.begin(), v.end(), item_not_in_vector)); benchmark::DoNotOptimize(std::find(v.begin(), v.end(), item_not_in_vector));
} }
......
...@@ -8,7 +8,7 @@ namespace { ...@@ -8,7 +8,7 @@ namespace {
std::map<int, int> ConstructRandomMap(int size) { std::map<int, int> ConstructRandomMap(int size) {
std::map<int, int> m; std::map<int, int> m;
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
m.insert(std::make_pair(rand() % size, rand() % size)); m.insert(std::make_pair(std::rand() % size, std::rand() % size));
} }
return m; return m;
} }
...@@ -17,14 +17,14 @@ std::map<int, int> ConstructRandomMap(int size) { ...@@ -17,14 +17,14 @@ std::map<int, int> ConstructRandomMap(int size) {
// Basic version. // Basic version.
static void BM_MapLookup(benchmark::State& state) { static void BM_MapLookup(benchmark::State& state) {
const int size = state.range(0); const int size = static_cast<int>(state.range(0));
std::map<int, int> m; std::map<int, int> m;
for (auto _ : state) { for (auto _ : state) {
state.PauseTiming(); state.PauseTiming();
m = ConstructRandomMap(size); m = ConstructRandomMap(size);
state.ResumeTiming(); state.ResumeTiming();
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
benchmark::DoNotOptimize(m.find(rand() % size)); benchmark::DoNotOptimize(m.find(std::rand() % size));
} }
} }
state.SetItemsProcessed(state.iterations() * size); state.SetItemsProcessed(state.iterations() * size);
...@@ -35,7 +35,7 @@ BENCHMARK(BM_MapLookup)->Range(1 << 3, 1 << 12); ...@@ -35,7 +35,7 @@ BENCHMARK(BM_MapLookup)->Range(1 << 3, 1 << 12);
class MapFixture : public ::benchmark::Fixture { class MapFixture : public ::benchmark::Fixture {
public: public:
void SetUp(const ::benchmark::State& st) { void SetUp(const ::benchmark::State& st) {
m = ConstructRandomMap(st.range(0)); m = ConstructRandomMap(static_cast<int>(st.range(0)));
} }
void TearDown(const ::benchmark::State&) { m.clear(); } void TearDown(const ::benchmark::State&) { m.clear(); }
...@@ -44,10 +44,10 @@ class MapFixture : public ::benchmark::Fixture { ...@@ -44,10 +44,10 @@ class MapFixture : public ::benchmark::Fixture {
}; };
BENCHMARK_DEFINE_F(MapFixture, Lookup)(benchmark::State& state) { BENCHMARK_DEFINE_F(MapFixture, Lookup)(benchmark::State& state) {
const int size = state.range(0); const int size = static_cast<int>(state.range(0));
for (auto _ : state) { for (auto _ : state) {
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
benchmark::DoNotOptimize(m.find(rand() % size)); benchmark::DoNotOptimize(m.find(std::rand() % size));
} }
} }
state.SetItemsProcessed(state.iterations() * size); state.SetItemsProcessed(state.iterations() * size);
......
#include "benchmark/benchmark.h" #include "benchmark/benchmark.h"
#include <cassert> #include <cassert>
#include <iostream>
#include <set> #include <set>
#include <vector>
class MultipleRangesFixture : public ::benchmark::Fixture { class MultipleRangesFixture : public ::benchmark::Fixture {
public: public:
...@@ -27,25 +29,46 @@ class MultipleRangesFixture : public ::benchmark::Fixture { ...@@ -27,25 +29,46 @@ class MultipleRangesFixture : public ::benchmark::Fixture {
{7, 6, 3}}) {} {7, 6, 3}}) {}
void SetUp(const ::benchmark::State& state) { void SetUp(const ::benchmark::State& state) {
std::vector<int> ranges = {state.range(0), state.range(1), state.range(2)}; std::vector<int64_t> ranges = {state.range(0), state.range(1),
state.range(2)};
assert(expectedValues.find(ranges) != expectedValues.end()); assert(expectedValues.find(ranges) != expectedValues.end());
actualValues.insert(ranges); actualValues.insert(ranges);
} }
// NOTE: This is not TearDown as we want to check after _all_ runs are
// complete.
virtual ~MultipleRangesFixture() { virtual ~MultipleRangesFixture() {
assert(actualValues.size() == expectedValues.size()); assert(actualValues.size() == expectedValues.size());
if (actualValues.size() != expectedValues.size()) {
std::cout << "EXPECTED\n";
for (auto v : expectedValues) {
std::cout << "{";
for (int64_t iv : v) {
std::cout << iv << ", ";
}
std::cout << "}\n";
}
std::cout << "ACTUAL\n";
for (auto v : actualValues) {
std::cout << "{";
for (int64_t iv : v) {
std::cout << iv << ", ";
}
std::cout << "}\n";
}
}
} }
std::set<std::vector<int>> expectedValues; std::set<std::vector<int64_t>> expectedValues;
std::set<std::vector<int>> actualValues; std::set<std::vector<int64_t>> actualValues;
}; };
BENCHMARK_DEFINE_F(MultipleRangesFixture, Empty)(benchmark::State& state) { BENCHMARK_DEFINE_F(MultipleRangesFixture, Empty)(benchmark::State& state) {
for (auto _ : state) { for (auto _ : state) {
int product = state.range(0) * state.range(1) * state.range(2); int64_t product = state.range(0) * state.range(1) * state.range(2);
for (int x = 0; x < product; x++) { for (int64_t x = 0; x < product; x++) {
benchmark::DoNotOptimize(x); benchmark::DoNotOptimize(x);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment