diff --git a/common/src/main/java/com/dfsek/terra/profiler/Timings.java b/common/src/main/java/com/dfsek/terra/profiler/Timings.java index 22ef0f003..3875285b3 100644 --- a/common/src/main/java/com/dfsek/terra/profiler/Timings.java +++ b/common/src/main/java/com/dfsek/terra/profiler/Timings.java @@ -1,9 +1,12 @@ package com.dfsek.terra.profiler; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; public class Timings { private final Map subItems = new HashMap<>(); @@ -38,25 +41,33 @@ public class Timings { return subItems.computeIfAbsent(id, s -> new Timings()); } - public String toString(int indent, Timings parent) { + public String toString(int indent, Timings parent, Set branches) { StringBuilder builder = new StringBuilder(); builder.append((double) min() / 1000000).append("ms min / ").append(average() / 1000000).append("ms avg / ") .append((double) max() / 1000000).append("ms max (").append(timings.size()).append(" samples, ") .append((sum() / parent.sum()) * 100).append("% of parent)"); - subItems.forEach((id, timings) -> { + List frames = new ArrayList<>(); + Set newBranches = new HashSet<>(branches); + newBranches.add(indent); + subItems.forEach((id, timings) -> frames.add(id + ": " + timings.toString(indent + 1, this, newBranches))); + + for(int i = 0; i < frames.size(); i++) { builder.append('\n'); - for(int i = 0; i <= indent; i++) { - builder.append('\t'); + for(int j = 0; j < indent; j++) { + if(branches.contains(j)) builder.append("│ "); + else builder.append(" "); } - builder.append(id).append(": ").append(timings.toString(indent + 1, this)); - }); + if(i == frames.size() - 1 && !frames.get(i).contains("\n")) builder.append("└───"); + else builder.append("├───"); + builder.append(frames.get(i)); + } return builder.toString(); } @Override public String toString() { - return toString(0, this); + return toString(1, this, Collections.emptySet()); } } diff --git a/common/src/test/java/profiler/ProfilerTest.java b/common/src/test/java/profiler/ProfilerTest.java index b60ea32e5..86ad4e5a1 100644 --- a/common/src/test/java/profiler/ProfilerTest.java +++ b/common/src/test/java/profiler/ProfilerTest.java @@ -29,6 +29,7 @@ public class ProfilerTest { PROFILER.push("thing"); Thread.sleep(1); doOtherThing(); + thing4(); PROFILER.pop("thing"); } @@ -36,6 +37,7 @@ public class ProfilerTest { PROFILER.push("thing2"); Thread.sleep(2); doThirdOtherThing(); + thing4(); PROFILER.pop("thing2"); } @@ -44,4 +46,10 @@ public class ProfilerTest { Thread.sleep(2); PROFILER.pop("thing3"); } + + private static void thing4() throws InterruptedException { + PROFILER.push("thing4"); + Thread.sleep(2); + PROFILER.pop("thing4"); + } }