Skip to content

Commit dcaaa12

Browse files
committed
Moved PerformanceMetric(s) properties behind getters
1 parent aa17f4f commit dcaaa12

File tree

8 files changed

+190
-126
lines changed

8 files changed

+190
-126
lines changed

src/main/java/bwapi/BWClient.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,10 @@ public void startGame(BWClientConfiguration gameConfiguration) {
124124
long ticksBefore = Kernel32.INSTANCE.GetTickCount();
125125

126126
botWrapper.onFrame();
127-
performanceMetrics.flushSideEffects.time(() -> getGame().sideEffects.flushTo(liveGameData));
128-
performanceMetrics.frameDurationReceiveToSend.stopTiming();
127+
performanceMetrics.getFlushSideEffects().time(() -> getGame().sideEffects.flushTo(liveGameData));
128+
performanceMetrics.getFrameDurationReceiveToSend().stopTiming();
129129
long ticksAfter = Kernel32.INSTANCE.GetTickCount();
130130

131-
// Measure differential between JVM timer and WinAPI's GetTickCount, used by BWAPI 4.4 and below
132-
if (doTime()) {
133-
long deltaTicks = ticksAfter - ticksBefore;
134-
long deltaMillis = (long) performanceMetrics.frameDurationReceiveToSend.runningTotal.last;
135-
long delta = deltaMillis - deltaTicks;
136-
if (delta > 0) {
137-
performanceMetrics.positiveTimeDelta.record(delta);
138-
} else if (delta < 0) {
139-
performanceMetrics.negativeTimeDelta.record(-delta);
140-
}
141-
}
142-
143131
client.sendFrameReceiveFrame();
144132
if (!client.isConnected()) {
145133
System.out.println("Reconnecting...");

src/main/java/bwapi/BWClientConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class BWClientConfiguration {
6969
/**
7070
* Checks that the configuration is in a valid state. Throws an IllegalArgumentException if it isn't.
7171
*/
72-
public void validate() {
72+
void validate() {
7373
if (async && maxFrameDurationMs < 0) {
7474
throw new IllegalArgumentException("maxFrameDurationMs needs to be a non-negative number (it's how long JBWAPI waits for a bot response before returning control to BWAPI).");
7575
}
@@ -78,7 +78,7 @@ public void validate() {
7878
}
7979
}
8080

81-
public void log(String value) {
81+
void log(String value) {
8282
if (logVerbosely) {
8383
System.out.println(value);
8484
}

src/main/java/bwapi/BotWrapper.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void onFrame() {
113113

114114
configuration.log("Main: Enqueued frame #" + frame);
115115
if (frame > 0) {
116-
performanceMetrics.clientIdle.startTiming();
116+
performanceMetrics.getClientIdle().startTiming();
117117
}
118118
frameBuffer.lockSize.lock();
119119
try {
@@ -147,13 +147,13 @@ void onFrame() {
147147
configuration.log("Main: Waiting " + remainingNanos / 1000000 + "ms for bot on frame #" + frame);
148148
frameBuffer.conditionSize.awaitNanos(remainingNanos);
149149
long excessNanos = Math.max(0, (System.nanoTime() - endNanos) / 1000000);
150-
performanceMetrics.excessSleep.record(excessNanos);
150+
performanceMetrics.getExcessSleep().record(excessNanos);
151151
}
152152
}
153153
} catch(InterruptedException ignored) {
154154
} finally {
155155
frameBuffer.lockSize.unlock();
156-
performanceMetrics.clientIdle.stopTiming();
156+
performanceMetrics.getClientIdle().stopTiming();
157157
configuration.log("Main: onFrame asynchronous end");
158158
}
159159
} else {
@@ -189,7 +189,7 @@ private Thread createBotThread() {
189189

190190
boolean doUnsafeRead = false;
191191
configuration.log("Bot: Ready for another frame");
192-
performanceMetrics.botIdle.startTiming();
192+
performanceMetrics.getBotIdle().startTiming();
193193
frameBuffer.lockSize.lock();
194194
try {
195195
doUnsafeRead = isUnsafeReadReady();
@@ -201,7 +201,7 @@ private Thread createBotThread() {
201201
} finally {
202202
frameBuffer.lockSize.unlock();
203203
}
204-
performanceMetrics.botIdle.stopTiming();
204+
performanceMetrics.getBotIdle().stopTiming();
205205

206206
if (doUnsafeRead) {
207207
configuration.log("Bot: Reading live frame");
@@ -243,10 +243,10 @@ private void handleEvents() {
243243
}
244244

245245
if (configuration.async) {
246-
performanceMetrics.framesBehind.record(Math.max(1, frameBuffer.framesBuffered()) - 1);
246+
performanceMetrics.getFramesBehind().record(Math.max(1, frameBuffer.framesBuffered()) - 1);
247247
}
248248

249-
performanceMetrics.botResponse.timeIf(
249+
performanceMetrics.getBotResponse().timeIf(
250250
! gameOver && (gameData.getFrameCount() > 0 || ! configuration.unlimitedFrameZero),
251251
() -> {
252252
for (int i = 0; i < gameData.getEventCount(); i++) {

src/main/java/bwapi/Client.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ void sendFrameReceiveFrame() {
234234
final PerformanceMetrics metrics = bwClient.getPerformanceMetrics();
235235

236236
// Tell BWAPI that we are done with the current frame
237-
metrics.frameDurationReceiveToSend.stopTiming();
237+
metrics.getFrameDurationReceiveToSend().stopTiming();
238238
if (bwClient.doTime()) {
239-
metrics.communicationSendToReceive.startTiming();
240-
metrics.communicationSendToSent.startTiming();
239+
metrics.getCommunicationSendToReceive().startTiming();
240+
metrics.getCommunicationSendToSent().startTiming();
241241
}
242242
try {
243243
// 1 is the "frame done" signal to BWAPI
@@ -251,19 +251,19 @@ void sendFrameReceiveFrame() {
251251
disconnect();
252252
return;
253253
}
254-
metrics.communicationSendToSent.stopTiming();
255-
metrics.frameDurationReceiveToSent.stopTiming();
256-
metrics.frameDurationReceiveToSentGTC.stopTiming();
254+
metrics.getCommunicationSendToSent().stopTiming();
255+
metrics.getFrameDurationReceiveToSent().stopTiming();
256+
metrics.getFrameDurationReceiveToSentGTC().stopTiming();
257257
if (bwClient.doTime()) {
258258
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);
259+
metrics.getNumberOfEvents().record(eventCount);
260+
metrics.getNumberOfEventsTimesDurationReceiveToSent().record(eventCount * metrics.getFrameDurationReceiveToSent().getRunningTotal().getLast());
261+
metrics.getNumberOfEventsTimesDurationReceiveToSentGTC().record(eventCount * metrics.getFrameDurationReceiveToSentGTC().getRunningTotal().getLast());
262262
}
263263

264264
// Listen for BWAPI to indicate that a new frame is ready
265265
if (bwClient.doTime()) {
266-
metrics.communicationListenToReceive.startTiming();
266+
metrics.getCommunicationListenToReceive().startTiming();
267267
}
268268
boolean frameReady = false;
269269
while (!frameReady) {
@@ -279,19 +279,19 @@ void sendFrameReceiveFrame() {
279279
break;
280280
}
281281
}
282-
metrics.communicationListenToReceive.stopTiming();
283-
metrics.communicationSendToReceive.stopTiming();
282+
metrics.getCommunicationListenToReceive().stopTiming();
283+
metrics.getCommunicationSendToReceive().stopTiming();
284284

285285
if (bwClient.doTime()) {
286-
metrics.frameDurationReceiveToSend.startTiming();
287-
metrics.frameDurationReceiveToSent.startTiming();
288-
metrics.frameDurationReceiveToSentGTC.startTiming();
286+
metrics.getFrameDurationReceiveToSend().startTiming();
287+
metrics.getFrameDurationReceiveToSent().startTiming();
288+
metrics.getFrameDurationReceiveToSentGTC().startTiming();
289289
}
290-
metrics.frameDurationReceiveToReceive.stopTiming();
291-
metrics.frameDurationReceiveToReceiveGTC.stopTiming();
290+
metrics.getFrameDurationReceiveToReceive().stopTiming();
291+
metrics.getFrameDurationReceiveToReceiveGTC().stopTiming();
292292
if (bwClient.doTime()) {
293-
metrics.frameDurationReceiveToReceive.startTiming();
294-
metrics.frameDurationReceiveToReceiveGTC.startTiming();
293+
metrics.getFrameDurationReceiveToReceive().startTiming();
294+
metrics.getFrameDurationReceiveToReceiveGTC().startTiming();
295295
}
296296
}
297297

src/main/java/bwapi/FrameBuffer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ void enqueueFrame() {
101101
try {
102102
while (full()) {
103103
configuration.log("Main: Waiting for frame buffer capacity");
104-
performanceMetrics.intentionallyBlocking.startTiming();
104+
performanceMetrics.getIntentionallyBlocking().startTiming();
105105
conditionSize.awaitUninterruptibly();
106106
}
107-
performanceMetrics.intentionallyBlocking.stopTiming();
107+
performanceMetrics.getIntentionallyBlocking().stopTiming();
108108
} finally { lockSize.unlock(); };
109109

110110
// For the first frame of the game, populate all buffers completely
@@ -115,15 +115,15 @@ void enqueueFrame() {
115115
copyBuffer(liveData, frameBuffer, true);
116116
}
117117
} else {
118-
performanceMetrics.copyingToBuffer.time(() -> {
118+
performanceMetrics.getCopyingToBuffer().time(() -> {
119119
ByteBuffer dataTarget = dataBuffer.get(indexGame());
120120
copyBuffer(liveData, dataTarget, false);
121121
});
122122
}
123123

124124
lockSize.lock();
125125
try {
126-
performanceMetrics.frameBufferSize.record(framesBuffered());
126+
performanceMetrics.getFrameBufferSize().record(framesBuffered());
127127
++stepGame;
128128
conditionSize.signalAll();
129129
} finally { lockSize.unlock(); }

src/main/java/bwapi/PerformanceMetric.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,34 @@
99
* Aggregates labeled time series data.
1010
*/
1111
public class PerformanceMetric {
12-
class RunningTotal {
13-
int samples = 0;
14-
double last = 0d;
15-
double mean = 0d;
16-
double min = Long.MAX_VALUE;
17-
double max = Long.MIN_VALUE;
12+
public class RunningTotal {
13+
private int samples = 0;
14+
private double last = 0d;
15+
private double mean = 0d;
16+
private double min = Long.MAX_VALUE;
17+
private double max = Long.MIN_VALUE;
1818
void record(double value) {
1919
last = value;
2020
min = Math.min(min, value);
2121
max = Math.max(max, value);
2222
mean = (mean * samples + value) / (samples + 1d);
2323
++samples;
2424
}
25+
public double getSamples() {
26+
return samples;
27+
}
28+
public double getLast() {
29+
return last;
30+
}
31+
public double getMean() {
32+
return mean;
33+
}
34+
public double getMin() {
35+
return min;
36+
}
37+
public double getMax() {
38+
return max;
39+
}
2540
}
2641
class Threshold {
2742
double threshold;
@@ -45,10 +60,10 @@ public String toString() {
4560

4661
private final String name;
4762
private long timeStarted = 0;
48-
public int interrupted = 0;
63+
private int interrupted = 0;
4964
private boolean usingGetTickCount = false;
5065

51-
RunningTotal runningTotal = new RunningTotal();
66+
private final RunningTotal runningTotal = new RunningTotal();
5267
private ArrayList<Threshold> thresholds = new ArrayList<>();
5368

5469
PerformanceMetric(PerformanceMetrics metrics, String name, double... thresholds) {
@@ -59,6 +74,14 @@ public String toString() {
5974
metrics.addMetric(this);
6075
}
6176

77+
public RunningTotal getRunningTotal() {
78+
return runningTotal;
79+
}
80+
81+
public int getInterrupted() {
82+
return interrupted;
83+
}
84+
6285
/**
6386
* Records the duration of a function call.
6487
* @param runnable The function to time

0 commit comments

Comments
 (0)