escaping_benchmark.cc 2.8 KB

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