2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gflags/gflags.h>
18 #include <folly/Baton.h>
19 #include <folly/Benchmark.h>
20 #include <folly/wangle/Future.h>
21 #include <folly/wangle/Promise.h>
22 #include <semaphore.h>
25 using namespace folly::wangle;
35 void someThens(size_t n) {
36 auto f = makeFuture<int>(42);
37 for (size_t i = 0; i < n; i++) {
38 f = f.then(incr<int>);
42 } // anonymous namespace
44 BENCHMARK(constantFuture) {
48 // This shouldn't get too far below 100%
49 BENCHMARK_RELATIVE(promiseAndFuture) {
51 Future<int> f = p.getFuture();
56 // The higher the better. At the time of writing, it's only about 40% :(
57 BENCHMARK_RELATIVE(withThen) {
59 Future<int> f = p.getFuture().then(incr<int>);
71 // look for >= 50% relative
72 BENCHMARK_RELATIVE(twoThens) {
76 // look for >= 25% relative
77 BENCHMARK_RELATIVE(fourThens) {
81 // look for >= 1% relative
82 BENCHMARK_RELATIVE(hundredThens) {
86 // Lock contention. Although in practice fulfil()s tend to be temporally
87 // separate from then()s, still sometimes they will be concurrent. So the
88 // higher this number is, the better.
91 BENCHMARK(no_contention) {
92 vector<Promise<int>> promises(10000);
93 vector<Future<int>> futures;
94 std::thread producer, consumer;
97 folly::Baton<> b1, b2;
98 for (auto& p : promises)
99 futures.push_back(p.getFuture());
101 consumer = std::thread([&]{
103 for (auto& f : futures) f.then(incr<int>);
107 producer = std::thread([&]{
109 for (auto& p : promises) p.setValue(42);
116 // The only thing we are measuring is how long fulfil + callbacks take
120 BENCHMARK_RELATIVE(contention) {
121 vector<Promise<int>> promises(10000);
122 vector<Future<int>> futures;
123 std::thread producer, consumer;
125 sem_init(&sem, 0, 0);
128 folly::Baton<> b1, b2;
129 for (auto& p : promises)
130 futures.push_back(p.getFuture());
132 consumer = std::thread([&]{
134 for (auto& f : futures) {
140 producer = std::thread([&]{
142 for (auto& p : promises) {
152 // The astute reader will notice that we're not *precisely* comparing apples
153 // to apples here. Well, maybe it's like comparing Granny Smith to
154 // Braeburn or something. In the serial version, we waited for the futures
155 // to be all set up, but here we are probably still doing that work
156 // (although in parallel). But even though there is more work (on the order
157 // of 2x), it is being done by two threads. Hopefully most of the difference
158 // we see is due to lock contention and not false parallelism.
160 // Be warned that if the box is under heavy load, this will greatly skew
161 // these results (scheduling overhead will begin to dwarf lock contention).
162 // I'm not sure but I'd guess in Windtunnel this will mean large variance,
163 // because I expect they load the boxes as much as they can?
168 int main(int argc, char** argv) {
169 gflags::ParseCommandLineFlags(&argc, &argv, true);
170 folly::runBenchmarks();