proxygen
ParallelTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-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 #include <array>
18 #include <iostream>
19 #include <memory>
20 #include <vector>
21 
22 #include <glog/logging.h>
23 
24 #include <folly/gen/Base.h>
25 #include <folly/gen/Parallel.h>
27 
28 using namespace folly;
29 using namespace folly::gen;
30 using std::vector;
31 
32 const auto square = [](int i) { return i * i; };
33 const auto even = [](int i) { return 0 == i % 2; };
34 static auto sleepyWork = [](int i) {
35  const auto sleepyTime = std::chrono::microseconds(100);
36  std::this_thread::sleep_for(sleepyTime);
37  return i;
38 };
39 
40 static auto isPrime = [](int n) {
41  if (n < 2) {
42  return false;
43  } else if (n > 2) {
44  for (int d = 3; d * d <= n; d += 2) {
45  if (0 == n % d) {
46  return false;
47  }
48  }
49  }
50  return true;
51 };
52 
53 struct {
54  template <class T>
55  std::unique_ptr<T> operator()(T t) const {
56  return std::make_unique<T>(std::move(t));
57  }
58 } makeUnique;
59 
60 static auto primes = seq(1, 1 << 14) | filter(isPrime) | as<vector<size_t>>();
61 
62 static auto primeFactors = [](int n) {
63  return from(primes) | filter([&](int d) { return 0 == n % d; }) | count;
64 };
65 
66 TEST(ParallelTest, Serial) {
67  EXPECT_EQ(
68  seq(1, 10) | map(square) | filter(even) | sum,
69  seq(1, 10) | parallel(map(square) | filter(even)) | sum);
70 }
71 
73 
74 TEST(ParallelTest, ComputeBound64) {
75  int length = 1 << 10;
76  EXPECT_EQ(
77  seq<size_t>(1, length) | heavyWork | sum,
78  seq<size_t>(1, length) | parallel(heavyWork) | sum);
79 }
80 
81 TEST(ParallelTest, Take) {
82  int length = 1 << 18;
83  int limit = 1 << 14;
84  EXPECT_EQ(
85  seq(1, length) | take(limit) | count,
86  seq(1, length) | parallel(heavyWork) | take(limit) | count);
87 }
88 
89 TEST(ParallelTest, Unique) {
90  auto uniqued = from(primes) | map(makeUnique) | as<vector>();
91  EXPECT_EQ(
92  primes.size(),
95  EXPECT_EQ(
96  2,
99  count);
100 }
101 
102 TEST(ParallelTest, PSum) {
103  EXPECT_EQ(
104  from(primes) | map(sleepyWork) | sum,
106 }
107 
108 int main(int argc, char* argv[]) {
109  testing::InitGoogleTest(&argc, argv);
110  gflags::ParseCommandLineFlags(&argc, &argv, true);
111  return RUN_ALL_TESTS();
112 }
const auto even
Sub sub(Sink sink)
Definition: Parallel.h:104
int main(int argc, char *argv[])
struct @64 makeUnique
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2232
constexpr detail::Count count
Definition: Base-inl.h:2551
From from(Container &source)
Definition: Base.h:438
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static auto primes
Gen seq(Value first, Value last)
Definition: Base.h:484
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
auto heavyWork
static auto primeFactors
constexpr detail::Sum sum
Definition: Base-inl.h:2549
const auto square
char ** argv
static auto isPrime
static auto sleepyWork
Map map(Predicate pred=Predicate())
Definition: Base.h:545
Parallel parallel(Ops ops, size_t threads=0)
Definition: Parallel.h:88
Filter filter(Predicate pred=Predicate())
Definition: Base.h:646
detail::Take take(Number count)
Definition: Base-inl.h:2582
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5370
TEST(IStream, ByLine)
Definition: IStreamTest.cpp:32
constexpr detail::Dereference dereference
Definition: Base-inl.h:2575