bm_chttp2_hpack.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. /* Microbenchmarks around CHTTP2 HPACK operations */
  19. #include <grpc/support/alloc.h>
  20. #include <grpc/support/log.h>
  21. #include <string.h>
  22. #include <sstream>
  23. extern "C" {
  24. #include "src/core/ext/transport/chttp2/transport/hpack_encoder.h"
  25. #include "src/core/ext/transport/chttp2/transport/hpack_parser.h"
  26. #include "src/core/lib/slice/slice_internal.h"
  27. #include "src/core/lib/slice/slice_string_helpers.h"
  28. #include "src/core/lib/transport/static_metadata.h"
  29. }
  30. #include "test/cpp/microbenchmarks/helpers.h"
  31. #include "third_party/benchmark/include/benchmark/benchmark.h"
  32. auto &force_library_initialization = Library::get();
  33. ////////////////////////////////////////////////////////////////////////////////
  34. // HPACK encoder
  35. //
  36. static void BM_HpackEncoderInitDestroy(benchmark::State &state) {
  37. TrackCounters track_counters;
  38. ExecCtx _local_exec_ctx;
  39. grpc_chttp2_hpack_compressor c;
  40. while (state.KeepRunning()) {
  41. grpc_chttp2_hpack_compressor_init(&c);
  42. grpc_chttp2_hpack_compressor_destroy(&c);
  43. grpc_exec_ctx_flush();
  44. }
  45. grpc_exec_ctx_finish();
  46. track_counters.Finish(state);
  47. }
  48. BENCHMARK(BM_HpackEncoderInitDestroy);
  49. template <class Fixture>
  50. static void BM_HpackEncoderEncodeHeader(benchmark::State &state) {
  51. TrackCounters track_counters;
  52. ExecCtx _local_exec_ctx;
  53. static bool logged_representative_output = false;
  54. grpc_metadata_batch b;
  55. grpc_metadata_batch_init(&b);
  56. std::vector<grpc_mdelem> elems = Fixture::GetElems();
  57. std::vector<grpc_linked_mdelem> storage(elems.size());
  58. for (size_t i = 0; i < elems.size(); i++) {
  59. GPR_ASSERT(GRPC_LOG_IF_ERROR(
  60. "addmd", grpc_metadata_batch_add_tail(&b, &storage[i], elems[i])));
  61. }
  62. grpc_chttp2_hpack_compressor c;
  63. grpc_chttp2_hpack_compressor_init(&c);
  64. grpc_transport_one_way_stats stats;
  65. memset(&stats, 0, sizeof(stats));
  66. grpc_slice_buffer outbuf;
  67. grpc_slice_buffer_init(&outbuf);
  68. while (state.KeepRunning()) {
  69. grpc_encode_header_options hopt = {
  70. static_cast<uint32_t>(state.iterations()),
  71. state.range(0) != 0,
  72. Fixture::kEnableTrueBinary,
  73. (size_t)state.range(1),
  74. &stats,
  75. };
  76. grpc_chttp2_encode_header(&c, NULL, 0, &b, &hopt, &outbuf);
  77. if (!logged_representative_output && state.iterations() > 3) {
  78. logged_representative_output = true;
  79. for (size_t i = 0; i < outbuf.count; i++) {
  80. char *s = grpc_dump_slice(outbuf.slices[i], GPR_DUMP_HEX);
  81. gpr_log(GPR_DEBUG, "%" PRIdPTR ": %s", i, s);
  82. gpr_free(s);
  83. }
  84. }
  85. grpc_slice_buffer_reset_and_unref_internal(&outbuf);
  86. grpc_exec_ctx_flush();
  87. }
  88. grpc_metadata_batch_destroy(&b);
  89. grpc_chttp2_hpack_compressor_destroy(&c);
  90. grpc_slice_buffer_destroy_internal(&outbuf);
  91. grpc_exec_ctx_finish();
  92. std::ostringstream label;
  93. label << "framing_bytes/iter:" << (static_cast<double>(stats.framing_bytes) /
  94. static_cast<double>(state.iterations()))
  95. << " header_bytes/iter:" << (static_cast<double>(stats.header_bytes) /
  96. static_cast<double>(state.iterations()));
  97. state.SetLabel(label.str());
  98. track_counters.Finish(state);
  99. }
  100. namespace hpack_encoder_fixtures {
  101. class EmptyBatch {
  102. public:
  103. static constexpr bool kEnableTrueBinary = false;
  104. static std::vector<grpc_mdelem> GetElems() { return {}; }
  105. };
  106. class SingleStaticElem {
  107. public:
  108. static constexpr bool kEnableTrueBinary = false;
  109. static std::vector<grpc_mdelem> GetElems() {
  110. return {GRPC_MDELEM_GRPC_ACCEPT_ENCODING_IDENTITY_COMMA_DEFLATE};
  111. }
  112. };
  113. class SingleInternedElem {
  114. public:
  115. static constexpr bool kEnableTrueBinary = false;
  116. static std::vector<grpc_mdelem> GetElems() {
  117. return {grpc_mdelem_from_slices(
  118. grpc_slice_intern(grpc_slice_from_static_string("abc")),
  119. grpc_slice_intern(grpc_slice_from_static_string("def")))};
  120. }
  121. };
  122. template <int kLength, bool kTrueBinary>
  123. class SingleInternedBinaryElem {
  124. public:
  125. static constexpr bool kEnableTrueBinary = kTrueBinary;
  126. static std::vector<grpc_mdelem> GetElems() {
  127. grpc_slice bytes = MakeBytes();
  128. std::vector<grpc_mdelem> out = {grpc_mdelem_from_slices(
  129. grpc_slice_intern(grpc_slice_from_static_string("abc-bin")),
  130. grpc_slice_intern(bytes))};
  131. grpc_slice_unref(bytes);
  132. return out;
  133. }
  134. private:
  135. static grpc_slice MakeBytes() {
  136. std::vector<char> v;
  137. for (int i = 0; i < kLength; i++) {
  138. v.push_back(static_cast<char>(rand()));
  139. }
  140. return grpc_slice_from_copied_buffer(v.data(), v.size());
  141. }
  142. };
  143. class SingleInternedKeyElem {
  144. public:
  145. static constexpr bool kEnableTrueBinary = false;
  146. static std::vector<grpc_mdelem> GetElems() {
  147. return {grpc_mdelem_from_slices(
  148. grpc_slice_intern(grpc_slice_from_static_string("abc")),
  149. grpc_slice_from_static_string("def"))};
  150. }
  151. };
  152. class SingleNonInternedElem {
  153. public:
  154. static constexpr bool kEnableTrueBinary = false;
  155. static std::vector<grpc_mdelem> GetElems() {
  156. return {grpc_mdelem_from_slices(grpc_slice_from_static_string("abc"),
  157. grpc_slice_from_static_string("def"))};
  158. }
  159. };
  160. template <int kLength, bool kTrueBinary>
  161. class SingleNonInternedBinaryElem {
  162. public:
  163. static constexpr bool kEnableTrueBinary = kTrueBinary;
  164. static std::vector<grpc_mdelem> GetElems() {
  165. return {grpc_mdelem_from_slices(grpc_slice_from_static_string("abc-bin"),
  166. MakeBytes())};
  167. }
  168. private:
  169. static grpc_slice MakeBytes() {
  170. std::vector<char> v;
  171. for (int i = 0; i < kLength; i++) {
  172. v.push_back(static_cast<char>(rand()));
  173. }
  174. return grpc_slice_from_copied_buffer(v.data(), v.size());
  175. }
  176. };
  177. class RepresentativeClientInitialMetadata {
  178. public:
  179. static constexpr bool kEnableTrueBinary = true;
  180. static std::vector<grpc_mdelem> GetElems() {
  181. return {
  182. GRPC_MDELEM_SCHEME_HTTP, GRPC_MDELEM_METHOD_POST,
  183. grpc_mdelem_from_slices(
  184. GRPC_MDSTR_PATH,
  185. grpc_slice_intern(grpc_slice_from_static_string("/foo/bar"))),
  186. grpc_mdelem_from_slices(GRPC_MDSTR_AUTHORITY,
  187. grpc_slice_intern(grpc_slice_from_static_string(
  188. "foo.test.google.fr:1234"))),
  189. GRPC_MDELEM_GRPC_ACCEPT_ENCODING_IDENTITY_COMMA_DEFLATE_COMMA_GZIP,
  190. GRPC_MDELEM_TE_TRAILERS,
  191. GRPC_MDELEM_CONTENT_TYPE_APPLICATION_SLASH_GRPC,
  192. grpc_mdelem_from_slices(
  193. GRPC_MDSTR_USER_AGENT,
  194. grpc_slice_intern(grpc_slice_from_static_string(
  195. "grpc-c/3.0.0-dev (linux; chttp2; green)")))};
  196. }
  197. };
  198. class RepresentativeServerInitialMetadata {
  199. public:
  200. static constexpr bool kEnableTrueBinary = true;
  201. static std::vector<grpc_mdelem> GetElems() {
  202. return {GRPC_MDELEM_STATUS_200,
  203. GRPC_MDELEM_CONTENT_TYPE_APPLICATION_SLASH_GRPC,
  204. GRPC_MDELEM_GRPC_ACCEPT_ENCODING_IDENTITY_COMMA_DEFLATE_COMMA_GZIP};
  205. }
  206. };
  207. class RepresentativeServerTrailingMetadata {
  208. public:
  209. static constexpr bool kEnableTrueBinary = true;
  210. static std::vector<grpc_mdelem> GetElems() {
  211. return {GRPC_MDELEM_GRPC_STATUS_0};
  212. }
  213. };
  214. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, EmptyBatch)->Args({0, 16384});
  215. // test with eof (shouldn't affect anything)
  216. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, EmptyBatch)->Args({1, 16384});
  217. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, SingleStaticElem)
  218. ->Args({0, 16384});
  219. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, SingleInternedKeyElem)
  220. ->Args({0, 16384});
  221. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, SingleInternedElem)
  222. ->Args({0, 16384});
  223. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  224. SingleInternedBinaryElem<1, false>)
  225. ->Args({0, 16384});
  226. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  227. SingleInternedBinaryElem<3, false>)
  228. ->Args({0, 16384});
  229. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  230. SingleInternedBinaryElem<10, false>)
  231. ->Args({0, 16384});
  232. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  233. SingleInternedBinaryElem<31, false>)
  234. ->Args({0, 16384});
  235. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  236. SingleInternedBinaryElem<100, false>)
  237. ->Args({0, 16384});
  238. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  239. SingleInternedBinaryElem<1, true>)
  240. ->Args({0, 16384});
  241. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  242. SingleInternedBinaryElem<3, true>)
  243. ->Args({0, 16384});
  244. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  245. SingleInternedBinaryElem<10, true>)
  246. ->Args({0, 16384});
  247. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  248. SingleInternedBinaryElem<31, true>)
  249. ->Args({0, 16384});
  250. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  251. SingleInternedBinaryElem<100, true>)
  252. ->Args({0, 16384});
  253. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, SingleNonInternedElem)
  254. ->Args({0, 16384});
  255. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  256. SingleNonInternedBinaryElem<1, false>)
  257. ->Args({0, 16384});
  258. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  259. SingleNonInternedBinaryElem<3, false>)
  260. ->Args({0, 16384});
  261. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  262. SingleNonInternedBinaryElem<10, false>)
  263. ->Args({0, 16384});
  264. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  265. SingleNonInternedBinaryElem<31, false>)
  266. ->Args({0, 16384});
  267. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  268. SingleNonInternedBinaryElem<100, false>)
  269. ->Args({0, 16384});
  270. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  271. SingleNonInternedBinaryElem<1, true>)
  272. ->Args({0, 16384});
  273. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  274. SingleNonInternedBinaryElem<3, true>)
  275. ->Args({0, 16384});
  276. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  277. SingleNonInternedBinaryElem<10, true>)
  278. ->Args({0, 16384});
  279. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  280. SingleNonInternedBinaryElem<31, true>)
  281. ->Args({0, 16384});
  282. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  283. SingleNonInternedBinaryElem<100, true>)
  284. ->Args({0, 16384});
  285. // test with a tiny frame size, to highlight continuation costs
  286. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader, SingleNonInternedElem)
  287. ->Args({0, 1});
  288. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  289. RepresentativeClientInitialMetadata)
  290. ->Args({0, 16384});
  291. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  292. RepresentativeServerInitialMetadata)
  293. ->Args({0, 16384});
  294. BENCHMARK_TEMPLATE(BM_HpackEncoderEncodeHeader,
  295. RepresentativeServerTrailingMetadata)
  296. ->Args({1, 16384});
  297. } // namespace hpack_encoder_fixtures
  298. ////////////////////////////////////////////////////////////////////////////////
  299. // HPACK parser
  300. //
  301. static void BM_HpackParserInitDestroy(benchmark::State &state) {
  302. TrackCounters track_counters;
  303. ExecCtx _local_exec_ctx;
  304. grpc_chttp2_hpack_parser p;
  305. while (state.KeepRunning()) {
  306. grpc_chttp2_hpack_parser_init(&p);
  307. grpc_chttp2_hpack_parser_destroy(&p);
  308. grpc_exec_ctx_flush();
  309. }
  310. grpc_exec_ctx_finish();
  311. track_counters.Finish(state);
  312. }
  313. BENCHMARK(BM_HpackParserInitDestroy);
  314. static void UnrefHeader(void *user_data, grpc_mdelem md) {
  315. GRPC_MDELEM_UNREF(md);
  316. }
  317. template <class Fixture>
  318. static void BM_HpackParserParseHeader(benchmark::State &state) {
  319. TrackCounters track_counters;
  320. ExecCtx _local_exec_ctx;
  321. std::vector<grpc_slice> init_slices = Fixture::GetInitSlices();
  322. std::vector<grpc_slice> benchmark_slices = Fixture::GetBenchmarkSlices();
  323. grpc_chttp2_hpack_parser p;
  324. grpc_chttp2_hpack_parser_init(&p);
  325. p.on_header = UnrefHeader;
  326. p.on_header_user_data = nullptr;
  327. for (auto slice : init_slices) {
  328. grpc_chttp2_hpack_parser_parse(&p, slice);
  329. }
  330. while (state.KeepRunning()) {
  331. for (auto slice : benchmark_slices) {
  332. grpc_chttp2_hpack_parser_parse(&p, slice);
  333. }
  334. grpc_exec_ctx_flush();
  335. }
  336. for (auto slice : init_slices) grpc_slice_unref(slice);
  337. for (auto slice : benchmark_slices) grpc_slice_unref(slice);
  338. grpc_chttp2_hpack_parser_destroy(&p);
  339. grpc_exec_ctx_finish();
  340. track_counters.Finish(state);
  341. }
  342. namespace hpack_parser_fixtures {
  343. static grpc_slice MakeSlice(std::vector<uint8_t> bytes) {
  344. grpc_slice s = grpc_slice_malloc(bytes.size());
  345. uint8_t *p = GRPC_SLICE_START_PTR(s);
  346. for (auto b : bytes) {
  347. *p++ = b;
  348. }
  349. return s;
  350. }
  351. class EmptyBatch {
  352. public:
  353. static std::vector<grpc_slice> GetInitSlices() { return {}; }
  354. static std::vector<grpc_slice> GetBenchmarkSlices() {
  355. return {MakeSlice({})};
  356. }
  357. };
  358. class IndexedSingleStaticElem {
  359. public:
  360. static std::vector<grpc_slice> GetInitSlices() {
  361. return {MakeSlice(
  362. {0x40, 0x07, ':', 's', 't', 'a', 't', 'u', 's', 0x03, '2', '0', '0'})};
  363. }
  364. static std::vector<grpc_slice> GetBenchmarkSlices() {
  365. return {MakeSlice({0xbe})};
  366. }
  367. };
  368. class AddIndexedSingleStaticElem {
  369. public:
  370. static std::vector<grpc_slice> GetInitSlices() { return {}; }
  371. static std::vector<grpc_slice> GetBenchmarkSlices() {
  372. return {MakeSlice(
  373. {0x40, 0x07, ':', 's', 't', 'a', 't', 'u', 's', 0x03, '2', '0', '0'})};
  374. }
  375. };
  376. class KeyIndexedSingleStaticElem {
  377. public:
  378. static std::vector<grpc_slice> GetInitSlices() {
  379. return {MakeSlice(
  380. {0x40, 0x07, ':', 's', 't', 'a', 't', 'u', 's', 0x03, '2', '0', '0'})};
  381. }
  382. static std::vector<grpc_slice> GetBenchmarkSlices() {
  383. return {MakeSlice({0x7e, 0x03, 'd', 'e', 'f'})};
  384. }
  385. };
  386. class IndexedSingleInternedElem {
  387. public:
  388. static std::vector<grpc_slice> GetInitSlices() {
  389. return {MakeSlice({0x40, 0x03, 'a', 'b', 'c', 0x03, 'd', 'e', 'f'})};
  390. }
  391. static std::vector<grpc_slice> GetBenchmarkSlices() {
  392. return {MakeSlice({0xbe})};
  393. }
  394. };
  395. class AddIndexedSingleInternedElem {
  396. public:
  397. static std::vector<grpc_slice> GetInitSlices() { return {}; }
  398. static std::vector<grpc_slice> GetBenchmarkSlices() {
  399. return {MakeSlice({0x40, 0x03, 'a', 'b', 'c', 0x03, 'd', 'e', 'f'})};
  400. }
  401. };
  402. class KeyIndexedSingleInternedElem {
  403. public:
  404. static std::vector<grpc_slice> GetInitSlices() {
  405. return {MakeSlice({0x40, 0x03, 'a', 'b', 'c', 0x03, 'd', 'e', 'f'})};
  406. }
  407. static std::vector<grpc_slice> GetBenchmarkSlices() {
  408. return {MakeSlice({0x7e, 0x03, 'g', 'h', 'i'})};
  409. }
  410. };
  411. class NonIndexedElem {
  412. public:
  413. static std::vector<grpc_slice> GetInitSlices() { return {}; }
  414. static std::vector<grpc_slice> GetBenchmarkSlices() {
  415. return {MakeSlice({0x00, 0x03, 'a', 'b', 'c', 0x03, 'd', 'e', 'f'})};
  416. }
  417. };
  418. template <int kLength, bool kTrueBinary>
  419. class NonIndexedBinaryElem;
  420. template <int kLength>
  421. class NonIndexedBinaryElem<kLength, true> {
  422. public:
  423. static std::vector<grpc_slice> GetInitSlices() { return {}; }
  424. static std::vector<grpc_slice> GetBenchmarkSlices() {
  425. std::vector<uint8_t> v = {
  426. 0x00, 0x07, 'a', 'b', 'c',
  427. '-', 'b', 'i', 'n', static_cast<uint8_t>(kLength + 1),
  428. 0};
  429. for (int i = 0; i < kLength; i++) {
  430. v.push_back(static_cast<uint8_t>(i));
  431. }
  432. return {MakeSlice(v)};
  433. }
  434. };
  435. template <>
  436. class NonIndexedBinaryElem<1, false> {
  437. public:
  438. static std::vector<grpc_slice> GetInitSlices() { return {}; }
  439. static std::vector<grpc_slice> GetBenchmarkSlices() {
  440. return {MakeSlice(
  441. {0x00, 0x07, 'a', 'b', 'c', '-', 'b', 'i', 'n', 0x82, 0xf7, 0xb3})};
  442. }
  443. };
  444. template <>
  445. class NonIndexedBinaryElem<3, false> {
  446. public:
  447. static std::vector<grpc_slice> GetInitSlices() { return {}; }
  448. static std::vector<grpc_slice> GetBenchmarkSlices() {
  449. return {MakeSlice({0x00, 0x07, 'a', 'b', 'c', '-', 'b', 'i', 'n', 0x84,
  450. 0x7f, 0x4e, 0x29, 0x3f})};
  451. }
  452. };
  453. template <>
  454. class NonIndexedBinaryElem<10, false> {
  455. public:
  456. static std::vector<grpc_slice> GetInitSlices() { return {}; }
  457. static std::vector<grpc_slice> GetBenchmarkSlices() {
  458. return {MakeSlice({0x00, 0x07, 'a', 'b', 'c', '-', 'b',
  459. 'i', 'n', 0x8b, 0x71, 0x0c, 0xa5, 0x81,
  460. 0x73, 0x7b, 0x47, 0x13, 0xe9, 0xf7, 0xe3})};
  461. }
  462. };
  463. template <>
  464. class NonIndexedBinaryElem<31, false> {
  465. public:
  466. static std::vector<grpc_slice> GetInitSlices() { return {}; }
  467. static std::vector<grpc_slice> GetBenchmarkSlices() {
  468. return {MakeSlice({0x00, 0x07, 'a', 'b', 'c', '-', 'b', 'i', 'n',
  469. 0xa3, 0x92, 0x43, 0x7f, 0xbe, 0x7c, 0xea, 0x6f, 0xf3,
  470. 0x3d, 0xa7, 0xa7, 0x67, 0xfb, 0xe2, 0x82, 0xf7, 0xf2,
  471. 0x8f, 0x1f, 0x9d, 0xdf, 0xf1, 0x7e, 0xb3, 0xef, 0xb2,
  472. 0x8f, 0x53, 0x77, 0xce, 0x0c, 0x13, 0xe3, 0xfd, 0x87})};
  473. }
  474. };
  475. template <>
  476. class NonIndexedBinaryElem<100, false> {
  477. public:
  478. static std::vector<grpc_slice> GetInitSlices() { return {}; }
  479. static std::vector<grpc_slice> GetBenchmarkSlices() {
  480. return {MakeSlice(
  481. {0x00, 0x07, 'a', 'b', 'c', '-', 'b', 'i', 'n', 0xeb, 0x1d, 0x4d,
  482. 0xe8, 0x96, 0x8c, 0x14, 0x20, 0x06, 0xc1, 0xc3, 0xdf, 0x6e, 0x1f, 0xef,
  483. 0xde, 0x2f, 0xde, 0xb7, 0xf2, 0xfe, 0x6d, 0xd4, 0xe4, 0x7d, 0xf5, 0x55,
  484. 0x46, 0x52, 0x3d, 0x91, 0xf2, 0xd4, 0x6f, 0xca, 0x34, 0xcd, 0xd9, 0x39,
  485. 0xbd, 0x03, 0x27, 0xe3, 0x9c, 0x74, 0xcc, 0x17, 0x34, 0xed, 0xa6, 0x6a,
  486. 0x77, 0x73, 0x10, 0xcd, 0x8e, 0x4e, 0x5c, 0x7c, 0x72, 0x39, 0xd8, 0xe6,
  487. 0x78, 0x6b, 0xdb, 0xa5, 0xb7, 0xab, 0xe7, 0x46, 0xae, 0x21, 0xab, 0x7f,
  488. 0x01, 0x89, 0x13, 0xd7, 0xca, 0x17, 0x6e, 0xcb, 0xd6, 0x79, 0x71, 0x68,
  489. 0xbf, 0x8a, 0x3f, 0x32, 0xe8, 0xba, 0xf5, 0xbe, 0xb3, 0xbc, 0xde, 0x28,
  490. 0xc7, 0xcf, 0x62, 0x7a, 0x58, 0x2c, 0xcf, 0x4d, 0xe3})};
  491. }
  492. };
  493. class RepresentativeClientInitialMetadata {
  494. public:
  495. static std::vector<grpc_slice> GetInitSlices() {
  496. return {grpc_slice_from_static_string(
  497. // generated with:
  498. // ```
  499. // tools/codegen/core/gen_header_frame.py --compression inc --no_framing
  500. // < test/core/bad_client/tests/simple_request.headers
  501. // ```
  502. "@\x05:path\x08/foo/bar"
  503. "@\x07:scheme\x04http"
  504. "@\x07:method\x04POST"
  505. "@\x0a:authority\x09localhost"
  506. "@\x0c"
  507. "content-type\x10"
  508. "application/grpc"
  509. "@\x14grpc-accept-encoding\x15identity,deflate,gzip"
  510. "@\x02te\x08trailers"
  511. "@\x0auser-agent\"bad-client grpc-c/0.12.0.0 (linux)")};
  512. }
  513. static std::vector<grpc_slice> GetBenchmarkSlices() {
  514. // generated with:
  515. // ```
  516. // tools/codegen/core/gen_header_frame.py --compression pre --no_framing
  517. // --hex < test/core/bad_client/tests/simple_request.headers
  518. // ```
  519. return {MakeSlice({0xc5, 0xc4, 0xc3, 0xc2, 0xc1, 0xc0, 0xbf, 0xbe})};
  520. }
  521. };
  522. class RepresentativeServerInitialMetadata {
  523. public:
  524. static std::vector<grpc_slice> GetInitSlices() {
  525. return {grpc_slice_from_static_string(
  526. // generated with:
  527. // ```
  528. // tools/codegen/core/gen_header_frame.py --compression inc --no_framing
  529. // <
  530. // test/cpp/microbenchmarks/representative_server_initial_metadata.headers
  531. // ```
  532. "@\x07:status\x03"
  533. "200"
  534. "@\x0c"
  535. "content-type\x10"
  536. "application/grpc"
  537. "@\x14grpc-accept-encoding\x15identity,deflate,gzip")};
  538. }
  539. static std::vector<grpc_slice> GetBenchmarkSlices() {
  540. // generated with:
  541. // ```
  542. // tools/codegen/core/gen_header_frame.py --compression pre --no_framing
  543. // --hex <
  544. // test/cpp/microbenchmarks/representative_server_initial_metadata.headers
  545. // ```
  546. return {MakeSlice({0xc0, 0xbf, 0xbe})};
  547. }
  548. };
  549. class RepresentativeServerTrailingMetadata {
  550. public:
  551. static std::vector<grpc_slice> GetInitSlices() {
  552. return {grpc_slice_from_static_string(
  553. // generated with:
  554. // ```
  555. // tools/codegen/core/gen_header_frame.py --compression inc --no_framing
  556. // <
  557. // test/cpp/microbenchmarks/representative_server_trailing_metadata.headers
  558. // ```
  559. "@\x0bgrpc-status\x01"
  560. "0"
  561. "@\x0cgrpc-message\x00")};
  562. }
  563. static std::vector<grpc_slice> GetBenchmarkSlices() {
  564. // generated with:
  565. // ```
  566. // tools/codegen/core/gen_header_frame.py --compression pre --no_framing
  567. // --hex <
  568. // test/cpp/microbenchmarks/representative_server_trailing_metadata.headers
  569. // ```
  570. return {MakeSlice({0xbf, 0xbe})};
  571. }
  572. };
  573. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, EmptyBatch);
  574. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, IndexedSingleStaticElem);
  575. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, AddIndexedSingleStaticElem);
  576. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, KeyIndexedSingleStaticElem);
  577. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, IndexedSingleInternedElem);
  578. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, AddIndexedSingleInternedElem);
  579. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, KeyIndexedSingleInternedElem);
  580. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedElem);
  581. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<1, false>);
  582. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<3, false>);
  583. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<10, false>);
  584. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<31, false>);
  585. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<100, false>);
  586. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<1, true>);
  587. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<3, true>);
  588. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<10, true>);
  589. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<31, true>);
  590. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader, NonIndexedBinaryElem<100, true>);
  591. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader,
  592. RepresentativeClientInitialMetadata);
  593. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader,
  594. RepresentativeServerInitialMetadata);
  595. BENCHMARK_TEMPLATE(BM_HpackParserParseHeader,
  596. RepresentativeServerTrailingMetadata);
  597. } // namespace hpack_parser_fixtures
  598. BENCHMARK_MAIN();