There is no need to start a second task to measure lag, it can in fact be measured with enough precision from the exisitng one.

This commit is contained in:
md_5 2012-05-17 20:33:40 +10:00
parent 1247401a5f
commit 3eac027ddb
5 changed files with 38 additions and 53 deletions

View File

@ -84,7 +84,7 @@ public class Essentials extends JavaPlugin implements IEssentials
private transient ExecuteTimer execTimer;
private transient I18n i18n;
private transient Metrics metrics;
private transient LagMeter lagMeter;
private transient EssentialsTimer timer;
@Override
public ISettings getSettings()
@ -239,12 +239,9 @@ public class Essentials extends JavaPlugin implements IEssentials
pm.registerEvents(tntListener, this);
final EssentialsTimer timer = new EssentialsTimer(this);
timer = new EssentialsTimer(this);
getScheduler().scheduleSyncRepeatingTask(this, timer, 1, 100);
lagMeter = new LagMeter();
getScheduler().scheduleSyncRepeatingTask(this, lagMeter, 0, 40);
Economy.setEss(this);
execTimer.mark("RegListeners");
@ -626,9 +623,10 @@ public class Essentials extends JavaPlugin implements IEssentials
return i18n;
}
public LagMeter getLagMeter()
@Override
public EssentialsTimer getTimer()
{
return lagMeter;
return timer;
}
private static class EssentialsWorldListener implements Listener, Runnable

View File

@ -2,6 +2,7 @@ package com.earth2me.essentials;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
import java.util.logging.Level;
import org.bukkit.entity.Player;
@ -11,6 +12,8 @@ public class EssentialsTimer implements Runnable
{
private final transient IEssentials ess;
private final transient Set<User> onlineUsers = new HashSet<User>();
private transient long lastPoll = System.currentTimeMillis() - 3000;
private final transient LinkedList<Float> history = new LinkedList<Float>();
EssentialsTimer(final IEssentials ess)
{
@ -21,6 +24,21 @@ public class EssentialsTimer implements Runnable
public void run()
{
final long currentTime = System.currentTimeMillis();
long timeSpent = (currentTime - lastPoll) / 1000;
if (timeSpent == 0)
{
timeSpent = 1;
}
if (history.size() > 10)
{
history.remove();
}
float tps = 100f / timeSpent;
if (tps <= 20)
{
history.add(tps);
}
lastPoll = currentTime;
for (Player player : ess.getServer().getOnlinePlayers())
{
try
@ -51,4 +69,17 @@ public class EssentialsTimer implements Runnable
user.resetInvulnerabilityAfterTeleport();
}
}
public float getAverageTPS()
{
float avg = 0;
for (Float f : history)
{
if (f != null)
{
avg += f;
}
}
return avg / history.size();
}
}

View File

@ -71,5 +71,5 @@ public interface IEssentials extends Plugin
void setMetrics(Metrics metrics);
LagMeter getLagMeter();
EssentialsTimer getTimer();
}

View File

@ -1,44 +0,0 @@
package com.earth2me.essentials;
import java.util.LinkedList;
public class LagMeter implements Runnable
{
private transient long lastPoll = System.currentTimeMillis() - 3000;
private final transient LinkedList<Float> history = new LinkedList<Float>();
@Override
public void run()
{
long now = System.currentTimeMillis();
long timeSpent = (now - lastPoll) / 1000;
if (timeSpent == 0)
{
timeSpent = 1;
}
if (history.size() > 10)
{
history.remove();
}
float tps = 40f / timeSpent;
if (tps <= 20)
{
history.add(tps);
}
lastPoll = now;
}
public float getAverageTPS()
{
float avg = 0;
for (Float f : history)
{
if (f != null)
{
avg += f;
}
}
return avg / history.size();
}
}

View File

@ -17,7 +17,7 @@ public class Commandgc extends EssentialsCommand
@Override
protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception
{
float tps = ess.getLagMeter().getAverageTPS();
float tps = ess.getTimer().getAverageTPS();
ChatColor color;
if (tps >= 18)
{