Add Huffman encoder benchmarks.

PiperOrigin-RevId: 334497445
Change-Id: I98b38429c02dabdfb2844f98fae22fc1183383ed
diff --git a/http2/hpack/huffman/hpack_huffman_encoder_benchmark.cc b/http2/hpack/huffman/hpack_huffman_encoder_benchmark.cc
new file mode 100644
index 0000000..1f365c6
--- /dev/null
+++ b/http2/hpack/huffman/hpack_huffman_encoder_benchmark.cc
@@ -0,0 +1,60 @@
+// Copyright 2020 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+//
+// $ blaze run -c opt --dynamic_mode=off \
+//     -- //net/third_party/quiche/src/http2/hpack/huffman:hpack_huffman_encoder_benchmark \
+//     --benchmarks=all --benchmark_memory_usage --benchmark_repetitions=1
+//
+// Benchmark                  Time(ns)  CPU(ns) Allocs Iterations
+// -----------------------------------------------------------------------------
+// BM_EncodeSmallStrings           239       239    0 2456085   0.000B  peak-mem
+// BM_EncodeLargeString/1k        4560      4561    5  153325   1.125kB peak-mem
+// BM_EncodeLargeString/4k       18787     18788    7   38430   4.500kB peak-mem
+// BM_EncodeLargeString/32k     147680    147657   10    4664  36.000kB peak-mem
+// BM_EncodeLargeString/256k   1161688   1161511   13     601 288.000kB peak-mem
+// BM_EncodeLargeString/2M    10042722  10036764   16      75   2.250MB peak-mem
+// BM_EncodeLargeString/16M   76127338  76138839   19       9  18.000MB peak-mem
+// BM_EncodeLargeString/128M 640008098 640154859   22       1 144.000MB peak-mem
+//
+
+#include <string>
+
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Weverything"
+// This header has multiple DCHECK_* macros with signed-unsigned comparison.
+#include "testing/base/public/benchmark.h"
+#pragma clang diagnostic pop
+
+#include "net/third_party/quiche/src/http2/hpack/huffman/hpack_huffman_encoder.h"
+
+namespace http2 {
+namespace {
+
+void BM_EncodeSmallStrings(benchmark::State& state) {
+  const std::vector<const std::string> inputs{
+      ":method", ":path", "cookie", "set-cookie", "vary", "accept-encoding"};
+  for (auto s : state) {
+    for (const auto& input : inputs) {
+      std::string result;
+      ExactHuffmanSize(input);
+      HuffmanEncode(input, &result);
+    }
+  }
+}
+
+void BM_EncodeLargeString(benchmark::State& state) {
+  const std::string input(state.range(0), 'a');
+  for (auto s : state) {
+    std::string result;
+    ExactHuffmanSize(input);
+    HuffmanEncode(input, &result);
+  }
+}
+
+BENCHMARK(BM_EncodeSmallStrings);
+BENCHMARK(BM_EncodeLargeString)->Range(1024, 128 * 1024 * 1024);
+
+}  // namespace
+}  // namespace http2
diff --git a/spdy/core/hpack/hpack_huffman_table_benchmark.cc b/spdy/core/hpack/hpack_huffman_table_benchmark.cc
new file mode 100644
index 0000000..aeea726
--- /dev/null
+++ b/spdy/core/hpack/hpack_huffman_table_benchmark.cc
@@ -0,0 +1,64 @@
+// Copyright 2020 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+//
+// $ blaze run -c opt --dynamic_mode=off \
+//     -- //net/third_party/quiche/src/spdy/core/hpack:hpack_huffman_table_benchmark \
+//     --benchmarks=all --benchmark_memory_usage --benchmark_repetitions=1
+//
+// Benchmark                   Time(ns)  CPU(ns) Allocs Iterations
+// -----------------------------------------------------------------------------
+// BM_EncodeSmallStrings            463        441  0  100000   0.000B  peak-mem
+// BM_EncodeLargeString/1k         9003       9069  5    4861   1.125kB peak-mem
+// BM_EncodeLargeString/4k        34808      35157  7    1597   4.500kB peak-mem
+// BM_EncodeLargeString/32k      275973     270741 10     207  36.000kB peak-mem
+// BM_EncodeLargeString/256k    2234748    2236850 13      29 288.000kB peak-mem
+// BM_EncodeLargeString/2M     18248449   18717995 16       3   2.250MB peak-mem
+// BM_EncodeLargeString/16M   144944895  144415061 19       1  18.000MB peak-mem
+// BM_EncodeLargeString/128M 1200907841 1207238809 86       1 144.009MB peak-mem
+//
+
+#include <string>
+
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Weverything"
+// This header has multiple DCHECK_* macros with signed-unsigned comparison.
+#include "testing/base/public/benchmark.h"
+#pragma clang diagnostic pop
+
+#include "net/third_party/quiche/src/spdy/core/hpack/hpack_constants.h"
+#include "net/third_party/quiche/src/spdy/core/hpack/hpack_huffman_table.h"
+#include "net/third_party/quiche/src/spdy/core/hpack/hpack_output_stream.h"
+
+namespace spdy {
+namespace {
+
+void BM_EncodeSmallStrings(benchmark::State& state) {
+  const HpackHuffmanTable& table = ObtainHpackHuffmanTable();
+  const std::vector<const std::string> inputs{
+      ":method", ":path", "cookie", "set-cookie", "vary", "accept-encoding"};
+  for (auto s : state) {
+    for (const auto& input : inputs) {
+      HpackOutputStream output_stream;
+      table.EncodedSize(input);
+      table.EncodeString(input, &output_stream);
+    }
+  }
+}
+
+void BM_EncodeLargeString(benchmark::State& state) {
+  const HpackHuffmanTable& table = ObtainHpackHuffmanTable();
+  const std::string input(state.range(0), 'a');
+  for (auto s : state) {
+    HpackOutputStream output_stream;
+    table.EncodedSize(input);
+    table.EncodeString(input, &output_stream);
+  }
+}
+
+BENCHMARK(BM_EncodeSmallStrings);
+BENCHMARK(BM_EncodeLargeString)->Range(1024, 128 * 1024 * 1024);
+
+}  // namespace
+}  // namespace spdy