Skip to content

Instantly share code, notes, and snippets.

@yuxuanchen1997
Last active September 6, 2024 15:43
Show Gist options
  • Save yuxuanchen1997/8a13a22bf6e1c146f0f7194dcdd92581 to your computer and use it in GitHub Desktop.
Save yuxuanchen1997/8a13a22bf6e1c146f0f7194dcdd92581 to your computer and use it in GitHub Desktop.
Pure Call Overhead Benchmark for folly::coro::Task. Credit to original author @keshpr
#include <coroutine>
#include <folly/experimental/coro/BlockingWait.h>
#include <folly/experimental/coro/Task.h>
#include <folly/init/Init.h>
#include <benchmark/benchmark.h>
#include <gflags/gflags.h>
DEFINE_int32(numThreads, 1, "");
template <size_t ArgRetSize>
struct Val {
char arr[ArgRetSize];
};
template <
template <typename>
typename TTask,
size_t RetSize,
size_t ArgSize,
size_t Depth>
struct echo_awaitable_task {
static TTask<Val<RetSize>> task(Val<ArgSize> arg) {
co_return co_await echo_awaitable_task<TTask, RetSize, ArgSize, Depth - 1>::
task(arg);
}
};
template <template <typename> typename TTask, size_t RetSize, size_t ArgSize>
struct echo_awaitable_task<TTask, RetSize, ArgSize, 0> {
static TTask<Val<RetSize>> task(Val<ArgSize> arg) {
Val<RetSize> ret;
co_return ret;
}
};
template <size_t RetSize, size_t ArgSize, size_t Depth>
void BM_EchoFollyCoroTaskWithDepth(benchmark::State& state) {
for (auto _ : state) {
Val<ArgSize> arg;
auto task =
echo_awaitable_task<folly::coro::Task, RetSize, ArgSize, Depth>::task(
arg);
auto result = folly::coro::blockingWait(std::move(task));
benchmark::DoNotOptimize(result);
}
}
#define BENCH_(name) \
BENCHMARK(name) \
->Threads(FLAGS_numThreads) \
->MeasureProcessCPUTime() \
->UseRealTime()
#define COMMA ,
#define BENCH_DEPTH_FOLLY_CORO_(ret_size, arg_size, depth) \
BENCH_(BM_EchoFollyCoroTaskWithDepth<ret_size COMMA arg_size COMMA depth>);
#define BENCH_DEPTH_FOLLY_CORO(depth) \
BENCH_DEPTH_FOLLY_CORO_(0, 0, depth) \
BENCH_DEPTH_FOLLY_CORO_(0, 70, depth) \
BENCH_DEPTH_FOLLY_CORO_(70, 0, depth) \
BENCH_DEPTH_FOLLY_CORO_(70, 70, depth) \
BENCH_DEPTH_FOLLY_CORO_(24, 0, depth) \
BENCH_DEPTH_FOLLY_CORO_(24, 32, depth) \
BENCH_DEPTH_FOLLY_CORO_(128, 128, depth)
BENCH_DEPTH_FOLLY_CORO(0)
BENCH_DEPTH_FOLLY_CORO(20)
BENCH_DEPTH_FOLLY_CORO(30)
BENCH_DEPTH_FOLLY_CORO(40)
int main(int argc, char **argv) {
benchmark::Initialize(&argc, argv);
folly::init(&argc, &argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment