diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle index 8803835b4d..ad4df67aa3 100644 --- a/gradle/dependencies.gradle +++ b/gradle/dependencies.gradle @@ -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', diff --git a/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/policy/PolicyStats.java b/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/policy/PolicyStats.java index 7294e1685c..e14d5451c4 100644 --- a/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/policy/PolicyStats.java +++ b/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/policy/PolicyStats.java @@ -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) 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() @@ -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) @@ -301,11 +301,16 @@ public String toString() { public static abstract class Metric { public enum MetricType { NUMBER, PERCENT, OBJECT } - public abstract ImmutableSet 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 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(); } diff --git a/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/Metrics.java b/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/Metrics.java index 5ee2985540..e47102a492 100644 --- a/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/Metrics.java +++ b/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/Metrics.java @@ -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); diff --git a/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/TextReporter.java b/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/TextReporter.java index 86a48a284f..d7309c365a 100644 --- a/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/TextReporter.java +++ b/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/TextReporter.java @@ -88,7 +88,7 @@ protected Set 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)); diff --git a/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/csv/CsvReporter.java b/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/csv/CsvReporter.java index 0098acd63b..fd7e9668b6 100644 --- a/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/csv/CsvReporter.java +++ b/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/csv/CsvReporter.java @@ -61,9 +61,9 @@ protected String assemble(List 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)) diff --git a/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/table/TableReporter.java b/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/table/TableReporter.java index 4b6cf0f22e..c6d10dbed8 100644 --- a/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/table/TableReporter.java +++ b/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/report/table/TableReporter.java @@ -52,9 +52,9 @@ protected String assemble(List 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(); } }