Skip to content

Commit b7cf2af

Browse files
committed
CodeTimer now supports configurable reporting units
1 parent 37a6de9 commit b7cf2af

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

common/src/main/java/net/rptools/lib/StringUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.List;
22+
import java.util.concurrent.TimeUnit;
2223
import java.util.regex.Pattern;
2324

2425
/**
@@ -28,6 +29,18 @@ public class StringUtil {
2829
private static NumberFormat nf = NumberFormat.getNumberInstance();
2930
private static final int MIN_FRACTION_DIGITS = 0;
3031

32+
public static String formatTimeUnit(TimeUnit unit) {
33+
return switch (unit) {
34+
case NANOSECONDS -> "ns";
35+
case MICROSECONDS -> "μs";
36+
case MILLISECONDS -> "ms";
37+
case SECONDS -> "s";
38+
case MINUTES -> "m";
39+
case HOURS -> "h";
40+
case DAYS -> "d";
41+
};
42+
}
43+
3144
public static String formatDecimal(double value) {
3245
String result1;
3346
result1 = nf.format(value); // On a separate line to allow for breakpoints

src/main/java/net/rptools/lib/CodeTimer.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.LinkedHashMap;
1919
import java.util.List;
2020
import java.util.Map;
21+
import java.util.concurrent.TimeUnit;
2122
import net.rptools.maptool.client.AppState;
2223
import net.rptools.maptool.client.MapTool;
2324

@@ -66,6 +67,7 @@ public static CodeTimer get() {
6667
private final Map<String, Timer> timeMap = new LinkedHashMap<>();
6768
private final String name;
6869
private long threshold = 1;
70+
private TimeUnit reportingUnit = TimeUnit.MILLISECONDS;
6971
private boolean enabled;
7072

7173
private CodeTimer(String n) {
@@ -78,7 +80,15 @@ public boolean isEnabled() {
7880
}
7981

8082
public void setThreshold(long threshold) {
81-
this.threshold = threshold;
83+
this.threshold = TimeUnit.NANOSECONDS.convert(threshold, TimeUnit.MILLISECONDS);
84+
}
85+
86+
public void setThreshold(long threshold, TimeUnit timeUnit) {
87+
this.threshold = TimeUnit.NANOSECONDS.convert(threshold, timeUnit);
88+
}
89+
90+
public void setReportingUnit(TimeUnit reportingUnit) {
91+
this.reportingUnit = reportingUnit;
8292
}
8393

8494
public void setEnabled(boolean enabled) {
@@ -144,11 +154,18 @@ public String toString() {
144154
++i;
145155

146156
var id = entry.getKey();
147-
long elapsed = entry.getValue().getElapsed() / 1_000_000;
157+
long elapsed = entry.getValue().getElapsed();
148158
if (elapsed < threshold) {
149159
continue;
150160
}
151-
builder.append(String.format(" %3d. %6d ms %s\n", i, elapsed, id));
161+
162+
builder.append(
163+
String.format(
164+
" %3d. %6d %s %s\n",
165+
i,
166+
reportingUnit.convert(elapsed, TimeUnit.NANOSECONDS),
167+
StringUtil.formatTimeUnit(reportingUnit),
168+
id));
152169
}
153170

154171
if (!counterMap.isEmpty()) {

0 commit comments

Comments
 (0)