Last active
February 5, 2018 18:32
-
-
Save gdvalle/935d216e058188e3fb472fee70cf9ec5 to your computer and use it in GitHub Desktop.
OpenResty micro benchmark lib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local ffi = require "ffi" | |
local ngx = ngx | |
local ngx_update_time = ngx.update_time | |
local ngx_say = ngx.say | |
local tonumber = tonumber | |
ffi.cdef[[ | |
typedef long time_t; | |
typedef struct timeval { | |
time_t tv_sec; | |
time_t tv_usec; | |
} timeval; | |
int gettimeofday(struct timeval* t, void* tzp); | |
]] | |
local gettimeofday_struct = ffi.new("timeval") | |
local gettimeofday = ffi.C.gettimeofday | |
local _M = {} | |
local function time() | |
gettimeofday(gettimeofday_struct, nil) | |
return tonumber(gettimeofday_struct.tv_sec) * 1000000 + tonumber(gettimeofday_struct.tv_usec) | |
end | |
_M.time = time | |
function _M.bench(name, rounds, func) | |
ngx_update_time() | |
local start = time() | |
for _ = 1,rounds do | |
func() | |
end | |
local stop = time() | |
ngx_say(((stop - start) / rounds) .. ": " .. name) | |
end | |
return _M |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment