|
| 1 | +/* |
| 2 | + * Copyright 2025 VMware, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.micrometer.core.instrument; |
| 17 | + |
| 18 | +import com.sun.management.ThreadMXBean; |
| 19 | +import io.micrometer.core.Issue; |
| 20 | +import io.micrometer.core.instrument.distribution.pause.NoPauseDetector; |
| 21 | +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
| 22 | +import org.junit.jupiter.api.*; |
| 23 | +import org.junit.jupiter.api.condition.DisabledIfSystemProperty; |
| 24 | + |
| 25 | +import java.lang.management.ManagementFactory; |
| 26 | + |
| 27 | +import static io.micrometer.core.instrument.MeterRegistryAllocationTest.JAVA_VM_NAME_J9_REGEX; |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests expected allocation (or lack thereof) for methods expected to be used on hot |
| 32 | + * paths from {@link MeterRegistry}. |
| 33 | + */ |
| 34 | +@DisabledIfSystemProperty(named = "java.vm.name", matches = JAVA_VM_NAME_J9_REGEX, |
| 35 | + disabledReason = "Sun ThreadMXBean with allocation counter not available") |
| 36 | +class MeterRegistryAllocationTest { |
| 37 | + |
| 38 | + // Should match "Eclipse OpenJ9 VM" and "IBM J9 VM" |
| 39 | + static final String JAVA_VM_NAME_J9_REGEX = ".*J9 VM$"; |
| 40 | + |
| 41 | + MeterRegistry registry = new SimpleMeterRegistry(); |
| 42 | + |
| 43 | + Tags tags = Tags.of("a", "b", "c", "d"); |
| 44 | + |
| 45 | + static ThreadMXBean threadMXBean = (ThreadMXBean) ManagementFactory.getThreadMXBean(); |
| 46 | + |
| 47 | + /** |
| 48 | + * Number of bytes allocated when constructing a Meter.Id. This can vary by JVM and |
| 49 | + * configuration, which is why we measure it programmatically. For most common cases, |
| 50 | + * it should be 40 bytes. |
| 51 | + */ |
| 52 | + static long newIdBytes; |
| 53 | + |
| 54 | + @BeforeAll |
| 55 | + static void setup() { |
| 56 | + // Construct once here so any static initialization doesn't get measured. |
| 57 | + new Meter.Id("name", Tags.empty(), null, null, Meter.Type.OTHER); |
| 58 | + newIdBytes = measureAllocatedBytes(() -> new Meter.Id("name", Tags.empty(), null, null, Meter.Type.OTHER)); |
| 59 | + } |
| 60 | + |
| 61 | + private static long measureAllocatedBytes(Runnable runnable) { |
| 62 | + long currentThreadId = Thread.currentThread().getId(); |
| 63 | + long allocatedBytesBefore = threadMXBean.getThreadAllocatedBytes(currentThreadId); |
| 64 | + |
| 65 | + runnable.run(); |
| 66 | + |
| 67 | + return threadMXBean.getThreadAllocatedBytes(currentThreadId) - allocatedBytesBefore; |
| 68 | + } |
| 69 | + |
| 70 | + @BeforeEach |
| 71 | + void setUp() { |
| 72 | + registry.timer("timer", tags); |
| 73 | + LongTaskTimer.builder("ltt").tags(tags).register(registry); |
| 74 | + Counter.builder("counter").tags(tags).register(registry); |
| 75 | + } |
| 76 | + |
| 77 | + @Nested |
| 78 | + @DisplayName("Internal implementation details") |
| 79 | + class Internal { |
| 80 | + |
| 81 | + @Issue("#6670") |
| 82 | + @Test |
| 83 | + void retrieveExistingTimer_noAllocation() { |
| 84 | + Meter.Id id = new Meter.Id("timer", tags, null, null, Meter.Type.TIMER); |
| 85 | + // test allocation from internal implementation (non-public API) |
| 86 | + assertNoAllocation(() -> registry.timer(id, AbstractTimerBuilder.DEFAULT_DISTRIBUTION_CONFIG, |
| 87 | + NoPauseDetector.INSTANCE)); |
| 88 | + } |
| 89 | + |
| 90 | + @Issue("#6670") |
| 91 | + @Test |
| 92 | + void retrieveExistingLongTaskTimer_noAllocation() { |
| 93 | + Meter.Id id = new Meter.Id("ltt", tags, null, null, Meter.Type.LONG_TASK_TIMER); |
| 94 | + // test allocation from internal implementation (non-public API) |
| 95 | + assertNoAllocation( |
| 96 | + () -> registry.more().longTaskTimer(id, LongTaskTimer.Builder.DEFAULT_DISTRIBUTION_CONFIG)); |
| 97 | + } |
| 98 | + |
| 99 | + @Issue("#6670") |
| 100 | + @Test |
| 101 | + void retrieveExistingCounter_noAllocation() { |
| 102 | + Meter.Id id = new Meter.Id("counter", tags, null, null, Meter.Type.COUNTER); |
| 103 | + // test allocation from internal implementation (non-public API) |
| 104 | + assertNoAllocation(() -> registry.counter(id)); |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + @Nested |
| 110 | + @DisplayName("Public API available to users") |
| 111 | + class PublicApi { |
| 112 | + |
| 113 | + @Issue("#6670") |
| 114 | + @Test |
| 115 | + void retrieveExistingTimer_onlyIdAllocation() { |
| 116 | + assertBytesAllocated(newIdBytes, () -> registry.timer("timer", tags)); |
| 117 | + } |
| 118 | + |
| 119 | + @Issue("#6670") |
| 120 | + @Test |
| 121 | + void retrieveExistingLongTaskTimer_onlyIdAllocation() { |
| 122 | + assertBytesAllocated(newIdBytes, () -> registry.more().longTaskTimer("ltt", tags)); |
| 123 | + } |
| 124 | + |
| 125 | + @Issue("#6670") |
| 126 | + @Test |
| 127 | + void retrieveExistingCounter_onlyIdAllocation() { |
| 128 | + assertBytesAllocated(newIdBytes, () -> registry.counter("counter", tags)); |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + private void assertBytesAllocated(long bytes, Runnable runnable) { |
| 134 | + assertThat(measureAllocatedBytes(runnable)).isEqualTo(bytes); |
| 135 | + } |
| 136 | + |
| 137 | + private void assertNoAllocation(Runnable runnable) { |
| 138 | + assertBytesAllocated(0, runnable); |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments