-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeter.lua
More file actions
77 lines (64 loc) · 1.91 KB
/
Copy pathmeter.lua
File metadata and controls
77 lines (64 loc) · 1.91 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
local sqrt = math.sqrt
local meter = {}
meter.__index = meter
function meter.New(capacity)
local self = setmetatable({}, meter)
self.capacity = capacity or 1000
self.stages = {} -- [stage] = { values = {…} }
self.results = nil
return self
end
function meter:AddMeasurement(stage, duration)
-- Safety check: ignore non‑numeric durations
if type(duration) ~= "number" then return end
local s = self.stages[stage]
if not s then
s = { values = {} }
self.stages[stage] = s
end
local v = s.values
v[#v + 1] = duration
while #v > self.capacity do
table.remove(v, 1)
end
end
function meter:Calculate()
local results = {}
for stage, s in pairs(self.stages) do
local values = s.values
local n = #values
if n == 0 then
results[stage] = { avg = 0, std = 0, min = 0, max = 0, unit = "us" }
else
local sum, sumSq, minV, maxV = 0, 0, math.huge, -math.huge
for i = 1, n do
local v = values[i]
sum = sum + v
sumSq = sumSq + v * v
if v < minV then minV = v end
if v > maxV then maxV = v end
end
local avg = sum / n
local variance = (n > 1) and (sumSq - n * avg * avg) / (n - 1) or 0
local std = sqrt(variance)
local scale, unit
if avg < 1e-4 then
scale, unit = 1e6, "us"
elseif avg < 0.1 then
scale, unit = 1e3, "ms"
else
scale, unit = 1, "s"
end
results[stage] = {
avg = avg * scale,
std = std * scale,
min = minV * scale,
max = maxV * scale,
unit = unit,
}
end
end
self.results = results
return results
end
PerformanceMeter = meter