proxygen
HTTPHeadersBenchmark.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #include <algorithm>
11 #include <folly/Benchmark.h>
13 
14 using namespace folly;
15 using namespace proxygen;
16 
17 // buck build @mode/opt proxygen/lib/http/test:http_headers_benchmark
18 // ./buck-out/gen/proxygen/lib/http/test/http_headers_benchmark -bm_min_iters 100
19 // ============================================================================
20 // proxygen/lib/http/test/HTTPHeadersBenchmark.cpp relative time/iter iters/s
21 // ============================================================================
22 // HTTPCommonHeadersHash 3.50us 285.82K
23 // HTTPCommonHeadersGetHeaderCodeFromTableCommonHeaderName 161.70ns 6.18M
24 // memchr 1.02us 976.02K
25 // stdFind 5.59us 178.94K
26 // ============================================================================
27 
28 namespace {
29 
30 std::vector<HTTPHeaderCode> getTestHeaderCodes() {
31  std::vector<HTTPHeaderCode> testHeaderCodes;
33  j < HTTPCommonHeaders::num_header_codes; ++j) {
34  testHeaderCodes.push_back(static_cast<HTTPHeaderCode>(j));
35  }
36  return testHeaderCodes;
37 }
38 
39 std::vector<const std::string *> getTestHeaderStrings() {
40  std::vector<const std::string *> testHeaderStrings;
42  j < HTTPCommonHeaders::num_header_codes; ++j) {
43  testHeaderStrings.push_back(
44  HTTPCommonHeaders::getPointerToHeaderName(
45  static_cast<HTTPHeaderCode>(j)));
46  }
47  return testHeaderStrings;
48 }
49 
50 static const std::string* testHeaderNames =
51  HTTPCommonHeaders::getPointerToHeaderName(HTTP_HEADER_NONE);
52 
53 static const std::vector<HTTPHeaderCode> testHeaderCodes = getTestHeaderCodes();
54 
55 static const std::vector<const std::string *> testHeaderStrings =
56  getTestHeaderStrings();
57 
58 }
59 
60 void HTTPCommonHeadersHashBench(int iters) {
61  for (int i = 0; i < iters; ++i) {
62  for (auto const& testHeaderString: testHeaderStrings) {
63  HTTPCommonHeaders::hash(*testHeaderString);
64  }
65  }
66 }
67 
69  for (int i = 0; i < iters; ++i) {
71  j < HTTPCommonHeaders::num_header_codes; ++j) {
72  HTTPCommonHeaders::getHeaderCodeFromTableCommonHeaderName(
73  &testHeaderNames[j], TABLE_CAMELCASE);
74  }
75  }
76 }
77 
78 BENCHMARK(HTTPCommonHeadersHash, iters) {
80 }
81 
82 BENCHMARK(HTTPCommonHeadersGetHeaderCodeFromTableCommonHeaderName, iters) {
84 }
85 
86 void memchrBench(int iters) {
87  for (int i = 0; i < iters; ++i) {
89  j < HTTPCommonHeaders::num_header_codes; ++j) {
90  CHECK(
91  memchr(
92  (void*)testHeaderCodes.data(), static_cast<HTTPHeaderCode>(j),
93  testHeaderCodes.size()) != nullptr);
94  }
95  }
96 }
97 
98 void stdFindBench(int iters) {
99  for (int i = 0; i < iters; ++i) {
101  j < HTTPCommonHeaders::num_header_codes; ++j) {
102  auto address = HTTPCommonHeaders::getPointerToHeaderName(
103  static_cast<HTTPHeaderCode>(j));
104  CHECK(
105  std::find(
106  testHeaderStrings.begin(), testHeaderStrings.end(), address) !=
107  testHeaderStrings.end());
108  }
109  }
110 }
111 
112 BENCHMARK(memchr, iters) {
113  memchrBench(iters);
114 }
115 
116 BENCHMARK(stdFind, iters) {
117  stdFindBench(iters);
118 }
119 
120 int main(int argc, char** argv) {
121  gflags::ParseCommandLineFlags(&argc, &argv, true);
123  return 0;
124 }
void memchrBench(int iters)
void HTTPCommonHeadersGetHeaderCodeFromTableCommonHeaderNameBench(int iters)
int main(int argc, char **argv)
void stdFindBench(int iters)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void runBenchmarks()
Definition: Benchmark.cpp:456
void HTTPCommonHeadersHashBench(int iters)
char ** argv
BENCHMARK(fbFollyGlobalBenchmarkBaseline)
Definition: Benchmark.cpp:84
const char * string
Definition: Conv.cpp:212
const uint8_t HTTPHeaderCodeCommonOffset