Change Huffman encoder benchmark to typical character distribution.
Reimplement a Huffman decoder and run it on random binary input to generate
characters from the ideal distribution corresponding to the Huffman code.
This is much closer to real life usage than repeated 'a' characters, giving more
meaningful benchmark results.
PiperOrigin-RevId: 335674758
Change-Id: I7f90439066691b45fea693715091f70b15628920
diff --git a/http2/hpack/huffman/hpack_huffman_encoder_benchmark.cc b/http2/hpack/huffman/hpack_huffman_encoder_benchmark.cc
deleted file mode 100644
index 370eb72..0000000
--- a/http2/hpack/huffman/hpack_huffman_encoder_benchmark.cc
+++ /dev/null
@@ -1,129 +0,0 @@
-// 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 255 255 0 2557738 0.000B peak-mem
-// BM_E.SmallStringsFast 247 247 0 2849201 0.000B peak-mem
-// BM_EncodeLargeString/1k 4270 4271 1 166430 656.000B peak-mem
-// BM_EncodeLargeString/4k 16819 16820 1 40483 2.516kB peak-mem
-// BM_EncodeLargeString/32k 135426 135416 1 5155 20.016kB peak-mem
-// BM_EncodeLargeString/256k 1080893 1080922 1 647 160.016kB peak-mem
-// BM_EncodeLargeString/2M 8698878 8699261 1 77 1.250MB peak-mem
-// BM_EncodeLargeString/16M 70013626 70009631 1 10 10.000MB peak-mem
-// BM_EncodeLargeString/128M 581697663 581739687 1 1 80.000MB peak-mem
-// BM_E.LargeStringFast/1k 3820 3820 1 184203 656.000B peak-mem
-// BM_E.LargeStringFast/4k 15148 15146 1 46341 2.516kB peak-mem
-// BM_E.LargeStringFast/32k 120409 120426 1 5803 20.016kB peak-mem
-// BM_E.LargeStringFast/256k 968802 968841 1 725 160.016kB peak-mem
-// BM_E.LargeStringFast/2M 7769441 7767875 1 90 1.250MB peak-mem
-// BM_E.LargeStringFast/16M 62571561 62581958 1 10 10.000MB peak-mem
-// BM_E.LargeStringFast/128M 527393576 527376986 1 1 80.000MB peak-mem
-// BM_EncodeLongCode/1k 15197 15200 1 45281 3.766kB peak-mem
-// BM_EncodeLongCode/4k 60782 60775 1 10000 15.016kB peak-mem
-// BM_EncodeLongCode/32k 489516 489692 1 1441 120.016kB peak-mem
-// BM_EncodeLongCode/256k 3902949 3905536 1 179 960.016kB peak-mem
-// BM_EncodeLongCode/2M 31275026 31281987 1 23 7.500MB peak-mem
-// BM_EncodeLongCode/16M 258391322 258361112 1 3 60.000MB peak-mem
-// BM_EncodeLongCode/128M 2098369854 2098398258 1 1 480.000MB peak-mem
-// BM_E.LongCodeFast/1k 3915 3915 1 179602 3.766kB peak-mem
-// BM_E.LongCodeFast/4k 15433 15435 1 45491 15.016kB peak-mem
-// BM_E.LongCodeFast/32k 124756 124750 1 5689 120.016kB peak-mem
-// BM_E.LongCodeFast/256k 1007523 1008291 1 700 960.016kB peak-mem
-// BM_E.LongCodeFast/2M 8129166 8132764 1 87 7.500MB peak-mem
-// BM_E.LongCodeFast/16M 72076964 72079949 1 9 60.000MB peak-mem
-// BM_E.LongCodeFast/128M 631963502 631948474 1 1 480.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) {
- size_t encoded_size = HuffmanSize(input);
- std::string result;
- HuffmanEncode(input, encoded_size, &result);
- }
- }
-}
-
-void BM_EncodeSmallStringsFast(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) {
- size_t encoded_size = HuffmanSize(input);
- std::string result;
- HuffmanEncodeFast(input, encoded_size, &result);
- }
- }
-}
-
-void BM_EncodeLargeString(benchmark::State& state) {
- const std::string input(state.range(0), 'a');
- for (auto s : state) {
- size_t encoded_size = HuffmanSize(input);
- std::string result;
- HuffmanEncode(input, encoded_size, &result);
- }
-}
-
-void BM_EncodeLargeStringFast(benchmark::State& state) {
- const std::string input(state.range(0), 'a');
- for (auto s : state) {
- size_t encoded_size = HuffmanSize(input);
- std::string result;
- HuffmanEncodeFast(input, encoded_size, &result);
- }
-}
-
-// 13 is one of the characters with the longest encoding: 30 bits.
-// This will never be run in production, because HuffmanEncode is only called on
-// strings that become shorter when encoded, but it gives an idea of compression
-// speed when many characters in the input are encoded with long codes.
-void BM_EncodeLongCode(benchmark::State& state) {
- const std::string input(state.range(0), 13);
- for (auto s : state) {
- size_t encoded_size = HuffmanSize(input);
- std::string result;
- HuffmanEncode(input, encoded_size, &result);
- }
-}
-
-void BM_EncodeLongCodeFast(benchmark::State& state) {
- const std::string input(state.range(0), 13);
- for (auto s : state) {
- size_t encoded_size = HuffmanSize(input);
- std::string result;
- HuffmanEncodeFast(input, encoded_size, &result);
- }
-}
-
-BENCHMARK(BM_EncodeSmallStrings);
-BENCHMARK(BM_EncodeLargeString)->Range(1024, 128 * 1024 * 1024);
-BENCHMARK(BM_EncodeLongCode)->Range(1024, 128 * 1024 * 1024);
-BENCHMARK(BM_EncodeSmallStringsFast);
-BENCHMARK(BM_EncodeLargeStringFast)->Range(1024, 128 * 1024 * 1024);
-BENCHMARK(BM_EncodeLongCodeFast)->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
deleted file mode 100644
index aeea726..0000000
--- a/spdy/core/hpack/hpack_huffman_table_benchmark.cc
+++ /dev/null
@@ -1,64 +0,0 @@
-// 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