Skip to content

Commit

Permalink
Use LongAdder in CumulativeTimer and CumulativeDistributionSummary
Browse files Browse the repository at this point in the history
  • Loading branch information
izeye committed Dec 18, 2023
1 parent 019f97b commit 9209370
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import io.micrometer.core.instrument.distribution.TimeWindowMax;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.DoubleAdder;
import java.util.concurrent.atomic.LongAdder;

/**
* Cumulative distribution summary.
Expand All @@ -36,7 +36,7 @@
*/
public class CumulativeDistributionSummary extends AbstractDistributionSummary {

private final AtomicLong count;
private final LongAdder count;

private final DoubleAdder total;

Expand Down Expand Up @@ -66,21 +66,21 @@ public CumulativeDistributionSummary(Id id, Clock clock, DistributionStatisticCo
protected CumulativeDistributionSummary(Id id, Clock clock, DistributionStatisticConfig distributionStatisticConfig,
double scale, Histogram histogram) {
super(id, scale, histogram);
this.count = new AtomicLong();
this.count = new LongAdder();
this.total = new DoubleAdder();
this.max = new TimeWindowMax(clock, distributionStatisticConfig);
}

@Override
protected void recordNonNegative(double amount) {
count.incrementAndGet();
count.increment();
total.add(amount);
max.record(amount);
}

@Override
public long count() {
return count.get();
return count.longValue();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
import io.micrometer.core.instrument.util.TimeUtils;

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

/**
* @author Jon Schneider
*/
public class CumulativeTimer extends AbstractTimer {

private final AtomicLong count;
private final LongAdder count;

private final AtomicLong total;
private final LongAdder total;

private final TimeWindowMax max;

Expand Down Expand Up @@ -61,27 +61,27 @@ public CumulativeTimer(Id id, Clock clock, DistributionStatisticConfig distribut
protected CumulativeTimer(Id id, Clock clock, DistributionStatisticConfig distributionStatisticConfig,
PauseDetector pauseDetector, TimeUnit baseTimeUnit, Histogram histogram) {
super(id, clock, pauseDetector, baseTimeUnit, histogram);
this.count = new AtomicLong();
this.total = new AtomicLong();
this.count = new LongAdder();
this.total = new LongAdder();
this.max = new TimeWindowMax(clock, distributionStatisticConfig);
}

@Override
protected void recordNonNegative(long amount, TimeUnit unit) {
long nanoAmount = (long) TimeUtils.convert(amount, unit, TimeUnit.NANOSECONDS);
count.getAndAdd(1);
total.getAndAdd(nanoAmount);
count.increment();
total.add(nanoAmount);
max.record(nanoAmount, TimeUnit.NANOSECONDS);
}

@Override
public long count() {
return count.get();
return count.longValue();
}

@Override
public double totalTime(TimeUnit unit) {
return TimeUtils.nanosToUnit(total.get(), unit);
return TimeUtils.nanosToUnit(total.doubleValue(), unit);
}

@Override
Expand Down

0 comments on commit 9209370

Please sign in to comment.