Craig Tiller | b8e2bca | 2017-02-16 16:26:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | /* Test out various metadata handling primitives */ |
| 35 | |
| 36 | #include <grpc/grpc.h> |
| 37 | |
| 38 | extern "C" { |
| 39 | #include "src/core/lib/transport/metadata.h" |
| 40 | #include "src/core/lib/transport/static_metadata.h" |
| 41 | } |
| 42 | |
| 43 | #include "third_party/benchmark/include/benchmark/benchmark.h" |
| 44 | |
| 45 | static class InitializeStuff { |
| 46 | public: |
| 47 | InitializeStuff() { grpc_init(); } |
| 48 | ~InitializeStuff() { grpc_shutdown(); } |
| 49 | } initialize_stuff; |
| 50 | |
| 51 | static void BM_SliceFromStatic(benchmark::State& state) { |
| 52 | while (state.KeepRunning()) { |
| 53 | benchmark::DoNotOptimize(grpc_slice_from_static_string("abc")); |
| 54 | } |
| 55 | } |
| 56 | BENCHMARK(BM_SliceFromStatic); |
| 57 | |
| 58 | static void BM_SliceFromCopied(benchmark::State& state) { |
| 59 | while (state.KeepRunning()) { |
| 60 | grpc_slice_unref(grpc_slice_from_copied_string("abc")); |
| 61 | } |
| 62 | } |
| 63 | BENCHMARK(BM_SliceFromCopied); |
| 64 | |
| 65 | static void BM_SliceIntern(benchmark::State& state) { |
| 66 | gpr_slice slice = grpc_slice_from_static_string("abc"); |
| 67 | while (state.KeepRunning()) { |
| 68 | grpc_slice_unref(grpc_slice_intern(slice)); |
| 69 | } |
| 70 | } |
| 71 | BENCHMARK(BM_SliceIntern); |
| 72 | |
| 73 | static void BM_SliceReIntern(benchmark::State& state) { |
| 74 | gpr_slice slice = grpc_slice_intern(grpc_slice_from_static_string("abc")); |
| 75 | while (state.KeepRunning()) { |
| 76 | grpc_slice_unref(grpc_slice_intern(slice)); |
| 77 | } |
| 78 | grpc_slice_unref(slice); |
| 79 | } |
| 80 | BENCHMARK(BM_SliceReIntern); |
| 81 | |
| 82 | static void BM_SliceInternStaticMetadata(benchmark::State& state) { |
| 83 | while (state.KeepRunning()) { |
| 84 | grpc_slice_intern(GRPC_MDSTR_GZIP); |
| 85 | } |
| 86 | } |
| 87 | BENCHMARK(BM_SliceInternStaticMetadata); |
| 88 | |
| 89 | static void BM_SliceInternEqualToStaticMetadata(benchmark::State& state) { |
| 90 | gpr_slice slice = grpc_slice_from_static_string("gzip"); |
| 91 | while (state.KeepRunning()) { |
| 92 | grpc_slice_intern(slice); |
| 93 | } |
| 94 | } |
| 95 | BENCHMARK(BM_SliceInternEqualToStaticMetadata); |
| 96 | |
| 97 | static void BM_MetadataFromNonInternedSlices(benchmark::State& state) { |
| 98 | gpr_slice k = grpc_slice_from_static_string("key"); |
| 99 | gpr_slice v = grpc_slice_from_static_string("value"); |
| 100 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 101 | while (state.KeepRunning()) { |
| 102 | GRPC_MDELEM_UNREF(&exec_ctx, grpc_mdelem_create(&exec_ctx, k, v, NULL)); |
| 103 | } |
| 104 | grpc_exec_ctx_finish(&exec_ctx); |
| 105 | } |
| 106 | BENCHMARK(BM_MetadataFromNonInternedSlices); |
| 107 | |
| 108 | static void BM_MetadataFromInternedSlices(benchmark::State& state) { |
| 109 | gpr_slice k = grpc_slice_intern(grpc_slice_from_static_string("key")); |
| 110 | gpr_slice v = grpc_slice_intern(grpc_slice_from_static_string("value")); |
| 111 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 112 | while (state.KeepRunning()) { |
| 113 | GRPC_MDELEM_UNREF(&exec_ctx, grpc_mdelem_create(&exec_ctx, k, v, NULL)); |
| 114 | } |
| 115 | grpc_exec_ctx_finish(&exec_ctx); |
| 116 | grpc_slice_unref(k); |
| 117 | grpc_slice_unref(v); |
| 118 | } |
| 119 | BENCHMARK(BM_MetadataFromInternedSlices); |
| 120 | |
| 121 | static void BM_MetadataFromInternedSlicesAlreadyInIndex( |
| 122 | benchmark::State& state) { |
| 123 | gpr_slice k = grpc_slice_intern(grpc_slice_from_static_string("key")); |
| 124 | gpr_slice v = grpc_slice_intern(grpc_slice_from_static_string("value")); |
| 125 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 126 | grpc_mdelem seed = grpc_mdelem_create(&exec_ctx, k, v, NULL); |
| 127 | while (state.KeepRunning()) { |
| 128 | GRPC_MDELEM_UNREF(&exec_ctx, grpc_mdelem_create(&exec_ctx, k, v, NULL)); |
| 129 | } |
| 130 | GRPC_MDELEM_UNREF(&exec_ctx, seed); |
| 131 | grpc_exec_ctx_finish(&exec_ctx); |
| 132 | grpc_slice_unref(k); |
| 133 | grpc_slice_unref(v); |
| 134 | } |
| 135 | BENCHMARK(BM_MetadataFromInternedSlicesAlreadyInIndex); |
| 136 | |
| 137 | static void BM_MetadataFromInternedKey(benchmark::State& state) { |
| 138 | gpr_slice k = grpc_slice_intern(grpc_slice_from_static_string("key")); |
| 139 | gpr_slice v = grpc_slice_from_static_string("value"); |
| 140 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 141 | while (state.KeepRunning()) { |
| 142 | GRPC_MDELEM_UNREF(&exec_ctx, grpc_mdelem_create(&exec_ctx, k, v, NULL)); |
| 143 | } |
| 144 | grpc_exec_ctx_finish(&exec_ctx); |
| 145 | grpc_slice_unref(k); |
| 146 | } |
| 147 | BENCHMARK(BM_MetadataFromInternedKey); |
| 148 | |
| 149 | static void BM_MetadataFromNonInternedSlicesWithBackingStore( |
| 150 | benchmark::State& state) { |
| 151 | gpr_slice k = grpc_slice_from_static_string("key"); |
| 152 | gpr_slice v = grpc_slice_from_static_string("value"); |
| 153 | char backing_store[sizeof(grpc_mdelem_data)]; |
| 154 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 155 | while (state.KeepRunning()) { |
| 156 | GRPC_MDELEM_UNREF( |
| 157 | &exec_ctx, |
| 158 | grpc_mdelem_create(&exec_ctx, k, v, |
| 159 | reinterpret_cast<grpc_mdelem_data*>(backing_store))); |
| 160 | } |
| 161 | grpc_exec_ctx_finish(&exec_ctx); |
| 162 | } |
| 163 | BENCHMARK(BM_MetadataFromNonInternedSlicesWithBackingStore); |
| 164 | |
| 165 | static void BM_MetadataFromInternedSlicesWithBackingStore( |
| 166 | benchmark::State& state) { |
| 167 | gpr_slice k = grpc_slice_intern(grpc_slice_from_static_string("key")); |
| 168 | gpr_slice v = grpc_slice_intern(grpc_slice_from_static_string("value")); |
| 169 | char backing_store[sizeof(grpc_mdelem_data)]; |
| 170 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 171 | while (state.KeepRunning()) { |
| 172 | GRPC_MDELEM_UNREF( |
| 173 | &exec_ctx, |
| 174 | grpc_mdelem_create(&exec_ctx, k, v, |
| 175 | reinterpret_cast<grpc_mdelem_data*>(backing_store))); |
| 176 | } |
| 177 | grpc_exec_ctx_finish(&exec_ctx); |
| 178 | grpc_slice_unref(k); |
| 179 | grpc_slice_unref(v); |
| 180 | } |
| 181 | BENCHMARK(BM_MetadataFromInternedSlicesWithBackingStore); |
| 182 | |
| 183 | static void BM_MetadataFromInternedKeyWithBackingStore( |
| 184 | benchmark::State& state) { |
| 185 | gpr_slice k = grpc_slice_intern(grpc_slice_from_static_string("key")); |
| 186 | gpr_slice v = grpc_slice_from_static_string("value"); |
| 187 | char backing_store[sizeof(grpc_mdelem_data)]; |
| 188 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 189 | while (state.KeepRunning()) { |
| 190 | GRPC_MDELEM_UNREF( |
| 191 | &exec_ctx, |
| 192 | grpc_mdelem_create(&exec_ctx, k, v, |
| 193 | reinterpret_cast<grpc_mdelem_data*>(backing_store))); |
| 194 | } |
| 195 | grpc_exec_ctx_finish(&exec_ctx); |
| 196 | grpc_slice_unref(k); |
| 197 | } |
| 198 | BENCHMARK(BM_MetadataFromInternedKeyWithBackingStore); |
| 199 | |
| 200 | static void BM_MetadataFromStaticMetadataStrings(benchmark::State& state) { |
| 201 | gpr_slice k = GRPC_MDSTR_STATUS; |
| 202 | gpr_slice v = GRPC_MDSTR_200; |
| 203 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 204 | while (state.KeepRunning()) { |
| 205 | GRPC_MDELEM_UNREF(&exec_ctx, grpc_mdelem_create(&exec_ctx, k, v, NULL)); |
| 206 | } |
| 207 | grpc_exec_ctx_finish(&exec_ctx); |
| 208 | grpc_slice_unref(k); |
| 209 | } |
| 210 | BENCHMARK(BM_MetadataFromStaticMetadataStrings); |
| 211 | |
| 212 | static void BM_MetadataFromStaticMetadataStringsNotIndexed( |
| 213 | benchmark::State& state) { |
| 214 | gpr_slice k = GRPC_MDSTR_STATUS; |
| 215 | gpr_slice v = GRPC_MDSTR_GZIP; |
| 216 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 217 | while (state.KeepRunning()) { |
| 218 | GRPC_MDELEM_UNREF(&exec_ctx, grpc_mdelem_create(&exec_ctx, k, v, NULL)); |
| 219 | } |
| 220 | grpc_exec_ctx_finish(&exec_ctx); |
| 221 | grpc_slice_unref(k); |
| 222 | } |
| 223 | BENCHMARK(BM_MetadataFromStaticMetadataStringsNotIndexed); |
| 224 | |
| 225 | static void BM_MetadataRefUnrefExternal(benchmark::State& state) { |
| 226 | char backing_store[sizeof(grpc_mdelem_data)]; |
| 227 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 228 | grpc_mdelem el = |
| 229 | grpc_mdelem_create(&exec_ctx, grpc_slice_from_static_string("a"), |
| 230 | grpc_slice_from_static_string("b"), |
| 231 | reinterpret_cast<grpc_mdelem_data*>(backing_store)); |
| 232 | while (state.KeepRunning()) { |
| 233 | GRPC_MDELEM_UNREF(&exec_ctx, GRPC_MDELEM_REF(el)); |
| 234 | } |
| 235 | GRPC_MDELEM_UNREF(&exec_ctx, el); |
| 236 | grpc_exec_ctx_finish(&exec_ctx); |
| 237 | } |
| 238 | BENCHMARK(BM_MetadataRefUnrefExternal); |
| 239 | |
| 240 | static void BM_MetadataRefUnrefInterned(benchmark::State& state) { |
| 241 | char backing_store[sizeof(grpc_mdelem_data)]; |
| 242 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 243 | gpr_slice k = grpc_slice_intern(grpc_slice_from_static_string("key")); |
| 244 | gpr_slice v = grpc_slice_intern(grpc_slice_from_static_string("value")); |
| 245 | grpc_mdelem el = grpc_mdelem_create( |
| 246 | &exec_ctx, k, v, reinterpret_cast<grpc_mdelem_data*>(backing_store)); |
| 247 | grpc_slice_unref(k); |
| 248 | grpc_slice_unref(v); |
| 249 | while (state.KeepRunning()) { |
| 250 | GRPC_MDELEM_UNREF(&exec_ctx, GRPC_MDELEM_REF(el)); |
| 251 | } |
| 252 | GRPC_MDELEM_UNREF(&exec_ctx, el); |
| 253 | grpc_exec_ctx_finish(&exec_ctx); |
| 254 | } |
| 255 | BENCHMARK(BM_MetadataRefUnrefInterned); |
| 256 | |
| 257 | static void BM_MetadataRefUnrefAllocated(benchmark::State& state) { |
| 258 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 259 | grpc_mdelem el = |
| 260 | grpc_mdelem_create(&exec_ctx, grpc_slice_from_static_string("a"), |
| 261 | grpc_slice_from_static_string("b"), NULL); |
| 262 | while (state.KeepRunning()) { |
| 263 | GRPC_MDELEM_UNREF(&exec_ctx, GRPC_MDELEM_REF(el)); |
| 264 | } |
| 265 | GRPC_MDELEM_UNREF(&exec_ctx, el); |
| 266 | grpc_exec_ctx_finish(&exec_ctx); |
| 267 | } |
| 268 | BENCHMARK(BM_MetadataRefUnrefAllocated); |
| 269 | |
| 270 | static void BM_MetadataRefUnrefStatic(benchmark::State& state) { |
| 271 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 272 | grpc_mdelem el = |
| 273 | grpc_mdelem_create(&exec_ctx, GRPC_MDSTR_STATUS, GRPC_MDSTR_200, NULL); |
| 274 | while (state.KeepRunning()) { |
| 275 | GRPC_MDELEM_UNREF(&exec_ctx, GRPC_MDELEM_REF(el)); |
| 276 | } |
| 277 | GRPC_MDELEM_UNREF(&exec_ctx, el); |
| 278 | grpc_exec_ctx_finish(&exec_ctx); |
| 279 | } |
| 280 | BENCHMARK(BM_MetadataRefUnrefStatic); |
| 281 | |
| 282 | BENCHMARK_MAIN(); |