fancy unicode symbols B)

This commit is contained in:
dfsek
2021-04-12 01:16:26 -07:00
parent f8e7e343cb
commit 23fb7753ab
2 changed files with 26 additions and 7 deletions
@@ -1,9 +1,12 @@
package com.dfsek.terra.profiler; package com.dfsek.terra.profiler;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
public class Timings { public class Timings {
private final Map<String, Timings> subItems = new HashMap<>(); private final Map<String, Timings> subItems = new HashMap<>();
@@ -38,25 +41,33 @@ public class Timings {
return subItems.computeIfAbsent(id, s -> new Timings()); return subItems.computeIfAbsent(id, s -> new Timings());
} }
public String toString(int indent, Timings parent) { public String toString(int indent, Timings parent, Set<Integer> branches) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append((double) min() / 1000000).append("ms min / ").append(average() / 1000000).append("ms avg / ") 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((double) max() / 1000000).append("ms max (").append(timings.size()).append(" samples, ")
.append((sum() / parent.sum()) * 100).append("% of parent)"); .append((sum() / parent.sum()) * 100).append("% of parent)");
subItems.forEach((id, timings) -> { List<String> frames = new ArrayList<>();
Set<Integer> 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'); builder.append('\n');
for(int i = 0; i <= indent; i++) { for(int j = 0; j < indent; j++) {
builder.append('\t'); 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(); return builder.toString();
} }
@Override @Override
public String toString() { public String toString() {
return toString(0, this); return toString(1, this, Collections.emptySet());
} }
} }
@@ -29,6 +29,7 @@ public class ProfilerTest {
PROFILER.push("thing"); PROFILER.push("thing");
Thread.sleep(1); Thread.sleep(1);
doOtherThing(); doOtherThing();
thing4();
PROFILER.pop("thing"); PROFILER.pop("thing");
} }
@@ -36,6 +37,7 @@ public class ProfilerTest {
PROFILER.push("thing2"); PROFILER.push("thing2");
Thread.sleep(2); Thread.sleep(2);
doThirdOtherThing(); doThirdOtherThing();
thing4();
PROFILER.pop("thing2"); PROFILER.pop("thing2");
} }
@@ -44,4 +46,10 @@ public class ProfilerTest {
Thread.sleep(2); Thread.sleep(2);
PROFILER.pop("thing3"); PROFILER.pop("thing3");
} }
private static void thing4() throws InterruptedException {
PROFILER.push("thing4");
Thread.sleep(2);
PROFILER.pop("thing4");
}
} }