Commit 4269fd6e authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook Github Bot

CollectAll benchmark

Reviewed By: yfeldblum

Differential Revision: D17892633

fbshipit-source-id: 1b4b2cffb040b1abf709ea2ccd4015426398dda0
parent 15f1d999
/*
* Copyright 2018-present 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/executors/CPUThreadPoolExecutor.h>
#include <folly/experimental/coro/BlockingWait.h>
#include <folly/experimental/coro/Collect.h>
#include <folly/experimental/coro/Generator.h>
#include <folly/experimental/coro/Task.h>
#include <folly/synchronization/Baton.h>
void doWork() {}
folly::CPUThreadPoolExecutor executor(4);
void collectAllFuture(size_t batchSize) {
std::vector<folly::Future<folly::Unit>> futures;
for (size_t i = 0; i < batchSize; ++i) {
futures.emplace_back(folly::via(&executor, [] { doWork(); }));
}
folly::collectAll(std::move(futures)).get();
}
void collectAllFutureInline(size_t batchSize) {
std::vector<folly::Future<folly::Unit>> futures;
for (size_t i = 0; i < batchSize; ++i) {
futures.emplace_back(folly::via(&executor, [] { doWork(); })
.via(&folly::InlineExecutor::instance()));
}
folly::collectAll(std::move(futures)).get();
}
void collectAllSemiFuture(size_t batchSize) {
std::vector<folly::SemiFuture<folly::Unit>> futures;
for (size_t i = 0; i < batchSize; ++i) {
futures.emplace_back(folly::via(&executor, [] { doWork(); }));
}
folly::collectAllSemiFuture(std::move(futures)).get();
}
folly::coro::Task<void> co_doWork() {
co_await[]()->folly::coro::Task<void> {
doWork();
co_return;
}
().scheduleOn(&executor);
}
void collectAllCoro(size_t batchSize) {
folly::coro::blockingWait([&]() -> folly::coro::Task<void> {
co_await folly::coro::collectAllRange(
[&]() -> folly::coro::Generator<folly::coro::Task<void>&&> {
for (size_t i = 0; i < batchSize; ++i) {
co_yield co_doWork();
}
}());
}());
}
void collectAllBaton(size_t batchSize) {
folly::Baton<> baton;
std::shared_ptr<folly::Baton<>> batonGuard(
&baton, [](folly::Baton<>* baton) { baton->post(); });
for (size_t i = 0; i < batchSize; ++i) {
executor.add([batonGuard]() { doWork(); });
}
batonGuard.reset();
baton.wait();
}
BENCHMARK(collectAllFuture10000, iters) {
for (size_t i = 0; i < iters; ++i) {
collectAllFuture(10000);
}
}
BENCHMARK(collectAllFutureInline10000, iters) {
for (size_t i = 0; i < iters; ++i) {
collectAllFutureInline(10000);
}
}
BENCHMARK(collectAllSemiFuture10000, iters) {
for (size_t i = 0; i < iters; ++i) {
collectAllSemiFuture(10000);
}
}
BENCHMARK(collectAllCoro10000, iters) {
for (size_t i = 0; i < iters; ++i) {
collectAllCoro(10000);
}
}
BENCHMARK(collectAllBaton10000, iters) {
for (size_t i = 0; i < iters; ++i) {
collectAllBaton(10000);
}
}
BENCHMARK(collectAllFuture100, iters) {
for (size_t i = 0; i < iters; ++i) {
collectAllFuture(100);
}
}
BENCHMARK(collectAllFutureInline100, iters) {
for (size_t i = 0; i < iters; ++i) {
collectAllFutureInline(100);
}
}
BENCHMARK(collectAllSemiFuture100, iters) {
for (size_t i = 0; i < iters; ++i) {
collectAllSemiFuture(100);
}
}
BENCHMARK(collectAllCoro100, iters) {
for (size_t i = 0; i < iters; ++i) {
collectAllCoro(100);
}
}
BENCHMARK(collectAllBaton100, iters) {
for (size_t i = 0; i < iters; ++i) {
collectAllBaton(100);
}
}
int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
folly::runBenchmarks();
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment