/** Update the value. */ void set(double v) { value.set(v); }
@Override public double value() { return value.get(); } }
/** Create a new instance. */ DefaultMaxGauge(Clock clock, Id id) { this.clock = clock; this.id = id; this.value = new AtomicDouble(Double.NaN); }
@Override public void calcMeasurements(List<Measurement> measurements, long msNow, long secondInterval) { long currentCount = count.longValue(); long currentTotalTime = totalTime.longValue(); measurements.add(newMeasurement(idCount, msNow, (double) (currentCount - lastCount) / secondInterval)); measurements .add(newMeasurement(idTotalTime, msNow, (currentTotalTime - lastTotalTime) / secondInterval * CNV_SECONDS)); measurements.add(newMeasurement(idMax, msNow, max.get() * CNV_SECONDS)); lastCount = currentCount; lastTotalTime = currentTotalTime; // maybe lost some max value, but not so important? max.set(0); } }
/** Set the current value to the maximum of the current value or the provided value. */ public void max(double v) { if (Double.isFinite(v)) { double max = get(); while (isGreaterThan(v, max) && !compareAndSet(max, v)) { max = value.get(); } } }
@Test public void maxLt() { AtomicDouble v = new AtomicDouble(2.0); v.max(0.0); Assertions.assertEquals(2.0, v.get(), 1e-12); }
@Test public void set() { AtomicDouble v = new AtomicDouble(13.0); v.set(42.0); Assertions.assertEquals(42.0, v.get(), 1e-12); }
@Test public void addAndGet() { AtomicDouble v = new AtomicDouble(13.0); Assertions.assertEquals(55.0, v.addAndGet(42.0), 1e-12); Assertions.assertEquals(55.0, v.get(), 1e-12); }
@Test public void getAndSet() { AtomicDouble v = new AtomicDouble(13.0); Assertions.assertEquals(13.0, v.getAndSet(42.0), 1e-12); Assertions.assertEquals(42.0, v.get(), 1e-12); }
@Test public void compareAndSet() { AtomicDouble v = new AtomicDouble(13.0); Assertions.assertTrue(v.compareAndSet(13.0, 42.0)); Assertions.assertEquals(42.0, v.get(), 1e-12); }
@Override public void add(double amount) { if (Double.isFinite(amount) && amount > 0.0) { value.addAndGet(amount); } }
@Test public void initWithValue() { AtomicDouble v = new AtomicDouble(42.0); Assertions.assertEquals(42.0, v.get(), 1e-12); }
@Test public void getAndAdd() { AtomicDouble v = new AtomicDouble(13.0); Assertions.assertEquals(13.0, v.getAndAdd(42.0), 1e-12); Assertions.assertEquals(55.0, v.get(), 1e-12); }
public void record(long nanoAmount) { if (nanoAmount >= 0) { totalTime.add(nanoAmount); count.increment(); max.max(nanoAmount); } }
private void rollCount(long now) { final long stepTime = now / step; final long lastInit = lastInitPos.get(); if (lastInit < stepTime && lastInitPos.compareAndSet(lastInit, stepTime)) { final double v = current.getAndSet(init); // Need to check if there was any activity during the previous step interval. If there was // then the init position will move forward by 1, otherwise it will be older. No activity // means the previous interval should be set to the `init` value. previous = (lastInit == stepTime - 1) ? v : init; } }
@Test public void maxNegative() { AtomicDouble v = new AtomicDouble(-42.0); v.max(-41.0); Assertions.assertEquals(-41.0, v.get(), 1e-12); }
@Test public void compareAndSetFail() { AtomicDouble v = new AtomicDouble(13.0); Assertions.assertFalse(v.compareAndSet(12.0, 42.0)); Assertions.assertEquals(13.0, v.get(), 1e-12); }