-
Notifications
You must be signed in to change notification settings - Fork 1
/
speedtest.c
133 lines (106 loc) · 3.26 KB
/
speedtest.c
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
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <pthread.h>
#include <assert.h>
#include <time.h> // srand
#include "num_format.h"
// Set number of collisions allowed (max is: 2^JH_NRBITS-1)
// #define JH_NRBITS 5
// Set word size to be used by the hash table
// #define JH_FORCE_64 1
// #define JH_FORCE_32 1
#include "jellyhash.h"
void print_usage()
{
printf("usage: speedtest [Options] <num_ops>\n"
" Test hash table speed. Table capacity is: b*2^l. Memory is: (k-l+%i)*capacity.\n"
" Rehash limit is: %i\n"
" -k <k> Element size\n"
" -l <l> Bits for bucket addressing\n"
" -b <b> Number of elements per bucket\n"
" -t <t> Number of threads to use\n", JH_NRBITS+1, (1<<JH_NRBITS)-1);
exit(EXIT_FAILURE);
}
#define die(fmt,...) do { \
fprintf(stderr, "[%s:%i] Error: %s() "fmt"\n", __FILE__, __LINE__, __func__, ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while(0)
typedef struct {
int threadid;
JellyHash *jhash;
size_t num_ops;
} TestThread;
static inline void* speedtest(void *ptr)
{
TestThread *thread = (TestThread*)ptr;
srand(time(NULL) + getpid() + thread->threadid);
JellyHash *jhash = thread->jhash;
size_t num_ops = thread->num_ops;
size_t i, kwords = jh_nwords(jhash->k);
HWord bkmer[kwords], result[kwords];
int found;
HKey hpos;
memset(bkmer, 0, sizeof(HWord)*kwords);
memset(result, 0, sizeof(HWord)*kwords);
for(i = 0; i < num_ops; i++)
{
bkmer[0] = num_ops*thread->threadid + i;
hpos = jelly_hash_find(jhash, (char*)bkmer, 1, &found);
if(hpos == JHASH_NULL) {
jelly_hash_print_stats(jhash, stdout);
die("Hash full!");
}
}
pthread_exit(NULL);
}
int main(int argc, char **argv)
{
uint32_t k = 62, l = 20, b = 32, num_of_threads = 1;
size_t num_ops;
int c;
while ((c = getopt(argc, argv, "k:l:b:t:h")) >= 0)
if (c == 'k') k = atoi(optarg);
else if (c == 'l') l = atoi(optarg);
else if (c == 'b') b = atoi(optarg);
else if (c == 't') num_of_threads = atoi(optarg);
else if (c == 'h') print_usage();
if(optind == argc)
print_usage();
num_ops = atol(argv[argc-1]);
num_ops /= num_of_threads;
/* initialize random seed: */
srand(time(NULL) + getpid());
JellyHash jhash;
jelly_hash_alloc(&jhash, l, b, k);
char memstr[50];
bytes_to_str((jhash.nbins * jhash.binsize * jhash.keylen) / 8, 1, memstr);
printf("JellyHash Test k: %u, l: %u, b: %u, threads: %u mem: %s\n",
k, l, b, num_of_threads, memstr);
jelly_hash_print_stats(&jhash, stdout);
// TestThread tt = {.threadid = 101, .jhash = &jhash};
// speedtest(&tt);
uint32_t i;
pthread_t threads[num_of_threads];
TestThread threaddata[num_of_threads];
for(i = 0; i < num_of_threads; i++) {
threaddata[i].threadid = i;
threaddata[i].jhash = &jhash;
threaddata[i].num_ops = num_ops;
}
if(num_of_threads <= 1) {
speedtest(&threaddata[0]);
}
else {
for(i = 0; i < num_of_threads; i++)
if(pthread_create(&threads[i], NULL, speedtest, &threaddata[i]) != 0)
die("Creating thread failed\n");
for(i = 0; i < num_of_threads; i++)
if(pthread_join(threads[i], NULL) != 0)
die("Creating thread failed\n");
}
jelly_hash_print_stats(&jhash, stdout);
jelly_hash_dealloc(&jhash);
return EXIT_SUCCESS;
}