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 <memory>
18 
19 #include "benchmark/benchmark.h"
20 #include "src/statsd_config.pb.h"
21 #include "tests/statsd_test_util.h"
22 
23 using namespace std;
24 
25 namespace android {
26 namespace os {
27 namespace statsd {
28 namespace {
29 
30 const ConfigKey cfgKey(0, 12345);
31 
createEvents()32 vector<shared_ptr<LogEvent>> createEvents() {
33     vector<shared_ptr<LogEvent>> events;
34     for (int i = 1; i <= 1000; i++) {
35         events.push_back(CreateTwoValueLogEvent(/* atomId */ 1, /* eventTimeNs */ i,
36                                                 /* value1 */ i % 50, /* value2 */ i));
37     }
38     return events;
39 }
40 
createConfig()41 StatsdConfig createConfig() {
42     StatsdConfig config;
43     *config.add_atom_matcher() = CreateSimpleAtomMatcher("matcher", /* atomId */ 1);
44 
45     *config.add_value_metric() =
46             createValueMetric("Value", config.atom_matcher(0), /* valueField */ 2,
47                               /* condition */ nullopt, /* states */ {});
48     config.mutable_value_metric(0)->mutable_dimensions_in_what()->set_field(1);
49     config.mutable_value_metric(0)->mutable_dimensions_in_what()->add_child()->set_field(1);
50     config.mutable_value_metric(0)->set_use_diff(true);
51 
52     return config;
53 }
54 
BM_ValueMetricPushedDiffedViaStatsLogProcessor(benchmark::State & state)55 void BM_ValueMetricPushedDiffedViaStatsLogProcessor(benchmark::State& state) {
56     StatsdConfig config = createConfig();
57     sp<StatsLogProcessor> processor = CreateStatsLogProcessor(1, 1, config, cfgKey);
58     vector<shared_ptr<LogEvent>> events = createEvents();
59 
60     for (auto _ : state) {
61         for (const auto& event : events) {
62             processor->OnLogEvent(event.get());
63         }
64     }
65 }
66 BENCHMARK(BM_ValueMetricPushedDiffedViaStatsLogProcessor);
67 
BM_ValueMetricPushedDiffedViaNumericValueMetricProducer(benchmark::State & state)68 void BM_ValueMetricPushedDiffedViaNumericValueMetricProducer(benchmark::State& state) {
69     StatsdConfig config = createConfig();
70     sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>();
71     sp<NumericValueMetricProducer> producer = createNumericValueMetricProducer(
72             pullerManager, config.value_metric(0), /* atomId */ 1, /* isPulled */ false, cfgKey,
73             /* protoHash */ 0x123456, /* timeBaseNs */ 100, /* startTimeNs */ 100,
74             /* logEventMatcherIndex */ 0);
75 
76     vector<shared_ptr<LogEvent>> events = createEvents();
77 
78     for (auto _ : state) {
79         for (const auto& event : events) {
80             producer->onMatchedLogEvent(/* matcherIndex */ 0, *event);
81         }
82     }
83 }
84 BENCHMARK(BM_ValueMetricPushedDiffedViaNumericValueMetricProducer);
85 
86 }  // anonymous namespace
87 }  // namespace statsd
88 }  // namespace os
89 }  // namespace android
90