couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject [43/50] folsom commit: updated refs/heads/import-master to 4824aec
Date Thu, 31 Jul 2014 21:25:31 GMT
Added ability to resize sliding window


Project: http://git-wip-us.apache.org/repos/asf/couchdb-folsom/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-folsom/commit/5694f42d
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-folsom/tree/5694f42d
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-folsom/diff/5694f42d

Branch: refs/heads/import-master
Commit: 5694f42dff1f0b732a081cca3a29a96a7064d08e
Parents: 66071d9
Author: Vasco <vas@vas.io>
Authored: Sat Jan 4 18:45:35 2014 +0000
Committer: Vasco <vas@vas.io>
Committed: Sat Jan 4 18:45:35 2014 +0000

----------------------------------------------------------------------
 rebar                               | Bin 101515 -> 135173 bytes
 src/folsom_sample_slide.erl         |   7 +++-
 src/folsom_sample_slide_server.erl  |   9 +++++-
 src/folsom_sample_slide_uniform.erl |   7 +++-
 test/folsom_sample_slide_test.erl   |  53 ++++++++++++++++++++++++++++++-
 5 files changed, 72 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-folsom/blob/5694f42d/rebar
----------------------------------------------------------------------
diff --git a/rebar b/rebar
index 77abae6..860c19e 100755
Binary files a/rebar and b/rebar differ

http://git-wip-us.apache.org/repos/asf/couchdb-folsom/blob/5694f42d/src/folsom_sample_slide.erl
----------------------------------------------------------------------
diff --git a/src/folsom_sample_slide.erl b/src/folsom_sample_slide.erl
index 631d821..b82acf9 100644
--- a/src/folsom_sample_slide.erl
+++ b/src/folsom_sample_slide.erl
@@ -28,7 +28,8 @@
          update/2,
          get_values/1,
          moment/0,
-         trim/2
+         trim/2,
+	 resize/2
         ]).
 
 -include("folsom.hrl").
