Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

script: add clock module #500

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
script: add clock module
  • Loading branch information
myzhan committed Aug 19, 2022
commit e8612f9fac616b6418ae80d30ba094903ebe45b1
6 changes: 6 additions & 0 deletions scripts/clock.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- example script that demonstrates use of the clock module

function setup(thread)
print("Current wall time in us:" .. string.format("%.0f", clock.realtime()))
print("Current monotonic time in us:" .. string.format("%.0f", clock.monotonic()))
end
42 changes: 36 additions & 6 deletions src/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "script.h"
#include "http_parser.h"
#include "zmalloc.h"
Expand All @@ -19,6 +20,8 @@ static int script_stats_len(lua_State *);
static int script_stats_index(lua_State *);
static int script_thread_index(lua_State *);
static int script_thread_newindex(lua_State *);
static int script_clock_real_time(lua_State *);
static int script_clock_monotonic(lua_State *);
static int script_wrk_lookup(lua_State *);
static int script_wrk_connect(lua_State *);

Expand All @@ -45,6 +48,12 @@ static const struct luaL_Reg threadlib[] = {
{ NULL, NULL }
};

static const struct luaL_Reg clocklib[] = {
{ "realtime", script_clock_real_time },
{ "monotonic", script_clock_monotonic },
{ NULL, NULL }
};

lua_State *script_create(char *file, char *url, char **headers) {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
Expand All @@ -57,6 +66,8 @@ lua_State *script_create(char *file, char *url, char **headers) {
luaL_newmetatable(L, "wrk.thread");
luaL_register(L, NULL, threadlib);

luaL_register(L, "clock", clocklib);

struct http_parser_url parts = {};
script_parse_url(url, &parts);
char *path = "/";
Expand All @@ -74,12 +85,12 @@ lua_State *script_create(char *file, char *url, char **headers) {

lua_getglobal(L, "wrk");

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, "port", push_url_part(L, url, &parts, UF_PORT));
set_fields(L, 4, fields);
set_field(L, 5, "scheme", push_url_part(L, url, &parts, UF_SCHEMA));
set_field(L, 5, "host", push_url_part(L, url, &parts, UF_HOST));
set_field(L, 5, "port", push_url_part(L, url, &parts, UF_PORT));
set_fields(L, 5, fields);

lua_getfield(L, 4, "headers");
lua_getfield(L, 5, "headers");
for (char **h = headers; *h; h++) {
char *p = strchr(*h, ':');
if (p && p[1] == ' ') {
Expand All @@ -88,7 +99,7 @@ lua_State *script_create(char *file, char *url, char **headers) {
lua_settable(L, 5);
}
}
lua_pop(L, 5);
lua_pop(L, 6);

if (file && luaL_dofile(L, file)) {
const char *cause = lua_tostring(L, -1);
Expand Down Expand Up @@ -436,6 +447,25 @@ static int script_thread_newindex(lua_State *L) {
return 0;
}

static int script_clock_real_time(lua_State *L) {
struct timespec t = {0, 0};
uint64_t now = 0;
clock_gettime(CLOCK_REALTIME, &t);
now = t.tv_sec * 1000000 + t.tv_nsec/1000;
lua_pushnumber(L, now);
return 1;
}

static int script_clock_monotonic(lua_State *L) {
struct timespec t = {0, 0};
uint64_t now = 0;
clock_gettime(CLOCK_MONOTONIC, &t);
now = t.tv_sec * 1000000 + t.tv_nsec/1000;
lua_pushnumber(L, now);
return 1;
}


static int script_wrk_lookup(lua_State *L) {
struct addrinfo *addrs;
struct addrinfo hints = {
Expand Down