Performance

This commit is contained in:
Andrea Cavalli 2020-06-19 23:43:32 +02:00
parent d16315a4ba
commit d6ac2e306a
5 changed files with 41 additions and 4 deletions

View File

@ -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<T> implements AtomicDetailedTimeAbsoluteSamplesSnapshot<T> {
private final boolean isSnapshot;
private final int sampleTime;
private final int samplesCount;
private HashMap<T, AtomicTimeAbsoluteSamples> detailedAtomicTimeSamples = new HashMap<>();
private Object2ObjectMap<T, AtomicTimeAbsoluteSamples> detailedAtomicTimeSamples = new Object2ObjectOpenHashMap<>();
/**
* @param sampleTime in milliseconds
@ -22,7 +27,7 @@ public class AtomicDetailedTimeAbsoluteSamples<T> implements AtomicDetailedTimeA
public AtomicDetailedTimeAbsoluteSamples(int sampleTime, int samplesCount, HashMap<T, AtomicTimeAbsoluteSamplesSnapshot> 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<T> implements AtomicDetailedTimeA
getDetailed(detail).set(count);
}
@Override
public synchronized Set<T> 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<T> implements AtomicDetailedTimeA
}
public synchronized AtomicDetailedTimeAbsoluteSamples<T> snapshot() {
if (isSnapshot) {
return this;
}
var clonedDetailedAtomicTimeSamples = new HashMap<T, AtomicTimeAbsoluteSamplesSnapshot>(detailedAtomicTimeSamples);
clonedDetailedAtomicTimeSamples.replaceAll((key, value) -> ((AtomicTimeAbsoluteSamples) value).snapshot());
return new AtomicDetailedTimeAbsoluteSamples<>(sampleTime,

View File

@ -1,7 +1,11 @@
package org.warp.commonutils.metrics;
import java.util.Set;
public interface AtomicDetailedTimeAbsoluteSamplesSnapshot<T> extends AtomicTimeAbsoluteSamplesSnapshot {
Set<T> getDetails();
double getAveragePerSecond(T detail, long timeRange);
long getCurrentCount(T detail);

View File

@ -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<T> extends AtomicTimeIncrementalSamples implements
AtomicDetailedTimeIncrementalSamplesSnapshot<T> {
private HashMap<T, AtomicTimeIncrementalSamples> detailedAtomicTimeSamples = new HashMap<>();
private Object2ObjectMap<T, AtomicTimeIncrementalSamples> detailedAtomicTimeSamples = new Object2ObjectOpenHashMap<>();
/**
* @param sampleTime in milliseconds
@ -19,7 +24,7 @@ public class AtomicDetailedTimeIncrementalSamples<T> extends AtomicTimeIncrement
public AtomicDetailedTimeIncrementalSamples(long startTime, long[] samples, int sampleTime, long currentSampleStartTime, long totalEvents,
HashMap<T, AtomicTimeIncrementalSamplesSnapshot> 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<T> extends AtomicTimeIncrement
increment(count);
}
@Override
public synchronized Set<T> 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<T> extends AtomicTimeIncrement
}
public synchronized AtomicDetailedTimeIncrementalSamples<T> snapshot() {
if (isSnapshot) {
return this;
}
var clonedDetailedAtomicTimeSamples = new HashMap<T, AtomicTimeIncrementalSamplesSnapshot>(detailedAtomicTimeSamples);
clonedDetailedAtomicTimeSamples.replaceAll((key, value) -> ((AtomicTimeIncrementalSamples) value).snapshot());
return new AtomicDetailedTimeIncrementalSamples<>(startTime, Arrays.copyOf(this.samples, this.samples.length), sampleTime,

View File

@ -1,7 +1,11 @@
package org.warp.commonutils.metrics;
import java.util.Set;
public interface AtomicDetailedTimeIncrementalSamplesSnapshot<T> extends AtomicTimeIncrementalSamplesSnapshot {
Set<T> getDetails();
double getAveragePerSecond(T detail, long timeRange);
long getApproximateCount(T detail, long timeRange);

View File

@ -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);
}
}