1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
12 #include <yaz/xmalloc.h>
13 #include <yaz/options.h>
18 static char* zoom_events[10];
20 /* re-sorting event numbers to progress numbers */
21 static int zoom_progress[10];
23 /* commando line parameters */
24 static struct parameters_t {
49 void print_event_line(struct event_line_t *pel)
51 printf ("%d\t%ld.%06ld\t%d\t%d\t%s\t%d\t%s\n",
52 pel->connection, pel->time_sec, pel->time_usec,
54 pel->event, pel->zoom_event,
55 pel->error, pel->errmsg);
58 void update_events(int *elc, struct event_line_t *els,
65 const char * eventmsg,
69 int ielc = repeat*parameters.concurrent + conn;
70 int iels = repeat*parameters.concurrent*10 + conn*10 + elc[ielc];
72 els[iels].connection = conn;
73 els[iels].time_sec = sec;
74 els[iels].time_usec = usec;
75 els[iels].progress = prog;
76 els[iels].event = event;
79 strcpy(els[iels].zoom_event, eventmsg);
81 strcpy(els[iels].zoom_event, "---");
83 els[iels].error = error;
84 strcpy(els[iels].errmsg, errmsg);
85 /* print_event_line(&els[iels]); */
89 void print_events(int *elc, struct event_line_t *els,
97 for (k=0; k < parameters.repeat; k++){
98 for (i=0; i < connections; i++){
99 ielc = k * parameters.concurrent + i;
100 for (j=0; j < elc[ielc]; j++){
101 iels = k * parameters.concurrent * 10 + i * 10 + j;
102 print_event_line(&els[iels]);
112 void init_statics(void)
115 char nullstring[1] = "";
118 zoom_events[ZOOM_EVENT_NONE] = "ZOOM_EVENT_NONE";
119 zoom_events[ZOOM_EVENT_CONNECT] = "ZOOM_EVENT_CONNECT";
120 zoom_events[ZOOM_EVENT_SEND_DATA] = "ZOOM_EVENT_SEND_DATA";
121 zoom_events[ZOOM_EVENT_RECV_DATA] = "ZOOM_EVENT_RECV_DATA";
122 zoom_events[ZOOM_EVENT_TIMEOUT] = "ZOOM_EVENT_TIMEOUT";
123 zoom_events[ZOOM_EVENT_UNKNOWN] = "ZOOM_EVENT_UNKNOWN";
124 zoom_events[ZOOM_EVENT_SEND_APDU] = "ZOOM_EVENT_SEND_APDU";
125 zoom_events[ZOOM_EVENT_RECV_APDU] = "ZOOM_EVENT_RECV_APDU";
126 zoom_events[ZOOM_EVENT_RECV_RECORD] = "ZOOM_EVENT_RECV_RECORD";
127 zoom_events[ZOOM_EVENT_RECV_SEARCH] = "ZOOM_EVENT_RECV_SEARCH";
128 zoom_events[ZOOM_EVENT_END] = "ZOOM_EVENT_END";
130 /* re-sorting event numbers to progress numbers */
131 zoom_progress[ZOOM_EVENT_NONE] = 0;
132 zoom_progress[ZOOM_EVENT_CONNECT] = 1;
133 zoom_progress[ZOOM_EVENT_SEND_DATA] = 3;
134 zoom_progress[ZOOM_EVENT_RECV_DATA] = 4;
135 zoom_progress[ZOOM_EVENT_TIMEOUT] = 9;
136 zoom_progress[ZOOM_EVENT_UNKNOWN] = 10;
137 zoom_progress[ZOOM_EVENT_SEND_APDU] = 2;
138 zoom_progress[ZOOM_EVENT_RECV_APDU] = 5;
139 zoom_progress[ZOOM_EVENT_RECV_RECORD] = 7;
140 zoom_progress[ZOOM_EVENT_RECV_SEARCH] = 6;
141 zoom_progress[ZOOM_EVENT_END] = 8;
144 parameters.concurrent = 1;
145 parameters.timeout = 0;
146 parameters.repeat = 1;
147 strcpy(parameters.proxy, nullstring);
148 parameters.gnuplot = 0;
149 parameters.piggypack = 0;
151 /* progress initializing */
152 for (i = 0; i < 4096; i++){
153 parameters.progress[i] = 0;
166 void time_init(struct time_type *ptime)
168 gettimeofday(&(ptime->now), 0);
169 gettimeofday(&(ptime->then), 0);
174 void time_stamp(struct time_type *ptime)
176 gettimeofday(&(ptime->now), 0);
177 ptime->sec = ptime->now.tv_sec - ptime->then.tv_sec;
178 ptime->usec = ptime->now.tv_usec - ptime->then.tv_usec;
179 if (ptime->usec < 0){
181 ptime->usec += 1000000;
185 long time_sec(struct time_type *ptime)
190 long time_usec(struct time_type *ptime)
195 void print_option_error(void)
197 fprintf(stderr, "zoom-benchmark: Call error\n");
198 fprintf(stderr, "zoom-benchmark -h host:port -q pqf-query "
199 "[-c no_concurrent (max 4096)] "
202 "[-g (gnuplot outfile)] "
204 /* "[-t timeout] \n"); */
209 void read_params(int argc, char **argv, struct parameters_t *p_parameters){
212 while ((ret = options("h:q:c:t:p:bgn:", argv, argc, &arg)) != -2)
217 strcpy(p_parameters->host, arg);
220 strcpy(p_parameters->query, arg);
223 strcpy(p_parameters->proxy, arg);
226 p_parameters->concurrent = atoi(arg);
230 p_parameters->timeout = atoi(arg);
234 p_parameters->piggypack = 1;
237 p_parameters->gnuplot = 1;
240 p_parameters->repeat = atoi(arg);
243 print_option_error();
246 print_option_error();
251 printf("zoom-benchmark\n");
252 printf(" host: %s \n", p_parameters->host);
253 printf(" query: %s \n", p_parameters->query);
254 printf(" concurrent: %d \n", p_parameters->concurrent);
255 printf(" repeat: %d \n", p_parameters->repeat);
257 printf(" timeout: %d \n", p_parameters->timeout);
259 printf(" proxy: %s \n", p_parameters->proxy);
260 printf(" piggypack: %d \n\n", p_parameters->piggypack);
261 printf(" gnuplot: %d \n\n", p_parameters->gnuplot);
264 if (! strlen(p_parameters->host))
265 print_option_error();
266 if (! strlen(p_parameters->query))
267 print_option_error();
268 if (! (p_parameters->concurrent > 0))
269 print_option_error();
270 if (! (p_parameters->repeat > 0))
271 print_option_error();
272 if (! (p_parameters->timeout >= 0))
273 print_option_error();
274 if (! ( p_parameters->concurrent <= 4096))
275 print_option_error();
278 void print_table_header(void)
280 if (parameters.gnuplot)
282 printf ("target\tsecond.usec\tprogress\tevent\teventname\t");
283 printf("error\terrorname\n");
287 int main(int argc, char **argv)
289 struct time_type time;
293 struct event_line_t *els;
300 read_params(argc, argv, ¶meters);
302 z = (ZOOM_connection *) xmalloc(sizeof(*z) * parameters.concurrent);
303 r = (ZOOM_resultset *) xmalloc(sizeof(*r) * parameters.concurrent);
304 elc = (int *) xmalloc(sizeof(*elc) * parameters.concurrent * parameters.repeat);
305 els = (struct event_line_t *) xmalloc(
306 sizeof(*els) * parameters.concurrent * parameters.repeat * 10);
307 o = ZOOM_options_create();
310 ZOOM_options_set (o, "async", "1");
312 /* get first record of result set (using piggypack) */
313 if (parameters.piggypack)
314 ZOOM_options_set (o, "count", "1");
317 if (strlen(parameters.proxy))
318 ZOOM_options_set (o, "proxy", parameters.proxy);
321 /* preferred record syntax */
323 ZOOM_options_set (o, "preferredRecordSyntax", "usmarc");
324 ZOOM_options_set (o, "elementSetName", "F");
329 for (k = 0; k < parameters.repeat; k++){
331 /* progress zeroing */
332 for (i = 0; i < 4096; i++){
333 parameters.progress[i] = k * 5 -1;
336 /* connect to all concurrent connections*/
337 for ( i = 0; i < parameters.concurrent; i++){
338 /* set event count to zero */
339 elc[k * parameters.concurrent + i] = 0;
341 /* create connection - pass options (they are the same for all) */
342 z[i] = ZOOM_connection_create(o);
344 /* connect and init */
345 ZOOM_connection_connect(z[i], parameters.host, 0);
348 for (i = 0; i < parameters.concurrent; i++)
349 r[i] = ZOOM_connection_search_pqf (z[i], parameters.query);
351 /* network I/O. pass number of connections and array of connections */
352 while ((i = ZOOM_event (parameters.concurrent, z))){
353 int event = ZOOM_connection_last_event(z[i-1]);
357 //int progress = zoom_progress[event];
359 if (event == ZOOM_EVENT_SEND_DATA || event == ZOOM_EVENT_RECV_DATA)
364 /* updating events and event list */
365 error = ZOOM_connection_error(z[i-1] , &errmsg, &addinfo);
367 parameters.progress[i] = zoom_progress[ZOOM_EVENT_UNKNOWN];
368 //parameters.progress[i] = zoom_progress[ZOOM_EVENT_NONE];
369 else if (event == ZOOM_EVENT_CONNECT)
370 parameters.progress[i] = zoom_progress[event];
372 //parameters.progress[i] = zoom_progress[event];
373 parameters.progress[i] += 1;
375 update_events(elc, els,
377 time_sec(&time), time_usec(&time),
378 parameters.progress[i],
379 event, zoom_events[event],
383 /* destroy connections */
384 for (i = 0; i<parameters.concurrent; i++)
386 ZOOM_resultset_destroy (r[i]);
387 ZOOM_connection_destroy (z[i]);
392 } /* for (k = 0; k < parameters.repeat; k++) repeat loop */
396 if (parameters.gnuplot){
397 printf("# gnuplot data and instruction file \n");
398 printf("# gnuplot thisfile \n");
400 printf("set title \"Z39.50 connection plot\"\n");
401 printf("set xlabel \"Connection\"\n");
402 printf("set ylabel \"Time Seconds\"\n");
403 printf("set zlabel \"Progress\"\n");
404 printf("set ticslevel 0\n");
405 printf("set grid\n");
406 printf("set pm3d\n");
407 printf("splot '-' using ($1):($2):($3) t '' with points\n");
412 print_table_header();
413 print_events(elc, els, parameters.concurrent);
415 if (parameters.gnuplot){
417 printf("pause -1 \"Hit ENTER to return\"\n");
420 /* destroy data structures and exit */
425 ZOOM_options_destroy(o);
431 * indent-tabs-mode: nil
433 * vim: shiftwidth=4 tabstop=8 expandtab