1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/*
* Copyright (C) 2012-2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#ifndef BIONIC_BENCHMARK_H_
#define BIONIC_BENCHMARK_H_
namespace testing {
class Benchmark;
template <typename T> class BenchmarkWantsArg;
template <typename T> class BenchmarkWithArg;
void BenchmarkRegister(Benchmark* bm);
int PrettyPrintInt(char* str, int len, unsigned int arg);
class Benchmark {
public:
Benchmark(const char* name, void (*fn)(int)) : name_(strdup(name)), fn_(fn) {
BenchmarkRegister(this);
}
explicit Benchmark(const char* name) : name_(strdup(name)), fn_(NULL) {}
virtual ~Benchmark() {
free(name_);
}
const char* Name() { return name_; }
virtual const char* ArgName() { return NULL; }
virtual void RunFn(int iterations) { fn_(iterations); }
protected:
char* name_;
private:
void (*fn_)(int);
};
template <typename T>
class BenchmarkWantsArgBase : public Benchmark {
public:
BenchmarkWantsArgBase(const char* name, void (*fn)(int, T)) : Benchmark(name) {
fn_arg_ = fn;
}
BenchmarkWantsArgBase<T>* Arg(const char* arg_name, T arg) {
BenchmarkRegister(new BenchmarkWithArg<T>(name_, fn_arg_, arg_name, arg));
return this;
}
protected:
virtual void RunFn(int) { printf("can't run arg benchmark %s without arg\n", Name()); }
void (*fn_arg_)(int, T);
};
template <typename T>
class BenchmarkWithArg : public BenchmarkWantsArg<T> {
public:
BenchmarkWithArg(const char* name, void (*fn)(int, T), const char* arg_name, T arg) :
BenchmarkWantsArg<T>(name, fn), arg_(arg) {
arg_name_ = strdup(arg_name);
}
virtual ~BenchmarkWithArg() {
free(arg_name_);
}
virtual const char* ArgName() { return arg_name_; }
protected:
virtual void RunFn(int iterations) { BenchmarkWantsArg<T>::fn_arg_(iterations, arg_); }
private:
T arg_;
char* arg_name_;
};
template <typename T>
class BenchmarkWantsArg : public BenchmarkWantsArgBase<T> {
public:
BenchmarkWantsArg<T>(const char* name, void (*fn)(int, T)) :
BenchmarkWantsArgBase<T>(name, fn) { }
};
template <>
class BenchmarkWantsArg<int> : public BenchmarkWantsArgBase<int> {
public:
BenchmarkWantsArg<int>(const char* name, void (*fn)(int, int)) :
BenchmarkWantsArgBase<int>(name, fn) { }
BenchmarkWantsArg<int>* Arg(int arg) {
char arg_name[100];
PrettyPrintInt(arg_name, sizeof(arg_name), arg);
BenchmarkRegister(new BenchmarkWithArg<int>(name_, fn_arg_, arg_name, arg));
return this;
}
};
static inline Benchmark* BenchmarkFactory(const char* name, void (*fn)(int)) {
return new Benchmark(name, fn);
}
template <typename T>
static inline BenchmarkWantsArg<T>* BenchmarkFactory(const char* name, void (*fn)(int, T)) {
return new BenchmarkWantsArg<T>(name, fn);
}
} // namespace testing
template <typename T>
static inline void BenchmarkAddArg(::testing::Benchmark* b, const char* name, T arg) {
::testing::BenchmarkWantsArg<T>* ba;
ba = static_cast< ::testing::BenchmarkWantsArg<T>* >(b);
ba->Arg(name, arg);
}
void SetBenchmarkBytesProcessed(uint64_t);
void ResetBenchmarkTiming(void);
void StopBenchmarkTiming(void);
void StartBenchmarkTiming(void);
void StartBenchmarkTiming(uint64_t);
void StopBenchmarkTiming(uint64_t);
#define BENCHMARK(f) \
static ::testing::Benchmark* _benchmark_##f __attribute__((unused)) = /* NOLINT */ \
(::testing::Benchmark*)::testing::BenchmarkFactory(#f, f) /* NOLINT */
#endif // BIONIC_BENCHMARK_H_
|