-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvulcan.c
172 lines (143 loc) · 4.57 KB
/
vulcan.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
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
163
164
165
166
167
168
169
170
171
172
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <limits.h>
#include <pthread.h>
#include "hiredis/hiredis.h"
#define NUM_THREADS 40
#define COMMAND_LEN 10
#define STRING 1
#define LIST 2
#define SET 3
#define ZSET 4
// defaults
char *hostname = "127.0.0.1";
int port = 6390;
void usage() {
printf("usage: ./vulcan [number of keys] [string|list|set|zset] -h [host] -p [port] \n");
exit(1);
}
struct vulcan_params {
int unique_id;
long count;
int type;
char command[COMMAND_LEN];
} typedef vulcan_params_t;
void generate_data(void *vulcan_params_pt) {
vulcan_params_t *vulcan_params = (vulcan_params_t*) vulcan_params_pt;
int id = vulcan_params->unique_id;
long count = vulcan_params->count;
int type = vulcan_params->type;
char command[COMMAND_LEN];
strcpy(command, vulcan_params->command);
// Get a Redis context and connection
redisContext *context;
struct timeval timeout = { 2, 500000 }; // 2.5 seconds
context = redisConnectWithTimeout(hostname, port, timeout);
if (context == NULL || context->err) {
if (context) {
fprintf(stderr, "[vulcan] Redis connection error: %s\n", context->errstr);
redisFree(context);
} else {
fprintf(stderr, "[vulcan] Redis connection error: can't allocate redis context\n");
}
exit(1);
}
redisReply *reply;
for (long i = 0; i < count; i++) {
// build the string key, using the unique id and the iteration number
char key[80];
sprintf(key, "%i:%i:%ld", type, id, i);
// set the key with a test value, special case for ZSET
if (type == ZSET) {
reply = redisCommand(context, "%s %s %s %i", command, key, "test", rand());
} else {
reply = redisCommand(context, "%s %s %s", command, key, "test");
}
freeReplyObject(reply);
}
redisFree(context);
free(vulcan_params_pt);
pthread_exit(NULL);
}
int main(int argc, char **argv) {
if (argc < 3) usage();
// Parse arguments
long count = strtol(argv[1], NULL, 10);
if (errno == ERANGE) {
fprintf(stderr, "[vulcan] Requested count is greater than LONG_MAX, using %ld.\n", LONG_MAX);
}
char *type = argv[2];
if (argc > 3) {
if (strncmp(argv[3], "-h", 2) == 0) {
if (argv[4] != NULL) {
hostname = argv[4];
} else {
usage();
}
} else {
usage();
}
if (argc > 5) {
if (strncmp(argv[5], "-p", 2) == 0) {
if (argv[6] != NULL) {
port = atoi(argv[6]);
} else {
usage();
}
} else {
usage();
}
}
}
fprintf(stderr, "[vulcan] Using hostname %s and port %i\n", hostname, port);
// Determine Redis datatype
int data_type;
char command[COMMAND_LEN];
if (strncmp(type, "string", 6) == 0) {
data_type = STRING;
strcpy(command, "SET");
} else if (strncmp(type, "list", 4) == 0) {
data_type = LIST;
strcpy(command, "LPUSH");
} else if (strncmp(type, "set", 3) == 0) {
data_type = SET;
strcpy(command, "SADD");
} else if (strncmp(type, "zset", 4) == 0) {
data_type = ZSET;
strcpy(command, "ZADD");
} else {
usage(); // invalid datatype
}
// Do the actual work and time it. We'll print timing data to stderr.
struct timeval t1, t2;
double elapsed_time;
gettimeofday(&t1, NULL);
// Each thread does 1/NUM_THREADS of the work
pthread_t threads[NUM_THREADS];
long per_thread = count / NUM_THREADS;
long t;
for (t = 0; t < NUM_THREADS; t++) {
// build the vulcan_params struct
vulcan_params_t *data = (vulcan_params_t*) malloc(sizeof(vulcan_params_t));
if (data == NULL) {
fprintf(stderr, "malloc failed");
exit(-1);
}
data->unique_id = t;
data->count = per_thread;
data->type = data_type;
strcpy(data->command, command);
pthread_create(&threads[t], NULL, (void *) generate_data, (void *) data);
}
for (t = 0; t < NUM_THREADS; t++) {
(void) pthread_join(threads[t], NULL);
}
gettimeofday(&t2, NULL);
elapsed_time = (t2.tv_sec - t1.tv_sec) * 1000.0;
elapsed_time += (t2.tv_usec - t1.tv_usec) / 1000.0;
fprintf(stderr, "[vulcan] set %ld %ss in %lf milliseconds\n", count, type, elapsed_time);
return 0;
}