refactor(metrics): Use modified moving average for topic metrics speed
This commit is contained in:
parent
e586119752
commit
abb74056bd
|
@ -43,7 +43,6 @@
|
||||||
-export([ inc/2
|
-export([ inc/2
|
||||||
, inc/3
|
, inc/3
|
||||||
, val/2
|
, val/2
|
||||||
, rate/2
|
|
||||||
, metrics/1
|
, metrics/1
|
||||||
, register/1
|
, register/1
|
||||||
, unregister/1
|
, unregister/1
|
||||||
|
@ -52,6 +51,10 @@
|
||||||
, all_registered_topics/0
|
, all_registered_topics/0
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
%% Exposed for test only.
|
||||||
|
-export([ rate/2
|
||||||
|
]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([ init/1
|
-export([ init/1
|
||||||
, handle_call/3
|
, handle_call/3
|
||||||
|
@ -78,13 +81,11 @@
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-define(TICKING_INTERVAL, 1).
|
-define(TICKING_INTERVAL, 1).
|
||||||
|
-define(SPEED_AVERAGE_WINDOW_SIZE, 5).
|
||||||
|
|
||||||
-record(speed, {
|
-record(speed, {
|
||||||
last = 0 :: number(),
|
last = 0 :: number(),
|
||||||
tick = 1 :: number(),
|
last_v = 0 :: number()
|
||||||
last_v = 0 :: number(),
|
|
||||||
acc = 0 :: number(),
|
|
||||||
samples = [] :: list()
|
|
||||||
}).
|
}).
|
||||||
|
|
||||||
-record(state, {
|
-record(state, {
|
||||||
|
@ -358,25 +359,15 @@ counters_size() ->
|
||||||
number_of_registered_topics() ->
|
number_of_registered_topics() ->
|
||||||
proplists:get_value(size, ets:info(?TAB)).
|
proplists:get_value(size, ets:info(?TAB)).
|
||||||
|
|
||||||
calculate_speed(CurVal, #speed{last_v = LastVal, tick = Tick, acc = Acc, samples = Samples}) ->
|
calculate_speed(CurVal, #speed{last = Last,
|
||||||
|
last_v = LastVal
|
||||||
|
}) ->
|
||||||
%% calculate the current speed based on the last value of the counter
|
%% calculate the current speed based on the last value of the counter
|
||||||
CurSpeed = (CurVal - LastVal) / ?TICKING_INTERVAL,
|
CurSpeed = (CurVal - LastVal) / ?TICKING_INTERVAL,
|
||||||
|
#speed{last = mma(?SPEED_AVERAGE_WINDOW_SIZE, Last, CurSpeed),
|
||||||
|
last_v = CurVal
|
||||||
|
}.
|
||||||
|
|
||||||
%% calculate the average speed in last 5 seconds
|
%% Modified Moving Average ref: https://en.wikipedia.org/wiki/Moving_average
|
||||||
case Tick < 5 of
|
mma(WindowSize, LastSpeed, CurSpeed) ->
|
||||||
true ->
|
(LastSpeed * (WindowSize - 1) + CurSpeed) / WindowSize.
|
||||||
Acc1 = Acc + CurSpeed,
|
|
||||||
#speed{last = Acc1 / Tick,
|
|
||||||
last_v = CurVal,
|
|
||||||
acc = Acc1,
|
|
||||||
samples = Samples ++ [CurSpeed],
|
|
||||||
tick = Tick + 1};
|
|
||||||
false ->
|
|
||||||
[FirstSpeed | Speeds] = Samples,
|
|
||||||
Acc1 = Acc + CurSpeed - FirstSpeed,
|
|
||||||
#speed{last = Acc1 / Tick,
|
|
||||||
last_v = CurVal,
|
|
||||||
acc = Acc1,
|
|
||||||
samples = Speeds ++ [CurSpeed],
|
|
||||||
tick = Tick}
|
|
||||||
end.
|
|
||||||
|
|
Loading…
Reference in New Issue