Comparison

util/statistics.lua @ 11120:b2331f3dfeea

Merge 0.11->trunk
author Matthew Wild <mwild1@gmail.com>
date Wed, 30 Sep 2020 09:50:33 +0100
parent 10883:d75d805c852f
child 11523:5f15ab7c6ae5
comparison
equal deleted inserted replaced
11119:68df52bf08d5 11120:b2331f3dfeea
42 42
43 43
44 local registry = {}; 44 local registry = {};
45 local methods; 45 local methods;
46 methods = { 46 methods = {
47 amount = function (name, initial) 47 amount = function (name, conf)
48 local v = initial or 0; 48 local v = conf and conf.initial or 0;
49 registry[name..":amount"] = function () return "amount", v; end 49 registry[name..":amount"] = function ()
50 return "amount", v, conf;
51 end
50 return function (new_v) v = new_v; end 52 return function (new_v) v = new_v; end
51 end; 53 end;
52 counter = function (name, initial) 54 counter = function (name, conf)
53 local v = initial or 0; 55 local v = conf and conf.initial or 0;
54 registry[name..":amount"] = function () return "amount", v; end 56 registry[name..":amount"] = function ()
57 return "amount", v, conf;
58 end
55 return function (delta) 59 return function (delta)
56 v = v + delta; 60 v = v + delta;
57 end; 61 end;
58 end; 62 end;
59 rate = function (name) 63 rate = function (name, conf)
60 local since, n = time(), 0; 64 local since, n, total = time(), 0, 0;
61 registry[name..":rate"] = function () 65 registry[name..":rate"] = function ()
66 total = total + n;
62 local t = time(); 67 local t = time();
63 local stats = { 68 local stats = {
64 rate = n/(t-since); 69 rate = n/(t-since);
65 count = n; 70 count = n;
71 total = total;
72 units = conf and conf.units;
73 type = conf and conf.type;
66 }; 74 };
67 since, n = t, 0; 75 since, n = t, 0;
68 return "rate", stats.rate, stats; 76 return "rate", stats.rate, stats;
69 end; 77 end;
70 return function () 78 return function ()
71 n = n + 1; 79 n = n + 1;
72 end; 80 end;
73 end; 81 end;
74 distribution = function (name, unit, type) 82 distribution = function (name, conf)
75 type = type or "distribution"; 83 local units = conf and conf.units;
84 local type = conf and conf.type or "distribution";
76 local events, last_event = {}, 0; 85 local events, last_event = {}, 0;
77 local n_actual_events = 0; 86 local n_actual_events = 0;
78 local since = time(); 87 local since = time();
79 88
80 registry[name..":"..type] = function () 89 registry[name..":"..type] = function ()
81 local new_time = time(); 90 local new_time = time();
82 local stats = get_distribution_stats(events, n_actual_events, since, new_time, unit); 91 local stats = get_distribution_stats(events, n_actual_events, since, new_time, units);
83 events, last_event = {}, 0; 92 events, last_event = {}, 0;
84 n_actual_events = 0; 93 n_actual_events = 0;
85 since = new_time; 94 since = new_time;
86 return type, stats.average, stats; 95 return type, stats.average, stats;
87 end; 96 end;
92 last_event = (last_event%duration_max_samples) + 1; 101 last_event = (last_event%duration_max_samples) + 1;
93 events[last_event] = value; 102 events[last_event] = value;
94 end 103 end
95 end; 104 end;
96 end; 105 end;
97 sizes = function (name) 106 sizes = function (name, conf)
98 return methods.distribution(name, "bytes", "size"); 107 conf = conf or { units = "bytes", type = "size" }
108 return methods.distribution(name, conf);
99 end; 109 end;
100 times = function (name) 110 times = function (name, conf)
111 local units = conf and conf.units or "seconds";
101 local events, last_event = {}, 0; 112 local events, last_event = {}, 0;
102 local n_actual_events = 0; 113 local n_actual_events = 0;
103 local since = time(); 114 local since = time();
104 115
105 registry[name..":duration"] = function () 116 registry[name..":duration"] = function ()
106 local new_time = time(); 117 local new_time = time();
107 local stats = get_distribution_stats(events, n_actual_events, since, new_time, "seconds"); 118 local stats = get_distribution_stats(events, n_actual_events, since, new_time, units);
108 events, last_event = {}, 0; 119 events, last_event = {}, 0;
109 n_actual_events = 0; 120 n_actual_events = 0;
110 since = new_time; 121 since = new_time;
111 return "duration", stats.average, stats; 122 return "duration", stats.average, stats;
112 end; 123 end;