1 /*
2  * Copyright (C) 2018 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 #include <vector>
17 
18 #include "FieldValue.h"
19 #include "HashableDimensionKey.h"
20 #include "benchmark/benchmark.h"
21 #include "logd/LogEvent.h"
22 #include "metric_util.h"
23 #include "stats_event.h"
24 #include "stats_log_util.h"
25 
26 namespace android {
27 namespace os {
28 namespace statsd {
29 
30 using std::vector;
31 
createLogEventAndMatcher(LogEvent * event,FieldMatcher * field_matcher)32 static void createLogEventAndMatcher(LogEvent* event, FieldMatcher* field_matcher) {
33     AStatsEvent* statsEvent = AStatsEvent_obtain();
34     AStatsEvent_setAtomId(statsEvent, 1);
35     AStatsEvent_overwriteTimestamp(statsEvent, 100000);
36 
37     std::vector<int> attributionUids = {100, 100};
38     std::vector<string> attributionTags = {"LOCATION", "LOCATION"};
39     writeAttribution(statsEvent, attributionUids, attributionTags);
40 
41     AStatsEvent_writeFloat(statsEvent, 3.2f);
42     AStatsEvent_writeString(statsEvent, "LOCATION");
43     AStatsEvent_writeInt64(statsEvent, 990);
44 
45     parseStatsEventToLogEvent(statsEvent, event);
46 
47     field_matcher->set_field(1);
48     auto child = field_matcher->add_child();
49     child->set_field(1);
50     child->set_position(FIRST);
51     child->add_child()->set_field(1);
52 }
53 
BM_FilterValue(benchmark::State & state)54 static void BM_FilterValue(benchmark::State& state) {
55     LogEvent event(/*uid=*/0, /*pid=*/0);
56     FieldMatcher field_matcher;
57     createLogEventAndMatcher(&event, &field_matcher);
58 
59     std::vector<Matcher> matchers;
60     translateFieldMatcher(field_matcher, &matchers);
61 
62     while (state.KeepRunning()) {
63         HashableDimensionKey output;
64         filterValues(matchers, event.getValues(), &output);
65     }
66 }
67 
68 BENCHMARK(BM_FilterValue);
69 
70 }  //  namespace statsd
71 }  //  namespace os
72 }  //  namespace android
73