1
%% The contents of this file are subject to the Mozilla Public License
2
%% Version 1.1 (the "License"); you may not use this file except in
3
%% compliance with the License. You may obtain a copy of the License at
4
%% http://www.mozilla.org/MPL/
6
%% Software distributed under the License is distributed on an "AS IS"
7
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
8
%% License for the specific language governing rights and limitations
11
%% The Original Code is RabbitMQ Management Console.
13
%% The Initial Developer of the Original Code is VMware, Inc.
14
%% Copyright (c) 2010-2012 VMware, Inc. All rights reserved.
17
-module(rabbit_mgmt_test_db_unit).
19
-include("rabbit_mgmt.hrl").
20
-include_lib("eunit/include/eunit.hrl").
23
T = fun (Before, After) ->
24
?assertEqual(After, unstats(
26
cutoff(), stats(Before))))
28
%% Cut off old sample, move to base
29
T({[{8999, 123}, {9000, 456}], 0},
30
{[{9000, 456}], 123}),
31
%% Amalgamate old samples to rounder one
32
T({[{9001, 100}, {9010, 020}, {10000, 003}], 0},
34
%% The same, but a bit less
35
T({[{9000, 100}, {9901, 020}, {9910, 003}], 0},
36
{[{9000, 100}, {9910, 023}], 0}),
37
%% Nothing needs to be done
38
T({[{9000, 100}, {9990, 020}, {9991, 003}], 0},
39
{[{9000, 100}, {9990, 020}, {9991, 003}], 0}),
40
%% Invent a newer sample that's acceptable
41
T({[{9001, 10}, {9010, 02}], 0},
43
%% ...but don't if it's too old
44
T({[{8001, 10}, {8010, 02}], 0},
50
T = fun ({First, Last, Incr}, Stats, Results) ->
51
?assertEqual(format(Results),
52
rabbit_mgmt_stats:format(
53
#range{first = First * 1000,
60
%% Just three samples, all of which we format. Note the
61
%% instantaneous rate is taken from the penultimate sample.
62
T({10, 30, 10}, {[{10, 10}, {20, 20}, {30, 30}], 1},
63
{[{30, 61}, {20, 31}, {10, 11}], 2.0, 2.5, 103/3, 61}),
65
%% Skip over the second (and ditto).
66
T({10, 30, 20}, {[{10, 10}, {20, 20}, {30, 30}], 1},
67
{[{30, 61}, {10, 11}], 2.0, 2.5, 36.0, 61}),
69
%% Skip over some and invent some. Note that the instantaneous
70
%% rate drops to 0 since the last event is now in the past.
71
T({0, 40, 20}, {[{10, 10}, {20, 20}, {30, 30}], 1},
72
{[{40, 61}, {20, 31}, {0, 1}], 0.0, 1.5, 31.0, 61}),
74
%% And a case where the range starts after the samples
75
T({20, 40, 10}, {[{10, 10}, {20, 20}, {30, 30}], 1},
76
{[{40, 61}, {30, 61}, {20, 31}], 0.0, 1.5, 51.0, 61}),
78
%% A single sample - which should lead to some bits not getting generated
79
T({10, 10, 10}, {[{10, 10}, {20, 20}, {30, 30}], 1},
80
{[{10, 11}], 0.0, 11}),
82
%% No samples - which should also lead to some bits not getting generated
83
T({10, 0, 10}, {[{10, 10}, {20, 20}, {30, 30}], 1},
89
format_no_range_test() ->
91
T = fun (Stats, Results) ->
92
?assertEqual(format(Results),
93
rabbit_mgmt_stats:format(
94
no_range, stats(Stats), Interval * 1000))
98
T({[{10, 10}, {20, 20}, {30, 30}], 1},
103
%%--------------------------------------------------------------------
106
{[{10, 1}, {100, 10}, {1000, 100}], %% Sec
109
stats({Diffs, Base}) ->
110
#stats{diffs = gb_trees:from_orddict(secs_to_millis(Diffs)), base = Base}.
112
unstats(#stats{diffs = Diffs, base = Base}) ->
113
{millis_to_secs(gb_trees:to_list(Diffs)), Base}.
115
secs_to_millis(L) -> [{TS * 1000, S} || {TS, S} <- L].
116
millis_to_secs(L) -> [{TS div 1000, S} || {TS, S} <- L].
118
format({Rate, Count}) ->
122
format({Samples, Rate, Count}) ->
124
{samples, format_samples(Samples)}],
127
format({Samples, Rate, AvgRate, Avg, Count}) ->
129
{samples, format_samples(Samples)},
134
format_samples(Samples) ->
135
[[{sample, S}, {timestamp, TS * 1000}] || {TS, S} <- Samples].