xref: /aosp_15_r20/external/grpc-grpc/test/cpp/microbenchmarks/bm_huffman_decode.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <cstdint>
16 #include <random>
17 
18 #include <benchmark/benchmark.h>
19 
20 #include "absl/strings/escaping.h"
21 
22 #include "src/core/ext/transport/chttp2/transport/bin_encoder.h"
23 #include "src/core/ext/transport/chttp2/transport/decode_huff.h"
24 #include "src/core/lib/gprpp/no_destruct.h"
25 #include "src/core/lib/slice/slice.h"
26 #include "test/core/util/test_config.h"
27 #include "test/cpp/microbenchmarks/huffman_geometries/index.h"
28 
MakeInput(int min,int max)29 std::vector<uint8_t> MakeInput(int min, int max) {
30   std::vector<uint8_t> v;
31   std::uniform_int_distribution<> distribution(min, max);
32   static std::mt19937 rd(0);
33   v.reserve(1024 * 1024);
34   for (int i = 0; i < 1024 * 1024; i++) {
35     v.push_back(distribution(rd));
36   }
37   grpc_core::Slice s = grpc_core::Slice::FromCopiedBuffer(v);
38   grpc_core::Slice c(grpc_chttp2_huffman_compress(s.c_slice()));
39   return std::vector<uint8_t>(c.begin(), c.end());
40 }
41 
MakeBase64()42 std::vector<uint8_t> MakeBase64() {
43   auto src = MakeInput(0, 255);
44   auto s = absl::Base64Escape(
45       absl::string_view(reinterpret_cast<char*>(src.data()), src.size()));
46   return std::vector<uint8_t>(s.begin(), s.end());
47 }
48 
AllChars()49 const std::vector<uint8_t>& AllChars() {
50   static const auto* const data = new std::vector<uint8_t>(MakeInput(0, 255));
51   return *data;
52 };
AsciiChars()53 const std::vector<uint8_t>& AsciiChars() {
54   static const auto* const data = new std::vector<uint8_t>(MakeInput(32, 126));
55   return *data;
56 };
AlphaChars()57 const std::vector<uint8_t>& AlphaChars() {
58   static const auto* const data = new std::vector<uint8_t>(MakeInput('a', 'z'));
59   return *data;
60 };
Base64Chars()61 const std::vector<uint8_t>& Base64Chars() {
62   static const auto* const data = new std::vector<uint8_t>(MakeBase64());
63   return *data;
64 };
65 
66 using CharSet = const std::vector<uint8_t>& (*)();
67 
68 template <template <typename Sink> class Decoder>
BM_Decode(benchmark::State & state,CharSet chars_gen)69 static void BM_Decode(benchmark::State& state, CharSet chars_gen) {
70   const std::vector<uint8_t>& chars = chars_gen();
71   std::vector<uint8_t> output;
72   auto add = [&output](uint8_t c) { output.push_back(c); };
73   for (auto _ : state) {
74     output.clear();
75     Decoder<decltype(add)>(add, chars.data(), chars.data() + chars.size())
76         .Run();
77   }
78 }
79 
80 #define DECL_BENCHMARK(cls, name)                     \
81   static auto name = BM_Decode<cls>;                  \
82   BENCHMARK_CAPTURE(name, all_chars, AllChars);       \
83   BENCHMARK_CAPTURE(name, base64_chars, Base64Chars); \
84   BENCHMARK_CAPTURE(name, ascii_chars, AsciiChars);   \
85   BENCHMARK_CAPTURE(name, alpha_chars, AlphaChars)
86 
87 DECL_HUFFMAN_VARIANTS();
88 
89 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
90 // and others do not. This allows us to support both modes.
91 namespace benchmark {
RunTheBenchmarksNamespaced()92 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
93 }  // namespace benchmark
94 
main(int argc,char ** argv)95 int main(int argc, char** argv) {
96   grpc::testing::TestEnvironment env(&argc, argv);
97   benchmark::Initialize(&argc, argv);
98   benchmark::RunTheBenchmarksNamespaced();
99   return 0;
100 }
101