[benchmark] Fix static initialization bug in bm_huffman_decode (#32245)

* [benchmark] Fix static initialization bug in bm_huffman_decode

* fix performance
pull/32241/head
Craig Tiller 2 years ago committed by GitHub
parent bbeb15006a
commit 32be2c2179
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 43
      test/cpp/microbenchmarks/bm_huffman_decode.cc

@ -19,34 +19,38 @@
#include "src/core/ext/transport/chttp2/transport/bin_encoder.h" #include "src/core/ext/transport/chttp2/transport/bin_encoder.h"
#include "src/core/ext/transport/chttp2/transport/decode_huff.h" #include "src/core/ext/transport/chttp2/transport/decode_huff.h"
#include "src/core/lib/gprpp/no_destruct.h"
#include "src/core/lib/slice/slice.h" #include "src/core/lib/slice/slice.h"
#include "test/core/util/test_config.h" #include "test/core/util/test_config.h"
static const std::vector<uint8_t>* kInput = [] { const std::vector<uint8_t>* Input() {
std::vector<uint8_t> v; static const grpc_core::NoDestruct<std::vector<uint8_t>> v([]() {
std::mt19937 rd(0); std::vector<uint8_t> v;
std::uniform_int_distribution<> dist_ty(0, 100); std::mt19937 rd(0);
std::uniform_int_distribution<> dist_byte(0, 255); std::uniform_int_distribution<> dist_ty(0, 100);
std::uniform_int_distribution<> dist_normal(32, 126); std::uniform_int_distribution<> dist_byte(0, 255);
for (int i = 0; i < 1024 * 1024; i++) { std::uniform_int_distribution<> dist_normal(32, 126);
if (dist_ty(rd) == 1) { for (int i = 0; i < 1024 * 1024; i++) {
v.push_back(dist_byte(rd)); if (dist_ty(rd) == 1) {
} else { v.push_back(dist_byte(rd));
v.push_back(dist_normal(rd)); } else {
v.push_back(dist_normal(rd));
}
} }
} grpc_core::Slice s = grpc_core::Slice::FromCopiedBuffer(v);
grpc_core::Slice s = grpc_core::Slice::FromCopiedBuffer(v); grpc_core::Slice c(grpc_chttp2_huffman_compress(s.c_slice()));
grpc_core::Slice c(grpc_chttp2_huffman_compress(s.c_slice())); return std::vector<uint8_t>(c.begin(), c.end());
return new std::vector<uint8_t>(c.begin(), c.end()); }());
}(); return v.get();
}
static void BM_Decode(benchmark::State& state) { static void BM_Decode(benchmark::State& state) {
std::vector<uint8_t> output; std::vector<uint8_t> output;
auto add = [&output](uint8_t c) { output.push_back(c); }; auto add = [&output](uint8_t c) { output.push_back(c); };
for (auto _ : state) { for (auto _ : state) {
output.clear(); output.clear();
grpc_core::HuffDecoder<decltype(add)>(add, kInput->data(), grpc_core::HuffDecoder<decltype(add)>(add, Input()->data(),
kInput->data() + kInput->size()) Input()->data() + Input()->size())
.Run(); .Run();
} }
} }
@ -446,7 +450,7 @@ static void BM_LegacyDecode(benchmark::State& state) {
for (auto _ : state) { for (auto _ : state) {
output.clear(); output.clear();
decode_state = 0; decode_state = 0;
for (auto c : *kInput) { for (auto c : *Input()) {
nibble(c >> 4); nibble(c >> 4);
nibble(c & 0xf); nibble(c & 0xf);
} }
@ -463,6 +467,7 @@ void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
int main(int argc, char** argv) { int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(&argc, argv); grpc::testing::TestEnvironment env(&argc, argv);
benchmark::Initialize(&argc, argv); benchmark::Initialize(&argc, argv);
Input(); // Force initialization of input data.
benchmark::RunTheBenchmarksNamespaced(); benchmark::RunTheBenchmarksNamespaced();
return 0; return 0;
} }

Loading…
Cancel
Save