mirror of
https://github.com/wg/wrk
synced 2026-06-10 00:55:51 +08:00
Compare commits
14 Commits
@@ -1,13 +1,14 @@
|
||||
CFLAGS := -std=c99 -Wall -O2
|
||||
LIBS := -lpthread -lm
|
||||
CFLAGS := -std=c99 -Wall -O2 -D_REENTRANT
|
||||
LIBS := -lpthread -lm -lcrypto -lssl
|
||||
|
||||
TARGET := $(shell uname -s | tr [A-Z] [a-z] 2>/dev/null || echo unknown)
|
||||
|
||||
ifeq ($(TARGET), sunos)
|
||||
LIBS += -lsocket
|
||||
CFLAGS += -D_PTHREADS -D_POSIX_C_SOURCE=200112L
|
||||
LIBS += -lsocket
|
||||
endif
|
||||
|
||||
SRC := wrk.c aprintf.c stats.c units.c ae.c zmalloc.c http_parser.c tinymt64.c
|
||||
SRC := wrk.c net.c ssl.c aprintf.c stats.c units.c ae.c zmalloc.c http_parser.c tinymt64.c
|
||||
BIN := wrk
|
||||
|
||||
ODIR := obj
|
||||
|
||||
@@ -6,21 +6,21 @@ wrk - a HTTP benchmarking tool
|
||||
|
||||
Basic Usage
|
||||
|
||||
wrk -t8 -c400 -r10m http://localhost:8080/index.html
|
||||
wrk -t12 -c400 -d30s http://127.0.0.1:8080/index.html
|
||||
|
||||
This runs wrk with 8 threads, keeping 400 connections open, and making a
|
||||
total of 10 million HTTP GET requests to http://localhost:8080/index.html
|
||||
This runs a benchmark for 30 seconds, using 12 threads, and keeping
|
||||
400 HTTP connections open.
|
||||
|
||||
Output:
|
||||
|
||||
Making 10000000 requests to http://localhost:8080/index.html
|
||||
8 threads and 400 connections
|
||||
Running 30s test @ http://127.0.0.1:8080/index.html
|
||||
12 threads and 400 connections
|
||||
Thread Stats Avg Stdev Max +/- Stdev
|
||||
Latency 439.75us 350.49us 7.60ms 92.88%
|
||||
Req/Sec 61.13k 8.26k 72.00k 87.54%
|
||||
10000088 requests in 19.87s, 3.42GB read
|
||||
Requests/sec: 503396.23
|
||||
Transfer/sec: 176.16MB
|
||||
Latency 635.91us 0.89ms 12.92ms 93.69%
|
||||
Req/Sec 56.20k 8.07k 62.00k 86.54%
|
||||
22464657 requests in 30.00s, 17.76GB read
|
||||
Requests/sec: 748868.53
|
||||
Transfer/sec: 606.33MB
|
||||
|
||||
Benchmarking Tips
|
||||
|
||||
|
||||
+286
-169
@@ -37,19 +37,24 @@
|
||||
# define MIN(a,b) ((a) < (b) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#ifndef ARRAY_SIZE
|
||||
# define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
|
||||
#endif
|
||||
|
||||
#ifndef BIT_AT
|
||||
# define BIT_AT(a, i) \
|
||||
(!!((unsigned int) (a)[(unsigned int) (i) >> 3] & \
|
||||
(1 << ((unsigned int) (i) & 7))))
|
||||
#endif
|
||||
|
||||
#ifndef ELEM_AT
|
||||
# define ELEM_AT(a, i, v) ((unsigned int) (i) < ARRAY_SIZE(a) ? (a)[(i)] : (v))
|
||||
#endif
|
||||
|
||||
#if HTTP_PARSER_DEBUG
|
||||
#define SET_ERRNO(e) \
|
||||
do { \
|
||||
parser->http_errno = (e); \
|
||||
parser->error_lineno = __LINE__; \
|
||||
} while (0)
|
||||
#else
|
||||
#define SET_ERRNO(e) \
|
||||
do { \
|
||||
parser->http_errno = (e); \
|
||||
} while(0)
|
||||
#endif
|
||||
|
||||
|
||||
/* Run the notify callback FOR, returning ER if it fails */
|
||||
@@ -123,31 +128,10 @@ do { \
|
||||
|
||||
|
||||
static const char *method_strings[] =
|
||||
{ "DELETE"
|
||||
, "GET"
|
||||
, "HEAD"
|
||||
, "POST"
|
||||
, "PUT"
|
||||
, "CONNECT"
|
||||
, "OPTIONS"
|
||||
, "TRACE"
|
||||
, "COPY"
|
||||
, "LOCK"
|
||||
, "MKCOL"
|
||||
, "MOVE"
|
||||
, "PROPFIND"
|
||||
, "PROPPATCH"
|
||||
, "UNLOCK"
|
||||
, "REPORT"
|
||||
, "MKACTIVITY"
|
||||
, "CHECKOUT"
|
||||
, "MERGE"
|
||||
, "M-SEARCH"
|
||||
, "NOTIFY"
|
||||
, "SUBSCRIBE"
|
||||
, "UNSUBSCRIBE"
|
||||
, "PATCH"
|
||||
, "PURGE"
|
||||
{
|
||||
#define XX(num, name, string) #string,
|
||||
HTTP_METHOD_MAP(XX)
|
||||
#undef XX
|
||||
};
|
||||
|
||||
|
||||
@@ -205,41 +189,49 @@ static const int8_t unhex[256] =
|
||||
};
|
||||
|
||||
|
||||
static const uint8_t normal_url_char[256] = {
|
||||
/* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
/* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */
|
||||
0, 1, 1, 0, 1, 1, 1, 1,
|
||||
/* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
/* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
/* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */
|
||||
1, 1, 1, 1, 1, 1, 1, 0,
|
||||
/* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
/* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
/* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
/* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
/* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
/* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
/* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
/* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */
|
||||
1, 1, 1, 1, 1, 1, 1, 0, };
|
||||
#if HTTP_PARSER_STRICT
|
||||
# define T(v) 0
|
||||
#else
|
||||
# define T(v) v
|
||||
#endif
|
||||
|
||||
|
||||
static const uint8_t normal_url_char[32] = {
|
||||
/* 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel */
|
||||
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0,
|
||||
/* 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si */
|
||||
0 | T(2) | 0 | 0 | T(16) | 0 | 0 | 0,
|
||||
/* 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb */
|
||||
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0,
|
||||
/* 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us */
|
||||
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0,
|
||||
/* 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' */
|
||||
0 | 2 | 4 | 0 | 16 | 32 | 64 | 128,
|
||||
/* 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / */
|
||||
1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
|
||||
/* 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 */
|
||||
1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
|
||||
/* 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? */
|
||||
1 | 2 | 4 | 8 | 16 | 32 | 64 | 0,
|
||||
/* 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G */
|
||||
1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
|
||||
/* 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O */
|
||||
1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
|
||||
/* 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W */
|
||||
1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
|
||||
/* 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ */
|
||||
1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
|
||||
/* 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g */
|
||||
1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
|
||||
/* 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o */
|
||||
1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
|
||||
/* 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w */
|
||||
1 | 2 | 4 | 8 | 16 | 32 | 64 | 128,
|
||||
/* 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del */
|
||||
1 | 2 | 4 | 8 | 16 | 32 | 64 | 0, };
|
||||
|
||||
#undef T
|
||||
|
||||
enum state
|
||||
{ s_dead = 1 /* important that this is > 0 */
|
||||
|
||||
@@ -266,13 +258,9 @@ enum state
|
||||
, s_req_schema
|
||||
, s_req_schema_slash
|
||||
, s_req_schema_slash_slash
|
||||
, s_req_host_start
|
||||
, s_req_host_v6_start
|
||||
, s_req_host_v6
|
||||
, s_req_host_v6_end
|
||||
, s_req_host
|
||||
, s_req_port_start
|
||||
, s_req_port
|
||||
, s_req_server_start
|
||||
, s_req_server
|
||||
, s_req_server_with_at
|
||||
, s_req_path
|
||||
, s_req_query_string_start
|
||||
, s_req_query_string
|
||||
@@ -350,6 +338,19 @@ enum header_states
|
||||
, h_connection_close
|
||||
};
|
||||
|
||||
enum http_host_state
|
||||
{
|
||||
s_http_host_dead = 1
|
||||
, s_http_userinfo_start
|
||||
, s_http_userinfo
|
||||
, s_http_host_start
|
||||
, s_http_host_v6_start
|
||||
, s_http_host
|
||||
, s_http_host_v6
|
||||
, s_http_host_v6_end
|
||||
, s_http_host_port_start
|
||||
, s_http_host_port
|
||||
};
|
||||
|
||||
/* Macros for character classes; depends on strict-mode */
|
||||
#define CR '\r'
|
||||
@@ -359,15 +360,21 @@ enum header_states
|
||||
#define IS_NUM(c) ((c) >= '0' && (c) <= '9')
|
||||
#define IS_ALPHANUM(c) (IS_ALPHA(c) || IS_NUM(c))
|
||||
#define IS_HEX(c) (IS_NUM(c) || (LOWER(c) >= 'a' && LOWER(c) <= 'f'))
|
||||
#define IS_MARK(c) ((c) == '-' || (c) == '_' || (c) == '.' || \
|
||||
(c) == '!' || (c) == '~' || (c) == '*' || (c) == '\'' || (c) == '(' || \
|
||||
(c) == ')')
|
||||
#define IS_USERINFO_CHAR(c) (IS_ALPHANUM(c) || IS_MARK(c) || (c) == '%' || \
|
||||
(c) == ';' || (c) == ':' || (c) == '&' || (c) == '=' || (c) == '+' || \
|
||||
(c) == '$' || (c) == ',')
|
||||
|
||||
#if HTTP_PARSER_STRICT
|
||||
#define TOKEN(c) (tokens[(unsigned char)c])
|
||||
#define IS_URL_CHAR(c) (normal_url_char[(unsigned char) (c)])
|
||||
#define IS_URL_CHAR(c) (BIT_AT(normal_url_char, (unsigned char)c))
|
||||
#define IS_HOST_CHAR(c) (IS_ALPHANUM(c) || (c) == '.' || (c) == '-')
|
||||
#else
|
||||
#define TOKEN(c) ((c == ' ') ? ' ' : tokens[(unsigned char)c])
|
||||
#define IS_URL_CHAR(c) \
|
||||
(normal_url_char[(unsigned char) (c)] || ((c) & 0x80))
|
||||
(BIT_AT(normal_url_char, (unsigned char)c) || ((c) & 0x80))
|
||||
#define IS_HOST_CHAR(c) \
|
||||
(IS_ALPHANUM(c) || (c) == '.' || (c) == '-' || (c) == '_')
|
||||
#endif
|
||||
@@ -401,7 +408,7 @@ static struct {
|
||||
};
|
||||
#undef HTTP_STRERROR_GEN
|
||||
|
||||
int http_message_needs_eof(http_parser *parser);
|
||||
int http_message_needs_eof(const http_parser *parser);
|
||||
|
||||
/* Our URL parser.
|
||||
*
|
||||
@@ -417,7 +424,15 @@ int http_message_needs_eof(http_parser *parser);
|
||||
static enum state
|
||||
parse_url_char(enum state s, const char ch)
|
||||
{
|
||||
assert(!isspace(ch));
|
||||
if (ch == ' ' || ch == '\r' || ch == '\n') {
|
||||
return s_dead;
|
||||
}
|
||||
|
||||
#if HTTP_PARSER_STRICT
|
||||
if (ch == '\t' || ch == '\f') {
|
||||
return s_dead;
|
||||
}
|
||||
#endif
|
||||
|
||||
switch (s) {
|
||||
case s_req_spaces_before_url:
|
||||
@@ -455,67 +470,33 @@ parse_url_char(enum state s, const char ch)
|
||||
|
||||
case s_req_schema_slash_slash:
|
||||
if (ch == '/') {
|
||||
return s_req_host_start;
|
||||
return s_req_server_start;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case s_req_host_start:
|
||||
if (ch == '[') {
|
||||
return s_req_host_v6_start;
|
||||
case s_req_server_with_at:
|
||||
if (ch == '@') {
|
||||
return s_dead;
|
||||
}
|
||||
|
||||
if (IS_HOST_CHAR(ch)) {
|
||||
return s_req_host;
|
||||
/* FALLTHROUGH */
|
||||
case s_req_server_start:
|
||||
case s_req_server:
|
||||
if (ch == '/') {
|
||||
return s_req_path;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case s_req_host:
|
||||
if (IS_HOST_CHAR(ch)) {
|
||||
return s_req_host;
|
||||
if (ch == '?') {
|
||||
return s_req_query_string_start;
|
||||
}
|
||||
|
||||
/* FALLTHROUGH */
|
||||
case s_req_host_v6_end:
|
||||
switch (ch) {
|
||||
case ':':
|
||||
return s_req_port_start;
|
||||
|
||||
case '/':
|
||||
return s_req_path;
|
||||
|
||||
case '?':
|
||||
return s_req_query_string_start;
|
||||
if (ch == '@') {
|
||||
return s_req_server_with_at;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case s_req_host_v6:
|
||||
if (ch == ']') {
|
||||
return s_req_host_v6_end;
|
||||
}
|
||||
|
||||
/* FALLTHROUGH */
|
||||
case s_req_host_v6_start:
|
||||
if (IS_HEX(ch) || ch == ':') {
|
||||
return s_req_host_v6;
|
||||
}
|
||||
break;
|
||||
|
||||
case s_req_port:
|
||||
switch (ch) {
|
||||
case '/':
|
||||
return s_req_path;
|
||||
|
||||
case '?':
|
||||
return s_req_query_string_start;
|
||||
}
|
||||
|
||||
/* FALLTHROUGH */
|
||||
case s_req_port_start:
|
||||
if (IS_NUM(ch)) {
|
||||
return s_req_port;
|
||||
if (IS_USERINFO_CHAR(ch) || ch == '[' || ch == ']') {
|
||||
return s_req_server;
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -637,13 +618,9 @@ size_t http_parser_execute (http_parser *parser,
|
||||
case s_req_schema:
|
||||
case s_req_schema_slash:
|
||||
case s_req_schema_slash_slash:
|
||||
case s_req_host_start:
|
||||
case s_req_host_v6_start:
|
||||
case s_req_host_v6:
|
||||
case s_req_host_v6_end:
|
||||
case s_req_host:
|
||||
case s_req_port_start:
|
||||
case s_req_port:
|
||||
case s_req_server_start:
|
||||
case s_req_server:
|
||||
case s_req_server_with_at:
|
||||
case s_req_query_string_start:
|
||||
case s_req_query_string:
|
||||
case s_req_fragment_start:
|
||||
@@ -889,6 +866,7 @@ size_t http_parser_execute (http_parser *parser,
|
||||
case s_res_line_almost_done:
|
||||
STRICT_CHECK(ch != LF);
|
||||
parser->state = s_header_field_start;
|
||||
CALLBACK_NOTIFY(status_complete);
|
||||
break;
|
||||
|
||||
case s_start_req:
|
||||
@@ -918,7 +896,7 @@ size_t http_parser_execute (http_parser *parser,
|
||||
/* or PROPFIND|PROPPATCH|PUT|PATCH|PURGE */
|
||||
break;
|
||||
case 'R': parser->method = HTTP_REPORT; break;
|
||||
case 'S': parser->method = HTTP_SUBSCRIBE; break;
|
||||
case 'S': parser->method = HTTP_SUBSCRIBE; /* or SEARCH */ break;
|
||||
case 'T': parser->method = HTTP_TRACE; break;
|
||||
case 'U': parser->method = HTTP_UNLOCK; /* or UNSUBSCRIBE */ break;
|
||||
default:
|
||||
@@ -965,6 +943,12 @@ size_t http_parser_execute (http_parser *parser,
|
||||
} else {
|
||||
goto error;
|
||||
}
|
||||
} else if (parser->method == HTTP_SUBSCRIBE) {
|
||||
if (parser->index == 1 && ch == 'E') {
|
||||
parser->method = HTTP_SEARCH;
|
||||
} else {
|
||||
goto error;
|
||||
}
|
||||
} else if (parser->index == 1 && parser->method == HTTP_POST) {
|
||||
if (ch == 'R') {
|
||||
parser->method = HTTP_PROPFIND; /* or HTTP_PROPPATCH */
|
||||
@@ -998,7 +982,7 @@ size_t http_parser_execute (http_parser *parser,
|
||||
|
||||
MARK(url);
|
||||
if (parser->method == HTTP_CONNECT) {
|
||||
parser->state = s_req_host_start;
|
||||
parser->state = s_req_server_start;
|
||||
}
|
||||
|
||||
parser->state = parse_url_char((enum state)parser->state, ch);
|
||||
@@ -1013,10 +997,7 @@ size_t http_parser_execute (http_parser *parser,
|
||||
case s_req_schema:
|
||||
case s_req_schema_slash:
|
||||
case s_req_schema_slash_slash:
|
||||
case s_req_host_start:
|
||||
case s_req_host_v6_start:
|
||||
case s_req_host_v6:
|
||||
case s_req_port_start:
|
||||
case s_req_server_start:
|
||||
{
|
||||
switch (ch) {
|
||||
/* No whitespace allowed here */
|
||||
@@ -1036,9 +1017,8 @@ size_t http_parser_execute (http_parser *parser,
|
||||
break;
|
||||
}
|
||||
|
||||
case s_req_host:
|
||||
case s_req_host_v6_end:
|
||||
case s_req_port:
|
||||
case s_req_server:
|
||||
case s_req_server_with_at:
|
||||
case s_req_path:
|
||||
case s_req_query_string_start:
|
||||
case s_req_query_string:
|
||||
@@ -1867,7 +1847,7 @@ error:
|
||||
|
||||
/* Does the parser need to see an EOF to find the end of the message? */
|
||||
int
|
||||
http_message_needs_eof (http_parser *parser)
|
||||
http_message_needs_eof (const http_parser *parser)
|
||||
{
|
||||
if (parser->type == HTTP_REQUEST) {
|
||||
return 0;
|
||||
@@ -1890,7 +1870,7 @@ http_message_needs_eof (http_parser *parser)
|
||||
|
||||
|
||||
int
|
||||
http_should_keep_alive (http_parser *parser)
|
||||
http_should_keep_alive (const http_parser *parser)
|
||||
{
|
||||
if (parser->http_major > 0 && parser->http_minor > 0) {
|
||||
/* HTTP/1.1 */
|
||||
@@ -1908,9 +1888,10 @@ http_should_keep_alive (http_parser *parser)
|
||||
}
|
||||
|
||||
|
||||
const char * http_method_str (enum http_method m)
|
||||
const char *
|
||||
http_method_str (enum http_method m)
|
||||
{
|
||||
return method_strings[m];
|
||||
return ELEM_AT(method_strings, m, "<unknown>");
|
||||
}
|
||||
|
||||
|
||||
@@ -1937,6 +1918,144 @@ http_errno_description(enum http_errno err) {
|
||||
return http_strerror_tab[err].description;
|
||||
}
|
||||
|
||||
static enum http_host_state
|
||||
http_parse_host_char(enum http_host_state s, const char ch) {
|
||||
switch(s) {
|
||||
case s_http_userinfo:
|
||||
case s_http_userinfo_start:
|
||||
if (ch == '@') {
|
||||
return s_http_host_start;
|
||||
}
|
||||
|
||||
if (IS_USERINFO_CHAR(ch)) {
|
||||
return s_http_userinfo;
|
||||
}
|
||||
break;
|
||||
|
||||
case s_http_host_start:
|
||||
if (ch == '[') {
|
||||
return s_http_host_v6_start;
|
||||
}
|
||||
|
||||
if (IS_HOST_CHAR(ch)) {
|
||||
return s_http_host;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case s_http_host:
|
||||
if (IS_HOST_CHAR(ch)) {
|
||||
return s_http_host;
|
||||
}
|
||||
|
||||
/* FALLTHROUGH */
|
||||
case s_http_host_v6_end:
|
||||
if (ch == ':') {
|
||||
return s_http_host_port_start;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case s_http_host_v6:
|
||||
if (ch == ']') {
|
||||
return s_http_host_v6_end;
|
||||
}
|
||||
|
||||
/* FALLTHROUGH */
|
||||
case s_http_host_v6_start:
|
||||
if (IS_HEX(ch) || ch == ':' || ch == '.') {
|
||||
return s_http_host_v6;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case s_http_host_port:
|
||||
case s_http_host_port_start:
|
||||
if (IS_NUM(ch)) {
|
||||
return s_http_host_port;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return s_http_host_dead;
|
||||
}
|
||||
|
||||
static int
|
||||
http_parse_host(const char * buf, struct http_parser_url *u, int found_at) {
|
||||
enum http_host_state s;
|
||||
|
||||
const char *p;
|
||||
size_t buflen = u->field_data[UF_HOST].off + u->field_data[UF_HOST].len;
|
||||
|
||||
u->field_data[UF_HOST].len = 0;
|
||||
|
||||
s = found_at ? s_http_userinfo_start : s_http_host_start;
|
||||
|
||||
for (p = buf + u->field_data[UF_HOST].off; p < buf + buflen; p++) {
|
||||
enum http_host_state new_s = http_parse_host_char(s, *p);
|
||||
|
||||
if (new_s == s_http_host_dead) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
switch(new_s) {
|
||||
case s_http_host:
|
||||
if (s != s_http_host) {
|
||||
u->field_data[UF_HOST].off = p - buf;
|
||||
}
|
||||
u->field_data[UF_HOST].len++;
|
||||
break;
|
||||
|
||||
case s_http_host_v6:
|
||||
if (s != s_http_host_v6) {
|
||||
u->field_data[UF_HOST].off = p - buf;
|
||||
}
|
||||
u->field_data[UF_HOST].len++;
|
||||
break;
|
||||
|
||||
case s_http_host_port:
|
||||
if (s != s_http_host_port) {
|
||||
u->field_data[UF_PORT].off = p - buf;
|
||||
u->field_data[UF_PORT].len = 0;
|
||||
u->field_set |= (1 << UF_PORT);
|
||||
}
|
||||
u->field_data[UF_PORT].len++;
|
||||
break;
|
||||
|
||||
case s_http_userinfo:
|
||||
if (s != s_http_userinfo) {
|
||||
u->field_data[UF_USERINFO].off = p - buf ;
|
||||
u->field_data[UF_USERINFO].len = 0;
|
||||
u->field_set |= (1 << UF_USERINFO);
|
||||
}
|
||||
u->field_data[UF_USERINFO].len++;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
s = new_s;
|
||||
}
|
||||
|
||||
/* Make sure we don't end somewhere unexpected */
|
||||
switch (s) {
|
||||
case s_http_host_start:
|
||||
case s_http_host_v6_start:
|
||||
case s_http_host_v6:
|
||||
case s_http_host_port_start:
|
||||
case s_http_userinfo:
|
||||
case s_http_userinfo_start:
|
||||
return 1;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
|
||||
struct http_parser_url *u)
|
||||
@@ -1944,9 +2063,10 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
|
||||
enum state s;
|
||||
const char *p;
|
||||
enum http_parser_url_fields uf, old_uf;
|
||||
int found_at = 0;
|
||||
|
||||
u->port = u->field_set = 0;
|
||||
s = is_connect ? s_req_host_start : s_req_spaces_before_url;
|
||||
s = is_connect ? s_req_server_start : s_req_spaces_before_url;
|
||||
uf = old_uf = UF_MAX;
|
||||
|
||||
for (p = buf; p < buf + buflen; p++) {
|
||||
@@ -1960,10 +2080,7 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
|
||||
/* Skip delimeters */
|
||||
case s_req_schema_slash:
|
||||
case s_req_schema_slash_slash:
|
||||
case s_req_host_start:
|
||||
case s_req_host_v6_start:
|
||||
case s_req_host_v6_end:
|
||||
case s_req_port_start:
|
||||
case s_req_server_start:
|
||||
case s_req_query_string_start:
|
||||
case s_req_fragment_start:
|
||||
continue;
|
||||
@@ -1972,13 +2089,12 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
|
||||
uf = UF_SCHEMA;
|
||||
break;
|
||||
|
||||
case s_req_host:
|
||||
case s_req_host_v6:
|
||||
uf = UF_HOST;
|
||||
break;
|
||||
case s_req_server_with_at:
|
||||
found_at = 1;
|
||||
|
||||
case s_req_port:
|
||||
uf = UF_PORT;
|
||||
/* FALLTROUGH */
|
||||
case s_req_server:
|
||||
uf = UF_HOST;
|
||||
break;
|
||||
|
||||
case s_req_path:
|
||||
@@ -2011,23 +2127,19 @@ http_parser_parse_url(const char *buf, size_t buflen, int is_connect,
|
||||
old_uf = uf;
|
||||
}
|
||||
|
||||
/* host must be present if there is a schema */
|
||||
/* parsing http:///toto will fail */
|
||||
if ((u->field_set & ((1 << UF_SCHEMA) | (1 << UF_HOST))) != 0) {
|
||||
if (http_parse_host(buf, u, found_at) != 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* CONNECT requests can only contain "hostname:port" */
|
||||
if (is_connect && u->field_set != ((1 << UF_HOST)|(1 << UF_PORT))) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Make sure we don't end somewhere unexpected */
|
||||
switch (s) {
|
||||
case s_req_host_v6_start:
|
||||
case s_req_host_v6:
|
||||
case s_req_host_v6_end:
|
||||
case s_req_host:
|
||||
case s_req_port_start:
|
||||
return 1;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (u->field_set & (1 << UF_PORT)) {
|
||||
/* Don't bother with endp; we've already validated the string */
|
||||
unsigned long v = strtoul(buf + u->field_data[UF_PORT].off, NULL, 10);
|
||||
@@ -2056,3 +2168,8 @@ http_parser_pause(http_parser *parser, int paused) {
|
||||
assert(0 && "Attempting to pause parser in error state");
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
http_body_is_final(const struct http_parser *parser) {
|
||||
return parser->state == s_message_done;
|
||||
}
|
||||
|
||||
+48
-61
@@ -24,11 +24,13 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define HTTP_PARSER_VERSION_MAJOR 1
|
||||
#define HTTP_PARSER_VERSION_MINOR 0
|
||||
#define HTTP_PARSER_VERSION_MAJOR 2
|
||||
#define HTTP_PARSER_VERSION_MINOR 1
|
||||
|
||||
#include <sys/types.h>
|
||||
#if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
|
||||
#include <BaseTsd.h>
|
||||
#include <stddef.h>
|
||||
typedef __int8 int8_t;
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef __int16 int16_t;
|
||||
@@ -37,9 +39,6 @@ typedef __int32 int32_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef __int64 int64_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
|
||||
typedef unsigned int size_t;
|
||||
typedef int ssize_t;
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
@@ -51,14 +50,6 @@ typedef int ssize_t;
|
||||
# define HTTP_PARSER_STRICT 1
|
||||
#endif
|
||||
|
||||
/* Compile with -DHTTP_PARSER_DEBUG=1 to add extra debugging information to
|
||||
* the error reporting facility.
|
||||
*/
|
||||
#ifndef HTTP_PARSER_DEBUG
|
||||
# define HTTP_PARSER_DEBUG 0
|
||||
#endif
|
||||
|
||||
|
||||
/* Maximium header size allowed */
|
||||
#define HTTP_MAX_HEADER_SIZE (80*1024)
|
||||
|
||||
@@ -77,7 +68,7 @@ typedef struct http_parser_settings http_parser_settings;
|
||||
* chunked' headers that indicate the presence of a body.
|
||||
*
|
||||
* http_data_cb does not return data chunks. It will be call arbitrarally
|
||||
* many times for each string. E.G. you might get 10 callbacks for "on_path"
|
||||
* many times for each string. E.G. you might get 10 callbacks for "on_url"
|
||||
* each providing just a few characters more data.
|
||||
*/
|
||||
typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
|
||||
@@ -85,43 +76,44 @@ typedef int (*http_cb) (http_parser*);
|
||||
|
||||
|
||||
/* Request Methods */
|
||||
#define HTTP_METHOD_MAP(XX) \
|
||||
XX(0, DELETE) \
|
||||
XX(1, GET) \
|
||||
XX(2, HEAD) \
|
||||
XX(3, POST) \
|
||||
XX(4, PUT) \
|
||||
/* pathological */ \
|
||||
XX(5, CONNECT) \
|
||||
XX(6, OPTIONS) \
|
||||
XX(7, TRACE) \
|
||||
/* webdav */ \
|
||||
XX(8, COPY) \
|
||||
XX(9, LOCK) \
|
||||
XX(10, MKCOL) \
|
||||
XX(11, MOVE) \
|
||||
XX(12, PROPFIND) \
|
||||
XX(13, PROPPATCH) \
|
||||
XX(14, UNLOCK) \
|
||||
/* subversion */ \
|
||||
XX(15, REPORT) \
|
||||
XX(16, MKACTIVITY) \
|
||||
XX(17, CHECKOUT) \
|
||||
XX(18, MERGE) \
|
||||
/* upnp */ \
|
||||
XX(19, MSEARCH) \
|
||||
XX(20, NOTIFY) \
|
||||
XX(21, SUBSCRIBE) \
|
||||
XX(22, UNSUBSCRIBE) \
|
||||
/* RFC-5789 */ \
|
||||
XX(23, PATCH) \
|
||||
XX(24, PURGE) \
|
||||
#define HTTP_METHOD_MAP(XX) \
|
||||
XX(0, DELETE, DELETE) \
|
||||
XX(1, GET, GET) \
|
||||
XX(2, HEAD, HEAD) \
|
||||
XX(3, POST, POST) \
|
||||
XX(4, PUT, PUT) \
|
||||
/* pathological */ \
|
||||
XX(5, CONNECT, CONNECT) \
|
||||
XX(6, OPTIONS, OPTIONS) \
|
||||
XX(7, TRACE, TRACE) \
|
||||
/* webdav */ \
|
||||
XX(8, COPY, COPY) \
|
||||
XX(9, LOCK, LOCK) \
|
||||
XX(10, MKCOL, MKCOL) \
|
||||
XX(11, MOVE, MOVE) \
|
||||
XX(12, PROPFIND, PROPFIND) \
|
||||
XX(13, PROPPATCH, PROPPATCH) \
|
||||
XX(14, SEARCH, SEARCH) \
|
||||
XX(15, UNLOCK, UNLOCK) \
|
||||
/* subversion */ \
|
||||
XX(16, REPORT, REPORT) \
|
||||
XX(17, MKACTIVITY, MKACTIVITY) \
|
||||
XX(18, CHECKOUT, CHECKOUT) \
|
||||
XX(19, MERGE, MERGE) \
|
||||
/* upnp */ \
|
||||
XX(20, MSEARCH, M-SEARCH) \
|
||||
XX(21, NOTIFY, NOTIFY) \
|
||||
XX(22, SUBSCRIBE, SUBSCRIBE) \
|
||||
XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \
|
||||
/* RFC-5789 */ \
|
||||
XX(24, PATCH, PATCH) \
|
||||
XX(25, PURGE, PURGE) \
|
||||
|
||||
enum http_method
|
||||
{
|
||||
#define XX(num, name) HTTP_##name = num,
|
||||
#define XX(num, name, string) HTTP_##name = num,
|
||||
HTTP_METHOD_MAP(XX)
|
||||
#undef X
|
||||
#undef XX
|
||||
};
|
||||
|
||||
|
||||
@@ -149,6 +141,7 @@ enum flags
|
||||
\
|
||||
/* Callback-related errors */ \
|
||||
XX(CB_message_begin, "the on_message_begin callback failed") \
|
||||
XX(CB_status_complete, "the on_status_complete callback failed") \
|
||||
XX(CB_url, "the on_url callback failed") \
|
||||
XX(CB_header_field, "the on_header_field callback failed") \
|
||||
XX(CB_header_value, "the on_header_value callback failed") \
|
||||
@@ -195,13 +188,6 @@ enum http_errno {
|
||||
/* Get an http_errno value from an http_parser */
|
||||
#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
|
||||
|
||||
/* Get the line number that generated the current error */
|
||||
#if HTTP_PARSER_DEBUG
|
||||
#define HTTP_PARSER_ERRNO_LINE(p) ((p)->error_lineno)
|
||||
#else
|
||||
#define HTTP_PARSER_ERRNO_LINE(p) 0
|
||||
#endif
|
||||
|
||||
|
||||
struct http_parser {
|
||||
/** PRIVATE **/
|
||||
@@ -228,10 +214,6 @@ struct http_parser {
|
||||
*/
|
||||
unsigned char upgrade : 1;
|
||||
|
||||
#if HTTP_PARSER_DEBUG
|
||||
uint32_t error_lineno;
|
||||
#endif
|
||||
|
||||
/** PUBLIC **/
|
||||
void *data; /* A pointer to get hook to the "connection" or "socket" object */
|
||||
};
|
||||
@@ -240,6 +222,7 @@ struct http_parser {
|
||||
struct http_parser_settings {
|
||||
http_cb on_message_begin;
|
||||
http_data_cb on_url;
|
||||
http_cb on_status_complete;
|
||||
http_data_cb on_header_field;
|
||||
http_data_cb on_header_value;
|
||||
http_cb on_headers_complete;
|
||||
@@ -255,7 +238,8 @@ enum http_parser_url_fields
|
||||
, UF_PATH = 3
|
||||
, UF_QUERY = 4
|
||||
, UF_FRAGMENT = 5
|
||||
, UF_MAX = 6
|
||||
, UF_USERINFO = 6
|
||||
, UF_MAX = 7
|
||||
};
|
||||
|
||||
|
||||
@@ -287,12 +271,12 @@ size_t http_parser_execute(http_parser *parser,
|
||||
|
||||
|
||||
/* If http_should_keep_alive() in the on_headers_complete or
|
||||
* on_message_complete callback returns true, then this will be should be
|
||||
* on_message_complete callback returns 0, then this should be
|
||||
* the last message on the connection.
|
||||
* If you are the server, respond with the "Connection: close" header.
|
||||
* If you are the client, close the connection.
|
||||
*/
|
||||
int http_should_keep_alive(http_parser *parser);
|
||||
int http_should_keep_alive(const http_parser *parser);
|
||||
|
||||
/* Returns a string version of the HTTP method. */
|
||||
const char *http_method_str(enum http_method m);
|
||||
@@ -311,6 +295,9 @@ int http_parser_parse_url(const char *buf, size_t buflen,
|
||||
/* Pause or un-pause the parser; a nonzero value pauses */
|
||||
void http_parser_pause(http_parser *parser, int paused);
|
||||
|
||||
/* Checks if this is the final chunk of the body. */
|
||||
int http_body_is_final(const http_parser *parser);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <getopt.h>
|
||||
#include <math.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/uio.h>
|
||||
|
||||
#include "ssl.h"
|
||||
#include "aprintf.h"
|
||||
#include "stats.h"
|
||||
#include "units.h"
|
||||
#include "zmalloc.h"
|
||||
|
||||
struct config;
|
||||
|
||||
static void *thread_main(void *);
|
||||
static int connect_socket(thread *, connection *);
|
||||
static int reconnect_socket(thread *, connection *);
|
||||
|
||||
static int calibrate(aeEventLoop *, long long, void *);
|
||||
static int sample_rate(aeEventLoop *, long long, void *);
|
||||
static int check_timeouts(aeEventLoop *, long long, void *);
|
||||
|
||||
static void socket_connected(aeEventLoop *, int, void *, int);
|
||||
static void socket_writeable(aeEventLoop *, int, void *, int);
|
||||
static void socket_readable(aeEventLoop *, int, void *, int);
|
||||
static int request_complete(http_parser *);
|
||||
|
||||
static uint64_t time_us();
|
||||
|
||||
static char *extract_url_part(char *, struct http_parser_url *, enum http_parser_url_fields);
|
||||
static char *format_request(char *, char *, char *, char **);
|
||||
|
||||
static int parse_args(struct config *, char **, char **, int, char **);
|
||||
static void print_stats_header();
|
||||
static void print_stats(char *, stats *, char *(*)(long double));
|
||||
static void print_stats_latency(stats *);
|
||||
|
||||
#endif /* MAIN_H */
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright (C) 2013 - Will Glozer. All rights reserved.
|
||||
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "net.h"
|
||||
|
||||
status sock_connect(connection *c) {
|
||||
return OK;
|
||||
}
|
||||
|
||||
status sock_close(connection *c) {
|
||||
return OK;
|
||||
}
|
||||
|
||||
status sock_read(connection *c, size_t *n) {
|
||||
ssize_t r = read(c->fd, c->buf, sizeof(c->buf));
|
||||
*n = (size_t) r;
|
||||
return r > 0 ? OK : ERROR;
|
||||
}
|
||||
|
||||
status sock_write(connection *c, char *buf, size_t len, size_t *n) {
|
||||
ssize_t r;
|
||||
if ((r = write(c->fd, buf, len)) == -1) {
|
||||
switch (errno) {
|
||||
case EAGAIN: return RETRY;
|
||||
default: return ERROR;
|
||||
}
|
||||
}
|
||||
*n = (size_t) r;
|
||||
return OK;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef NET_H
|
||||
#define NET_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
#include "wrk.h"
|
||||
|
||||
typedef enum {
|
||||
OK,
|
||||
ERROR,
|
||||
RETRY
|
||||
} status;
|
||||
|
||||
struct sock {
|
||||
status (*connect)(connection *);
|
||||
status ( *close)(connection *);
|
||||
status ( *read)(connection *, size_t *);
|
||||
status ( *write)(connection *, char *, size_t, size_t *);
|
||||
};
|
||||
|
||||
status sock_connect(connection *);
|
||||
status sock_close(connection *);
|
||||
status sock_read(connection *, size_t *);
|
||||
status sock_write(connection *, char *, size_t, size_t *);
|
||||
|
||||
#endif /* NET_H */
|
||||
@@ -0,0 +1,95 @@
|
||||
// Copyright (C) 2013 - Will Glozer. All rights reserved.
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
#include "ssl.h"
|
||||
|
||||
static pthread_mutex_t *locks;
|
||||
|
||||
static void ssl_lock(int mode, int n, const char *file, int line) {
|
||||
pthread_mutex_t *lock = &locks[n];
|
||||
if (mode & CRYPTO_LOCK) {
|
||||
pthread_mutex_lock(lock);
|
||||
} else {
|
||||
pthread_mutex_unlock(lock);
|
||||
}
|
||||
}
|
||||
|
||||
static unsigned long ssl_id() {
|
||||
return (unsigned long) pthread_self();
|
||||
}
|
||||
|
||||
SSL_CTX *ssl_init() {
|
||||
SSL_CTX *ctx = NULL;
|
||||
|
||||
SSL_load_error_strings();
|
||||
SSL_library_init();
|
||||
OpenSSL_add_all_algorithms();
|
||||
|
||||
if ((locks = calloc(CRYPTO_num_locks(), sizeof(pthread_mutex_t)))) {
|
||||
for (int i = 0; i < CRYPTO_num_locks(); i++) {
|
||||
pthread_mutex_init(&locks[i], NULL);
|
||||
}
|
||||
|
||||
CRYPTO_set_locking_callback(ssl_lock);
|
||||
CRYPTO_set_id_callback(ssl_id);
|
||||
|
||||
if ((ctx = SSL_CTX_new(TLSv1_client_method()))) {
|
||||
SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
|
||||
SSL_CTX_set_verify_depth(ctx, 0);
|
||||
SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
|
||||
SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT);
|
||||
}
|
||||
}
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
status ssl_connect(connection *c) {
|
||||
int r;
|
||||
SSL_set_fd(c->ssl, c->fd);
|
||||
if ((r = SSL_connect(c->ssl)) != 1) {
|
||||
switch (SSL_get_error(c->ssl, r)) {
|
||||
case SSL_ERROR_WANT_READ: return RETRY;
|
||||
case SSL_ERROR_WANT_WRITE: return RETRY;
|
||||
default: return ERROR;
|
||||
}
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
status ssl_close(connection *c) {
|
||||
SSL_shutdown(c->ssl);
|
||||
SSL_clear(c->ssl);
|
||||
return OK;
|
||||
}
|
||||
|
||||
status ssl_read(connection *c, size_t *n) {
|
||||
int r;
|
||||
if ((r = SSL_read(c->ssl, c->buf, sizeof(c->buf))) <= 0) {
|
||||
switch (SSL_get_error(c->ssl, r)) {
|
||||
case SSL_ERROR_WANT_READ: return RETRY;
|
||||
case SSL_ERROR_WANT_WRITE: return RETRY;
|
||||
default: return ERROR;
|
||||
}
|
||||
}
|
||||
*n = (size_t) r;
|
||||
return OK;
|
||||
}
|
||||
|
||||
status ssl_write(connection *c, char *buf, size_t len, size_t *n) {
|
||||
int r;
|
||||
if ((r = SSL_write(c->ssl, buf, len)) <= 0) {
|
||||
switch (SSL_get_error(c->ssl, r)) {
|
||||
case SSL_ERROR_WANT_READ: return RETRY;
|
||||
case SSL_ERROR_WANT_WRITE: return RETRY;
|
||||
default: return ERROR;
|
||||
}
|
||||
}
|
||||
*n = (size_t) r;
|
||||
return OK;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef SSL_H
|
||||
#define SSL_H
|
||||
|
||||
#include "net.h"
|
||||
|
||||
SSL_CTX *ssl_init();
|
||||
|
||||
status ssl_connect(connection *);
|
||||
status ssl_close(connection *);
|
||||
status ssl_read(connection *, size_t *);
|
||||
status ssl_write(connection *, char *, size_t, size_t *);
|
||||
|
||||
#endif /* SSL_H */
|
||||
+42
-20
@@ -8,42 +8,43 @@
|
||||
#include "zmalloc.h"
|
||||
|
||||
stats *stats_alloc(uint64_t samples) {
|
||||
stats *stats = zcalloc(sizeof(stats) + sizeof(uint64_t) * samples);
|
||||
stats->samples = samples;
|
||||
return stats;
|
||||
stats *s = zcalloc(sizeof(stats) + sizeof(uint64_t) * samples);
|
||||
s->samples = samples;
|
||||
s->min = UINT64_MAX;
|
||||
return s;
|
||||
}
|
||||
|
||||
void stats_free(stats *stats) {
|
||||
zfree(stats);
|
||||
}
|
||||
|
||||
void stats_reset(stats *stats) {
|
||||
stats->limit = 0;
|
||||
stats->index = 0;
|
||||
stats->min = UINT64_MAX;
|
||||
stats->max = 0;
|
||||
}
|
||||
|
||||
void stats_record(stats *stats, uint64_t x) {
|
||||
stats->data[stats->index++] = x;
|
||||
if (x < stats->min) stats->min = x;
|
||||
if (x > stats->max) stats->max = x;
|
||||
if (stats->limit < stats->samples) stats->limit++;
|
||||
if (stats->index == stats->samples) stats->index = 0;
|
||||
}
|
||||
|
||||
uint64_t stats_min(stats *stats) {
|
||||
uint64_t min = 0;
|
||||
for (uint64_t i = 0; i < stats->limit; i++) {
|
||||
uint64_t x = stats->data[i];
|
||||
if (x < min || min == 0) min = x;
|
||||
}
|
||||
return min;
|
||||
static int stats_compare(const void *a, const void *b) {
|
||||
uint64_t *x = (uint64_t *) a;
|
||||
uint64_t *y = (uint64_t *) b;
|
||||
return *x - *y;
|
||||
}
|
||||
|
||||
uint64_t stats_max(stats *stats) {
|
||||
uint64_t max = 0;
|
||||
for (uint64_t i = 0; i < stats->limit; i++) {
|
||||
uint64_t x = stats->data[i];
|
||||
if (x > max || max == 0) max = x;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
long double stats_summarize(stats *stats) {
|
||||
qsort(stats->data, stats->limit, sizeof(uint64_t), &stats_compare);
|
||||
|
||||
long double stats_mean(stats *stats) {
|
||||
uint64_t sum = 0;
|
||||
if (stats->limit == 0) return 0.0;
|
||||
|
||||
uint64_t sum = 0;
|
||||
for (uint64_t i = 0; i < stats->limit; i++) {
|
||||
sum += stats->data[i];
|
||||
}
|
||||
@@ -71,3 +72,24 @@ long double stats_within_stdev(stats *stats, long double mean, long double stdev
|
||||
|
||||
return (sum / (long double) stats->limit) * 100;
|
||||
}
|
||||
|
||||
uint64_t stats_percentile(stats *stats, long double p) {
|
||||
uint64_t rank = round((p / 100.0) * stats->limit + 0.5);
|
||||
return stats->data[rank - 1];
|
||||
}
|
||||
|
||||
void stats_sample(stats *dst, tinymt64_t *state, uint64_t count, stats *src) {
|
||||
for (uint64_t i = 0; i < count; i++) {
|
||||
uint64_t n = rand64(state, src->limit);
|
||||
stats_record(dst, src->data[n]);
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t rand64(tinymt64_t *state, uint64_t n) {
|
||||
uint64_t x, max = ~UINT64_C(0);
|
||||
max -= max % n;
|
||||
do {
|
||||
x = tinymt64_generate_uint64(state);
|
||||
} while (x >= max);
|
||||
return x % n;
|
||||
}
|
||||
|
||||
+16
-4
@@ -1,21 +1,33 @@
|
||||
#ifndef STATS_H
|
||||
#define STATS_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "tinymt64.h"
|
||||
|
||||
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
|
||||
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
|
||||
|
||||
typedef struct {
|
||||
uint64_t samples;
|
||||
uint64_t index;
|
||||
uint64_t limit;
|
||||
uint64_t min;
|
||||
uint64_t max;
|
||||
uint64_t data[];
|
||||
} stats;
|
||||
|
||||
stats *stats_alloc(uint64_t);
|
||||
void stats_free(stats *);
|
||||
void stats_reset(stats *);
|
||||
|
||||
void stats_record(stats *, uint64_t);
|
||||
uint64_t stats_min(stats *);
|
||||
uint64_t stats_max(stats *);
|
||||
long double stats_mean(stats *);
|
||||
|
||||
long double stats_summarize(stats *);
|
||||
long double stats_stdev(stats *stats, long double);
|
||||
long double stats_within_stdev(stats *, long double, long double, uint64_t);
|
||||
uint64_t stats_percentile(stats *, long double);
|
||||
|
||||
void stats_sample(stats *, tinymt64_t *, uint64_t, stats *);
|
||||
uint64_t rand64(tinymt64_t *, uint64_t);
|
||||
|
||||
#endif /* STATS_H */
|
||||
|
||||
|
||||
+10
-2
@@ -62,10 +62,10 @@ static int scan_units(char *s, uint64_t *n, units *m) {
|
||||
|
||||
if ((c = sscanf(s, "%"SCNu64"%2s", &base, unit)) < 1) return -1;
|
||||
|
||||
if (c == 2) {
|
||||
if (c == 2 && strncasecmp(unit, m->base, 3)) {
|
||||
for (i = 0; m->units[i] != NULL; i++) {
|
||||
scale *= m->scale;
|
||||
if (!strncasecmp(unit, m->units[i], sizeof(unit))) break;
|
||||
if (!strncasecmp(unit, m->units[i], 3)) break;
|
||||
}
|
||||
if (m->units[i] == NULL) return -1;
|
||||
}
|
||||
@@ -91,6 +91,14 @@ char *format_time_us(long double n) {
|
||||
return format_units(n, units, 2);
|
||||
}
|
||||
|
||||
char *format_time_s(long double n) {
|
||||
return format_units(n, &time_units_s, 0);
|
||||
}
|
||||
|
||||
int scan_metric(char *s, uint64_t *n) {
|
||||
return scan_units(s, n, &metric_units);
|
||||
}
|
||||
|
||||
int scan_time(char *s, uint64_t *n) {
|
||||
return scan_units(s, n, &time_units_s);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
char *format_binary(long double);
|
||||
char *format_metric(long double);
|
||||
char *format_time_us(long double);
|
||||
char *format_time_s(long double);
|
||||
|
||||
int scan_metric(char *, uint64_t *);
|
||||
int scan_time(char *, uint64_t *);
|
||||
|
||||
#endif /* UNITS_H */
|
||||
|
||||
@@ -1,44 +1,24 @@
|
||||
// Copyright (C) 2012 - Will Glozer. All rights reserved.
|
||||
|
||||
#include "wrk.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <getopt.h>
|
||||
#include <math.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/uio.h>
|
||||
|
||||
#include "aprintf.h"
|
||||
#include "stats.h"
|
||||
#include "units.h"
|
||||
#include "zmalloc.h"
|
||||
#include "tinymt64.h"
|
||||
#include "main.h"
|
||||
|
||||
static struct config {
|
||||
struct addrinfo addr;
|
||||
uint64_t threads;
|
||||
uint64_t connections;
|
||||
uint64_t requests;
|
||||
uint64_t duration;
|
||||
uint64_t timeout;
|
||||
bool latency;
|
||||
SSL_CTX *ctx;
|
||||
} cfg;
|
||||
|
||||
static struct {
|
||||
char *method;
|
||||
char *body;
|
||||
size_t size;
|
||||
char *buf;
|
||||
} request;
|
||||
} req;
|
||||
|
||||
static struct {
|
||||
stats *latency;
|
||||
@@ -46,21 +26,39 @@ static struct {
|
||||
pthread_mutex_t mutex;
|
||||
} statistics;
|
||||
|
||||
static struct sock sock = {
|
||||
.connect = sock_connect,
|
||||
.close = sock_close,
|
||||
.read = sock_read,
|
||||
.write = sock_write
|
||||
};
|
||||
|
||||
static const struct http_parser_settings parser_settings = {
|
||||
.on_message_complete = request_complete
|
||||
};
|
||||
|
||||
static volatile sig_atomic_t stop = 0;
|
||||
|
||||
static void handler(int sig) {
|
||||
stop = 1;
|
||||
}
|
||||
|
||||
static void usage() {
|
||||
printf("Usage: wrk <options> <url> \n"
|
||||
" Options: \n"
|
||||
" -c, --connections <n> Connections to keep open \n"
|
||||
" -r, --requests <n> Total requests to make \n"
|
||||
" -t, --threads <n> Number of threads to use \n"
|
||||
" -c, --connections <N> Connections to keep open \n"
|
||||
" -d, --duration <T> Duration of test \n"
|
||||
" -t, --threads <N> Number of threads to use \n"
|
||||
" \n"
|
||||
" -H, --header <h> Add header to request \n"
|
||||
" -H, --header <H> Add header to request \n"
|
||||
" -M, --method <M> HTTP method \n"
|
||||
" --body <B> Request body \n"
|
||||
" --latency Print latency statistics \n"
|
||||
" --timeout <T> Socket/request timeout \n"
|
||||
" -v, --version Print version details \n"
|
||||
" \n"
|
||||
" Numeric arguments may include a SI unit (2k, 2M, 2G)\n");
|
||||
" Numeric arguments may include a SI unit (1k, 1M, 1G)\n"
|
||||
" Time arguments may include a time unit (2s, 2m, 2h)\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
@@ -81,10 +79,11 @@ int main(int argc, char **argv) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char *host = extract_url_part(url, &parser_url, UF_HOST);
|
||||
char *port = extract_url_part(url, &parser_url, UF_PORT);
|
||||
char *service = port ? port : extract_url_part(url, &parser_url, UF_SCHEMA);
|
||||
char *path = "/";
|
||||
char *schema = extract_url_part(url, &parser_url, UF_SCHEMA);
|
||||
char *host = extract_url_part(url, &parser_url, UF_HOST);
|
||||
char *port = extract_url_part(url, &parser_url, UF_PORT);
|
||||
char *service = port ? port : schema;
|
||||
char *path = "/";
|
||||
|
||||
if (parser_url.field_set & (1 << UF_PATH)) {
|
||||
path = &url[parser_url.field_data[UF_PATH].off];
|
||||
@@ -104,14 +103,9 @@ int main(int argc, char **argv) {
|
||||
for (addr = addrs; addr != NULL; addr = addr->ai_next) {
|
||||
int fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
|
||||
if (fd == -1) continue;
|
||||
if (connect(fd, addr->ai_addr, addr->ai_addrlen) == -1) {
|
||||
if (errno == EHOSTUNREACH || errno == ECONNREFUSED) {
|
||||
close(fd);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
rc = connect(fd, addr->ai_addr, addr->ai_addrlen);
|
||||
close(fd);
|
||||
break;
|
||||
if (rc == 0) break;
|
||||
}
|
||||
|
||||
if (addr == NULL) {
|
||||
@@ -120,10 +114,23 @@ int main(int argc, char **argv) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!strncmp("https", schema, 5)) {
|
||||
if ((cfg.ctx = ssl_init()) == NULL) {
|
||||
fprintf(stderr, "unable to initialize SSL\n");
|
||||
ERR_print_errors_fp(stderr);
|
||||
exit(1);
|
||||
}
|
||||
sock.connect = ssl_connect;
|
||||
sock.close = ssl_close;
|
||||
sock.read = ssl_read;
|
||||
sock.write = ssl_write;
|
||||
}
|
||||
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
cfg.addr = *addr;
|
||||
request.buf = format_request(host, port, path, headers);
|
||||
request.size = strlen(request.buf);
|
||||
signal(SIGINT, SIG_IGN);
|
||||
cfg.addr = *addr;
|
||||
req.buf = format_request(host, port, path, headers);
|
||||
req.size = strlen(req.buf);
|
||||
|
||||
pthread_mutex_init(&statistics.mutex, NULL);
|
||||
statistics.latency = stats_alloc(SAMPLES);
|
||||
@@ -131,12 +138,12 @@ int main(int argc, char **argv) {
|
||||
|
||||
thread *threads = zcalloc(cfg.threads * sizeof(thread));
|
||||
uint64_t connections = cfg.connections / cfg.threads;
|
||||
uint64_t requests = cfg.requests / cfg.threads;
|
||||
uint64_t stop_at = time_us() + (cfg.duration * 1000000);
|
||||
|
||||
for (uint64_t i = 0; i < cfg.threads; i++) {
|
||||
thread *t = &threads[i];
|
||||
t->connections = connections;
|
||||
t->requests = requests;
|
||||
t->stop_at = stop_at;
|
||||
|
||||
if (pthread_create(&t->thread, NULL, &thread_main, t)) {
|
||||
char *msg = strerror(errno);
|
||||
@@ -145,7 +152,15 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
}
|
||||
|
||||
printf("Making %"PRIu64" requests to %s\n", cfg.requests, url);
|
||||
struct sigaction sa = {
|
||||
.sa_handler = handler,
|
||||
.sa_flags = 0,
|
||||
};
|
||||
sigfillset(&sa.sa_mask);
|
||||
sigaction(SIGINT, &sa, NULL);
|
||||
|
||||
char *time = format_time_s(cfg.duration);
|
||||
printf("Running %s test @ %s\n", time, url);
|
||||
printf(" %"PRIu64" threads and %"PRIu64" connections\n", cfg.threads, cfg.connections);
|
||||
|
||||
uint64_t start = time_us();
|
||||
@@ -175,6 +190,7 @@ int main(int argc, char **argv) {
|
||||
print_stats_header();
|
||||
print_stats("Latency", statistics.latency, format_time_us);
|
||||
print_stats("Req/Sec", statistics.requests, format_metric);
|
||||
if (cfg.latency) print_stats_latency(statistics.latency);
|
||||
|
||||
char *runtime_msg = format_time_us(runtime_us);
|
||||
|
||||
@@ -201,16 +217,17 @@ void *thread_main(void *arg) {
|
||||
thread->cs = zmalloc(thread->connections * sizeof(connection));
|
||||
thread->loop = loop;
|
||||
tinymt64_init(&thread->rand, time_us());
|
||||
thread->latency = stats_alloc(100000);
|
||||
|
||||
connection *c = thread->cs;
|
||||
|
||||
for (uint64_t i = 0; i < thread->connections; i++, c++) {
|
||||
c->thread = thread;
|
||||
c->latency = 0;
|
||||
c->thread = thread;
|
||||
c->ssl = cfg.ctx ? SSL_new(cfg.ctx) : NULL;
|
||||
connect_socket(thread, c);
|
||||
}
|
||||
|
||||
aeCreateTimeEvent(loop, SAMPLE_INTERVAL_MS, sample_rate, thread, NULL);
|
||||
aeCreateTimeEvent(loop, CALIBRATE_DELAY_MS, calibrate, thread, NULL);
|
||||
aeCreateTimeEvent(loop, TIMEOUT_INTERVAL_MS, check_timeouts, thread, NULL);
|
||||
|
||||
thread->start = time_us();
|
||||
@@ -219,6 +236,15 @@ void *thread_main(void *arg) {
|
||||
aeDeleteEventLoop(loop);
|
||||
zfree(thread->cs);
|
||||
|
||||
uint64_t max = thread->latency->max;
|
||||
stats_free(thread->latency);
|
||||
|
||||
pthread_mutex_lock(&statistics.mutex);
|
||||
for (uint64_t i = 0; i < thread->missed; i++) {
|
||||
stats_record(statistics.latency, max);
|
||||
}
|
||||
pthread_mutex_unlock(&statistics.mutex);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -233,70 +259,95 @@ static int connect_socket(thread *thread, connection *c) {
|
||||
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
|
||||
if (connect(fd, addr.ai_addr, addr.ai_addrlen) == -1) {
|
||||
if (errno != EINPROGRESS) {
|
||||
thread->errors.connect++;
|
||||
goto error;
|
||||
}
|
||||
if (errno != EINPROGRESS) goto error;
|
||||
}
|
||||
|
||||
flags = 1;
|
||||
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags));
|
||||
|
||||
if (aeCreateFileEvent(loop, fd, AE_WRITABLE, socket_writeable, c) != AE_OK) {
|
||||
goto error;
|
||||
flags = AE_READABLE | AE_WRITABLE;
|
||||
if (aeCreateFileEvent(loop, fd, flags, socket_connected, c) == AE_OK) {
|
||||
c->parser.data = c;
|
||||
c->fd = fd;
|
||||
return fd;
|
||||
}
|
||||
|
||||
http_parser_init(&c->parser, HTTP_RESPONSE);
|
||||
c->parser.data = c;
|
||||
c->fd = fd;
|
||||
|
||||
return fd;
|
||||
|
||||
error:
|
||||
thread->errors.connect++;
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int reconnect_socket(thread *thread, connection *c) {
|
||||
aeDeleteFileEvent(thread->loop, c->fd, AE_WRITABLE | AE_READABLE);
|
||||
sock.close(c);
|
||||
close(c->fd);
|
||||
return connect_socket(thread, c);
|
||||
}
|
||||
|
||||
static int calibrate(aeEventLoop *loop, long long id, void *data) {
|
||||
thread *thread = data;
|
||||
|
||||
uint64_t elapsed_ms = (time_us() - thread->start) / 1000;
|
||||
uint64_t req_per_ms = thread->requests / elapsed_ms;
|
||||
|
||||
if (!req_per_ms) return CALIBRATE_DELAY_MS / 2;
|
||||
|
||||
thread->rate = (req_per_ms * SAMPLE_INTERVAL_MS) / 10;
|
||||
thread->start = time_us();
|
||||
thread->requests = 0;
|
||||
stats_reset(thread->latency);
|
||||
|
||||
aeCreateTimeEvent(loop, SAMPLE_INTERVAL_MS, sample_rate, thread, NULL);
|
||||
|
||||
return AE_NOMORE;
|
||||
}
|
||||
|
||||
static int sample_rate(aeEventLoop *loop, long long id, void *data) {
|
||||
thread *thread = data;
|
||||
|
||||
uint64_t n = rand64(&thread->rand, thread->connections);
|
||||
uint64_t elapsed_ms = (time_us() - thread->start) / 1000;
|
||||
connection *c = thread->cs + n;
|
||||
uint64_t requests = (thread->complete / elapsed_ms) * 1000;
|
||||
uint64_t requests = (thread->requests / elapsed_ms) * 1000;
|
||||
uint64_t missed = thread->rate - MIN(thread->rate, thread->latency->limit);
|
||||
uint64_t count = thread->rate - missed;
|
||||
|
||||
pthread_mutex_lock(&statistics.mutex);
|
||||
stats_record(statistics.latency, c->latency);
|
||||
stats_sample(statistics.latency, &thread->rand, count, thread->latency);
|
||||
stats_record(statistics.requests, requests);
|
||||
pthread_mutex_unlock(&statistics.mutex);
|
||||
|
||||
return SAMPLE_INTERVAL_MS + rand64(&thread->rand, SAMPLE_INTERVAL_MS);
|
||||
uint64_t max = thread->latency->max;
|
||||
thread->missed += missed;
|
||||
thread->requests = 0;
|
||||
thread->start = time_us();
|
||||
stats_reset(thread->latency);
|
||||
thread->latency->max = max;
|
||||
|
||||
return SAMPLE_INTERVAL_MS;
|
||||
}
|
||||
|
||||
static int request_complete(http_parser *parser) {
|
||||
connection *c = parser->data;
|
||||
thread *thread = c->thread;
|
||||
uint64_t now = time_us();
|
||||
|
||||
thread->complete++;
|
||||
thread->requests++;
|
||||
|
||||
stats_record(thread->latency, now - c->start);
|
||||
|
||||
if (parser->status_code > 399) {
|
||||
thread->errors.status++;
|
||||
}
|
||||
|
||||
if (++thread->complete >= thread->requests) {
|
||||
if (now >= thread->stop_at) {
|
||||
aeStop(thread->loop);
|
||||
goto done;
|
||||
}
|
||||
|
||||
c->latency = time_us() - c->start;
|
||||
if (!http_should_keep_alive(parser)) goto reconnect;
|
||||
|
||||
http_parser_init(parser, HTTP_RESPONSE);
|
||||
aeDeleteFileEvent(thread->loop, c->fd, AE_READABLE);
|
||||
aeCreateFileEvent(thread->loop, c->fd, AE_WRITABLE, socket_writeable, c);
|
||||
|
||||
goto done;
|
||||
@@ -310,9 +361,10 @@ static int request_complete(http_parser *parser) {
|
||||
|
||||
static int check_timeouts(aeEventLoop *loop, long long id, void *data) {
|
||||
thread *thread = data;
|
||||
connection *c = thread->cs;
|
||||
connection *c = thread->cs;
|
||||
uint64_t now = time_us();
|
||||
|
||||
uint64_t maxAge = time_us() - (cfg.timeout * 1000);
|
||||
uint64_t maxAge = now - (cfg.timeout * 1000);
|
||||
|
||||
for (uint64_t i = 0; i < thread->connections; i++, c++) {
|
||||
if (maxAge > c->start) {
|
||||
@@ -320,16 +372,54 @@ static int check_timeouts(aeEventLoop *loop, long long id, void *data) {
|
||||
}
|
||||
}
|
||||
|
||||
if (stop || now >= thread->stop_at) {
|
||||
aeStop(loop);
|
||||
}
|
||||
|
||||
return TIMEOUT_INTERVAL_MS;
|
||||
}
|
||||
|
||||
static void socket_connected(aeEventLoop *loop, int fd, void *data, int mask) {
|
||||
connection *c = data;
|
||||
|
||||
switch (sock.connect(c)) {
|
||||
case OK: break;
|
||||
case ERROR: goto error;
|
||||
case RETRY: return;
|
||||
}
|
||||
|
||||
http_parser_init(&c->parser, HTTP_RESPONSE);
|
||||
c->written = 0;
|
||||
|
||||
aeCreateFileEvent(c->thread->loop, c->fd, AE_READABLE, socket_readable, c);
|
||||
aeCreateFileEvent(c->thread->loop, c->fd, AE_WRITABLE, socket_writeable, c);
|
||||
|
||||
return;
|
||||
|
||||
error:
|
||||
c->thread->errors.connect++;
|
||||
reconnect_socket(c->thread, c);
|
||||
|
||||
}
|
||||
|
||||
static void socket_writeable(aeEventLoop *loop, int fd, void *data, int mask) {
|
||||
connection *c = data;
|
||||
size_t len = req.size - c->written;
|
||||
size_t n;
|
||||
|
||||
if (write(fd, request.buf, request.size) < request.size) goto error;
|
||||
c->start = time_us();
|
||||
aeDeleteFileEvent(loop, fd, AE_WRITABLE);
|
||||
aeCreateFileEvent(loop, fd, AE_READABLE, socket_readable, c);
|
||||
switch (sock.write(c, req.buf + c->written, len, &n)) {
|
||||
case OK: break;
|
||||
case ERROR: goto error;
|
||||
case RETRY: return;
|
||||
}
|
||||
|
||||
if (!c->written) c->start = time_us();
|
||||
|
||||
c->written += n;
|
||||
if (c->written == req.size) {
|
||||
c->written = 0;
|
||||
aeDeleteFileEvent(loop, fd, AE_WRITABLE);
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
@@ -338,11 +428,17 @@ static void socket_writeable(aeEventLoop *loop, int fd, void *data, int mask) {
|
||||
reconnect_socket(c->thread, c);
|
||||
}
|
||||
|
||||
|
||||
static void socket_readable(aeEventLoop *loop, int fd, void *data, int mask) {
|
||||
connection *c = data;
|
||||
ssize_t n;
|
||||
size_t n;
|
||||
|
||||
switch (sock.read(c, &n)) {
|
||||
case OK: break;
|
||||
case ERROR: goto error;
|
||||
case RETRY: return;
|
||||
}
|
||||
|
||||
if ((n = read(fd, c->buf, sizeof(c->buf))) <= 0) goto error;
|
||||
if (http_parser_execute(&c->parser, &parser_settings, c->buf, n) != n) goto error;
|
||||
c->thread->bytes += n;
|
||||
|
||||
@@ -359,15 +455,6 @@ static uint64_t time_us() {
|
||||
return (t.tv_sec * 1000000) + t.tv_usec;
|
||||
}
|
||||
|
||||
static uint64_t rand64(tinymt64_t *state, uint64_t n) {
|
||||
uint64_t x, max = ~UINT64_C(0);
|
||||
max -= max % n;
|
||||
do {
|
||||
x = tinymt64_generate_uint64(state);
|
||||
} while (x >= max);
|
||||
return x % n;
|
||||
}
|
||||
|
||||
static char *extract_url_part(char *url, struct http_parser_url *parser_url, enum http_parser_url_fields field) {
|
||||
char *part = NULL;
|
||||
|
||||
@@ -382,26 +469,43 @@ static char *extract_url_part(char *url, struct http_parser_url *parser_url, enu
|
||||
}
|
||||
|
||||
static char *format_request(char *host, char *port, char *path, char **headers) {
|
||||
char *req = NULL;
|
||||
|
||||
aprintf(&req, "GET %s HTTP/1.1\r\n", path);
|
||||
aprintf(&req, "Host: %s", host);
|
||||
if (port) aprintf(&req, ":%s", port);
|
||||
aprintf(&req, "\r\n");
|
||||
char *buf = NULL;
|
||||
char *head = NULL;
|
||||
|
||||
for (char **h = headers; *h != NULL; h++) {
|
||||
aprintf(&req, "%s\r\n", *h);
|
||||
aprintf(&head, "%s\r\n", *h);
|
||||
if (!strncasecmp(*h, "Host:", 5)) {
|
||||
host = NULL;
|
||||
port = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
aprintf(&req, "\r\n");
|
||||
return req;
|
||||
if (req.body) {
|
||||
size_t len = strlen(req.body);
|
||||
aprintf(&head, "Content-Length: %zd\r\n", len);
|
||||
}
|
||||
|
||||
aprintf(&buf, "%s %s HTTP/1.1\r\n", req.method, path);
|
||||
if (host) aprintf(&buf, "Host: %s", host);
|
||||
if (port) aprintf(&buf, ":%s", port);
|
||||
if (host) aprintf(&buf, "\r\n");
|
||||
|
||||
aprintf(&buf, "%s\r\n", head ? head : "");
|
||||
aprintf(&buf, "%s", req.body ? req.body : "");
|
||||
|
||||
free(head);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static struct option longopts[] = {
|
||||
{ "connections", required_argument, NULL, 'c' },
|
||||
{ "requests", required_argument, NULL, 'r' },
|
||||
{ "duration", required_argument, NULL, 'd' },
|
||||
{ "threads", required_argument, NULL, 't' },
|
||||
{ "header", required_argument, NULL, 'H' },
|
||||
{ "method", required_argument, NULL, 'M' },
|
||||
{ "body", required_argument, NULL, 'B' },
|
||||
{ "latency", no_argument, NULL, 'L' },
|
||||
{ "timeout", required_argument, NULL, 'T' },
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
{ "version", no_argument, NULL, 'v' },
|
||||
{ NULL, 0, NULL, 0 }
|
||||
@@ -413,10 +517,11 @@ static int parse_args(struct config *cfg, char **url, char **headers, int argc,
|
||||
memset(cfg, 0, sizeof(struct config));
|
||||
cfg->threads = 2;
|
||||
cfg->connections = 10;
|
||||
cfg->requests = 100;
|
||||
cfg->duration = 10;
|
||||
cfg->timeout = SOCKET_TIMEOUT_MS;
|
||||
req.method = "GET";
|
||||
|
||||
while ((c = getopt_long(argc, argv, "t:c:r:H:v?", longopts, NULL)) != -1) {
|
||||
while ((c = getopt_long(argc, argv, "t:c:d:H:M:B:T:Lrv?", longopts, NULL)) != -1) {
|
||||
switch (c) {
|
||||
case 't':
|
||||
if (scan_metric(optarg, &cfg->threads)) return -1;
|
||||
@@ -424,16 +529,31 @@ static int parse_args(struct config *cfg, char **url, char **headers, int argc,
|
||||
case 'c':
|
||||
if (scan_metric(optarg, &cfg->connections)) return -1;
|
||||
break;
|
||||
case 'r':
|
||||
if (scan_metric(optarg, &cfg->requests)) return -1;
|
||||
case 'd':
|
||||
if (scan_time(optarg, &cfg->duration)) return -1;
|
||||
break;
|
||||
case 'H':
|
||||
*header++ = optarg;
|
||||
break;
|
||||
case 'M':
|
||||
req.method = optarg;
|
||||
break;
|
||||
case 'B':
|
||||
req.body = optarg;
|
||||
break;
|
||||
case 'L':
|
||||
cfg->latency = true;
|
||||
break;
|
||||
case 'T':
|
||||
if (scan_time(optarg, &cfg->timeout)) return -1;
|
||||
cfg->timeout *= 1000;
|
||||
break;
|
||||
case 'v':
|
||||
printf("wrk %s [%s] ", VERSION, aeGetApiName());
|
||||
printf("Copyright (C) 2012 Will Glozer\n");
|
||||
break;
|
||||
case 'r':
|
||||
fprintf(stderr, "wrk 2.0.0+ uses -d instead of -r\n");
|
||||
case 'h':
|
||||
case '?':
|
||||
case ':':
|
||||
@@ -442,7 +562,7 @@ static int parse_args(struct config *cfg, char **url, char **headers, int argc,
|
||||
}
|
||||
}
|
||||
|
||||
if (optind == argc || !cfg->threads || !cfg->requests) return -1;
|
||||
if (optind == argc || !cfg->threads || !cfg->duration) return -1;
|
||||
|
||||
if (!cfg->connections || cfg->connections < cfg->threads) {
|
||||
fprintf(stderr, "number of connections must be >= threads\n");
|
||||
@@ -473,8 +593,8 @@ static void print_units(long double n, char *(*fmt)(long double), int width) {
|
||||
}
|
||||
|
||||
static void print_stats(char *name, stats *stats, char *(*fmt)(long double)) {
|
||||
long double mean = stats_mean(stats);
|
||||
long double max = stats_max(stats);
|
||||
uint64_t max = stats->max;
|
||||
long double mean = stats_summarize(stats);
|
||||
long double stdev = stats_stdev(stats, mean);
|
||||
|
||||
printf(" %-10s", name);
|
||||
@@ -483,3 +603,15 @@ static void print_stats(char *name, stats *stats, char *(*fmt)(long double)) {
|
||||
print_units(max, fmt, 9);
|
||||
printf("%8.2Lf%%\n", stats_within_stdev(stats, mean, stdev, 1));
|
||||
}
|
||||
|
||||
static void print_stats_latency(stats *stats) {
|
||||
long double percentiles[] = { 50.0, 75.0, 90.0, 99.0 };
|
||||
printf(" Latency Distribution\n");
|
||||
for (size_t i = 0; i < sizeof(percentiles) / sizeof(long double); i++) {
|
||||
long double p = percentiles[i];
|
||||
uint64_t n = stats_percentile(stats, p);
|
||||
printf("%7.0Lf%%", p);
|
||||
print_units(n, format_time_us, 10);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,17 +6,20 @@
|
||||
#include <inttypes.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
#include "stats.h"
|
||||
#include "ae.h"
|
||||
#include "http_parser.h"
|
||||
#include "tinymt64.h"
|
||||
|
||||
#define VERSION "1.1.0"
|
||||
#define VERSION "2.2.0"
|
||||
#define RECVBUF 8192
|
||||
#define SAMPLES 100000
|
||||
#define SAMPLES 100000000
|
||||
|
||||
#define SOCKET_TIMEOUT_MS 2000
|
||||
#define SAMPLE_INTERVAL_MS 100
|
||||
#define SAMPLE_INTERVAL_MS 10
|
||||
#define CALIBRATE_DELAY_MS 500
|
||||
#define TIMEOUT_INTERVAL_MS 2000
|
||||
|
||||
typedef struct {
|
||||
@@ -31,10 +34,14 @@ typedef struct {
|
||||
pthread_t thread;
|
||||
aeEventLoop *loop;
|
||||
uint64_t connections;
|
||||
uint64_t requests;
|
||||
uint64_t stop_at;
|
||||
uint64_t complete;
|
||||
uint64_t requests;
|
||||
uint64_t bytes;
|
||||
uint64_t start;
|
||||
uint64_t rate;
|
||||
uint64_t missed;
|
||||
stats *latency;
|
||||
tinymt64_t rand;
|
||||
errors errors;
|
||||
struct connection *cs;
|
||||
@@ -44,32 +51,10 @@ typedef struct connection {
|
||||
thread *thread;
|
||||
http_parser parser;
|
||||
int fd;
|
||||
SSL *ssl;
|
||||
uint64_t start;
|
||||
uint64_t latency;
|
||||
size_t written;
|
||||
char buf[RECVBUF];
|
||||
} connection;
|
||||
|
||||
struct config;
|
||||
|
||||
static void *thread_main(void *);
|
||||
static int connect_socket(thread *, connection *);
|
||||
static int reconnect_socket(thread *, connection *);
|
||||
|
||||
static int sample_rate(aeEventLoop *, long long, void *);
|
||||
static int check_timeouts(aeEventLoop *, long long, void *);
|
||||
|
||||
static void socket_writeable(aeEventLoop *, int, void *, int);
|
||||
static void socket_readable(aeEventLoop *, int, void *, int);
|
||||
static int request_complete(http_parser *);
|
||||
|
||||
static uint64_t time_us();
|
||||
static uint64_t rand64(tinymt64_t *, uint64_t);
|
||||
|
||||
static char *extract_url_part(char *, struct http_parser_url *, enum http_parser_url_fields);
|
||||
static char *format_request(char *, char *, char *, char **);
|
||||
|
||||
static int parse_args(struct config *, char **, char **, int, char **);
|
||||
static void print_stats_header();
|
||||
static void print_stats(char *, stats *, char *(*)(long double));
|
||||
|
||||
#endif /* WRK_H */
|
||||
|
||||
Reference in New Issue
Block a user