mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-11 10:16:15 +00:00
implement TerraPlugin#getProfier
This commit is contained in:
@@ -3,8 +3,6 @@ package com.dfsek.terra.profiler;
|
||||
import java.util.Map;
|
||||
|
||||
public interface Profiler {
|
||||
ProfilerImpl INSTANCE = new ProfilerImpl();
|
||||
|
||||
void push(String frame);
|
||||
|
||||
void pop(String frame);
|
||||
|
||||
@@ -13,10 +13,12 @@ public class ProfilerImpl implements Profiler {
|
||||
private static final ThreadLocal<Map<String, List<Long>>> TIMINGS = ThreadLocal.withInitial(HashMap::new);
|
||||
private final List<Map<String, List<Long>>> accessibleThreadMaps = new ArrayList<>();
|
||||
private volatile boolean running = false;
|
||||
private static boolean instantiated = false;
|
||||
|
||||
|
||||
public ProfilerImpl() {
|
||||
|
||||
if(instantiated) throw new IllegalStateException("Only one instance of Profiler may exist!");
|
||||
instantiated = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -64,16 +66,14 @@ public class ProfilerImpl implements Profiler {
|
||||
@Override
|
||||
public Map<String, Timings> getTimings() {
|
||||
Map<String, Timings> map = new HashMap<>();
|
||||
accessibleThreadMaps.forEach(smap -> {
|
||||
smap.forEach((key, list) -> {
|
||||
String[] keys = key.split("\\.");
|
||||
Timings timings = map.computeIfAbsent(keys[0], id -> new Timings());
|
||||
for(int i = 1; i < keys.length; i++) {
|
||||
timings = timings.getSubItem(keys[i]);
|
||||
}
|
||||
list.forEach(timings::addTime);
|
||||
});
|
||||
});
|
||||
accessibleThreadMaps.forEach(smap -> smap.forEach((key, list) -> {
|
||||
String[] keys = key.split("\\.");
|
||||
Timings timings = map.computeIfAbsent(keys[0], id -> new Timings());
|
||||
for(int i = 1; i < keys.length; i++) {
|
||||
timings = timings.getSubItem(keys[i]);
|
||||
}
|
||||
list.forEach(timings::addTime);
|
||||
}));
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package profiler;
|
||||
|
||||
import com.dfsek.terra.profiler.Profiler;
|
||||
import com.dfsek.terra.profiler.ProfilerImpl;
|
||||
|
||||
public class ProfilerTest {
|
||||
private static final Profiler PROFILER = new ProfilerImpl();
|
||||
//@Test
|
||||
public static void main(String... a) throws InterruptedException {
|
||||
Profiler.INSTANCE.start();
|
||||
PROFILER.start();
|
||||
for(int i = 0; i < 1000; i++) {
|
||||
doThing();
|
||||
}
|
||||
@@ -17,29 +19,29 @@ public class ProfilerTest {
|
||||
for(int i = 0; i < 100; i++) {
|
||||
doOtherThing();
|
||||
}
|
||||
Profiler.INSTANCE.stop();
|
||||
Profiler.INSTANCE.getTimings().forEach((id, timings) -> {
|
||||
PROFILER.stop();
|
||||
PROFILER.getTimings().forEach((id, timings) -> {
|
||||
System.out.println(id + ": " + timings.toString());
|
||||
});
|
||||
}
|
||||
|
||||
private static void doThing() throws InterruptedException {
|
||||
Profiler.INSTANCE.push("thing");
|
||||
PROFILER.push("thing");
|
||||
Thread.sleep(1);
|
||||
doOtherThing();
|
||||
Profiler.INSTANCE.pop("thing");
|
||||
PROFILER.pop("thing");
|
||||
}
|
||||
|
||||
private static void doOtherThing() throws InterruptedException {
|
||||
Profiler.INSTANCE.push("thing2");
|
||||
PROFILER.push("thing2");
|
||||
Thread.sleep(2);
|
||||
doThirdOtherThing();
|
||||
Profiler.INSTANCE.pop("thing2");
|
||||
PROFILER.pop("thing2");
|
||||
}
|
||||
|
||||
private static void doThirdOtherThing() throws InterruptedException {
|
||||
Profiler.INSTANCE.push("thing3");
|
||||
PROFILER.push("thing3");
|
||||
Thread.sleep(2);
|
||||
Profiler.INSTANCE.pop("thing3");
|
||||
PROFILER.pop("thing3");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user