1010
1111public class SynchronizationTest {
1212
13- private void sleepUnchecked (int milliseconds ) {
13+ private void sleepUnchecked (long milliseconds ) {
1414 try {
1515 Thread .sleep (milliseconds );
1616 } catch (InterruptedException exception ) {
@@ -26,12 +26,6 @@ private boolean measureApproximateEquality(double expected, double actual, doubl
2626 return expected + margin >= actual && expected - margin <= actual ;
2727 }
2828
29- private void assertWithin (double expected , double actual , double margin ) {
30- assertTrue (
31- describeApproximateExpectation (expected , actual , margin ),
32- measureApproximateEquality (expected , actual , margin ));
33- }
34-
3529 private void assertWithin (String message , double expected , double actual , double margin ) {
3630 assertTrue (
3731 message + ": " + describeApproximateExpectation (expected , actual , margin ),
@@ -83,10 +77,10 @@ public void async_IfBotDelay_ThenClientBuffers() {
8377 environment .configuration .asyncFrameBufferSize = 4 ;
8478
8579 environment .onFrame (1 , () -> {
86- sleepUnchecked (40 );
80+ sleepUnchecked (50 );
8781 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 ());
9084 });
9185
9286 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() {
161155
162156 @ Test
163157 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 );
165172 }
166173
167174 @ Test
168175 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 );
175188 }
176189
177190 @ Test
@@ -181,8 +194,7 @@ public void async_MeasurePerformance_FrameBufferSizeAndFramesBehind() {
181194 environment .configuration .unlimitedFrameZero = true ;
182195 environment .configuration .asyncFrameBufferSize = 3 ;
183196 environment .configuration .maxFrameDurationMs = 20 ;
184- environment .configuration .logVerbosely = true ;
185-
197+
186198 environment .onFrame (5 , () -> {
187199 assertWithin ("5: Frame buffer average" , 0 , environment .metrics ().frameBufferSize .avgValue , 0.1 );
188200 assertWithin ("5: Frame buffer minimum" , 0 , environment .metrics ().frameBufferSize .minValue , 0.1 );
@@ -208,13 +220,8 @@ public void async_MeasurePerformance_FrameBufferSizeAndFramesBehind() {
208220 environment .runGame (8 );
209221 }
210222
211- @ Test
212- public void async_MeasurePerformance_FlushSideEffects () {
213-
214- }
215-
216223 /**
217- * Number of milliseconds of leeway to give in performance metrics.
224+ * Number of milliseconds of leeway to give in potentially noisy performance metrics.
218225 * Increase if tests are flaky due to variance in execution speed.
219226 */
220227 private final static long MS_MARGIN = 10 ;
@@ -254,14 +261,48 @@ public void MeasurePerformance_BotResponse() {
254261 }
255262
256263 @ 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 );
259271 }
260272
261273 @ 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 );
264285 }
265286
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+ }
267308}
0 commit comments