escaping_benchmark.cc (3955B)
1 // Copyright 2018 The Abseil 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 // https://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 <memory> 17 #include <random> 18 #include <string> 19 20 #include "absl/base/internal/raw_logging.h" 21 #include "absl/strings/escaping.h" 22 #include "absl/strings/internal/escaping_test_common.h" 23 #include "absl/strings/str_cat.h" 24 #include "absl/strings/string_view.h" 25 #include "benchmark/benchmark.h" 26 27 namespace { 28 29 void BM_CUnescapeHexString(benchmark::State& state) { 30 std::string src; 31 for (int i = 0; i < 50; i++) { 32 src += "\\x55"; 33 } 34 for (auto _ : state) { 35 std::string dest; 36 benchmark::DoNotOptimize(src); 37 bool result = absl::CUnescape(src, &dest); 38 benchmark::DoNotOptimize(result); 39 benchmark::DoNotOptimize(dest); 40 } 41 } 42 BENCHMARK(BM_CUnescapeHexString); 43 44 void BM_WebSafeBase64Escape_string(benchmark::State& state) { 45 std::string raw; 46 for (int i = 0; i < 10; ++i) { 47 for (const auto& test_set : absl::strings_internal::base64_strings()) { 48 raw += std::string(test_set.plaintext); 49 } 50 } 51 for (auto _ : state) { 52 std::string escaped; 53 benchmark::DoNotOptimize(raw); 54 absl::WebSafeBase64Escape(raw, &escaped); 55 benchmark::DoNotOptimize(escaped); 56 } 57 } 58 BENCHMARK(BM_WebSafeBase64Escape_string); 59 60 void BM_HexStringToBytes(benchmark::State& state) { 61 const int size = state.range(0); 62 std::string input, output; 63 for (int i = 0; i < size; ++i) input += "1c"; 64 for (auto _ : state) { 65 benchmark::DoNotOptimize(input); 66 bool result = absl::HexStringToBytes(input, &output); 67 benchmark::DoNotOptimize(result); 68 benchmark::DoNotOptimize(output); 69 } 70 } 71 BENCHMARK(BM_HexStringToBytes)->Range(1, 1 << 8); 72 73 void BM_HexStringToBytes_Fail(benchmark::State& state) { 74 std::string binary; 75 absl::string_view hex_input1 = "1c2f003"; 76 absl::string_view hex_input2 = "1c2f0032f40123456789abcdef**"; 77 for (auto _ : state) { 78 benchmark::DoNotOptimize(hex_input1); 79 bool result1 = absl::HexStringToBytes(hex_input1, &binary); 80 benchmark::DoNotOptimize(result1); 81 benchmark::DoNotOptimize(binary); 82 benchmark::DoNotOptimize(hex_input2); 83 bool result2 = absl::HexStringToBytes(hex_input2, &binary); 84 benchmark::DoNotOptimize(result2); 85 benchmark::DoNotOptimize(binary); 86 } 87 } 88 BENCHMARK(BM_HexStringToBytes_Fail); 89 90 // Used for the CEscape benchmarks 91 const char kStringValueNoEscape[] = "1234567890"; 92 const char kStringValueSomeEscaped[] = "123\n56789\xA1"; 93 const char kStringValueMostEscaped[] = "\xA1\xA2\ny\xA4\xA5\xA6z\b\r"; 94 95 void CEscapeBenchmarkHelper(benchmark::State& state, const char* string_value, 96 int max_len) { 97 std::string src; 98 while (src.size() < max_len) { 99 absl::StrAppend(&src, string_value); 100 } 101 102 for (auto _ : state) { 103 benchmark::DoNotOptimize(src); 104 std::string result = absl::CEscape(src); 105 benchmark::DoNotOptimize(result); 106 } 107 } 108 109 void BM_CEscape_NoEscape(benchmark::State& state) { 110 CEscapeBenchmarkHelper(state, kStringValueNoEscape, state.range(0)); 111 } 112 BENCHMARK(BM_CEscape_NoEscape)->Range(1, 1 << 14); 113 114 void BM_CEscape_SomeEscaped(benchmark::State& state) { 115 CEscapeBenchmarkHelper(state, kStringValueSomeEscaped, state.range(0)); 116 } 117 BENCHMARK(BM_CEscape_SomeEscaped)->Range(1, 1 << 14); 118 119 void BM_CEscape_MostEscaped(benchmark::State& state) { 120 CEscapeBenchmarkHelper(state, kStringValueMostEscaped, state.range(0)); 121 } 122 BENCHMARK(BM_CEscape_MostEscaped)->Range(1, 1 << 14); 123 124 } // namespace