Split tests into test and benchmarks.
[folly.git] / folly / test / RandomBenchmark.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 #include <folly/Random.h>
18
19 #include <folly/Benchmark.h>
20 #include <folly/Foreach.h>
21 #include <folly/Range.h>
22
23 #include <glog/logging.h>
24
25 #include <algorithm>
26 #include <thread>
27 #include <vector>
28 #include <random>
29
30 using namespace folly;
31
32 BENCHMARK(minstdrand, n) {
33   BenchmarkSuspender braces;
34   std::random_device rd;
35   std::minstd_rand rng(rd());
36
37   braces.dismiss();
38
39   FOR_EACH_RANGE(i, 0, n) { doNotOptimizeAway(rng()); }
40 }
41
42 BENCHMARK(mt19937, n) {
43   BenchmarkSuspender braces;
44   std::random_device rd;
45   std::mt19937 rng(rd());
46
47   braces.dismiss();
48
49   FOR_EACH_RANGE(i, 0, n) { doNotOptimizeAway(rng()); }
50 }
51
52 BENCHMARK(threadprng, n) {
53   BenchmarkSuspender braces;
54   ThreadLocalPRNG tprng;
55   tprng();
56
57   braces.dismiss();
58
59   FOR_EACH_RANGE(i, 0, n) { doNotOptimizeAway(tprng()); }
60 }
61
62 BENCHMARK(RandomDouble) { doNotOptimizeAway(Random::randDouble01()); }
63 BENCHMARK(Random32) { doNotOptimizeAway(Random::rand32()); }
64 BENCHMARK(Random32Num) { doNotOptimizeAway(Random::rand32(100)); }
65 BENCHMARK(Random64) { doNotOptimizeAway(Random::rand64()); }
66 BENCHMARK(Random64Num) { doNotOptimizeAway(Random::rand64(100ul << 32)); }
67 BENCHMARK(Random64OneIn) { doNotOptimizeAway(Random::oneIn(100)); }
68
69 int main(int argc, char** argv) {
70   gflags::ParseCommandLineFlags(&argc, &argv, true);
71   folly::runBenchmarks();
72   return 0;
73 }