1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 Index Data
3 * See the file LICENSE for details.
11 #include <sys/types.h>
24 #include <yaz/yaz-util.h>
25 #include <yaz/proto.h>
26 #include <yaz/comstack.h>
28 #define PACKET_SIZE 64
33 static int send_packet(const char *host)
35 char buf[PACKET_SIZE];
40 COMSTACK cs = cs_create_host(host, 1, &add);
45 if (cs_connect(cs, add) < 0)
48 for (i = 0; i<sizeof(buf); i++)
51 buf[i] = rand() & 0xff;
53 cs_put(cs, buf, sizeof(buf));
61 static void test_file(const char *fname)
64 ODR odr = odr_createmem(ODR_DECODE);
65 char buf[PACKET_SIZE];
67 int fd =open(fname, O_RDONLY, 0666);
70 yaz_log(YLOG_ERRNO|YLOG_FATAL, "open %s", fname);
73 while (off < sizeof(buf))
76 rd = read(fd, buf+off, sizeof(buf)-off);
78 yaz_log(YLOG_ERRNO|YLOG_FATAL, "read %s", fname);
87 yaz_log(YLOG_ERRNO|YLOG_FATAL, "close %s", fname);
90 odr_setbuf(odr, buf, off, 0);
91 z_GDU(odr, &req, 0, 0);
97 static void test_random(int run, const char *fname, const char *fname2,
101 char buf[PACKET_SIZE];
106 if (!strcmp(fname2, "-"))
109 dumpfile = fopen(fname2, "w");
112 for (i = 0; i<sizeof(buf); i++)
113 buf[i] = rand() & 0xff;
115 for (j = 0; j<sizeof(buf)-1; j++)
121 odr = odr_createmem(ODR_DECODE);
125 int fd =open(fname, O_TRUNC|O_CREAT|O_WRONLY, 0666);
128 yaz_log(YLOG_ERRNO|YLOG_FATAL, "open %s", fname);
131 while (sizeof(buf)-j-off > 0)
134 wrote = write(fd, buf+off+j, sizeof(buf)-j-off);
136 yaz_log(YLOG_ERRNO|YLOG_FATAL, "write %s", fname);
143 yaz_log(YLOG_ERRNO|YLOG_FATAL, "close %s", fname);
147 mbuf = malloc(sizeof(buf)-j);
148 memcpy(mbuf, buf+j, sizeof(buf)-j);
149 odr_setbuf(odr, mbuf, sizeof(buf)-j, 0);
150 if (z_GDU(odr, &req, 0, 0))
155 odr_geterrorx(odr, &ex);
159 odr_dumpBER(dumpfile, buf+j, sizeof(buf)-j);
164 if (dumpfile && dumpfile != stdout)
168 void sigint_handler(int x)
173 int main(int argc, char **argv)
175 int start = 0, end = 10000000, ret, i, estat[100];
178 char *packet_fname = 0;
180 signal(SIGINT, sigint_handler);
181 signal(SIGTERM, sigint_handler);
182 for (i = 0; i<sizeof(estat)/sizeof(*estat); i++)
185 while ((ret = options("s:e:b:p:", argv, argc, &arg)) != -2)
202 if (!strcmp(arg, "random"))
205 while(!stop && (end == 0 || i < end))
209 printf ("\r[%d]", i); fflush(stdout);
210 test_random(i, packet_fname, ber_fname, estat);
216 fprintf (stderr, "usage\n");
217 fprintf (stderr, " [-s start] [-e end] [-b berdump] [-p packetdump] random\n");
222 for (i = 0; i < sizeof(estat)/sizeof(*estat); i++)
224 printf ("%3d %9d\n", i, estat[i]);
230 * c-file-style: "Stroustrup"
231 * indent-tabs-mode: nil
233 * vim: shiftwidth=4 tabstop=8 expandtab