add some benchmarks
authorHans Fugal <fugalh@fb.com>
Thu, 2 Oct 2014 17:39:53 +0000 (10:39 -0700)
committerAndrii Grynenko <andrii@fb.com>
Wed, 15 Oct 2014 00:48:13 +0000 (17:48 -0700)
Summary: For great speed

Test Plan:
$ fbmake opt && _bin/folly/wangle/wangle-bench
============================================================================
folly/wangle/test/Benchmark.cpp                 relative  time/iter  iters/s
============================================================================
constantFuture                                             235.92ns    4.24M
promiseAndFuture                                 100.37%   235.04ns    4.25M
withThen                                          41.97%   562.17ns    1.78M
----------------------------------------------------------------------------
oneThen                                                    539.03ns    1.86M
twoThens                                          64.01%   842.14ns    1.19M
fourThens                                         39.27%     1.37us  728.62K
hundredThens                                       1.82%    29.63us   33.75K
============================================================================

Reviewed By: davejwatson@fb.com

Subscribers: trunkagent, net-systems@, fugalh, exa, njormrod

FB internal diff: D1587871

folly/wangle/test/Benchmark.cpp [new file with mode: 0644]

diff --git a/folly/wangle/test/Benchmark.cpp b/folly/wangle/test/Benchmark.cpp
new file mode 100644 (file)
index 0000000..7d18db6
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2014 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <folly/Benchmark.h>
+#include <folly/wangle/Future.h>
+#include <folly/wangle/Promise.h>
+
+using namespace folly::wangle;
+
+template <class T>
+T incr(Try<T>&& t) {
+  return t.value() + 1;
+}
+
+void someThens(size_t n) {
+  auto f = makeFuture<int>(42);
+  for (size_t i = 0; i < n; i++) {
+    f = f.then(incr<int>);
+  }
+}
+
+BENCHMARK(constantFuture) {
+  makeFuture(42);
+}
+
+// This shouldn't get too far below 100%
+BENCHMARK_RELATIVE(promiseAndFuture) {
+  Promise<int> p;
+  Future<int> f = p.getFuture();
+  p.setValue(42);
+  f.value();
+}
+
+// The higher the better. At the time of writing, it's only about 40% :(
+BENCHMARK_RELATIVE(withThen) {
+  Promise<int> p;
+  Future<int> f = p.getFuture().then(incr<int>);
+  p.setValue(42);
+  f.value();
+}
+
+// thens
+BENCHMARK_DRAW_LINE()
+
+BENCHMARK(oneThen) {
+  someThens(1);
+}
+
+// look for >= 50% relative
+BENCHMARK_RELATIVE(twoThens) {
+  someThens(2);
+}
+
+// look for >= 25% relative
+BENCHMARK_RELATIVE(fourThens) {
+  someThens(4);
+}
+
+// look for >= 1% relative
+BENCHMARK_RELATIVE(hundredThens) {
+  someThens(100);
+}
+
+int main() {
+  folly::runBenchmarks();
+}