Use db_printf instead of dprintf which is a public symbol in many systems
[yaz-moved-to-github.git] / util / benchmark.c
1 /* $Id: benchmark.c,v 1.6 2004-06-15 09:58:13 adam 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 static void db_printf(int level, char *fmt, ...);
43
44 int main(int argc, char **argv)
45 {
46     char *host;
47     int port;
48     int c;
49     int i;
50     int ok;
51     int nok = 0;
52
53     while ((c = getopt(argc, argv, "c:s:p:fbd:rv:")) != -1) {
54         switch (c) {
55         case 'c': options.nconnect = atoi(optarg); break;
56         case 's': options.nsearch = atoi(optarg); break;
57         case 'p': options.npresent = atoi(optarg); break;
58         case 'f': options.full = 1; break;
59         case 'b': options.full = 0; break;
60         case 'd': options.delay = atoi(optarg); break;
61         case 'r': options.random = 1; break;
62         case 'v': options.verbosity = atoi(optarg); break;
63         default: goto USAGE;
64         }
65     }
66
67     if (argc-optind != 2) {
68     USAGE:
69         fprintf(stderr, "Usage: %s [options] <host> <port>\n"
70 "       -c <n>  Make <n> connection to the server [default: 3]\n"
71 "       -s <n>  Perform <n> searches on each connection [3]\n"
72 "       -p <n>  Make <n> present requests after each search [3]\n"
73 "       -f      Fetch full records [default: brief]\n"
74 "       -b      Fetch brief records\n"
75 "       -d <n>  Delay <n> ms after each operation\n"
76 "       -r      Delays are random between 0 and the specified number of ms\n"
77 "       -v <n>  Set verbosity level to <n> [0, silent on success]\n"
78 , argv[0]);
79         return 1;
80     }
81
82     host = argv[optind];
83     port = atoi(argv[optind+1]);
84
85     for (i = 0; i < options.nconnect; i++) {
86         db_printf(2, "iteration %d of %d", i+1, options.nconnect);
87         ok = test(host, port);
88         if (ok) nok++;
89     }
90
91     db_printf(1, "passed %d of %d tests", nok, options.nconnect);
92     if (nok < options.nconnect)
93         printf("Failed %d of %d tests\n",
94                options.nconnect-nok, options.nconnect);
95
96
97     return 0;
98 }
99
100
101 static int test(char *host, int port)
102 {
103     ZOOM_connection conn;
104     int error;
105     const char *errmsg, *addinfo;
106
107     conn = ZOOM_connection_new(host, port);
108     if ((error = ZOOM_connection_error(conn, &errmsg, &addinfo))) {
109         fprintf(stderr, "ZOOM error: %s (%d): %s\n", errmsg, error, addinfo);
110         return 0;
111     }
112
113     ZOOM_connection_destroy(conn);
114     return 1;
115 }
116
117 static void db_printf(int level, char *fmt, ...)
118 {
119     va_list ap;
120
121     if (level > options.verbosity)
122         return;
123
124     fprintf(stderr, "DEBUG(%d): ", level);
125     va_start(ap, fmt);
126     vfprintf(stderr, fmt, ap);
127     fputc('\n', stderr);
128     va_end(ap);
129 }