2016-06-02 05:08:01 +08:00
|
|
|
#undef NDEBUG
|
|
|
|
#include "benchmark/benchmark.h"
|
2016-08-29 03:24:16 +08:00
|
|
|
#include "output_test.h"
|
2016-06-02 05:08:01 +08:00
|
|
|
#include <cassert>
|
2016-05-19 01:59:34 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
2016-08-29 03:24:16 +08:00
|
|
|
#include <cstdlib>
|
2016-06-04 00:33:17 +08:00
|
|
|
#include <cmath>
|
2016-05-19 01:59:34 +08:00
|
|
|
|
2016-06-02 05:08:01 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
#define ADD_COMPLEXITY_CASES(...) \
|
|
|
|
int CONCAT(dummy, __LINE__) = AddComplexityTest(__VA_ARGS__)
|
|
|
|
|
2016-08-29 03:24:16 +08:00
|
|
|
int AddComplexityTest(std::string big_o_test_name,
|
2016-06-02 05:08:01 +08:00
|
|
|
std::string rms_test_name, std::string big_o) {
|
2016-08-29 03:24:16 +08:00
|
|
|
SetSubstitutions({
|
|
|
|
{"%bigo_name", big_o_test_name},
|
|
|
|
{"%rms_name", rms_test_name},
|
|
|
|
{"%bigo_str", "[ ]*" + std::string(dec_re) + " " + big_o},
|
|
|
|
{"%bigo", big_o},
|
|
|
|
{"%rms", "[ ]*[0-9]+ %"}
|
2016-06-02 05:08:01 +08:00
|
|
|
});
|
2016-08-29 03:24:16 +08:00
|
|
|
AddCases(TC_ConsoleOut, {
|
|
|
|
{"^%bigo_name %bigo_str %bigo_str[ ]*$"},
|
|
|
|
{"^%bigo_name", MR_Not}, // Assert we we didn't only matched a name.
|
|
|
|
{"^%rms_name %rms %rms[ ]*$", MR_Next}
|
|
|
|
});
|
|
|
|
AddCases(TC_JSONOut, {
|
|
|
|
{"\"name\": \"%bigo_name\",$"},
|
2016-06-02 05:08:01 +08:00
|
|
|
{"\"cpu_coefficient\": [0-9]+,$", MR_Next},
|
|
|
|
{"\"real_coefficient\": [0-9]{1,5},$", MR_Next},
|
2016-08-29 03:24:16 +08:00
|
|
|
{"\"big_o\": \"%bigo\",$", MR_Next},
|
2016-06-02 05:08:01 +08:00
|
|
|
{"\"time_unit\": \"ns\"$", MR_Next},
|
|
|
|
{"}", MR_Next},
|
2016-08-29 03:24:16 +08:00
|
|
|
{"\"name\": \"%rms_name\",$"},
|
2016-06-02 05:08:01 +08:00
|
|
|
{"\"rms\": [0-9]+%$", MR_Next},
|
|
|
|
{"}", MR_Next}
|
|
|
|
});
|
2016-08-29 03:24:16 +08:00
|
|
|
AddCases(TC_CSVOut, {
|
|
|
|
{"^\"%bigo_name\",,%float,%float,%bigo,,,,,$"},
|
|
|
|
{"^\"%bigo_name\"", MR_Not},
|
|
|
|
{"^\"%rms_name\",,%float,%float,,,,,,$", MR_Next}
|
2016-06-02 05:08:01 +08:00
|
|
|
});
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace
|
|
|
|
|
|
|
|
// ========================================================================= //
|
|
|
|
// --------------------------- Testing BigO O(1) --------------------------- //
|
|
|
|
// ========================================================================= //
|
|
|
|
|
2016-05-19 01:59:34 +08:00
|
|
|
void BM_Complexity_O1(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
2016-08-08 16:04:59 +08:00
|
|
|
for (int i=0; i < 1024; ++i) {
|
|
|
|
benchmark::DoNotOptimize(&i);
|
|
|
|
}
|
2016-05-19 01:59:34 +08:00
|
|
|
}
|
2016-08-05 03:30:14 +08:00
|
|
|
state.SetComplexityN(state.range(0));
|
2016-05-19 01:59:34 +08:00
|
|
|
}
|
2016-06-03 01:48:53 +08:00
|
|
|
BENCHMARK(BM_Complexity_O1) -> Range(1, 1<<18) -> Complexity(benchmark::o1);
|
|
|
|
BENCHMARK(BM_Complexity_O1) -> Range(1, 1<<18) -> Complexity();
|
2016-07-23 07:31:05 +08:00
|
|
|
BENCHMARK(BM_Complexity_O1) -> Range(1, 1<<18) -> Complexity([](int){return 1.0; });
|
2016-06-02 05:08:01 +08:00
|
|
|
|
2016-06-03 04:40:21 +08:00
|
|
|
const char* big_o_1_test_name = "BM_Complexity_O1_BigO";
|
|
|
|
const char* rms_o_1_test_name = "BM_Complexity_O1_RMS";
|
2016-08-10 04:14:15 +08:00
|
|
|
const char* enum_big_o_1 = "\\([0-9]+\\)";
|
|
|
|
// FIXME: Tolerate both '(1)' and 'lgN' as output when the complexity is auto deduced.
|
|
|
|
// See https://github.com/google/benchmark/issues/272
|
|
|
|
const char* auto_big_o_1 = "(\\([0-9]+\\))|(lgN)";
|
2016-06-03 04:40:21 +08:00
|
|
|
const char* lambda_big_o_1 = "f\\(N\\)";
|
2016-06-02 05:08:01 +08:00
|
|
|
|
|
|
|
// Add enum tests
|
2016-08-29 03:24:16 +08:00
|
|
|
ADD_COMPLEXITY_CASES(big_o_1_test_name, rms_o_1_test_name, enum_big_o_1);
|
2016-06-02 05:08:01 +08:00
|
|
|
|
2016-07-23 07:31:05 +08:00
|
|
|
// Add auto enum tests
|
2016-08-29 03:24:16 +08:00
|
|
|
ADD_COMPLEXITY_CASES(big_o_1_test_name, rms_o_1_test_name, auto_big_o_1);
|
2016-07-23 07:31:05 +08:00
|
|
|
|
2016-06-02 05:08:01 +08:00
|
|
|
// Add lambda tests
|
2016-08-29 03:24:16 +08:00
|
|
|
ADD_COMPLEXITY_CASES(big_o_1_test_name, rms_o_1_test_name, lambda_big_o_1);
|
2016-06-02 05:08:01 +08:00
|
|
|
|
|
|
|
// ========================================================================= //
|
|
|
|
// --------------------------- Testing BigO O(N) --------------------------- //
|
|
|
|
// ========================================================================= //
|
|
|
|
|
|
|
|
std::vector<int> ConstructRandomVector(int size) {
|
|
|
|
std::vector<int> v;
|
|
|
|
v.reserve(size);
|
|
|
|
for (int i = 0; i < size; ++i) {
|
2016-08-29 03:24:16 +08:00
|
|
|
v.push_back(std::rand() % size);
|
2016-06-02 05:08:01 +08:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
2016-05-19 01:59:34 +08:00
|
|
|
|
2016-06-02 05:08:01 +08:00
|
|
|
void BM_Complexity_O_N(benchmark::State& state) {
|
2016-08-05 03:30:14 +08:00
|
|
|
auto v = ConstructRandomVector(state.range(0));
|
|
|
|
const int item_not_in_vector = state.range(0)*2; // Test worst case scenario (item not in vector)
|
2016-05-19 01:59:34 +08:00
|
|
|
while (state.KeepRunning()) {
|
2016-05-24 02:50:35 +08:00
|
|
|
benchmark::DoNotOptimize(std::find(v.begin(), v.end(), item_not_in_vector));
|
2016-05-19 01:59:34 +08:00
|
|
|
}
|
2016-08-05 03:30:14 +08:00
|
|
|
state.SetComplexityN(state.range(0));
|
2016-05-19 01:59:34 +08:00
|
|
|
}
|
2016-06-02 05:08:01 +08:00
|
|
|
BENCHMARK(BM_Complexity_O_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity(benchmark::oN);
|
2016-06-03 04:21:52 +08:00
|
|
|
BENCHMARK(BM_Complexity_O_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity([](int n) -> double{return n; });
|
2016-06-03 01:01:10 +08:00
|
|
|
BENCHMARK(BM_Complexity_O_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity();
|
2016-05-25 04:25:59 +08:00
|
|
|
|
2016-06-03 04:40:21 +08:00
|
|
|
const char* big_o_n_test_name = "BM_Complexity_O_N_BigO";
|
|
|
|
const char* rms_o_n_test_name = "BM_Complexity_O_N_RMS";
|
|
|
|
const char* enum_auto_big_o_n = "N";
|
|
|
|
const char* lambda_big_o_n = "f\\(N\\)";
|
2016-05-25 04:25:59 +08:00
|
|
|
|
2016-06-02 05:08:01 +08:00
|
|
|
// Add enum tests
|
2016-08-29 03:24:16 +08:00
|
|
|
ADD_COMPLEXITY_CASES(big_o_n_test_name, rms_o_n_test_name, enum_auto_big_o_n);
|
2016-06-02 05:08:01 +08:00
|
|
|
|
|
|
|
// Add lambda tests
|
2016-08-29 03:24:16 +08:00
|
|
|
ADD_COMPLEXITY_CASES(big_o_n_test_name, rms_o_n_test_name, lambda_big_o_n);
|
2016-06-02 05:08:01 +08:00
|
|
|
|
|
|
|
// ========================================================================= //
|
|
|
|
// ------------------------- Testing BigO O(N*lgN) ------------------------- //
|
|
|
|
// ========================================================================= //
|
2016-05-19 01:59:34 +08:00
|
|
|
|
|
|
|
static void BM_Complexity_O_N_log_N(benchmark::State& state) {
|
2016-08-05 03:30:14 +08:00
|
|
|
auto v = ConstructRandomVector(state.range(0));
|
2016-05-19 01:59:34 +08:00
|
|
|
while (state.KeepRunning()) {
|
|
|
|
std::sort(v.begin(), v.end());
|
|
|
|
}
|
2016-08-05 03:30:14 +08:00
|
|
|
state.SetComplexityN(state.range(0));
|
2016-05-19 01:59:34 +08:00
|
|
|
}
|
2016-06-02 05:08:01 +08:00
|
|
|
BENCHMARK(BM_Complexity_O_N_log_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity(benchmark::oNLogN);
|
2016-06-04 00:33:17 +08:00
|
|
|
BENCHMARK(BM_Complexity_O_N_log_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity([](int n) {return n * std::log2(n); });
|
2016-06-03 01:01:10 +08:00
|
|
|
BENCHMARK(BM_Complexity_O_N_log_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity();
|
2016-05-19 01:59:34 +08:00
|
|
|
|
2016-06-03 04:40:21 +08:00
|
|
|
const char* big_o_n_lg_n_test_name = "BM_Complexity_O_N_log_N_BigO";
|
|
|
|
const char* rms_o_n_lg_n_test_name = "BM_Complexity_O_N_log_N_RMS";
|
|
|
|
const char* enum_auto_big_o_n_lg_n = "NlgN";
|
|
|
|
const char* lambda_big_o_n_lg_n = "f\\(N\\)";
|
2016-06-02 05:08:01 +08:00
|
|
|
|
|
|
|
// Add enum tests
|
2016-08-29 03:24:16 +08:00
|
|
|
ADD_COMPLEXITY_CASES(big_o_n_lg_n_test_name, rms_o_n_lg_n_test_name, enum_auto_big_o_n_lg_n);
|
2016-06-02 05:08:01 +08:00
|
|
|
|
|
|
|
// Add lambda tests
|
2016-08-29 03:24:16 +08:00
|
|
|
ADD_COMPLEXITY_CASES(big_o_n_lg_n_test_name, rms_o_n_lg_n_test_name, lambda_big_o_n_lg_n);
|
2016-06-02 05:08:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
// ========================================================================= //
|
|
|
|
// --------------------------- TEST CASES END ------------------------------ //
|
|
|
|
// ========================================================================= //
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2016-08-29 03:24:16 +08:00
|
|
|
RunOutputTests(argc, argv);
|
2016-05-19 01:59:34 +08:00
|
|
|
}
|
|
|
|
|