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