feat add benchmark
Some checks failed
android / build (push) Successful in 5m9s
linux-arm-gcc / linux-gcc-arm (Debug) (push) Failing after 5m8s
linux-arm-gcc / linux-gcc-arm (Release) (push) Failing after 6m20s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Release) (push) Failing after 8m11s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Debug) (push) Failing after 8m57s
linux-mips-gcc / linux-gcc-mipsel (Debug) (push) Failing after 9m33s
linux-mips-gcc / linux-gcc-mipsel (Release) (push) Failing after 6m28s
linux-arm-gcc / linux-gcc-armhf (Debug) (push) Failing after 11m55s
linux-arm-gcc / linux-gcc-armhf (Release) (push) Failing after 12m11s
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Failing after 7m21s
linux-riscv64-gcc / linux-gcc-riscv64 (Debug) (push) Failing after 6m48s
linux-x64-gcc / linux-gcc (Debug) (push) Failing after 6m58s
linux-x64-gcc / linux-gcc (Release) (push) Failing after 5m19s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Failing after 12m5s
linux-riscv64-gcc / linux-gcc-riscv64 (Release) (push) Failing after 8m28s
linux-x86-gcc / linux-gcc (Release) (push) Failing after 7m37s
linux-x86-gcc / linux-gcc (Debug) (push) Failing after 10m43s

This commit is contained in:
tqcq 2024-06-12 21:48:07 +08:00
parent 8d1b951a25
commit 7248d70d38
4 changed files with 69 additions and 18 deletions

View File

@ -362,6 +362,8 @@ if(TILE_BUILD_BENCHMARKS)
target_sources(tile_bm_all PRIVATE ${benchmark_file})
endmacro()
tile_add_bm(fiber_detail_fiber_benchmark
"tile/fiber/detail/fiber_benchmark.cc")
tile_add_bm(base_casting_benchmark "tile/base/casting_benchmark.cc")
tile_add_bm(base_thread_mutex_benchmark "tile/base/thread/mutex_benchmark.cc")
tile_add_bm(base_encoding_benchmark "tile/base/encoding_benchmark.cc")

View File

@ -79,11 +79,13 @@ Fiber::~Fiber() {
}
void Fiber::Resume() {
TILE_CHECK_NE(ctx_->fctx, nullptr);
auto caller = Current();
TILE_CHECK_NE(caller, this, "Can't `Resume()` self");
SetCurrent(this);
// TILE_LOG_INFO("Resume before proc: {}", fmt::ptr(&proc_));
ctx_->fctx = jump_fcontext(ctx_->fctx, nullptr).fctx;
ctx_->fctx =
jump_fcontext(internal::Exchange(ctx_->fctx, nullptr), nullptr).fctx;
// TILE_LOG_INFO("Resume after proc: {}", fmt::ptr(&proc_));
SetCurrent(caller);
}

View File

@ -0,0 +1,37 @@
#include "tile/fiber/detail/fiber.h"
#include "tile/base/random.h"
#include "tile/base/string.h"
#include "benchmark/benchmark.h"
namespace tile {
namespace fiber {
namespace detail {
void Benchmark_FiberSwitch(benchmark::State &state) {
std::unique_ptr<Fiber> master_fiber;
int switch_cnt = 0;
master_fiber = Fiber::Create([&] {
while (state.KeepRunning()) {
std::unique_ptr<Fiber> worker_fiber1 =
Fiber::Create([&] { ++switch_cnt; });
auto start = std::chrono::steady_clock::now();
worker_fiber1->Resume();
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::duration<double>>(
end - start);
state.SetIterationTime(elapsed.count());
}
});
master_fiber->Resume();
state.counters["switch_cnt"] = switch_cnt;
}
} // namespace detail
} // namespace fiber
} // namespace tile
BENCHMARK(tile::fiber::detail::Benchmark_FiberSwitch)->UseManualTime();

View File

@ -7,30 +7,40 @@ namespace fiber {
namespace detail {
TEST(Fiber, Base) {
constexpr auto kMaxCnt = 5000000;
constexpr auto kMaxCnt = 100 * 1000;
int cnt = 0;
int resume_cnt = 0;
// 0 -> master fiber
// [1, 9] -> worker fibers
std::unique_ptr<Fiber> master_fiber;
master_fiber = Fiber::Create([&] {
TILE_LOG_INFO("master fiber");
// ASSERT_EQ(cnt, 0);
ASSERT_EQ(Fiber::MasterFiber(), master_fiber.get());
ASSERT_EQ(Fiber::Current(), master_fiber.get());
ASSERT_EQ(cnt, 0);
while (cnt < kMaxCnt) {
std::unique_ptr<Fiber> worker_fiber = Fiber::Create([&] {
ASSERT_EQ(Fiber::Current(), worker_fiber.get());
++cnt;
});
int old = cnt;
worker_fiber->Resume();
ASSERT_EQ(old + 1, cnt);
ASSERT_EQ(Fiber::Current(), master_fiber.get());
}
ASSERT_EQ(cnt, kMaxCnt);
});
auto master_fiber = Fiber::Create();
Fiber::SetMasterFiber(master_fiber.get());
Fiber::SetCurrent(master_fiber.get());
while (cnt < kMaxCnt) {
std::unique_ptr<Fiber> worker_fiber = Fiber::Create([&] {
ASSERT_EQ(Fiber::Current(), worker_fiber.get());
++cnt;
});
int old = cnt;
worker_fiber->Resume();
++resume_cnt;
ASSERT_EQ(old + 1, cnt);
ASSERT_EQ(Fiber::Current(), Fiber::MasterFiber());
}
ASSERT_EQ(resume_cnt, kMaxCnt);
// Fiber::SetCurrent(master_fiber.get());
// master_fiber->Resume();
master_fiber->Resume();
}
} // namespace detail