proxygen
FBStringBenchmark.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2012-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 //
18 // Author: andrei.alexandrescu@fb.com
19 
20 #include <folly/FBString.h>
21 
22 #include <cstdlib>
23 #include <fstream>
24 #include <list>
25 #include <sstream>
26 
27 #include <boost/random.hpp>
28 
29 #include <folly/Benchmark.h>
30 #include <folly/Random.h>
33 
34 using namespace std;
35 using namespace folly;
36 
37 static const int seed = folly::randomNumberSeed();
38 typedef boost::mt19937 RandomT;
39 static RandomT rng(seed);
40 
41 template <class Integral1, class Integral2>
42 Integral2 random(Integral1 low, Integral2 up) {
43  boost::uniform_int<> range(low, up);
44  return range(rng);
45 }
46 
47 template <class String>
48 void randomString(String* toFill, size_t size = 1000) {
49  assert(toFill);
50  toFill->resize(size);
51  FOR_EACH (i, *toFill) { *i = random('a', 'z'); }
52 }
53 
54 template <class String>
55 void randomBinaryString(String* toFill, size_t size = 1000) {
56  assert(toFill);
57  toFill->resize(size);
58  FOR_EACH (i, *toFill) { *i = random('0', '1'); }
59 }
60 
61 template <class String, class Integral>
62 void Num2String(String& str, Integral n) {
63  str.resize(30, '\0');
64  sprintf(&str[0], "%lu", static_cast<unsigned long>(n));
65  str.resize(strlen(str.c_str()));
66 }
67 
68 std::list<char> RandomList(unsigned int maxSize) {
69  std::list<char> lst(random(0u, maxSize));
70  std::list<char>::iterator i = lst.begin();
71  for (; i != lst.end(); ++i) {
72  *i = random('a', 'z');
73  }
74  return lst;
75 }
76 
77 #define CONCAT(A, B) CONCAT_HELPER(A, B)
78 #define CONCAT_HELPER(A, B) A##B
79 #define BENCHFUN(F) CONCAT(CONCAT(BM_, F), CONCAT(_, STRING))
80 
81 #define STRING string
82 #include <folly/test/FBStringTestBenchmarks.cpp.h> // nolint
83 #undef STRING
84 #define STRING fbstring
85 #include <folly/test/FBStringTestBenchmarks.cpp.h> // nolint
86 #undef STRING
87 
88 int main(int argc, char** argv) {
89  gflags::ParseCommandLineFlags(&argc, &argv, true);
91  return 0;
92 }
93 
94 /*
95 malloc
96 
97 BENCHFUN(defaultCtor) 100000 1.426 s 14.26 us 68.47 k
98 BM_copyCtor_string/32k 100000 63.48 ms 634.8 ns 1.502 M
99 BM_ctorFromArray_string/32k 100000 303.3 ms 3.033 us 321.9 k
100 BM_ctorFromChar_string/1M 100000 9.915 ms 99.15 ns 9.619 M
101 BM_assignmentOp_string/256 100000 69.09 ms 690.9 ns 1.38 M
102 BENCHFUN(assignmentFill) 100000 1.775 ms 17.75 ns 53.73 M
103 BM_resize_string/512k 100000 1.667 s 16.67 us 58.58 k
104 BM_findSuccessful_string/512k 100000 287.3 ms 2.873 us 339.9 k
105 BM_findUnsuccessful_string/512k 100000 320.3 ms 3.203 us 304.9 k
106 BM_replace_string/256 100000 69.68 ms 696.8 ns 1.369 M
107 BM_push_back_string/1k 100000 433.1 ms 4.331 us 225.5 k
108 
109 BENCHFUN(defaultCtor) 100000 1.086 s 10.86 us 89.91 k
110 BM_copyCtor_fbstring/32k 100000 4.218 ms 42.18 ns 22.61 M
111 BM_ctorFromArray_fbstring/32k 100000 145.2 ms 1.452 us 672.7 k
112 BM_ctorFromChar_fbstring/1M 100000 9.21 ms 92.1 ns 10.35 M
113 BM_assignmentOp_fbstring/256 100000 61.95 ms 619.5 ns 1.54 M
114 BENCHFUN(assignmentFill) 100000 1.41 ms 14.1 ns 67.64 M
115 BM_resize_fbstring/512k 100000 1.668 s 16.68 us 58.56 k
116 BM_findSuccessful_fbstring/512k 100000 20.6 ms 206 ns 4.629 M
117 BM_findUnsuccessful_fbstring/512k 100000 141.3 ms 1.413 us 691.1 k
118 BM_replace_fbstring/256 100000 77.12 ms 771.2 ns 1.237 M
119 BM_push_back_fbstring/1k 100000 1.745 s 17.45 us 55.95 k
120 
121 jemalloc
122 
123 BENCHFUN(defaultCtor) 100000 1.426 s 14.26 us 68.5 k
124 BM_copyCtor_string/32k 100000 275.7 ms 2.757 us 354.2 k
125 BM_ctorFromArray_string/32k 100000 270 ms 2.7 us 361.7 k
126 BM_ctorFromChar_string/1M 100000 10.36 ms 103.6 ns 9.206 M
127 BM_assignmentOp_string/256 100000 70.44 ms 704.3 ns 1.354 M
128 BENCHFUN(assignmentFill) 100000 1.766 ms 17.66 ns 54 M
129 BM_resize_string/512k 100000 1.675 s 16.75 us 58.29 k
130 BM_findSuccessful_string/512k 100000 90.89 ms 908.9 ns 1.049 M
131 BM_findUnsuccessful_string/512k 100000 315.1 ms 3.151 us 309.9 k
132 BM_replace_string/256 100000 71.14 ms 711.4 ns 1.341 M
133 BM_push_back_string/1k 100000 425.1 ms 4.251 us 229.7 k
134 
135 BENCHFUN(defaultCtor) 100000 1.082 s 10.82 us 90.23 k
136 BM_copyCtor_fbstring/32k 100000 4.213 ms 42.13 ns 22.64 M
137 BM_ctorFromArray_fbstring/32k 100000 113.2 ms 1.132 us 863 k
138 BM_ctorFromChar_fbstring/1M 100000 9.162 ms 91.62 ns 10.41 M
139 BM_assignmentOp_fbstring/256 100000 61.34 ms 613.4 ns 1.555 M
140 BENCHFUN(assignmentFill) 100000 1.408 ms 14.08 ns 67.73 M
141 BM_resize_fbstring/512k 100000 1.671 s 16.71 us 58.43 k
142 BM_findSuccessful_fbstring/512k 100000 8.723 ms 87.23 ns 10.93 M
143 BM_findUnsuccessful_fbstring/512k 100000 141.3 ms 1.413 us 691.2 k
144 BM_replace_fbstring/256 100000 77.83 ms 778.3 ns 1.225 M
145 BM_push_back_fbstring/1k 100000 1.744 s 17.44 us 55.99 k
146 */
void Num2String(String &str, Integral n)
Integral2 random(Integral1 low, Integral2 up)
void randomBinaryString(String *toFill, size_t size=1000)
static const int seed
STL namespace.
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
boost::mt19937 RandomT
void runBenchmarks()
Definition: Benchmark.cpp:456
std::list< char > RandomList(unsigned int maxSize)
Gen range(Value begin, Value end)
Definition: Base.h:467
char ** argv
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
static RandomT rng(seed)
#define FOR_EACH(i, c)
Definition: Foreach.h:143
uint32_t randomNumberSeed()
Definition: Random.h:367
int main(int argc, char **argv)
void randomString(String *toFill, size_t size=1000)