Happy new year
[yaz-moved-to-github.git] / util / benchmark.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 #if HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <yaz/options.h>
13 #include <stdarg.h>
14
15 #include <yaz/zoom.h>
16
17
18 struct boptions {
19     int nconnect;               /* number of connections to make */
20     int nsearch;                /* number of searches on each connection */
21     int npresent;               /* number of presents for each search */
22     int full;                   /* 1 = fetch full records, 0 = brief */
23     int delay;                  /* number of ms to delay between ops */
24     int random;                 /* if true, delay is random 0-specified */
25     int verbosity;              /* 0 = quiet, higher => more verbose */
26 } boptions = {
27     3,
28     3,
29     3,
30     0,
31     1000,
32     1,
33     0,
34 };
35
36
37 static int test(char *host, int port);
38 static void db_printf(int level, char *fmt, ...);
39 static void usage(const char *prog);
40
41 int main(int argc, char **argv)
42 {
43     char *host = 0;
44     int port = 0;
45     int c;
46     int i;
47     int ok;
48     int nok = 0;
49     char *arg;
50
51     while ((c = options("c:s:p:fbd:rv:", argv, argc, &arg)) != -2) {
52         switch (c) {
53         case 0:
54             if (!host)
55                 host = arg;
56             else if (!port)
57                 port = atoi(arg);
58             else
59                 usage(*argv);
60             break;
61         case 'c': boptions.nconnect = atoi(arg); break;
62         case 's': boptions.nsearch = atoi(arg); break;
63         case 'p': boptions.npresent = atoi(arg); break;
64         case 'f': boptions.full = 1; break;
65         case 'b': boptions.full = 0; break;
66         case 'd': boptions.delay = atoi(arg); break;
67         case 'r': boptions.random = 1; break;
68         case 'v': boptions.verbosity = atoi(arg); break;
69         default: usage(*argv);
70         }
71     }
72
73     if (!host || !port)
74         usage(*argv);
75
76     for (i = 0; i < boptions.nconnect; i++) {
77         db_printf(2, "iteration %d of %d", i+1, boptions.nconnect);
78         ok = test(host, port);
79         if (ok) nok++;
80     }
81
82     db_printf(1, "passed %d of %d tests", nok, boptions.nconnect);
83     if (nok < boptions.nconnect)
84         printf("Failed %d of %d tests\n",
85                boptions.nconnect-nok, boptions.nconnect);
86
87     return 0;
88 }
89
90 static void usage(const char *prog)
91 {
92     fprintf(stderr, "Usage: %s [options] <host> <port>\n"
93 "       -c <n>  Make <n> connection to the server [default: 3]\n"
94 "       -s <n>  Perform <n> searches on each connection [3]\n"
95 "       -p <n>  Make <n> present requests after each search [3]\n"
96 "       -f      Fetch full records [default: brief]\n"
97 "       -b      Fetch brief records\n"
98 "       -d <n>  Delay <n> ms after each operation\n"
99 "       -r      Delays are random between 0 and the specified number of ms\n"
100 "       -v <n>  Set verbosity level to <n> [0, silent on success]\n"
101             , prog);
102     exit(1);
103 }
104
105 static int test(char *host, int port)
106 {
107     ZOOM_connection conn;
108     int error;
109     const char *errmsg, *addinfo;
110
111     conn = ZOOM_connection_new(host, port);
112     if ((error = ZOOM_connection_error(conn, &errmsg, &addinfo))) {
113         fprintf(stderr, "ZOOM error: %s (%d): %s\n", errmsg, error, addinfo);
114         return 0;
115     }
116
117     ZOOM_connection_destroy(conn);
118     return 1;
119 }
120
121 static void db_printf(int level, char *fmt, ...)
122 {
123     va_list ap;
124
125     if (level > boptions.verbosity)
126         return;
127
128     fprintf(stderr, "DEBUG(%d): ", level);
129     va_start(ap, fmt);
130     vfprintf(stderr, fmt, ap);
131     fputc('\n', stderr);
132     va_end(ap);
133 }
134 /*
135  * Local variables:
136  * c-basic-offset: 4
137  * c-file-style: "Stroustrup"
138  * indent-tabs-mode: nil
139  * End:
140  * vim: shiftwidth=4 tabstop=8 expandtab
141  */
142