webbench.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. int keepalive = 0;
  54. const char* url = NULL;
  55. #define REQUEST_SIZE 2048
  56. char request[REQUEST_SIZE] = {0};
  57. char buf[1460] = {0};
  58. int mypipe[2]; // IPC
  59. static const char options[] = "?hV01kt:p:c:";
  60. static const struct option long_options[] = {
  61. {"help", no_argument, NULL, 'h'},
  62. {"version", no_argument, NULL, 'V'},
  63. {"time", required_argument, NULL, 't'},
  64. {"proxy", required_argument, NULL, 'p'},
  65. {"clients", required_argument, NULL, 'c'},
  66. {"http10", no_argument, NULL, '0'},
  67. {"http11", no_argument, NULL, '1'},
  68. {"keepalive", no_argument, NULL, 'k'},
  69. {"get", no_argument, &method, METHOD_GET},
  70. {"head", no_argument, &method, METHOD_HEAD},
  71. {"options", no_argument, &method, METHOD_OPTIONS},
  72. {"trace", no_argument, &method, METHOD_TRACE},
  73. {NULL, 0, NULL, 0}
  74. };
  75. void print_usage() {
  76. printf("Usage: webbench [%s] URL\n", options);
  77. puts("\n\
  78. Options:\n\
  79. -?|-h|--help Print this information.\n\
  80. -V|--version Print version.\n\
  81. -0|--http10 Use HTTP/1.0 protocol.\n\
  82. -1|--http11 Use HTTP/1.1 protocol.\n\
  83. -k|--keepalive Connection: keep-alive.\n\
  84. -t|--time <sec> Run benchmark for <sec> seconds. Default 30.\n\
  85. -p|--proxy <server:port> Use proxy server for request.\n\
  86. -c|--clients <n> Run <n> HTTP clients. Default one.\n\
  87. --get Use GET request method.\n\
  88. --head Use HEAD request method.\n\
  89. --options Use OPTIONS request method.\n\
  90. --trace Use TRACE request method.\n\
  91. ");
  92. }
  93. int parse_cmdline(int argc, char** argv) {
  94. int opt = 0;
  95. int opt_idx = 0;
  96. while ((opt=getopt_long(argc, argv, options, long_options, &opt_idx)) != EOF) {
  97. switch (opt) {
  98. case '?':
  99. case 'h': print_usage(); exit(1);
  100. case 'V': puts(VERSION); exit(1);
  101. case '0': http = 0; break;
  102. case '1': http = 1; break;
  103. case 'k': keepalive = 1; break;
  104. case 't': time = atoi(optarg); break;
  105. case 'c': clients = atoi(optarg); break;
  106. case 'p':
  107. {
  108. // host:port
  109. char* pos = strrchr(optarg, ':');
  110. proxy_host = optarg;
  111. if (pos == NULL) break;
  112. if (pos == optarg ||
  113. pos == optarg + strlen(optarg) - 1) {
  114. printf("Error option --proxy\n");
  115. return -2;
  116. }
  117. *pos = '\0';
  118. proxy_port = atoi(pos+1);
  119. }
  120. break;
  121. }
  122. }
  123. if (optind == argc) {
  124. printf("Missing URL\n");
  125. return -2;
  126. }
  127. url = argv[optind];
  128. return 0;
  129. }
  130. static void alarm_handler(int singal) {
  131. timerexpired = 1;
  132. }
  133. int main(int argc, char** argv) {
  134. if (argc == 1) {
  135. print_usage();
  136. return 2;
  137. }
  138. int ret = parse_cmdline(argc, argv);
  139. if (ret != 0) {
  140. return ret;
  141. }
  142. printf("%d clients, running %d sec\n", clients, time);
  143. // domain port url
  144. const char* req_url = "/";
  145. if (proxy_host) {
  146. strncpy(host, proxy_host, sizeof(host));
  147. port = proxy_port;
  148. } else {
  149. // http://host:port/path
  150. const char* pos1 = strstr(url, "http://");
  151. if (pos1 == NULL) {
  152. pos1 = url;
  153. } else {
  154. pos1 += strlen("http://");
  155. }
  156. const char* pos2 = strchr(pos1, '/');
  157. if (pos2 == NULL) {
  158. pos2 = url + strlen(url);
  159. } else {
  160. req_url = pos2;
  161. }
  162. int len = pos2 - pos1;
  163. char* server = (char*)malloc(len+1);
  164. memcpy(server, pos1, len);
  165. server[len] = '\0';
  166. char* pos3 = strrchr(server, ':');
  167. if (pos3 == NULL) {
  168. port = 80;
  169. } else {
  170. *pos3 = '\0';
  171. port = atoi(pos3+1);
  172. }
  173. strncpy(host, server, sizeof(host));
  174. free(server);
  175. }
  176. char Host[256];
  177. snprintf(Host, sizeof(Host), "Host: %s:%d\r\n", host, port);
  178. printf("%s", Host);
  179. // test connect
  180. int sock = Connect(host, port);
  181. if (sock < 0) {
  182. printf("Connect failed!\n");
  183. return -20;
  184. } else {
  185. printf("Connect test OK!\n");
  186. close(sock);
  187. }
  188. // build request
  189. switch (method) {
  190. case METHOD_GET:
  191. default:
  192. strcpy(request, "GET");
  193. break;
  194. case METHOD_HEAD:
  195. strcpy(request, "HEAD");
  196. break;
  197. case METHOD_OPTIONS:
  198. strcpy(request, "OPTIONS");
  199. break;
  200. case METHOD_TRACE:
  201. strcpy(request, "TRACE");
  202. break;
  203. }
  204. strcat(request, " ");
  205. strcat(request, req_url);
  206. strcat(request, " ");
  207. if (http == 0) {
  208. strcat(request, "HTTP/1.0");
  209. } else if (http == 1) {
  210. strcat(request, "HTTP/1.1");
  211. }
  212. strcat(request, "\r\n");
  213. strcat(request, "User-Agent: webbench/1.18.3.15\r\n");
  214. strcat(request, Host);
  215. strcat(request, "Cache-Control: no-cache\r\n");
  216. if (keepalive) {
  217. strcat(request, "Connection: keep-alive\r\n");
  218. }
  219. else {
  220. strcat(request, "Connection: close\r\n");
  221. }
  222. strcat(request, "\r\n");
  223. printf("%s", request);
  224. // IPC
  225. if (pipe(mypipe) < 0) {
  226. perror("pipe");
  227. exit(20);
  228. }
  229. // fork childs
  230. pid_t pid = 0;
  231. FILE* fp = NULL;
  232. int succeed = 0, failed = 0, bytes = 0;
  233. int childs = clients;
  234. for (int i = 0; i < childs; ++i) {
  235. pid = fork();
  236. if (pid < 0) {
  237. perror("fork");
  238. exit(-10);
  239. }
  240. if (pid == 0) {
  241. // child
  242. //printf("child[%d] start\n", getpid());
  243. signal(SIGALRM, alarm_handler);
  244. alarm(time);
  245. int sock = -1;
  246. int len = strlen(request);
  247. int wrbytes, rdbytes;
  248. while (1) {
  249. connect:
  250. if (timerexpired) break;
  251. if (sock == -1) {
  252. sock = Connect(host, port);
  253. }
  254. if (sock < 0) {
  255. ++failed;
  256. continue;
  257. }
  258. write:
  259. if (timerexpired) break;
  260. wrbytes = write(sock, request, len);
  261. //printf("write %d bytes\n", wrbytes);
  262. if (wrbytes != len) {
  263. ++failed;
  264. goto close;
  265. }
  266. //printf("%s\n", request);
  267. read:
  268. if (timerexpired) break;
  269. rdbytes = read(sock, buf, sizeof(buf));
  270. //printf("read %d bytes\n", rdbytes);
  271. if (rdbytes <= 0) {
  272. ++failed;
  273. goto close;
  274. }
  275. //printf("%s\n", buf);
  276. bytes += rdbytes;
  277. ++succeed;
  278. close:
  279. if (!keepalive) {
  280. close(sock);
  281. sock = -1;
  282. }
  283. }
  284. fp = fdopen(mypipe[1], "w");
  285. if (fp == NULL) {
  286. perror("fdopen");
  287. return 30;
  288. }
  289. fprintf(fp, "%d %d %d\n", succeed, failed, bytes);
  290. fclose(fp);
  291. //printf("child[%d] end\n", getpid());
  292. return 0;
  293. }
  294. }
  295. fp = fdopen(mypipe[0], "r");
  296. if (fp == NULL) {
  297. perror("fdopen");
  298. return 30;
  299. }
  300. while (1) {
  301. int i,j,k;
  302. fscanf(fp, "%d %d %d", &i, &j, &k);
  303. succeed += i;
  304. failed += j;
  305. bytes += k;
  306. if (--childs==0) break;
  307. }
  308. fclose(fp);
  309. printf("recv %d bytes/sec, %d succeed, %d failed\n",
  310. (int)(bytes)/time,
  311. succeed,
  312. failed);
  313. return 0;
  314. }