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