1 /*
2  * Copyright (C) 2019 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 "stats_event.h"
19 
constructStatsEvent()20 static struct stats_event* constructStatsEvent() {
21     struct stats_event* event = stats_event_obtain();
22     stats_event_set_atom_id(event, 100);
23 
24     // randomly sample atom size
25     int numElements = rand() % 800;
26     for (int i = 0; i < numElements; i++) {
27         stats_event_write_int32(event, i);
28     }
29 
30     return event;
31 }
32 
BM_stats_event_truncate_buffer(benchmark::State & state)33 static void BM_stats_event_truncate_buffer(benchmark::State& state) {
34     while (state.KeepRunning()) {
35         struct stats_event* event = constructStatsEvent();
36         stats_event_build(event);
37         stats_event_write(event);
38         stats_event_release(event);
39     }
40 }
41 
42 BENCHMARK(BM_stats_event_truncate_buffer);
43 
BM_stats_event_full_buffer(benchmark::State & state)44 static void BM_stats_event_full_buffer(benchmark::State& state) {
45     while (state.KeepRunning()) {
46         struct stats_event* event = constructStatsEvent();
47         stats_event_truncate_buffer(event, false);
48         stats_event_build(event);
49         stats_event_write(event);
50         stats_event_release(event);
51     }
52 }
53 
54 BENCHMARK(BM_stats_event_full_buffer);
55