Fixed use of dprintf(). The define of dprintf() is a part <stdio.h> in
[yaz-moved-to-github.git] / util / benchmark.c
1 /* $Id: benchmark.c,v 1.5 2004-05-19 05:32:47 oleg Exp $
2  * Copyright (C) 2003-2004 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 <stdarg.h>
18
19 #include <yaz/zoom.h>
20
21
22 struct options {
23     int nconnect;               /* number of connections to make */
24     int nsearch;                /* number of searches on each connection */
25     int npresent;               /* number of presents for each search */
26     int full;                   /* 1 = fetch full records, 0 = brief */
27     int delay;                  /* number of ms to delay between ops */
28     int random;                 /* if true, delay is random 0-specified */
29     int verbosity;              /* 0 = quiet, higher => more verbose */
30 } options = {
31     3,
32     3,
33     3,
34     0,
35     1000,
36     1,
37     0,
38 };
39
40
41 static int test(char *host, int port);
42 #ifndef HAVE_DPRINTF
43 static void dprintf(int level, char *fmt, ...);
44 #endif
45
46 int main(int argc, char **argv)
47 {
48     char *host;
49     int port;
50     int c;
51     int i;
52     int ok;
53     int nok = 0;
54
55     while ((c = getopt(argc, argv, "c:s:p:fbd:rv:")) != -1) {
56         switch (c) {
57         case 'c': options.nconnect = atoi(optarg); break;
58         case 's': options.nsearch = atoi(optarg); break;
59         case 'p': options.npresent = atoi(optarg); break;
60         case 'f': options.full = 1; break;
61         case 'b': options.full = 0; break;
62         case 'd': options.delay = atoi(optarg); break;
63         case 'r': options.random = 1; break;
64         case 'v': options.verbosity = atoi(optarg); break;
65         default: goto USAGE;
66         }
67     }
68
69     if (argc-optind != 2) {
70     USAGE:
71         fprintf(stderr, "Usage: %s [options] <host> <port>\n"
72 "       -c <n>  Make <n> connection to the server [default: 3]\n"
73 "       -s <n>  Perform <n> searches on each connection [3]\n"
74 "       -p <n>  Make <n> present requests after each search [3]\n"
75 "       -f      Fetch full records [default: brief]\n"
76 "       -b      Fetch brief records\n"
77 "       -d <n>  Delay <n> ms after each operation\n"
78 "       -r      Delays are random between 0 and the specified number of ms\n"
79 "       -v <n>  Set verbosity level to <n> [0, silent on success]\n"
80 , argv[0]);
81         return 1;
82     }
83
84     host = argv[optind];
85     port = atoi(argv[optind+1]);
86
87     for (i = 0; i < options.nconnect; i++) {
88         dprintf(2, "iteration %d of %d", i+1, options.nconnect);
89         ok = test(host, port);
90         if (ok) nok++;
91     }
92
93     dprintf(1, "passed %d of %d tests", nok, options.nconnect);
94     if (nok < options.nconnect)
95         printf("Failed %d of %d tests\n",
96                options.nconnect-nok, options.nconnect);
97
98
99     return 0;
100 }
101
102
103 static int test(char *host, int port)
104 {
105     ZOOM_connection conn;
106     int error;
107     const char *errmsg, *addinfo;
108
109     conn = ZOOM_connection_new(host, port);
110     if ((error = ZOOM_connection_error(conn, &errmsg, &addinfo))) {
111         fprintf(stderr, "ZOOM error: %s (%d): %s\n", errmsg, error, addinfo);
112         return 0;
113     }
114
115     ZOOM_connection_destroy(conn);
116     return 1;
117 }
118
119 #ifndef HAVE_DPRINTF
120 static void dprintf(int level, char *fmt, ...)
121 {
122     va_list ap;
123
124     if (level > options.verbosity)
125         return;
126
127     fprintf(stderr, "DEBUG(%d): ", level);
128     va_start(ap, fmt);
129     vfprintf(stderr, fmt, ap);
130     fputc('\n', stderr);
131     va_end(ap);
132 }
133 #endif