bench_utf8_checker.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #include <boost/beast/websocket/detail/utf8_checker.hpp>
  10. #include <boost/beast/_experimental/unit_test/suite.hpp>
  11. #include <chrono>
  12. #include <random>
  13. #ifndef BEAST_USE_BOOST_LOCALE_BENCHMARK
  14. #define BEAST_USE_BOOST_LOCALE_BENCHMARK 0
  15. #endif
  16. #if BEAST_USE_BOOST_LOCALE_BENCHMARK
  17. #include <boost/locale.hpp>
  18. #endif
  19. namespace boost {
  20. namespace beast {
  21. class utf8_checker_test : public beast::unit_test::suite
  22. {
  23. std::mt19937 rng_;
  24. public:
  25. using size_type = std::uint64_t;
  26. class timer
  27. {
  28. public:
  29. using clock_type =
  30. std::chrono::system_clock;
  31. private:
  32. clock_type::time_point when_;
  33. public:
  34. using duration =
  35. clock_type::duration;
  36. timer()
  37. : when_(clock_type::now())
  38. {
  39. }
  40. duration
  41. elapsed() const
  42. {
  43. return clock_type::now() - when_;
  44. }
  45. };
  46. template<class UInt = std::size_t>
  47. UInt
  48. rand(std::size_t n)
  49. {
  50. return static_cast<UInt>(
  51. std::uniform_int_distribution<
  52. std::size_t>{0, n-1}(rng_));
  53. }
  54. static
  55. inline
  56. size_type
  57. throughput(std::chrono::duration<
  58. double> const& elapsed, size_type items)
  59. {
  60. using namespace std::chrono;
  61. return static_cast<size_type>(
  62. 1 / (elapsed/items).count());
  63. }
  64. std::string
  65. corpus(std::size_t n)
  66. {
  67. std::string s;
  68. s.reserve(n);
  69. while(n--)
  70. s.push_back(static_cast<char>(
  71. ' ' + rand(95)));
  72. return s;
  73. }
  74. void
  75. checkBeast(std::string const& s)
  76. {
  77. beast::websocket::detail::check_utf8(
  78. s.data(), s.size());
  79. }
  80. #if BEAST_USE_BOOST_LOCALE_BENCHMARK
  81. void
  82. checkLocale(std::string const& s)
  83. {
  84. using namespace boost::locale;
  85. auto p = s.begin();
  86. auto const e = s.end();
  87. while(p != e)
  88. {
  89. auto cp = utf::utf_traits<char>::decode(p, e);
  90. if(cp == utf::illegal)
  91. break;
  92. }
  93. }
  94. #endif
  95. template<class F>
  96. typename timer::clock_type::duration
  97. test(F const& f)
  98. {
  99. timer t;
  100. f();
  101. return t.elapsed();
  102. }
  103. void
  104. run() override
  105. {
  106. auto const s = corpus(32 * 1024 * 1024);
  107. for(int i = 0; i < 5; ++ i)
  108. {
  109. auto const elapsed = test([&]{
  110. checkBeast(s);
  111. checkBeast(s);
  112. checkBeast(s);
  113. checkBeast(s);
  114. checkBeast(s);
  115. });
  116. log << "beast: " << throughput(elapsed, s.size()) << " char/s" << std::endl;
  117. }
  118. #if BEAST_USE_BOOST_LOCALE_BENCHMARK
  119. for(int i = 0; i < 5; ++ i)
  120. {
  121. auto const elapsed = test([&]{
  122. checkLocale(s);
  123. checkLocale(s);
  124. checkLocale(s);
  125. checkLocale(s);
  126. checkLocale(s);
  127. });
  128. log << "locale: " << throughput(elapsed, s.size()) << " char/s" << std::endl;
  129. }
  130. #endif
  131. pass();
  132. }
  133. };
  134. BEAST_DEFINE_TESTSUITE(beast,benchmarks,utf8_checker);
  135. } // beast
  136. } // boost