lat_connect.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * lat_connect.c - simple TCP connection latency test
  3. *
  4. * Three programs in one -
  5. * server usage: lat_connect -s
  6. * client usage: lat_connect hostname [N]
  7. * shutdown: lat_connect -hostname
  8. *
  9. * Copyright (c) 1994 Larry McVoy. Distributed under the FSF GPL with
  10. * additional restriction that results may published only if
  11. * (1) the benchmark is unmodified, and
  12. * (2) the version in the sccsid below is included in the report.
  13. * Support for this development by Sun Microsystems is gratefully acknowledged.
  14. */
  15. char *id = "$Id$\n";
  16. #include "bench.h"
  17. void server_main(int ac, char **av);
  18. void client_main(int ac, char **av);
  19. void
  20. doit(char *server)
  21. {
  22. int sock = tcp_connect(server, TCP_CONNECT, SOCKOPT_NONE);
  23. close(sock);
  24. }
  25. int
  26. main(int ac, char **av)
  27. {
  28. if (ac != 2) {
  29. fprintf(stderr, "Usage: %s -s OR %s [-]serverhost\n",
  30. av[0], av[0]);
  31. exit(1);
  32. }
  33. if (!strcmp(av[1], "-s")) {
  34. if (fork() == 0) {
  35. server_main(ac, av);
  36. }
  37. exit(0);
  38. } else {
  39. client_main(ac, av);
  40. }
  41. exit(0);
  42. /* NOTREACHED */
  43. }
  44. void
  45. client_main(int ac, char **av)
  46. {
  47. int sock;
  48. char *server;
  49. char buf[256];
  50. if (ac != 2) {
  51. fprintf(stderr, "usage: %s host\n", av[0]);
  52. exit(1);
  53. }
  54. server = av[1][0] == '-' ? &av[1][1] : av[1];
  55. /*
  56. * Stop server code.
  57. */
  58. if (av[1][0] == '-') {
  59. sock = tcp_connect(server, TCP_CONNECT, SOCKOPT_NONE);
  60. write(sock, "0", 1);
  61. close(sock);
  62. exit(0);
  63. /* NOTREACHED */
  64. }
  65. /*
  66. * We don't want more than a few of these, they stack up in time wait.
  67. * XXX - report an error if the clock is too shitty?
  68. */
  69. BENCH(doit(server), 0);
  70. sprintf(buf, "TCP/IP connection cost to %s", server);
  71. micro(buf, get_n());
  72. exit(0);
  73. /* NOTREACHED */
  74. }
  75. void
  76. server_main(int ac, char **av)
  77. {
  78. int newsock, sock, n;
  79. char c;
  80. if (ac != 2) {
  81. fprintf(stderr, "usage: %s -s\n", av[0]);
  82. exit(1);
  83. }
  84. GO_AWAY;
  85. sock = tcp_server(TCP_CONNECT, SOCKOPT_REUSE);
  86. for (;;) {
  87. newsock = tcp_accept(sock, SOCKOPT_NONE);
  88. c = 0;
  89. n = read(newsock, &c, 1);
  90. if (n > 0 && c == '0') {
  91. tcp_done(TCP_CONNECT);
  92. exit(0);
  93. }
  94. close(newsock);
  95. }
  96. /* NOTREACHED */
  97. }