openresty 计算执行时间,


location /test_time {
    content_by_lua_block {
        ngx.say("test t1")
        local t1 = ngx.now()
        ngx.say(t1)
        ngx.sleep(0.05)
        ngx.say("test t2")
        local t2 = ngx.now()
        ngx.say(t2)
        ngx.say(string.format("%3f", t2-t1))
        ngx.exit(200)
    }
}

执行过程:
$curl ‘http://test.jinbo.com/test_time’
test t1
1555403140.677
test t2
1555403140.727
0.050000

保留小数位知识点:
string.format("%3f", t2-t1) #保留3位小数,四舍五入与c语言一致

相关内容

    暂无相关文章