SteamWar/SpigotCore
Archiviert
13
0

Optimize TPSWatcher

Dieser Commit ist enthalten in:
jojo 2020-09-27 17:27:00 +02:00
Ursprung 987f4f5477
Commit 3959e3c8b8

Datei anzeigen

@ -21,37 +21,39 @@ package de.steamwar.core;
import org.bukkit.Bukkit;
import java.util.Iterator;
import java.util.LinkedList;
public class TPSWatcher {
private static class TPSMeter {
private LinkedList<Double> meter = new LinkedList<>();
private int count;
private int count = 30;
public TPSMeter(int count) {
this.count = count;
}
private double sum = 0;
private int sumCount = 0;
public void add(double tps) {
meter.addLast(tps);
private void add(double tps) {
sum += tps;
sumCount++;
if (sumCount > 20) {
meter.addLast(sum);
if (meter.size() > count) meter.removeFirst();
sum = 0;
sumCount = 0;
}
}
public double get() {
private double average(int count) {
count = Math.min(count, meter.size());
if (count == 0) return 20.0;
Iterator<Double> doubleIterator = meter.iterator();
double total = 0;
for (double d : meter) total += d;
return total / meter.size();
for (int i = 0; i < count; i++) {
total += doubleIterator.next();
}
return total / count;
}
private double now = 20.0;
private TPSMeter one_minute = new TPSMeter(1200);
private TPSMeter five_minutes = new TPSMeter(6000);
private TPSMeter ten_minutes = new TPSMeter(12000);
private long lastTime = System.currentTimeMillis();
private static final double tickTimeDefault = 1000 / 20.0;
@ -62,31 +64,29 @@ public class TPSWatcher {
long tickTime = System.currentTimeMillis() - lastTime;
lastTime = System.currentTimeMillis();
double now = 0;
if (tickTime != 0) {
now = (tickTimeDefault / tickTime) * tickDefault;
} else {
now = 0;
}
one_minute.add(now);
five_minutes.add(now);
ten_minutes.add(now);
add(now);
}, 1, 1);
}
public double getTPS() {
return now;
public enum TpsAverage {
LAST_SECOND(1),
LAST_TEN_SECONDS(10),
LAST_THIRTY_SECONDS(30);
private int count;
TpsAverage(int count) {
this.count = count;
}
public double get1TPS() {
return one_minute.get();
}
public double get5TPS() {
return five_minutes.get();
}
public double get10TPS() {
return ten_minutes.get();
public double getTPS(TpsAverage average) {
return average(average.count);
}
}