19e83e867e24ab616541dce3c7ab36649bef571a
[yaz-moved-to-github.git] / zoom / zoom-benchmark.c
1 /*
2  * $Id: zoom-benchmark.c,v 1.1 2005-09-07 13:45:15 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/zoom.h>
15
16
17
18
19 int main(int argc, char **argv)
20 {
21
22     struct timeval now;
23     struct timeval then;
24     long sec=0, usec=0;
25
26     int i;
27     int same_target = 0;
28     int no = argc-2;
29     ZOOM_connection z[1024]; /* allow at most 1024 connections */
30     ZOOM_resultset r[1024];  /* and result sets .. */
31     ZOOM_options o = ZOOM_options_create ();
32
33     if (argc < 3)
34     {
35         fprintf (stderr, "usage:\n%s target1 target2 ... targetN query\n",
36                  *argv);
37         exit (1);
38     }
39     if (argc == 4 && isdigit(argv[1][0]) && !strchr(argv[1],'.'))
40     {
41         no = atoi(argv[1]);
42         same_target = 1;
43     }
44
45     if (no > 500)
46         no = 500;
47
48     /* naming events */
49     
50     //typedef enum {
51     //    ZOOM_EVENT_NONE = 0,
52     //    ZOOM_EVENT_CONNECT = 1,
53     //    ZOOM_EVENT_SEND_DATA  = 2,
54     //    ZOOM_EVENT_RECV_DATA = 3,
55     //    ZOOM_EVENT_TIMEOUT = 4,
56     //    ZOOM_EVENT_UNKNOWN = 5,
57     //    ZOOM_EVENT_SEND_APDU = 6,
58     //    ZOOM_EVENT_RECV_APDU = 7,
59     //    ZOOM_EVENT_RECV_RECORD = 8,
60     //    ZOOM_EVENT_RECV_SEARCH = 9
61     //} ZOOM_events;
62
63     char* zoom_events[10];
64     zoom_events[ZOOM_EVENT_NONE] = "ZOOM_EVENT_NONE";
65     zoom_events[ZOOM_EVENT_CONNECT] = "ZOOM_EVENT_CONNECT";
66     zoom_events[ZOOM_EVENT_SEND_DATA] = "ZOOM_EVENT_SEND_DATA";
67     zoom_events[ZOOM_EVENT_RECV_DATA] = "ZOOM_EVENT_RECV_DATA";
68     zoom_events[ZOOM_EVENT_TIMEOUT] = "ZOOM_EVENT_TIMEOUT";
69     zoom_events[ZOOM_EVENT_UNKNOWN] = "ZOOM_EVENT_UNKNOWN";
70     zoom_events[ZOOM_EVENT_SEND_APDU] = "ZOOM_EVENT_SEND_APDU";
71     zoom_events[ZOOM_EVENT_RECV_APDU] = "ZOOM_EVENT_RECV_APDU";
72     zoom_events[ZOOM_EVENT_RECV_RECORD] = "ZOOM_EVENT_RECV_RECORD";
73     zoom_events[ZOOM_EVENT_RECV_SEARCH] = "ZOOM_EVENT_RECV_SEARCH";
74
75     int zoom_progress[10];
76     zoom_progress[ZOOM_EVENT_NONE] = 0;
77     zoom_progress[ZOOM_EVENT_CONNECT] = 1;
78     zoom_progress[ZOOM_EVENT_SEND_DATA] = 3;
79     zoom_progress[ZOOM_EVENT_RECV_DATA] = 4;
80     zoom_progress[ZOOM_EVENT_TIMEOUT] = 8;
81     zoom_progress[ZOOM_EVENT_UNKNOWN] = 9;
82     zoom_progress[ZOOM_EVENT_SEND_APDU] = 2;
83     zoom_progress[ZOOM_EVENT_RECV_APDU] = 5;
84     zoom_progress[ZOOM_EVENT_RECV_RECORD] = 7;
85     zoom_progress[ZOOM_EVENT_RECV_SEARCH] = 6;
86    
87
88     /* async mode */
89     ZOOM_options_set (o, "async", "1");
90
91     /* get first 10 records of result set (using piggyback) */
92     ZOOM_options_set (o, "count", "1");
93
94     /* preferred record syntax */
95     //ZOOM_options_set (o, "preferredRecordSyntax", "usmarc");
96     //ZOOM_options_set (o, "elementSetName", "F");
97
98     /* connect to all */
99     for (i = 0; i<no; i++)
100     {
101         /* create connection - pass options (they are the same for all) */
102         z[i] = ZOOM_connection_create (o);
103
104         /* connect and init */
105         if (same_target)
106             ZOOM_connection_connect (z[i], argv[2], 0);
107         else
108             ZOOM_connection_connect (z[i], argv[1+i], 0);
109     }
110     /* search all */
111     for (i = 0; i<no; i++)
112         r[i] = ZOOM_connection_search_pqf (z[i], argv[argc-1]);
113
114     printf ("second.usec target progress event eventname error errorname\n");
115
116
117     gettimeofday(&then, 0);
118     /* network I/O. pass number of connections and array of connections */
119     while ((i = ZOOM_event (no, z)))
120     { 
121
122         int event = ZOOM_connection_last_event(z[i-1]);
123         const char *errmsg;
124         const char *addinfo;
125         int error = 0;
126         int progress = zoom_progress[event];
127
128         gettimeofday(&now, 0);
129         //stamp(&now, &then);
130         sec = now.tv_sec - then.tv_sec;
131         usec = now.tv_usec - then.tv_usec;
132         if (usec < 0){
133             sec--;
134             usec += 1000000;
135         }
136  
137         error = ZOOM_connection_error(z[i-1] , &errmsg, &addinfo);
138         if (error)
139             progress = -progress;
140         
141         printf ("%i.%06i %d %d %d %s %d '%s' \n", sec, usec, 
142                 i-1, progress,
143                 event, zoom_events[event], 
144                 error, errmsg);
145
146     }
147     
148     /* no more to be done. Inspect results */
149     for (i = 0; i<no && 0; i++)
150     {
151         int error;
152         const char *errmsg, *addinfo;
153         const char *tname = (same_target ? argv[2] : argv[1+i]);
154         /* display errors if any */
155         if ((error = ZOOM_connection_error(z[i], &errmsg, &addinfo)))
156             fprintf (stderr, "%s error: %s (%d) %s\n", tname, errmsg,
157                      error, addinfo);
158         else
159         {
160             /* OK, no major errors. Look at the result count */
161             int pos;
162             printf ("%s: %d hits\n", tname, ZOOM_resultset_size(r[i]));
163             /* go through all records at target */
164             for (pos = 0; pos < 10; pos++)
165             {
166                 int len; /* length of buffer rec */
167                 const char *rec =
168                     ZOOM_record_get (
169                         ZOOM_resultset_record (r[i], pos), "render", &len);
170                 /* if rec is non-null, we got a record for display */
171                 if (rec)
172                 {
173                     printf ("%d\n", pos+1);
174                     if (rec)
175                         fwrite (rec, 1, len, stdout);
176                     printf ("\n");
177                 }
178             }
179         }
180     }
181
182     /* destroy and exit */
183     for (i = 0; i<no; i++)
184     {
185         ZOOM_resultset_destroy (r[i]);
186         ZOOM_connection_destroy (z[i]);
187     }
188     ZOOM_options_destroy(o);
189     exit (0);
190 }
191 /*
192  * Local variables:
193  * c-basic-offset: 4
194  * indent-tabs-mode: nil
195  * End:
196  * vim: shiftwidth=4 tabstop=8 expandtab
197  */
198