blob: 5ca1d4792e6e8df1fcee78cfc6e3e0a6a5e436fe [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
Christopher Ferris339ac372015-02-20 18:31:06 -080042bool Benchmark::header_printed_;
43
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080044std::vector<Benchmark*>& Benchmark::List() {
45 static std::vector<Benchmark*> list;
46 return list;
47}
48
Greg Hackmann567bfb32015-02-20 11:00:14 -080049int Benchmark::MaxNameColumnWidth() {
Christopher Ferris339ac372015-02-20 18:31:06 -080050 size_t max = 20;
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080051 for (auto& benchmark : List()) {
52 max = std::max(max, benchmark->NameColumnWidth());
53 }
Christopher Ferris339ac372015-02-20 18:31:06 -080054 return static_cast<int>(max);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080055}
56
Christopher Ferris339ac372015-02-20 18:31:06 -080057size_t Benchmark::RunAll(std::vector<regex_t*>& regs) {
58 size_t benchmarks_run = 0;
59 header_printed_ = false;
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080060 for (auto& benchmark : List()) {
Christopher Ferris339ac372015-02-20 18:31:06 -080061 benchmarks_run += benchmark->RunAllArgs(regs);
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080062 }
Christopher Ferris339ac372015-02-20 18:31:06 -080063 return benchmarks_run;
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080064}
65
Christopher Ferris339ac372015-02-20 18:31:06 -080066void Benchmark::PrintHeader() {
67 if (!header_printed_) {
68 printf("%-*s %10s %10s\n", MaxNameColumnWidth(), "", "iterations", "ns/op");
69 header_printed_ = true;
70 }
71}
72
73template <typename T>
74bool BenchmarkT<T>::ShouldRun(std::vector<regex_t*>& regs, T arg) {
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080075 if (regs.empty()) {
76 return true;
77 }
78
79 for (const auto& re : regs) {
Christopher Ferris339ac372015-02-20 18:31:06 -080080 if (regexec(re, GetNameStr(arg).c_str(), 0, NULL, 0) != REG_NOMATCH) {
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080081 return true;
82 }
83 }
84 return false;
85}
86
87void Benchmark::StopBenchmarkTiming() {
88 if (start_time_ns_ != 0) {
89 total_time_ns_ += NanoTime() - start_time_ns_;
90 }
91 start_time_ns_ = 0;
92}
93
94void Benchmark::StartBenchmarkTiming() {
95 if (start_time_ns_ == 0) {
96 start_time_ns_ = NanoTime();
97 }
98}
99
100std::string BenchmarkWithoutArg::GetNameStr(void*) {
101 return Name();
102}
103
104template <>
105std::string BenchmarkWithArg<int>::GetNameStr(int arg) {
106 return Name() + "/" + PrettyInt(arg, 2);
107}
108
109template <>
110std::string BenchmarkWithArg<double>::GetNameStr(double arg) {
111 return Name() + "/" + android::StringPrintf("%0.6f", arg);
112}
113
114template<typename T>
115void BenchmarkT<T>::RunWithArg(T arg) {
116 int new_iterations = 1;
117 int iterations;
118 while (new_iterations < 1e8) {
119 bytes_processed_ = 0;
120 total_time_ns_ = 0;
121 start_time_ns_ = 0;
122
123 iterations = new_iterations;
124 RunIterations(iterations, arg);
125 if (total_time_ns_ >= 1e9) {
126 break;
127 }
128
129 if (total_time_ns_/iterations == 0) {
130 new_iterations = 1e9;
131 } else {
132 new_iterations = 1e9/ (total_time_ns_/iterations);
133 }
134 new_iterations = std::max(iterations + 1,
135 std::min(new_iterations + new_iterations/2, 100*iterations));
136
137 new_iterations = Round(new_iterations);
138 }
139
140 printf("%-*s %10s %10" PRId64, MaxNameColumnWidth(), GetNameStr(arg).c_str(),
141 PrettyInt(iterations, 10).c_str(), total_time_ns_/iterations);
142
143 if (total_time_ns_ > 0 && bytes_processed_ > 0) {
144 double gib_processed = static_cast<double>(bytes_processed_)/1e9;
145 double seconds = static_cast<double>(total_time_ns_)/1e9;
146 printf(" %8.3f GiB/s", gib_processed/seconds);
147 }
148 printf("\n");
149 fflush(stdout);
150}
151
152template class BenchmarkT<int>;
153template class BenchmarkT<double>;
154template class BenchmarkT<void*>;
155
156template class BenchmarkWithArg<int>;
157template class BenchmarkWithArg<double>;
158
159} // namespace testing