1
0
mirror of https://github.com/wg/wrk synced 2025-01-06 21:32:54 +08:00
This commit is contained in:
Vladimir Chebotarev 2024-08-30 22:44:27 +00:00 committed by GitHub
commit ec1bf32ff2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 25 additions and 7 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
obj/ obj/
wrk wrc

View File

@ -19,7 +19,7 @@ endif
SRC := wrk.c net.c ssl.c aprintf.c stats.c script.c units.c \ SRC := wrk.c net.c ssl.c aprintf.c stats.c script.c units.c \
ae.c zmalloc.c http_parser.c ae.c zmalloc.c http_parser.c
BIN := wrk BIN := wrc
VER ?= $(shell git describe --tags --always --dirty) VER ?= $(shell git describe --tags --always --dirty)
ODIR := obj ODIR := obj

View File

@ -45,7 +45,7 @@ static const struct luaL_Reg threadlib[] = {
{ NULL, NULL } { NULL, NULL }
}; };
lua_State *script_create(char *file, char *url, char **headers) { lua_State *script_create(char *file, char *url, char *method, char *data, char **headers) {
lua_State *L = luaL_newstate(); lua_State *L = luaL_newstate();
luaL_openlibs(L); luaL_openlibs(L);
(void) luaL_dostring(L, "wrk = require \"wrk\""); (void) luaL_dostring(L, "wrk = require \"wrk\"");
@ -77,6 +77,10 @@ lua_State *script_create(char *file, char *url, char **headers) {
set_field(L, 4, "scheme", push_url_part(L, url, &parts, UF_SCHEMA)); set_field(L, 4, "scheme", push_url_part(L, url, &parts, UF_SCHEMA));
set_field(L, 4, "host", push_url_part(L, url, &parts, UF_HOST)); set_field(L, 4, "host", push_url_part(L, url, &parts, UF_HOST));
set_field(L, 4, "port", push_url_part(L, url, &parts, UF_PORT)); set_field(L, 4, "port", push_url_part(L, url, &parts, UF_PORT));
lua_pushstring(L, method);
set_field(L, 4, "method", 0);
lua_pushstring(L, data);
set_field(L, 4, "body", 0);
set_fields(L, 4, fields); set_fields(L, 4, fields);
lua_getfield(L, 4, "headers"); lua_getfield(L, 4, "headers");

View File

@ -9,7 +9,7 @@
#include "stats.h" #include "stats.h"
#include "wrk.h" #include "wrk.h"
lua_State *script_create(char *, char *, char **); lua_State *script_create(char *, char *, char *, char *, char **);
bool script_resolve(lua_State *, char *, char *); bool script_resolve(lua_State *, char *, char *);
void script_setup(lua_State *, thread *); void script_setup(lua_State *, thread *);

View File

@ -14,6 +14,8 @@ static struct config {
bool dynamic; bool dynamic;
bool latency; bool latency;
char *host; char *host;
char *method;
char *data;
char *script; char *script;
SSL_CTX *ctx; SSL_CTX *ctx;
} cfg; } cfg;
@ -49,7 +51,9 @@ static void usage() {
" -t, --threads <N> Number of threads to use \n" " -t, --threads <N> Number of threads to use \n"
" \n" " \n"
" -s, --script <S> Load Lua script file \n" " -s, --script <S> Load Lua script file \n"
" -X, --method <S> HTTP method (default: GET) \n"
" -H, --header <H> Add header to request \n" " -H, --header <H> Add header to request \n"
" --data <S> Body \n"
" --latency Print latency statistics \n" " --latency Print latency statistics \n"
" --timeout <T> Socket/request timeout \n" " --timeout <T> Socket/request timeout \n"
" -v, --version Print version details \n" " -v, --version Print version details \n"
@ -92,7 +96,7 @@ int main(int argc, char **argv) {
statistics.requests = stats_alloc(MAX_THREAD_RATE_S); statistics.requests = stats_alloc(MAX_THREAD_RATE_S);
thread *threads = zcalloc(cfg.threads * sizeof(thread)); thread *threads = zcalloc(cfg.threads * sizeof(thread));
lua_State *L = script_create(cfg.script, url, headers); lua_State *L = script_create(cfg.script, url, cfg.method, cfg.data, headers);
if (!script_resolve(L, host, service)) { if (!script_resolve(L, host, service)) {
char *msg = strerror(errno); char *msg = strerror(errno);
fprintf(stderr, "unable to connect to %s:%s %s\n", host, service, msg); fprintf(stderr, "unable to connect to %s:%s %s\n", host, service, msg);
@ -106,7 +110,7 @@ int main(int argc, char **argv) {
t->loop = aeCreateEventLoop(10 + cfg.connections * 3); t->loop = aeCreateEventLoop(10 + cfg.connections * 3);
t->connections = cfg.connections / cfg.threads; t->connections = cfg.connections / cfg.threads;
t->L = script_create(cfg.script, url, headers); t->L = script_create(cfg.script, url, cfg.method, cfg.data, headers);
script_init(L, t, argc - optind, &argv[optind]); script_init(L, t, argc - optind, &argv[optind]);
if (i == 0) { if (i == 0) {
@ -471,7 +475,9 @@ static struct option longopts[] = {
{ "duration", required_argument, NULL, 'd' }, { "duration", required_argument, NULL, 'd' },
{ "threads", required_argument, NULL, 't' }, { "threads", required_argument, NULL, 't' },
{ "script", required_argument, NULL, 's' }, { "script", required_argument, NULL, 's' },
{ "method", required_argument, NULL, 'X' },
{ "header", required_argument, NULL, 'H' }, { "header", required_argument, NULL, 'H' },
{ "data", required_argument, NULL, 'D' },
{ "latency", no_argument, NULL, 'L' }, { "latency", no_argument, NULL, 'L' },
{ "timeout", required_argument, NULL, 'T' }, { "timeout", required_argument, NULL, 'T' },
{ "help", no_argument, NULL, 'h' }, { "help", no_argument, NULL, 'h' },
@ -488,8 +494,10 @@ static int parse_args(struct config *cfg, char **url, struct http_parser_url *pa
cfg->connections = 10; cfg->connections = 10;
cfg->duration = 10; cfg->duration = 10;
cfg->timeout = SOCKET_TIMEOUT_MS; cfg->timeout = SOCKET_TIMEOUT_MS;
cfg->method = "GET";
cfg->data = "";
while ((c = getopt_long(argc, argv, "t:c:d:s:H:T:Lrv?", longopts, NULL)) != -1) { while ((c = getopt_long(argc, argv, "t:c:d:D:s:X:H:T:Lrv?", longopts, NULL)) != -1) {
switch (c) { switch (c) {
case 't': case 't':
if (scan_metric(optarg, &cfg->threads)) return -1; if (scan_metric(optarg, &cfg->threads)) return -1;
@ -503,9 +511,15 @@ static int parse_args(struct config *cfg, char **url, struct http_parser_url *pa
case 's': case 's':
cfg->script = optarg; cfg->script = optarg;
break; break;
case 'X':
cfg->method = optarg;
break;
case 'H': case 'H':
*header++ = optarg; *header++ = optarg;
break; break;
case 'D':
cfg->data = optarg;
break;
case 'L': case 'L':
cfg->latency = true; cfg->latency = true;
break; break;