webbench.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <netdb.h> // for gethostbyname
  6. #include <stddef.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <signal.h>
  11. #include <getopt.h>
  12. #include <unistd.h>
  13. int Connect(const char* host, int port) {
  14. struct sockaddr_in addr;
  15. addr.sin_family = AF_INET;
  16. in_addr_t inaddr = inet_addr(host);
  17. if (inaddr != INADDR_NONE) {
  18. addr.sin_addr.s_addr = inaddr;
  19. }
  20. else {
  21. struct hostent* phe = gethostbyname(host);
  22. if (phe == NULL) {
  23. return -1;
  24. }
  25. memcpy(&addr.sin_addr, phe->h_addr_list[0], phe->h_length);
  26. }
  27. addr.sin_port = htons(port);
  28. int sock = socket(AF_INET, SOCK_STREAM, 0);
  29. if (sock < 0) {
  30. perror("socket");
  31. return -2;
  32. }
  33. if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
  34. perror("connect");
  35. return -3;
  36. }
  37. return sock;
  38. }
  39. #define METHOD_GET 0
  40. #define METHOD_HEAD 1
  41. #define METHOD_OPTIONS 2
  42. #define METHOD_TRACE 3
  43. #define VERSION "webbench/1.19.3.15"
  44. volatile int timerexpired = 0; // for timer
  45. int time = 30;
  46. int clients = 1;
  47. char host[64] = {0};
  48. int port = 80;
  49. char* proxy_host = NULL;
  50. int proxy_port = 80;
  51. int method = METHOD_GET;
  52. int http = 1; // 1=HTTP/1.1 0=HTTP/1.0
  53. const char* url = NULL;
  54. #define REQUEST_SIZE 2048
  55. char request[REQUEST_SIZE] = {0};
  56. char buf[1460] = {0};
  57. int mypipe[2]; // IPC
  58. static const char options[] = "?hV01t:p:c:";
  59. static const struct option long_options[] = {
  60. {"help", no_argument, NULL, 'h'},
  61. {"version", no_argument, NULL, 'V'},
  62. {"time", required_argument, NULL, 't'},
  63. {"proxy", required_argument, NULL, 'p'},
  64. {"clients", required_argument, NULL, 'c'},
  65. {"http10", no_argument, NULL, '0'},
  66. {"http11", no_argument, NULL, '1'},
  67. {"get", no_argument, &method, METHOD_GET},
  68. {"head", no_argument, &method, METHOD_HEAD},
  69. {"options", no_argument, &method, METHOD_OPTIONS},
  70. {"trace", no_argument, &method, METHOD_TRACE},
  71. {NULL, 0, NULL, 0}
  72. };
  73. void print_usage() {
  74. printf("Usage: webbench [%s] URL\n", options);
  75. puts("\n\
  76. Options:\n\
  77. -?|-h|--help Print this information.\n\
  78. -V|--version Print version.\n\
  79. -0|--http10 Use HTTP/1.0 protocol.\n\
  80. -1|--http11 Use HTTP/1.1 protocol.\n\
  81. -t|--time <sec> Run benchmark for <sec> seconds. Default 30.\n\
  82. -p|--proxy <server:port> Use proxy server for request.\n\
  83. -c|--clients <n> Run <n> HTTP clients. Default one.\n\
  84. --get Use GET request method.\n\
  85. --head Use HEAD request method.\n\
  86. --options Use OPTIONS request method.\n\
  87. --trace Use TRACE request method.\n\
  88. ");
  89. }
  90. int parse_cmdline(int argc, char** argv) {
  91. int opt = 0;
  92. int opt_idx = 0;
  93. while ((opt=getopt_long(argc, argv, options, long_options, &opt_idx)) != EOF) {
  94. switch (opt) {
  95. case '?':
  96. case 'h': print_usage(); exit(1);
  97. case 'V': puts(VERSION); exit(1);
  98. case '0': http = 0; break;
  99. case '1': http = 1; break;
  100. case 't': time = atoi(optarg); break;
  101. case 'c': clients = atoi(optarg); break;
  102. case 'p':
  103. {
  104. // host:port
  105. char* pos = strrchr(optarg, ':');
  106. proxy_host = optarg;
  107. if (pos == NULL) break;
  108. if (pos == optarg ||
  109. pos == optarg + strlen(optarg) - 1) {
  110. printf("Error option --proxy\n");
  111. return -2;
  112. }
  113. *pos = '\0';
  114. proxy_port = atoi(pos+1);
  115. }
  116. break;
  117. }
  118. }
  119. if (optind == argc) {
  120. printf("Missing URL\n");
  121. return -2;
  122. }
  123. url = argv[optind];
  124. return 0;
  125. }
  126. static void alarm_handler(int singal) {
  127. timerexpired = 1;
  128. }
  129. int main(int argc, char** argv) {
  130. if (argc == 1) {
  131. print_usage();
  132. return 2;
  133. }
  134. int ret = parse_cmdline(argc, argv);
  135. if (ret != 0) {
  136. return ret;
  137. }
  138. printf("%d clients, running %d sec\n", clients, time);
  139. // domain port url
  140. const char* req_url = "/";
  141. if (proxy_host) {
  142. strncpy(host, proxy_host, sizeof(host));
  143. port = proxy_port;
  144. } else {
  145. // http://host:port/path
  146. const char* pos1 = strstr(url, "http://");
  147. if (pos1 == NULL) {
  148. pos1 = url;
  149. } else {
  150. pos1 += strlen("http://");
  151. }
  152. const char* pos2 = strchr(pos1, '/');
  153. if (pos2 == NULL) {
  154. pos2 = url + strlen(url);
  155. } else {
  156. req_url = pos2;
  157. }
  158. int len = pos2 - pos1;
  159. char* server = (char*)malloc(len+1);
  160. memcpy(server, pos1, len);
  161. server[len] = '\0';
  162. char* pos3 = strrchr(server, ':');
  163. if (pos3 == NULL) {
  164. port = 80;
  165. } else {
  166. *pos3 = '\0';
  167. port = atoi(pos3+1);
  168. }
  169. strncpy(host, server, sizeof(host));
  170. free(server);
  171. }
  172. printf("server %s:%d\n", host, port);
  173. // test connect
  174. int sock = Connect(host, port);
  175. if (sock < 0) {
  176. printf("Connect failed!\n");
  177. return -20;
  178. } else {
  179. printf("Connect test OK!\n");
  180. close(sock);
  181. }
  182. // build request
  183. switch (method) {
  184. case METHOD_GET:
  185. default:
  186. strcpy(request, "GET");
  187. break;
  188. case METHOD_HEAD:
  189. strcpy(request, "HEAD");
  190. break;
  191. case METHOD_OPTIONS:
  192. strcpy(request, "OPTIONS");
  193. break;
  194. case METHOD_TRACE:
  195. strcpy(request, "TRACE");
  196. break;
  197. }
  198. strcat(request, " ");
  199. strcat(request, req_url);
  200. strcat(request, " ");
  201. if (http == 0) {
  202. strcat(request, "HTTP/1.0");
  203. } else if (http == 1) {
  204. strcat(request, "HTTP/1.1");
  205. }
  206. strcat(request, "\r\n");
  207. strcat(request, "User-Agent: webbench/1.18.3.15\r\n");
  208. strcat(request, "Cache-Control: no-cache\r\n");
  209. strcat(request, "Connection: close\r\n");
  210. strcat(request, "\r\n");
  211. printf("%s", request);
  212. // IPC
  213. if (pipe(mypipe) < 0) {
  214. perror("pipe");
  215. exit(20);
  216. }
  217. // fork childs
  218. pid_t pid = 0;
  219. FILE* fp = NULL;
  220. int succeed = 0, failed = 0, bytes = 0;
  221. int childs = clients;
  222. for (int i = 0; i < childs; ++i) {
  223. pid = fork();
  224. if (pid < 0) {
  225. perror("fork");
  226. exit(-10);
  227. }
  228. if (pid == 0) {
  229. // child
  230. //printf("child[%d] start\n", getpid());
  231. signal(SIGALRM, alarm_handler);
  232. alarm(time);
  233. int sock = -1;
  234. loop:
  235. while (1) {
  236. if (timerexpired) break;
  237. sock = Connect(host, port);
  238. if (sock <= 0) {
  239. ++failed;
  240. continue;
  241. }
  242. int len = strlen(request);
  243. int wrbytes = write(sock, request, len);
  244. //printf("write %d bytes\n", wrbytes);
  245. if (wrbytes != len) {
  246. ++failed;
  247. sock = -1;
  248. continue;
  249. }
  250. while (1) {
  251. if (timerexpired) break;
  252. int rdbytes = read(sock, buf, sizeof(buf));
  253. //printf("read %d bytes\n", rdbytes);
  254. if (rdbytes < 0) {
  255. ++failed;
  256. sock = -1;
  257. goto loop;
  258. }
  259. if (rdbytes == 0) break;
  260. bytes += rdbytes;
  261. }
  262. close(sock);
  263. ++succeed;
  264. }
  265. fp = fdopen(mypipe[1], "w");
  266. if (fp == NULL) {
  267. perror("fdopen");
  268. return 30;
  269. }
  270. fprintf(fp, "%d %d %d\n", succeed, failed, bytes);
  271. fclose(fp);
  272. //printf("child[%d] end\n", getpid());
  273. return 0;
  274. }
  275. }
  276. fp = fdopen(mypipe[0], "r");
  277. if (fp == NULL) {
  278. perror("fdopen");
  279. return 30;
  280. }
  281. while (1) {
  282. int i,j,k;
  283. fscanf(fp, "%d %d %d", &i, &j, &k);
  284. succeed += i;
  285. failed += j;
  286. bytes += k;
  287. if (--childs==0) break;
  288. }
  289. fclose(fp);
  290. printf("recv %d bytes/sec, %d succeed, %d failed\n",
  291. (int)(bytes)/time,
  292. succeed,
  293. failed);
  294. return 0;
  295. }