Skip to content

Commit f006e84

Browse files
committed
Add FabricTPSRetriever for tracking and calculating TPS metrics
1 parent d1091d2 commit f006e84

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package it.renvins.serverpulse.fabric.metrics;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
import it.renvins.serverpulse.api.metrics.ITPSRetriever;
7+
import it.renvins.serverpulse.fabric.task.FabricScheduler;
8+
import it.renvins.serverpulse.fabric.task.FabricTask;
9+
import lombok.RequiredArgsConstructor;
10+
11+
@RequiredArgsConstructor
12+
public class FabricTPSRetriever implements ITPSRetriever {
13+
14+
private static final int TICK_PER_SECOND = 20;
15+
private static final int TICK_PER_MIN = TICK_PER_SECOND * 60; // 1200
16+
private static final int TICK_FIVE_MIN = TICK_PER_MIN * 5;
17+
private static final int TICK_FIFTEEN_MIN = TICK_PER_MIN * 15;
18+
19+
private static final int MAX_HISTORY_SIZE = TICK_FIFTEEN_MIN;
20+
21+
private final Queue<Long> tickHistory = new LinkedList<>();
22+
23+
private final FabricScheduler scheduler;
24+
25+
private long lastTickTimeNano = -1;
26+
27+
private double tps1m = 20.0;
28+
private double tps5m = 20.0;
29+
private double tps15m = 20.0;
30+
31+
@Override
32+
public double[] getTPS() {
33+
return new double[0];
34+
}
35+
36+
public void startTickMonitor() {
37+
lastTickTimeNano = System.nanoTime();
38+
39+
scheduler.runTaskTimerAsync(() -> {
40+
long currentTimeNano = System.nanoTime();
41+
long elapsedTime = currentTimeNano - lastTickTimeNano;
42+
lastTickTimeNano = currentTimeNano;
43+
44+
tickHistory.offer(elapsedTime);
45+
if (tickHistory.size() > MAX_HISTORY_SIZE) {
46+
tickHistory.poll();
47+
}
48+
}, 1, 1);
49+
}
50+
51+
public void calculateAverages() {
52+
// Calculate TPS for 1m, 5m, and 15m
53+
}
54+
55+
public double calculateTPSFromAvgNano(long totalNano, int count) {
56+
if (count == 0) {
57+
return 20.0;
58+
}
59+
double avgTickTimeMillis = (double) totalNano / count / 1_000_000.0;
60+
if (avgTickTimeMillis <= 0) {
61+
return 20.0;
62+
}
63+
// TPS: 1 second (1000ms) / avg tick time (ms)
64+
double tps = 1000.0 / avgTickTimeMillis;
65+
return Math.min(tps, 20.0);
66+
}
67+
}

0 commit comments

Comments
 (0)