Added basic print output.
[awesomized/libmemcached] / src / memslap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <sys/mman.h>
7 #include <fcntl.h>
8 #include <sys/time.h>
9 #include <getopt.h>
10
11 #include <memcached.h>
12
13 #include "client_options.h"
14 #include "utilities.h"
15
16
17 /* Use this for string generation */
18 static const char ALPHANUMERICS[]=
19 "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
20
21 #define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1)
22
23 /* Types */
24 typedef struct pairs_st pairs_st;
25 typedef struct conclusions_st conclusions_st;
26
27 struct conclusions_st {
28 long int load_time;
29 long int read_time;
30 unsigned int rows_loaded;
31 unsigned int rows_read;
32 };
33
34 struct pairs_st {
35 char *key;
36 size_t key_length;
37 char *value;
38 size_t value_length;
39 };
40
41 /* Prototypes */
42 void options_parse(int argc, char *argv[]);
43 static pairs_st *pairs_generate(void);
44 static void pairs_free(pairs_st *pairs);
45 static void get_random_string(char *buffer, size_t size);
46 void conclusions_print(conclusions_st *conclusion);
47
48 static int opt_verbose= 0;
49 static unsigned int opt_default_pairs= 100;
50 static int opt_displayflag= 0;
51 static char *opt_servers= NULL;
52
53 int main(int argc, char *argv[])
54 {
55 unsigned int x;
56 memcached_return rc;
57 memcached_st *memc;
58 struct timeval start_time, end_time;
59 pairs_st *pairs;
60 conclusions_st conclusion;
61
62 memset(&conclusion, 0, sizeof(conclusions_st));
63
64 srandom(time(NULL));
65 memc= memcached_init(NULL);
66 options_parse(argc, argv);
67
68 if (!opt_servers)
69 exit(0);
70
71 parse_opt_servers(memc, opt_servers);
72
73 pairs= pairs_generate();
74
75
76 gettimeofday(&start_time, NULL);
77 for (x= 0; x < opt_default_pairs; x++)
78 {
79 rc= memcached_set(memc, pairs[x].key, pairs[x].key_length,
80 pairs[x].value, pairs[x].value_length,
81 0, 0);
82 if (rc != MEMCACHED_SUCCESS)
83 fprintf(stderr, "Failured on insert of %.*s\n",
84 (unsigned int)pairs[x].key_length, pairs[x].key);
85 conclusion.rows_loaded++;
86 }
87 gettimeofday(&end_time, NULL);
88 conclusion.load_time= timedif(end_time, start_time);
89
90 gettimeofday(&start_time, NULL);
91 for (x= 0; x < opt_default_pairs; x++)
92 {
93 char *value;
94 size_t value_length;
95 uint16_t flags;
96
97 value= memcached_get(memc, pairs[x].key, pairs[x].key_length,
98 &value_length,
99 &flags, &rc);
100
101 if (rc != MEMCACHED_SUCCESS)
102 fprintf(stderr, "Failured on read of %.*s\n",
103 (unsigned int)pairs[x].key_length, pairs[x].key);
104 conclusion.rows_read++;
105 free(value);
106 }
107 gettimeofday(&end_time, NULL);
108 conclusion.read_time= timedif(end_time, start_time);
109
110 pairs_free(pairs);
111
112 free(opt_servers);
113
114 memcached_deinit(memc);
115
116 conclusions_print(&conclusion);
117
118 return 0;
119 }
120
121 void options_parse(int argc, char *argv[])
122 {
123 static struct option long_options[]=
124 {
125 {"version", no_argument, NULL, OPT_VERSION},
126 {"help", no_argument, NULL, OPT_HELP},
127 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
128 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
129 {"servers", required_argument, NULL, OPT_SERVERS},
130 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
131 {"default-pairs", required_argument, NULL, OPT_SLAP_DEFAULT_PAIRS},
132 {0, 0, 0, 0},
133 };
134
135 int option_index= 0;
136 int option_rv;
137
138 while (1)
139 {
140 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
141 if (option_rv == -1) break;
142 switch (option_rv)
143 {
144 case 0:
145 break;
146 case OPT_VERBOSE: /* --verbose or -v */
147 opt_verbose = OPT_VERBOSE;
148 break;
149 case OPT_DEBUG: /* --debug or -d */
150 opt_verbose = OPT_DEBUG;
151 break;
152 case OPT_VERSION: /* --version or -V */
153 printf("memcache tools, memcat, v1.0\n");
154 exit(0);
155 break;
156 case OPT_HELP: /* --help or -h */
157 printf("useful help messages go here\n");
158 exit(0);
159 break;
160 case OPT_SERVERS: /* --servers or -s */
161 opt_servers= strdup(optarg);
162 break;
163 case OPT_SLAP_DEFAULT_PAIRS:
164 opt_default_pairs= strtol(optarg, (char **)NULL, 10);
165 break;
166 case '?':
167 /* getopt_long already printed an error message. */
168 exit(1);
169 default:
170 abort();
171 }
172 }
173 }
174
175 static void pairs_free(pairs_st *pairs)
176 {
177 unsigned int x;
178
179 for (x= 0; x < opt_default_pairs; x++)
180 {
181 free(pairs[x].key);
182 free(pairs[x].value);
183 }
184
185 free(pairs);
186 }
187
188 static pairs_st *pairs_generate(void)
189 {
190 unsigned int x;
191 pairs_st *pairs;
192
193 pairs= (pairs_st*)malloc(sizeof(pairs_st) * opt_default_pairs);
194
195 if (!pairs)
196 goto error;
197
198 for (x= 0; x < opt_default_pairs; x++)
199 {
200 pairs[x].key= (char *)malloc(sizeof(char) * 100);
201 if (!pairs[x].key)
202 goto error;
203 get_random_string(pairs[x].key, 100);
204 pairs[x].key_length= 100;
205
206 pairs[x].value= (char *)malloc(sizeof(char) * 400);
207 if (!pairs[x].value)
208 goto error;
209 get_random_string(pairs[x].value, 400);
210 pairs[x].value_length= 400;
211 }
212
213 return pairs;
214 error:
215 fprintf(stderr, "Memory Allocation failure in pairs_generate.\n");
216 exit(0);
217 }
218
219 void conclusions_print(conclusions_st *conclusion)
220 {
221 printf("\tLoaded %u rows\n", conclusion->rows_loaded);
222 printf("\tRead %u rows\n", conclusion->rows_read);
223 printf("\tTook %ld.%03ld seconds to load data\n", conclusion->load_time / 1000,
224 conclusion->load_time % 1000);
225 printf("\tTook %ld.%03ld seconds to read data\n", conclusion->read_time / 1000,
226 conclusion->read_time % 1000);
227 }
228
229 static void get_random_string(char *buffer, size_t size)
230 {
231 char *buffer_ptr= buffer;
232
233 while (--size)
234 *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
235 *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
236 }