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