-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime.lua
More file actions
38 lines (32 loc) · 723 Bytes
/
time.lua
File metadata and controls
38 lines (32 loc) · 723 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
local socket = require('socket')
local log = require('./log')
local _startTimes = {}
-- Returns the epoch time in ms
function now()
return socket.gettime() * 1000
end
function start()
local key = {}
_startTimes[key] = now()
return key
end
function done(key, label, opts)
local endTime = now()
opts = opts or {}
if label == nil then
label = 'time'
end
local message = opts.message or ''
local threshold = opts.threshold
local startTime = _startTimes[key]
local dt = endTime - startTime
if (not opts.quiet) and ((not threshold) or dt > threshold) then
log(label .. ': ' .. math.ceil(dt) .. 'ms ' .. message)
end
return dt
end
return {
now = now,
start = start,
done = done
}