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