@@ -47,6 +48,10 @@ update(#slide{reservoir = Reservoir} = Sample, Value) ->
     ets:insert(Reservoir, {{Moment, Rnd}, Value}),
     Sample.
 
+resize(Sample, NewSize) ->
+    folsom_sample_slide_server:resize(Sample#slide.server, NewSize),
+    Sample#slide{window = NewSize}.
+
 get_values(#slide{window = Window, reservoir = Reservoir}) ->
     Oldest = moment() - Window,
     ets:select(Reservoir, [{{{'$1','_'},'$2'},[{'>=', '$1', Oldest}],['$2']}]).

http://git-wip-us.apache.org/repos/asf/couchdb-folsom/blob/5694f42d/src/folsom_sample_slide_server.erl
----------------------------------------------------------------------
diff --git a/src/folsom_sample_slide_server.erl b/src/folsom_sample_slide_server.erl
index 5986918..f02b4a2 100644
--- a/src/folsom_sample_slide_server.erl
+++ b/src/folsom_sample_slide_server.erl
@@ -31,7 +31,7 @@
 -behaviour(gen_server).
 
 %% API
--export([start_link/3, stop/1]).
+-export([start_link/3, stop/1, resize/2]).
 
 -record(state, {sample_mod,  reservoir, window}).
 
@@ -48,6 +48,13 @@ stop(Pid) ->
 init([SampleMod, Reservoir, Window]) ->
     {ok, #state{sample_mod = SampleMod, reservoir = Reservoir, window = Window}, timeout(Window)}.
 
+resize(Pid, NewWindow) ->
+    gen_server:call(Pid, {resize, NewWindow}).
+
+handle_call({resize, NewWindow}, _From, State) ->
+    NewState = State#state{window=NewWindow},
+    Reply = ok,
+    {reply, Reply, NewState, timeout(NewWindow)};
 handle_call(_Request, _From, State) ->
     Reply = ok,
     {reply, Reply, State}.

http://git-wip-us.apache.org/repos/asf/couchdb-folsom/blob/5694f42d/src/folsom_sample_slide_uniform.erl
----------------------------------------------------------------------
diff --git a/src/folsom_sample_slide_uniform.erl b/src/folsom_sample_slide_uniform.erl
index 1d0204c..d99373f 100644
--- a/src/folsom_sample_slide_uniform.erl
+++ b/src/folsom_sample_slide_uniform.erl
@@ -28,7 +28,8 @@
          update/2,
          get_values/1,
          moment/0,
-         trim/2
+         trim/2,
+	 resize/2
         ]).
 
 -include("folsom.hrl").
@@ -53,6 +54,10 @@ update(#slide_uniform{reservoir = Reservoir, size = Size} = Sample0, Value)
->
     end,
     Sample.
 
+resize(Sample, NewSize) ->
+    folsom_sample_slide_server:resize(Sample#slide.server, NewSize),
+    Sample#slide{window = NewSize}.
+
 maybe_update(Reservoir, {{_Moment, Rnd}, _Value}=Obj, Size) when Rnd =< Size ->
     ets:insert(Reservoir, Obj);
 maybe_update(_Reservoir, _Obj, _Size) ->

http://git-wip-us.apache.org/repos/asf/couchdb-folsom/blob/5694f42d/test/folsom_sample_slide_test.erl
----------------------------------------------------------------------
diff --git a/test/folsom_sample_slide_test.erl b/test/folsom_sample_slide_test.erl
index fa31a0e..dc08d95 100644
--- a/test/folsom_sample_slide_test.erl
+++ b/test/folsom_sample_slide_test.erl
@@ -27,7 +27,9 @@
 -include("folsom.hrl").
 
 -define(HISTO, test_slide).
+-define(HISTO2, test_slide2).
 -define(WINDOW, 30).
+-define(DOUBLE_WINDOW, 60).
 -define(RUNTIME, 90).
 -define(READINGS, 10).
 
@@ -41,7 +43,9 @@ slide_test_() ->
      [{"Create sliding window",
        fun create/0},
       {"test sliding window",
-       {timeout, 30, fun exercise/0}}
+       {timeout, 30, fun exercise/0}},
+      {"resize sliding window",
+       {timeout, 30, fun resize_window/0}}
      ]}.
 
 create() ->
@@ -87,6 +91,53 @@ exercise() ->
     check_table(Slide, []),
     ok.
 
+resize_window() ->
+    %% create a new histogram
+    %% will leave the trim server running, as resize() needs it
+    ok = folsom_metrics:new_histogram(?HISTO2, slide, ?WINDOW),
+    #histogram{sample=Slide} = folsom_metrics_histogram:get_value(?HISTO2),
+    Moments = lists:seq(1, ?RUNTIME ),
+    %% pump in 90 seconds worth of readings
+    Moment = lists:foldl(fun(_X, Tick) ->
+                                 Tock = tick(Tick),
+                                 [folsom_sample_slide:update(Slide, N) ||
+                                     N <- lists:duplicate(?READINGS, Tock)],
+                                 Tock end,
+                         0,
+                         Moments),
+    %% are all readings in the table?
+    check_table(Slide, Moments),
+    
+    %% get values only returns last ?WINDOW seconds
+    ExpectedValues = lists:sort(lists:flatten([lists:duplicate(?READINGS, N) ||
+                                                  N <- lists:seq(?RUNTIME - ?WINDOW, ?RUNTIME)])),
+    Values = lists:sort(folsom_sample_slide:get_values(Slide)),
+    ?assertEqual(ExpectedValues, Values),
+
+    %%resize the sliding window
+    NewSlide = folsom_sample_slide:resize(Slide, ?DOUBLE_WINDOW),
+
+    %% get values only returns last ?WINDOW*2 seconds
+    NewExpectedValues = lists:sort(lists:flatten([lists:duplicate(?READINGS, N) ||
+                                                  N <- lists:seq(?RUNTIME - ?DOUBLE_WINDOW,
?RUNTIME)])),
+    NewValues = lists:sort(folsom_sample_slide:get_values(NewSlide)),
+    ?assertEqual(NewExpectedValues, NewValues),
+    
+    
+    %% trim the table
+    Trimmed = folsom_sample_slide:trim(NewSlide#slide.reservoir, ?DOUBLE_WINDOW),
+    ?assertEqual((?RUNTIME - ?DOUBLE_WINDOW - 1) * ?READINGS, Trimmed),
+    check_table(NewSlide, lists:seq(?RUNTIME - ?DOUBLE_WINDOW, ?RUNTIME)),
+    %% increment the clock past the window
+    tick(Moment, ?DOUBLE_WINDOW*2),
+    %% get values should be empty
+    ?assertEqual([], folsom_sample_slide:get_values(NewSlide)),
+    %% trim, and table should be empty
+    Trimmed2 = folsom_sample_slide:trim(NewSlide#slide.reservoir, ?DOUBLE_WINDOW),
+    ?assertEqual((?RUNTIME * ?READINGS) - ((?RUNTIME - ?DOUBLE_WINDOW - 1) * ?READINGS),
Trimmed2),
+    check_table(NewSlide, []),
+    ok.
+
 tick(Moment0, IncrBy) ->
     Moment = Moment0 + IncrBy,
     meck:expect(folsom_utils, now_epoch, fun() ->


Mime
View raw message