Software /
code /
prosody
Annotate
util/statistics.lua @ 10799:763bb2ce3f60
util.pposix,signal: Pass around various OS numbers as integers [Lua 5.3]
Passing around PIDs, UIDs etc as integers makes it more sane in Lua 5.3.
Getting 1234.0 as PID is silly. Shouldn't change any behavior as these
are all integers on the C side and the integral floats are accepted as
integers when passed back from Lua into C.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 04 May 2020 21:51:30 +0200 |
parent | 10314:bbc879eab1bf |
child | 10883:d75d805c852f |
rev | line source |
---|---|
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local t_sort = table.sort |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local m_floor = math.floor; |
7988
dc758422d896
util.statistics,statsd,throttle,timer: Replace dependency on LuaSockect with util.time for precision time
Kim Alvefur <zash@zash.se>
parents:
6649
diff
changeset
|
3 local time = require "util.time".now; |
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local function nop_function() end |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local function percentile(arr, length, pc) |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local n = pc/100 * (length + 1); |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local k, d = m_floor(n), n%1; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 if k == 0 then |
6562
2b5ced5ca31f
util.statistics: Return 0 as percentile if data out of range
Matthew Wild <mwild1@gmail.com>
parents:
6555
diff
changeset
|
11 return arr[1] or 0; |
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 elseif k >= length then |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 return arr[length]; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 end |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 return arr[k] + d*(arr[k+1] - arr[k]); |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 end |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local function new_registry(config) |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 config = config or {}; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 local duration_sample_interval = config.duration_sample_interval or 5; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 local duration_max_samples = config.duration_max_stored_samples or 5000; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
6555
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
23 local function get_distribution_stats(events, n_actual_events, since, new_time, units) |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
24 local n_stored_events = #events; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
25 t_sort(events); |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
26 local sum = 0; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
27 for i = 1, n_stored_events do |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
28 sum = sum + events[i]; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
29 end |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
30 |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
31 return { |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
32 samples = events; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
33 sample_count = n_stored_events; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
34 count = n_actual_events, |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
35 rate = n_actual_events/(new_time-since); |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
36 average = n_stored_events > 0 and sum/n_stored_events or 0, |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
37 min = events[1] or 0, |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
38 max = events[n_stored_events] or 0, |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
39 units = units, |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
40 }; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
41 end |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
42 |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
43 |
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 local registry = {}; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 local methods; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 methods = { |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 amount = function (name, initial) |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 local v = initial or 0; |
6555
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
49 registry[name..":amount"] = function () return "amount", v; end |
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 return function (new_v) v = new_v; end |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 end; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 counter = function (name, initial) |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 local v = initial or 0; |
6555
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
54 registry[name..":amount"] = function () return "amount", v; end |
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 return function (delta) |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 v = v + delta; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 end; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 end; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 rate = function (name) |
10314
bbc879eab1bf
util.statistics: Add a total count for rate counters, counting from server start.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7988
diff
changeset
|
60 local since, n, total = time(), 0, 0; |
6555
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
61 registry[name..":rate"] = function () |
10314
bbc879eab1bf
util.statistics: Add a total count for rate counters, counting from server start.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7988
diff
changeset
|
62 total = total + n; |
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 local t = time(); |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 local stats = { |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 rate = n/(t-since); |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 count = n; |
10314
bbc879eab1bf
util.statistics: Add a total count for rate counters, counting from server start.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7988
diff
changeset
|
67 total = total; |
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 }; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 since, n = t, 0; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 return "rate", stats.rate, stats; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 end; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 return function () |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 n = n + 1; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 end; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 end; |
6555
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
76 distribution = function (name, unit, type) |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
77 type = type or "distribution"; |
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 local events, last_event = {}, 0; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 local n_actual_events = 0; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 local since = time(); |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 |
6555
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
82 registry[name..":"..type] = function () |
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 local new_time = time(); |
6555
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
84 local stats = get_distribution_stats(events, n_actual_events, since, new_time, unit); |
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 events, last_event = {}, 0; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 n_actual_events = 0; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 since = new_time; |
6555
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
88 return type, stats.average, stats; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
89 end; |
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 |
6555
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
91 return function (value) |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
92 n_actual_events = n_actual_events + 1; |
6649
99fa40d498cc
util.statistics: Collect duration sample even if run fewer times than the sample interval
Kim Alvefur <zash@zash.se>
parents:
6562
diff
changeset
|
93 if n_actual_events%duration_sample_interval == 1 then |
6555
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
94 last_event = (last_event%duration_max_samples) + 1; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
95 events[last_event] = value; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
96 end |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
97 end; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
98 end; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
99 sizes = function (name) |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
100 return methods.distribution(name, "bytes", "size"); |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
101 end; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
102 times = function (name) |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
103 local events, last_event = {}, 0; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
104 local n_actual_events = 0; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
105 local since = time(); |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
106 |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
107 registry[name..":duration"] = function () |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
108 local new_time = time(); |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
109 local stats = get_distribution_stats(events, n_actual_events, since, new_time, "seconds"); |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
110 events, last_event = {}, 0; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
111 n_actual_events = 0; |
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
112 since = new_time; |
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 return "duration", stats.average, stats; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 end; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 return function () |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 n_actual_events = n_actual_events + 1; |
6649
99fa40d498cc
util.statistics: Collect duration sample even if run fewer times than the sample interval
Kim Alvefur <zash@zash.se>
parents:
6562
diff
changeset
|
118 if n_actual_events%duration_sample_interval ~= 1 then |
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 return nop_function; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 end |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 local start_time = time(); |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 return function () |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 local end_time = time(); |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 local duration = end_time - start_time; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 last_event = (last_event%duration_max_samples) + 1; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 events[last_event] = duration; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 end |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 end; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 end; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 get_stats = function () |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 return registry; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 end; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 }; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 return methods; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 end |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 return { |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 new = new_registry; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 get_histogram = function (duration, n_buckets) |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 n_buckets = n_buckets or 100; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 local events, n_events = duration.samples, duration.sample_count; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 if not (events and n_events) then |
6555
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
145 return nil, "not a valid distribution stat"; |
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 end |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 local histogram = {}; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 for i = 1, 100, 100/n_buckets do |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 histogram[i] = percentile(events, n_events, i); |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 end |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 return histogram; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 end; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 get_percentile = function (duration, pc) |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 local events, n_events = duration.samples, duration.sample_count; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 if not (events and n_events) then |
6555
7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents:
6553
diff
changeset
|
158 return nil, "not a valid distribution stat"; |
6553
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 end |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 return percentile(events, n_events, pc); |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 end; |
0e29c05c3503
util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 } |