216506d88b26625bdc38521a34a64f77a7d0e63e
[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 <sys/time.h>
9 #include <getopt.h>
10
11 #include <memcached.h>
12
13 #include "client_options.h"
14 #include "utilities.h"
15 #include "generator.h"
16
17
18 /* Types */
19 typedef struct conclusions_st conclusions_st;
20
21 struct conclusions_st {
22 long int load_time;
23 long int read_time;
24 unsigned int rows_loaded;
25 unsigned int rows_read;
26 };
27
28 /* Prototypes */
29 void options_parse(int argc, char *argv[]);
30 void conclusions_print(conclusions_st *conclusion);
31
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;
36
37 int main(int argc, char *argv[])
38 {
39 unsigned int x;
40 memcached_return rc;
41 memcached_st *memc;
42 struct timeval start_time, end_time;
43 pairs_st *pairs;
44 conclusions_st conclusion;
45
46 memset(&conclusion, 0, sizeof(conclusions_st));
47
48 srandom(time(NULL));
49 memc= memcached_init(NULL);
50 options_parse(argc, argv);
51
52 if (!opt_servers)
53 exit(0);
54
55 parse_opt_servers(memc, opt_servers);
56
57 pairs= pairs_generate(opt_default_pairs);
58
59
60 gettimeofday(&start_time, NULL);
61 for (x= 0; x < opt_default_pairs; x++)
62 {
63 rc= memcached_set(memc, pairs[x].key, pairs[x].key_length,
64 pairs[x].value, pairs[x].value_length,
65 0, 0);
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++;
70 }
71 gettimeofday(&end_time, NULL);
72 conclusion.load_time= timedif(end_time, start_time);
73
74 gettimeofday(&start_time, NULL);
75 for (x= 0; x < opt_default_pairs; x++)
76 {
77 char *value;
78 size_t value_length;
79 uint16_t flags;
80
81 value= memcached_get(memc, pairs[x].key, pairs[x].key_length,
82 &value_length,
83 &flags, &rc);
84
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++;
89 free(value);
90 }
91 gettimeofday(&end_time, NULL);
92 conclusion.read_time= timedif(end_time, start_time);
93
94 pairs_free(pairs);
95
96 free(opt_servers);
97
98 memcached_deinit(memc);
99
100 conclusions_print(&conclusion);
101
102 return 0;
103 }
104
105 void options_parse(int argc, char *argv[])
106 {
107 static struct option long_options[]=
108 {
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},
116 {0, 0, 0, 0},
117 };
118
119 int option_index= 0;
120 int option_rv;
121
122 while (1)
123 {
124 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
125 if (option_rv == -1) break;
126 switch (option_rv)
127 {
128 case 0:
129 break;
130 case OPT_VERBOSE: /* --verbose or -v */
131 opt_verbose = OPT_VERBOSE;
132 break;
133 case OPT_DEBUG: /* --debug or -d */
134 opt_verbose = OPT_DEBUG;
135 break;
136 case OPT_VERSION: /* --version or -V */
137 printf("memcache tools, memcat, v1.0\n");
138 exit(0);
139 break;
140 case OPT_HELP: /* --help or -h */
141 printf("useful help messages go here\n");
142 exit(0);
143 break;
144 case OPT_SERVERS: /* --servers or -s */
145 opt_servers= strdup(optarg);
146 break;
147 case OPT_SLAP_DEFAULT_PAIRS:
148 opt_default_pairs= strtol(optarg, (char **)NULL, 10);
149 break;
150 case '?':
151 /* getopt_long already printed an error message. */
152 exit(1);
153 default:
154 abort();
155 }
156 }
157 }
158
159 void conclusions_print(conclusions_st *conclusion)
160 {
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);
167 }