1
0

webbench.c 9.3 KB

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