2 * $Id: zoom-benchmark.c,v 1.13 2005-10-22 13:28:03 adam Exp $
4 * Asynchronous multi-target client doing search and piggyback retrieval
13 #include <yaz/xmalloc.h>
14 #include <yaz/options.h>
19 static char* zoom_events[10];
21 /* re-sorting event numbers to progress numbers */
22 static int zoom_progress[10];
24 /* commando line parameters */
25 static struct parameters_t {
50 void print_event_line(struct event_line_t *pel)
52 printf ("%d\t%ld.%06ld\t%d\t%d\t%s\t%d\t%s\n",
53 pel->connection, pel->time_sec, pel->time_usec,
55 pel->event, pel->zoom_event,
56 pel->error, pel->errmsg);
59 void update_events(int *elc, struct event_line_t *els,
66 const char * eventmsg,
70 int ielc = repeat*parameters.concurrent + conn;
71 int iels = repeat*parameters.concurrent*10 + conn*10 + elc[ielc];
73 els[iels].connection = conn;
74 els[iels].time_sec = sec;
75 els[iels].time_usec = usec;
76 els[iels].progress = prog;
77 els[iels].event = event;
78 strcpy(els[iels].zoom_event, eventmsg);
79 els[iels].error = error;
80 strcpy(els[iels].errmsg, errmsg);
81 /* print_event_line(&els[iels]); */
85 void print_events(int *elc, struct event_line_t *els,
93 for (k=0; k < parameters.repeat; k++){
94 for (i=0; i < connections; i++){
95 ielc = k * parameters.concurrent + i;
96 for (j=0; j < elc[ielc]; j++){
97 iels = k * parameters.concurrent * 10 + i * 10 + j;
98 print_event_line(&els[iels]);
111 char nullstring[1] = "";
114 zoom_events[ZOOM_EVENT_NONE] = "ZOOM_EVENT_NONE";
115 zoom_events[ZOOM_EVENT_CONNECT] = "ZOOM_EVENT_CONNECT";
116 zoom_events[ZOOM_EVENT_SEND_DATA] = "ZOOM_EVENT_SEND_DATA";
117 zoom_events[ZOOM_EVENT_RECV_DATA] = "ZOOM_EVENT_RECV_DATA";
118 zoom_events[ZOOM_EVENT_TIMEOUT] = "ZOOM_EVENT_TIMEOUT";
119 zoom_events[ZOOM_EVENT_UNKNOWN] = "ZOOM_EVENT_UNKNOWN";
120 zoom_events[ZOOM_EVENT_SEND_APDU] = "ZOOM_EVENT_SEND_APDU";
121 zoom_events[ZOOM_EVENT_RECV_APDU] = "ZOOM_EVENT_RECV_APDU";
122 zoom_events[ZOOM_EVENT_RECV_RECORD] = "ZOOM_EVENT_RECV_RECORD";
123 zoom_events[ZOOM_EVENT_RECV_SEARCH] = "ZOOM_EVENT_RECV_SEARCH";
125 /* re-sorting event numbers to progress numbers */
126 zoom_progress[ZOOM_EVENT_NONE] = 0;
127 zoom_progress[ZOOM_EVENT_CONNECT] = 1;
128 zoom_progress[ZOOM_EVENT_SEND_DATA] = 3;
129 zoom_progress[ZOOM_EVENT_RECV_DATA] = 4;
130 zoom_progress[ZOOM_EVENT_TIMEOUT] = 8;
131 zoom_progress[ZOOM_EVENT_UNKNOWN] = 9;
132 zoom_progress[ZOOM_EVENT_SEND_APDU] = 2;
133 zoom_progress[ZOOM_EVENT_RECV_APDU] = 5;
134 zoom_progress[ZOOM_EVENT_RECV_RECORD] = 7;
135 zoom_progress[ZOOM_EVENT_RECV_SEARCH] = 6;
138 parameters.concurrent = 1;
139 parameters.timeout = 0;
140 parameters.repeat = 1;
141 strcpy(parameters.proxy, nullstring);
142 parameters.gnuplot = 0;
143 parameters.piggypack = 0;
145 /* progress initializing */
146 for (i = 0; i < 4096; i++){
147 parameters.progress[i] = 0;
160 void time_init(struct time_type *ptime)
162 gettimeofday(&(ptime->now), 0);
163 gettimeofday(&(ptime->then), 0);
168 void time_stamp(struct time_type *ptime)
170 gettimeofday(&(ptime->now), 0);
171 ptime->sec = ptime->now.tv_sec - ptime->then.tv_sec;
172 ptime->usec = ptime->now.tv_usec - ptime->then.tv_usec;
173 if (ptime->usec < 0){
175 ptime->usec += 1000000;
179 long time_sec(struct time_type *ptime)
184 long time_usec(struct time_type *ptime)
189 void print_option_error()
191 fprintf(stderr, "zoom-benchmark: Call error\n");
192 fprintf(stderr, "zoom-benchmark -h host:port -q pqf-query "
193 "[-c no_concurrent (max 4096)] "
196 "[-g (gnuplot outfile)] "
198 /* "[-t timeout] \n"); */
203 void read_params(int argc, char **argv, struct parameters_t *p_parameters){
206 while ((ret = options("h:q:c:t:p:bgn:", argv, argc, &arg)) != -2)
211 strcpy(p_parameters->host, arg);
214 strcpy(p_parameters->query, arg);
217 strcpy(p_parameters->proxy, arg);
220 p_parameters->concurrent = atoi(arg);
224 p_parameters->timeout = atoi(arg);
228 p_parameters->piggypack = 1;
231 p_parameters->gnuplot = 1;
234 p_parameters->repeat = atoi(arg);
237 print_option_error();
240 print_option_error();
245 printf("zoom-benchmark\n");
246 printf(" host: %s \n", p_parameters->host);
247 printf(" query: %s \n", p_parameters->query);
248 printf(" concurrent: %d \n", p_parameters->concurrent);
249 printf(" repeat: %d \n", p_parameters->repeat);
251 printf(" timeout: %d \n", p_parameters->timeout);
253 printf(" proxy: %s \n", p_parameters->proxy);
254 printf(" piggypack: %d \n\n", p_parameters->piggypack);
255 printf(" gnuplot: %d \n\n", p_parameters->gnuplot);
258 if (! strlen(p_parameters->host))
259 print_option_error();
260 if (! strlen(p_parameters->query))
261 print_option_error();
262 if (! (p_parameters->concurrent > 0))
263 print_option_error();
264 if (! (p_parameters->repeat > 0))
265 print_option_error();
266 if (! (p_parameters->timeout >= 0))
267 print_option_error();
268 if (! ( p_parameters->concurrent <= 4096))
269 print_option_error();
272 void print_table_header()
274 if (parameters.gnuplot)
276 printf ("target\tsecond.usec\tprogress\tevent\teventname\t");
277 printf("error\terrorname\n");
281 int main(int argc, char **argv)
283 struct time_type time;
287 struct event_line_t *els;
294 read_params(argc, argv, ¶meters);
296 z = xmalloc(sizeof(*z) * parameters.concurrent);
297 r = xmalloc(sizeof(*r) * parameters.concurrent);
298 elc = xmalloc(sizeof(*elc) * parameters.concurrent * parameters.repeat);
299 els = xmalloc(sizeof(*els)
300 * parameters.concurrent * parameters.repeat * 10);
301 o = ZOOM_options_create();
304 ZOOM_options_set (o, "async", "1");
306 /* get first record of result set (using piggypack) */
307 if (parameters.piggypack)
308 ZOOM_options_set (o, "count", "1");
311 if (strlen(parameters.proxy))
312 ZOOM_options_set (o, "proxy", parameters.proxy);
315 /* preferred record syntax */
317 ZOOM_options_set (o, "preferredRecordSyntax", "usmarc");
318 ZOOM_options_set (o, "elementSetName", "F");
323 for (k = 0; k < parameters.repeat; k++){
325 /* progress zeroing */
326 for (i = 0; i < 4096; i++){
327 parameters.progress[i] = k * 5 -1;
330 /* connect to all concurrent connections*/
331 for ( i = 0; i < parameters.concurrent; i++){
332 /* set event count to zero */
333 elc[k * parameters.concurrent + i] = 0;
335 /* create connection - pass options (they are the same for all) */
336 z[i] = ZOOM_connection_create(o);
338 /* connect and init */
339 ZOOM_connection_connect(z[i], parameters.host, 0);
342 for (i = 0; i < parameters.concurrent; i++)
343 r[i] = ZOOM_connection_search_pqf (z[i], parameters.query);
345 /* network I/O. pass number of connections and array of connections */
346 while ((i = ZOOM_event (parameters.concurrent, z))){
347 int event = ZOOM_connection_last_event(z[i-1]);
351 int progress = zoom_progress[event];
353 if (event == ZOOM_EVENT_SEND_DATA || event == ZOOM_EVENT_RECV_DATA)
358 /* updating events and event list */
359 error = ZOOM_connection_error(z[i-1] , &errmsg, &addinfo);
361 parameters.progress[i] = -progress;
363 parameters.progress[i] += 1;
365 update_events(elc, els,
367 time_sec(&time), time_usec(&time),
368 parameters.progress[i],
369 event, zoom_events[event],
373 /* destroy connections */
374 for (i = 0; i<parameters.concurrent; i++)
376 ZOOM_resultset_destroy (r[i]);
377 ZOOM_connection_destroy (z[i]);
382 } /* for (k = 0; k < parameters.repeat; k++) repeat loop */
386 if (parameters.gnuplot){
387 printf("# gnuplot data and instruction file \n");
388 printf("# gnuplot thisfile \n");
390 printf("set title \"Z39.50 connection plot\"\n");
391 printf("set xlabel \"Connection\"\n");
392 printf("set ylabel \"Time Seconds\"\n");
393 printf("set zlabel \"Progress\"\n");
394 printf("set ticslevel 0\n");
395 printf("set grid\n");
396 printf("set pm3d\n");
397 printf("splot '-' using ($1):($2):($3) t '' with points\n");
402 print_table_header();
403 print_events(elc, els, parameters.concurrent);
405 if (parameters.gnuplot){
407 printf("pause -1 \"Hit ENTER to return\"\n");
410 /* destroy data structures and exit */
415 ZOOM_options_destroy(o);
421 * indent-tabs-mode: nil
423 * vim: shiftwidth=4 tabstop=8 expandtab