blob: ec69c367ba0ee0b007dfbba751f55b4bde22656f [file] [log] [blame]
Andy Hung73a14702020-11-24 13:04:46 -08001/*
2 * Copyright (C) 2021 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 <binder/Parcel.h>
18#include <benchmark/benchmark.h>
19
20// Usage: atest binderParcelBenchmark
21
22// For static assert(false) we need a template version to avoid early failure.
23// See: https://stackoverflow.com/questions/51523965/template-dependent-false
24template <typename T>
25constexpr bool dependent_false_v = false;
26
27template <template <typename ...> class V, typename T, typename... Args>
28void writeVector(android::Parcel &p, const V<T, Args...> &v) {
29 if constexpr (std::is_same_v<T, bool>) {
30 p.writeBoolVector(v);
31 } else if constexpr (std::is_same_v<T, uint8_t>) {
32 p.writeByteVector(v);
33 } else if constexpr (std::is_same_v<T, char16_t>) {
34 p.writeCharVector(v);
35 } else if constexpr (std::is_same_v<T, int32_t>) {
36 p.writeInt32Vector(v);
37 } else if constexpr (std::is_same_v<T, int64_t>) {
38 p.writeInt64Vector(v);
39 } else {
40 static_assert(dependent_false_v<V<T>>);
41 }
42}
43
44template <template <typename ...> class V, typename T, typename... Args>
45void readVector(android::Parcel &p, V<T, Args...> *v) {
46 if constexpr (std::is_same_v<T, bool>) {
47 p.readBoolVector(v);
48 } else if constexpr (std::is_same_v<T, uint8_t>) {
49 p.readByteVector(v);
50 } else if constexpr (std::is_same_v<T, char16_t>) {
51 p.readCharVector(v);
52 } else if constexpr (std::is_same_v<T, int32_t>) {
53 p.readInt32Vector(v);
54 } else if constexpr (std::is_same_v<T, int64_t>) {
55 p.readInt64Vector(v);
56 } else {
57 static_assert(dependent_false_v<V<T>>);
58 }
59}
60
61// Construct a series of args { 1 << 0, 1 << 1, ..., 1 << 10 }
62static void VectorArgs(benchmark::internal::Benchmark* b) {
63 for (int i = 0; i < 10; ++i) {
64 b->Args({1 << i});
65 }
66}
67
68template <typename T>
69static void BM_ParcelVector(benchmark::State& state) {
70 const size_t elements = state.range(0);
71
72 std::vector<T> v1(elements);
73 std::vector<T> v2(elements);
74 android::Parcel p;
75 while (state.KeepRunning()) {
76 p.setDataPosition(0);
77 writeVector(p, v1);
78
79 p.setDataPosition(0);
80 readVector(p, &v2);
81
82 benchmark::DoNotOptimize(v2[0]);
83 benchmark::ClobberMemory();
84 }
85 state.SetComplexityN(elements);
86}
87
88/*
89 Parcel vector write than read.
90 The read and write vectors are fixed, no resizing required.
91
92 Results on Crosshatch Pixel 3XL
93
94 #BM_BoolVector/1 40 ns 40 ns 17261011
95 #BM_BoolVector/2 46 ns 46 ns 15029619
96 #BM_BoolVector/4 65 ns 64 ns 10888021
97 #BM_BoolVector/8 114 ns 114 ns 6130937
98 #BM_BoolVector/16 179 ns 179 ns 3902462
99 #BM_BoolVector/32 328 ns 327 ns 2138812
100 #BM_BoolVector/64 600 ns 598 ns 1169414
101 #BM_BoolVector/128 1168 ns 1165 ns 601281
102 #BM_BoolVector/256 2288 ns 2281 ns 305737
103 #BM_BoolVector/512 4535 ns 4521 ns 154668
104 #BM_ByteVector/1 53 ns 52 ns 13212196
105 #BM_ByteVector/2 53 ns 53 ns 13194050
106 #BM_ByteVector/4 50 ns 50 ns 13768037
107 #BM_ByteVector/8 50 ns 50 ns 13890210
108 #BM_ByteVector/16 50 ns 50 ns 13897305
109 #BM_ByteVector/32 51 ns 51 ns 13679862
110 #BM_ByteVector/64 54 ns 53 ns 12988544
111 #BM_ByteVector/128 64 ns 64 ns 10921227
112 #BM_ByteVector/256 82 ns 81 ns 8542549
113 #BM_ByteVector/512 118 ns 118 ns 5862931
114 #BM_CharVector/1 32 ns 32 ns 21783579
115 #BM_CharVector/2 38 ns 38 ns 18200971
116 #BM_CharVector/4 53 ns 53 ns 13111785
117 #BM_CharVector/8 80 ns 80 ns 8698331
118 #BM_CharVector/16 159 ns 159 ns 4390738
119 #BM_CharVector/32 263 ns 262 ns 2667310
120 #BM_CharVector/64 486 ns 485 ns 1441118
121 #BM_CharVector/128 937 ns 934 ns 749006
122 #BM_CharVector/256 1848 ns 1843 ns 379537
123 #BM_CharVector/512 3650 ns 3639 ns 191713
124 #BM_Int32Vector/1 31 ns 31 ns 22104147
125 #BM_Int32Vector/2 38 ns 38 ns 18075471
126 #BM_Int32Vector/4 53 ns 52 ns 13249969
127 #BM_Int32Vector/8 80 ns 80 ns 8719798
128 #BM_Int32Vector/16 161 ns 160 ns 4350096
129 #BM_Int32Vector/32 271 ns 270 ns 2591896
130 #BM_Int32Vector/64 499 ns 498 ns 1406201
131 #BM_Int32Vector/128 948 ns 945 ns 740052
132 #BM_Int32Vector/256 1855 ns 1849 ns 379127
133 #BM_Int32Vector/512 3665 ns 3653 ns 191533
134 #BM_Int64Vector/1 31 ns 31 ns 22388370
135 #BM_Int64Vector/2 38 ns 38 ns 18300347
136 #BM_Int64Vector/4 53 ns 53 ns 13137818
137 #BM_Int64Vector/8 81 ns 81 ns 8599613
138 #BM_Int64Vector/16 167 ns 166 ns 4195953
139 #BM_Int64Vector/32 280 ns 280 ns 2499271
140 #BM_Int64Vector/64 523 ns 522 ns 1341380
141 #BM_Int64Vector/128 991 ns 988 ns 707437
142 #BM_Int64Vector/256 1940 ns 1934 ns 361704
143 #BM_Int64Vector/512 3843 ns 3831 ns 183204
144*/
145
146static void BM_BoolVector(benchmark::State& state) {
147 BM_ParcelVector<bool>(state);
148}
149
150static void BM_ByteVector(benchmark::State& state) {
151 BM_ParcelVector<uint8_t>(state);
152}
153
154static void BM_CharVector(benchmark::State& state) {
155 BM_ParcelVector<char16_t>(state);
156}
157
158static void BM_Int32Vector(benchmark::State& state) {
159 BM_ParcelVector<int32_t>(state);
160}
161
162static void BM_Int64Vector(benchmark::State& state) {
163 BM_ParcelVector<int64_t>(state);
164}
165
166BENCHMARK(BM_BoolVector)->Apply(VectorArgs);
167BENCHMARK(BM_ByteVector)->Apply(VectorArgs);
168BENCHMARK(BM_CharVector)->Apply(VectorArgs);
169BENCHMARK(BM_Int32Vector)->Apply(VectorArgs);
170BENCHMARK(BM_Int64Vector)->Apply(VectorArgs);
171
172BENCHMARK_MAIN();