From e02d52ce8d45550a4d77f11971e31cf0732e5f0c Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 4 Feb 2020 00:49:40 +0000 Subject: Monitor average tick durations & report them in /spark tps --- .../me/lucko/spark/forge/ForgeTickCounter.java | 59 ------------------ .../java/me/lucko/spark/forge/ForgeTickHook.java | 59 ++++++++++++++++++ .../me/lucko/spark/forge/ForgeTickReporter.java | 71 ++++++++++++++++++++++ .../spark/forge/plugin/ForgeClientSparkPlugin.java | 15 +++-- .../spark/forge/plugin/ForgeServerSparkPlugin.java | 15 +++-- 5 files changed, 152 insertions(+), 67 deletions(-) delete mode 100644 spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickCounter.java create mode 100644 spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickHook.java create mode 100644 spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickReporter.java (limited to 'spark-forge') diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickCounter.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickCounter.java deleted file mode 100644 index 7e387a7..0000000 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickCounter.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * This file is part of spark. - * - * Copyright (c) lucko (Luck) - * Copyright (c) contributors - * - * 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, see . - */ - -package me.lucko.spark.forge; - -import me.lucko.spark.common.sampler.AbstractTickCounter; -import me.lucko.spark.common.sampler.TickCounter; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.event.TickEvent; -import net.minecraftforge.eventbus.api.SubscribeEvent; - -public class ForgeTickCounter extends AbstractTickCounter implements TickCounter { - private final TickEvent.Type type; - - public ForgeTickCounter(TickEvent.Type type) { - this.type = type; - } - - @SubscribeEvent - public void onTick(TickEvent e) { - if (e.phase != TickEvent.Phase.START) { - return; - } - - if (e.type != this.type) { - return; - } - - onTick(); - } - - @Override - public void start() { - MinecraftForge.EVENT_BUS.register(this); - } - - @Override - public void close() { - MinecraftForge.EVENT_BUS.unregister(this); - } - -} diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickHook.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickHook.java new file mode 100644 index 0000000..910b5b5 --- /dev/null +++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickHook.java @@ -0,0 +1,59 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * 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, see . + */ + +package me.lucko.spark.forge; + +import me.lucko.spark.common.sampler.tick.AbstractTickHook; +import me.lucko.spark.common.sampler.tick.TickHook; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; + +public class ForgeTickHook extends AbstractTickHook implements TickHook { + private final TickEvent.Type type; + + public ForgeTickHook(TickEvent.Type type) { + this.type = type; + } + + @SubscribeEvent + public void onTick(TickEvent e) { + if (e.phase != TickEvent.Phase.START) { + return; + } + + if (e.type != this.type) { + return; + } + + onTick(); + } + + @Override + public void start() { + MinecraftForge.EVENT_BUS.register(this); + } + + @Override + public void close() { + MinecraftForge.EVENT_BUS.unregister(this); + } + +} diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickReporter.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickReporter.java new file mode 100644 index 0000000..8e0b36f --- /dev/null +++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeTickReporter.java @@ -0,0 +1,71 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * 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, see . + */ + +package me.lucko.spark.forge; + +import me.lucko.spark.common.sampler.tick.AbstractTickReporter; +import me.lucko.spark.common.sampler.tick.TickReporter; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; + +public class ForgeTickReporter extends AbstractTickReporter implements TickReporter { + private final TickEvent.Type type; + + private long start = 0; + + public ForgeTickReporter(TickEvent.Type type) { + this.type = type; + } + + @SubscribeEvent + public void onTick(TickEvent e) { + if (e.type != this.type) { + return; + } + + switch (e.phase) { + case START: + this.start = System.nanoTime(); + break; + case END: + if (this.start == 0) { + return; + } + + double duration = (System.nanoTime() - this.start) / 1000000d; + onTick(duration); + break; + default: + throw new AssertionError(e.phase); + } + } + + @Override + public void start() { + MinecraftForge.EVENT_BUS.register(this); + } + + @Override + public void close() { + MinecraftForge.EVENT_BUS.unregister(this); + } + +} diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java index 2b6220a..070c28a 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeClientSparkPlugin.java @@ -27,10 +27,12 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; -import me.lucko.spark.common.sampler.TickCounter; +import me.lucko.spark.common.sampler.tick.TickHook; +import me.lucko.spark.common.sampler.tick.TickReporter; import me.lucko.spark.forge.ForgeCommandSender; import me.lucko.spark.forge.ForgeSparkMod; -import me.lucko.spark.forge.ForgeTickCounter; +import me.lucko.spark.forge.ForgeTickHook; +import me.lucko.spark.forge.ForgeTickReporter; import net.minecraft.client.Minecraft; import net.minecraft.client.network.play.ClientPlayNetHandler; import net.minecraft.command.ICommandSource; @@ -132,8 +134,13 @@ public class ForgeClientSparkPlugin extends ForgeSparkPlugin implements Suggesti } @Override - public TickCounter createTickCounter() { - return new ForgeTickCounter(TickEvent.Type.CLIENT); + public TickHook createTickHook() { + return new ForgeTickHook(TickEvent.Type.CLIENT); + } + + @Override + public TickReporter createTickReporter() { + return new ForgeTickReporter(TickEvent.Type.CLIENT); } @Override diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java index a2aafb2..27489fb 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/plugin/ForgeServerSparkPlugin.java @@ -27,10 +27,12 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.suggestion.SuggestionProvider; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; -import me.lucko.spark.common.sampler.TickCounter; +import me.lucko.spark.common.sampler.tick.TickHook; +import me.lucko.spark.common.sampler.tick.TickReporter; import me.lucko.spark.forge.ForgeCommandSender; import me.lucko.spark.forge.ForgeSparkMod; -import me.lucko.spark.forge.ForgeTickCounter; +import me.lucko.spark.forge.ForgeTickHook; +import me.lucko.spark.forge.ForgeTickReporter; import net.minecraft.command.CommandSource; import net.minecraft.command.ICommandSource; import net.minecraft.entity.player.PlayerEntity; @@ -116,8 +118,13 @@ public class ForgeServerSparkPlugin extends ForgeSparkPlugin implements Command< } @Override - public TickCounter createTickCounter() { - return new ForgeTickCounter(TickEvent.Type.SERVER); + public TickHook createTickHook() { + return new ForgeTickHook(TickEvent.Type.SERVER); + } + + @Override + public TickReporter createTickReporter() { + return new ForgeTickReporter(TickEvent.Type.SERVER); } @Override -- cgit