2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.onap.aai.sparky.analytics;
28 import static org.junit.Assert.assertEquals;
30 import java.security.SecureRandom;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.onap.aai.sparky.analytics.AveragingRingBuffer;
36 import org.powermock.modules.junit4.PowerMockRunner;
39 * The Class AveragingRingBufferTest.
41 @RunWith(PowerMockRunner.class)
42 public class AveragingRingBufferTest {
44 protected SecureRandom random = new SecureRandom();
49 * @throws Exception the exception
52 public void init() throws Exception {
53 // nothing at the moment
57 * Validate pre index roll averaging.
60 public void validatePreIndexRollAveraging() {
62 AveragingRingBuffer arb = new AveragingRingBuffer(5);
63 assertEquals(0, arb.getAvg());
66 * On initial buffer fill, the average will be re-calculated on the fly for the first nth data
67 * points until the data buffer has been filled the first time, and then the buffer
68 * automatically recalculates the average every time the buffer index rolls over, to the keep
69 * the average relative to the last "nth" data points.
72 // [ 1, 0, 0, 0, 0 ], sum = 1, avg = 1/1 =1
74 assertEquals(1, arb.getAvg());
76 // [ 1, 2, 0, 0, 0 ], sum = 3, avg = 3/2 = 1
78 assertEquals(1, arb.getAvg());
80 // [ 1, 2, 3, 0, 0 ], sum = 6, avg = 6/3 = 2
82 assertEquals(2, arb.getAvg());
84 // [ 1, 2, 3, 4, 0 ], sum = 10, avg = 10/4 = 2
86 assertEquals(2, arb.getAvg());
88 // [ 1, 2, 3, 4, 5 ], sum = 15, avg = 15/5 = 3
90 assertEquals(3, arb.getAvg());
95 * Validate post index roll averaging.
98 public void validatePostIndexRollAveraging() {
100 AveragingRingBuffer arb = new AveragingRingBuffer(5);
108 * The behavior switches, and now doesn't re-calculate the average until each nth data point, to
109 * reduce the computational over-head of re-calculating on each value.
112 // [ 10, 2, 3, 4, 5 ],
114 assertEquals(3, arb.getAvg());
116 // [ 10, 20, 3, 4, 5 ],
118 assertEquals(3, arb.getAvg());
120 // [ 10, 20, 30, 4, 5 ],
122 assertEquals(3, arb.getAvg());
124 // [ 10, 20, 30, 40, 5 ],
126 assertEquals(3, arb.getAvg());
128 // [ 10, 20, 30, 40, 50 ], s=150, avg=150/5=30
130 assertEquals(30, arb.getAvg());