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