-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathbench-hashtable-mpmc-op-get.cpp
162 lines (132 loc) · 4.83 KB
/
bench-hashtable-mpmc-op-get.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Copyright (C) 2018-2023 Daniele Salvatore Albano
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD license. See the LICENSE file for details.
**/
#include <cstdio>
#include <cstring>
#include <benchmark/benchmark.h>
#include "misc.h"
#include "exttypes.h"
#include "xalloc.h"
#include "clock.h"
#include "config.h"
#include "thread.h"
#include "memory_fences.h"
#include "transaction.h"
#include "spinlock.h"
#include "log/log.h"
#include "memory_fences.h"
#include "utils_cpu.h"
#include "fiber/fiber.h"
#include "fiber/fiber_scheduler.h"
#include "data_structures/double_linked_list/double_linked_list.h"
#include "data_structures/queue_mpmc/queue_mpmc.h"
#include "data_structures/hashtable/mcmp/hashtable.h"
#include "worker/worker_stats.h"
#include "worker/worker_context.h"
#include "worker/worker.h"
#include "data_structures/hashtable/mcmp/hashtable_op_get.h"
#include "../tests/unit_tests/support.h"
#include "../tests/unit_tests/data_structures/hashtable/mpmc/fixtures-hashtable-mpmc.h"
#include "benchmark-program.hpp"
#include "benchmark-support.hpp"
// It is possible to control the amount of threads used for the test tuning the two defines below
#define TEST_THREADS_RANGE_BEGIN (1)
#define TEST_THREADS_RANGE_END (utils_cpu_count())
static void hashtable_op_get_not_found_key(benchmark::State& state) {
static hashtable_t* hashtable;
hashtable_value_data_t value;
worker_context_t worker_context = { 0 };
worker_context.worker_index = state.thread_index();
worker_context_set(&worker_context);
transaction_set_worker_index(worker_context.worker_index);
if (state.thread_index() == 0) {
hashtable = test_support_init_hashtable(state.range(0));
}
test_support_set_thread_affinity(state.thread_index());
for (auto _ : state) {
transaction_t transaction = { 0 };
transaction_acquire(&transaction);
benchmark::DoNotOptimize(hashtable_mcmp_op_get(
hashtable,
0,
&transaction,
test_key_1,
test_key_1_len,
&value));
transaction_release(&transaction);
}
if (state.thread_index() == 0) {
hashtable_mcmp_free(hashtable);
}
}
static void hashtable_op_get_single_key_external(benchmark::State& state) {
static hashtable_t* hashtable;
static hashtable_bucket_index_t bucket_index;
static hashtable_chunk_index_t chunk_index;
static hashtable_chunk_slot_index_t chunk_slot_index;
hashtable_value_data_t value;
bool result;
char error_message[150] = {0};
worker_context_t worker_context = { 0 };
worker_context.worker_index = state.thread_index();
worker_context_set(&worker_context);
transaction_set_worker_index(worker_context.worker_index);
if (state.thread_index() == 0) {
hashtable = test_support_init_hashtable(state.range(0));
bucket_index = test_key_1_hash % hashtable->ht_current->buckets_count;
chunk_index = HASHTABLE_TO_CHUNK_INDEX(bucket_index);
chunk_slot_index = 0;
char *test_key_1_clone = (char*)xalloc_alloc(test_key_1_len + 1);
strncpy(test_key_1_clone, test_key_1, test_key_1_len);
HASHTABLE_SET_KEY_DB_0_BY_INDEX(
chunk_index,
chunk_slot_index,
test_key_1_hash,
test_key_1_clone,
test_key_1_len,
test_value_1);
}
test_support_set_thread_affinity(state.thread_index());
for (auto _ : state) {
transaction_t transaction = { 0 };
transaction_acquire(&transaction);
benchmark::DoNotOptimize((result = hashtable_mcmp_op_get(
hashtable,
0,
&transaction,
test_key_1,
test_key_1_len,
&value)));
transaction_release(&transaction);
if (!result) {
sprintf(
error_message,
"Unable to get the key <%s> with bucket index <%lu>, chunk index <%lu> and chunk slot index <%u> for the thread <%d>",
test_key_1,
bucket_index,
chunk_index,
chunk_slot_index,
state.thread_index());
state.SkipWithError(error_message);
break;
}
}
if (state.thread_index() == 0) {
hashtable_mcmp_free(hashtable);
}
}
static void BenchArguments(benchmark::internal::Benchmark* b) {
b
->Arg(256)
->ThreadRange(TEST_THREADS_RANGE_BEGIN, TEST_THREADS_RANGE_END)
->Iterations(10000)
->DisplayAggregatesOnly(false);
}
BENCHMARK(hashtable_op_get_not_found_key)
->Apply(BenchArguments);
BENCHMARK(hashtable_op_get_single_key_external)
->Apply(BenchArguments);