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 <benchmark/benchmark.h>
16
17 #include <cstdint>
18 #include <random>
19
20 #include "absl/strings/escaping.h"
21 #include "src/core/ext/transport/chttp2/transport/bin_encoder.h"
22 #include "src/core/ext/transport/chttp2/transport/decode_huff.h"
23 #include "src/core/lib/slice/slice.h"
24 #include "src/core/util/no_destruct.h"
25 #include "test/core/test_util/test_config.h"
26 #include "test/cpp/microbenchmarks/huffman_geometries/index.h"
27
MakeInput(int min,int max)28 std::vector<uint8_t> MakeInput(int min, int max) {
29 std::vector<uint8_t> v;
30 std::uniform_int_distribution<> distribution(min, max);
31 static std::mt19937 rd(0);
32 v.reserve(1024 * 1024);
33 for (int i = 0; i < 1024 * 1024; i++) {
34 v.push_back(distribution(rd));
35 }
36 grpc_core::Slice s = grpc_core::Slice::FromCopiedBuffer(v);
37 grpc_core::Slice c(grpc_chttp2_huffman_compress(s.c_slice()));
38 return std::vector<uint8_t>(c.begin(), c.end());
39 }
40
MakeBase64()41 std::vector<uint8_t> MakeBase64() {
42 auto src = MakeInput(0, 255);
43 auto s = absl::Base64Escape(
44 absl::string_view(reinterpret_cast<char*>(src.data()), src.size()));
45 return std::vector<uint8_t>(s.begin(), s.end());
46 }
47
AllChars()48 const std::vector<uint8_t>& AllChars() {
49 static const auto* const data = new std::vector<uint8_t>(MakeInput(0, 255));
50 return *data;
51 };
AsciiChars()52 const std::vector<uint8_t>& AsciiChars() {
53 static const auto* const data = new std::vector<uint8_t>(MakeInput(32, 126));
54 return *data;
55 };
AlphaChars()56 const std::vector<uint8_t>& AlphaChars() {
57 static const auto* const data = new std::vector<uint8_t>(MakeInput('a', 'z'));
58 return *data;
59 };
Base64Chars()60 const std::vector<uint8_t>& Base64Chars() {
61 static const auto* const data = new std::vector<uint8_t>(MakeBase64());
62 return *data;
63 };
64
65 using CharSet = const std::vector<uint8_t>& (*)();
66
67 template <template <typename Sink> class Decoder>
BM_Decode(benchmark::State & state,CharSet chars_gen)68 static void BM_Decode(benchmark::State& state, CharSet chars_gen) {
69 const std::vector<uint8_t>& chars = chars_gen();
70 std::vector<uint8_t> output;
71 auto add = [&output](uint8_t c) { output.push_back(c); };
72 for (auto _ : state) {
73 output.clear();
74 Decoder<decltype(add)>(add, chars.data(), chars.data() + chars.size())
75 .Run();
76 }
77 }
78
79 #define DECL_BENCHMARK(cls, name) \
80 static auto name = BM_Decode<cls>; \
81 BENCHMARK_CAPTURE(name, all_chars, AllChars); \
82 BENCHMARK_CAPTURE(name, base64_chars, Base64Chars); \
83 BENCHMARK_CAPTURE(name, ascii_chars, AsciiChars); \
84 BENCHMARK_CAPTURE(name, alpha_chars, AlphaChars)
85
86 DECL_HUFFMAN_VARIANTS();
87
88 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
89 // and others do not. This allows us to support both modes.
90 namespace benchmark {
RunTheBenchmarksNamespaced()91 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
92 } // namespace benchmark
93
main(int argc,char ** argv)94 int main(int argc, char** argv) {
95 grpc::testing::TestEnvironment env(&argc, argv);
96 benchmark::Initialize(&argc, argv);
97 benchmark::RunTheBenchmarksNamespaced();
98 return 0;
99 }
100