statsmanager, util.statistics: API changes, remove debugging
authorMatthew Wild <mwild1@gmail.com>
Wed, 21 Jan 2015 01:26:06 +0000 (01:26 +0000)
committerMatthew Wild <mwild1@gmail.com>
Wed, 21 Jan 2015 01:26:06 +0000 (01:26 +0000)
core/statsmanager.lua
util/statistics.lua

index 83df61819007d3809467af10e13421baa33c2fc5..62d217ef2a9f2832cadee65a1f46d0ef82afd401 100644 (file)
@@ -23,29 +23,21 @@ if stats_interval then
                return f(name);
        end
 
-       local mark_collection_start = measure("duration", "stats.collection_time");
-       local mark_processing_start = measure("duration", "stats.processing_time");
+       local mark_collection_start = measure("times", "stats.collection");
+       local mark_processing_start = measure("times", "stats.processing");
 
        function collect()
                local mark_collection_done = mark_collection_start();
                changed_stats, stats_extra = {}, {};
-               for name, getter in pairs(stats.get_stats()) do
+               for stat_name, getter in pairs(stats.get_stats()) do
                        local type, value, extra = getter();
-                       local stat_name = name..":"..type;
                        local old_value = latest_stats[stat_name];
                        latest_stats[stat_name] = value;
                        if value ~= old_value then
                                changed_stats[stat_name] = value;
                        end
                        if extra then
-                               print(stat_name, extra)
                                stats_extra[stat_name] = extra;
-                               if type == "duration" then
-                                       local rate = extra.rate;
-                                       local rate_name = name..":rate";
-                                       latest_stats[rate_name] = rate;
-                                       changed_stats[rate_name] = rate;
-                               end
                        end
                end
                mark_collection_done();
index 9d3320532e6ddc67c19486a4f33a22e2f86cdf56..08c765ae43785214509bfed2a987f98e976ffe2a 100644 (file)
@@ -20,24 +20,45 @@ local function new_registry(config)
        local duration_sample_interval = config.duration_sample_interval or 5;
        local duration_max_samples = config.duration_max_stored_samples or 5000;
 
+       local function get_distribution_stats(events, n_actual_events, since, new_time, units)
+               local n_stored_events = #events;
+               t_sort(events);
+               local sum = 0;
+               for i = 1, n_stored_events do
+                       sum = sum + events[i];
+               end
+
+               return {
+                       samples = events;
+                       sample_count = n_stored_events;
+                       count = n_actual_events,
+                       rate = n_actual_events/(new_time-since);
+                       average = n_stored_events > 0 and sum/n_stored_events or 0,
+                       min = events[1] or 0,
+                       max = events[n_stored_events] or 0,
+                       units = units,
+               };
+       end
+
+
        local registry = {};
        local methods;
        methods = {
                amount = function (name, initial)
                        local v = initial or 0;
-                       registry[name] = function () return "amount", v; end
+                       registry[name..":amount"] = function () return "amount", v; end
                        return function (new_v) v = new_v; end
                end;
                counter = function (name, initial)
                        local v = initial or 0;
-                       registry[name] = function () return "amount", v; end
+                       registry[name..":amount"] = function () return "amount", v; end
                        return function (delta)
                                v = v + delta;
                        end;
                end;
                rate = function (name)
                        local since, n = time(), 0;
-                       registry[name] = function ()
+                       registry[name..":rate"] = function ()
                                local t = time();
                                local stats = {
                                        rate = n/(t-since);
@@ -50,35 +71,43 @@ local function new_registry(config)
                                n = n + 1;
                        end;
                end;
-               duration = function (name)
+               distribution = function (name, unit, type)
+                       type = type or "distribution";
                        local events, last_event = {}, 0;
                        local n_actual_events = 0;
                        local since = time();
 
-                       registry[name] = function ()
-                               local n_stored_events = #events;
-                               t_sort(events);
-                               local sum = 0;
-                               for i = 1, n_stored_events do
-                                       sum = sum + events[i];
-                               end
-
+                       registry[name..":"..type] = function ()
                                local new_time = time();
+                               local stats = get_distribution_stats(events, n_actual_events, since, new_time, unit);
+                               events, last_event = {}, 0;
+                               n_actual_events = 0;
+                               since = new_time;
+                               return type, stats.average, stats;
+                       end;
 
-                               local stats = {
-                                       samples = events;
-                                       sample_count = n_stored_events;
-                                       count = n_actual_events,
-                                       rate = n_actual_events/(new_time-since);
-                                       average = n_stored_events > 0 and sum/n_stored_events or 0,
-                                       min = events[1],
-                                       max = events[n_stored_events],
-                               };
+                       return function (value)
+                               n_actual_events = n_actual_events + 1;
+                               if n_actual_events%duration_sample_interval > 0 then
+                                       last_event = (last_event%duration_max_samples) + 1;
+                                       events[last_event] = value;
+                               end
+                       end;
+               end;
+               sizes = function (name)
+                       return methods.distribution(name, "bytes", "size");
+               end;
+               times = function (name)
+                       local events, last_event = {}, 0;
+                       local n_actual_events = 0;
+                       local since = time();
 
+                       registry[name..":duration"] = function ()
+                               local new_time = time();
+                               local stats = get_distribution_stats(events, n_actual_events, since, new_time, "seconds");
                                events, last_event = {}, 0;
                                n_actual_events = 0;
                                since = new_time;
-
                                return "duration", stats.average, stats;
                        end;
 
@@ -111,7 +140,7 @@ return {
                n_buckets = n_buckets or 100;
                local events, n_events = duration.samples, duration.sample_count;
                if not (events and n_events) then
-                       return nil, "not a valid duration stat";
+                       return nil, "not a valid distribution stat";
                end
                local histogram = {};
 
@@ -124,7 +153,7 @@ return {
        get_percentile = function (duration, pc)
                local events, n_events = duration.samples, duration.sample_count;
                if not (events and n_events) then
-                       return nil, "not a valid duration stat";
+                       return nil, "not a valid distribution stat";
                end
                return percentile(events, n_events, pc);
        end;