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,36 +21,38 @@ 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 int count = 30;
private LinkedList<Double> meter = new LinkedList<>(); private double sum = 0;
private int count; private int sumCount = 0;
public TPSMeter(int count) { private void add(double tps) {
this.count = count; sum += tps;
} sumCount++;
if (sumCount > 20) {
public void add(double tps) { meter.addLast(sum);
meter.addLast(tps);
if (meter.size() > count) meter.removeFirst(); if (meter.size() > count) meter.removeFirst();
sum = 0;
sumCount = 0;
} }
public double get() {
double total = 0;
for (double d : meter) total += d;
return total / meter.size();
}
} }
private double now = 20.0; private double average(int count) {
private TPSMeter one_minute = new TPSMeter(1200); count = Math.min(count, meter.size());
private TPSMeter five_minutes = new TPSMeter(6000); if (count == 0) return 20.0;
private TPSMeter ten_minutes = new TPSMeter(12000); Iterator<Double> doubleIterator = meter.iterator();
double total = 0;
for (int i = 0; i < count; i++) {
total += doubleIterator.next();
}
return total / count;
}
private long lastTime = System.currentTimeMillis(); private long lastTime = System.currentTimeMillis();
@ -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() { public double getTPS(TpsAverage average) {
return one_minute.get(); return average(average.count);
}
public double get5TPS() {
return five_minutes.get();
}
public double get10TPS() {
return ten_minutes.get();
} }
} }