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