10
10
11
11
public class SynchronizationTest {
12
12
13
- private void sleepUnchecked (int milliseconds ) {
13
+ private void sleepUnchecked (long milliseconds ) {
14
14
try {
15
15
Thread .sleep (milliseconds );
16
16
} catch (InterruptedException exception ) {
@@ -26,12 +26,6 @@ private boolean measureApproximateEquality(double expected, double actual, doubl
26
26
return expected + margin >= actual && expected - margin <= actual ;
27
27
}
28
28
29
- private void assertWithin (double expected , double actual , double margin ) {
30
- assertTrue (
31
- describeApproximateExpectation (expected , actual , margin ),
32
- measureApproximateEquality (expected , actual , margin ));
33
- }
34
-
35
29
private void assertWithin (String message , double expected , double actual , double margin ) {
36
30
assertTrue (
37
31
message + ": " + describeApproximateExpectation (expected , actual , margin ),
@@ -83,10 +77,10 @@ public void async_IfBotDelay_ThenClientBuffers() {
83
77
environment .configuration .asyncFrameBufferSize = 4 ;
84
78
85
79
environment .onFrame (1 , () -> {
86
- sleepUnchecked (40 );
80
+ sleepUnchecked (50 );
87
81
assertEquals ("Bot should be observing an old frame" , 1 , environment .bwClient .getGame ().getFrameCount ());
88
- assertEquals ("Client should be as far ahead as the frame buffer allows" , 4 , environment .liveGameData ().getFrameCount ());
89
- assertEquals ("Bot should be behind the live game" , 3 , environment .bwClient .framesBehind ());
82
+ assertEquals ("Client should be as far ahead as the frame buffer allows" , 5 , environment .liveGameData ().getFrameCount ());
83
+ assertEquals ("Bot should be behind the live game" , 4 , environment .bwClient .framesBehind ());
90
84
});
91
85
92
86
environment .onFrame (6 , () -> { // Maybe it should be possible to demand that these assertions pass a frame earlier?
@@ -161,17 +155,36 @@ public void async_IfFrameZeroWaitsDisabled_ThenClientBuffers() {
161
155
162
156
@ Test
163
157
public void async_MeasurePerformance_TotalFrameDuration () {
164
-
158
+ final int frames = 10 ;
159
+ final int frameSleep = 20 ;
160
+ SynchronizationEnvironment environment = new SynchronizationEnvironment ();
161
+ environment .configuration .async = true ;
162
+ environment .configuration .unlimitedFrameZero = true ;
163
+ environment .configuration .maxFrameDurationMs = frameSleep + 20 ;
164
+ IntStream .range (0 , frames ).forEach (i -> environment .onFrame (i , () -> {
165
+ sleepUnchecked (frameSleep );
166
+ }));
167
+ environment .runGame (frames );
168
+
169
+ // Assume copying accounts for almost all the frame time except what the bot uses
170
+ double meanCopy = environment .metrics ().copyingToBuffer .avgValue ;
171
+ assertWithin ("Total frame duration: Average" , environment .metrics ().totalFrameDuration .avgValue , meanCopy + frameSleep , MS_MARGIN );
165
172
}
166
173
167
174
@ Test
168
175
public void async_MeasurePerformance_CopyingToBuffer () {
169
-
170
- }
171
-
172
- @ Test
173
- public void async_MeasurePerformance_IntentionallyBlocking () {
174
-
176
+ // Somewhat lazy test; just verify that we're getting sane values
177
+ SynchronizationEnvironment environment = new SynchronizationEnvironment ();
178
+ environment .configuration .async = true ;
179
+ environment .runGame (20 );
180
+ final double minObserved = 2 ;
181
+ final double maxObserved = 15 ;
182
+ final double meanObserved = (minObserved + maxObserved ) / 2 ;
183
+ final double rangeObserved = (maxObserved - minObserved ) / 2 ;
184
+ assertWithin ("Copy to buffer: minimum" , environment .metrics ().copyingToBuffer .minValue , meanObserved , rangeObserved );
185
+ assertWithin ("Copy to buffer: maximum" , environment .metrics ().copyingToBuffer .maxValue , meanObserved , rangeObserved );
186
+ assertWithin ("Copy to buffer: average" , environment .metrics ().copyingToBuffer .avgValue , meanObserved , rangeObserved );
187
+ assertWithin ("Copy to buffer: previous" , environment .metrics ().copyingToBuffer .lastValue , meanObserved , rangeObserved );
175
188
}
176
189
177
190
@ Test
@@ -181,8 +194,7 @@ public void async_MeasurePerformance_FrameBufferSizeAndFramesBehind() {
181
194
environment .configuration .unlimitedFrameZero = true ;
182
195
environment .configuration .asyncFrameBufferSize = 3 ;
183
196
environment .configuration .maxFrameDurationMs = 20 ;
184
- environment .configuration .logVerbosely = true ;
185
-
197
+
186
198
environment .onFrame (5 , () -> {
187
199
assertWithin ("5: Frame buffer average" , 0 , environment .metrics ().frameBufferSize .avgValue , 0.1 );
188
200
assertWithin ("5: Frame buffer minimum" , 0 , environment .metrics ().frameBufferSize .minValue , 0.1 );
@@ -208,13 +220,8 @@ public void async_MeasurePerformance_FrameBufferSizeAndFramesBehind() {
208
220
environment .runGame (8 );
209
221
}
210
222
211
- @ Test
212
- public void async_MeasurePerformance_FlushSideEffects () {
213
-
214
- }
215
-
216
223
/**
217
- * Number of milliseconds of leeway to give in performance metrics.
224
+ * Number of milliseconds of leeway to give in potentially noisy performance metrics.
218
225
* Increase if tests are flaky due to variance in execution speed.
219
226
*/
220
227
private final static long MS_MARGIN = 10 ;
@@ -254,14 +261,48 @@ public void MeasurePerformance_BotResponse() {
254
261
}
255
262
256
263
@ Test
257
- public void async_MeasurePerformance_BwapiResponse () {
258
-
264
+ public void MeasurePerformance_BwapiResponse () {
265
+ final long bwapiDelayMs = 50 ;
266
+ SynchronizationEnvironment environment = new SynchronizationEnvironment ();
267
+ environment .setBwapiDelayMs (bwapiDelayMs );
268
+ environment .runGame ();
269
+ System .out .println (environment .metrics ());
270
+ assertWithin ("BWAPI Response: Average" , environment .metrics ().bwapiResponse .avgValue , bwapiDelayMs , MS_MARGIN );
259
271
}
260
272
261
273
@ Test
262
- public void async_MeasurePerformance_BotIdle () {
263
-
274
+ public void MeasurePerformance_BotIdle () {
275
+ final long bwapiDelayMs = 10 ;
276
+ final int frames = 10 ;
277
+ SynchronizationEnvironment environment = new SynchronizationEnvironment ();
278
+ environment .configuration .async = true ;
279
+ environment .configuration .asyncFrameBufferSize = 3 ;
280
+ environment .configuration .unlimitedFrameZero = true ;
281
+ environment .setBwapiDelayMs (bwapiDelayMs );
282
+ environment .runGame (frames );
283
+ double expected = environment .metrics ().copyingToBuffer .avgValue + bwapiDelayMs ;
284
+ assertWithin ("Bot Idle: Average" , environment .metrics ().botIdle .avgValue , expected , MS_MARGIN );
264
285
}
265
286
266
-
287
+ @ Test
288
+ public void async_MeasurePerformance_IntentionallyBlocking () {
289
+ SynchronizationEnvironment environment = new SynchronizationEnvironment ();
290
+ environment .configuration .async = true ;
291
+ environment .configuration .unlimitedFrameZero = true ;
292
+ environment .configuration .asyncFrameBufferSize = 2 ;
293
+ environment .configuration .maxFrameDurationMs = 20 ;
294
+ final int frameDelayMs = 100 ;
295
+ environment .onFrame (1 , () -> {
296
+ sleepUnchecked (100 );
297
+ });
298
+ environment .onFrame (2 , () -> {
299
+ assertWithin (
300
+ "2: Intentionally blocking previous" ,
301
+ environment .metrics ().intentionallyBlocking .lastValue ,
302
+ frameDelayMs - environment .configuration .asyncFrameBufferSize * environment .configuration .maxFrameDurationMs ,
303
+ MS_MARGIN );
304
+ sleepUnchecked (100 );
305
+ });
306
+ environment .runGame (3 );
307
+ }
267
308
}
0 commit comments