proxygen
FBStringTestBenchmarks.cpp.h
Go to the documentation of this file.
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 
24 void BENCHFUN(initRNG)(size_t /* iters */, size_t) {
25  srand(seed);
26 }
28 
29 void BENCHFUN(defaultCtor)(size_t iters, size_t) {
30  FOR_EACH_RANGE (i, 0, iters) {
31  STRING s[4096];
33  }
34 }
36 
37 void BENCHFUN(copyCtor)(size_t iters, size_t arg) {
38  STRING s;
40  randomString(&s, arg);
41  }
42  FOR_EACH_RANGE (i, 0, iters) {
43  STRING s1 = s;
44  doNotOptimizeAway(&s1);
45  }
46 }
48 
49 void BENCHFUN(ctorFromArray)(size_t iters, size_t arg) {
50  STRING s;
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 }
63 
64 void BENCHFUN(ctorFromTwoPointers)(size_t iters, size_t arg) {
65  /* library-local */ static STRING s;
67  if (s.size() < arg) {
68  s.resize(arg);
69  }
70  }
71  FOR_EACH_RANGE (i, 0, iters) {
72  STRING s1(s.begin(), s.end());
73  doNotOptimizeAway(&s1);
74  }
75 }
81 
82 void BENCHFUN(ctorFromChar)(size_t iters, size_t arg) {
83  FOR_EACH_RANGE (i, 0, iters) {
84  STRING s1('a', arg);
85  doNotOptimizeAway(&s1);
86  }
87 }
89 
90 void BENCHFUN(assignmentOp)(size_t iters, size_t arg) {
91  STRING s;
93  randomString(&s, arg);
94  }
95  FOR_EACH_RANGE (i, 0, iters) {
96  STRING s1;
98  randomString(&s1, arg);
99  doNotOptimizeAway(&s1);
100  }
101  s1 = s;
102  }
103 }
105 
106 void BENCHFUN(assignmentFill)(size_t iters, size_t) {
107  STRING s;
108  FOR_EACH_RANGE (i, 0, iters) {
109  s = static_cast<char>(i);
111  }
112 }
114 
115 void BENCHFUN(resize)(size_t iters, size_t arg) {
116  STRING s;
117  FOR_EACH_RANGE (i, 0, iters) {
118  s.resize(random(0, arg));
120  }
121 }
123 
124 void BENCHFUN(findSuccessful)(size_t iters, size_t /* arg */) {
125  size_t pos, len;
126  STRING s;
127 
129  // Text courtesy (ahem) of
130  // http://www.psychologytoday.com/blog/career-transitions/200906/
131  // the-dreaded-writing-sample
132  s = "\
133 Even if you've mastered the art of the cover letter and the resume, \
134 another part of the job search process can trip up an otherwise \
135 qualified candidate: the writing sample.\n\
136 \n\
137 Strong writing and communication skills are highly sought after by \
138 most employers. Whether crafting short emails or lengthy annual \
139 reports, many workers use their writing skills every day. And for an \
140 employer seeking proof behind that ubiquitous candidate \
141 phrase,\"excellent communication skills\", a required writing sample \
142 is invaluable.\n\
143 \n\
144 Writing samples need the same care and attention given to cover \
145 letters and resumes. Candidates with otherwise impeccable credentials \
146 are routinely eliminated by a poorly chosen writing sample. Notice I \
147 said \"poorly chosen\" not \"poorly written.\" Because that's the rub: \
148 a writing sample not only reveals the individual's writing skills, it \
149 also offers a peek into what they consider important or relevant for \
150 the position. If you miss that mark with your writing sample, don't \
151 expect to get a call for an interview.";
152 
153  pos = random(0, s.size());
154  len = random(0, s.size() - pos);
155  }
156  FOR_EACH_RANGE (i, 0, iters) {
157  doNotOptimizeAway(s.find(s.data(), pos, len));
158  }
159 }
161 
162 void BENCHFUN(findUnsuccessful)(size_t iters, size_t /* arg */) {
163  STRING s, s1;
164 
166  s = "\
167 Even if you've mastered the art of the cover letter and the resume, \
168 another part of the job search process can trip up an otherwise \
169 qualified candidate: the writing sample.\n\
170 \n\
171 Strong writing and communication skills are highly sought after by \
172 most employers. Whether crafting short emails or lengthy annual \
173 reports, many workers use their writing skills every day. And for an \
174 employer seeking proof behind that ubiquitous candidate \
175 phrase,\"excellent communication skills\", a required writing sample \
176 is invaluable.\n\
177 \n\
178 Writing samples need the same care and attention given to cover \
179 letters and resumes. Candidates with otherwise impeccable credentials \
180 are routinely eliminated by a poorly chosen writing sample. Notice I \
181 said \"poorly chosen\" not \"poorly written.\" Because that's the rub: \
182 a writing sample not only reveals the individual's writing skills, it \
183 also offers a peek into what they consider important or relevant for \
184 the position. If you miss that mark with your writing sample, don't \
185 expect to get a call for an interview.";
186 
187  s1 = "So how do you tackle that writing sample request?";
188  }
189 
190  FOR_EACH_RANGE (i, 0, iters) { doNotOptimizeAway(s.find(s1)); }
191 }
193 
194 void BENCHFUN(equality)(size_t iters, size_t arg) {
195  std::vector<STRING> haystack(arg);
196 
198  for (auto& hay : haystack) {
199  randomBinaryString(&hay, 1024);
200  }
201  }
202 
203  FOR_EACH_RANGE (i, 0, iters) {
204  STRING needle;
205  randomBinaryString(&needle, 1024);
206  doNotOptimizeAway(std::find(haystack.begin(), haystack.end(), needle));
207  }
208 }
210 
211 void BENCHFUN(replace)(size_t iters, size_t arg) {
212  STRING s;
214  randomString(&s, arg);
215  }
216  FOR_EACH_RANGE (i, 0, iters) {
217  BenchmarkSuspender susp;
219  auto const pos = random(0, s.size());
220  auto toRemove = random(0, s.size() - pos);
221  auto toInsert = random(0, arg);
222  STRING s1;
223  randomString(&s1, toInsert);
224  susp.dismiss();
225 
226  s.replace(pos, toRemove, s1);
227  }
228 }
230 
231 void BENCHFUN(push_back)(size_t iters, size_t arg) {
232  FOR_EACH_RANGE (i, 0, iters) {
233  STRING s;
234  FOR_EACH_RANGE (j, 0, arg) { s += ' '; }
235  }
236 }
241 
242 void BENCHFUN(short_append)(size_t iters, size_t arg) {
243  FOR_EACH_RANGE (i, 0, iters) {
244  STRING s;
245  FOR_EACH_RANGE (j, 0, arg) { s += "012"; }
246  }
247 }
250 
251 void BENCHFUN(getline)(size_t iters, size_t arg) {
252  string lines;
253 
255  string line;
256  FOR_EACH_RANGE (i, 0, 512) {
257  randomString(&line, arg);
258  lines += line;
259  lines += '\n';
260  }
261  }
262 
263  STRING line;
264  while (iters) {
265  std::istringstream is(lines);
266  while (iters && getline(is, line)) {
267  folly::doNotOptimizeAway(line.size());
268  iters--;
269  }
270  }
271 }
const string needle
void BENCHFUN() resize(size_t iters, size_t arg)
Integral2 random(Integral1 low, Integral2 up)
void BENCHFUN() defaultCtor(size_t iters, size_t)
void randomBinaryString(String *toFill, size_t size=1000)
static const int seed
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
void BENCHFUN() getline(size_t iters, size_t arg)
void BENCHFUN() ctorFromTwoPointers(size_t iters, size_t arg)
void BENCHFUN() ctorFromChar(size_t iters, size_t arg)
#define STRING
void BENCHFUN() assignmentOp(size_t iters, size_t arg)
void BENCHFUN() replace(size_t iters, size_t arg)
void BENCHFUN() push_back(size_t iters, size_t arg)
#define FOR_EACH_RANGE(i, begin, end)
Definition: Foreach.h:313
void BENCHFUN() initRNG(size_t, size_t)
void BENCHFUN() findSuccessful(size_t iters, size_t)
void BENCHFUN() copyCtor(size_t iters, size_t arg)
void BENCHFUN() equality(size_t iters, size_t arg)
void BENCHFUN() ctorFromArray(size_t iters, size_t arg)
S lines(StringPiece source)
Definition: String.h:80
#define BENCHMARK_PARAM(name, param)
Definition: Benchmark.h:417
static set< string > s
#define BENCHFUN(F)
void BENCHFUN() assignmentFill(size_t iters, size_t)
void BENCHFUN() findUnsuccessful(size_t iters, size_t)
void BENCHFUN() short_append(size_t iters, size_t arg)
void randomString(String *toFill, size_t size=1000)
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258