<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv=Content-Type content="text/html; charset=utf8"> <title>/usr/web/sources/contrib/quanstro/tcptest.c - Plan 9 from Bell Labs</title> <!-- THIS FILE IS AUTOMATICALLY GENERATED. --> <!-- EDIT sources.tr INSTEAD. --> </meta> </head> <body> <p style="margin-top: 0; margin-bottom: 0.17in"></p> <p style="line-height: 1.2em; margin-left: 1.00in; text-indent: 0.00in; margin-right: 1.00in; margin-top: 0; margin-bottom: 0; text-align: center;"> <span style="font-size: 10pt"><a href="/plan9/">Plan 9 from Bell Labs</a>’s /usr/web/sources/contrib/quanstro/tcptest.c</span></p> <p style="margin-top: 0; margin-bottom: 0.17in"></p> <p style="margin-top: 0; margin-bottom: 0.17in"></p> <center><font size=-1> Copyright © 2009 Alcatel-Lucent.<br /> Distributed under the <a href="/plan9/license.html">Lucent Public License version 1.02</a>. <br /> <a href="/plan9/download.html">Download the Plan 9 distribution.</a> </font> </center> <p style="margin-top: 0; margin-bottom: 0.17in"></p> <table width="100%" cellspacing=0 border=0><tr><td align="center"> <table cellspacing=0 cellpadding=5 bgcolor="#eeeeff"><tr><td align="left"> <pre> <!-- END HEADER --> #include <u.h> #include <libc.h> char *service = "17766"; char *laddr = "*"; char *iaddr; uchar flag[127]; ulong n; long sleepms; #define dprint(...) {if(flag['d']) print(__VA_ARGS__);} enum { Blen = 8*1024, }; void usage(void) { fprint(2, "usage: %s [-p port] [-i ip -n count] [-l]\n", argv0); exits("usage"); } int notef(void*, char *note) { if(strstr(note, "alarm") != nil) return 1; return 0; } char* ·netmkaddr(char *linear, char *net, char *service) { char *r; static QLock stupid; qlock(&stupid); /* protect stupid static memory */ r = netmkaddr(linear, net, service); if(r != nil) r = strdup(r); qunlock(&stupid); return r; } void client(void) { char *dst, b[Blen]; int fd, i; atnotify(notef, 1); sleep(1); dprint("client\n"); dst = ·netmkaddr(iaddr, "tcp", service); alarm(3*1000); if((fd = dial(dst, 0, 0, 0)) < 0) sysfatal("tcptest: %r"); alarm(0); free(dst); dprint("dialed\n"); for (i = 0; i < n; ++i) { if(write(fd, b, sizeof b) < 0) sysfatal("tcptest: %r"); } close(fd); dprint("wrote\n"); } double cyclestoseconds(uvlong cycles) { char buf[128], *f[10]; int fd, n; static uvlong fasthz; if(fasthz == 0){ fd = open("/dev/time", OREAD); if(fd == 0) return -1.; n = read(fd, buf, sizeof(buf)-1); if(n > 0){ buf[n] = 0; n = tokenize(buf, f, nelem(f)); if(n == 4) fasthz = strtoull(f[3], 0, 0); } close(fd); if(fasthz == 0) return -1.; } return (double)cycles / (double)fasthz; } void server(void) { int acfd, lcfd, dfd; char adir[40], ldir[40], b[Blen], *a; long c; uvlong i; vlong startns, deltans; double ratemb, maxr; NetConnInfo *nc; dprint("server\n"); a = ·netmkaddr(laddr, "tcp", service); acfd = announce(a, adir); if(acfd == -1) sysfatal("tcptest: %r"); free(a); dprint("listen\n"); if((lcfd = listen(adir, ldir)) < 0) sysfatal("tcptest: %r"); dprint("listened\n"); if((dfd = accept(lcfd, ldir)) < 0) sysfatal("tcptest: %r"); nc = getnetconninfo(ldir, dfd); dprint("accepted\n"); startns = nsec(); for (i = 0;; i++){ c = readn(dfd, b, sizeof b); if(c != sizeof b) break; if(sleepms) sleep(sleepms); } deltans = nsec() - startns; close(dfd); close(lcfd); print("%s count %llud; ", nc? nc->raddr: "(unknown)", i); free(nc); i *= Blen; ratemb = i*1000./deltans; if(sleepms > 0){ maxr = Blen*1000./sleepms / 1e6; print("%llud bytes in %lld ns @ %.2g MB/s (%ldms; limit %.2g MB/s)\n", i, deltans, ratemb, sleepms, maxr); } else print("%llud bytes in %lld ns @ %.2g MB/s (0ms)\n", i, deltans, ratemb); } void main(int argc, char **argv) { ARGBEGIN { case 'p': service = EARGF(usage()); break; case 'i': iaddr = EARGF(usage()); break; case 'a': laddr = EARGF(usage()); break; case 'n': n = strtoul(EARGF(usage()), nil, 0); break; case 's': sleepms = strtoul(EARGF(usage()), nil, 0); break; case 'd': case 'l': flag[ARGC()] = 1; break; default: usage(); }ARGEND if(argc != 0) usage(); if(flag['l']){ // loop back switch (fork()) { case -1: sysfatal("tcptest: %r"); break; case 0: server(); break; default: client(); wait(); break; } }else if(iaddr != nil) client(); else server(); exits(nil); } <!-- BEGIN TAIL --> </pre> </td></tr></table> </td></tr></table> <p style="margin-top: 0; margin-bottom: 0.17in"></p> <p style="line-height: 1.2em; margin-left: 1.00in; text-indent: 0.00in; margin-right: 1.00in; margin-top: 0; margin-bottom: 0; text-align: center;"> <span style="font-size: 10pt"></span></p> <p style="margin-top: 0; margin-bottom: 0.50in"></p> <p style="margin-top: 0; margin-bottom: 0.33in"></p> <center><table border="0"><tr> <td valign="middle"><a href="http://www.alcatel-lucent.com/"><img border="0" src="/plan9/img/logo_ft.gif" alt="Bell Labs" /> </a></td> <td valign="middle"><a href="http://www.opensource.org"><img border="0" alt="OSI certified" src="/plan9/img/osi-certified-60x50.gif" /> </a></td> <td><img style="padding-right: 45px;" alt="Powered by Plan 9" src="/plan9/img/power36.gif" /> </td> </tr></table></center> <p style="margin-top: 0; margin-bottom: 0.17in"></p> <center> <span style="font-size: 10pt">(<a href="/plan9/">Return to Plan 9 Home Page</a>)</span> </center> <p style="margin-top: 0; margin-bottom: 0.17in"></p> <center><font size=-1> <span style="font-size: 10pt"><a href="http://www.lucent.com/copyright.html">Copyright</a></span> <span style="font-size: 10pt">© 2009 Alcatel-Lucent.</span> <span style="font-size: 10pt">All Rights Reserved.</span> <br /> <span style="font-size: 10pt">Comments to</span> <span style="font-size: 10pt"><a href="mailto:webmaster@plan9.bell-labs.com">webmaster@plan9.bell-labs.com</a>.</span> </font></center> </body> </html>