diff --git a/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeAbsoluteSamples.java b/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeAbsoluteSamples.java index 7b8ac9a..e9ba97f 100644 --- a/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeAbsoluteSamples.java +++ b/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeAbsoluteSamples.java @@ -1,13 +1,18 @@ package org.warp.commonutils.metrics; +import it.unimi.dsi.fastutil.objects.Object2ObjectMap; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; +import java.util.Collections; import java.util.HashMap; +import java.util.Set; public class AtomicDetailedTimeAbsoluteSamples implements AtomicDetailedTimeAbsoluteSamplesSnapshot { private final boolean isSnapshot; private final int sampleTime; private final int samplesCount; - private HashMap detailedAtomicTimeSamples = new HashMap<>(); + private Object2ObjectMap detailedAtomicTimeSamples = new Object2ObjectOpenHashMap<>(); /** * @param sampleTime in milliseconds @@ -22,7 +27,7 @@ public class AtomicDetailedTimeAbsoluteSamples implements AtomicDetailedTimeA public AtomicDetailedTimeAbsoluteSamples(int sampleTime, int samplesCount, HashMap detailedAtomicTimeSamples, boolean isSnapshot) { this.sampleTime = sampleTime; this.samplesCount = samplesCount; - this.detailedAtomicTimeSamples = new HashMap<>(); + this.detailedAtomicTimeSamples = new Object2ObjectOpenHashMap<>(); detailedAtomicTimeSamples.forEach((detail, sample) -> this.detailedAtomicTimeSamples.put(detail, (AtomicTimeAbsoluteSamples) sample)); this.isSnapshot = isSnapshot; } @@ -45,6 +50,11 @@ public class AtomicDetailedTimeAbsoluteSamples implements AtomicDetailedTimeA getDetailed(detail).set(count); } + @Override + public synchronized Set getDetails() { + return Collections.unmodifiableSet(new ObjectOpenHashSet<>(detailedAtomicTimeSamples.keySet())); + } + @Override public synchronized double getAveragePerSecond(T detail, long timeRange) { updateSamples(); @@ -86,6 +96,9 @@ public class AtomicDetailedTimeAbsoluteSamples implements AtomicDetailedTimeA } public synchronized AtomicDetailedTimeAbsoluteSamples snapshot() { + if (isSnapshot) { + return this; + } var clonedDetailedAtomicTimeSamples = new HashMap(detailedAtomicTimeSamples); clonedDetailedAtomicTimeSamples.replaceAll((key, value) -> ((AtomicTimeAbsoluteSamples) value).snapshot()); return new AtomicDetailedTimeAbsoluteSamples<>(sampleTime, diff --git a/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeAbsoluteSamplesSnapshot.java b/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeAbsoluteSamplesSnapshot.java index 9040111..4776a34 100644 --- a/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeAbsoluteSamplesSnapshot.java +++ b/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeAbsoluteSamplesSnapshot.java @@ -1,7 +1,11 @@ package org.warp.commonutils.metrics; +import java.util.Set; + public interface AtomicDetailedTimeAbsoluteSamplesSnapshot extends AtomicTimeAbsoluteSamplesSnapshot { + Set getDetails(); + double getAveragePerSecond(T detail, long timeRange); long getCurrentCount(T detail); diff --git a/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeIncrementalSamples.java b/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeIncrementalSamples.java index 71e2a70..1e0eca1 100644 --- a/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeIncrementalSamples.java +++ b/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeIncrementalSamples.java @@ -1,12 +1,17 @@ package org.warp.commonutils.metrics; +import it.unimi.dsi.fastutil.objects.Object2ObjectMap; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; +import java.util.Set; public class AtomicDetailedTimeIncrementalSamples extends AtomicTimeIncrementalSamples implements AtomicDetailedTimeIncrementalSamplesSnapshot { - private HashMap detailedAtomicTimeSamples = new HashMap<>(); + private Object2ObjectMap detailedAtomicTimeSamples = new Object2ObjectOpenHashMap<>(); /** * @param sampleTime in milliseconds @@ -19,7 +24,7 @@ public class AtomicDetailedTimeIncrementalSamples extends AtomicTimeIncrement public AtomicDetailedTimeIncrementalSamples(long startTime, long[] samples, int sampleTime, long currentSampleStartTime, long totalEvents, HashMap detailedAtomicTimeSamples, boolean isSnapshot) { super(startTime, samples, sampleTime, currentSampleStartTime, totalEvents, isSnapshot); - this.detailedAtomicTimeSamples = new HashMap<>(); + this.detailedAtomicTimeSamples = new Object2ObjectOpenHashMap<>(); detailedAtomicTimeSamples.forEach((detail, sample) -> this.detailedAtomicTimeSamples.put(detail, (AtomicTimeIncrementalSamples) sample)); } @@ -38,6 +43,11 @@ public class AtomicDetailedTimeIncrementalSamples extends AtomicTimeIncrement increment(count); } + @Override + public synchronized Set getDetails() { + return Collections.unmodifiableSet(new ObjectOpenHashSet<>(detailedAtomicTimeSamples.keySet())); + } + @Override public synchronized double getAveragePerSecond(T detail, long timeRange) { updateSamples(); @@ -73,6 +83,9 @@ public class AtomicDetailedTimeIncrementalSamples extends AtomicTimeIncrement } public synchronized AtomicDetailedTimeIncrementalSamples snapshot() { + if (isSnapshot) { + return this; + } var clonedDetailedAtomicTimeSamples = new HashMap(detailedAtomicTimeSamples); clonedDetailedAtomicTimeSamples.replaceAll((key, value) -> ((AtomicTimeIncrementalSamples) value).snapshot()); return new AtomicDetailedTimeIncrementalSamples<>(startTime, Arrays.copyOf(this.samples, this.samples.length), sampleTime, diff --git a/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeIncrementalSamplesSnapshot.java b/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeIncrementalSamplesSnapshot.java index 2b3bca4..84b981f 100644 --- a/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeIncrementalSamplesSnapshot.java +++ b/src/main/java/org/warp/commonutils/metrics/AtomicDetailedTimeIncrementalSamplesSnapshot.java @@ -1,7 +1,11 @@ package org.warp.commonutils.metrics; +import java.util.Set; + public interface AtomicDetailedTimeIncrementalSamplesSnapshot extends AtomicTimeIncrementalSamplesSnapshot { + Set getDetails(); + double getAveragePerSecond(T detail, long timeRange); long getApproximateCount(T detail, long timeRange); diff --git a/src/main/java/org/warp/commonutils/metrics/AtomicTimeIncrementalSamples.java b/src/main/java/org/warp/commonutils/metrics/AtomicTimeIncrementalSamples.java index 0ad63e2..fd2de8b 100644 --- a/src/main/java/org/warp/commonutils/metrics/AtomicTimeIncrementalSamples.java +++ b/src/main/java/org/warp/commonutils/metrics/AtomicTimeIncrementalSamples.java @@ -124,6 +124,9 @@ public class AtomicTimeIncrementalSamples implements AtomicTimeIncrementalSample } public synchronized AtomicTimeIncrementalSamplesSnapshot snapshot() { + if (isSnapshot) { + return this; + } return new AtomicTimeIncrementalSamples(startTime, Arrays.copyOf(this.samples, this.samples.length), sampleTime, currentSampleStartTime, totalEvents, true); } }