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