From 44f147afd80c1092dc28e565ed75120e283696e3 Mon Sep 17 00:00:00 2001 From: fogninid Date: Wed, 16 Oct 2024 12:04:35 +0000 Subject: [PATCH] improve average performance of long task timer for out-of-order start/stop of many tasks --- .../instrument/internal/DefaultLongTaskTimer.java | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/internal/DefaultLongTaskTimer.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/internal/DefaultLongTaskTimer.java index 9ad28d1e1a..2501f84d78 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/internal/DefaultLongTaskTimer.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/internal/DefaultLongTaskTimer.java @@ -29,7 +29,6 @@ import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ConcurrentSkipListSet; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.StreamSupport; @@ -55,8 +54,6 @@ public class DefaultLongTaskTimer extends AbstractMeter implements LongTaskTimer */ private final NavigableSet activeTasks = new ConcurrentSkipListSet<>(); - private final AtomicLong taskId = new AtomicLong(); - private final Clock clock; private final TimeUnit baseTimeUnit; @@ -98,7 +95,7 @@ public DefaultLongTaskTimer(Id id, Clock clock, TimeUnit baseTimeUnit, @Override public Sample start() { - SampleImpl sample = new SampleImpl(taskId.incrementAndGet()); + SampleImpl sample = new SampleImpl(); activeTasks.add(sample); return sample; } @@ -231,12 +228,9 @@ class SampleImpl extends Sample implements Comparable { private final long startTime; - private final long id; - private volatile boolean stopped; - private SampleImpl(long id) { - this.id = id; + private SampleImpl() { this.startTime = clock.monotonicTime(); } @@ -268,7 +262,7 @@ public String toString() { public int compareTo(DefaultLongTaskTimer.SampleImpl o) { int startCompare = Long.compare(startTime, o.startTime); if (startCompare == 0) { - return Long.compare(id, o.id); + return Long.compare(hashCode(), o.hashCode()); } return startCompare; }