Annotate

util/statistics.lua @ 11026:a086825ed73a

util.dataforms: Convert media element sizes to avoid error on Lua 5.3 The stanza API does not accept number values and threw an error due to the height and width attributes of the media element (XEP-0221). This part had no test coverage previously, explaining why it was not discovered until now.
author Kim Alvefur <zash@zash.se>
date Sun, 16 Aug 2020 12:55:55 +0200
parent 10883:d75d805c852f
child 11523:5f15ab7c6ae5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 = {
10883
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
47 amount = function (name, conf)
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
48 local v = conf and conf.initial or 0;
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
49 registry[name..":amount"] = function ()
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
50 return "amount", v, conf;
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
51 end
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 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
53 end;
10883
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
54 counter = function (name, conf)
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
55 local v = conf and conf.initial or 0;
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
56 registry[name..":amount"] = function ()
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
57 return "amount", v, conf;
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
58 end
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 return function (delta)
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 v = v + delta;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end;
10883
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
63 rate = function (name, conf)
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
64 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
65 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
66 total = total + n;
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 local t = time();
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 local stats = {
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 rate = n/(t-since);
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 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
71 total = total;
10883
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
72 units = conf and conf.units;
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
73 type = conf and conf.type;
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 };
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 since, n = t, 0;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 return "rate", stats.rate, stats;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 return function ()
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 n = n + 1;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end;
10883
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
82 distribution = function (name, conf)
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
83 local units = conf and conf.units;
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
84 local type = conf and conf.type or "distribution";
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 local events, last_event = {}, 0;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 local n_actual_events = 0;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 local since = time();
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
89 registry[name..":"..type] = function ()
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 local new_time = time();
10883
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
91 local stats = get_distribution_stats(events, n_actual_events, since, new_time, units);
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 events, last_event = {}, 0;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 n_actual_events = 0;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 since = new_time;
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
95 return type, stats.average, stats;
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
96 end;
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
98 return function (value)
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
99 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
100 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
101 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
102 events[last_event] = value;
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
103 end
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
104 end;
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
105 end;
10883
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
106 sizes = function (name, conf)
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
107 conf = conf or { units = "bytes", type = "size" }
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
108 return methods.distribution(name, conf);
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
109 end;
10883
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
110 times = function (name, conf)
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
111 local units = conf and conf.units or "seconds";
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
112 local events, last_event = {}, 0;
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
113 local n_actual_events = 0;
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
114 local since = time();
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
115
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
116 registry[name..":duration"] = function ()
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
117 local new_time = time();
10883
d75d805c852f util.statistics: Unify API of methods to include a config table
Kim Alvefur <zash@zash.se>
parents: 10314
diff changeset
118 local stats = get_distribution_stats(events, n_actual_events, since, new_time, units);
6555
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
119 events, last_event = {}, 0;
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
120 n_actual_events = 0;
7b2d16c14659 statsmanager, util.statistics: API changes, remove debugging
Matthew Wild <mwild1@gmail.com>
parents: 6553
diff changeset
121 since = new_time;
6553
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 return "duration", stats.average, stats;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 end;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 return function ()
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 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
127 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
128 return nop_function;
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
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 local start_time = time();
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 return function ()
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 local end_time = time();
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 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
135 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
136 events[last_event] = duration;
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 end;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 end;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 get_stats = function ()
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 return registry;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 end;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 };
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 return methods;
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
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 return {
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 new = new_registry;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 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
151 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
152 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
153 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
154 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
155 end
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 local histogram = {};
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 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
159 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
160 end
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 return histogram;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 end;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 get_percentile = function (duration, pc)
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 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
166 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
167 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
168 end
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 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
170 end;
0e29c05c3503 util.statistics: New library for gathering various kinds of statistics
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 }