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