Fix SimpleBarrier
[folly.git] / folly / test / FBVectorBenchmark.cpp
1 /*
2  * Copyright 2016 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/Benchmark.h>
21 #include <folly/FBString.h>
22 #include <folly/FBVector.h>
23 #include <folly/Foreach.h>
24 #include <folly/Random.h>
25 #include <folly/Traits.h>
26 #include <folly/portability/GFlags.h>
27 #include <folly/portability/GTest.h>
28
29 #include <list>
30 #include <memory>
31 #include <boost/random.hpp>
32
33 using namespace std;
34 using namespace folly;
35
36 auto static const seed = randomNumberSeed();
37 typedef boost::mt19937 RandomT;
38 static RandomT rng(seed);
39
40 template <class Integral1, class Integral2>
41 Integral2 random(Integral1 low, Integral2 up) {
42   boost::uniform_int<> range(low, up);
43   return range(rng);
44 }
45
46 template <class String>
47 void randomString(String* toFill, unsigned int maxSize = 1000) {
48   assert(toFill);
49   toFill->resize(random(0, maxSize));
50   FOR_EACH (i, *toFill) {
51     *i = random('a', 'z');
52   }
53 }
54
55 template <class String, class Integral>
56 void Num2String(String& str, Integral /* n */) {
57   str.resize(10, '\0');
58   sprintf(&str[0], "%ul", 10);
59   str.resize(strlen(str.c_str()));
60 }
61
62 std::list<char> RandomList(unsigned int maxSize) {
63   std::list<char> lst(random(0u, maxSize));
64   std::list<char>::iterator i = lst.begin();
65   for (; i != lst.end(); ++i) {
66     *i = random('a', 'z');
67   }
68   return lst;
69 }
70
71 template<class T> T randomObject();
72
73 template<> int randomObject<int>() {
74   return random(0, 1024);
75 }
76
77 template<> folly::fbstring randomObject<folly::fbstring>() {
78   folly::fbstring result;
79   randomString(&result);
80   return result;
81 }
82
83 #define CONCAT(A, B) CONCAT_HELPER(A, B)
84 #define CONCAT_HELPER(A, B) A##B
85 #define BENCHFUN(F) CONCAT(CONCAT(BM_, F), CONCAT(_, VECTOR))
86 #define TESTFUN(F) TEST(fbvector, CONCAT(F, VECTOR))
87
88 typedef vector<int> IntVector;
89 typedef fbvector<int> IntFBVector;
90 typedef vector<folly::fbstring> FBStringVector;
91 typedef fbvector<folly::fbstring> FBStringFBVector;
92
93 #define VECTOR IntVector
94 #include <folly/test/FBVectorTestBenchmarks.cpp.h> // nolint
95 #undef VECTOR
96 #define VECTOR IntFBVector
97 #include <folly/test/FBVectorTestBenchmarks.cpp.h> // nolint
98 #undef VECTOR
99 #define VECTOR FBStringVector
100 #include <folly/test/FBVectorTestBenchmarks.cpp.h> // nolint
101 #undef VECTOR
102 #define VECTOR FBStringFBVector
103 #include <folly/test/FBVectorTestBenchmarks.cpp.h> // nolint
104 #undef VECTOR
105
106 int main(int argc, char** argv) {
107   gflags::ParseCommandLineFlags(&argc, &argv, true);
108   folly::runBenchmarks();
109   return 0;
110 }