Skip to content

Commit

Permalink
Improved the simulator's report formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Nov 30, 2020
1 parent 64b50b6 commit a54afaa
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ ext {
]
pluginVersions = [
bnd: '5.2.0',
checkstyle: '8.37',
checkstyle: '8.38',
coveralls: '2.8.4',
coverity: '1.0.10',
errorprone: '1.3.0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public PolicyStats(String name) {
this.metrics = new LinkedHashMap<>();
this.stopwatch = Stopwatch.createUnstarted();

addMetric("Policy", this::name);
addPercentMetric("Hit rate", this::hitRate);
addMetric("Hits", this::hitCount);
addMetric("Misses", this::missCount);
addMetric("Requests", this::requestCount);
addMetric("Evictions", this::evictionCount);
addMetric(Metric.of("Policy", (Supplier<String>) this::name, OBJECT, true));
addMetric(Metric.of("Hit Rate", (DoubleSupplier) this::hitRate, PERCENT, true));
addMetric(Metric.of("Hits", (LongSupplier) this::hitCount, NUMBER, true));
addMetric(Metric.of("Misses", (LongSupplier) this::missCount, NUMBER, true));
addMetric(Metric.of("Requests", (LongSupplier) this::requestCount, NUMBER, true));
addMetric(Metric.of("Evictions", (LongSupplier) this::evictionCount, NUMBER, true));
addPercentMetric("Admit rate",
() -> (admittedCount + rejectedCount) == 0 ? 0 : admissionRate());
addMetric(Metric.builder()
Expand All @@ -87,7 +87,7 @@ public PolicyStats(String name) {
.type(NUMBER)
.build());
addMetric(Metric.builder()
.value((LongSupplier) this::requestsWeight)
.value((DoubleSupplier) this::weightedHitRate)
.addToCharacteristics(WEIGHTED)
.name("Weighted Hit Rate")
.type(PERCENT)
Expand Down Expand Up @@ -301,11 +301,16 @@ public String toString() {
public static abstract class Metric {
public enum MetricType { NUMBER, PERCENT, OBJECT }

public abstract ImmutableSet<Characteristic> characteristics();
public abstract MetricType type();
public abstract Object value();
public abstract String name();
public abstract Object value();
public abstract MetricType type();
public abstract boolean required();
public abstract ImmutableSet<Characteristic> characteristics();

@SuppressWarnings("PMD.ShortMethodName")
public static Metric of(String name, Object value, MetricType type, boolean required) {
return builder().name(name).value(value).type(type).required(required).build();
}
public static PolicyStats_Metric_Builder builder() {
return PolicyStats_Metric_Builder.builder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ public String format(Metric metric) {
return "";
} else if (metric.value() instanceof LongSupplier) {
long value = ((LongSupplier) metric.value()).getAsLong();
return longFormatter().apply(value);
return (value > 0) || metric.required()
? longFormatter().apply(value)
: "";
} else if (metric.value() instanceof DoubleSupplier) {
double value = ((DoubleSupplier) metric.value()).getAsDouble();
if ((value == 0.0) && !metric.required()) {
return "";
}
return (metric.type() == MetricType.PERCENT)
? percentFormatter().apply(value)
: doubleFormatter().apply(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected Set<String> headers() {
.flatMap(policyStats -> policyStats.metrics().values().stream())
.filter(metric -> metric.characteristics().isEmpty()
|| metric.characteristics().stream().anyMatch(characteristics::contains))
.filter(metric -> !metrics().format(metric).isEmpty())
.filter(metric -> metric.required() || !metrics().format(metric).isEmpty())
.map(Metric::name)
.collect(toImmutableSet());
headers = ImmutableSet.copyOf(Sets.intersection(all, used));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ protected String assemble(List<PolicyStats> results) {
@Override
protected Metrics newMetrics() {
return Metrics.builder()
.percentFormatter(value -> (value == 0) ? "" : String.format("%.2f", 100 * value))
.doubleFormatter(value -> (value == 0) ? "" : String.format("%.2f", value))
.longFormatter(value -> (value == 0) ? "" : String.format("%d", value))
.percentFormatter(value -> String.format("%.2f", 100 * value))
.doubleFormatter(value -> String.format("%.2f", value))
.longFormatter(value -> String.format("%d", value))
.objectFormatter(object -> {
return (object instanceof Stopwatch)
? Long.toString(((Stopwatch) object).elapsed(TimeUnit.MILLISECONDS))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ protected String assemble(List<PolicyStats> results) {
@Override
protected Metrics newMetrics() {
return Metrics.builder()
.percentFormatter(value -> (value == 0) ? "" : String.format("%.2f %%", 100 * value))
.doubleFormatter(value -> (value == 0) ? "" : String.format("%.2f", value))
.longFormatter(value -> (value == 0) ? "" : String.format("%,d", value))
.percentFormatter(value -> String.format("%.2f %%", 100 * value))
.doubleFormatter(value -> String.format("%.2f", value))
.longFormatter(value -> String.format("%,d", value))
.build();
}
}

0 comments on commit a54afaa

Please sign in to comment.