Skip to content

Commit

Permalink
Add ThreadSafeCounter benchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Aug 18, 2021
1 parent 53912a8 commit 5b6e2d2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
77 changes: 73 additions & 4 deletions benchmark/bench_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "td/utils/port/thread.h"
#include "td/utils/Slice.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/ThreadSafeCounter.h"

#include "td/telegram/telegram_api.h"
#include "td/telegram/telegram_api.hpp"
Expand Down Expand Up @@ -313,7 +314,7 @@ class AtomicReleaseCasIncBench final : public td::Benchmark {
template <int ThreadN>
std::atomic<td::uint64> AtomicReleaseCasIncBench<ThreadN>::a_;

template <int ThreadN = 2>
template <int ThreadN>
class RwMutexReadBench final : public td::Benchmark {
td::string get_description() const final {
return PSTRING() << "RwMutexRead" << ThreadN;
Expand All @@ -333,7 +334,8 @@ class RwMutexReadBench final : public td::Benchmark {
}
}
};
template <int ThreadN = 2>

template <int ThreadN>
class RwMutexWriteBench final : public td::Benchmark {
td::string get_description() const final {
return PSTRING() << "RwMutexWrite" << ThreadN;
Expand All @@ -353,19 +355,86 @@ class RwMutexWriteBench final : public td::Benchmark {
}
}
};

class ThreadSafeCounterBench final : public td::Benchmark {
static td::ThreadSafeCounter counter_;
int thread_count_;

td::string get_description() const final {
return PSTRING() << "ThreadSafeCounter" << thread_count_;
}
void run(int n) final {
counter_.clear();
td::vector<td::thread> threads;
for (int i = 0; i < thread_count_; i++) {
threads.emplace_back([n] {
for (int i = 0; i < n; i++) {
counter_.add(1);
}
});
}
for (auto &thread : threads) {
thread.join();
}
CHECK(counter_.sum() == n * thread_count_);
}

public:
explicit ThreadSafeCounterBench(int thread_count) : thread_count_(thread_count) {
}
};
td::ThreadSafeCounter ThreadSafeCounterBench::counter_;

template <bool StrictOrder>
class AtomicCounterBench final : public td::Benchmark {
static std::atomic<td::int64> counter_;
int thread_count_;

td::string get_description() const final {
return PSTRING() << "AtomicCounter" << thread_count_;
}
void run(int n) final {
counter_.store(0);
td::vector<td::thread> threads;
for (int i = 0; i < thread_count_; i++) {
threads.emplace_back([n] {
for (int i = 0; i < n; i++) {
counter_.fetch_add(1, StrictOrder ? std::memory_order_seq_cst : std::memory_order_relaxed);
}
});
}
for (auto &thread : threads) {
thread.join();
}
CHECK(counter_.load() == n * thread_count_);
}

public:
explicit AtomicCounterBench(int thread_count) : thread_count_(thread_count) {
}
};
template <bool StrictOrder>
std::atomic<td::int64> AtomicCounterBench<StrictOrder>::counter_;

#endif

int main() {
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(DEBUG));
#if !TD_THREAD_UNSUPPORTED
for (int i = 1; i <= 16; i *= 2) {
td::bench(ThreadSafeCounterBench(i));
td::bench(AtomicCounterBench<false>(i));
td::bench(AtomicCounterBench<true>(i));
}

td::bench(AtomicReleaseIncBench<1>());
td::bench(AtomicReleaseIncBench<2>());
td::bench(AtomicReleaseCasIncBench<1>());
td::bench(AtomicReleaseCasIncBench<2>());
td::bench(RwMutexWriteBench<1>());
td::bench(RwMutexReadBench<1>());
td::bench(RwMutexWriteBench<>());
td::bench(RwMutexReadBench<>());
td::bench(RwMutexWriteBench<2>());
td::bench(RwMutexReadBench<2>());
#endif
#if !TD_WINDOWS
td::bench(UtimeBench());
Expand Down
4 changes: 4 additions & 0 deletions tdutils/td/utils/ThreadSafeCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class ThreadSafeCounter {
return counter_.sum(0);
}

void clear() {
counter_.clear();
}

private:
ThreadSafeMultiCounter<1> counter_;
};
Expand Down

0 comments on commit 5b6e2d2

Please sign in to comment.