~ubuntu-branches/ubuntu/saucy/rabbitmq-server/saucy-proposed

« back to all changes in this revision

Viewing changes to plugins-src/rabbitmq-management/test/src/rabbit_mgmt_test_db_unit.erl

  • Committer: Package Import Robot
  • Author(s): Emile Joubert
  • Date: 2013-05-02 11:19:31 UTC
  • mfrom: (0.5.2) (0.1.37 sid)
  • Revision ID: package-import@ubuntu.com-20130502111931-xnoj0sejto2tewcj
Tags: 3.1.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/
 
5
%%
 
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
 
9
%%   under the License.
 
10
%%
 
11
%%   The Original Code is RabbitMQ Management Console.
 
12
%%
 
13
%%   The Initial Developer of the Original Code is VMware, Inc.
 
14
%%   Copyright (c) 2010-2012 VMware, Inc.  All rights reserved.
 
15
%%
 
16
 
 
17
-module(rabbit_mgmt_test_db_unit).
 
18
 
 
19
-include("rabbit_mgmt.hrl").
 
20
-include_lib("eunit/include/eunit.hrl").
 
21
 
 
22
gc_test() ->
 
23
    T = fun (Before, After) ->
 
24
                ?assertEqual(After, unstats(
 
25
                                      rabbit_mgmt_stats:gc(
 
26
                                        cutoff(), stats(Before))))
 
27
        end,
 
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},
 
33
      {[{10000, 123}], 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},
 
42
      {[{9100, 12}], 0}),
 
43
    %% ...but don't if it's too old
 
44
    T({[{8001, 10}, {8010, 02}], 0},
 
45
      {[], 12}),
 
46
    ok.
 
47
 
 
48
format_test() ->
 
49
    Interval = 10,
 
50
    T = fun ({First, Last, Incr}, Stats, Results) ->
 
51
                ?assertEqual(format(Results),
 
52
                             rabbit_mgmt_stats:format(
 
53
                               #range{first = First * 1000,
 
54
                                      last  = Last * 1000,
 
55
                                      incr  = Incr * 1000},
 
56
                               stats(Stats),
 
57
                               Interval * 1000))
 
58
        end,
 
59
 
 
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}),
 
64
 
 
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}),
 
68
 
 
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}),
 
73
 
 
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}),
 
77
 
 
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}),
 
81
 
 
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},
 
84
      {[], 0.0, 1}),
 
85
 
 
86
    %% TODO more?
 
87
    ok.
 
88
 
 
89
format_no_range_test() ->
 
90
    Interval = 10,
 
91
    T = fun (Stats, Results) ->
 
92
                ?assertEqual(format(Results),
 
93
                             rabbit_mgmt_stats:format(
 
94
                               no_range, stats(Stats), Interval * 1000))
 
95
        end,
 
96
 
 
97
    %% Just three samples
 
98
    T({[{10, 10}, {20, 20}, {30, 30}], 1},
 
99
      {0.0, 61}),
 
100
    ok.
 
101
 
 
102
 
 
103
%%--------------------------------------------------------------------
 
104
 
 
105
cutoff() ->
 
106
    {[{10, 1}, {100, 10}, {1000, 100}], %% Sec
 
107
     10000000}. %% Millis
 
108
 
 
109
stats({Diffs, Base}) ->
 
110
    #stats{diffs = gb_trees:from_orddict(secs_to_millis(Diffs)), base = Base}.
 
111
 
 
112
unstats(#stats{diffs = Diffs, base = Base}) ->
 
113
    {millis_to_secs(gb_trees:to_list(Diffs)), Base}.
 
114
 
 
115
secs_to_millis(L) -> [{TS * 1000, S} || {TS, S} <- L].
 
116
millis_to_secs(L) -> [{TS div 1000, S} || {TS, S} <- L].
 
117
 
 
118
format({Rate, Count}) ->
 
119
    {[{rate,     Rate}],
 
120
     Count};
 
121
 
 
122
format({Samples, Rate, Count}) ->
 
123
    {[{rate,     Rate},
 
124
      {samples,  format_samples(Samples)}],
 
125
     Count};
 
126
 
 
127
format({Samples, Rate, AvgRate, Avg, Count}) ->
 
128
    {[{rate,     Rate},
 
129
      {samples,  format_samples(Samples)},
 
130
      {avg_rate, AvgRate},
 
131
      {avg,      Avg}],
 
132
     Count}.
 
133
 
 
134
format_samples(Samples) ->
 
135
    [[{sample, S}, {timestamp, TS * 1000}] || {TS, S} <- Samples].