FBStringTestBenchmarks.cpp.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright 2011-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * This file is supposed to be included from within
  18. * FBStringTest. Do not use otherwise.
  19. *
  20. * override-include-guard
  21. */
  22. void BENCHFUN(initRNG)(size_t /* iters */, size_t) {
  23. srand(seed);
  24. }
  25. BENCHMARK_PARAM(BENCHFUN(initRNG), 0)
  26. void BENCHFUN(defaultCtor)(size_t iters, size_t) {
  27. FOR_EACH_RANGE (i, 0, iters) {
  28. STRING s[4096];
  29. doNotOptimizeAway(&s);
  30. }
  31. }
  32. BENCHMARK_PARAM(BENCHFUN(defaultCtor), 0)
  33. void BENCHFUN(copyCtor)(size_t iters, size_t arg) {
  34. STRING s;
  35. BENCHMARK_SUSPEND {
  36. randomString(&s, arg);
  37. }
  38. FOR_EACH_RANGE (i, 0, iters) {
  39. STRING s1 = s;
  40. doNotOptimizeAway(&s1);
  41. }
  42. }
  43. BENCHMARK_PARAM(BENCHFUN(copyCtor), 32768)
  44. void BENCHFUN(ctorFromArray)(size_t iters, size_t arg) {
  45. STRING s;
  46. BENCHMARK_SUSPEND {
  47. randomString(&s, arg);
  48. if (s.empty()) {
  49. s = "This is rare.";
  50. }
  51. }
  52. FOR_EACH_RANGE (i, 0, iters) {
  53. STRING s1(s.data(), s.size());
  54. doNotOptimizeAway(&s1);
  55. }
  56. }
  57. BENCHMARK_PARAM(BENCHFUN(ctorFromArray), 32768)
  58. void BENCHFUN(ctorFromTwoPointers)(size_t iters, size_t arg) {
  59. /* library-local */ static STRING s;
  60. BENCHMARK_SUSPEND {
  61. if (s.size() < arg) {
  62. s.resize(arg);
  63. }
  64. }
  65. FOR_EACH_RANGE (i, 0, iters) {
  66. STRING s1(s.begin(), s.end());
  67. doNotOptimizeAway(&s1);
  68. }
  69. }
  70. BENCHMARK_PARAM(BENCHFUN(ctorFromTwoPointers), 0)
  71. BENCHMARK_PARAM(BENCHFUN(ctorFromTwoPointers), 7)
  72. BENCHMARK_PARAM(BENCHFUN(ctorFromTwoPointers), 15)
  73. BENCHMARK_PARAM(BENCHFUN(ctorFromTwoPointers), 23)
  74. BENCHMARK_PARAM(BENCHFUN(ctorFromTwoPointers), 24)
  75. void BENCHFUN(ctorFromChar)(size_t iters, size_t arg) {
  76. FOR_EACH_RANGE (i, 0, iters) {
  77. STRING s1('a', arg);
  78. doNotOptimizeAway(&s1);
  79. }
  80. }
  81. BENCHMARK_PARAM(BENCHFUN(ctorFromChar), 1048576)
  82. void BENCHFUN(assignmentOp)(size_t iters, size_t arg) {
  83. STRING s;
  84. BENCHMARK_SUSPEND {
  85. randomString(&s, arg);
  86. }
  87. FOR_EACH_RANGE (i, 0, iters) {
  88. STRING s1;
  89. BENCHMARK_SUSPEND {
  90. randomString(&s1, arg);
  91. doNotOptimizeAway(&s1);
  92. }
  93. s1 = s;
  94. }
  95. }
  96. BENCHMARK_PARAM(BENCHFUN(assignmentOp), 256)
  97. void BENCHFUN(assignmentFill)(size_t iters, size_t) {
  98. STRING s;
  99. FOR_EACH_RANGE (i, 0, iters) {
  100. s = static_cast<char>(i);
  101. doNotOptimizeAway(&s);
  102. }
  103. }
  104. BENCHMARK_PARAM(BENCHFUN(assignmentFill), 0)
  105. void BENCHFUN(resize)(size_t iters, size_t arg) {
  106. STRING s;
  107. FOR_EACH_RANGE (i, 0, iters) {
  108. s.resize(random(0, arg));
  109. doNotOptimizeAway(&s);
  110. }
  111. }
  112. BENCHMARK_PARAM(BENCHFUN(resize), 524288)
  113. void BENCHFUN(findSuccessful)(size_t iters, size_t /* arg */) {
  114. size_t pos, len;
  115. STRING s;
  116. BENCHMARK_SUSPEND {
  117. // Text courtesy (ahem) of
  118. // http://www.psychologytoday.com/blog/career-transitions/200906/
  119. // the-dreaded-writing-sample
  120. s = "\
  121. Even if you've mastered the art of the cover letter and the resume, \
  122. another part of the job search process can trip up an otherwise \
  123. qualified candidate: the writing sample.\n\
  124. \n\
  125. Strong writing and communication skills are highly sought after by \
  126. most employers. Whether crafting short emails or lengthy annual \
  127. reports, many workers use their writing skills every day. And for an \
  128. employer seeking proof behind that ubiquitous candidate \
  129. phrase,\"excellent communication skills\", a required writing sample \
  130. is invaluable.\n\
  131. \n\
  132. Writing samples need the same care and attention given to cover \
  133. letters and resumes. Candidates with otherwise impeccable credentials \
  134. are routinely eliminated by a poorly chosen writing sample. Notice I \
  135. said \"poorly chosen\" not \"poorly written.\" Because that's the rub: \
  136. a writing sample not only reveals the individual's writing skills, it \
  137. also offers a peek into what they consider important or relevant for \
  138. the position. If you miss that mark with your writing sample, don't \
  139. expect to get a call for an interview.";
  140. pos = random(0, s.size());
  141. len = random(0, s.size() - pos);
  142. }
  143. FOR_EACH_RANGE (i, 0, iters) {
  144. doNotOptimizeAway(s.find(s.data(), pos, len));
  145. }
  146. }
  147. BENCHMARK_PARAM(BENCHFUN(findSuccessful), 524288)
  148. void BENCHFUN(findUnsuccessful)(size_t iters, size_t /* arg */) {
  149. STRING s, s1;
  150. BENCHMARK_SUSPEND {
  151. s = "\
  152. Even if you've mastered the art of the cover letter and the resume, \
  153. another part of the job search process can trip up an otherwise \
  154. qualified candidate: the writing sample.\n\
  155. \n\
  156. Strong writing and communication skills are highly sought after by \
  157. most employers. Whether crafting short emails or lengthy annual \
  158. reports, many workers use their writing skills every day. And for an \
  159. employer seeking proof behind that ubiquitous candidate \
  160. phrase,\"excellent communication skills\", a required writing sample \
  161. is invaluable.\n\
  162. \n\
  163. Writing samples need the same care and attention given to cover \
  164. letters and resumes. Candidates with otherwise impeccable credentials \
  165. are routinely eliminated by a poorly chosen writing sample. Notice I \
  166. said \"poorly chosen\" not \"poorly written.\" Because that's the rub: \
  167. a writing sample not only reveals the individual's writing skills, it \
  168. also offers a peek into what they consider important or relevant for \
  169. the position. If you miss that mark with your writing sample, don't \
  170. expect to get a call for an interview.";
  171. s1 = "So how do you tackle that writing sample request?";
  172. }
  173. FOR_EACH_RANGE (i, 0, iters) { doNotOptimizeAway(s.find(s1)); }
  174. }
  175. BENCHMARK_PARAM(BENCHFUN(findUnsuccessful), 524288)
  176. void BENCHFUN(equality)(size_t iters, size_t arg) {
  177. std::vector<STRING> haystack(arg);
  178. BENCHMARK_SUSPEND {
  179. for (auto& hay : haystack) {
  180. randomBinaryString(&hay, 1024);
  181. }
  182. }
  183. FOR_EACH_RANGE (i, 0, iters) {
  184. STRING needle;
  185. randomBinaryString(&needle, 1024);
  186. doNotOptimizeAway(std::find(haystack.begin(), haystack.end(), needle));
  187. }
  188. }
  189. BENCHMARK_PARAM(BENCHFUN(equality), 65536)
  190. void BENCHFUN(replace)(size_t iters, size_t arg) {
  191. STRING s;
  192. BENCHMARK_SUSPEND {
  193. randomString(&s, arg);
  194. }
  195. FOR_EACH_RANGE (i, 0, iters) {
  196. BenchmarkSuspender susp;
  197. doNotOptimizeAway(&s);
  198. auto const pos = random(0, s.size());
  199. auto toRemove = random(0, s.size() - pos);
  200. auto toInsert = random(0, arg);
  201. STRING s1;
  202. randomString(&s1, toInsert);
  203. susp.dismiss();
  204. s.replace(pos, toRemove, s1);
  205. }
  206. }
  207. BENCHMARK_PARAM(BENCHFUN(replace), 256)
  208. void BENCHFUN(push_back)(size_t iters, size_t arg) {
  209. FOR_EACH_RANGE (i, 0, iters) {
  210. STRING s;
  211. FOR_EACH_RANGE (j, 0, arg) { s += ' '; }
  212. }
  213. }
  214. BENCHMARK_PARAM(BENCHFUN(push_back), 1)
  215. BENCHMARK_PARAM(BENCHFUN(push_back), 23)
  216. BENCHMARK_PARAM(BENCHFUN(push_back), 127)
  217. BENCHMARK_PARAM(BENCHFUN(push_back), 1024)
  218. void BENCHFUN(short_append)(size_t iters, size_t arg) {
  219. FOR_EACH_RANGE (i, 0, iters) {
  220. STRING s;
  221. FOR_EACH_RANGE (j, 0, arg) { s += "012"; }
  222. }
  223. }
  224. BENCHMARK_PARAM(BENCHFUN(short_append), 23)
  225. BENCHMARK_PARAM(BENCHFUN(short_append), 1024)
  226. void BENCHFUN(getline)(size_t iters, size_t arg) {
  227. string lines;
  228. BENCHMARK_SUSPEND {
  229. string line;
  230. FOR_EACH_RANGE (i, 0, 512) {
  231. randomString(&line, arg);
  232. lines += line;
  233. lines += '\n';
  234. }
  235. }
  236. STRING line;
  237. while (iters) {
  238. std::istringstream is(lines);
  239. while (iters && getline(is, line)) {
  240. folly::doNotOptimizeAway(line.size());
  241. iters--;
  242. }
  243. }
  244. }
  245. BENCHMARK_PARAM(BENCHFUN(getline), 23)
  246. BENCHMARK_PARAM(BENCHFUN(getline), 1000)