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 #include "benchmark/benchmark.h"
18 #include "logd/LogEvent.h"
19 #include "stats_event.h"
20
21 namespace android {
22 namespace os {
23 namespace statsd {
24
createAndParseStatsEvent(uint8_t * msg)25 static size_t createAndParseStatsEvent(uint8_t* msg) {
26 AStatsEvent* event = AStatsEvent_obtain();
27 AStatsEvent_setAtomId(event, 100);
28 AStatsEvent_writeInt32(event, 2);
29 AStatsEvent_writeFloat(event, 2.0);
30 AStatsEvent_build(event);
31
32 size_t size;
33 uint8_t* buf = AStatsEvent_getBuffer(event, &size);
34 memcpy(msg, buf, size);
35 return size;
36 }
37
BM_LogEventCreation(benchmark::State & state)38 static void BM_LogEventCreation(benchmark::State& state) {
39 uint8_t msg[LOGGER_ENTRY_MAX_PAYLOAD];
40 size_t size = createAndParseStatsEvent(msg);
41 while (state.KeepRunning()) {
42 LogEvent event(/*uid=*/ 1000, /*pid=*/ 1001);
43 benchmark::DoNotOptimize(event.parseBuffer(msg, size));
44 }
45 }
46 BENCHMARK(BM_LogEventCreation);
47
48 } // namespace statsd
49 } // namespace os
50 } // namespace android
51