main.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include <library/cpp/string_utils/base64/base64.h>
  2. #include <library/cpp/testing/benchmark/bench.h>
  3. #include <util/generic/buffer.h>
  4. #include <util/generic/singleton.h>
  5. #include <util/generic/string.h>
  6. #include <util/generic/vector.h>
  7. #include <util/generic/xrange.h>
  8. #include <util/generic/yexception.h>
  9. #include <util/random/random.h>
  10. #include <array>
  11. static TString GenerateRandomData(const size_t minSize, const size_t maxSize) {
  12. Y_ENSURE(minSize <= maxSize, "wow");
  13. TString r;
  14. for (size_t i = 0; i < minSize; ++i) {
  15. r.push_back(RandomNumber<char>());
  16. }
  17. if (minSize == maxSize) {
  18. return r;
  19. }
  20. const size_t size = RandomNumber<size_t>() % (maxSize - minSize + 1);
  21. for (size_t i = 0; i < size; ++i) {
  22. r.push_back(RandomNumber<char>());
  23. }
  24. return r;
  25. }
  26. template <size_t N>
  27. static std::array<TString, N> GenerateRandomDataVector(const size_t minSize, const size_t maxSize) {
  28. std::array<TString, N> r;
  29. for (size_t i = 0; i < N; ++i) {
  30. r[i] = GenerateRandomData(minSize, maxSize);
  31. }
  32. return r;
  33. }
  34. template <size_t N>
  35. static std::array<TString, N> Encode(const std::array<TString, N>& d) {
  36. std::array<TString, N> r;
  37. for (size_t i = 0, iEnd = d.size(); i < iEnd; ++i) {
  38. r[i] = Base64Encode(d[i]);
  39. }
  40. return r;
  41. }
  42. namespace {
  43. template <size_t N, size_t MinSize, size_t MaxSize>
  44. struct TRandomDataHolder {
  45. TRandomDataHolder()
  46. : Data(GenerateRandomDataVector<N>(MinSize, MaxSize))
  47. , DataEncoded(Encode<N>(Data))
  48. {
  49. for (size_t i = 0; i < N; ++i) {
  50. const size_t size = Data[i].size();
  51. const size_t sizeEnc = DataEncoded[i].size();
  52. PlaceToEncode[i].Resize(Base64EncodeBufSize(size));
  53. PlaceToDecode[i].Resize(Base64DecodeBufSize(sizeEnc));
  54. }
  55. }
  56. static constexpr size_t Size = N;
  57. const std::array<TString, N> Data;
  58. const std::array<TString, N> DataEncoded;
  59. std::array<TBuffer, N> PlaceToEncode;
  60. std::array<TBuffer, N> PlaceToDecode;
  61. };
  62. template <size_t N, size_t Size>
  63. using TFixedSizeRandomDataHolder = TRandomDataHolder<N, Size, Size>;
  64. using FSRDH_1 = TFixedSizeRandomDataHolder<10, 1>;
  65. using FSRDH_2 = TFixedSizeRandomDataHolder<10, 2>;
  66. using FSRDH_4 = TFixedSizeRandomDataHolder<10, 4>;
  67. using FSRDH_8 = TFixedSizeRandomDataHolder<10, 8>;
  68. using FSRDH_16 = TFixedSizeRandomDataHolder<10, 16>;
  69. using FSRDH_32 = TFixedSizeRandomDataHolder<10, 32>;
  70. using FSRDH_64 = TFixedSizeRandomDataHolder<10, 64>;
  71. using FSRDH_128 = TFixedSizeRandomDataHolder<10, 128>;
  72. using FSRDH_1024 = TFixedSizeRandomDataHolder<10, 1024>;
  73. using FSRDH_10240 = TFixedSizeRandomDataHolder<10, 10240>;
  74. using FSRDH_102400 = TFixedSizeRandomDataHolder<10, 102400>;
  75. using FSRDH_1048576 = TFixedSizeRandomDataHolder<10, 1048576>;
  76. using FSRDH_10485760 = TFixedSizeRandomDataHolder<10, 10485760>;
  77. }
  78. template <typename T>
  79. static inline void BenchEncode(T& d, const NBench::NCpu::TParams& iface) {
  80. for (const auto it : xrange(iface.Iterations())) {
  81. Y_UNUSED(it);
  82. for (size_t i = 0; i < d.Size; ++i) {
  83. NBench::Escape(d.PlaceToEncode[i].data());
  84. Y_DO_NOT_OPTIMIZE_AWAY(
  85. Base64Encode(d.PlaceToEncode[i].data(), (const unsigned char*)d.Data[i].data(), d.Data[i].size()));
  86. NBench::Clobber();
  87. }
  88. }
  89. }
  90. template <typename T>
  91. static inline void BenchEncodeUrl(T& d, const NBench::NCpu::TParams& iface) {
  92. for (const auto it : xrange(iface.Iterations())) {
  93. Y_UNUSED(it);
  94. for (size_t i = 0; i < d.Size; ++i) {
  95. NBench::Escape(d.PlaceToEncode[i].data());
  96. Y_DO_NOT_OPTIMIZE_AWAY(
  97. Base64EncodeUrl(d.PlaceToEncode[i].data(), (const unsigned char*)d.Data[i].data(), d.Data[i].size()));
  98. NBench::Clobber();
  99. }
  100. }
  101. }
  102. template <typename T>
  103. static inline void BenchDecode(T& d, const NBench::NCpu::TParams& iface) {
  104. for (const auto it : xrange(iface.Iterations())) {
  105. Y_UNUSED(it);
  106. for (size_t i = 0; i < d.Size; ++i) {
  107. NBench::Escape(d.PlaceToDecode[i].data());
  108. Y_DO_NOT_OPTIMIZE_AWAY(
  109. Base64Decode(d.PlaceToDecode[i].data(), (const char*)d.DataEncoded[i].data(), (const char*)(d.DataEncoded[i].data() + d.DataEncoded[i].size())));
  110. NBench::Clobber();
  111. }
  112. }
  113. }
  114. Y_CPU_BENCHMARK(EncodeF1, iface) {
  115. auto& d = *Singleton<FSRDH_1>();
  116. BenchEncode(d, iface);
  117. }
  118. Y_CPU_BENCHMARK(DecodeF1, iface) {
  119. auto& d = *Singleton<FSRDH_1>();
  120. BenchDecode(d, iface);
  121. }
  122. Y_CPU_BENCHMARK(EncodeF2, iface) {
  123. auto& d = *Singleton<FSRDH_2>();
  124. BenchEncode(d, iface);
  125. }
  126. Y_CPU_BENCHMARK(DecodeF2, iface) {
  127. auto& d = *Singleton<FSRDH_2>();
  128. BenchDecode(d, iface);
  129. }
  130. Y_CPU_BENCHMARK(EncodeF4, iface) {
  131. auto& d = *Singleton<FSRDH_4>();
  132. BenchEncode(d, iface);
  133. }
  134. Y_CPU_BENCHMARK(DecodeF4, iface) {
  135. auto& d = *Singleton<FSRDH_4>();
  136. BenchDecode(d, iface);
  137. }
  138. Y_CPU_BENCHMARK(EncodeF8, iface) {
  139. auto& d = *Singleton<FSRDH_8>();
  140. BenchEncode(d, iface);
  141. }
  142. Y_CPU_BENCHMARK(DecodeF8, iface) {
  143. auto& d = *Singleton<FSRDH_8>();
  144. BenchDecode(d, iface);
  145. }
  146. Y_CPU_BENCHMARK(EncodeF16, iface) {
  147. auto& d = *Singleton<FSRDH_16>();
  148. BenchEncode(d, iface);
  149. }
  150. Y_CPU_BENCHMARK(DecodeF16, iface) {
  151. auto& d = *Singleton<FSRDH_16>();
  152. BenchDecode(d, iface);
  153. }
  154. Y_CPU_BENCHMARK(EncodeF32, iface) {
  155. auto& d = *Singleton<FSRDH_32>();
  156. BenchEncode(d, iface);
  157. }
  158. Y_CPU_BENCHMARK(DecodeF32, iface) {
  159. auto& d = *Singleton<FSRDH_32>();
  160. BenchDecode(d, iface);
  161. }
  162. Y_CPU_BENCHMARK(EncodeF64, iface) {
  163. auto& d = *Singleton<FSRDH_64>();
  164. BenchEncode(d, iface);
  165. }
  166. Y_CPU_BENCHMARK(DecodeF64, iface) {
  167. auto& d = *Singleton<FSRDH_64>();
  168. BenchDecode(d, iface);
  169. }
  170. Y_CPU_BENCHMARK(EncodeF128, iface) {
  171. auto& d = *Singleton<FSRDH_128>();
  172. BenchEncode(d, iface);
  173. }
  174. Y_CPU_BENCHMARK(DecodeF128, iface) {
  175. auto& d = *Singleton<FSRDH_128>();
  176. BenchDecode(d, iface);
  177. }
  178. Y_CPU_BENCHMARK(EncodeF1024, iface) {
  179. auto& d = *Singleton<FSRDH_1024>();
  180. BenchEncode(d, iface);
  181. }
  182. Y_CPU_BENCHMARK(DecodeF1024, iface) {
  183. auto& d = *Singleton<FSRDH_1024>();
  184. BenchDecode(d, iface);
  185. }
  186. Y_CPU_BENCHMARK(EncodeF10240, iface) {
  187. auto& d = *Singleton<FSRDH_10240>();
  188. BenchEncode(d, iface);
  189. }
  190. Y_CPU_BENCHMARK(DecodeF10240, iface) {
  191. auto& d = *Singleton<FSRDH_10240>();
  192. BenchDecode(d, iface);
  193. }
  194. Y_CPU_BENCHMARK(EncodeF102400, iface) {
  195. auto& d = *Singleton<FSRDH_102400>();
  196. BenchEncode(d, iface);
  197. }
  198. Y_CPU_BENCHMARK(DecodeF102400, iface) {
  199. auto& d = *Singleton<FSRDH_102400>();
  200. BenchDecode(d, iface);
  201. }
  202. Y_CPU_BENCHMARK(EncodeF1048576, iface) {
  203. auto& d = *Singleton<FSRDH_1048576>();
  204. BenchEncode(d, iface);
  205. }
  206. Y_CPU_BENCHMARK(DecodeF1048576, iface) {
  207. auto& d = *Singleton<FSRDH_1048576>();
  208. BenchDecode(d, iface);
  209. }
  210. Y_CPU_BENCHMARK(EncodeF10485760, iface) {
  211. auto& d = *Singleton<FSRDH_10485760>();
  212. BenchEncode(d, iface);
  213. }
  214. Y_CPU_BENCHMARK(DecodeF10485760, iface) {
  215. auto& d = *Singleton<FSRDH_10485760>();
  216. BenchDecode(d, iface);
  217. }
  218. Y_CPU_BENCHMARK(EncodeUrlF1, iface) {
  219. auto& d = *Singleton<FSRDH_1>();
  220. BenchEncodeUrl(d, iface);
  221. }
  222. Y_CPU_BENCHMARK(EncodeUrlF2, iface) {
  223. auto& d = *Singleton<FSRDH_2>();
  224. BenchEncodeUrl(d, iface);
  225. }
  226. Y_CPU_BENCHMARK(EncodeUrlF4, iface) {
  227. auto& d = *Singleton<FSRDH_4>();
  228. BenchEncodeUrl(d, iface);
  229. }
  230. Y_CPU_BENCHMARK(EncodeUrlF8, iface) {
  231. auto& d = *Singleton<FSRDH_8>();
  232. BenchEncodeUrl(d, iface);
  233. }
  234. Y_CPU_BENCHMARK(EncodeUrlF16, iface) {
  235. auto& d = *Singleton<FSRDH_16>();
  236. BenchEncodeUrl(d, iface);
  237. }
  238. Y_CPU_BENCHMARK(EncodeUrlF32, iface) {
  239. auto& d = *Singleton<FSRDH_32>();
  240. BenchEncodeUrl(d, iface);
  241. }
  242. Y_CPU_BENCHMARK(EncodeUrlF64, iface) {
  243. auto& d = *Singleton<FSRDH_64>();
  244. BenchEncodeUrl(d, iface);
  245. }
  246. Y_CPU_BENCHMARK(EncodeUrlF128, iface) {
  247. auto& d = *Singleton<FSRDH_128>();
  248. BenchEncodeUrl(d, iface);
  249. }
  250. Y_CPU_BENCHMARK(EncodeUrlF1024, iface) {
  251. auto& d = *Singleton<FSRDH_1024>();
  252. BenchEncodeUrl(d, iface);
  253. }
  254. Y_CPU_BENCHMARK(EncodeUrlF10240, iface) {
  255. auto& d = *Singleton<FSRDH_10240>();
  256. BenchEncodeUrl(d, iface);
  257. }
  258. Y_CPU_BENCHMARK(EncodeUrlF102400, iface) {
  259. auto& d = *Singleton<FSRDH_102400>();
  260. BenchEncodeUrl(d, iface);
  261. }
  262. Y_CPU_BENCHMARK(EncodeUrlF1048576, iface) {
  263. auto& d = *Singleton<FSRDH_1048576>();
  264. BenchEncodeUrl(d, iface);
  265. }
  266. Y_CPU_BENCHMARK(EncodeUrlF10485760, iface) {
  267. auto& d = *Singleton<FSRDH_10485760>();
  268. BenchEncodeUrl(d, iface);
  269. }