Fixed snapshots time

This commit is contained in:
Andrea Cavalli 2020-06-13 01:55:48 +02:00
parent 9fb047c3ac
commit 1945844454
4 changed files with 28 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import java.util.HashMap;
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<>();
@ -15,13 +16,15 @@ public class AtomicDetailedTimeAbsoluteSamples<T> implements AtomicDetailedTimeA
public AtomicDetailedTimeAbsoluteSamples(int sampleTime, int samplesCount) {
this.sampleTime = sampleTime;
this.samplesCount = samplesCount;
this.isSnapshot = false;
}
public AtomicDetailedTimeAbsoluteSamples(int sampleTime, int samplesCount, HashMap<T, AtomicTimeAbsoluteSamplesSnapshot> detailedAtomicTimeSamples) {
public AtomicDetailedTimeAbsoluteSamples(int sampleTime, int samplesCount, HashMap<T, AtomicTimeAbsoluteSamplesSnapshot> detailedAtomicTimeSamples, boolean isSnapshot) {
this.sampleTime = sampleTime;
this.samplesCount = samplesCount;
this.detailedAtomicTimeSamples = new HashMap<>();
detailedAtomicTimeSamples.forEach((detail, sample) -> this.detailedAtomicTimeSamples.put(detail, (AtomicTimeAbsoluteSamples) sample));
this.isSnapshot = isSnapshot;
}
private synchronized void updateSamples() {
@ -86,6 +89,6 @@ public class AtomicDetailedTimeAbsoluteSamples<T> implements AtomicDetailedTimeA
var clonedDetailedAtomicTimeSamples = new HashMap<T, AtomicTimeAbsoluteSamplesSnapshot>(detailedAtomicTimeSamples);
clonedDetailedAtomicTimeSamples.replaceAll((key, value) -> ((AtomicTimeAbsoluteSamples) value).snapshot());
return new AtomicDetailedTimeAbsoluteSamples<>(sampleTime,
samplesCount, clonedDetailedAtomicTimeSamples);
samplesCount, clonedDetailedAtomicTimeSamples, true);
}
}

View File

@ -17,8 +17,8 @@ public class AtomicDetailedTimeIncrementalSamples<T> extends AtomicTimeIncrement
}
public AtomicDetailedTimeIncrementalSamples(long startTime, long[] samples, int sampleTime, long currentSampleStartTime, long totalEvents,
HashMap<T, AtomicTimeIncrementalSamplesSnapshot> detailedAtomicTimeSamples) {
super(startTime, samples, sampleTime, currentSampleStartTime, totalEvents);
HashMap<T, AtomicTimeIncrementalSamplesSnapshot> detailedAtomicTimeSamples, boolean isSnapshot) {
super(startTime, samples, sampleTime, currentSampleStartTime, totalEvents, isSnapshot);
this.detailedAtomicTimeSamples = new HashMap<>();
detailedAtomicTimeSamples.forEach((detail, sample) -> detailedAtomicTimeSamples.put(detail, (AtomicTimeIncrementalSamples) sample));
}
@ -76,6 +76,6 @@ public class AtomicDetailedTimeIncrementalSamples<T> extends AtomicTimeIncrement
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,
currentSampleStartTime, totalEvents, clonedDetailedAtomicTimeSamples);
currentSampleStartTime, totalEvents, clonedDetailedAtomicTimeSamples, isSnapshot);
}
}

View File

@ -4,6 +4,7 @@ import java.util.Arrays;
public class AtomicTimeAbsoluteSamples implements AtomicTimeAbsoluteSamplesSnapshot {
protected final boolean isSnapshot;
protected long startTime;
protected final long[] samples;
protected final int sampleTime;
@ -22,19 +23,26 @@ public class AtomicTimeAbsoluteSamples implements AtomicTimeAbsoluteSamplesSnaps
startTime = -1;
if (samplesCount < 1) throw new IndexOutOfBoundsException();
if (sampleTime < 1) throw new IndexOutOfBoundsException();
this.isSnapshot = false;
}
public AtomicTimeAbsoluteSamples(long startTime, long[] samples, int sampleTime, long currentSampleStartTime, long totalSamplesSum, long totalSamplesCount) {
public AtomicTimeAbsoluteSamples(long startTime, long[] samples, int sampleTime, long currentSampleStartTime, long totalSamplesSum, long totalSamplesCount, boolean isSnapshot) {
this.startTime = startTime;
this.samples = samples;
this.sampleTime = sampleTime;
this.currentSampleStartTime = currentSampleStartTime;
this.totalSamplesSum = totalSamplesSum;
this.totalSamplesCount = totalSamplesCount;
this.isSnapshot = isSnapshot;
}
protected synchronized void updateSamples() {
checkStarted();
if (isSnapshot) {
return;
}
long currentTime = System.nanoTime() / 1000000L;
long timeDiff = currentTime - currentSampleStartTime;
long timeToShift = timeDiff - (timeDiff % sampleTime);
@ -107,6 +115,6 @@ public class AtomicTimeAbsoluteSamples implements AtomicTimeAbsoluteSamplesSnaps
}
public synchronized AtomicTimeAbsoluteSamplesSnapshot snapshot() {
return new AtomicTimeAbsoluteSamples(startTime, Arrays.copyOf(this.samples, this.samples.length), sampleTime, currentSampleStartTime, totalSamplesSum, totalSamplesCount);
return new AtomicTimeAbsoluteSamples(startTime, Arrays.copyOf(this.samples, this.samples.length), sampleTime, currentSampleStartTime, totalSamplesSum, totalSamplesCount, true);
}
}

View File

@ -4,6 +4,7 @@ import java.util.Arrays;
public class AtomicTimeIncrementalSamples implements AtomicTimeIncrementalSamplesSnapshot {
protected final boolean isSnapshot;
protected long startTime;
protected final long[] samples;
protected final int sampleTime;
@ -21,18 +22,25 @@ public class AtomicTimeIncrementalSamples implements AtomicTimeIncrementalSample
startTime = -1;
if (samplesCount < 1) throw new IndexOutOfBoundsException();
if (sampleTime < 1) throw new IndexOutOfBoundsException();
this.isSnapshot = false;
}
public AtomicTimeIncrementalSamples(long startTime, long[] samples, int sampleTime, long currentSampleStartTime, long totalEvents) {
public AtomicTimeIncrementalSamples(long startTime, long[] samples, int sampleTime, long currentSampleStartTime, long totalEvents, boolean isSnapshot) {
this.startTime = startTime;
this.samples = samples;
this.sampleTime = sampleTime;
this.currentSampleStartTime = currentSampleStartTime;
this.totalEvents = totalEvents;
this.isSnapshot = isSnapshot;
}
protected synchronized void updateSamples() {
checkStarted();
if (isSnapshot) {
return;
}
long currentTime = System.nanoTime() / 1000000L;
long timeDiff = currentTime - currentSampleStartTime;
long timeToShift = timeDiff - (timeDiff % sampleTime);
@ -116,6 +124,6 @@ public class AtomicTimeIncrementalSamples implements AtomicTimeIncrementalSample
}
public synchronized AtomicTimeIncrementalSamplesSnapshot snapshot() {
return new AtomicTimeIncrementalSamples(startTime, Arrays.copyOf(this.samples, this.samples.length), sampleTime, currentSampleStartTime, totalEvents);
return new AtomicTimeIncrementalSamples(startTime, Arrays.copyOf(this.samples, this.samples.length), sampleTime, currentSampleStartTime, totalEvents, true);
}
}