Added parameter to memslap so that key generation can be controlled through
[m6w6/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 pairs_free(pairs);
74
75 free(opt_servers);
76
77 memcached_deinit(memc);
78
79 return 0;
80 }
81
82 void options_parse(int argc, char *argv[])
83 {
84 static struct option long_options[]=
85 {
86 {"version", no_argument, NULL, OPT_VERSION},
87 {"help", no_argument, NULL, OPT_HELP},
88 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
89 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
90 {"servers", required_argument, NULL, OPT_SERVERS},
91 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
92 {"default-pairs", required_argument, NULL, OPT_SLAP_DEFAULT_PAIRS},
93 {0, 0, 0, 0},
94 };
95
96 int option_index= 0;
97 int option_rv;
98
99 while (1)
100 {
101 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
102 if (option_rv == -1) break;
103 switch (option_rv)
104 {
105 case 0:
106 break;
107 case OPT_VERBOSE: /* --verbose or -v */
108 opt_verbose = OPT_VERBOSE;
109 break;
110 case OPT_DEBUG: /* --debug or -d */
111 opt_verbose = OPT_DEBUG;
112 break;
113 case OPT_VERSION: /* --version or -V */
114 printf("memcache tools, memcat, v1.0\n");
115 exit(0);
116 break;
117 case OPT_HELP: /* --help or -h */
118 printf("useful help messages go here\n");
119 exit(0);
120 break;
121 case OPT_SERVERS: /* --servers or -s */
122 opt_servers= strdup(optarg);
123 break;
124 case OPT_SLAP_DEFAULT_PAIRS:
125 opt_default_pairs= strtol(optarg, (char **)NULL, 10);
126 break;
127 case '?':
128 /* getopt_long already printed an error message. */
129 exit(1);
130 default:
131 abort();
132 }
133 }
134 }
135
136 static void pairs_free(pairs_st *pairs)
137 {
138 unsigned int x;
139
140 for (x= 0; x < opt_default_pairs; x++)
141 {
142 free(pairs[x].key);
143 free(pairs[x].value);
144 }
145
146 free(pairs);
147 }
148
149 static pairs_st *pairs_generate(void)
150 {
151 unsigned int x;
152 pairs_st *pairs;
153
154 pairs= (pairs_st*)malloc(sizeof(pairs_st) * opt_default_pairs);
155
156 if (!pairs)
157 goto error;
158
159 for (x= 0; x < opt_default_pairs; x++)
160 {
161 pairs[x].key= (char *)malloc(sizeof(char) * 100);
162 if (!pairs[x].key)
163 goto error;
164 get_random_string(pairs[x].key, 100);
165 pairs[x].key_length= 100;
166
167 pairs[x].value= (char *)malloc(sizeof(char) * 400);
168 if (!pairs[x].value)
169 goto error;
170 get_random_string(pairs[x].value, 400);
171 pairs[x].value_length= 400;
172 }
173
174 return pairs;
175 error:
176 fprintf(stderr, "Memory Allocation failure in pairs_generate.\n");
177 exit(0);
178 }
179
180 static void get_random_string(char *buffer, size_t size)
181 {
182 char *buffer_ptr= buffer;
183
184 while (--size)
185 *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
186 *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
187 }