1 /*
2 * Copyright (C) 2017 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 <stdio.h>
18
19 #include <android-base/file.h>
20 #include <benchmark/benchmark.h>
21 #include <log/logcat.h>
22
23 // Dump the statistics and report results
24
logcat_popen_libc(benchmark::State & state,const char * cmd)25 static void logcat_popen_libc(benchmark::State& state, const char* cmd) {
26 while (state.KeepRunning()) {
27 FILE* fp = popen(cmd, "r");
28 std::string ret;
29 android::base::ReadFdToString(fileno(fp), &ret);
30 pclose(fp);
31 }
32 }
33
BM_logcat_stat_popen_libc(benchmark::State & state)34 static void BM_logcat_stat_popen_libc(benchmark::State& state) {
35 logcat_popen_libc(state, "logcat -b all -S");
36 }
37 BENCHMARK(BM_logcat_stat_popen_libc);
38
logcat_popen_liblogcat(benchmark::State & state,const char * cmd)39 static void logcat_popen_liblogcat(benchmark::State& state, const char* cmd) {
40 while (state.KeepRunning()) {
41 android_logcat_context ctx;
42 FILE* fp = android_logcat_popen(&ctx, cmd);
43 std::string ret;
44 android::base::ReadFdToString(fileno(fp), &ret);
45 android_logcat_pclose(&ctx, fp);
46 }
47 }
48
BM_logcat_stat_popen_liblogcat(benchmark::State & state)49 static void BM_logcat_stat_popen_liblogcat(benchmark::State& state) {
50 logcat_popen_liblogcat(state, "logcat -b all -S");
51 }
52 BENCHMARK(BM_logcat_stat_popen_liblogcat);
53
logcat_system_libc(benchmark::State & state,const char * cmd)54 static void logcat_system_libc(benchmark::State& state, const char* cmd) {
55 while (state.KeepRunning()) {
56 system(cmd);
57 }
58 }
59
BM_logcat_stat_system_libc(benchmark::State & state)60 static void BM_logcat_stat_system_libc(benchmark::State& state) {
61 logcat_system_libc(state, "logcat -b all -S >/dev/null 2>/dev/null");
62 }
63 BENCHMARK(BM_logcat_stat_system_libc);
64
logcat_system_liblogcat(benchmark::State & state,const char * cmd)65 static void logcat_system_liblogcat(benchmark::State& state, const char* cmd) {
66 while (state.KeepRunning()) {
67 android_logcat_system(cmd);
68 }
69 }
70
BM_logcat_stat_system_liblogcat(benchmark::State & state)71 static void BM_logcat_stat_system_liblogcat(benchmark::State& state) {
72 logcat_system_liblogcat(state, "logcat -b all -S >/dev/null 2>/dev/null");
73 }
74 BENCHMARK(BM_logcat_stat_system_liblogcat);
75
76 // Dump the logs and report results
77
BM_logcat_dump_popen_libc(benchmark::State & state)78 static void BM_logcat_dump_popen_libc(benchmark::State& state) {
79 logcat_popen_libc(state, "logcat -b all -d");
80 }
81 BENCHMARK(BM_logcat_dump_popen_libc);
82
BM_logcat_dump_popen_liblogcat(benchmark::State & state)83 static void BM_logcat_dump_popen_liblogcat(benchmark::State& state) {
84 logcat_popen_liblogcat(state, "logcat -b all -d");
85 }
86 BENCHMARK(BM_logcat_dump_popen_liblogcat);
87
BM_logcat_dump_system_libc(benchmark::State & state)88 static void BM_logcat_dump_system_libc(benchmark::State& state) {
89 logcat_system_libc(state, "logcat -b all -d >/dev/null 2>/dev/null");
90 }
91 BENCHMARK(BM_logcat_dump_system_libc);
92
BM_logcat_dump_system_liblogcat(benchmark::State & state)93 static void BM_logcat_dump_system_liblogcat(benchmark::State& state) {
94 logcat_system_liblogcat(state, "logcat -b all -d >/dev/null 2>/dev/null");
95 }
96 BENCHMARK(BM_logcat_dump_system_liblogcat);
97