escaping_benchmark.cc
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/strings/escaping.h"
00016 
00017 #include <cstdio>
00018 #include <cstring>
00019 #include <random>
00020 
00021 #include "benchmark/benchmark.h"
00022 #include "absl/base/internal/raw_logging.h"
00023 #include "absl/strings/internal/escaping_test_common.h"
00024 
00025 namespace {
00026 
00027 void BM_CUnescapeHexString(benchmark::State& state) {
00028   std::string src;
00029   for (int i = 0; i < 50; i++) {
00030     src += "\\x55";
00031   }
00032   std::string dest;
00033   for (auto _ : state) {
00034     absl::CUnescape(src, &dest);
00035   }
00036 }
00037 BENCHMARK(BM_CUnescapeHexString);
00038 
00039 void BM_WebSafeBase64Escape_string(benchmark::State& state) {
00040   std::string raw;
00041   for (int i = 0; i < 10; ++i) {
00042     for (const auto& test_set : absl::strings_internal::base64_strings()) {
00043       raw += std::string(test_set.plaintext);
00044     }
00045   }
00046 
00047   // The actual benchmark loop is tiny...
00048   std::string escaped;
00049   for (auto _ : state) {
00050     absl::WebSafeBase64Escape(raw, &escaped);
00051   }
00052 
00053   // We want to be sure the compiler doesn't throw away the loop above,
00054   // and the easiest way to ensure that is to round-trip the results and verify
00055   // them.
00056   std::string round_trip;
00057   absl::WebSafeBase64Unescape(escaped, &round_trip);
00058   ABSL_RAW_CHECK(round_trip == raw, "");
00059 }
00060 BENCHMARK(BM_WebSafeBase64Escape_string);
00061 
00062 // Used for the CEscape benchmarks
00063 const char kStringValueNoEscape[] = "1234567890";
00064 const char kStringValueSomeEscaped[] = "123\n56789\xA1";
00065 const char kStringValueMostEscaped[] = "\xA1\xA2\ny\xA4\xA5\xA6z\b\r";
00066 
00067 void CEscapeBenchmarkHelper(benchmark::State& state, const char* string_value,
00068                             int max_len) {
00069   std::string src;
00070   while (src.size() < max_len) {
00071     absl::StrAppend(&src, string_value);
00072   }
00073 
00074   for (auto _ : state) {
00075     absl::CEscape(src);
00076   }
00077 }
00078 
00079 void BM_CEscape_NoEscape(benchmark::State& state) {
00080   CEscapeBenchmarkHelper(state, kStringValueNoEscape, state.range(0));
00081 }
00082 BENCHMARK(BM_CEscape_NoEscape)->Range(1, 1 << 14);
00083 
00084 void BM_CEscape_SomeEscaped(benchmark::State& state) {
00085   CEscapeBenchmarkHelper(state, kStringValueSomeEscaped, state.range(0));
00086 }
00087 BENCHMARK(BM_CEscape_SomeEscaped)->Range(1, 1 << 14);
00088 
00089 void BM_CEscape_MostEscaped(benchmark::State& state) {
00090   CEscapeBenchmarkHelper(state, kStringValueMostEscaped, state.range(0));
00091 }
00092 BENCHMARK(BM_CEscape_MostEscaped)->Range(1, 1 << 14);
00093 
00094 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14