This diff adds a timer to tcpbench as a command-line option (-t) so
that it is possible to stop the tcpbench client after a certain number
of seconds. This makes it easier to use tcpbench as part of a script.

Any comments and feedback would be appreciated.

Thanks,
Lawrence


Index: tcpbench.1
===================================================================
RCS file: /cvs/src/usr.bin/tcpbench/tcpbench.1,v
retrieving revision 1.16
diff -u -p -r1.16 tcpbench.1
--- tcpbench.1  18 Dec 2011 01:19:07 -0000      1.16
+++ tcpbench.1  26 Jan 2012 01:57:50 -0000
@@ -33,6 +33,7 @@
 .Op Fl r Ar interval
 .Op Fl S Ar space
 .Op Fl T Ar toskeyword
+.Op Fl t Ar secs
 .Op Fl V Ar rtable
 .Ar hostname
 .Nm
@@ -125,6 +126,10 @@ or one of the DiffServ Code Points:
 .Ar af11 ... af43 ,
 .Ar cs0 ... cs7 ;
 or a number in either hex or decimal.
+.It Fl t Ar secs
+Stop after
+.Ar secs
+seconds.
 .It Fl u
 Use UDP instead of TCP; this must be specified on both the client
 and the server.
Index: tcpbench.c
===================================================================
RCS file: /cvs/src/usr.bin/tcpbench/tcpbench.c,v
retrieving revision 1.29
diff -u -p -r1.29 tcpbench.c
--- tcpbench.c  18 Dec 2011 02:42:53 -0000      1.29
+++ tcpbench.c  26 Jan 2012 01:57:50 -0000
@@ -176,7 +176,7 @@ usage(void)
            "usage: tcpbench -l\n"
            "       tcpbench [-uv] [-B buf] [-b addr] [-k kvars] [-n 
connections]\n"
            "                [-p port] [-r interval] [-S space] [-T 
toskeyword]\n"
-           "                [-V rtable] hostname\n"
+           "                [-t secs] [-V rtable] hostname\n"
            "       tcpbench -s [-uv] [-B buf] [-k kvars] [-p port]\n"
            "                [-r interval] [-S space] [-T toskeyword] [-V 
rtable]\n");
        exit(1);
@@ -967,11 +967,19 @@ map_tos(char *s, int *val)
        return (0);
 }
 
+static void
+quit(int sig, short event, void *arg)
+{
+       exit(0);
+}
+
 int
 main(int argc, char **argv)
 {
        extern int optind;
        extern char *optarg;
+       struct timeval tv;
+       unsigned int secs;
 
        char kerr[_POSIX2_LINE_MAX], *tmp;
        struct addrinfo *aitop, *aib, hints;
@@ -980,7 +988,7 @@ main(int argc, char **argv)
        int ch, herr, nconn;
        struct nlist nl[] = { { "_tcbtable" }, { "" } };
        const char *host = NULL, *port = DEFAULT_PORT, *srcbind = NULL;
-       struct event ev_sigint, ev_sigterm, ev_sighup;
+       struct event ev_sigint, ev_sigterm, ev_sighup, ev_progtimer;
        struct statctx *udp_sc = NULL;
 
        /* Init world */
@@ -994,8 +1002,9 @@ main(int argc, char **argv)
        ptb->Tflag = -1;
        nconn = 1;
        aib = NULL;
+       secs = 0;
 
-       while ((ch = getopt(argc, argv, "b:B:hlk:n:p:r:sS:T:uvV:")) != -1) {
+       while ((ch = getopt(argc, argv, "b:B:hlk:n:p:r:sS:t:T:uvV:")) != -1) {
                switch (ch) {
                case 'b':
                        srcbind = optarg;
@@ -1068,6 +1077,12 @@ main(int argc, char **argv)
                        if (ptb->Tflag == -1 || ptb->Tflag > 255 || errstr)
                                errx(1, "illegal tos value %s", optarg);
                        break;
+               case 't':
+                       secs = strtonum(optarg, 1, UINT_MAX, &errstr);
+                       if (errstr != NULL)
+                               errx(1, "secs is %s: %s",
+                                   errstr, optarg);
+                       break;
                case 'h':
                default:
                        usage();
@@ -1171,8 +1186,15 @@ main(int argc, char **argv)
 
        if (ptb->sflag)
                server_init(aitop, udp_sc);
-       else
+       else {
+               if (secs > 0) {
+                       timerclear(&tv);
+                       tv.tv_sec = secs + 1;
+                       evtimer_set(&ev_progtimer, quit, NULL);
+                       evtimer_add(&ev_progtimer, &tv);
+               }
                client_init(aitop, nconn, udp_sc, aib);
+       }
        
        /* libevent main loop*/
        event_dispatch();

Reply via email to