]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - benchmarks/benchmark_main.cpp
am f27cc051: am 806f3bd7: Upgrade to tzdata2013i.
[android-sdk/platform-bionic.git] / benchmarks / benchmark_main.cpp
1 /*
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  */
17 #include "benchmark.h"
19 #include <regex.h>
20 #include <stdio.h>
21 #include <stdlib.h>
23 #include <string>
24 #include <map>
26 #include <inttypes.h>
28 static int64_t gBytesProcessed;
29 static int64_t gBenchmarkTotalTimeNs;
30 static int64_t gBenchmarkStartTimeNs;
32 typedef std::map<std::string, ::testing::Benchmark*> BenchmarkMap;
33 typedef BenchmarkMap::iterator BenchmarkMapIt;
34 static BenchmarkMap gBenchmarks;
36 static int Round(int n) {
37   int base = 1;
38   while (base*10 < n) {
39     base *= 10;
40   }
41   if (n < 2*base) {
42     return 2*base;
43   }
44   if (n < 5*base) {
45     return 5*base;
46   }
47   return 10*base;
48 }
50 static int64_t NanoTime() {
51   struct timespec t;
52   t.tv_sec = t.tv_nsec = 0;
53   clock_gettime(CLOCK_MONOTONIC, &t);
54   return static_cast<int64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec;
55 }
57 namespace testing {
59 Benchmark* Benchmark::Arg(int arg) {
60   args_.push_back(arg);
61   return this;
62 }
64 const char* Benchmark::Name() {
65   return name_;
66 }
68 bool Benchmark::ShouldRun(int argc, char* argv[]) {
69   if (argc == 1) {
70     return true;  // With no arguments, we run all benchmarks.
71   }
72   // Otherwise, we interpret each argument as a regular expression and
73   // see if any of our benchmarks match.
74   for (int i = 1; i < argc; i++) {
75     regex_t re;
76     if (regcomp(&re, argv[i], 0) != 0) {
77       fprintf(stderr, "couldn't compile \"%s\" as a regular expression!\n", argv[i]);
78       exit(EXIT_FAILURE);
79     }
80     int match = regexec(&re, name_, 0, NULL, 0);
81     regfree(&re);
82     if (match != REG_NOMATCH) {
83       return true;
84     }
85   }
86   return false;
87 }
89 void Benchmark::Register(const char* name, void (*fn)(int), void (*fn_range)(int, int)) {
90   name_ = name;
91   fn_ = fn;
92   fn_range_ = fn_range;
94   if (fn_ == NULL && fn_range_ == NULL) {
95     fprintf(stderr, "%s: missing function\n", name_);
96     exit(EXIT_FAILURE);
97   }
99   gBenchmarks.insert(std::make_pair(name, this));
102 void Benchmark::Run() {
103   if (fn_ != NULL) {
104     RunWithArg(0);
105   } else {
106     if (args_.empty()) {
107       fprintf(stderr, "%s: no args!\n", name_);
108       exit(EXIT_FAILURE);
109     }
110     for (size_t i = 0; i < args_.size(); ++i) {
111       RunWithArg(args_[i]);
112     }
113   }
116 void Benchmark::RunRepeatedlyWithArg(int iterations, int arg) {
117   gBytesProcessed = 0;
118   gBenchmarkTotalTimeNs = 0;
119   gBenchmarkStartTimeNs = NanoTime();
120   if (fn_ != NULL) {
121     fn_(iterations);
122   } else {
123     fn_range_(iterations, arg);
124   }
125   if (gBenchmarkStartTimeNs != 0) {
126     gBenchmarkTotalTimeNs += NanoTime() - gBenchmarkStartTimeNs;
127   }
130 void Benchmark::RunWithArg(int arg) {
131   // run once in case it's expensive
132   int iterations = 1;
133   RunRepeatedlyWithArg(iterations, arg);
134   while (gBenchmarkTotalTimeNs < 1e9 && iterations < 1e9) {
135     int last = iterations;
136     if (gBenchmarkTotalTimeNs/iterations == 0) {
137       iterations = 1e9;
138     } else {
139       iterations = 1e9 / (gBenchmarkTotalTimeNs/iterations);
140     }
141     iterations = std::max(last + 1, std::min(iterations + iterations/2, 100*last));
142     iterations = Round(iterations);
143     RunRepeatedlyWithArg(iterations, arg);
144   }
146   char throughput[100];
147   throughput[0] = '\0';
148   if (gBenchmarkTotalTimeNs > 0 && gBytesProcessed > 0) {
149     double mib_processed = static_cast<double>(gBytesProcessed)/1e6;
150     double seconds = static_cast<double>(gBenchmarkTotalTimeNs)/1e9;
151     snprintf(throughput, sizeof(throughput), " %8.2f MiB/s", mib_processed/seconds);
152   }
154   char full_name[100];
155   if (fn_range_ != NULL) {
156     if (arg >= (1<<20)) {
157       snprintf(full_name, sizeof(full_name), "%s/%dM", name_, arg/(1<<20));
158     } else if (arg >= (1<<10)) {
159       snprintf(full_name, sizeof(full_name), "%s/%dK", name_, arg/(1<<10));
160     } else {
161       snprintf(full_name, sizeof(full_name), "%s/%d", name_, arg);
162     }
163   } else {
164     snprintf(full_name, sizeof(full_name), "%s", name_);
165   }
167   printf("%-20s %10d %10" PRId64 "%s\n", full_name,
168          iterations, gBenchmarkTotalTimeNs/iterations, throughput);
169   fflush(stdout);
172 }  // namespace testing
174 void SetBenchmarkBytesProcessed(int64_t x) {
175   gBytesProcessed = x;
178 void StopBenchmarkTiming() {
179   if (gBenchmarkStartTimeNs != 0) {
180     gBenchmarkTotalTimeNs += NanoTime() - gBenchmarkStartTimeNs;
181   }
182   gBenchmarkStartTimeNs = 0;
185 void StartBenchmarkTiming() {
186   if (gBenchmarkStartTimeNs == 0) {
187     gBenchmarkStartTimeNs = NanoTime();
188   }
191 int main(int argc, char* argv[]) {
192   if (gBenchmarks.empty()) {
193     fprintf(stderr, "No benchmarks registered!\n");
194     exit(EXIT_FAILURE);
195   }
197   bool need_header = true;
198   for (BenchmarkMapIt it = gBenchmarks.begin(); it != gBenchmarks.end(); ++it) {
199     ::testing::Benchmark* b = it->second;
200     if (b->ShouldRun(argc, argv)) {
201       if (need_header) {
202         printf("%-20s %10s %10s\n", "", "iterations", "ns/op");
203         fflush(stdout);
204         need_header = false;
205       }
206       b->Run();
207     }
208   }
210   if (need_header) {
211     fprintf(stderr, "No matching benchmarks!\n");
212     fprintf(stderr, "Available benchmarks:\n");
213     for (BenchmarkMapIt it = gBenchmarks.begin(); it != gBenchmarks.end(); ++it) {
214       fprintf(stderr, "  %s\n", it->second->Name());
215     }
216     exit(EXIT_FAILURE);
217   }
219   return 0;