lat_unix_connect.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * lat_unix_connect.c - simple UNIX 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()
  21. {
  22. int sock = unix_connect("/tmp/af_unix");
  23. close(sock);
  24. }
  25. int
  26. main(int ac, char **av)
  27. {
  28. if (ac > 1 && !strcmp(av[1], "-s")) {
  29. if (fork() == 0) {
  30. server_main(ac, av);
  31. }
  32. exit(0);
  33. } else {
  34. client_main(ac, av);
  35. }
  36. exit(0);
  37. /* NOTREACHED */
  38. }
  39. void
  40. client_main(int ac, char **av)
  41. {
  42. char buf[256];
  43. if (ac != 1) {
  44. fprintf(stderr, "usage: %s\n", av[0]);
  45. exit(1);
  46. }
  47. BENCH(doit(), 100000);
  48. sprintf(buf, "UNIX connection cost ");
  49. micro(buf, get_n());
  50. exit(0);
  51. /* NOTREACHED */
  52. }
  53. void
  54. server_main(int ac, char **av)
  55. {
  56. int newsock, sock;
  57. char c;
  58. if (ac != 2) {
  59. fprintf(stderr, "usage: %s -s\n", av[0]);
  60. exit(1);
  61. }
  62. GO_AWAY;
  63. sock = unix_server("/tmp/af_unix");
  64. for (;;) {
  65. newsock = unix_accept(sock);
  66. c = 0;
  67. read(newsock, &c, 1);
  68. if (c && c == '0') {
  69. unix_done(sock, "/tmp/af_unix");
  70. exit(0);
  71. }
  72. close(newsock);
  73. }
  74. /* NOTREACHED */
  75. }