Improve FPS Profiler
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
package com.bartlomiejpluta.base.util.profiler;
|
package com.bartlomiejpluta.base.util.profiler;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import lombok.Getter;
|
||||||
import org.slf4j.LoggerFactory;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -12,32 +12,33 @@ import static java.util.function.Function.identity;
|
|||||||
import static java.util.stream.Collectors.counting;
|
import static java.util.stream.Collectors.counting;
|
||||||
import static java.util.stream.Collectors.groupingBy;
|
import static java.util.stream.Collectors.groupingBy;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class FPSProfiler {
|
public class FPSProfiler {
|
||||||
private static final Logger log = LoggerFactory.getLogger(FPSProfiler.class);
|
private final int batchSize;
|
||||||
|
|
||||||
private static final int MOD = 30;
|
|
||||||
private final List<Double> values = new LinkedList<>();
|
private final List<Double> values = new LinkedList<>();
|
||||||
private float fpsAccumulator = 0;
|
private float fpsAccumulator = 0;
|
||||||
private int pointer = 0;
|
private int pointer = 0;
|
||||||
private double fps = 0;
|
|
||||||
|
@Getter
|
||||||
|
private double instantFPS = 0;
|
||||||
|
|
||||||
|
private FPSProfiler(int batchSize) {
|
||||||
|
this.batchSize = batchSize;
|
||||||
|
}
|
||||||
|
|
||||||
public void update(float dt) {
|
public void update(float dt) {
|
||||||
fpsAccumulator += dt;
|
fpsAccumulator += dt;
|
||||||
|
|
||||||
if (++pointer % MOD == 0) {
|
if (++pointer % batchSize == 0) {
|
||||||
fps = pointer / fpsAccumulator;
|
instantFPS = pointer / fpsAccumulator;
|
||||||
fpsAccumulator = 0;
|
fpsAccumulator = 0;
|
||||||
pointer = 0;
|
pointer = 0;
|
||||||
|
|
||||||
values.add(fps);
|
values.add(instantFPS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public double currentFPS() {
|
public void logResult() {
|
||||||
return fps;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void printResult() {
|
|
||||||
log.info("Min FPS: {}, max FPS: {}, avg FPS: {}",
|
log.info("Min FPS: {}, max FPS: {}, avg FPS: {}",
|
||||||
values.stream().min(Double::compareTo).orElse(-1.0),
|
values.stream().min(Double::compareTo).orElse(-1.0),
|
||||||
values.stream().max(Double::compareTo).orElse(-1.0),
|
values.stream().max(Double::compareTo).orElse(-1.0),
|
||||||
@@ -62,4 +63,8 @@ public class FPSProfiler {
|
|||||||
.sorted(comparingInt(Map.Entry::getKey))
|
.sorted(comparingInt(Map.Entry::getKey))
|
||||||
.forEach(e -> log.info("{} FPS: {}% ({} occurrences)", e.getKey(), e.getValue() * 100.0f / values.size(), e.getValue()));
|
.forEach(e -> log.info("{} FPS: {}% ({} occurrences)", e.getKey(), e.getValue() * 100.0f / values.size(), e.getValue()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static FPSProfiler create(int batchSize) {
|
||||||
|
return new FPSProfiler(batchSize);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user