From a28324bb5020c8051ef9cdb7fd12a7ab3999bc1c Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Mon, 11 Dec 2023 00:08:17 +0900 Subject: [PATCH 1/2] Use cache for base time unit strings Closes gh-4352 --- .../java/io/micrometer/core/instrument/MeterRegistry.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java index d016131cca..d8f5221af7 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java @@ -36,6 +36,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.*; +import java.util.stream.Collectors; import static java.lang.String.format; import static java.util.Collections.emptyList; @@ -279,8 +280,13 @@ protected String getConventionName(Meter.Id id) { */ protected abstract DistributionStatisticConfig defaultHistogramConfig(); + private static final EnumMap BASE_TIME_UNIT_STRING_CACHE = Arrays.stream(TimeUnit.values()) + .collect(Collectors.toMap(Function.identity(), (timeUnit) -> timeUnit.toString().toLowerCase(), (l, r) -> { + throw new IllegalStateException("Duplicate keys should not exist."); + }, () -> new EnumMap<>(TimeUnit.class))); + private String getBaseTimeUnitStr() { - return getBaseTimeUnit().toString().toLowerCase(); + return BASE_TIME_UNIT_STRING_CACHE.get(getBaseTimeUnit()); } /** From 60fa8857dd755afd07b838d0c419236edbf27692 Mon Sep 17 00:00:00 2001 From: Jonatan Ivanov Date: Mon, 11 Dec 2023 11:54:53 +0100 Subject: [PATCH 2/2] Polish "Use cache for base time unit strings" --- .../core/instrument/MeterRegistry.java | 17 ++++++++++++----- .../core/instrument/MeterRegistryTest.java | 9 +++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java index d8f5221af7..8dd22eef63 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java @@ -60,6 +60,18 @@ */ public abstract class MeterRegistry { + // @formatter:off + private static final EnumMap BASE_TIME_UNIT_STRING_CACHE = Arrays.stream(TimeUnit.values()) + .collect( + Collectors.toMap( + Function.identity(), + (timeUnit) -> timeUnit.toString().toLowerCase(), + (k, v) -> { throw new IllegalStateException("Duplicate keys should not exist."); }, + () -> new EnumMap<>(TimeUnit.class) + ) + ); + // @formatter:on + protected final Clock clock; private final Object meterMapLock = new Object(); @@ -280,11 +292,6 @@ protected String getConventionName(Meter.Id id) { */ protected abstract DistributionStatisticConfig defaultHistogramConfig(); - private static final EnumMap BASE_TIME_UNIT_STRING_CACHE = Arrays.stream(TimeUnit.values()) - .collect(Collectors.toMap(Function.identity(), (timeUnit) -> timeUnit.toString().toLowerCase(), (l, r) -> { - throw new IllegalStateException("Duplicate keys should not exist."); - }, () -> new EnumMap<>(TimeUnit.class))); - private String getBaseTimeUnitStr() { return BASE_TIME_UNIT_STRING_CACHE.get(getBaseTimeUnit()); } diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/MeterRegistryTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/MeterRegistryTest.java index b4d094ea66..0940a64a91 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/MeterRegistryTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/MeterRegistryTest.java @@ -15,6 +15,7 @@ */ package io.micrometer.core.instrument; +import io.micrometer.core.Issue; import io.micrometer.core.instrument.config.MeterFilter; import io.micrometer.core.instrument.config.MeterFilterReply; import io.micrometer.core.instrument.distribution.DistributionStatisticConfig; @@ -205,4 +206,12 @@ void shouldNotLetRegisteringMetersTwice() { .hasNoCause(); } + @Test + @Issue("#4352") + void baseUnitStringShouldBeCachedAndReturnTheSameInstance() { + Timer timer1 = registry.timer("test.timer1"); + Timer timer2 = registry.timer("test.timer2"); + assertThat(timer1.getId().getBaseUnit()).isSameAs(timer2.getId().getBaseUnit()); + } + }