webbench.c 9.8 KB

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