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 org.bukkit.Bukkit;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
public class TPSWatcher { public class TPSWatcher {
private static class TPSMeter {
private LinkedList<Double> meter = new LinkedList<>(); private LinkedList<Double> meter = new LinkedList<>();
private int count; private int count = 30;
public TPSMeter(int count) { private double sum = 0;
this.count = count; private int sumCount = 0;
}
public void add(double tps) { private void add(double tps) {
meter.addLast(tps); sum += tps;
sumCount++;
if (sumCount > 20) {
meter.addLast(sum);
if (meter.size() > count) meter.removeFirst(); 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; double total = 0;
for (double d : meter) total += d; for (int i = 0; i < count; i++) {
return total / meter.size(); 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 long lastTime = System.currentTimeMillis();
private static final double tickTimeDefault = 1000 / 20.0; private static final double tickTimeDefault = 1000 / 20.0;
@ -62,31 +64,29 @@ public class TPSWatcher {
long tickTime = System.currentTimeMillis() - lastTime; long tickTime = System.currentTimeMillis() - lastTime;
lastTime = System.currentTimeMillis(); lastTime = System.currentTimeMillis();
double now = 0;
if (tickTime != 0) { if (tickTime != 0) {
now = (tickTimeDefault / tickTime) * tickDefault; now = (tickTimeDefault / tickTime) * tickDefault;
} else {
now = 0;
} }
one_minute.add(now); add(now);
five_minutes.add(now);
ten_minutes.add(now);
}, 1, 1); }, 1, 1);
} }
public double getTPS() { public enum TpsAverage {
return now; 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() { public double getTPS(TpsAverage average) {
return five_minutes.get(); return average(average.count);
}
public double get10TPS() {
return ten_minutes.get();
} }
} }