pretty formatting with tabs for gnuplot reading
[yaz-moved-to-github.git] / zoom / zoom-benchmark.c
1 /*
2  * $Id: zoom-benchmark.c,v 1.2 2005-09-09 13:51:43 marc Exp $
3  *
4  * Asynchronous multi-target client doing search and piggyback retrieval
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <sys/time.h>
12
13 #include <yaz/xmalloc.h>
14 #include <yaz/options.h>
15 #include <yaz/zoom.h>
16
17
18 /* naming events */
19 static char* zoom_events[10];
20
21 /* re-sorting event numbers to progress numbers */
22 static int zoom_progress[10];
23
24 /* commando line parameters */
25 static struct parameters_t { 
26     char host[1024];
27     char query[1024];
28     int concurrent;
29     int timeout;
30 } parameters;
31
32
33 void init_statics()
34 {
35     /* naming events */
36     zoom_events[ZOOM_EVENT_NONE] = "ZOOM_EVENT_NONE";
37     zoom_events[ZOOM_EVENT_CONNECT] = "ZOOM_EVENT_CONNECT";
38     zoom_events[ZOOM_EVENT_SEND_DATA] = "ZOOM_EVENT_SEND_DATA";
39     zoom_events[ZOOM_EVENT_RECV_DATA] = "ZOOM_EVENT_RECV_DATA";
40     zoom_events[ZOOM_EVENT_TIMEOUT] = "ZOOM_EVENT_TIMEOUT";
41     zoom_events[ZOOM_EVENT_UNKNOWN] = "ZOOM_EVENT_UNKNOWN";
42     zoom_events[ZOOM_EVENT_SEND_APDU] = "ZOOM_EVENT_SEND_APDU";
43     zoom_events[ZOOM_EVENT_RECV_APDU] = "ZOOM_EVENT_RECV_APDU";
44     zoom_events[ZOOM_EVENT_RECV_RECORD] = "ZOOM_EVENT_RECV_RECORD";
45     zoom_events[ZOOM_EVENT_RECV_SEARCH] = "ZOOM_EVENT_RECV_SEARCH";
46
47     /* re-sorting event numbers to progress numbers */
48     zoom_progress[ZOOM_EVENT_NONE] = 0;
49     zoom_progress[ZOOM_EVENT_CONNECT] = 1;
50     zoom_progress[ZOOM_EVENT_SEND_DATA] = 3;
51     zoom_progress[ZOOM_EVENT_RECV_DATA] = 4;
52     zoom_progress[ZOOM_EVENT_TIMEOUT] = 8;
53     zoom_progress[ZOOM_EVENT_UNKNOWN] = 9;
54     zoom_progress[ZOOM_EVENT_SEND_APDU] = 2;
55     zoom_progress[ZOOM_EVENT_RECV_APDU] = 5;
56     zoom_progress[ZOOM_EVENT_RECV_RECORD] = 7;
57     zoom_progress[ZOOM_EVENT_RECV_SEARCH] = 6;
58
59     /* parameters */
60     //parameters.host = "";
61     //parameters.query = "";
62     parameters.concurrent = 1;
63     parameters.timeout = 0;
64 }
65  
66 struct time_type 
67 {
68     struct timeval now;
69     struct timeval then;
70     long sec;
71     long usec;
72 };
73
74 void time_init(struct time_type *ptime)
75 {
76     gettimeofday(&(ptime->now), 0);
77     gettimeofday(&(ptime->then), 0);
78     ptime->sec = 0;
79     ptime->usec = 0;
80 }
81
82 void time_stamp(struct time_type *ptime)
83 {
84     gettimeofday(&(ptime->now), 0);
85     ptime->sec = ptime->now.tv_sec - ptime->then.tv_sec;
86     ptime->usec = ptime->now.tv_usec - ptime->then.tv_usec;
87     if (ptime->usec < 0){
88         ptime->sec--;
89         ptime->usec += 1000000;
90     }
91 }
92
93 long time_sec(struct time_type *ptime)
94 {
95     return ptime->sec;
96 }
97
98 long time_usec(struct time_type *ptime)
99 {
100     return ptime->usec;
101 }
102
103 void print_option_error()
104 {
105     fprintf(stderr, "zoom-benchmark:  Call error\n");
106     fprintf(stderr, "zoom-benchmark -h host:port -q pqf-query "
107             "[-c no_concurrent] "
108             "[-t timeout] \n");
109     exit(1);
110 }
111
112
113 void read_params(int argc, char **argv, struct parameters_t *p_parameters){    
114     char *arg;
115     int ret;
116     while ((ret = options("h:q:c:t:", argv, argc, &arg)) != -2)
117         {
118             switch (ret)
119                 {
120                 case 'h':
121                     strcpy(p_parameters->host, arg);
122                     break;
123                 case 'q':
124                     strcpy(p_parameters->query, arg);
125                     break;
126                 case 'c':
127                     p_parameters->concurrent = atoi(arg);
128                     break;
129                 case 't':
130                     p_parameters->timeout = atoi(arg);
131                     break;
132                 case 0:
133                     //for (i = 0; i<number; i++)
134                     //    yaz_log(level, "%s", arg);
135                     print_option_error();
136                     break;
137                 default:
138                     print_option_error();
139                 }
140         }
141
142     //printf("zoom-benchmark\n");
143     //printf("   host:       %s \n", p_parameters->host);
144     //printf("   query:      %s \n", p_parameters->query);
145     //printf("   concurrent: %d \n", p_parameters->concurrent);
146     //printf("   timeout:    %d \n\n", p_parameters->timeout);
147
148     if (! strlen(p_parameters->host))
149         print_option_error();
150     if (! strlen(p_parameters->query))
151         print_option_error();
152     if (! (p_parameters->concurrent > 0))
153         print_option_error();
154     if (! (p_parameters->timeout >= 0))
155         print_option_error();
156 }
157
158
159
160 int main(int argc, char **argv)
161 {
162     init_statics();
163     
164     struct time_type time;
165
166     read_params(argc, argv, &parameters);
167
168     ZOOM_connection z[parameters.concurrent];
169     ZOOM_resultset r[parameters.concurrent];
170     ZOOM_options o = ZOOM_options_create();
171
172
173     /* async mode */
174     ZOOM_options_set (o, "async", "1");
175
176     /* get first record of result set (using piggyback) */
177     ZOOM_options_set (o, "count", "1");
178
179     /* preferred record syntax */
180     //ZOOM_options_set (o, "preferredRecordSyntax", "usmarc");
181     //ZOOM_options_set (o, "elementSetName", "F");
182
183     /* connect to all concurrent connections*/
184     int i;
185     for ( i = 0; i < parameters.concurrent; i++){
186         /* create connection - pass options (they are the same for all) */
187         z[i] = ZOOM_connection_create(o);
188
189         /* connect and init */
190             ZOOM_connection_connect(z[i], parameters.host, 0);
191     }
192     /* search all */
193     for (i = 0; i < parameters.concurrent; i++)
194         r[i] = ZOOM_connection_search_pqf (z[i], parameters.query);
195
196     // print header of table
197     printf ("second.usec\ttarget\tprogress\tevent\teventname\terror\terrorname\n");
198     time_init(&time);
199     /* network I/O. pass number of connections and array of connections */
200     while ((i = ZOOM_event (parameters.concurrent, z)))
201     { 
202         time_stamp(&time);
203
204         int event = ZOOM_connection_last_event(z[i-1]);
205         const char *errmsg;
206         const char *addinfo;
207         int error = 0;
208         int progress = zoom_progress[event];
209
210  
211         error = ZOOM_connection_error(z[i-1] , &errmsg, &addinfo);
212         if (error)
213             progress = -progress;
214         
215         printf ("%i.%06i\t%d\t%d\t%d\t%s\t%d\t%s\n",
216                 time_sec(&time), time_usec(&time), 
217                 i-1, progress,
218                 event, zoom_events[event], 
219                 error, errmsg);
220
221     }
222     
223     /* no more to be done. Inspect results */
224     // commented out right now - do nothing
225 /*     for (i = 0; i<parameters.concurrent; i++) */
226 /*     { */
227 /*         int error; */
228 /*         const char *errmsg, *addinfo; */
229 /*         const char *tname = (same_target ? argv[2] : argv[1+i]); */
230 /*         /\* display errors if any *\/ */
231 /*         if ((error = ZOOM_connection_error(z[i], &errmsg, &addinfo))) */
232 /*             fprintf (stderr, "%s error: %s (%d) %s\n", tname, errmsg, */
233 /*                      error, addinfo); */
234 /*         else */
235 /*         { */
236 /*             /\* OK, no major errors. Look at the result count *\/ */
237 /*             int pos; */
238 /*             printf ("%s: %d hits\n", tname, ZOOM_resultset_size(r[i])); */
239 /*             /\* go through all records at target *\/ */
240 /*             for (pos = 0; pos < 10; pos++) */
241 /*             { */
242 /*                 int len; /\* length of buffer rec *\/ */
243 /*                 const char *rec = */
244 /*                     ZOOM_record_get ( */
245 /*                         ZOOM_resultset_record (r[i], pos), "render", &len); */
246 /*                 /\* if rec is non-null, we got a record for display *\/ */
247 /*                 if (rec) */
248 /*                 { */
249 /*                     printf ("%d\n", pos+1); */
250 /*                     if (rec) */
251 /*                         fwrite (rec, 1, len, stdout); */
252 /*                     printf ("\n"); */
253 /*                 } */
254 /*             } */
255 /*         } */
256 /*     } */
257
258     /* destroy and exit */
259     for (i = 0; i<parameters.concurrent; i++)
260     {
261         ZOOM_resultset_destroy (r[i]);
262         ZOOM_connection_destroy (z[i]);
263     }
264     ZOOM_options_destroy(o);
265     exit (0);
266 }
267 /*
268  * Local variables:
269  * c-basic-offset: 4
270  * indent-tabs-mode: nil
271  * End:
272  * vim: shiftwidth=4 tabstop=8 expandtab
273  */
274