Improve FPS Profiler
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package com.bartlomiejpluta.base.util.profiler;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.LinkedList;
|
||||
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.groupingBy;
|
||||
|
||||
@Slf4j
|
||||
public class FPSProfiler {
|
||||
private static final Logger log = LoggerFactory.getLogger(FPSProfiler.class);
|
||||
|
||||
private static final int MOD = 30;
|
||||
private final int batchSize;
|
||||
private final List<Double> values = new LinkedList<>();
|
||||
private float fpsAccumulator = 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) {
|
||||
fpsAccumulator += dt;
|
||||
|
||||
if (++pointer % MOD == 0) {
|
||||
fps = pointer / fpsAccumulator;
|
||||
if (++pointer % batchSize == 0) {
|
||||
instantFPS = pointer / fpsAccumulator;
|
||||
fpsAccumulator = 0;
|
||||
pointer = 0;
|
||||
|
||||
values.add(fps);
|
||||
values.add(instantFPS);
|
||||
}
|
||||
}
|
||||
|
||||
public double currentFPS() {
|
||||
return fps;
|
||||
}
|
||||
|
||||
public void printResult() {
|
||||
public void logResult() {
|
||||
log.info("Min FPS: {}, max FPS: {}, avg FPS: {}",
|
||||
values.stream().min(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))
|
||||
.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