2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * This file is supposed to be included from within
19 * FBStringTest. Do not use otherwise.
21 * override-include-guard
24 void BENCHFUN(initRNG)(size_t /* iters */, size_t) { srand(seed); }
25 BENCHMARK_PARAM(BENCHFUN(initRNG), 0);
27 void BENCHFUN(defaultCtor)(size_t iters, size_t) {
28 FOR_EACH_RANGE (i, 0, iters) {
30 doNotOptimizeAway(&s);
33 BENCHMARK_PARAM(BENCHFUN(defaultCtor), 0);
35 void BENCHFUN(copyCtor)(size_t iters, size_t arg) {
38 randomString(&s, arg);
40 FOR_EACH_RANGE (i, 0, iters) {
42 doNotOptimizeAway(&s1);
45 BENCHMARK_PARAM(BENCHFUN(copyCtor), 32768);
47 void BENCHFUN(ctorFromArray)(size_t iters, size_t arg) {
50 randomString(&s, arg);
55 FOR_EACH_RANGE (i, 0, iters) {
56 STRING s1(s.data(), s.size());
57 doNotOptimizeAway(&s1);
60 BENCHMARK_PARAM(BENCHFUN(ctorFromArray), 32768);
62 void BENCHFUN(ctorFromTwoPointers)(size_t iters, size_t arg) {
65 if (s.size() < arg) s.resize(arg);
67 FOR_EACH_RANGE (i, 0, iters) {
68 STRING s1(s.begin(), s.end());
69 doNotOptimizeAway(&s1);
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);
78 void BENCHFUN(ctorFromChar)(size_t iters, size_t arg) {
79 FOR_EACH_RANGE (i, 0, iters) {
81 doNotOptimizeAway(&s1);
84 BENCHMARK_PARAM(BENCHFUN(ctorFromChar), 1048576);
86 void BENCHFUN(assignmentOp)(size_t iters, size_t arg) {
89 randomString(&s, arg);
91 FOR_EACH_RANGE (i, 0, iters) {
94 randomString(&s1, arg);
95 doNotOptimizeAway(&s1);
100 BENCHMARK_PARAM(BENCHFUN(assignmentOp), 256);
102 void BENCHFUN(assignmentFill)(size_t iters, size_t) {
104 FOR_EACH_RANGE (i, 0, iters) {
105 s = static_cast<char>(i);
106 doNotOptimizeAway(&s);
109 BENCHMARK_PARAM(BENCHFUN(assignmentFill), 0);
111 void BENCHFUN(resize)(size_t iters, size_t arg) {
113 FOR_EACH_RANGE (i, 0, iters) {
114 s.resize(random(0, arg));
115 doNotOptimizeAway(&s);
118 BENCHMARK_PARAM(BENCHFUN(resize), 524288);
120 void BENCHFUN(findSuccessful)(size_t iters, size_t /* arg */) {
126 // Text courtesy (ahem) of
127 // http://www.psychologytoday.com/blog/career-transitions/200906/
128 // the-dreaded-writing-sample
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\
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 \
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.";
150 pos = random(0, s.size());
151 len = random(0, s.size() - pos);
153 FOR_EACH_RANGE (i, 0, iters) {
154 doNotOptimizeAway(s.find(s.data(), pos, len));
157 BENCHMARK_PARAM(BENCHFUN(findSuccessful), 524288);
159 void BENCHFUN(findUnsuccessful)(size_t iters, size_t /* arg */) {
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\
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 \
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.";
184 s1 = "So how do you tackle that writing sample request?";
187 FOR_EACH_RANGE (i, 0, iters) {
188 doNotOptimizeAway(s.find(s1));
191 BENCHMARK_PARAM(BENCHFUN(findUnsuccessful), 524288);
193 void BENCHFUN(equality)(size_t iters, size_t arg) {
194 std::vector<STRING> haystack(arg);
197 for (auto& hay : haystack) {
198 randomBinaryString(&hay, 1024);
202 FOR_EACH_RANGE (i, 0, iters) {
204 randomBinaryString(&needle, 1024);
205 doNotOptimizeAway(std::find(haystack.begin(), haystack.end(), needle));
208 BENCHMARK_PARAM(BENCHFUN(equality), 65536);
210 void BENCHFUN(replace)(size_t iters, size_t arg) {
213 randomString(&s, arg);
215 FOR_EACH_RANGE (i, 0, iters) {
216 BenchmarkSuspender susp;
217 doNotOptimizeAway(&s);
218 auto const pos = random(0, s.size());
219 auto toRemove = random(0, s.size() - pos);
220 auto toInsert = random(0, arg);
222 randomString(&s1, toInsert);
225 s.replace(pos, toRemove, s1);
228 BENCHMARK_PARAM(BENCHFUN(replace), 256);
230 void BENCHFUN(push_back)(size_t iters, size_t arg) {
231 FOR_EACH_RANGE (i, 0, iters) {
233 FOR_EACH_RANGE (j, 0, arg) {
238 BENCHMARK_PARAM(BENCHFUN(push_back), 1);
239 BENCHMARK_PARAM(BENCHFUN(push_back), 23);
240 BENCHMARK_PARAM(BENCHFUN(push_back), 127);
241 BENCHMARK_PARAM(BENCHFUN(push_back), 1024);
243 void BENCHFUN(short_append)(size_t iters, size_t arg) {
244 FOR_EACH_RANGE (i, 0, iters) {
246 FOR_EACH_RANGE (j, 0, arg) {
251 BENCHMARK_PARAM(BENCHFUN(short_append), 23);
252 BENCHMARK_PARAM(BENCHFUN(short_append), 1024);
254 void BENCHFUN(getline)(size_t iters, size_t arg) {
259 FOR_EACH_RANGE(i, 0, 512) {
260 randomString(&line, arg);
268 std::istringstream is(lines);
269 while (iters && getline(is, line)) {
270 folly::doNotOptimizeAway(line.size());
275 BENCHMARK_PARAM(BENCHFUN(getline), 23);
276 BENCHMARK_PARAM(BENCHFUN(getline), 1000);