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