File: benchmark.c

package info (click to toggle)
yaz 2.1.18-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 11,988 kB
  • ctags: 11,045
  • sloc: xml: 109,719; ansic: 41,566; sh: 10,625; makefile: 1,115; tcl: 380; yacc: 288
file content (146 lines) | stat: -rw-r--r-- 4,000 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* $Id: benchmark.c,v 1.9 2005/10/22 13:13:56 adam Exp $
 * Copyright (C) 1995-2005, Index Data ApS
 *
 * This file is part of the YAZ toolkit.
 *
 * See the file LICENSE.
 *
 * This is an elementary benchmarker for server performance.  It works
 * by repeatedly connecting to, seaching in and retrieving from the
 * specified server, and keeps statistics about the minimum, maximum
 * and average times for each operation.
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <yaz/options.h>
#include <stdarg.h>

#include <yaz/zoom.h>


struct boptions {
    int nconnect;               /* number of connections to make */
    int nsearch;                /* number of searches on each connection */
    int npresent;               /* number of presents for each search */
    int full;                   /* 1 = fetch full records, 0 = brief */
    int delay;                  /* number of ms to delay between ops */
    int random;                 /* if true, delay is random 0-specified */
    int verbosity;              /* 0 = quiet, higher => more verbose */
} boptions = {
    3,
    3,
    3,
    0,
    1000,
    1,
    0,
};


static int test(char *host, int port);
static void db_printf(int level, char *fmt, ...);
static void usage(const char *prog);

int main(int argc, char **argv)
{
    char *host = 0;
    int port = 0;
    int c;
    int i;
    int ok;
    int nok = 0;
    char *arg;
    
    while ((c = options("c:s:p:fbd:rv:", argv, argc, &arg)) != -2) {
        switch (c) {
        case 0:
            if (!host)
                host = arg;
            else if (!port)
                port = atoi(arg);
            else
                usage(*argv);
            break;
        case 'c': boptions.nconnect = atoi(arg); break;
        case 's': boptions.nsearch = atoi(arg); break;
        case 'p': boptions.npresent = atoi(arg); break;
        case 'f': boptions.full = 1; break;
        case 'b': boptions.full = 0; break;
        case 'd': boptions.delay = atoi(arg); break;
        case 'r': boptions.random = 1; break;
        case 'v': boptions.verbosity = atoi(arg); break;
        default: usage(*argv);
        }
    }

    if (!host || !port)
        usage(*argv);

    for (i = 0; i < boptions.nconnect; i++) {
        db_printf(2, "iteration %d of %d", i+1, boptions.nconnect);
        ok = test(host, port);
        if (ok) nok++;
    }

    db_printf(1, "passed %d of %d tests", nok, boptions.nconnect);
    if (nok < boptions.nconnect)
        printf("Failed %d of %d tests\n",
               boptions.nconnect-nok, boptions.nconnect);

    return 0;
}

static void usage(const char *prog)
{
    fprintf(stderr, "Usage: %s [options] <host> <port>\n"
"       -c <n>  Make <n> connection to the server [default: 3]\n"
"       -s <n>  Perform <n> searches on each connection [3]\n"
"       -p <n>  Make <n> present requests after each search [3]\n"
"       -f      Fetch full records [default: brief]\n"
"       -b      Fetch brief records\n"
"       -d <n>  Delay <n> ms after each operation\n"
"       -r      Delays are random between 0 and the specified number of ms\n"
"       -v <n>  Set verbosity level to <n> [0, silent on success]\n"
            , prog);
    exit(1);
}

static int test(char *host, int port)
{
    ZOOM_connection conn;
    int error;
    const char *errmsg, *addinfo;

    conn = ZOOM_connection_new(host, port);
    if ((error = ZOOM_connection_error(conn, &errmsg, &addinfo))) {
        fprintf(stderr, "ZOOM error: %s (%d): %s\n", errmsg, error, addinfo);
        return 0;
    }

    ZOOM_connection_destroy(conn);
    return 1;
}

static void db_printf(int level, char *fmt, ...)
{
    va_list ap;

    if (level > boptions.verbosity)
        return;

    fprintf(stderr, "DEBUG(%d): ", level);
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    fputc('\n', stderr);
    va_end(ap);
}
/*
 * Local variables:
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 * vim: shiftwidth=4 tabstop=8 expandtab
 */