Skip to content

Commit

Permalink
support pipelining via script request()
Browse files Browse the repository at this point in the history
  • Loading branch information
wg committed Feb 10, 2014
1 parent ade03d2 commit 4facab7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
16 changes: 16 additions & 0 deletions scripts/pipeline.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- example script demonstrating HTTP pipelining

init = function(args)
wrk.init(args)

local r = {}
r[1] = wrk.format(nil, "/?foo")
r[2] = wrk.format(nil, "/?bar")
r[3] = wrk.format(nil, "/?baz")

req = table.concat(r)
end

request = function()
return req
end
26 changes: 15 additions & 11 deletions src/wrk.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ static struct config {
uint64_t connections;
uint64_t duration;
uint64_t timeout;
uint64_t pipeline;
bool latency;
bool dynamic;
char *script;
Expand Down Expand Up @@ -145,7 +146,7 @@ int main(int argc, char **argv) {
script_init(t->L, cfg.script, argc - optind, &argv[optind]);

if (i == 0) {
script_verify_request(t->L);
cfg.pipeline = script_verify_request(t->L);
cfg.dynamic = !script_is_static(t->L);
if (script_want_response(t->L)) {
parser_settings.on_header_field = header_field;
Expand Down Expand Up @@ -386,8 +387,6 @@ static int response_complete(http_parser *parser) {
thread->complete++;
thread->requests++;

stats_record(thread->latency, now - c->start);

if (status > 399) {
thread->errors.status++;
}
Expand All @@ -403,15 +402,17 @@ static int response_complete(http_parser *parser) {
goto done;
}

if (!http_should_keep_alive(parser)) goto reconnect;

http_parser_init(parser, HTTP_RESPONSE);
aeCreateFileEvent(thread->loop, c->fd, AE_WRITABLE, socket_writeable, c);
if (--c->pending == 0) {
stats_record(thread->latency, now - c->start);
aeCreateFileEvent(thread->loop, c->fd, AE_WRITABLE, socket_writeable, c);
}

goto done;
if (!http_should_keep_alive(parser)) {
reconnect_socket(thread, c);
goto done;
}

reconnect:
reconnect_socket(thread, c);
http_parser_init(parser, HTTP_RESPONSE);

done:
return 0;
Expand Down Expand Up @@ -478,7 +479,10 @@ static void socket_writeable(aeEventLoop *loop, int fd, void *data, int mask) {
case RETRY: return;
}

if (!c->written) c->start = time_us();
if (!c->written) {
c->start = time_us();
c->pending = cfg.pipeline;
}

c->written += n;
if (c->written == c->length) {
Expand Down
1 change: 1 addition & 0 deletions src/wrk.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ typedef struct connection {
char *request;
size_t length;
size_t written;
uint64_t pending;
buffer headers;
buffer body;
char buf[RECVBUF];
Expand Down

0 comments on commit 4facab7

Please sign in to comment.