Software /
code /
prosody
Comparison
util/statistics.lua @ 6555:7b2d16c14659
statsmanager, util.statistics: API changes, remove debugging
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 21 Jan 2015 01:26:06 +0000 |
parent | 6553:0e29c05c3503 |
child | 6562:2b5ced5ca31f |
comparison
equal
deleted
inserted
replaced
6554:6c22bec3e8d0 | 6555:7b2d16c14659 |
---|---|
18 local function new_registry(config) | 18 local function new_registry(config) |
19 config = config or {}; | 19 config = config or {}; |
20 local duration_sample_interval = config.duration_sample_interval or 5; | 20 local duration_sample_interval = config.duration_sample_interval or 5; |
21 local duration_max_samples = config.duration_max_stored_samples or 5000; | 21 local duration_max_samples = config.duration_max_stored_samples or 5000; |
22 | 22 |
23 local function get_distribution_stats(events, n_actual_events, since, new_time, units) | |
24 local n_stored_events = #events; | |
25 t_sort(events); | |
26 local sum = 0; | |
27 for i = 1, n_stored_events do | |
28 sum = sum + events[i]; | |
29 end | |
30 | |
31 return { | |
32 samples = events; | |
33 sample_count = n_stored_events; | |
34 count = n_actual_events, | |
35 rate = n_actual_events/(new_time-since); | |
36 average = n_stored_events > 0 and sum/n_stored_events or 0, | |
37 min = events[1] or 0, | |
38 max = events[n_stored_events] or 0, | |
39 units = units, | |
40 }; | |
41 end | |
42 | |
43 | |
23 local registry = {}; | 44 local registry = {}; |
24 local methods; | 45 local methods; |
25 methods = { | 46 methods = { |
26 amount = function (name, initial) | 47 amount = function (name, initial) |
27 local v = initial or 0; | 48 local v = initial or 0; |
28 registry[name] = function () return "amount", v; end | 49 registry[name..":amount"] = function () return "amount", v; end |
29 return function (new_v) v = new_v; end | 50 return function (new_v) v = new_v; end |
30 end; | 51 end; |
31 counter = function (name, initial) | 52 counter = function (name, initial) |
32 local v = initial or 0; | 53 local v = initial or 0; |
33 registry[name] = function () return "amount", v; end | 54 registry[name..":amount"] = function () return "amount", v; end |
34 return function (delta) | 55 return function (delta) |
35 v = v + delta; | 56 v = v + delta; |
36 end; | 57 end; |
37 end; | 58 end; |
38 rate = function (name) | 59 rate = function (name) |
39 local since, n = time(), 0; | 60 local since, n = time(), 0; |
40 registry[name] = function () | 61 registry[name..":rate"] = function () |
41 local t = time(); | 62 local t = time(); |
42 local stats = { | 63 local stats = { |
43 rate = n/(t-since); | 64 rate = n/(t-since); |
44 count = n; | 65 count = n; |
45 }; | 66 }; |
48 end; | 69 end; |
49 return function () | 70 return function () |
50 n = n + 1; | 71 n = n + 1; |
51 end; | 72 end; |
52 end; | 73 end; |
53 duration = function (name) | 74 distribution = function (name, unit, type) |
75 type = type or "distribution"; | |
54 local events, last_event = {}, 0; | 76 local events, last_event = {}, 0; |
55 local n_actual_events = 0; | 77 local n_actual_events = 0; |
56 local since = time(); | 78 local since = time(); |
57 | 79 |
58 registry[name] = function () | 80 registry[name..":"..type] = function () |
59 local n_stored_events = #events; | |
60 t_sort(events); | |
61 local sum = 0; | |
62 for i = 1, n_stored_events do | |
63 sum = sum + events[i]; | |
64 end | |
65 | |
66 local new_time = time(); | 81 local new_time = time(); |
67 | 82 local stats = get_distribution_stats(events, n_actual_events, since, new_time, unit); |
68 local stats = { | |
69 samples = events; | |
70 sample_count = n_stored_events; | |
71 count = n_actual_events, | |
72 rate = n_actual_events/(new_time-since); | |
73 average = n_stored_events > 0 and sum/n_stored_events or 0, | |
74 min = events[1], | |
75 max = events[n_stored_events], | |
76 }; | |
77 | |
78 events, last_event = {}, 0; | 83 events, last_event = {}, 0; |
79 n_actual_events = 0; | 84 n_actual_events = 0; |
80 since = new_time; | 85 since = new_time; |
86 return type, stats.average, stats; | |
87 end; | |
81 | 88 |
89 return function (value) | |
90 n_actual_events = n_actual_events + 1; | |
91 if n_actual_events%duration_sample_interval > 0 then | |
92 last_event = (last_event%duration_max_samples) + 1; | |
93 events[last_event] = value; | |
94 end | |
95 end; | |
96 end; | |
97 sizes = function (name) | |
98 return methods.distribution(name, "bytes", "size"); | |
99 end; | |
100 times = function (name) | |
101 local events, last_event = {}, 0; | |
102 local n_actual_events = 0; | |
103 local since = time(); | |
104 | |
105 registry[name..":duration"] = function () | |
106 local new_time = time(); | |
107 local stats = get_distribution_stats(events, n_actual_events, since, new_time, "seconds"); | |
108 events, last_event = {}, 0; | |
109 n_actual_events = 0; | |
110 since = new_time; | |
82 return "duration", stats.average, stats; | 111 return "duration", stats.average, stats; |
83 end; | 112 end; |
84 | 113 |
85 return function () | 114 return function () |
86 n_actual_events = n_actual_events + 1; | 115 n_actual_events = n_actual_events + 1; |
109 new = new_registry; | 138 new = new_registry; |
110 get_histogram = function (duration, n_buckets) | 139 get_histogram = function (duration, n_buckets) |
111 n_buckets = n_buckets or 100; | 140 n_buckets = n_buckets or 100; |
112 local events, n_events = duration.samples, duration.sample_count; | 141 local events, n_events = duration.samples, duration.sample_count; |
113 if not (events and n_events) then | 142 if not (events and n_events) then |
114 return nil, "not a valid duration stat"; | 143 return nil, "not a valid distribution stat"; |
115 end | 144 end |
116 local histogram = {}; | 145 local histogram = {}; |
117 | 146 |
118 for i = 1, 100, 100/n_buckets do | 147 for i = 1, 100, 100/n_buckets do |
119 histogram[i] = percentile(events, n_events, i); | 148 histogram[i] = percentile(events, n_events, i); |
122 end; | 151 end; |
123 | 152 |
124 get_percentile = function (duration, pc) | 153 get_percentile = function (duration, pc) |
125 local events, n_events = duration.samples, duration.sample_count; | 154 local events, n_events = duration.samples, duration.sample_count; |
126 if not (events and n_events) then | 155 if not (events and n_events) then |
127 return nil, "not a valid duration stat"; | 156 return nil, "not a valid distribution stat"; |
128 end | 157 end |
129 return percentile(events, n_events, pc); | 158 return percentile(events, n_events, pc); |
130 end; | 159 end; |
131 } | 160 } |