Skip to content
Permalink
Browse files
Merge pull request #40 from juise/master
Add ewma for day support, add tests
  • Loading branch information
joewilliams committed Oct 9, 2012
2 parents a87ef8a + 223acaf commit e1499f30432015b923be66663779e44e4c2d1d1c
Showing 3 changed files with 31 additions and 9 deletions.
@@ -29,9 +29,10 @@

-module(folsom_ewma).

-define(M1_ALPHA, 1 - math:exp(-5 / 60.0)).
-define(M5_ALPHA, 1 - math:exp(-5 / 60.0 / 5)).
-define(M15_ALPHA, 1 - math:exp(-5 / 60.0 / 15)).
-define(M1_ALPHA, 1 - math:exp(-5 / 60.0)).
-define(M5_ALPHA, 1 - math:exp(-5 / 60.0 / 5)).
-define(M15_ALPHA, 1 - math:exp(-5 / 60.0 / 15)).
-define(D1_ALPHA, 1 - math:exp(-5 / 60.0 / 1440)).

-record(ewma, {
alpha,
@@ -47,7 +48,8 @@
tick/1,
one_minute_ewma/0,
five_minute_ewma/0,
fifteen_minute_ewma/0]).
fifteen_minute_ewma/0,
one_day_ewma/0]).


% API
@@ -61,6 +63,9 @@ five_minute_ewma() ->
fifteen_minute_ewma() ->
new(?M15_ALPHA, 5).

one_day_ewma() ->
new(?D1_ALPHA, 5).

new(Alpha, Interval) ->
#ewma{alpha = Alpha, interval = Interval}.

@@ -37,6 +37,7 @@
one,
five,
fifteen,
day,
count = 0,
start_time
}).
@@ -47,26 +48,31 @@ new(Name) ->
OneMin = folsom_ewma:one_minute_ewma(),
FiveMin = folsom_ewma:five_minute_ewma(),
FifteenMin = folsom_ewma:fifteen_minute_ewma(),
OneDay = folsom_ewma:one_day_ewma(),

ets:insert(?METER_TABLE,
{Name, #meter{one = OneMin,
five = FiveMin,
fifteen = FifteenMin,
day = OneDay,
start_time = folsom_utils:now_epoch_micro()}}).

tick(Name) ->
#meter{one = OneMin,
five = FiveMin,
fifteen = FifteenMin} = Meter = get_value(Name),
fifteen = FifteenMin,
day = OneDay} = Meter = get_value(Name),

OneMin1 = folsom_ewma:tick(OneMin),
FiveMin1 = folsom_ewma:tick(FiveMin),
FifteenMin1 = folsom_ewma:tick(FifteenMin),
OneDay1 = folsom_ewma:tick(OneDay),

ets:insert(?METER_TABLE,
{Name, Meter#meter{one = OneMin1,
five = FiveMin1,
fifteen = FifteenMin1}}).
fifteen = FifteenMin1,
day = OneDay1}}).

mark(Name) ->
mark(Name, 1).
@@ -75,28 +81,33 @@ mark(Name, Value) ->
#meter{count = Count,
one = OneMin,
five = FiveMin,
fifteen = FifteenMin} = Meter = get_value(Name),
fifteen = FifteenMin,
day = OneDay} = Meter = get_value(Name),

OneMin1 = folsom_ewma:update(OneMin, Value),
FiveMin1 = folsom_ewma:update(FiveMin, Value),
FifteenMin1 = folsom_ewma:update(FifteenMin, Value),
OneDay1 = folsom_ewma:update(OneDay, Value),

ets:insert(?METER_TABLE, {Name, Meter#meter{count = Count + Value,
one = OneMin1,
five = FiveMin1,
fifteen = FifteenMin1}}).
fifteen = FifteenMin1,
day = OneDay1}}).

get_values(Name) ->
#meter{one = OneMin,
five = FiveMin,
fifteen = FifteenMin,
day = OneDay,
count = Count} = Meter = get_value(Name),

L = [
{count, Count},
{one, get_rate(OneMin)},
{five, get_rate(FiveMin)},
{fifteen, get_rate(FifteenMin)},
{day, get_rate(OneDay)},
{mean, get_mean_rate(Meter)},
{acceleration, get_acceleration(Name)}
],
@@ -183,12 +183,18 @@ check_metrics() ->
_ ->
error
end,
ok = case proplists:get_value(day, Meter) of
Value1 when Value1 > 0.005 ->
ok;
_ ->
error
end,

?debugFmt("checking meter reader~n", []),
MeterReader = folsom_metrics:get_metric_value(meter_reader),
?debugFmt("~p~n", [MeterReader]),
ok = case proplists:get_value(one, MeterReader) of
Value1 when Value1 > 1 ->
Value2 when Value2 > 1 ->
ok;
_ ->
error

0 comments on commit e1499f3

Please sign in to comment.