1 /*
2  * Copyright (C) 2024 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 "benchmark/benchmark.h"
18 #include "tests/statsd_test_util.h"
19 
20 using namespace std;
21 namespace android {
22 namespace os {
23 namespace statsd {
24 
BM_OnLogEvent(benchmark::State & state)25 static void BM_OnLogEvent(benchmark::State& state) {
26     StatsdConfig config;
27     auto wakelockAcquireMatcher = CreateAcquireWakelockAtomMatcher();
28     *config.add_atom_matcher() = wakelockAcquireMatcher;
29 
30     *config.add_event_metric() =
31             createEventMetric("Event", wakelockAcquireMatcher.id(), /* condition */ nullopt);
32 
33     for (int atomId = 1000; atomId < 2000; atomId++) {
34         auto matcher = CreateSimpleAtomMatcher("name" + to_string(atomId), atomId);
35         *config.add_atom_matcher() = CreateSimpleAtomMatcher("name" + to_string(atomId), atomId);
36         *config.add_event_metric() = createEventMetric("Event" + to_string(atomId), matcher.id(),
37                                                        /* condition */ nullopt);
38     }
39 
40     ConfigKey cfgKey;
41     std::vector<std::unique_ptr<LogEvent>> events;
42     vector<int> attributionUids = {111};
43     vector<string> attributionTags = {"App1"};
44     for (int i = 1; i <= 10; i++) {
45         events.push_back(CreateAcquireWakelockEvent(2 + i, attributionUids, attributionTags,
46                                                     "wl" + to_string(i)));
47     }
48 
49     sp<StatsLogProcessor> processor = CreateStatsLogProcessor(1, 1, config, cfgKey);
50 
51     for (auto _ : state) {
52         for (const auto& event : events) {
53             processor->OnLogEvent(event.get());
54         }
55     }
56 }
57 BENCHMARK(BM_OnLogEvent);
58 
59 }  // namespace statsd
60 }  // namespace os
61 }  // namespace android
62