All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.fix.aggregating.profiler.engine.MaxThroughputPerSecondAccumulator Maven / Gradle / Ivy

There is a newer version: 1.6.6
Show newest version
package ru.fix.aggregating.profiler.engine;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

public class MaxThroughputPerSecondAccumulator {
    private static final long ONE_SECOND_MS = 1_000;

    private final LongAccumulator maxEventCountPerSecond = new LongAccumulator(Math::max, 0L);
    private final LongAdder eventCountSum = new LongAdder();
    private final AtomicLong startOfSecondTimestamp = new AtomicLong();

    public void call(long currentTimestamp, long eventCount) {
        long start = startOfSecondTimestamp.get();
        if (start + ONE_SECOND_MS <= currentTimestamp) {
            if (startOfSecondTimestamp.compareAndSet(start, currentTimestamp)) {
                long sum = AdderDrainer.drain(eventCountSum);
                maxEventCountPerSecond.accumulate(sum);
            }
        }
        eventCountSum.add(eventCount);
    }

    public long getAndReset(long currentTimestamp) {
        //update counters before report return
        call(currentTimestamp, 0);
        return maxEventCountPerSecond.getThenReset();
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy