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