From 6918e5edda68691c21e20d80597e0dbdc5a449ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Mon, 29 Mar 2021 20:28:29 +0200 Subject: [PATCH] Improve FPS Profiler --- .../base/util/profiler/FPSProfiler.java | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/api/src/main/java/com/bartlomiejpluta/base/util/profiler/FPSProfiler.java b/api/src/main/java/com/bartlomiejpluta/base/util/profiler/FPSProfiler.java index f4a2196a..0d06edf8 100644 --- a/api/src/main/java/com/bartlomiejpluta/base/util/profiler/FPSProfiler.java +++ b/api/src/main/java/com/bartlomiejpluta/base/util/profiler/FPSProfiler.java @@ -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 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); + } }