Get server TPS

Discussion in 'Resources' started by LazyLemons, Apr 24, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    LazyLemons

    Ticks per second is the measure of a server's lag, essentially, here's a quick way to find it:
    Lag.java
    Code:
    public class Lag
      implements Runnable
    {
      public static int TICK_COUNT= 0;
      public static long[] TICKS= new long[600];
      public static long LAST_TICK= 0L;
     
      public static double getTPS()
      {
        return getTPS(100);
      }
     
      public static double getTPS(int ticks)
      {
        if (TICK_COUNT< ticks) {
          return 20.0D;
        }
        int target = (TICK_COUNT- 1 - ticks) % TICKS.length;
        long elapsed = System.currentTimeMillis() - TICKS[target];
     
        return ticks / (elapsed / 1000.0D);
      }
     
      public static long getElapsed(int tickID)
      {
        if (TICK_COUNT- tickID >= TICKS.length)
        {
        }
     
        long time = TICKS[(tickID % TICKS.length)];
        return System.currentTimeMillis() - time;
      }
     
      public void run()
      {
        TICKS[(TICK_COUNT% TICKS.length)] = System.currentTimeMillis();
     
        TICK_COUNT+= 1;
      }
    }
    Start the task in your onEnable() method :
    Code:
        Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Lag(), 100L, 1L);
    
    And to get the TPS whenever you need :
    Code:
    Lag.getTPS();
    
    Another nifty thing is to get the server lag percentage, like so:
    Code:
        double tps = Lag.getTPS();
        double lag = Math.round((1.0D - tps / 20.0D) * 100.0D);
    
     
  2. Offline

    skipperguy12

    Thanks for posting this, needed it!
     
  3. Offline

    Bammerbom

  4. Offline

    Plo124

    LazyLemons
    Cant you get the percent by doing TPS*5?
     
    KingFaris11 likes this.
  5. That makes sense, yes you probably can. Although what he is working out is the percentage lag, what you are working out is the percentage of TPS you're getting out of what the TPS should be (20).
    E.g.
    If you get 19TPS, his would result in: 5%
    Yours would result in: 95%
    His shows the percentage it is lagging by, whereas yours shows how... stable... it is I guess.

    Both can show percentage lag. What I would do to show the lag (which is what he does but even more simple) is:
    Code:
    double percentage =  Double.valueOf(String.format("%.2f", (100 - tps * 5)));
    If the TPS is 20, this would return 0, meaning 0% lag. If the TPS was 10, this would return 50, meaning 50% lag, and so on.
    Edit: The reason I did the String.format() bit was because we want it to be to two decimal places only. I did the same with TPS as otherwise it gives a long number, usually the TPS is 19.99999999 or so on most normal servers with a fair amount of plugins.

    Question: What's the point of the "LAST_TICK" variable if it's never being used?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  6. This code, sometimes return > 20 tps.
     
  7. Offline

    Nateb1121

    It could be that Bukkit is running at greater than 20 TPS. How erroneously off it is?
     
  8. Offline

    Plo124

    Obviously it means your server is running extra good!
    Jk,jk

    If you have essentials, do /lag to get the TPS
     
  9. Offline

    LazyLemons

    It's possible that it returns > 20 ticks if the server has recovered from a short lag spike and is making up for lost time.

    It was going be to used for something else, at this point it's just redundant.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  10. Offline

    SuperstarGamer

    Yes you could, But lets say you have 18.5 TPS, It would show as 92.5 Percent, to fix this, you would do as you were doing, but after that, subtract the result from 100, this will give you the lag percentage :)
     
Thread Status:
Not open for further replies.

Share This Page