lat_http.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * lat_http.c - simple HTTP transaction latency test
  3. *
  4. * usage: lat_http hostname [port] < filelist
  5. *
  6. * Copyright (c) 1994-6 Larry McVoy. Distributed under the FSF GPL with
  7. * additional restriction that results may published only if
  8. * (1) the benchmark is unmodified, and
  9. * (2) the version in the sccsid below is included in the report.
  10. * Support for this development by Sun Microsystems is gratefully acknowledged.
  11. */
  12. char *id = "$Id$\n";
  13. #include "bench.h"
  14. char *buf;
  15. int debug;
  16. int echo;
  17. int
  18. http(char *server, char *file, int prog)
  19. {
  20. int sock;
  21. int n;
  22. int b = 0;
  23. sock = tcp_connect(server, prog, SOCKOPT_REUSE);
  24. sprintf(buf, "GET /%s HTTP/1.0\r\n\r\n\n", file);
  25. if (debug) {
  26. printf(buf);
  27. }
  28. write(sock, buf, strlen(buf));
  29. while ((n = read(sock, buf, XFERSIZE)) > 0) {
  30. b += n;
  31. if (echo) {
  32. write(1, buf, n);
  33. }
  34. }
  35. close(sock);
  36. if (debug) {
  37. printf("Got %d\n", b);
  38. }
  39. return (b);
  40. }
  41. void
  42. killhttp(char *server, int prog)
  43. {
  44. int sock;
  45. sock = tcp_connect(server, prog, SOCKOPT_REUSE);
  46. write(sock, "EXIT", 4);
  47. close(sock);
  48. }
  49. void chop(register char *s) { while (*s && *s != '\n') s++; *s = 0; }
  50. int
  51. main(int ac, char **av)
  52. {
  53. char *server;
  54. int i, prog;
  55. uint64 total = 0;
  56. uint64 usecs = 0;
  57. double avg;
  58. char *name = av[0];
  59. char file[1024];
  60. if (ac > 1 && !strcmp("-d", av[1])) {
  61. debug++;
  62. ac--, av++;
  63. }
  64. if (ac > 1 && !strcmp("-e", av[1])) {
  65. echo++;
  66. ac--, av++;
  67. }
  68. if (ac < 2) {
  69. fprintf(stderr, "Usage: %s [-d] [-e] [-]serverhost [port] < list\n",
  70. name);
  71. exit(1);
  72. }
  73. server = av[1];
  74. av++, ac--; /* eat server */
  75. if (ac > 1 && atoi(av[ac - 1]) != 0) {
  76. prog = -atoi(av[ac - 1]);
  77. ac--; /* eat port */
  78. } else {
  79. prog = -80;
  80. }
  81. if (server[0] == '-') {
  82. server++;
  83. killhttp(server, prog);
  84. exit(0);
  85. }
  86. i = 0;
  87. buf = valloc(XFERSIZE);
  88. bzero(buf, XFERSIZE);
  89. while (fgets(file, sizeof(file), stdin)) {
  90. chop(file);
  91. start(0);
  92. total += http(server, file, prog);
  93. usecs += stop(0,0);
  94. i++;
  95. }
  96. avg = total;
  97. avg /= (i - 1);
  98. if (avg > 1000) {
  99. avg /= 1000;
  100. fprintf(stderr, "Avg xfer: %.1fKB, ", avg);
  101. } else {
  102. fprintf(stderr, "Avg xfer %d, ", (int)avg);
  103. }
  104. settime(usecs);
  105. latency((uint64)1, total);
  106. exit(0);
  107. }