blob: 0eb779a5db01d89d6ff5a8014bcaa1a32d26136b [file] [log] [blame]
Christopher Ferrisdf4942c2015-02-17 19:58:53 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <inttypes.h>
18#include <regex.h>
19#include <stdio.h>
20#include <stdint.h>
21#include <stdlib.h>
22#include <time.h>
23
24#include <string>
25#include <vector>
26
27#include <utils/stringprintf.h>
28
29#include <benchmark/Benchmark.h>
30
31#include "utils.h"
32
33namespace testing {
34
35static uint64_t NanoTime() {
36 struct timespec t;
37 t.tv_sec = t.tv_nsec = 0;
38 clock_gettime(CLOCK_MONOTONIC, &t);
39 return static_cast<uint64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec;
40}
41
42std::vector<Benchmark*>& Benchmark::List() {
43 static std::vector<Benchmark*> list;
44 return list;
45}
46
47size_t Benchmark::MaxNameColumnWidth() {
48 size_t max = 20;
49 for (auto& benchmark : List()) {
50 max = std::max(max, benchmark->NameColumnWidth());
51 }
52 return max;
53}
54
55bool Benchmark::RunAll(std::vector<regex_t*>& regs) {
56 bool ran_benchmark = false;
57 for (auto& benchmark : List()) {
58 if (benchmark->ShouldRun(regs)) {
59 if (!ran_benchmark) {
60 printf("%-*s %10s %10s\n", MaxNameColumnWidth(), "", "iterations", "ns/op");
61 ran_benchmark = true;
62 }
63 benchmark->RunAll();
64 }
65 }
66 return ran_benchmark;
67}
68
69bool Benchmark::ShouldRun(std::vector<regex_t*>& regs) {
70 if (regs.empty()) {
71 return true;
72 }
73
74 for (const auto& re : regs) {
75 if (regexec(re, Name().c_str(), 0, NULL, 0) != REG_NOMATCH) {
76 return true;
77 }
78 }
79 return false;
80}
81
82void Benchmark::StopBenchmarkTiming() {
83 if (start_time_ns_ != 0) {
84 total_time_ns_ += NanoTime() - start_time_ns_;
85 }
86 start_time_ns_ = 0;
87}
88
89void Benchmark::StartBenchmarkTiming() {
90 if (start_time_ns_ == 0) {
91 start_time_ns_ = NanoTime();
92 }
93}
94
95std::string BenchmarkWithoutArg::GetNameStr(void*) {
96 return Name();
97}
98
99template <>
100std::string BenchmarkWithArg<int>::GetNameStr(int arg) {
101 return Name() + "/" + PrettyInt(arg, 2);
102}
103
104template <>
105std::string BenchmarkWithArg<double>::GetNameStr(double arg) {
106 return Name() + "/" + android::StringPrintf("%0.6f", arg);
107}
108
109template<typename T>
110void BenchmarkT<T>::RunWithArg(T arg) {
111 int new_iterations = 1;
112 int iterations;
113 while (new_iterations < 1e8) {
114 bytes_processed_ = 0;
115 total_time_ns_ = 0;
116 start_time_ns_ = 0;
117
118 iterations = new_iterations;
119 RunIterations(iterations, arg);
120 if (total_time_ns_ >= 1e9) {
121 break;
122 }
123
124 if (total_time_ns_/iterations == 0) {
125 new_iterations = 1e9;
126 } else {
127 new_iterations = 1e9/ (total_time_ns_/iterations);
128 }
129 new_iterations = std::max(iterations + 1,
130 std::min(new_iterations + new_iterations/2, 100*iterations));
131
132 new_iterations = Round(new_iterations);
133 }
134
135 printf("%-*s %10s %10" PRId64, MaxNameColumnWidth(), GetNameStr(arg).c_str(),
136 PrettyInt(iterations, 10).c_str(), total_time_ns_/iterations);
137
138 if (total_time_ns_ > 0 && bytes_processed_ > 0) {
139 double gib_processed = static_cast<double>(bytes_processed_)/1e9;
140 double seconds = static_cast<double>(total_time_ns_)/1e9;
141 printf(" %8.3f GiB/s", gib_processed/seconds);
142 }
143 printf("\n");
144 fflush(stdout);
145}
146
147template class BenchmarkT<int>;
148template class BenchmarkT<double>;
149template class BenchmarkT<void*>;
150
151template class BenchmarkWithArg<int>;
152template class BenchmarkWithArg<double>;
153
154} // namespace testing