-
Notifications
You must be signed in to change notification settings - Fork 340
/
SlidingWindowTimeReservoirTest.java
199 lines (157 loc) · 7.95 KB
/
SlidingWindowTimeReservoirTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Copyright (c) 2013, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.server.internal.monitoring;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Tests of {@link SlidingWindowTimeReservoir}.
*
* @author Stepan Vavra
*/
public class SlidingWindowTimeReservoirTest extends AbstractNanosReservoirTest {
@Test
public void testMultipleRequestsAtTheSameTimeZeroTime() {
testMultipleRequestsAtTheSameTime(0);
}
@Test
public void testMultipleRequestsAtTheSameTimeSystemTime() {
testMultipleRequestsAtTheSameTime(System.nanoTime());
}
private void testMultipleRequestsAtTheSameTime(final long now) {
final SlidingWindowTimeReservoir reservoir = slidingWindowTimeReservoir(now);
// put multiple requests at the beginning so that even the COLLISION_BUFFER bounds is tested
reservoirUpdateInNanos(reservoir, 10, now);
reservoirUpdateInNanos(reservoir, 20, now);
reservoirUpdateInNanos(reservoir, 30, now);
reservoirUpdateInNanos(reservoir, 40, now);
reservoirUpdateInNanos(reservoir, 50, now + 1);
// put multiple requests in the middle of the window
reservoirUpdateInNanos(reservoir, 60, now + 5);
reservoirUpdateInNanos(reservoir, 70, now + 5);
checkInNanos(reservoir, now + 5, 7, 10, 70, 40, 5);
// put multiple requests at the end of the window
reservoirUpdateInNanos(reservoir, 80, now + 10);
reservoirUpdateInNanos(reservoir, 90, now + 10);
checkInNanos(reservoir, now + 10, 9, 10, 90, 50);
// at 'now + 11' all the requests from 'now' should be gone
checkInNanos(reservoir, now + 11, 5, 50, 90, 70);
// at 'now + 12' the '50' value should be gone as well
checkInNanos(reservoir, now + 12, 4, 60, 90, 75);
}
@Test
public void testFirstUpdateOlderThanStartTimeZeroTime() {
testFirstUpdateOlderThanStartTime(0);
}
@Test
public void testFirstUpdateOlderThanStartTimeSystemTime() {
testFirstUpdateOlderThanStartTime(System.nanoTime());
}
@Test
public void testFirstUpdateOlderThanStartTimeMinTime() {
testFirstUpdateOlderThanStartTime(Long.MIN_VALUE);
}
private void testFirstUpdateOlderThanStartTime(final long now) {
final SlidingWindowTimeReservoir reservoir = slidingWindowTimeReservoir(now);
// put multiple requests at the beginning so that even the COLLISION_BUFFER bounds is tested
reservoirUpdateInNanos(reservoir, 10, now - 5);
reservoirUpdateInNanos(reservoir, 20, now - 4);
checkInNanos(reservoir, now, 2, 10, 20, 15, 5);
// put multiple requests at the end of the window
reservoirUpdateInNanos(reservoir, 30, now);
checkInNanos(reservoir, now, 3, 10, 30, 20, 5);
reservoirUpdateInNanos(reservoir, 40, now + 1);
checkInNanos(reservoir, now + 1, 4, 10, 40, 25, 6);
checkInNanos(reservoir, now + 5, 4, 10, 40, 25, 10);
checkInNanos(reservoir, now + 6, 3, 20, 40, 30, 10);
checkInNanos(reservoir, now + 7, 2, 30, 40, 35, 10);
checkInNanos(reservoir, now + 10, 2, 30, 40, 35, 10);
checkInNanos(reservoir, now + 11, 1, 40, 40, 40, 10);
checkInNanos(reservoir, now + 12, 0, 0, 0, 0, 10);
}
protected SlidingWindowTimeReservoir slidingWindowTimeReservoir(final long now) {
return new SlidingWindowTimeReservoir(10, TimeUnit.NANOSECONDS, now,
TimeUnit.NANOSECONDS);
}
@Test
public void testExhaustiveRequestsAtTheSameTimeZeroTime() {
testExhaustiveRequestsAtTheSameTime(0);
}
@Test
public void testExhaustiveRequestsAtTheSameTimeSystemTime() {
testExhaustiveRequestsAtTheSameTime(System.nanoTime());
}
@Test
public void testExhaustiveRequestsAtTheSameTimeMaxTime() {
testExhaustiveRequestsAtTheSameTime(Long.MAX_VALUE - 5);
}
/**
* This test exhaustively verifies the sliding window time reservoir. Step by step, basically all its capabilities are
* thoroughly tested.
*/
private void testExhaustiveRequestsAtTheSameTime(final long now) {
final SlidingWindowTimeReservoir reservoir = slidingWindowTimeReservoir(now);
// put multiple requests at the beginning so that even the COLLISION_BUFFER bounds is tested
for (int i = 0; i < COLLISION_BUFFER; ++i) {
reservoirUpdateInNanos(reservoir, 10, now);
}
// add one more request which should not fit into the collision buffer and will be thrown away
reservoirUpdateInNanos(reservoir, 999999, now);
// check again at 'now + 5' before we add more values
checkInNanos(reservoir, now + 5, COLLISION_BUFFER, 10, 10, 10, 5);
// put multiple requests in the middle of the window
for (int i = 0; i < COLLISION_BUFFER; ++i) {
reservoirUpdateInNanos(reservoir, 10, now + 5);
}
// add one more request which should not fit into the collision buffer and will be thrown away
reservoirUpdateInNanos(reservoir, 999999, now + 5);
for (int i = 0; i <= 5; ++i) {
// all the snapshots in past will return the same value as at 'now + 5'
checkInNanos(reservoir, now + i, COLLISION_BUFFER * 2, 10, 10, 10, 5);
}
// add a value in past, at 'now + 1', this will also help us test that we trim the reservoir correctly
reservoirUpdateInNanos(reservoir, 10, now + 1);
for (int i = 0; i <= 5; ++i) {
// all the snapshots in past will return the same value as at 'now + 5'
checkInNanos(reservoir, now + i, COLLISION_BUFFER * 2 + 1, 10, 10, 10, 5);
}
// put multiple requests at the end of the window
for (int i = 0; i < COLLISION_BUFFER; ++i) {
reservoirUpdateInNanos(reservoir, 10, now + 10);
}
// add one more request which should not fit into the collision buffer and will be thrown away
reservoirUpdateInNanos(reservoir, 999999, now + 10);
checkInNanos(reservoir, now + 10, COLLISION_BUFFER * 3 + 1, 10, 10, 10);
// at 'now + 11' all the requests from 'now' should be gone
checkInNanos(reservoir, now + 11, COLLISION_BUFFER * 2 + 1, 10, 10, 10);
// these values (from 'now') don't even fit into the reservoir, all the values will be thrown away
for (int i = 0; i < COLLISION_BUFFER + 1; ++i) {
reservoirUpdateInNanos(reservoir, 999999, now);
}
// check again at 'now + 11' to prove that values at 'now' weren't added at all
checkInNanos(reservoir, now + 11, COLLISION_BUFFER * 2 + 1, 10, 10, 10);
// at 'now + 12' the one additional request we added is gone
checkInNanos(reservoir, now + 12, COLLISION_BUFFER * 2, 10, 10, 10);
// at 'now + 15' it's the same as at 'now + 1'
checkInNanos(reservoir, now + 15, COLLISION_BUFFER * 2, 10, 10, 10);
// at 'now + 16' the values from 'now + 5' are gone
checkInNanos(reservoir, now + 16, COLLISION_BUFFER, 10, 10, 10);
// at 'now + 20' it's the the same as at 'now + 16'
checkInNanos(reservoir, now + 20, COLLISION_BUFFER, 10, 10, 10);
// at 'now + 21' all the requests are gone
checkInNanos(reservoir, now + 21, 0, 0, 0, 0);
}
}