main.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <library/cpp/http/io/headers.h>
  2. #include <benchmark/benchmark.h>
  3. #include <util/stream/str.h>
  4. void FindHeaderFirstMatch(benchmark::State& state) {
  5. THttpHeaders headers;
  6. headers.AddHeader("Host", "example.com");
  7. Y_ENSURE(headers.FindHeader("Host"));
  8. for (auto _ : state) {
  9. auto header = headers.FindHeader("Host");
  10. benchmark::DoNotOptimize(header);
  11. }
  12. }
  13. void FindHeaderNoMatchSameSize(benchmark::State& state) {
  14. THttpHeaders headers;
  15. for (char c = 'a'; c <= 'z'; ++c) {
  16. headers.AddHeader(TString::Join(c, "aaa"), "some value");
  17. }
  18. Y_ENSURE(!headers.FindHeader("Host"));
  19. for (auto _ : state) {
  20. auto header = headers.FindHeader("Host");
  21. benchmark::DoNotOptimize(header);
  22. }
  23. }
  24. void FindHeaderNoMatchDifferentSizesNoCommonPrefix(benchmark::State& state) {
  25. THttpHeaders headers;
  26. for (char c = 'a'; c <= 'z'; ++c) { // same number of headers as above
  27. headers.AddHeader("aaaaa", "some value");
  28. }
  29. Y_ENSURE(!headers.FindHeader("Host"));
  30. for (auto _ : state) {
  31. auto header = headers.FindHeader("Host");
  32. benchmark::DoNotOptimize(header);
  33. }
  34. }
  35. void FindHeaderNoMatchDifferentSizesCommonPrefix(benchmark::State& state) {
  36. THttpHeaders headers;
  37. for (char c = 'a'; c <= 'z'; ++c) {
  38. headers.AddHeader("Host2", "some value");
  39. }
  40. Y_ENSURE(!headers.FindHeader("Host"));
  41. for (auto _ : state) {
  42. auto header = headers.FindHeader("Host");
  43. benchmark::DoNotOptimize(header);
  44. }
  45. }
  46. void FindHeaderMoreRealisticUseCase(benchmark::State& state) {
  47. TString requestHeaders(R"(Host: yandex.ru
  48. User-Agent: Mozilla/5.0 ...
  49. Accept: */*
  50. Accept-Language: en-US,en;q=0.5
  51. Accept-Encoding: gzip, deflate, br
  52. Content-Type: text/plain;charset=UTF-8
  53. Content-Length: 1234
  54. Origin: https://a.yandex-team.ru
  55. Connection: keep-alive
  56. Referer: https://a.yandex-team.ru/
  57. Sec-Fetch-Dest: empty
  58. Sec-Fetch-Mode: no-cors
  59. Sec-Fetch-Site: cross-site
  60. TE: trailers)");
  61. TStringInput stream(requestHeaders);
  62. THttpHeaders headers(&stream);
  63. Y_ENSURE(headers.FindHeader("Content-Type"));
  64. for (auto _ : state) {
  65. auto header = headers.FindHeader("Content-Type");
  66. benchmark::DoNotOptimize(header);
  67. }
  68. }
  69. BENCHMARK(FindHeaderFirstMatch);
  70. BENCHMARK(FindHeaderNoMatchSameSize);
  71. BENCHMARK(FindHeaderNoMatchDifferentSizesNoCommonPrefix);
  72. BENCHMARK(FindHeaderNoMatchDifferentSizesCommonPrefix);
  73. BENCHMARK(FindHeaderMoreRealisticUseCase);