xref: /aosp_15_r20/external/abseil-cpp/absl/strings/escaping_benchmark.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
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 <cstdio>
19 #include <cstring>
20 #include <memory>
21 #include <random>
22 #include <string>
23 
24 #include "benchmark/benchmark.h"
25 #include "absl/base/internal/raw_logging.h"
26 #include "absl/strings/internal/escaping_test_common.h"
27 #include "absl/strings/str_cat.h"
28 
29 namespace {
30 
BM_CUnescapeHexString(benchmark::State & state)31 void BM_CUnescapeHexString(benchmark::State& state) {
32   std::string src;
33   for (int i = 0; i < 50; i++) {
34     src += "\\x55";
35   }
36   std::string dest;
37   for (auto _ : state) {
38     absl::CUnescape(src, &dest);
39   }
40 }
41 BENCHMARK(BM_CUnescapeHexString);
42 
BM_WebSafeBase64Escape_string(benchmark::State & state)43 void BM_WebSafeBase64Escape_string(benchmark::State& state) {
44   std::string raw;
45   for (int i = 0; i < 10; ++i) {
46     for (const auto& test_set : absl::strings_internal::base64_strings()) {
47       raw += std::string(test_set.plaintext);
48     }
49   }
50 
51   // The actual benchmark loop is tiny...
52   std::string escaped;
53   for (auto _ : state) {
54     absl::WebSafeBase64Escape(raw, &escaped);
55   }
56 
57   // We want to be sure the compiler doesn't throw away the loop above,
58   // and the easiest way to ensure that is to round-trip the results and verify
59   // them.
60   std::string round_trip;
61   absl::WebSafeBase64Unescape(escaped, &round_trip);
62   ABSL_RAW_CHECK(round_trip == raw, "");
63 }
64 BENCHMARK(BM_WebSafeBase64Escape_string);
65 
66 // Used for the CEscape benchmarks
67 const char kStringValueNoEscape[] = "1234567890";
68 const char kStringValueSomeEscaped[] = "123\n56789\xA1";
69 const char kStringValueMostEscaped[] = "\xA1\xA2\ny\xA4\xA5\xA6z\b\r";
70 
CEscapeBenchmarkHelper(benchmark::State & state,const char * string_value,int max_len)71 void CEscapeBenchmarkHelper(benchmark::State& state, const char* string_value,
72                             int max_len) {
73   std::string src;
74   while (src.size() < max_len) {
75     absl::StrAppend(&src, string_value);
76   }
77 
78   for (auto _ : state) {
79     absl::CEscape(src);
80   }
81 }
82 
BM_CEscape_NoEscape(benchmark::State & state)83 void BM_CEscape_NoEscape(benchmark::State& state) {
84   CEscapeBenchmarkHelper(state, kStringValueNoEscape, state.range(0));
85 }
86 BENCHMARK(BM_CEscape_NoEscape)->Range(1, 1 << 14);
87 
BM_CEscape_SomeEscaped(benchmark::State & state)88 void BM_CEscape_SomeEscaped(benchmark::State& state) {
89   CEscapeBenchmarkHelper(state, kStringValueSomeEscaped, state.range(0));
90 }
91 BENCHMARK(BM_CEscape_SomeEscaped)->Range(1, 1 << 14);
92 
BM_CEscape_MostEscaped(benchmark::State & state)93 void BM_CEscape_MostEscaped(benchmark::State& state) {
94   CEscapeBenchmarkHelper(state, kStringValueMostEscaped, state.range(0));
95 }
96 BENCHMARK(BM_CEscape_MostEscaped)->Range(1, 1 << 14);
97 
98 }  // namespace
99