|
| 1 | +package me.flashyreese.mods.sodiumextra.client; |
| 2 | + |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.Comparator; |
| 5 | +import java.util.Deque; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +public class FrameCounter { |
| 9 | + private static final FrameCounter INSTANCE = new FrameCounter(); |
| 10 | + private final Deque<FrameSample> samples = new ArrayDeque<>(); |
| 11 | + private final long smoothWindow = 500_000_000L; // 0.5 seconds in nanoseconds |
| 12 | + private final long windowNanos = 5_000_000_000L; // 5 seconds in nanoseconds |
| 13 | + private final long updateIntervalNanos = 500_000_000L; // 0.5 seconds in nanoseconds |
| 14 | + private long lastFrameTime = -1; |
| 15 | + private long lastUpdateTime = 0; |
| 16 | + private double cachedSmoothFps = 0; |
| 17 | + private double cachedAverageFps = 0; |
| 18 | + private double cachedOnePercentLowFps = 0; |
| 19 | + private double cachedPointOnePercentLowFps = 0; |
| 20 | + |
| 21 | + public static FrameCounter getInstance() { |
| 22 | + return FrameCounter.INSTANCE; |
| 23 | + } |
| 24 | + |
| 25 | + public synchronized void onFrame() { |
| 26 | + long now = System.nanoTime(); |
| 27 | + |
| 28 | + // Record new frame delta |
| 29 | + if (this.lastFrameTime != -1) { |
| 30 | + long delta = now - this.lastFrameTime; |
| 31 | + this.samples.addLast(new FrameSample(now, delta)); |
| 32 | + } |
| 33 | + this.lastFrameTime = now; |
| 34 | + |
| 35 | + // Trim old samples |
| 36 | + while (!this.samples.isEmpty() && now - this.samples.peekFirst().timestamp > this.windowNanos) { |
| 37 | + this.samples.removeFirst(); |
| 38 | + } |
| 39 | + |
| 40 | + // Throttle stat computation |
| 41 | + if (now - this.lastUpdateTime >= this.updateIntervalNanos) { |
| 42 | + this.lastUpdateTime = now; |
| 43 | + if (!this.samples.isEmpty()) { |
| 44 | + long totalNanos = this.samples.stream().mapToLong(s -> s.deltaNanos).sum(); |
| 45 | + this.cachedAverageFps = (double) (this.samples.size() * 1_000_000_000L) / totalNanos; |
| 46 | + this.cachedOnePercentLowFps = this.computePercentileLow(1.0); |
| 47 | + this.cachedPointOnePercentLowFps = this.computePercentileLow(0.1); |
| 48 | + this.cachedSmoothFps = this.computeSmoothFpsFromRecentFrames(now); |
| 49 | + } else { |
| 50 | + this.cachedAverageFps = this.cachedOnePercentLowFps = this.cachedPointOnePercentLowFps = this.cachedSmoothFps = 0; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private double computeSmoothFpsFromRecentFrames(long now) { |
| 56 | + List<Long> recent = this.samples.stream() |
| 57 | + .filter(s -> now - s.timestamp <= this.smoothWindow) |
| 58 | + .map(s -> s.deltaNanos) |
| 59 | + .toList(); |
| 60 | + |
| 61 | + if (recent.isEmpty()) return 0; |
| 62 | + |
| 63 | + double avgDelta = recent.stream().mapToLong(Long::longValue).average().orElse(0); |
| 64 | + return 1_000_000_000.0 / avgDelta; |
| 65 | + } |
| 66 | + |
| 67 | + public synchronized int getSmoothFps() { |
| 68 | + return (int) Math.round(this.cachedSmoothFps); |
| 69 | + } |
| 70 | + |
| 71 | + public synchronized int getAverageFps() { |
| 72 | + return (int) Math.round(this.cachedAverageFps); |
| 73 | + } |
| 74 | + |
| 75 | + public synchronized int getOnePercentLowFps() { |
| 76 | + return (int) Math.round(this.cachedOnePercentLowFps); |
| 77 | + } |
| 78 | + |
| 79 | + public synchronized int getPointOnePercentLowFps() { |
| 80 | + return (int) Math.round(this.cachedPointOnePercentLowFps); |
| 81 | + } |
| 82 | + |
| 83 | + private double computePercentileLow(double percent) { |
| 84 | + if (this.samples.isEmpty()) return 0; |
| 85 | + List<Long> deltas = this.samples.stream() |
| 86 | + .map(s -> s.deltaNanos) |
| 87 | + .sorted(Comparator.reverseOrder()) // slowest frames first |
| 88 | + .toList(); |
| 89 | + |
| 90 | + int count = Math.max(1, (int) Math.ceil(deltas.size() * (percent / 100.0))); |
| 91 | + long sum = 0; |
| 92 | + for (int i = 0; i < count; i++) sum += deltas.get(i); |
| 93 | + double avgDelta = sum / (double) count; |
| 94 | + return 1_000_000_000.0 / avgDelta; |
| 95 | + } |
| 96 | + |
| 97 | + private record FrameSample(long timestamp, long deltaNanos) { |
| 98 | + } |
| 99 | +} |
0 commit comments