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