diff options
author | Colin Cross <ccross@android.com> | 2013-11-15 17:49:47 -0800 |
---|---|---|
committer | Colin Cross <ccross@android.com> | 2013-12-10 18:24:30 -0800 |
commit | bd3efbc9b585e0c9801b0b35f282347cb8692ca8 (patch) | |
tree | ee91fc952e907515d959f80a55de157318ad124a /benchmarks | |
parent | 05ec00bf62ac168c9787a3d0640879ab3e502fe5 (diff) | |
download | bionic-bd3efbc9b585e0c9801b0b35f282347cb8692ca8.zip bionic-bd3efbc9b585e0c9801b0b35f282347cb8692ca8.tar.gz bionic-bd3efbc9b585e0c9801b0b35f282347cb8692ca8.tar.bz2 |
bionic: move benchmarks out of tests directory
Change-Id: I4d054965198af22c9a9c821d1bc53f4e9ea01248
Diffstat (limited to 'benchmarks')
-rw-r--r-- | benchmarks/Android.mk | 49 | ||||
-rw-r--r-- | benchmarks/benchmark.h | 61 | ||||
-rw-r--r-- | benchmarks/benchmark_main.cpp | 218 | ||||
-rw-r--r-- | benchmarks/math_benchmark.cpp | 62 | ||||
-rw-r--r-- | benchmarks/property_benchmark.cpp | 148 | ||||
-rw-r--r-- | benchmarks/string_benchmark.cpp | 112 | ||||
-rw-r--r-- | benchmarks/time_benchmark.cpp | 38 |
7 files changed, 688 insertions, 0 deletions
diff --git a/benchmarks/Android.mk b/benchmarks/Android.mk new file mode 100644 index 0000000..83e490f --- /dev/null +++ b/benchmarks/Android.mk @@ -0,0 +1,49 @@ +# +# Copyright (C) 2013 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. +# + +ifneq ($(BUILD_TINY_ANDROID), true) + +LOCAL_PATH := $(call my-dir) + +# ----------------------------------------------------------------------------- +# Benchmarks. +# ----------------------------------------------------------------------------- + +benchmark_c_flags = \ + -O2 \ + -Wall -Wextra \ + -Werror \ + -fno-builtin \ + +benchmark_src_files = \ + benchmark_main.cpp \ + math_benchmark.cpp \ + property_benchmark.cpp \ + string_benchmark.cpp \ + time_benchmark.cpp \ + +# Build benchmarks for the device (with bionic's .so). Run with: +# adb shell bionic-benchmarks +include $(CLEAR_VARS) +LOCAL_MODULE := bionic-benchmarks +LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk +LOCAL_CFLAGS += $(benchmark_c_flags) +LOCAL_C_INCLUDES += external/stlport/stlport bionic/ bionic/libstdc++/include +LOCAL_SHARED_LIBRARIES += libstlport +LOCAL_SRC_FILES := $(benchmark_src_files) +include $(BUILD_EXECUTABLE) + +endif # !BUILD_TINY_ANDROID diff --git a/benchmarks/benchmark.h b/benchmarks/benchmark.h new file mode 100644 index 0000000..d7af50f --- /dev/null +++ b/benchmarks/benchmark.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2012 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 <vector> + +namespace testing { + +class Benchmark { + public: + Benchmark(const char* name, void (*fn)(int)) { + Register(name, fn, NULL); + } + + Benchmark(const char* name, void (*fn_range)(int, int)) { + Register(name, NULL, fn_range); + } + + Benchmark* Arg(int x); + + const char* Name(); + + bool ShouldRun(int argc, char* argv[]); + void Run(); + + private: + const char* name_; + + void (*fn_)(int); + void (*fn_range_)(int, int); + + std::vector<int> args_; + + void Register(const char* name, void (*fn)(int), void (*fn_range)(int, int)); + void RunRepeatedlyWithArg(int iterations, int arg); + void RunWithArg(int arg); +}; + +} // namespace testing + +void SetBenchmarkBytesProcessed(int64_t); +void StopBenchmarkTiming(); +void StartBenchmarkTiming(); + +#define BENCHMARK(f) \ + static ::testing::Benchmark* _benchmark_##f __attribute__((unused)) = \ + (new ::testing::Benchmark(#f, f)) diff --git a/benchmarks/benchmark_main.cpp b/benchmarks/benchmark_main.cpp new file mode 100644 index 0000000..e15a688 --- /dev/null +++ b/benchmarks/benchmark_main.cpp @@ -0,0 +1,218 @@ +/* + * Copyright (C) 2012 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 "benchmark.h" + +#include <regex.h> +#include <stdio.h> +#include <stdlib.h> + +#include <string> +#include <map> + +static int64_t gBytesProcessed; +static int64_t gBenchmarkTotalTimeNs; +static int64_t gBenchmarkStartTimeNs; + +typedef std::map<std::string, ::testing::Benchmark*> BenchmarkMap; +typedef BenchmarkMap::iterator BenchmarkMapIt; +static BenchmarkMap gBenchmarks; + +static int Round(int n) { + int base = 1; + while (base*10 < n) { + base *= 10; + } + if (n < 2*base) { + return 2*base; + } + if (n < 5*base) { + return 5*base; + } + return 10*base; +} + +static int64_t NanoTime() { + struct timespec t; + t.tv_sec = t.tv_nsec = 0; + clock_gettime(CLOCK_MONOTONIC, &t); + return static_cast<int64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec; +} + +namespace testing { + +Benchmark* Benchmark::Arg(int arg) { + args_.push_back(arg); + return this; +} + +const char* Benchmark::Name() { + return name_; +} + +bool Benchmark::ShouldRun(int argc, char* argv[]) { + if (argc == 1) { + return true; // With no arguments, we run all benchmarks. + } + // Otherwise, we interpret each argument as a regular expression and + // see if any of our benchmarks match. + for (int i = 1; i < argc; i++) { + regex_t re; + if (regcomp(&re, argv[i], 0) != 0) { + fprintf(stderr, "couldn't compile \"%s\" as a regular expression!\n", argv[i]); + exit(EXIT_FAILURE); + } + int match = regexec(&re, name_, 0, NULL, 0); + regfree(&re); + if (match != REG_NOMATCH) { + return true; + } + } + return false; +} + +void Benchmark::Register(const char* name, void (*fn)(int), void (*fn_range)(int, int)) { + name_ = name; + fn_ = fn; + fn_range_ = fn_range; + + if (fn_ == NULL && fn_range_ == NULL) { + fprintf(stderr, "%s: missing function\n", name_); + exit(EXIT_FAILURE); + } + + gBenchmarks.insert(std::make_pair(name, this)); +} + +void Benchmark::Run() { + if (fn_ != NULL) { + RunWithArg(0); + } else { + if (args_.empty()) { + fprintf(stderr, "%s: no args!\n", name_); + exit(EXIT_FAILURE); + } + for (size_t i = 0; i < args_.size(); ++i) { + RunWithArg(args_[i]); + } + } +} + +void Benchmark::RunRepeatedlyWithArg(int iterations, int arg) { + gBytesProcessed = 0; + gBenchmarkTotalTimeNs = 0; + gBenchmarkStartTimeNs = NanoTime(); + if (fn_ != NULL) { + fn_(iterations); + } else { + fn_range_(iterations, arg); + } + if (gBenchmarkStartTimeNs != 0) { + gBenchmarkTotalTimeNs += NanoTime() - gBenchmarkStartTimeNs; + } +} + +void Benchmark::RunWithArg(int arg) { + // run once in case it's expensive + int iterations = 1; + RunRepeatedlyWithArg(iterations, arg); + while (gBenchmarkTotalTimeNs < 1e9 && iterations < 1e9) { + int last = iterations; + if (gBenchmarkTotalTimeNs/iterations == 0) { + iterations = 1e9; + } else { + iterations = 1e9 / (gBenchmarkTotalTimeNs/iterations); + } + iterations = std::max(last + 1, std::min(iterations + iterations/2, 100*last)); + iterations = Round(iterations); + RunRepeatedlyWithArg(iterations, arg); + } + + char throughput[100]; + throughput[0] = '\0'; + if (gBenchmarkTotalTimeNs > 0 && gBytesProcessed > 0) { + double mib_processed = static_cast<double>(gBytesProcessed)/1e6; + double seconds = static_cast<double>(gBenchmarkTotalTimeNs)/1e9; + snprintf(throughput, sizeof(throughput), " %8.2f MiB/s", mib_processed/seconds); + } + + char full_name[100]; + if (fn_range_ != NULL) { + if (arg >= (1<<20)) { + snprintf(full_name, sizeof(full_name), "%s/%dM", name_, arg/(1<<20)); + } else if (arg >= (1<<10)) { + snprintf(full_name, sizeof(full_name), "%s/%dK", name_, arg/(1<<10)); + } else { + snprintf(full_name, sizeof(full_name), "%s/%d", name_, arg); + } + } else { + snprintf(full_name, sizeof(full_name), "%s", name_); + } + + printf("%-20s %10lld %10lld%s\n", full_name, + static_cast<int64_t>(iterations), gBenchmarkTotalTimeNs/iterations, throughput); + fflush(stdout); +} + +} // namespace testing + +void SetBenchmarkBytesProcessed(int64_t x) { + gBytesProcessed = x; +} + +void StopBenchmarkTiming() { + if (gBenchmarkStartTimeNs != 0) { + gBenchmarkTotalTimeNs += NanoTime() - gBenchmarkStartTimeNs; + } + gBenchmarkStartTimeNs = 0; +} + +void StartBenchmarkTiming() { + if (gBenchmarkStartTimeNs == 0) { + gBenchmarkStartTimeNs = NanoTime(); + } +} + +int main(int argc, char* argv[]) { + if (gBenchmarks.empty()) { + fprintf(stderr, "No benchmarks registered!\n"); + exit(EXIT_FAILURE); + } + + bool need_header = true; + for (BenchmarkMapIt it = gBenchmarks.begin(); it != gBenchmarks.end(); ++it) { + ::testing::Benchmark* b = it->second; + if (b->ShouldRun(argc, argv)) { + if (need_header) { + printf("%-20s %10s %10s\n", "", "iterations", "ns/op"); + fflush(stdout); + need_header = false; + } + b->Run(); + } + } + + if (need_header) { + fprintf(stderr, "No matching benchmarks!\n"); + fprintf(stderr, "Available benchmarks:\n"); + for (BenchmarkMapIt it = gBenchmarks.begin(); it != gBenchmarks.end(); ++it) { + fprintf(stderr, " %s\n", it->second->Name()); + } + exit(EXIT_FAILURE); + } + + return 0; +} diff --git a/benchmarks/math_benchmark.cpp b/benchmarks/math_benchmark.cpp new file mode 100644 index 0000000..a8c1cfa --- /dev/null +++ b/benchmarks/math_benchmark.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2013 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 "benchmark.h" + +#include <math.h> + +// Avoid optimization. +double d; +double v; + +static void BM_math_sqrt(int iters) { + StartBenchmarkTiming(); + + d = 0.0; + v = 2.0; + for (int i = 0; i < iters; ++i) { + d += sqrt(v); + } + + StopBenchmarkTiming(); +} +BENCHMARK(BM_math_sqrt); + +static void BM_math_log10(int iters) { + StartBenchmarkTiming(); + + d = 0.0; + v = 1234.0; + for (int i = 0; i < iters; ++i) { + d += log10(v); + } + + StopBenchmarkTiming(); +} +BENCHMARK(BM_math_log10); + +static void BM_math_logb(int iters) { + StartBenchmarkTiming(); + + d = 0.0; + v = 1234.0; + for (int i = 0; i < iters; ++i) { + d += logb(v); + } + + StopBenchmarkTiming(); +} +BENCHMARK(BM_math_logb); diff --git a/benchmarks/property_benchmark.cpp b/benchmarks/property_benchmark.cpp new file mode 100644 index 0000000..4311a1d --- /dev/null +++ b/benchmarks/property_benchmark.cpp @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2012 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 "benchmark.h" +#include <unistd.h> + +#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ +#include <sys/_system_properties.h> + +#include <vector> +#include <string> + +extern void *__system_property_area__; + +#define TEST_NUM_PROPS \ + Arg(1)->Arg(4)->Arg(16)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024) + +struct LocalPropertyTestState { + LocalPropertyTestState(int nprops) : nprops(nprops), valid(false) { + static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_"; + + char dir_template[] = "/data/local/tmp/prop-XXXXXX"; + char *dirname = mkdtemp(dir_template); + if (!dirname) { + perror("making temp file for test state failed (is /data/local/tmp writable?)"); + return; + } + + old_pa = __system_property_area__; + __system_property_area__ = NULL; + + pa_dirname = dirname; + pa_filename = pa_dirname + "/__properties__"; + + __system_property_set_filename(pa_filename.c_str()); + __system_property_area_init(); + + names = new char* [nprops]; + name_lens = new int[nprops]; + values = new char* [nprops]; + value_lens = new int[nprops]; + + srandom(nprops); + + for (int i = 0; i < nprops; i++) { + name_lens[i] = random() % PROP_NAME_MAX; + names[i] = new char[PROP_NAME_MAX + 1]; + for (int j = 0; j < name_lens[i]; j++) { + names[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)]; + } + names[i][name_lens[i]] = 0; + value_lens[i] = random() % PROP_VALUE_MAX; + values[i] = new char[PROP_VALUE_MAX]; + for (int j = 0; j < value_lens[i]; j++) { + values[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)]; + } + __system_property_add(names[i], name_lens[i], values[i], value_lens[i]); + } + + valid = true; + } + + ~LocalPropertyTestState() { + if (!valid) + return; + + __system_property_area__ = old_pa; + + __system_property_set_filename(PROP_FILENAME); + unlink(pa_filename.c_str()); + rmdir(pa_dirname.c_str()); + + for (int i = 0; i < nprops; i++) { + delete names[i]; + delete values[i]; + } + delete[] names; + delete[] name_lens; + delete[] values; + delete[] value_lens; + } +public: + const int nprops; + char **names; + int *name_lens; + char **values; + int *value_lens; + bool valid; + +private: + std::string pa_dirname; + std::string pa_filename; + void *old_pa; +}; + +static void BM_property_get(int iters, int nprops) +{ + StopBenchmarkTiming(); + + LocalPropertyTestState pa(nprops); + char value[PROP_VALUE_MAX]; + + if (!pa.valid) + return; + + srandom(iters * nprops); + + StartBenchmarkTiming(); + + for (int i = 0; i < iters; i++) { + __system_property_get(pa.names[random() % nprops], value); + } + StopBenchmarkTiming(); +} +BENCHMARK(BM_property_get)->TEST_NUM_PROPS; + +static void BM_property_find(int iters, int nprops) +{ + StopBenchmarkTiming(); + + LocalPropertyTestState pa(nprops); + + if (!pa.valid) + return; + + srandom(iters * nprops); + + StartBenchmarkTiming(); + + for (int i = 0; i < iters; i++) { + __system_property_find(pa.names[random() % nprops]); + } + StopBenchmarkTiming(); +} +BENCHMARK(BM_property_find)->TEST_NUM_PROPS; diff --git a/benchmarks/string_benchmark.cpp b/benchmarks/string_benchmark.cpp new file mode 100644 index 0000000..536e253 --- /dev/null +++ b/benchmarks/string_benchmark.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2012 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 "benchmark.h" + +#include <string.h> + +#define KB 1024 +#define MB 1024*KB + +#define AT_COMMON_SIZES \ + Arg(8)->Arg(64)->Arg(512)->Arg(1*KB)->Arg(8*KB)->Arg(16*KB)->Arg(32*KB)->Arg(64*KB) + +// TODO: test unaligned operation too? (currently everything will be 8-byte aligned by malloc.) + +static void BM_string_memcmp(int iters, int nbytes) { + StopBenchmarkTiming(); + char* src = new char[nbytes]; char* dst = new char[nbytes]; + memset(src, 'x', nbytes); + memset(dst, 'x', nbytes); + StartBenchmarkTiming(); + + volatile int c __attribute__((unused)) = 0; + for (int i = 0; i < iters; ++i) { + c += memcmp(dst, src, nbytes); + } + + StopBenchmarkTiming(); + SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes)); + delete[] src; + delete[] dst; +} +BENCHMARK(BM_string_memcmp)->AT_COMMON_SIZES; + +static void BM_string_memcpy(int iters, int nbytes) { + StopBenchmarkTiming(); + char* src = new char[nbytes]; char* dst = new char[nbytes]; + memset(src, 'x', nbytes); + StartBenchmarkTiming(); + + for (int i = 0; i < iters; ++i) { + memcpy(dst, src, nbytes); + } + + StopBenchmarkTiming(); + SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes)); + delete[] src; + delete[] dst; +} +BENCHMARK(BM_string_memcpy)->AT_COMMON_SIZES; + +static void BM_string_memmove(int iters, int nbytes) { + StopBenchmarkTiming(); + char* buf = new char[nbytes + 64]; + memset(buf, 'x', nbytes + 64); + StartBenchmarkTiming(); + + for (int i = 0; i < iters; ++i) { + memmove(buf, buf + 1, nbytes); // Worst-case overlap. + } + + StopBenchmarkTiming(); + SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes)); + delete[] buf; +} +BENCHMARK(BM_string_memmove)->AT_COMMON_SIZES; + +static void BM_string_memset(int iters, int nbytes) { + StopBenchmarkTiming(); + char* dst = new char[nbytes]; + StartBenchmarkTiming(); + + for (int i = 0; i < iters; ++i) { + memset(dst, 0, nbytes); + } + + StopBenchmarkTiming(); + SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes)); + delete[] dst; +} +BENCHMARK(BM_string_memset)->AT_COMMON_SIZES; + +static void BM_string_strlen(int iters, int nbytes) { + StopBenchmarkTiming(); + char* s = new char[nbytes]; + memset(s, 'x', nbytes); + s[nbytes - 1] = 0; + StartBenchmarkTiming(); + + volatile int c __attribute__((unused)) = 0; + for (int i = 0; i < iters; ++i) { + c += strlen(s); + } + + StopBenchmarkTiming(); + SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(nbytes)); + delete[] s; +} +BENCHMARK(BM_string_strlen)->AT_COMMON_SIZES; diff --git a/benchmarks/time_benchmark.cpp b/benchmarks/time_benchmark.cpp new file mode 100644 index 0000000..75132e5 --- /dev/null +++ b/benchmarks/time_benchmark.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2013 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 "benchmark.h" + +#include <time.h> + +#if defined(__BIONIC__) + +// Used by the horrible android.text.format.Time class, which is used by Calendar. http://b/8270865. +extern "C" void localtime_tz(const time_t* const timep, struct tm* tmp, const char* tz); + +static void BM_time_localtime_tz(int iters) { + StartBenchmarkTiming(); + + time_t now(time(NULL)); + tm broken_down_time; + for (int i = 0; i < iters; ++i) { + localtime_tz(&now, &broken_down_time, "Europe/Berlin"); + } + + StopBenchmarkTiming(); +} +BENCHMARK(BM_time_localtime_tz); +#endif |