/// <summary> /// Times and records the duration of an event /// </summary> /// <param name="action">a <see cref="Action"/> whose <see cref="Action.Invoke"/> is timed</param> public void Time(Action action) { long startTime = clock.getTick(); try { action.Invoke(); } finally { update(this.clock.getTick() - startTime); } }
/// <summary> /// Updates the timer with the difference between current and start time. Call to this method will /// not reset the start time. Multiple calls result in multiple updates. /// </summary> /// <returns>the elapsed time in nanoseconds</returns> public long stop() { long elapsed = clock.getTick() - startTime; timer.Update(elapsed, TimeUnit.Nanoseconds); return(elapsed); }
private void rescaleIfNeeded() { var now = clock.getTick(); var next = _nextScaleTime.Get(); if (now >= next) { Rescale(now, next); } }
/// <summary> /// /// Creates a new ExponentiallyDecayingReservoir /// </summary> /// <param name="size">The number of samples to keep in the sampling reservoir</param> /// <param name="alpha">The exponential decay factor; the higher this is, the more biased the sample will be towards newer values</param> /// <param name="clock">the clock used to timestamp samples and track rescaling</param> public ExponentiallyDecayingReservoir(int size, double alpha, Clock clock) { _values = new ConcurrentDictionary <double, WeightedSample>(); _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); _alpha = alpha; _size = size; this.clock = clock; this._count = new AtomicLong(0); this._startTime = CurrentTimeInSeconds(); this._nextScaleTime = new AtomicLong(clock.getTick() + RESCALE_THRESHOLD); }
/// <summary> /// /// Creates a new ExponentiallyDecayingReservoir /// </summary> /// <param name="size">The number of samples to keep in the sampling reservoir</param> /// <param name="alpha">The exponential decay factor; the higher this is, the more biased the sample will be towards newer values</param> /// <param name="clock">the clock used to timestamp samples and track rescaling</param> public ExponentiallyDecayingReservoir(int size, double alpha, Clock clock) { _values = new ConcurrentDictionary<double, WeightedSample>(); _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); _alpha = alpha; _size = size; this.clock = clock; this._count = new AtomicLong(0); this._startTime = CurrentTimeInSeconds(); this._nextScaleTime = new AtomicLong(clock.getTick() + RESCALE_THRESHOLD); }
private bool shouldLoad() { for (;;) { long time = clock.getTick(); long currentReload = reloadAt.Get(); if (currentReload > time) { return(false); } if (reloadAt.CompareAndSet(currentReload, time + timeoutNS)) { return(true); } } }
private void tickIfNecessary() { long oldTick = _lastTick.Get(); long newTick = clock.getTick(); long age = newTick - oldTick; if (age > TICK_INTERVAL) { long newIntervalStartTick = newTick - age % TICK_INTERVAL; if (_lastTick.CompareAndSet(oldTick, newIntervalStartTick)) { long requiredTicks = age / TICK_INTERVAL; for (long i = 0; i < requiredTicks; i++) { _m1Rate.Tick(); _m5Rate.Tick(); _m15Rate.Tick(); } } } }
internal Context(Timer timer, Clock clock) { this.timer = timer; this.clock = clock; this.startTime = clock.getTick(); }