Skip to content

Commit

Permalink
Add benchmark for absl::HexStringToBytes
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 692960029
Change-Id: I98252f2de95a237622ccf16f200bbb16dc441d88
  • Loading branch information
derekmauro authored and copybara-github committed Nov 4, 2024
1 parent 85c26be commit 8596c6e
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions absl/strings/escaping_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,36 @@ void BM_WebSafeBase64Escape_string(benchmark::State& state) {
}
BENCHMARK(BM_WebSafeBase64Escape_string);

void BM_HexStringToBytes(benchmark::State& state) {
const int size = state.range(0);
std::string input, output;
for (int i = 0; i < size; ++i) input += "1c";
for (auto _ : state) {
benchmark::DoNotOptimize(input);
bool result = absl::HexStringToBytes(input, &output);
benchmark::DoNotOptimize(result);
benchmark::DoNotOptimize(output);
}
}
BENCHMARK(BM_HexStringToBytes)->Range(1, 1 << 8);

void BM_HexStringToBytes_Fail(benchmark::State& state) {
std::string binary;
absl::string_view hex_input1 = "1c2f003";
absl::string_view hex_input2 = "1c2f0032f40123456789abcdef**";
for (auto _ : state) {
benchmark::DoNotOptimize(hex_input1);
bool result1 = absl::HexStringToBytes(hex_input1, &binary);
benchmark::DoNotOptimize(result1);
benchmark::DoNotOptimize(binary);
benchmark::DoNotOptimize(hex_input2);
bool result2 = absl::HexStringToBytes(hex_input2, &binary);
benchmark::DoNotOptimize(result2);
benchmark::DoNotOptimize(binary);
}
}
BENCHMARK(BM_HexStringToBytes_Fail);

// Used for the CEscape benchmarks
const char kStringValueNoEscape[] = "1234567890";
const char kStringValueSomeEscaped[] = "123\n56789\xA1";
Expand Down

0 comments on commit 8596c6e

Please sign in to comment.