Skip to content

Commit be52a24

Browse files
committed
Fixed some collection and display of performance metrics
1 parent 3159f73 commit be52a24

File tree

5 files changed

+41
-37
lines changed

5 files changed

+41
-37
lines changed

src/main/java/bwapi/BWClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public Game getGame() {
2727
* Returns JBWAPI performance metrics.
2828
* Metrics will be mostly empty if metrics collection isn't timersEnabled in the bot configuration
2929
*/
30-
public PerformanceMetrics performanceMetrics() {
30+
public PerformanceMetrics getPerformanceMetrics() {
3131
return performanceMetrics;
3232
}
3333

src/main/java/bwapi/BWClientConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class BWClientConfiguration {
5353
* Enables collection of diagnostics.
5454
* When timersEnabled, JBWAPI collects and publishes performance metrics.
5555
*/
56-
public boolean diagnosePerformance = false;
56+
public boolean collectPerformanceMetrics = false;
5757

5858
/**
5959
* Checks that the configuration is in a valid state. Throws an IllegalArgumentException if it isn't.

src/main/java/bwapi/BotWrapper.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,25 @@ private Thread createBotThread() {
126126
frameBuffer.lockSize.unlock();
127127
}
128128
game.clientData().setBuffer(frameBuffer.peek());
129-
performanceMetrics.frameBufferSize.record(frameBuffer.framesBuffered());
129+
performanceMetrics.frameBufferSize.record(frameBuffer.framesBuffered() - 1);
130130
handleEvents();
131131
frameBuffer.dequeue();
132132
}
133133
});
134134
}
135135

136136
private void handleEvents() {
137-
performanceMetrics.botResponse.time(() -> {
138-
ClientData.GameData gameData = game.clientData().gameData();
139-
for (int i = 0; i < gameData.getEventCount(); i++) {
140-
ClientData.Event event = gameData.getEvents(i);
141-
EventHandler.operation(eventListener, game, event);
142-
if (event.getType() == EventType.MatchEnd) {
143-
gameOver = true;
144-
}
137+
ClientData.GameData gameData = game.clientData().gameData();
138+
if (gameData.getFrameCount() > 0 || ! configuration.asyncWaitOnFrameZero) {
139+
performanceMetrics.botResponse.startTiming();
140+
}
141+
for (int i = 0; i < gameData.getEventCount(); i++) {
142+
ClientData.Event event = gameData.getEvents(i);
143+
EventHandler.operation(eventListener, game, event);
144+
if (event.getType() == EventType.MatchEnd) {
145+
gameOver = true;
145146
}
146-
});
147+
}
148+
performanceMetrics.botResponse.stopTiming();
147149
}
148150
}

src/main/java/bwapi/PerformanceMetric.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public class PerformanceMetric {
66
private final String name;
7-
private final long maxThreshold;
7+
private final long maxAllowed;
88
public final boolean timerEnabled;
99

1010
public double minValue = Long.MAX_VALUE;
@@ -17,9 +17,9 @@ public class PerformanceMetric {
1717

1818
private long timeStarted = 0;
1919

20-
PerformanceMetric(String name, long maxThreshold, boolean timerEnabled) {
20+
PerformanceMetric(String name, long maxAllowed, boolean timerEnabled) {
2121
this.name = name;
22-
this.maxThreshold = maxThreshold;
22+
this.maxAllowed = maxAllowed;
2323
this.timerEnabled = timerEnabled;
2424
}
2525

@@ -40,20 +40,19 @@ void startTiming() {
4040
void stopTiming() {
4141
if (!timerEnabled) return;
4242
if (timeStarted <= 0) return;
43-
// Use nanosecond resolution timer,
44-
// and record in units of milliseconds.
43+
// Use nanosecond resolution timer, but record in units of milliseconds.
4544
long timeEnded = System.nanoTime();
4645
long timeDiff = timeEnded - timeStarted;
4746
timeStarted = 0;
48-
record(timeDiff / 1000d);
47+
record(timeDiff / 1000000d);
4948
}
5049

5150
void record(double value) {
5251
minValue = Math.min(minValue, value);
5352
maxValue = Math.max(maxValue, value);
5453
avgValue = (avgValue * samples + value) / (samples + 1d);
5554
++samples;
56-
if (value > maxThreshold) {
55+
if (value > maxAllowed) {
5756
avgValueExceeding = (avgValueExceeding * samplesExceeding + value) / (samplesExceeding + 1d);
5857
++samplesExceeding;
5958
}
@@ -64,22 +63,24 @@ public String toString() {
6463
DecimalFormat formatter = new DecimalFormat("###,###.#");
6564
return name
6665
+ ": "
67-
+ samples
68-
+ " averaging "
69-
+ formatter.format(avgValue)
70-
+ " ["
71-
+ formatter.format(minValue)
72-
+ " - "
73-
+ formatter.format(maxValue)
74-
+ "] over "
75-
+ samples
76-
+ " samples"
77-
+ (samplesExceeding > 0
78-
? ". "
79-
+ samplesExceeding
80-
+ " violations averaging "
81-
+ formatter.format(avgValueExceeding)
82-
: "")
66+
+ (samples > 0
67+
? formatter.format(samples)
68+
+ " samples averaging "
69+
+ formatter.format(avgValue)
70+
+ " ["
71+
+ formatter.format(minValue)
72+
+ " - "
73+
+ formatter.format(maxValue)
74+
+ "] over "
75+
+ samples
76+
+ " samples"
77+
+ (samplesExceeding > 0
78+
? ". "
79+
+ samplesExceeding
80+
+ " violations averaging "
81+
+ formatter.format(avgValueExceeding)
82+
: "")
83+
: "No samples")
8384
+ (interrupted > 0
8485
? ". Interrupted " + interrupted + " times"
8586
: "");

src/main/java/bwapi/PerformanceMetrics.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,21 @@ public class PerformanceMetrics {
5858
public PerformanceMetric botIdle;
5959

6060
public PerformanceMetrics(BWClientConfiguration configuration) {
61-
timersEnabled = configuration.diagnosePerformance;
61+
timersEnabled = configuration.collectPerformanceMetrics;
6262
final int frameDurationBufferMs = 5;
6363
final int sideEffectsBufferMs = 1;
6464
final int realTimeFrameMs = 42;
6565
totalFrameDuration = new PerformanceMetric("Total frame duration", configuration.asyncFrameDurationMs + frameDurationBufferMs, timersEnabled);
6666
copyingToBuffer = new PerformanceMetric("Time copying to buffer", 5, timersEnabled);
6767
intentionallyBlocking = new PerformanceMetric("Intentionally blocking", 0, timersEnabled);
68-
frameBufferSize = new PerformanceMetric("Frame buffer size", 0, timersEnabled);
68+
frameBufferSize = new PerformanceMetric("Frames buffered", 0, timersEnabled);
6969
flushSideEffects = new PerformanceMetric("Flush side effects", sideEffectsBufferMs , timersEnabled);
7070
botResponse = new PerformanceMetric("Bot Responses", configuration.asyncFrameDurationMs, timersEnabled);
7171
bwapiResponse = new PerformanceMetric("BWAPI Responses", realTimeFrameMs, timersEnabled);
7272
botIdle = new PerformanceMetric("Bot idle", Long.MAX_VALUE, timersEnabled);
7373
}
7474

75+
@Override
7576
public String toString() {
7677
return "Performance metrics:"
7778
+ "\n" + totalFrameDuration.toString()

0 commit comments

Comments
 (0)