Skip to content

Commit

Permalink
add CHANGES file and simplify script init
Browse files Browse the repository at this point in the history
  • Loading branch information
wg committed Feb 21, 2015
1 parent db6da47 commit 93348e2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
master

* The wrk global variable is the only global defined by default.
* wrk.init() calls the global init(), remove calls to wrk.init().
13 changes: 7 additions & 6 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ Scripting
wrk.format returns a HTTP request string containing the passed
parameters merged with values from the wrk table.

global init -- function called when the thread is initialized
global request -- function returning the HTTP message for each request
global response -- optional function called with HTTP response data
global done -- optional function called with results of run
The following globals are optional, and if defined must be functions:

global init -- called when the thread is initialized
global request -- returning the HTTP message for each request
global response -- called with HTTP response data
global done -- called with results of run

The init() function receives any extra command line arguments for the
script. Script arguments must be separated from wrk arguments with "--"
and scripts that override init() but not request() must call wrk.init()
script which must be separated from wrk arguments with "--".

The done() function receives a table containing result data, and two
statistics objects representing the sampled per-request latency and
Expand Down
2 changes: 0 additions & 2 deletions scripts/pipeline.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
-- 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")
Expand Down
40 changes: 22 additions & 18 deletions src/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,30 @@ void script_init(lua_State *L, char *script, int argc, char **argv) {
fprintf(stderr, "%s: %s\n", script, cause);
}

lua_getglobal(L, "init");
lua_getglobal(L, "wrk");
lua_getfield(L, -1, "init");
lua_newtable(L);
for (int i = 0; i < argc; i++) {
lua_pushstring(L, argv[i]);
lua_rawseti(L, 2, i);
lua_rawseti(L, -2, i);
}
lua_call(L, 1, 0);
lua_pop(L, 1);
}

void script_request(lua_State *L, char **buf, size_t *len) {
int pop = 1;
lua_getglobal(L, "request");
if (!lua_isfunction(L, -1)) {
lua_getglobal(L, "wrk");
lua_getfield(L, -1, "request");
pop += 2;
}
lua_call(L, 0, 1);
const char *str = lua_tolstring(L, 1, len);
const char *str = lua_tolstring(L, -1, len);
*buf = realloc(*buf, *len);
memcpy(*buf, str, *len);
lua_pop(L, 1);
lua_pop(L, pop);
}

void script_response(lua_State *L, int status, buffer *headers, buffer *body) {
Expand All @@ -101,27 +109,23 @@ void script_response(lua_State *L, int status, buffer *headers, buffer *body) {
buffer_reset(body);
}

bool script_is_function(lua_State *L, char *name) {
lua_getglobal(L, name);
bool is_function = lua_isfunction(L, -1);
lua_pop(L, 1);
return is_function;
}

bool script_is_static(lua_State *L) {
lua_getglobal(L, "wrk");
lua_getfield(L, 1, "request");
lua_getglobal(L, "request");
bool is_static = lua_equal(L, 2, 3);
lua_pop(L, 3);
return is_static;
return !script_is_function(L, "request");
}

bool script_want_response(lua_State *L) {
lua_getglobal(L, "response");
bool defined = lua_type(L, 1) == LUA_TFUNCTION;
lua_pop(L, 1);
return defined;
return script_is_function(L, "response");
}

bool script_has_done(lua_State *L) {
lua_getglobal(L, "done");
bool defined = lua_type(L, 1) == LUA_TFUNCTION;
lua_pop(L, 1);
return defined;
return script_is_function(L, "done");
}

void script_header_done(lua_State *L, luaL_Buffer *buffer) {
Expand Down
17 changes: 8 additions & 9 deletions src/wrk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ function wrk.init(args)

wrk.headers["Host"] = host
end
req = wrk.format()
end

function wrk.request()
return req
end
if type(init) == "function" then
init(args)
end

init = wrk.init
request = wrk.request
response = nil
done = nil
local req = wrk.format()
wrk.request = function()
return req
end
end

return wrk

0 comments on commit 93348e2

Please sign in to comment.