Skip to content

Commit

Permalink
bench: Stat bench
Browse files Browse the repository at this point in the history
  • Loading branch information
shramov committed Dec 25, 2024
1 parent 8bfb996 commit 9de363e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions bench/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ executable('bench-callback', sources: ['callback.cc'], dependencies: [fmt, tll])
executable('bench-callback-standalone', sources: ['callback-standalone.cc'])
executable('bench-ring', sources: ['ring.cc', '../src/ring.c'], dependencies: [fmt], include_directories: include)
executable('bench-refcount', sources: ['refcount.cc'], dependencies: [fmt, threads], include_directories: include)
executable('bench-stat', sources: ['stat.cc'], dependencies: [fmt, threads], include_directories: include)

custom_target('tll-bench-channel.1'
, output : 'tll-bench-channel.1'
Expand Down
80 changes: 80 additions & 0 deletions bench/stat.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <tll/stat.h>
#include <tll/util/bench.h>

#include <thread>

template <typename T>
struct StatT
{
tll::stat::FieldT<T, tll::stat::Sum, tll::stat::Unknown, 'f', '0'> f0;
tll::stat::FieldT<T, tll::stat::Min, tll::stat::Unknown, 'f', '1'> f1;
tll::stat::FieldT<T, tll::stat::Max, tll::stat::Unknown, 'f', '2'> f2;
tll::stat::GroupT<T, tll::stat::Unknown, 'f', '3'> grp;
};

using Stat = StatT<tll_stat_int_t>;

int acquire(tll::stat::Block<Stat> * block, int value)
{
if (auto p = block->acquire(); p) {
p->f0 = value;
block->release(p);
}
return value;
}

int acquire_wait(tll::stat::Block<Stat> * block, int value)
{
if (auto p = block->acquire_wait(); p) {
p->f0 = 1;
block->release(p);
}
return value;
}

template <typename F, typename ... Args>
inline int apply_func(tll::stat::Block<Stat> * block, F func, Args... args)
{
if (auto p = block->acquire(); p) {
func(p, std::forward<Args>(args)...);
block->release(p);
}
return 1;
}

int apply(tll::stat::Block<Stat> * block, int value)
{
apply_func(block, [value](auto * p) { p->f0 = value; });
return value;
}

int main()
{
using namespace std::chrono_literals;
constexpr unsigned count = 10000000;
tll::stat::Block<Stat> block { "integer" };
tll::bench::prewarm(100ms);
tll::bench::timeit(count, "acquire", acquire, &block, 1);
apply_func(&block, [](auto * p) { fmt::print("f0: {}\n", p->f0.value()); p->f0.reset(); });
tll::bench::timeit(count, "acquire loop", acquire_wait, &block, 1);
apply_func(&block, [](auto * p) { fmt::print("f0: {}\n", p->f0.value()); p->f0.reset(); });
tll::bench::timeit(count, "apply", apply, &block, 1);
apply_func(&block, [](auto * p) { fmt::print("f0: {}\n", p->f0.value()); p->f0.reset(); });
bool stop = false;
auto thread = std::thread([](auto * stop, auto * block) {
while (!*stop) {
apply_func(block, [](auto p) { p->f1 = 1; });
std::this_thread::yield();
}
}, &stop, &block);
tll::bench::prewarm(1ms);
tll::bench::timeit(count, "thread + acquire", acquire, &block, 1);
apply_func(&block, [](auto * p) { fmt::print("f0: {}\n", p->f0.value()); p->f0.reset(); });
tll::bench::timeit(count, "thread + acquire loop", acquire_wait, &block, 1);
apply_func(&block, [](auto * p) { fmt::print("f0: {}\n", p->f0.value()); p->f0.reset(); });
tll::bench::timeit(count, "thread + apply", apply, &block, 1);
apply_func(&block, [](auto * p) { fmt::print("f0: {}\n", p->f0.value()); p->f0.reset(); });
stop = true;
thread.join();
return 0;
}

0 comments on commit 9de363e

Please sign in to comment.