| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
/* Microbenchmarks around CHTTP2 HPACK operations */ |
| 20 |
|
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include <memory> |
| 24 |
#include <sstream> |
| 25 |
|
| 26 |
#include <benchmark/benchmark.h> |
| 27 |
|
| 28 |
#include <grpc/slice.h> |
| 29 |
#include <grpc/support/alloc.h> |
| 30 |
#include <grpc/support/log.h> |
| 31 |
|
| 32 |
#include "src/core/ext/transport/chttp2/transport/hpack_encoder.h" |
| 33 |
#include "src/core/ext/transport/chttp2/transport/hpack_parser.h" |
| 34 |
#include "src/core/lib/gprpp/time.h" |
| 35 |
#include "src/core/lib/resource_quota/resource_quota.h" |
| 36 |
#include "src/core/lib/slice/slice_internal.h" |
| 37 |
#include "src/core/lib/slice/slice_string_helpers.h" |
| 38 |
#include "src/core/lib/transport/metadata_batch.h" |
| 39 |
#include "src/core/lib/transport/timeout_encoding.h" |
| 40 |
#include "test/core/util/test_config.h" |
| 41 |
#include "test/cpp/microbenchmarks/helpers.h" |
| 42 |
#include "test/cpp/util/test_config.h" |
| 43 |
|
| 44 |
static auto* g_memory_allocator = new grpc_core::MemoryAllocator( |
| 45 |
grpc_core::ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator( |
| 46 |
"test")); |
| 47 |
|
| 48 |
static grpc_slice MakeSlice(const std::vector<uint8_t>& bytes) { |
| 49 |
grpc_slice s = grpc_slice_malloc(bytes.size()); |
| 50 |
uint8_t* p = GRPC_SLICE_START_PTR(s); |
| 51 |
for (auto b : bytes) { |
| 52 |
*p++ = b; |
| 53 |
} |
| 54 |
return s; |
| 55 |
} |
| 56 |
|
| 57 |
//////////////////////////////////////////////////////////////////////////////// |
| 58 |
// HPACK encoder |
| 59 |
// |
| 60 |
|
| 61 |
static void BM_HpackEncoderInitDestroy(benchmark::State& state) { |
| 62 |
grpc_core::ExecCtx exec_ctx; |
| 63 |
for (auto _ : state) { |
| 64 |
grpc_core::HPackCompressor c; |
| 65 |
grpc_core::ExecCtx::Get()->Flush(); |
| 66 |
} |
| 67 |
} |
| 68 |
BENCHMARK(BM_HpackEncoderInitDestroy); |
| 69 |
|
| 70 |
static void BM_HpackEncoderEncodeDeadline(benchmark::State& state) { |
| 71 |
grpc_core::ExecCtx exec_ctx; |
| 72 |
grpc_core::Timestamp saved_now = grpc_core::Timestamp::Now(); |
| 73 |
|
| 74 |
auto arena = grpc_core::MakeScopedArena(1024, g_memory_allocator); |
| 75 |
grpc_metadata_batch b(arena.get()); |
| 76 |
b.Set(grpc_core::GrpcTimeoutMetadata(), |
| 77 |
saved_now + grpc_core::Duration::Seconds(30)); |
| 78 |
|
| 79 |
grpc_core::HPackCompressor c; |
| 80 |
grpc_transport_one_way_stats stats; |
| 81 |
stats = {}; |
| 82 |
grpc_slice_buffer outbuf; |
| 83 |
grpc_slice_buffer_init(&outbuf); |
| 84 |
while (state.KeepRunning()) { |
| 85 |
c.EncodeHeaders( |
| 86 |
grpc_core::HPackCompressor::EncodeHeaderOptions{ |
| 87 |
static_cast<uint32_t>(state.iterations()), |
| 88 |
true, |
| 89 |
false, |
| 90 |
static_cast<size_t>(1024), |
| 91 |
&stats, |
| 92 |
}, |
| 93 |
b, &outbuf); |
| 94 |
grpc_slice_buffer_reset_and_unref(&outbuf); |
| 95 |
grpc_core::ExecCtx::Get()->Flush(); |
| 96 |
} |
| 97 |
grpc_slice_buffer_destroy(&outbuf); |
| 98 |
} |
| 99 |
BENCHMARK(BM_HpackEncoderEncodeDeadline); |
| 100 |
|
| 101 |
template <class Fixture> |
| 102 |
static void BM_HpackEncoderEncodeHeader(benchmark::State& state) { |
| 103 |
grpc_core::ExecCtx exec_ctx; |
| 104 |
static bool logged_representative_output = false; |
| 105 |
|
| 106 |
auto arena = grpc_core::MakeScopedArena(1024, g_memory_allocator); |
| 107 |
grpc_metadata_batch b(arena.get()); |
| 108 |
Fixture::Prepare(&b); |
| 109 |
|
| 110 |
grpc_core::HPackCompressor c; |
| 111 |
grpc_transport_one_way_stats stats; |
| 112 |
stats = {}; |
| 113 |
grpc_slice_buffer outbuf; |
| 114 |
grpc_slice_buffer_init(&outbuf); |
| 115 |
while (state.KeepRunning()) { |
| 116 |
static constexpr int kEnsureMaxFrameAtLeast = 2; |
| 117 |
c.EncodeHeaders( |
| 118 |
grpc_core::HPackCompressor::EncodeHeaderOptions{ |
| 119 |
static_cast<uint32_t>(state.iterations()), |
| 120 |
state.range(0) != 0, |
| 121 |
Fixture::kEnableTrueBinary, |
| 122 |
static_cast<size_t>(state.range(1) + kEnsureMaxFrameAtLeast), |
| 123 |
&stats, |
| 124 |
}, |
| 125 |
b, &outbuf); |
| 126 |
if (!logged_representative_output && state.iterations() > 3) { |
| 127 |
logged_representative_output = true; |
| 128 |
for (size_t i = 0; i < outbuf.count; i++) { |
| 129 |
char* s = grpc_dump_slice(outbuf.slices[i], GPR_DUMP_HEX); |
| 130 |
gpr_log(GPR_DEBUG, "%" PRIdPTR ": %s", i, s); |
| 131 |
gpr_free(s); |
| 132 |
} |
| 133 |
} |
| 134 |
grpc_slice_buffer_reset_and_unref(&outbuf); |
| 135 |
grpc_core::ExecCtx::Get()->Flush(); |
| 136 |
} |
| 137 |
grpc_slice_buffer_destroy(&outbuf); |
| 138 |
} |
| 139 |
|
| 140 |
namespace hpack_encoder_fixtures { |
| 141 |
|
| 142 |
class EmptyBatch { |
| 143 |
public: |
| 144 |
static constexpr bool kEnableTrueBinary = false; |
| 145 |
static void Prepare(grpc_metadata_batch*) {} |
| 146 |
}; |
| 147 |
|
| 148 |
class SingleStaticElem { |
| 149 |
public: |
| 150 |
static constexpr bool kEnableTrueBinary = false; |
| 151 |
static void Prepare(grpc_metadata_batch* b) { |
| 152 |
b->Set(grpc_core::GrpcAcceptEncodingMetadata(), |
| 153 |
grpc_core::CompressionAlgorithmSet( |
| 154 |
{GRPC_COMPRESS_NONE, GRPC_COMPRESS_DEFLATE})); |
| 155 |
} |
| 156 |
}; |
| 157 |
|
| 158 |
static void CrashOnAppendError(absl::string_view, const grpc_core::Slice&) { |
| 159 |
abort(); |
| 160 |
} |
| 161 |
|
| 162 |
class SingleNonBinaryElem { |
| 163 |
public: |
| 164 |
static constexpr bool kEnableTrueBinary = false; |
| 165 |
static void Prepare(grpc_metadata_batch* b) { |
| 166 |
b->Append("abc", grpc_core::Slice::FromStaticString("def"), |
| 167 |
CrashOnAppendError); |
| 168 |
} |
| 169 |
}; |
| 170 |
|
| 171 |
template <int kLength, bool kTrueBinary> |
| 172 |
class SingleBinaryElem { |
| 173 |
public: |
| 174 |
static constexpr bool kEnableTrueBinary = kTrueBinary; |
| 175 |
static void Prepare(grpc_metadata_batch* b) { |
| 176 |
b->Append("abc-bin", MakeBytes(), CrashOnAppendError); |
| 177 |
} |
| 178 |
|
| 179 |
private: |
| 180 |
static grpc_core::Slice MakeBytes() { |
| 181 |
std::vector<char> v; |
| 182 |
v.reserve(kLength); |
| 183 |
for (int i = 0; i < kLength; i++) { |
| 184 |
v.push_back(static_cast<char>(rand())); |
| 185 |
} |
| 186 |
return grpc_core::Slice::FromCopiedBuffer(v); |
| 187 |
} |
| 188 |
}; |
| 189 |
|
| 190 |
class RepresentativeClientInitialMetadata { |
| 191 |
public: |
| 192 |
static constexpr bool kEnableTrueBinary = true; |
| 193 |
static void Prepare(grpc_metadata_batch* b) { |
| 194 |
b->Set(grpc_core::HttpSchemeMetadata(), |
| 195 |
grpc_core::HttpSchemeMetadata::kHttp); |
| 196 |
b->Set(grpc_core::HttpMethodMetadata(), |
| 197 |
grpc_core::HttpMethodMetadata::kPost); |
| 198 |
b->Set( |
| 199 |
grpc_core::HttpPathMetadata(), |
| 200 |
grpc_core::Slice(grpc_core::StaticSlice::FromStaticString("/foo/bar"))); |
| 201 |
b->Set(grpc_core::HttpAuthorityMetadata(), |
| 202 |
grpc_core::Slice(grpc_core::StaticSlice::FromStaticString( |
| 203 |
"foo.test.google.fr:1234"))); |
| 204 |
b->Set( |
| 205 |
grpc_core::GrpcAcceptEncodingMetadata(), |
| 206 |
grpc_core::CompressionAlgorithmSet( |
| 207 |
{GRPC_COMPRESS_NONE, GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_GZIP})); |
| 208 |
b->Set(grpc_core::TeMetadata(), grpc_core::TeMetadata::kTrailers); |
| 209 |
b->Set(grpc_core::ContentTypeMetadata(), |
| 210 |
grpc_core::ContentTypeMetadata::kApplicationGrpc); |
| 211 |
b->Set(grpc_core::UserAgentMetadata(), |
| 212 |
grpc_core::Slice(grpc_core::StaticSlice::FromStaticString( |
| 213 |
"grpc-c/3.0.0-dev (linux; chttp2; green)"))); |
| 214 |
} |
| 215 |
}; |
| 216 |
|
| 217 |
// This fixture reflects how initial metadata are sent by a production client, |
| 218 |
// with non-indexed :path and binary headers. The metadata here are the same as |
| 219 |
// the corresponding parser benchmark below. |
| 220 |
class MoreRepresentativeClientInitialMetadata { |
| 221 |
public: |
| 222 |
static constexpr bool kEnableTrueBinary = true; |
| 223 |
static void Prepare(grpc_metadata_batch* b) { |
| 224 |
b->Set(grpc_core::HttpSchemeMetadata(), |
| 225 |
grpc_core::HttpSchemeMetadata::kHttp); |
| 226 |
b->Set(grpc_core::HttpMethodMetadata(), |
| 227 |
grpc_core::HttpMethodMetadata::kPost); |
| 228 |
b->Set(grpc_core::HttpPathMetadata(), |
| 229 |
grpc_core::Slice(grpc_core::StaticSlice::FromStaticString( |
| 230 |
"/grpc.test.FooService/BarMethod"))); |
| 231 |
b->Set(grpc_core::HttpAuthorityMetadata(), |
| 232 |
grpc_core::Slice(grpc_core::StaticSlice::FromStaticString( |
| 233 |
"foo.test.google.fr:1234"))); |
| 234 |
b->Set(grpc_core::GrpcTraceBinMetadata(), |
| 235 |
grpc_core::Slice(grpc_core::StaticSlice::FromStaticString( |
| 236 |
"\x00\x01\x02\x03\x04\x05\x06\x07\x08" |
| 237 |
"\x09\x0a\x0b\x0c\x0d\x0e\x0f" |
| 238 |
"\x10\x11\x12\x13\x14\x15\x16\x17\x18" |
| 239 |
"\x19\x1a\x1b\x1c\x1d\x1e\x1f" |
| 240 |
"\x20\x21\x22\x23\x24\x25\x26\x27\x28" |
| 241 |
"\x29\x2a\x2b\x2c\x2d\x2e\x2f" |
| 242 |
"\x30"))); |
| 243 |
b->Set(grpc_core::GrpcTagsBinMetadata(), |
| 244 |
grpc_core::Slice(grpc_core::StaticSlice::FromStaticString( |
| 245 |
"\x00\x01\x02\x03\x04\x05\x06\x07\x08" |
| 246 |
"\x09\x0a\x0b\x0c\x0d\x0e\x0f" |
| 247 |
"\x10\x11\x12\x13"))); |
| 248 |
b->Set( |
| 249 |
grpc_core::GrpcAcceptEncodingMetadata(), |
| 250 |
grpc_core::CompressionAlgorithmSet( |
| 251 |
{GRPC_COMPRESS_NONE, GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_GZIP})); |
| 252 |
b->Set(grpc_core::TeMetadata(), grpc_core::TeMetadata::kTrailers); |
| 253 |
b->Set(grpc_core::ContentTypeMetadata(), |
| 254 |
grpc_core::ContentTypeMetadata::kApplicationGrpc); |
| 255 |
b->Set(grpc_core::UserAgentMetadata(), |
| 256 |
grpc_core::Slice(grpc_core::StaticSlice::FromStaticString( |
| 257 |
"grpc-c/3.0.0-dev (linux; chttp2; green)"))); |
| 258 |
} |
| 259 |
}; |
| 260 |
|
| 261 |
class RepresentativeServerInitialMetadata { |
| 262 |
public: |
| 263 |
static constexpr bool kEnableTrueBinary = true; |
| 264 |
static void Prepare(grpc_metadata_batch* b) { |
| 265 |
b->Set(grpc_core::HttpStatusMetadata(), 200); |
| 266 |
b->Set(grpc_core::ContentTypeMetadata(), |
| 267 |
grpc_core::ContentTypeMetadata::kApplicationGrpc); |
| 268 |
b->Set( |
| 269 |
grpc_core::GrpcAcceptEncodingMetadata(), |
| 270 |
grpc_core::CompressionAlgorithmSet( |
| 271 |
{GRPC_COMPRESS_NONE, GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_GZIP})); |
| 272 |
} |
| 273 |
}; |
| 274 |
|
| 275 |
class RepresentativeServerTrailingMetadata { |
| 276 |
public: |
| 277 |
static constexpr bool kEnableTrueBinary = true; |
| 278 |
static void Prepare(grpc_metadata_batch* b) { |
| 279 |
b->Set(grpc_core::GrpcStatusMetadata(), GRPC_STATUS_OK); |
| 280 |
} |
| 281 |
}; |
| 282 |
|
| 283 |
BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, EmptyBatch)->Args({0, 16384}); |
| 284 |
// test with eof (shouldn't affect anything) |
| 285 |
BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, EmptyBatch)->Args({1, 16384}); |
| 286 |
BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, SingleStaticElem) |
| 287 |
->Args({0, 16384}); |
| 288 |
BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, SingleNonBinaryElem) |
| 289 |
->Args({0, 16384}); |
| 290 |
BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, SingleBinaryElem<1, false>) |
| 291 |
->Args({0, 16384}); |
| 292 |
BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, SingleBinaryElem<3, false>) |
| 293 |
->Args({0, 16384}); |
| 294 |
BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, SingleBinaryElem<10, false>) |
| 295 |
->Args({0, 16384}); |
| 296 |
BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, SingleBinaryElem<31, false>) |
| 297 |
->Args({0, 16384}); |
| 298 |
BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, SingleBinaryElem<100, false>) |
| 299 |
->Args({0, 16384}); |
| 300 |
// test with a tiny frame size, to highlight continuation costs |
| 301 |
BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, SingleNonBinaryElem) |
| 302 |
->Args({0, 1}); |
| 303 |
|
| 304 |
BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, |
| 305 |
RepresentativeClientInitialMetadata) |
| 306 |
->Args({0, 16384}); |
| 307 |
BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, |
| 308 |
MoreRepresentativeClientInitialMetadata) |
| 309 |
->Args({0, 16384}); |
| 310 |
BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, |
| 311 |
RepresentativeServerInitialMetadata) |
| 312 |
->Args({0, 16384}); |
| 313 |
BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, |
| 314 |
RepresentativeServerTrailingMetadata) |
| 315 |
->Args({1, 16384}); |
| 316 |
|
| 317 |
} // namespace hpack_encoder_fixtures |
| 318 |
|
| 319 |
//////////////////////////////////////////////////////////////////////////////// |
| 320 |
// HPACK parser |
| 321 |
// |
| 322 |
|
| 323 |
static void BM_HpackParserInitDestroy(benchmark::State& state) { |
| 324 |
grpc_core::ExecCtx exec_ctx; |
| 325 |
for (auto _ : state) { |
| 326 |
{ grpc_core::HPackParser(); } |
| 327 |
grpc_core::ExecCtx::Get()->Flush(); |
| 328 |
} |
| 329 |
} |
| 330 |
BENCHMARK(BM_HpackParserInitDestroy); |
| 331 |
|
| 332 |
template <class Fixture> |
| 333 |
static void BM_HpackParserParseHeader(benchmark::State& state) { |
| 334 |
std::vector<grpc_slice> init_slices = Fixture::GetInitSlices(); |
| 335 |
std::vector<grpc_slice> benchmark_slices = Fixture::GetBenchmarkSlices(); |
| 336 |
grpc_core::ExecCtx exec_ctx; |
| 337 |
grpc_core::HPackParser p; |
| 338 |
const int kArenaSize = 4096 * 4096; |
| 339 |
auto* arena = grpc_core::Arena::Create(kArenaSize, g_memory_allocator); |
| 340 |
grpc_core::ManualConstructor<grpc_metadata_batch> b; |
| 341 |
b.Init(arena); |
| 342 |
p.BeginFrame(&*b, std::numeric_limits<uint32_t>::max(), |
| 343 |
grpc_core::HPackParser::Boundary::None, |
| 344 |
grpc_core::HPackParser::Priority::None, |
| 345 |
grpc_core::HPackParser::LogInfo{ |
| 346 |
1, grpc_core::HPackParser::LogInfo::kHeaders, false}); |
| 347 |
auto parse_vec = [&p](const std::vector<grpc_slice>& slices) { |
| 348 |
for (size_t i = 0; i < slices.size(); ++i) { |
| 349 |
auto error = p.Parse(slices[i], i == slices.size() - 1); |
| 350 |
GPR_ASSERT(error.ok()); |
| 351 |
} |
| 352 |
}; |
| 353 |
parse_vec(init_slices); |
| 354 |
while (state.KeepRunning()) { |
| 355 |
b->Clear(); |
| 356 |
parse_vec(benchmark_slices); |
| 357 |
grpc_core::ExecCtx::Get()->Flush(); |
| 358 |
// Recreate arena every 4k iterations to avoid oom |
| 359 |
if (0 == (state.iterations() & 0xfff)) { |
| 360 |
b.Destroy(); |
| 361 |
arena->Destroy(); |
| 362 |
arena = grpc_core::Arena::Create(kArenaSize, g_memory_allocator); |
| 363 |
b.Init(arena); |
| 364 |
p.BeginFrame(&*b, std::numeric_limits<uint32_t>::max(), |
| 365 |
grpc_core::HPackParser::Boundary::None, |
| 366 |
grpc_core::HPackParser::Priority::None, |
| 367 |
grpc_core::HPackParser::LogInfo{ |
| 368 |
1, grpc_core::HPackParser::LogInfo::kHeaders, false}); |
| 369 |
} |
| 370 |
} |
| 371 |
// Clean up |
| 372 |
b.Destroy(); |
| 373 |
for (auto slice : init_slices) grpc_slice_unref(slice); |
| 374 |
for (auto slice : benchmark_slices) grpc_slice_unref(slice); |
| 375 |
arena->Destroy(); |
| 376 |
} |
| 377 |
|
| 378 |
namespace hpack_parser_fixtures { |
| 379 |
|
| 380 |
template <class EncoderFixture> |
| 381 |
class FromEncoderFixture { |
| 382 |
public: |
| 383 |
static std::vector<grpc_slice> GetInitSlices() { return Generate(0); } |
| 384 |
static std::vector<grpc_slice> GetBenchmarkSlices() { return Generate(1); } |
| 385 |
|
| 386 |
private: |
| 387 |
static std::vector<grpc_slice> Generate(int iteration) { |
| 388 |
grpc_core::ExecCtx exec_ctx; |
| 389 |
|
| 390 |
auto arena = grpc_core::MakeScopedArena(1024, g_memory_allocator); |
| 391 |
grpc_metadata_batch b(arena.get()); |
| 392 |
EncoderFixture::Prepare(&b); |
| 393 |
|
| 394 |
grpc_core::HPackCompressor c; |
| 395 |
grpc_transport_one_way_stats stats; |
| 396 |
std::vector<grpc_slice> out; |
| 397 |
stats = {}; |
| 398 |
bool done = false; |
| 399 |
int i = 0; |
| 400 |
while (!done) { |
| 401 |
grpc_slice_buffer outbuf; |
| 402 |
grpc_slice_buffer_init(&outbuf); |
| 403 |
c.EncodeHeaders( |
| 404 |
grpc_core::HPackCompressor::EncodeHeaderOptions{ |
| 405 |
static_cast<uint32_t>(i), |
| 406 |
false, |
| 407 |
EncoderFixture::kEnableTrueBinary, |
| 408 |
1024 * 1024, |
| 409 |
&stats, |
| 410 |
}, |
| 411 |
b, &outbuf); |
| 412 |
if (i == iteration) { |
| 413 |
for (size_t s = 0; s < outbuf.count; s++) { |
| 414 |
out.push_back(grpc_slice_ref(outbuf.slices[s])); |
| 415 |
} |
| 416 |
done = true; |
| 417 |
} |
| 418 |
grpc_slice_buffer_reset_and_unref(&outbuf); |
| 419 |
grpc_core::ExecCtx::Get()->Flush(); |
| 420 |
grpc_slice_buffer_destroy(&outbuf); |
| 421 |
i++; |
| 422 |
} |
| 423 |
// Remove the HTTP header. |
| 424 |
GPR_ASSERT(!out.empty()); |
| 425 |
GPR_ASSERT(GRPC_SLICE_LENGTH(out[0]) > 9); |
| 426 |
out[0] = grpc_slice_sub_no_ref(out[0], 9, GRPC_SLICE_LENGTH(out[0])); |
| 427 |
return out; |
| 428 |
} |
| 429 |
}; |
| 430 |
|
| 431 |
class EmptyBatch { |
| 432 |
public: |
| 433 |
static std::vector<grpc_slice> GetInitSlices() { return {}; } |
| 434 |
static std::vector<grpc_slice> GetBenchmarkSlices() { |
| 435 |
return {MakeSlice({})}; |
| 436 |
} |
| 437 |
}; |
| 438 |
|
| 439 |
class IndexedSingleStaticElem { |
| 440 |
public: |
| 441 |
static std::vector<grpc_slice> GetInitSlices() { |
| 442 |
return {MakeSlice( |
| 443 |
{0x40, 0x07, ':', 's', 't', 'a', 't', 'u', 's', 0x03, '2', '0', '0'})}; |
| 444 |
} |
| 445 |
static std::vector<grpc_slice> GetBenchmarkSlices() { |
| 446 |
return {MakeSlice({0xbe})}; |
| 447 |
} |
| 448 |
}; |
| 449 |
|
| 450 |
class AddIndexedSingleStaticElem { |
| 451 |
public: |
| 452 |
static std::vector<grpc_slice> GetInitSlices() { return {}; } |
| 453 |
static std::vector<grpc_slice> GetBenchmarkSlices() { |
| 454 |
return {MakeSlice( |
| 455 |
{0x40, 0x07, ':', 's', 't', 'a', 't', 'u', 's', 0x03, '2', '0', '0'})}; |
| 456 |
} |
| 457 |
}; |
| 458 |
|
| 459 |
class KeyIndexedSingleStaticElem { |
| 460 |
public: |
| 461 |
static std::vector<grpc_slice> GetInitSlices() { |
| 462 |
return {MakeSlice( |
| 463 |
{0x40, 0x07, ':', 's', 't', 'a', 't', 'u', 's', 0x03, '2', '0', '0'})}; |
| 464 |
} |
| 465 |
static std::vector<grpc_slice> GetBenchmarkSlices() { |
| 466 |
return {MakeSlice({0x7e, 0x03, '4', '0', '4'})}; |
| 467 |
} |
| 468 |
}; |
| 469 |
|
| 470 |
class IndexedSingleInternedElem { |
| 471 |
public: |
| 472 |
static std::vector<grpc_slice> GetInitSlices() { |
| 473 |
return {MakeSlice({0x40, 0x03, 'a', 'b', 'c', 0x03, 'd', 'e', 'f'})}; |
| 474 |
} |
| 475 |
static std::vector<grpc_slice> GetBenchmarkSlices() { |
| 476 |
return {MakeSlice({0xbe})}; |
| 477 |
} |
| 478 |
}; |
| 479 |
|
| 480 |
class AddIndexedSingleInternedElem { |
| 481 |
public: |
| 482 |
static std::vector<grpc_slice> GetInitSlices() { return {}; } |
| 483 |
static std::vector<grpc_slice> GetBenchmarkSlices() { |
| 484 |
return {MakeSlice({0x40, 0x03, 'a', 'b', 'c', 0x03, 'd', 'e', 'f'})}; |
| 485 |
} |
| 486 |
}; |
| 487 |
|
| 488 |
class KeyIndexedSingleInternedElem { |
| 489 |
public: |
| 490 |
static std::vector<grpc_slice> GetInitSlices() { |
| 491 |
return {MakeSlice({0x40, 0x03, 'a', 'b', 'c', 0x03, 'd', 'e', 'f'})}; |
| 492 |
} |
| 493 |
static std::vector<grpc_slice> GetBenchmarkSlices() { |
| 494 |
return {MakeSlice({0x7e, 0x03, 'g', 'h', 'i'})}; |
| 495 |
} |
| 496 |
}; |
| 497 |
|
| 498 |
class NonIndexedElem { |
| 499 |
public: |
| 500 |
static std::vector<grpc_slice> GetInitSlices() { return {}; } |
| 501 |
static std::vector<grpc_slice> GetBenchmarkSlices() { |
| 502 |
return {MakeSlice({0x00, 0x03, 'a', 'b', 'c', 0x03, 'd', 'e', 'f'})}; |
| 503 |
} |
| 504 |
}; |
| 505 |
|
| 506 |
template <int kLength, bool kTrueBinary> |
| 507 |
class NonIndexedBinaryElem; |
| 508 |
|
| 509 |
template <int kLength> |
| 510 |
class NonIndexedBinaryElem<kLength, true> { |
| 511 |
public: |
| 512 |
static std::vector<grpc_slice> GetInitSlices() { return {}; } |
| 513 |
static std::vector<grpc_slice> GetBenchmarkSlices() { |
| 514 |
std::vector<uint8_t> v = { |
| 515 |
0x00, 0x07, 'a', 'b', 'c', |
| 516 |
'-', 'b', 'i', 'n', static_cast<uint8_t>(kLength + 1), |
| 517 |
0}; |
| 518 |
for (int i = 0; i < kLength; i++) { |
| 519 |
v.push_back(static_cast<uint8_t>(i)); |
| 520 |
} |
| 521 |
return {MakeSlice(v)}; |
| 522 |
} |
| 523 |
}; |
| 524 |
|
| 525 |
template <> |
| 526 |
class NonIndexedBinaryElem<1, false> { |
| 527 |
public: |
| 528 |
static std::vector<grpc_slice> GetInitSlices() { return {}; } |
| 529 |
static std::vector<grpc_slice> GetBenchmarkSlices() { |
| 530 |
return {MakeSlice( |
| 531 |
{0x00, 0x07, 'a', 'b', 'c', '-', 'b', 'i', 'n', 0x82, 0xf7, 0xb3})}; |
| 532 |
} |
| 533 |
}; |
| 534 |
|
| 535 |
template <> |
| 536 |
class NonIndexedBinaryElem<3, false> { |
| 537 |
public: |
| 538 |
static std::vector<grpc_slice> GetInitSlices() { return {}; } |
| 539 |
static std::vector<grpc_slice> GetBenchmarkSlices() { |
| 540 |
return {MakeSlice({0x00, 0x07, 'a', 'b', 'c', '-', 'b', 'i', 'n', 0x84, |
| 541 |
0x7f, 0x4e, 0x29, 0x3f})}; |
| 542 |
} |
| 543 |
}; |
| 544 |
|
| 545 |
template <> |
| 546 |
class NonIndexedBinaryElem<10, false> { |
| 547 |
public: |
| 548 |
static std::vector<grpc_slice> GetInitSlices() { return {}; } |
| 549 |
static std::vector<grpc_slice> GetBenchmarkSlices() { |
| 550 |
return {MakeSlice({0x00, 0x07, 'a', 'b', 'c', '-', 'b', |
| 551 |
'i', 'n', 0x8b, 0x71, 0x0c, 0xa5, 0x81, |
| 552 |
0x73, 0x7b, 0x47, 0x13, 0xe9, 0xf7, 0xe3})}; |
| 553 |
} |
| 554 |
}; |
| 555 |
|
| 556 |
template <> |
| 557 |
class NonIndexedBinaryElem<31, false> { |
| 558 |
public: |
| 559 |
static std::vector<grpc_slice> GetInitSlices() { return {}; } |
| 560 |
static std::vector<grpc_slice> GetBenchmarkSlices() { |
| 561 |
return {MakeSlice({0x00, 0x07, 'a', 'b', 'c', '-', 'b', 'i', 'n', |
| 562 |
0xa3, 0x92, 0x43, 0x7f, 0xbe, 0x7c, 0xea, 0x6f, 0xf3, |
| 563 |
0x3d, 0xa7, 0xa7, 0x67, 0xfb, 0xe2, 0x82, 0xf7, 0xf2, |
| 564 |
0x8f, 0x1f, 0x9d, 0xdf, 0xf1, 0x7e, 0xb3, 0xef, 0xb2, |
| 565 |
0x8f, 0x53, 0x77, 0xce, 0x0c, 0x13, 0xe3, 0xfd, 0x87})}; |
| 566 |
} |
| 567 |
}; |
| 568 |
|
| 569 |
template <> |
| 570 |
class NonIndexedBinaryElem<100, false> { |
| 571 |
public: |
| 572 |
static std::vector<grpc_slice> GetInitSlices() { return {}; } |
| 573 |
static std::vector<grpc_slice> GetBenchmarkSlices() { |
| 574 |
return {MakeSlice( |
| 575 |
{0x00, 0x07, 'a', 'b', 'c', '-', 'b', 'i', 'n', 0xeb, 0x1d, 0x4d, |
| 576 |
0xe8, 0x96, 0x8c, 0x14, 0x20, 0x06, 0xc1, 0xc3, 0xdf, 0x6e, 0x1f, 0xef, |
| 577 |
0xde, 0x2f, 0xde, 0xb7, 0xf2, 0xfe, 0x6d, 0xd4, 0xe4, 0x7d, 0xf5, 0x55, |
| 578 |
0x46, 0x52, 0x3d, 0x91, 0xf2, 0xd4, 0x6f, 0xca, 0x34, 0xcd, 0xd9, 0x39, |
| 579 |
0xbd, 0x03, 0x27, 0xe3, 0x9c, 0x74, 0xcc, 0x17, 0x34, 0xed, 0xa6, 0x6a, |
| 580 |
0x77, 0x73, 0x10, 0xcd, 0x8e, 0x4e, 0x5c, 0x7c, 0x72, 0x39, 0xd8, 0xe6, |
| 581 |
0x78, 0x6b, 0xdb, 0xa5, 0xb7, 0xab, 0xe7, 0x46, 0xae, 0x21, 0xab, 0x7f, |
| 582 |
0x01, 0x89, 0x13, 0xd7, 0xca, 0x17, 0x6e, 0xcb, 0xd6, 0x79, 0x71, 0x68, |
| 583 |
0xbf, 0x8a, 0x3f, 0x32, 0xe8, 0xba, 0xf5, 0xbe, 0xb3, 0xbc, 0xde, 0x28, |
| 584 |
0xc7, 0xcf, 0x62, 0x7a, 0x58, 0x2c, 0xcf, 0x4d, 0xe3})}; |
| 585 |
} |
| 586 |
}; |
| 587 |
|
| 588 |
using RepresentativeClientInitialMetadata = FromEncoderFixture< |
| 589 |
hpack_encoder_fixtures::RepresentativeClientInitialMetadata>; |
| 590 |
using RepresentativeServerInitialMetadata = FromEncoderFixture< |
| 591 |
hpack_encoder_fixtures::RepresentativeServerInitialMetadata>; |
| 592 |
using RepresentativeServerTrailingMetadata = FromEncoderFixture< |
| 593 |
hpack_encoder_fixtures::RepresentativeServerTrailingMetadata>; |
| 594 |
using MoreRepresentativeClientInitialMetadata = FromEncoderFixture< |
| 595 |
hpack_encoder_fixtures::MoreRepresentativeClientInitialMetadata>; |
| 596 |
|
| 597 |
// Send the same deadline repeatedly |
| 598 |
class SameDeadline { |
| 599 |
public: |
| 600 |
static std::vector<grpc_slice> GetInitSlices() { |
| 601 |
return { |
| 602 |
grpc_slice_from_static_string("@\x0cgrpc-timeout\x03" |
| 603 |
"30S")}; |
| 604 |
} |
| 605 |
static std::vector<grpc_slice> GetBenchmarkSlices() { |
| 606 |
// Use saved key and literal value. |
| 607 |
return {MakeSlice({0x0f, 0x2f, 0x03, '3', '0', 'S'})}; |
| 608 |
} |
| 609 |
}; |
| 610 |
|
| 611 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, EmptyBatch); |
| 612 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, IndexedSingleStaticElem); |
| 613 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, AddIndexedSingleStaticElem); |
| 614 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, KeyIndexedSingleStaticElem); |
| 615 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, IndexedSingleInternedElem); |
| 616 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, AddIndexedSingleInternedElem); |
| 617 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, KeyIndexedSingleInternedElem); |
| 618 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedElem); |
| 619 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<1, false>); |
| 620 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<3, false>); |
| 621 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<10, false>); |
| 622 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<31, false>); |
| 623 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<100, false>); |
| 624 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<1, true>); |
| 625 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<3, true>); |
| 626 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<10, true>); |
| 627 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<31, true>); |
| 628 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<100, true>); |
| 629 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, |
| 630 |
RepresentativeClientInitialMetadata); |
| 631 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, |
| 632 |
MoreRepresentativeClientInitialMetadata); |
| 633 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, |
| 634 |
RepresentativeServerInitialMetadata); |
| 635 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, |
| 636 |
RepresentativeServerTrailingMetadata); |
| 637 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, |
| 638 |
RepresentativeClientInitialMetadata); |
| 639 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, |
| 640 |
MoreRepresentativeClientInitialMetadata); |
| 641 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, |
| 642 |
RepresentativeServerInitialMetadata); |
| 643 |
BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, SameDeadline); |
| 644 |
|
| 645 |
} // namespace hpack_parser_fixtures |
| 646 |
|
| 647 |
// Some distros have RunSpecifiedBenchmarks under the benchmark namespace, |
| 648 |
// and others do not. This allows us to support both modes. |
| 649 |
namespace benchmark { |
| 650 |
void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); } |
| 651 |
} // namespace benchmark |
| 652 |
|
| 653 |
int main(int argc, char** argv) { |
| 654 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 655 |
LibraryInitializer libInit; |
| 656 |
::benchmark::Initialize(&argc, argv); |
| 657 |
grpc::testing::InitTest(&argc, &argv, false); |
| 658 |
benchmark::RunTheBenchmarksNamespaced(); |
| 659 |
return 0; |
| 660 |
} |