Move ConcurrentHashMap test out of namespace td.

This commit is contained in:
levlam 2022-10-21 13:18:58 +03:00
parent f12ca7fbcb
commit 60f094bdf2
1 changed files with 25 additions and 29 deletions

View File

@ -32,19 +32,17 @@
#include <junction/ConcurrentMap_Linear.h>
#endif
namespace td {
// Non resizable HashMap. Just an example
template <class KeyT, class ValueT>
class ArrayHashMap {
public:
explicit ArrayHashMap(size_t n) : array_(n) {
explicit ArrayHashMap(std::size_t n) : array_(n) {
}
struct Node {
std::atomic<KeyT> key{KeyT{}};
std::atomic<ValueT> value{ValueT{}};
};
static std::string get_name() {
static td::string get_name() {
return "ArrayHashMap";
}
KeyT empty_key() const {
@ -60,15 +58,15 @@ class ArrayHashMap {
}
private:
AtomicHashArray<KeyT, std::atomic<ValueT>> array_;
td::AtomicHashArray<KeyT, std::atomic<ValueT>> array_;
};
template <class KeyT, class ValueT>
class ConcurrentHashMapMutex {
public:
explicit ConcurrentHashMapMutex(size_t) {
explicit ConcurrentHashMapMutex(std::size_t) {
}
static std::string get_name() {
static td::string get_name() {
return "ConcurrentHashMapMutex";
}
void insert(KeyT key, ValueT value) {
@ -85,7 +83,7 @@ class ConcurrentHashMapMutex {
}
private:
Mutex mutex_;
td::Mutex mutex_;
#if TD_HAVE_ABSL
absl::flat_hash_map<KeyT, ValueT> hash_map_;
#else
@ -98,7 +96,7 @@ class ConcurrentHashMapSpinlock {
public:
explicit ConcurrentHashMapSpinlock(size_t) {
}
static std::string get_name() {
static td::string get_name() {
return "ConcurrentHashMapSpinlock";
}
void insert(KeyT key, ValueT value) {
@ -115,7 +113,7 @@ class ConcurrentHashMapSpinlock {
}
private:
SpinLock spinlock_;
td::SpinLock spinlock_;
#if TD_HAVE_ABSL
absl::flat_hash_map<KeyT, ValueT> hash_map_;
#else
@ -129,7 +127,7 @@ class ConcurrentHashMapLibcuckoo {
public:
explicit ConcurrentHashMapLibcuckoo(size_t) {
}
static std::string get_name() {
static td::string get_name() {
return "ConcurrentHashMapLibcuckoo";
}
void insert(KeyT key, ValueT value) {
@ -149,9 +147,9 @@ class ConcurrentHashMapLibcuckoo {
template <class KeyT, class ValueT>
class ConcurrentHashMapJunction {
public:
explicit ConcurrentHashMapJunction(size_t size) : hash_map_() {
explicit ConcurrentHashMapJunction(std::size_t size) : hash_map_() {
}
static std::string get_name() {
static td::string get_name() {
return "ConcurrentHashMapJunction";
}
void insert(KeyT key, ValueT value) {
@ -174,25 +172,23 @@ class ConcurrentHashMapJunction {
};
#endif
} // namespace td
template <class HashMap>
class HashMapBenchmark final : public td::Benchmark {
struct Query {
int key;
int value;
};
std::vector<Query> queries;
td::vector<Query> queries;
td::unique_ptr<HashMap> hash_map;
size_t threads_n = 16;
static constexpr size_t MUL = 7273; //1000000000 + 7;
std::size_t threads_n = 16;
static constexpr std::size_t MUL = 7273; //1000000000 + 7;
int n_ = 0;
public:
explicit HashMapBenchmark(size_t threads_n) : threads_n(threads_n) {
explicit HashMapBenchmark(std::size_t threads_n) : threads_n(threads_n) {
}
std::string get_description() const final {
td::string get_description() const final {
return HashMap::get_name();
}
void start_up_n(int n) final {
@ -203,11 +199,11 @@ class HashMapBenchmark final : public td::Benchmark {
void run(int n) final {
n = n_;
std::vector<td::thread> threads;
td::vector<td::thread> threads;
for (size_t i = 0; i < threads_n; i++) {
size_t l = n * i / threads_n;
size_t r = n * (i + 1) / threads_n;
for (std::size_t i = 0; i < threads_n; i++) {
std::size_t l = n * i / threads_n;
std::size_t r = n * (i + 1) / threads_n;
threads.emplace_back([l, r, this] {
for (size_t i = l; i < r; i++) {
auto x = td::narrow_cast<int>((i + 1) * MUL % n_) + 3;
@ -240,14 +236,14 @@ static void bench_hash_map() {
TEST(ConcurrentHashMap, Benchmark) {
bench_hash_map<td::ConcurrentHashMap<int, int>>();
bench_hash_map<td::ArrayHashMap<int, int>>();
bench_hash_map<td::ConcurrentHashMapSpinlock<int, int>>();
bench_hash_map<td::ConcurrentHashMapMutex<int, int>>();
bench_hash_map<ArrayHashMap<int, int>>();
bench_hash_map<ConcurrentHashMapSpinlock<int, int>>();
bench_hash_map<ConcurrentHashMapMutex<int, int>>();
#if TD_WITH_LIBCUCKOO
bench_hash_map<td::ConcurrentHashMapLibcuckoo<int, int>>();
bench_hash_map<ConcurrentHashMapLibcuckoo<int, int>>();
#endif
#if TD_WITH_JUNCTION
bench_hash_map<td::ConcurrentHashMapJunction<int, int>>();
bench_hash_map<ConcurrentHashMapJunction<int, int>>();
#endif
}