implement TerraPlugin#getProfier

This commit is contained in:
dfsek 2021-04-12 00:05:53 -07:00
parent 4eadbb7d83
commit b3e3c28276
7 changed files with 56 additions and 22 deletions

View File

@ -3,8 +3,6 @@ package com.dfsek.terra.profiler;
import java.util.Map; import java.util.Map;
public interface Profiler { public interface Profiler {
ProfilerImpl INSTANCE = new ProfilerImpl();
void push(String frame); void push(String frame);
void pop(String frame); void pop(String frame);

View File

@ -13,10 +13,12 @@ public class ProfilerImpl implements Profiler {
private static final ThreadLocal<Map<String, List<Long>>> TIMINGS = ThreadLocal.withInitial(HashMap::new); private static final ThreadLocal<Map<String, List<Long>>> TIMINGS = ThreadLocal.withInitial(HashMap::new);
private final List<Map<String, List<Long>>> accessibleThreadMaps = new ArrayList<>(); private final List<Map<String, List<Long>>> accessibleThreadMaps = new ArrayList<>();
private volatile boolean running = false; private volatile boolean running = false;
private static boolean instantiated = false;
public ProfilerImpl() { public ProfilerImpl() {
if(instantiated) throw new IllegalStateException("Only one instance of Profiler may exist!");
instantiated = true;
} }
@Override @Override
@ -64,16 +66,14 @@ public class ProfilerImpl implements Profiler {
@Override @Override
public Map<String, Timings> getTimings() { public Map<String, Timings> getTimings() {
Map<String, Timings> map = new HashMap<>(); Map<String, Timings> map = new HashMap<>();
accessibleThreadMaps.forEach(smap -> { accessibleThreadMaps.forEach(smap -> smap.forEach((key, list) -> {
smap.forEach((key, list) -> {
String[] keys = key.split("\\."); String[] keys = key.split("\\.");
Timings timings = map.computeIfAbsent(keys[0], id -> new Timings()); Timings timings = map.computeIfAbsent(keys[0], id -> new Timings());
for(int i = 1; i < keys.length; i++) { for(int i = 1; i < keys.length; i++) {
timings = timings.getSubItem(keys[i]); timings = timings.getSubItem(keys[i]);
} }
list.forEach(timings::addTime); list.forEach(timings::addTime);
}); }));
});
return map; return map;
} }
} }

View File

@ -1,11 +1,13 @@
package profiler; package profiler;
import com.dfsek.terra.profiler.Profiler; import com.dfsek.terra.profiler.Profiler;
import com.dfsek.terra.profiler.ProfilerImpl;
public class ProfilerTest { public class ProfilerTest {
private static final Profiler PROFILER = new ProfilerImpl();
//@Test //@Test
public static void main(String... a) throws InterruptedException { public static void main(String... a) throws InterruptedException {
Profiler.INSTANCE.start(); PROFILER.start();
for(int i = 0; i < 1000; i++) { for(int i = 0; i < 1000; i++) {
doThing(); doThing();
} }
@ -17,29 +19,29 @@ public class ProfilerTest {
for(int i = 0; i < 100; i++) { for(int i = 0; i < 100; i++) {
doOtherThing(); doOtherThing();
} }
Profiler.INSTANCE.stop(); PROFILER.stop();
Profiler.INSTANCE.getTimings().forEach((id, timings) -> { PROFILER.getTimings().forEach((id, timings) -> {
System.out.println(id + ": " + timings.toString()); System.out.println(id + ": " + timings.toString());
}); });
} }
private static void doThing() throws InterruptedException { private static void doThing() throws InterruptedException {
Profiler.INSTANCE.push("thing"); PROFILER.push("thing");
Thread.sleep(1); Thread.sleep(1);
doOtherThing(); doOtherThing();
Profiler.INSTANCE.pop("thing"); PROFILER.pop("thing");
} }
private static void doOtherThing() throws InterruptedException { private static void doOtherThing() throws InterruptedException {
Profiler.INSTANCE.push("thing2"); PROFILER.push("thing2");
Thread.sleep(2); Thread.sleep(2);
doThirdOtherThing(); doThirdOtherThing();
Profiler.INSTANCE.pop("thing2"); PROFILER.pop("thing2");
} }
private static void doThirdOtherThing() throws InterruptedException { private static void doThirdOtherThing() throws InterruptedException {
Profiler.INSTANCE.push("thing3"); PROFILER.push("thing3");
Thread.sleep(2); Thread.sleep(2);
Profiler.INSTANCE.pop("thing3"); PROFILER.pop("thing3");
} }
} }

View File

@ -40,6 +40,8 @@ import com.dfsek.terra.config.PluginConfig;
import com.dfsek.terra.config.lang.LangUtil; import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.config.lang.Language; import com.dfsek.terra.config.lang.Language;
import com.dfsek.terra.config.pack.ConfigPack; import com.dfsek.terra.config.pack.ConfigPack;
import com.dfsek.terra.profiler.Profiler;
import com.dfsek.terra.profiler.ProfilerImpl;
import com.dfsek.terra.registry.master.AddonRegistry; import com.dfsek.terra.registry.master.AddonRegistry;
import com.dfsek.terra.registry.master.ConfigRegistry; import com.dfsek.terra.registry.master.ConfigRegistry;
import com.dfsek.terra.world.TerraWorld; import com.dfsek.terra.world.TerraWorld;
@ -64,6 +66,8 @@ public class TerraBukkitPlugin extends JavaPlugin implements TerraPlugin {
private final Map<World, TerraWorld> worldMap = new HashMap<>(); private final Map<World, TerraWorld> worldMap = new HashMap<>();
private final Map<String, ConfigPack> worlds = new HashMap<>(); private final Map<String, ConfigPack> worlds = new HashMap<>();
private final Profiler profiler = new ProfilerImpl();
private final ConfigRegistry registry = new ConfigRegistry(); private final ConfigRegistry registry = new ConfigRegistry();
private final CheckedRegistry<ConfigPack> checkedRegistry = new CheckedRegistry<>(registry); private final CheckedRegistry<ConfigPack> checkedRegistry = new CheckedRegistry<>(registry);
@ -141,6 +145,11 @@ public class TerraBukkitPlugin extends JavaPlugin implements TerraPlugin {
Bukkit.getScheduler().runTask(this, task); Bukkit.getScheduler().runTask(this, task);
} }
@Override
public Profiler getProfiler() {
return profiler;
}
@Override @Override
public void onDisable() { public void onDisable() {
BukkitChunkGeneratorWrapper.saveAll(); BukkitChunkGeneratorWrapper.saveAll();

View File

@ -46,6 +46,8 @@ import com.dfsek.terra.fabric.world.TerraBiomeSource;
import com.dfsek.terra.fabric.world.features.PopulatorFeature; import com.dfsek.terra.fabric.world.features.PopulatorFeature;
import com.dfsek.terra.fabric.world.generator.FabricChunkGenerator; import com.dfsek.terra.fabric.world.generator.FabricChunkGenerator;
import com.dfsek.terra.fabric.world.generator.FabricChunkGeneratorWrapper; import com.dfsek.terra.fabric.world.generator.FabricChunkGeneratorWrapper;
import com.dfsek.terra.profiler.Profiler;
import com.dfsek.terra.profiler.ProfilerImpl;
import com.dfsek.terra.registry.exception.DuplicateEntryException; import com.dfsek.terra.registry.exception.DuplicateEntryException;
import com.dfsek.terra.registry.master.AddonRegistry; import com.dfsek.terra.registry.master.AddonRegistry;
import com.dfsek.terra.registry.master.ConfigRegistry; import com.dfsek.terra.registry.master.ConfigRegistry;
@ -105,6 +107,9 @@ public class TerraFabricPlugin implements TerraPlugin, ModInitializer {
private final Map<Long, TerraWorld> worldMap = new HashMap<>(); private final Map<Long, TerraWorld> worldMap = new HashMap<>();
private final EventManager eventManager = new TerraEventManager(this); private final EventManager eventManager = new TerraEventManager(this);
private final GenericLoaders genericLoaders = new GenericLoaders(this); private final GenericLoaders genericLoaders = new GenericLoaders(this);
private final Profiler profiler = new ProfilerImpl();
private final Logger logger = new Logger() { private final Logger logger = new Logger() {
private final org.apache.logging.log4j.Logger logger = LogManager.getLogger(); private final org.apache.logging.log4j.Logger logger = LogManager.getLogger();
@ -389,6 +394,11 @@ public class TerraFabricPlugin implements TerraPlugin, ModInitializer {
return eventManager; return eventManager;
} }
@Override
public Profiler getProfiler() {
return profiler;
}
@Addon("Terra-Fabric") @Addon("Terra-Fabric")
@Author("Terra") @Author("Terra")
@Version("1.0.0") @Version("1.0.0")

View File

@ -21,6 +21,8 @@ import com.dfsek.terra.config.lang.Language;
import com.dfsek.terra.config.pack.ConfigPack; import com.dfsek.terra.config.pack.ConfigPack;
import com.dfsek.terra.platform.RawBiome; import com.dfsek.terra.platform.RawBiome;
import com.dfsek.terra.platform.RawWorldHandle; import com.dfsek.terra.platform.RawWorldHandle;
import com.dfsek.terra.profiler.Profiler;
import com.dfsek.terra.profiler.ProfilerImpl;
import com.dfsek.terra.registry.master.AddonRegistry; import com.dfsek.terra.registry.master.AddonRegistry;
import com.dfsek.terra.registry.master.ConfigRegistry; import com.dfsek.terra.registry.master.ConfigRegistry;
import com.dfsek.terra.world.TerraWorld; import com.dfsek.terra.world.TerraWorld;
@ -39,6 +41,8 @@ public class StandalonePlugin implements TerraPlugin {
private final RawWorldHandle worldHandle = new RawWorldHandle(); private final RawWorldHandle worldHandle = new RawWorldHandle();
private final EventManager eventManager = new TerraEventManager(this); private final EventManager eventManager = new TerraEventManager(this);
private final Profiler profiler = new ProfilerImpl();
@Override @Override
public WorldHandle getWorldHandle() { public WorldHandle getWorldHandle() {
return worldHandle; return worldHandle;
@ -147,4 +151,9 @@ public class StandalonePlugin implements TerraPlugin {
public EventManager getEventManager() { public EventManager getEventManager() {
return eventManager; return eventManager;
} }
@Override
public Profiler getProfiler() {
return profiler;
}
} }

View File

@ -14,6 +14,7 @@ import com.dfsek.terra.api.util.logging.DebugLogger;
import com.dfsek.terra.config.PluginConfig; import com.dfsek.terra.config.PluginConfig;
import com.dfsek.terra.config.lang.Language; import com.dfsek.terra.config.lang.Language;
import com.dfsek.terra.config.pack.ConfigPack; import com.dfsek.terra.config.pack.ConfigPack;
import com.dfsek.terra.profiler.Profiler;
import com.dfsek.terra.registry.master.AddonRegistry; import com.dfsek.terra.registry.master.AddonRegistry;
import com.dfsek.terra.registry.master.ConfigRegistry; import com.dfsek.terra.registry.master.ConfigRegistry;
import com.dfsek.terra.sponge.world.SpongeWorldHandle; import com.dfsek.terra.sponge.world.SpongeWorldHandle;
@ -138,4 +139,9 @@ public class TerraSpongePlugin implements TerraPlugin {
public EventManager getEventManager() { public EventManager getEventManager() {
return eventManager; return eventManager;
} }
@Override
public Profiler getProfiler() {
return null;
}
} }