11 #include <memcached.h>
13 #include "client_options.h"
14 #include "utilities.h"
17 /* Use this for string generation */
18 static const char ALPHANUMERICS
[]=
19 "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
21 #define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1)
24 typedef struct pairs_st pairs_st
;
25 typedef struct conclusions_st conclusions_st
;
27 struct conclusions_st
{
30 unsigned int rows_loaded
;
31 unsigned int rows_read
;
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
);
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
;
53 int main(int argc
, char *argv
[])
58 struct timeval start_time
, end_time
;
60 conclusions_st conclusion
;
62 memset(&conclusion
, 0, sizeof(conclusions_st
));
65 memc
= memcached_init(NULL
);
66 options_parse(argc
, argv
);
71 parse_opt_servers(memc
, opt_servers
);
73 pairs
= pairs_generate();
76 gettimeofday(&start_time
, NULL
);
77 for (x
= 0; x
< opt_default_pairs
; x
++)
79 rc
= memcached_set(memc
, pairs
[x
].key
, pairs
[x
].key_length
,
80 pairs
[x
].value
, pairs
[x
].value_length
,
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
++;
87 gettimeofday(&end_time
, NULL
);
88 conclusion
.load_time
= timedif(end_time
, start_time
);
90 gettimeofday(&start_time
, NULL
);
91 for (x
= 0; x
< opt_default_pairs
; x
++)
97 value
= memcached_get(memc
, pairs
[x
].key
, pairs
[x
].key_length
,
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
++;
107 gettimeofday(&end_time
, NULL
);
108 conclusion
.read_time
= timedif(end_time
, start_time
);
114 memcached_deinit(memc
);
116 conclusions_print(&conclusion
);
121 void options_parse(int argc
, char *argv
[])
123 static struct option long_options
[]=
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
},
140 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
141 if (option_rv
== -1) break;
146 case OPT_VERBOSE
: /* --verbose or -v */
147 opt_verbose
= OPT_VERBOSE
;
149 case OPT_DEBUG
: /* --debug or -d */
150 opt_verbose
= OPT_DEBUG
;
152 case OPT_VERSION
: /* --version or -V */
153 printf("memcache tools, memcat, v1.0\n");
156 case OPT_HELP
: /* --help or -h */
157 printf("useful help messages go here\n");
160 case OPT_SERVERS
: /* --servers or -s */
161 opt_servers
= strdup(optarg
);
163 case OPT_SLAP_DEFAULT_PAIRS
:
164 opt_default_pairs
= strtol(optarg
, (char **)NULL
, 10);
167 /* getopt_long already printed an error message. */
175 static void pairs_free(pairs_st
*pairs
)
179 for (x
= 0; x
< opt_default_pairs
; x
++)
182 free(pairs
[x
].value
);
188 static pairs_st
*pairs_generate(void)
193 pairs
= (pairs_st
*)malloc(sizeof(pairs_st
) * opt_default_pairs
);
198 for (x
= 0; x
< opt_default_pairs
; x
++)
200 pairs
[x
].key
= (char *)malloc(sizeof(char) * 100);
203 get_random_string(pairs
[x
].key
, 100);
204 pairs
[x
].key_length
= 100;
206 pairs
[x
].value
= (char *)malloc(sizeof(char) * 400);
209 get_random_string(pairs
[x
].value
, 400);
210 pairs
[x
].value_length
= 400;
215 fprintf(stderr
, "Memory Allocation failure in pairs_generate.\n");
219 void conclusions_print(conclusions_st
*conclusion
)
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);
229 static void get_random_string(char *buffer
, size_t size
)
231 char *buffer_ptr
= buffer
;
234 *buffer_ptr
++= ALPHANUMERICS
[random() % ALPHANUMERICS_SIZE
];
235 *buffer_ptr
++= ALPHANUMERICS
[random() % ALPHANUMERICS_SIZE
];