1 /*
2 * Copyright (C) 2022 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 "src/trace_processor/util/glob.h"
18
19 #include <benchmark/benchmark.h>
20 #include <sqlite3.h>
21
22 #include "perfetto/ext/base/scoped_file.h"
23
24 namespace {
25
26 using namespace perfetto;
27 using benchmark::Counter;
28 using perfetto::trace_processor::util::GlobMatcher;
29
30 static const char kAndroidGlob[] = "*android*";
31 static const char kLaunchingGlob[] = "launching: *";
32 static const char kChoreographerGlob[] = "Choreographer#doFrame*";
33 static const char kQuestionMarkGlob[] = "Choreo?rapher#doFrame*";
34 static const char kCharClassGlob[] = "Choreo[a-z]rapher#doFrame*";
35
LoadTraceStrings(benchmark::State & state)36 std::vector<std::string> LoadTraceStrings(benchmark::State& state) {
37 std::vector<std::string> strs;
38 // This requires that the user has downloaded the file
39 // go/perfetto-benchmark-slice-strings into /tmp/trace_strings. The file is
40 // too big (220 MB after uncompression) and it's not worth adding it to the
41 // //test/data. Also it contains data from a team member's phone and cannot
42 // be public.
43 base::ScopedFstream f(fopen("/tmp/slice_strings", "re"));
44 if (!f) {
45 state.SkipWithError(
46 "Test strings missing. Googlers: download "
47 "go/perfetto-benchmark-slice-strings and save into /tmp/slice_strings");
48 return strs;
49 }
50 char line[4096];
51 while (fgets(line, sizeof(line), *f)) {
52 strs.emplace_back(base::StringView(line).ToStdString());
53 }
54 return strs;
55 }
56
57 template <class... Args>
BM_Glob(benchmark::State & state,Args &&...args)58 static void BM_Glob(benchmark::State& state, Args&&... args) {
59 auto args_tuple = std::make_tuple(std::move(args)...);
60
61 std::vector<std::string> strs = LoadTraceStrings(state);
62 GlobMatcher glob = GlobMatcher::FromPattern(std::get<0>(args_tuple));
63 for (auto _ : state) {
64 for (const std::string& str : strs)
65 benchmark::DoNotOptimize(glob.Matches(base::StringView(str)));
66 benchmark::ClobberMemory();
67 }
68 state.counters["str/s"] = Counter(static_cast<double>(strs.size()),
69 Counter::kIsIterationInvariantRate);
70 state.counters["s/str"] =
71 Counter(static_cast<double>(strs.size()),
72 Counter::kIsIterationInvariantRate | Counter::kInvert);
73 }
74
75 BENCHMARK_CAPTURE(BM_Glob, android, kAndroidGlob);
76 BENCHMARK_CAPTURE(BM_Glob, launching, kLaunchingGlob);
77 BENCHMARK_CAPTURE(BM_Glob, choreographer, kChoreographerGlob);
78 BENCHMARK_CAPTURE(BM_Glob, question_mark, kQuestionMarkGlob);
79 BENCHMARK_CAPTURE(BM_Glob, char_class, kCharClassGlob);
80
81 template <class... Args>
BM_SqliteGlob(benchmark::State & state,Args &&...args)82 static void BM_SqliteGlob(benchmark::State& state, Args&&... args) {
83 auto args_tuple = std::make_tuple(std::move(args)...);
84 const char* glob = std::get<0>(args_tuple);
85
86 std::vector<std::string> strs = LoadTraceStrings(state);
87 for (auto _ : state) {
88 for (const std::string& str : strs)
89 benchmark::DoNotOptimize(sqlite3_strglob(glob, str.c_str()));
90 benchmark::ClobberMemory();
91 }
92 state.counters["str/s"] = Counter(static_cast<double>(strs.size()),
93 Counter::kIsIterationInvariantRate);
94 state.counters["s/str"] =
95 Counter(static_cast<double>(strs.size()),
96 Counter::kIsIterationInvariantRate | Counter::kInvert);
97 }
98
99 BENCHMARK_CAPTURE(BM_SqliteGlob, android, kAndroidGlob);
100 BENCHMARK_CAPTURE(BM_SqliteGlob, launching, kLaunchingGlob);
101 BENCHMARK_CAPTURE(BM_SqliteGlob, slice, kChoreographerGlob);
102 BENCHMARK_CAPTURE(BM_SqliteGlob, question_mark, kQuestionMarkGlob);
103 BENCHMARK_CAPTURE(BM_SqliteGlob, char_class, kCharClassGlob);
104
105 } // namespace
106