1
0
mirror of https://github.com/wg/wrk synced 2025-01-23 04:02:59 +08:00

add CHANGES file and simplify script init

This commit is contained in:
Will 2015-02-07 10:35:21 +09:00
parent db6da47fe3
commit 93348e2814
5 changed files with 41 additions and 35 deletions

4
CHANGES Normal file
View File

@ -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
README
View File

@ -50,14 +50,15 @@ Scripting
wrk.format returns a HTTP request string containing the passed wrk.format returns a HTTP request string containing the passed
parameters merged with values from the wrk table. parameters merged with values from the wrk table.
global init -- function called when the thread is initialized The following globals are optional, and if defined must be functions:
global request -- function returning the HTTP message for each request
global response -- optional function called with HTTP response data global init -- called when the thread is initialized
global done -- optional function called with results of run 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 The init() function receives any extra command line arguments for the
script. Script arguments must be separated from wrk arguments with "--" script which must be separated from wrk arguments with "--".
and scripts that override init() but not request() must call wrk.init()
The done() function receives a table containing result data, and two The done() function receives a table containing result data, and two
statistics objects representing the sampled per-request latency and statistics objects representing the sampled per-request latency and

View File

@ -1,8 +1,6 @@
-- example script demonstrating HTTP pipelining -- example script demonstrating HTTP pipelining
init = function(args) init = function(args)
wrk.init(args)
local r = {} local r = {}
r[1] = wrk.format(nil, "/?foo") r[1] = wrk.format(nil, "/?foo")
r[2] = wrk.format(nil, "/?bar") r[2] = wrk.format(nil, "/?bar")

View File

@ -65,22 +65,30 @@ void script_init(lua_State *L, char *script, int argc, char **argv) {
fprintf(stderr, "%s: %s\n", script, cause); fprintf(stderr, "%s: %s\n", script, cause);
} }
lua_getglobal(L, "init"); lua_getglobal(L, "wrk");
lua_getfield(L, -1, "init");
lua_newtable(L); lua_newtable(L);
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
lua_pushstring(L, argv[i]); lua_pushstring(L, argv[i]);
lua_rawseti(L, 2, i); lua_rawseti(L, -2, i);
} }
lua_call(L, 1, 0); lua_call(L, 1, 0);
lua_pop(L, 1);
} }
void script_request(lua_State *L, char **buf, size_t *len) { void script_request(lua_State *L, char **buf, size_t *len) {
int pop = 1;
lua_getglobal(L, "request"); 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); 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); *buf = realloc(*buf, *len);
memcpy(*buf, str, *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) { void script_response(lua_State *L, int status, buffer *headers, buffer *body) {
@ -101,27 +109,23 @@ void script_response(lua_State *L, int status, buffer *headers, buffer *body) {
buffer_reset(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) { bool script_is_static(lua_State *L) {
lua_getglobal(L, "wrk"); return !script_is_function(L, "request");
lua_getfield(L, 1, "request");
lua_getglobal(L, "request");
bool is_static = lua_equal(L, 2, 3);
lua_pop(L, 3);
return is_static;
} }
bool script_want_response(lua_State *L) { bool script_want_response(lua_State *L) {
lua_getglobal(L, "response"); return script_is_function(L, "response");
bool defined = lua_type(L, 1) == LUA_TFUNCTION;
lua_pop(L, 1);
return defined;
} }
bool script_has_done(lua_State *L) { bool script_has_done(lua_State *L) {
lua_getglobal(L, "done"); return script_is_function(L, "done");
bool defined = lua_type(L, 1) == LUA_TFUNCTION;
lua_pop(L, 1);
return defined;
} }
void script_header_done(lua_State *L, luaL_Buffer *buffer) { void script_header_done(lua_State *L, luaL_Buffer *buffer) {

View File

@ -42,16 +42,15 @@ function wrk.init(args)
wrk.headers["Host"] = host wrk.headers["Host"] = host
end end
req = wrk.format()
end
function wrk.request() if type(init) == "function" then
return req init(args)
end end
init = wrk.init local req = wrk.format()
request = wrk.request wrk.request = function()
response = nil return req
done = nil end
end
return wrk return wrk