1
0
mirror of https://github.com/wg/wrk synced 2025-01-04 20:13:06 +08:00

Added body --data option.

This commit is contained in:
Vladimir Chebotarev 2024-08-31 01:44:20 +03:00
parent 8bc5fd1ab5
commit c2716df433
3 changed files with 14 additions and 5 deletions

View File

@ -45,7 +45,7 @@ static const struct luaL_Reg threadlib[] = {
{ NULL, NULL }
};
lua_State *script_create(char *file, char *url, char *method, char **headers) {
lua_State *script_create(char *file, char *url, char *method, char *data, char **headers) {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
(void) luaL_dostring(L, "wrk = require \"wrk\"");
@ -79,6 +79,8 @@ lua_State *script_create(char *file, char *url, char *method, char **headers) {
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);
lua_getfield(L, 4, "headers");

View File

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

View File

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