PlotSquared/src/main/java/com/intellectualcrafters/plot/util/Lag.java

104 lines
3.7 KiB
Java
Raw Normal View History

2014-11-08 20:27:09 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// This program is free software; you can redistribute it and/or modify /
// it under the terms of the GNU General Public License as published by /
// the Free Software Foundation; either version 3 of the License, or /
// (at your option) any later version. /
// /
// This program is distributed in the hope that it will be useful, /
// but WITHOUT ANY WARRANTY; without even the implied warranty of /
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /
// GNU General Public License for more details. /
// /
// You should have received a copy of the GNU General Public License /
// along with this program; if not, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
2014-11-16 10:48:18 +01:00
package com.intellectualcrafters.plot.util;
2014-09-22 13:02:14 +02:00
/**
* TPS and Lag Checker.
*
2014-09-22 13:02:14 +02:00
* @author Citymonstret
*/
public class Lag implements Runnable {
2014-11-05 04:42:08 +01:00
/**
2014-11-21 23:45:46 +01:00
* Ticks
2014-11-05 04:42:08 +01:00
*/
2014-12-18 03:15:11 +01:00
public final static long[] T = new long[600];
2014-11-05 04:42:08 +01:00
/**
2014-11-21 23:45:46 +01:00
* Tick count
2014-11-05 04:42:08 +01:00
*/
2014-12-18 03:15:11 +01:00
public static int TC = 0;
2014-11-05 04:42:08 +01:00
/**
* something :_:
*/
@SuppressWarnings("unused")
2014-12-18 03:15:11 +01:00
public static long LT = 0L;
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* Get the server TPS
*
* @return server tick per second
*/
public static double getTPS() {
return Math.round(getTPS(100)) > 20.0D ? 20.0D : Math.round(getTPS(100));
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* Return the tick per second (measured in $ticks)
*
2014-12-18 03:15:11 +01:00
* @param ticks Ticks
*
2014-11-05 04:42:08 +01:00
* @return ticks per second
*/
public static double getTPS(final int ticks) {
if (TC < ticks) {
return 20.0D;
}
final int t = (TC - 1 - ticks) % T.length;
final long e = System.currentTimeMillis() - T[t];
return ticks / (e / 1000.0D);
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* Get number of ticks since
*
2014-12-18 03:15:11 +01:00
* @param tI Ticks <
*
2014-11-05 04:42:08 +01:00
* @return number of ticks since $tI
*/
public static long getElapsed(final int tI) {
final long t = T[tI % T.length];
return System.currentTimeMillis() - t;
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* Get lag percentage
*
* @return lag percentage
*/
public static double getPercentage() {
return Math.round((1.0D - (Lag.getTPS() / 20.0D)) * 100.0D);
}
2015-02-23 02:32:27 +01:00
2014-11-05 04:42:08 +01:00
/**
* Get TPS percentage (of 20)
*
* @return TPS percentage
*/
public static double getFullPercentage() {
return getTPS() * 5.0D;
}
2015-02-23 02:32:27 +01:00
2014-11-16 10:48:18 +01:00
@Override
public void run() {
T[TC % T.length] = System.currentTimeMillis();
TC++;
}
2014-09-22 13:02:14 +02:00
}