Skip to content

Commit b40e612

Browse files
committed
Added performance metrics related to event count to diagnose whether 4.4 tournament module bugs are ongoing
1 parent ebae46c commit b40e612

File tree

4 files changed

+100
-24
lines changed

4 files changed

+100
-24
lines changed

src/main/java/bwapi/BWClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void startGame(BWClientConfiguration gameConfiguration) {
114114
if (!client.isConnected()) {
115115
return;
116116
}
117-
client.update();
117+
client.sendFrameReceiveFrame();
118118
if (liveGameData.isInGame()) {
119119
performanceMetrics = new PerformanceMetrics(configuration);
120120
botWrapper.startNewGame(client.mapFile(), performanceMetrics);
@@ -140,7 +140,7 @@ public void startGame(BWClientConfiguration gameConfiguration) {
140140
}
141141
}
142142

143-
client.update();
143+
client.sendFrameReceiveFrame();
144144
if (!client.isConnected()) {
145145
System.out.println("Reconnecting...");
146146
client.reconnect();

src/main/java/bwapi/Client.java

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,18 @@ boolean connect() {
230230
return true;
231231
}
232232

233-
void update() {
233+
void sendFrameReceiveFrame() {
234+
final PerformanceMetrics metrics = bwClient.getPerformanceMetrics();
234235

235236
// Tell BWAPI that we are done with the current frame
236-
bwClient.getPerformanceMetrics().frameDurationReceiveToSend.stopTiming();
237-
bwClient.getPerformanceMetrics().communicationSendToReceive.startTiming();
238-
bwClient.getPerformanceMetrics().communicationSendToSent.startTiming();
237+
metrics.frameDurationReceiveToSend.stopTiming();
238+
if (bwClient.doTime()) {
239+
metrics.communicationSendToReceive.startTiming();
240+
metrics.communicationSendToSent.startTiming();
241+
}
239242
try {
240-
pipeObjectHandle.writeByte(1); // The "frame done" signal
243+
// 1 is the "frame done" signal to BWAPI
244+
pipeObjectHandle.writeByte(1);
241245
}
242246
catch (Exception e) {
243247
System.err.println("failed, disconnecting");
@@ -247,15 +251,25 @@ void update() {
247251
disconnect();
248252
return;
249253
}
250-
bwClient.getPerformanceMetrics().communicationSendToSent.stopTiming();
251-
bwClient.getPerformanceMetrics().frameDurationReceiveToSent.stopTiming();
254+
metrics.communicationSendToSent.stopTiming();
255+
metrics.frameDurationReceiveToSent.stopTiming();
256+
metrics.frameDurationReceiveToSentGTC.stopTiming();
257+
if (bwClient.doTime()) {
258+
final int eventCount = clientData.gameData().getEventCount();
259+
metrics.numberOfEvents.record(eventCount);
260+
metrics.numberOfEventsTimesDurationReceiveToSent.record(eventCount * metrics.frameDurationReceiveToSent.runningTotal.last);
261+
metrics.numberOfEventsTimesDurationReceiveToSentGTC.record(eventCount * metrics.frameDurationReceiveToSentGTC.runningTotal.last);
262+
}
252263

253264
// Listen for BWAPI to indicate that a new frame is ready
254-
bwClient.getPerformanceMetrics().communicationListenToReceive.startTiming();
255-
byte code = 1;
256-
while (code != 2) { // The "frame ready" signal
265+
if (bwClient.doTime()) {
266+
metrics.communicationListenToReceive.startTiming();
267+
}
268+
boolean frameReady = false;
269+
while (!frameReady) {
257270
try {
258-
code = pipeObjectHandle.readByte();
271+
// 2 is the "frame ready" signal from BWAPI
272+
frameReady = pipeObjectHandle.readByte() == 2;
259273
} catch (Exception e) {
260274
System.err.println("failed, disconnecting");
261275
if (bwClient.getConfiguration().debugConnection) {
@@ -265,14 +279,19 @@ void update() {
265279
break;
266280
}
267281
}
268-
bwClient.getPerformanceMetrics().communicationListenToReceive.stopTiming();
269-
bwClient.getPerformanceMetrics().communicationSendToReceive.stopTiming();
282+
metrics.communicationListenToReceive.stopTiming();
283+
metrics.communicationSendToReceive.stopTiming();
270284

271285
if (bwClient.doTime()) {
272-
bwClient.getPerformanceMetrics().frameDurationReceiveToSend.startTiming();
273-
bwClient.getPerformanceMetrics().frameDurationReceiveToSent.startTiming();
274-
bwClient.getPerformanceMetrics().frameDurationReceiveToReceive.stopTiming();
275-
bwClient.getPerformanceMetrics().frameDurationReceiveToReceive.startTiming();
286+
metrics.frameDurationReceiveToSend.startTiming();
287+
metrics.frameDurationReceiveToSent.startTiming();
288+
metrics.frameDurationReceiveToSentGTC.startTiming();
289+
}
290+
metrics.frameDurationReceiveToReceive.stopTiming();
291+
metrics.frameDurationReceiveToReceiveGTC.stopTiming();
292+
if (bwClient.doTime()) {
293+
metrics.frameDurationReceiveToReceive.startTiming();
294+
metrics.frameDurationReceiveToReceiveGTC.startTiming();
276295
}
277296
}
278297

src/main/java/bwapi/PerformanceMetric.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package bwapi;
22

3+
import com.sun.jna.platform.win32.Kernel32;
4+
35
import java.text.DecimalFormat;
46
import java.util.ArrayList;
57

@@ -44,6 +46,7 @@ public String toString() {
4446
private final String name;
4547
private long timeStarted = 0;
4648
public int interrupted = 0;
49+
private boolean usingGetTickCount = false;
4750

4851
RunningTotal runningTotal = new RunningTotal();
4952
private ArrayList<Threshold> thresholds = new ArrayList<>();
@@ -79,6 +82,15 @@ void timeIf(boolean condition, Runnable runnable) {
7982
}
8083
}
8184

85+
PerformanceMetric useGetTickCount() {
86+
usingGetTickCount = true;
87+
return this;
88+
}
89+
90+
long getNanoseconds() {
91+
return usingGetTickCount ? Kernel32.INSTANCE.GetTickCount() * 1000000L: System.nanoTime();
92+
}
93+
8294
/**
8395
* Manually start timing.
8496
* The next call to stopTiming() will record the duration in fractional milliseconds.
@@ -87,7 +99,7 @@ void startTiming() {
8799
if (timeStarted > 0) {
88100
++interrupted;
89101
}
90-
timeStarted = System.nanoTime();
102+
timeStarted = getNanoseconds();
91103
}
92104

93105

@@ -98,7 +110,7 @@ void startTiming() {
98110
void stopTiming() {
99111
if (timeStarted <= 0) return;
100112
// Use nanosecond resolution timer, but record in units of milliseconds.
101-
long timeEnded = System.nanoTime();
113+
long timeEnded = getNanoseconds();
102114
long timeDiff = timeEnded - timeStarted;
103115
timeStarted = 0;
104116
record(timeDiff / 1000000d);

src/main/java/bwapi/PerformanceMetrics.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,26 @@ public class PerformanceMetrics {
2323
*/
2424
PerformanceMetric frameDurationReceiveToSent;
2525

26+
/**
27+
* Duration of a frame cycle originating at
28+
* the time when JBWAPI observes a new frame in shared memory.
29+
* Uses GetTickCount() instead of System.nanoRime()
30+
*/
31+
PerformanceMetric frameDurationReceiveToSentGTC;
32+
2633
/**
2734
* Duration of a frame cycle originating at
2835
* the time when JBWAPI observes a new frame in shared memory.
2936
*/
3037
PerformanceMetric frameDurationReceiveToReceive;
3138

39+
/**
40+
* Duration of a frame cycle originating at
41+
* the time when JBWAPI observes a new frame in shared memory.
42+
* Uses GetTickCount() instead of System.nanoRime()
43+
*/
44+
PerformanceMetric frameDurationReceiveToReceiveGTC;
45+
3246
/**
3347
* Time spent copying game data from system pipe shared memory to a frame buffer.
3448
* Applicable only in asynchronous mode.
@@ -110,6 +124,31 @@ public class PerformanceMetrics {
110124
*/
111125
PerformanceMetric negativeTimeDelta;
112126

127+
/**
128+
* The number of events sent by BWAPI each frame.
129+
* Might help diagnosing issues related to https://github.com/bwapi/bwapi/issues/860 and https://github.com/davechurchill/StarcraftAITournamentManager/issues/42
130+
*/
131+
PerformanceMetric numberOfEvents;
132+
133+
/**
134+
* The number of events sent by BWAPI each frame,
135+
* multiplied by the duration of time spent on that frame (receive-to-sent).
136+
* Might help diagnosing issues related to:
137+
* - https://github.com/bwapi/bwapi/issues/860
138+
* - https://github.com/davechurchill/StarcraftAITournamentManager/issues/42
139+
*/
140+
PerformanceMetric numberOfEventsTimesDurationReceiveToSent;
141+
142+
/**
143+
* The number of events sent by BWAPI each frame,
144+
* multiplied by the duration of time spent on that frame (receive-to-sent),
145+
* and using GetTickCount() instead of System.nanoTime().
146+
* Might help diagnosing issues related to:
147+
* - https://github.com/bwapi/bwapi/issues/860
148+
* - https://github.com/davechurchill/StarcraftAITournamentManager/issues/42
149+
*/
150+
PerformanceMetric numberOfEventsTimesDurationReceiveToSentGTC;
151+
113152
private BWClientConfiguration configuration;
114153
private ArrayList<PerformanceMetric> performanceMetrics = new ArrayList<>();
115154

@@ -120,9 +159,11 @@ public class PerformanceMetrics {
120159

121160
public void reset() {
122161
performanceMetrics.clear();
123-
frameDurationReceiveToSend = new PerformanceMetric(this, "Frame duration: Receiving 'frame ready' -> before sending 'frame done'", 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 85);
124-
frameDurationReceiveToSent = new PerformanceMetric(this, "Frame duration: Receiving 'frame ready' -> after sending 'frame done'", 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 85);
125-
frameDurationReceiveToReceive = new PerformanceMetric(this, "Frame duration: From BWAPI receive to BWAPI receive", 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 85);
162+
frameDurationReceiveToSend = new PerformanceMetric(this, "Frame duration: After receiving 'frame ready' -> before sending 'frame done'", 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 85);
163+
frameDurationReceiveToSent = new PerformanceMetric(this, "Frame duration: After receiving 'frame ready' -> after sending 'frame done'", 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 85);
164+
frameDurationReceiveToSentGTC = new PerformanceMetric(this, "Frame duration: After receiving 'frame ready' -> after sending 'frame done' (Using GetTickCount())", 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 85).useGetTickCount();
165+
frameDurationReceiveToReceive = new PerformanceMetric(this, "Frame duration: After receiving 'frame ready' -> receiving next 'frame ready'", 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 85);
166+
frameDurationReceiveToReceiveGTC = new PerformanceMetric(this, "Frame duration: After receiving 'frame ready' -> receiving next 'frame ready' (Using GetTickCount())", 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 85).useGetTickCount();
126167
communicationSendToReceive = new PerformanceMetric(this, "BWAPI duration: Before sending 'frame done' -> After receiving 'frame ready'", 1, 3, 5, 10, 15, 20, 30);
127168
communicationSendToSent = new PerformanceMetric(this, "BWAPI duration: Before sending 'frame done' -> After sending 'frame done'", 1, 3, 5, 10, 15, 20, 30);
128169
communicationListenToReceive = new PerformanceMetric(this, "BWAPI duration: Before listening for 'frame ready' -> After receiving 'frame ready'", 1, 3, 5, 10, 15, 20, 30);
@@ -137,6 +178,9 @@ public void reset() {
137178
excessSleep = new PerformanceMetric(this, "Excess duration of client sleep", 1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 85);
138179
positiveTimeDelta = new PerformanceMetric(this, "Positive timer discrepancy compared to BWAPI", 1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 85);
139180
negativeTimeDelta = new PerformanceMetric(this, "Negative timer discrepancy compared to BWAPI", 1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 85);
181+
numberOfEvents = new PerformanceMetric(this, "Number of events received from BWAPI", 1, 2, 3, 4, 5, 6, 8, 10, 15, 20);
182+
numberOfEventsTimesDurationReceiveToSent = new PerformanceMetric(this, "Number of events received from BWAPI, multiplied by the receive-to-sent duration of that frame", 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 85);
183+
numberOfEventsTimesDurationReceiveToSentGTC = new PerformanceMetric(this, "Number of events received from BWAPI, multiplied by the receive-to-sent duration of that frame (Using GetTickCount())", 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 85);
140184
}
141185

142186
void addMetric(PerformanceMetric performanceMetric) {
@@ -154,3 +198,4 @@ public String toString() {
154198
return outputBuilder.toString();
155199
}
156200
}
201+

0 commit comments

Comments
 (0)