11 #include <memcached.h>
13 #include "client_options.h"
14 #include "utilities.h"
15 #include "generator.h"
19 typedef struct conclusions_st conclusions_st
;
21 struct conclusions_st
{
24 unsigned int rows_loaded
;
25 unsigned int rows_read
;
29 void options_parse(int argc
, char *argv
[]);
30 void conclusions_print(conclusions_st
*conclusion
);
32 static int opt_verbose
= 0;
33 static unsigned int opt_default_pairs
= 100;
34 static int opt_displayflag
= 0;
35 static char *opt_servers
= NULL
;
37 int main(int argc
, char *argv
[])
42 struct timeval start_time
, end_time
;
44 conclusions_st conclusion
;
46 memset(&conclusion
, 0, sizeof(conclusions_st
));
49 memc
= memcached_init(NULL
);
50 options_parse(argc
, argv
);
55 parse_opt_servers(memc
, opt_servers
);
57 pairs
= pairs_generate(opt_default_pairs
);
60 gettimeofday(&start_time
, NULL
);
61 for (x
= 0; x
< opt_default_pairs
; x
++)
63 rc
= memcached_set(memc
, pairs
[x
].key
, pairs
[x
].key_length
,
64 pairs
[x
].value
, pairs
[x
].value_length
,
66 if (rc
!= MEMCACHED_SUCCESS
)
67 fprintf(stderr
, "Failured on insert of %.*s\n",
68 (unsigned int)pairs
[x
].key_length
, pairs
[x
].key
);
69 conclusion
.rows_loaded
++;
71 gettimeofday(&end_time
, NULL
);
72 conclusion
.load_time
= timedif(end_time
, start_time
);
74 gettimeofday(&start_time
, NULL
);
75 for (x
= 0; x
< opt_default_pairs
; x
++)
81 value
= memcached_get(memc
, pairs
[x
].key
, pairs
[x
].key_length
,
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 conclusion
.rows_read
++;
91 gettimeofday(&end_time
, NULL
);
92 conclusion
.read_time
= timedif(end_time
, start_time
);
98 memcached_deinit(memc
);
100 conclusions_print(&conclusion
);
105 void options_parse(int argc
, char *argv
[])
107 static struct option long_options
[]=
109 {"version", no_argument
, NULL
, OPT_VERSION
},
110 {"help", no_argument
, NULL
, OPT_HELP
},
111 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
112 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
113 {"servers", required_argument
, NULL
, OPT_SERVERS
},
114 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
115 {"default-pairs", required_argument
, NULL
, OPT_SLAP_DEFAULT_PAIRS
},
124 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
125 if (option_rv
== -1) break;
130 case OPT_VERBOSE
: /* --verbose or -v */
131 opt_verbose
= OPT_VERBOSE
;
133 case OPT_DEBUG
: /* --debug or -d */
134 opt_verbose
= OPT_DEBUG
;
136 case OPT_VERSION
: /* --version or -V */
137 printf("memcache tools, memcat, v1.0\n");
140 case OPT_HELP
: /* --help or -h */
141 printf("useful help messages go here\n");
144 case OPT_SERVERS
: /* --servers or -s */
145 opt_servers
= strdup(optarg
);
147 case OPT_SLAP_DEFAULT_PAIRS
:
148 opt_default_pairs
= strtol(optarg
, (char **)NULL
, 10);
151 /* getopt_long already printed an error message. */
159 void conclusions_print(conclusions_st
*conclusion
)
161 printf("\tLoaded %u rows\n", conclusion
->rows_loaded
);
162 printf("\tRead %u rows\n", conclusion
->rows_read
);
163 printf("\tTook %ld.%03ld seconds to load data\n", conclusion
->load_time
/ 1000,
164 conclusion
->load_time
% 1000);
165 printf("\tTook %ld.%03ld seconds to read data\n", conclusion
->read_time
/ 1000,
166 conclusion
->read_time
% 1000);