public void CounterCanBeIncrementedOnMultipleThreads() { const int threadCount = 16; const long iterations = 1000 * 100; var counter = new CounterMetric(); List<Thread> threads = new List<Thread>(); TaskCompletionSource<int> tcs = new TaskCompletionSource<int>(); for (int i = 0; i < threadCount; i++) { threads.Add(new Thread(s => { tcs.Task.Wait(); for (long j = 0; j < iterations; j++) { counter.Increment(); } })); } threads.ForEach(t => t.Start()); tcs.SetResult(0); threads.ForEach(t => t.Join()); counter.Value.Should().Be(threadCount * iterations); }
public NancyAdapterGlobalMetrics() { this.clock = new TestClock(); this.scheduler = new TestScheduler(clock); this.timer = new TimerMetric(SamplingType.SlidingWindow, new MeterMetric(clock, scheduler), clock); this.meter = new MeterMetric(clock, scheduler); this.counter = new CounterMetric(); this.size = new HistogramMetric(); this.browser = new Browser(with => { with.ApplicationStartup((c, p) => { Metric.Config.WithNancy(new TestRegistry { TimerInstance = timer, MeterInstance = meter, CounterInstance = counter, HistogramInstance = size }, nancy => nancy.WithGlobalMetrics(config => config.RegisterAllMetrics(p))); }); with.Module(new TestModule(this.clock)); with.Module(new ActiveRequestsModule(this.requestTrigger.Task, result1, result2)); }); }
public void CounterCanDecrementMultipleTimes() { var counter = new CounterMetric(); counter.Decrement(); counter.Decrement(); counter.Decrement(); counter.Value.Should().Be(-3L); }
internal void Increment(CounterMetric metric) { // Notes: // - If we have long periods with no stats, we won't reset until we get one. // - This is a "pretty close" implementation. // - Accuracy on Gets isn't 100% and is subject to racing. // - We may write metrics into "old" buckets immediately before resetting at the interval. if (_clock.GetMillisecondTimestamp() - _lastResetAtTime > _periodMillis.Value) { Reset(); } // See note in Reset() about potential for losing current window counts here. _counters[(int) metric].Increment(); }
public void CounterCanDecrementWithValue() { var counter = new CounterMetric(); counter.Decrement(32L); counter.Value.Should().Be(-32L); }
public void CounterCanIncrement() { var counter = new CounterMetric(); counter.Increment(); counter.Value.Should().Be(1L); }
internal long GetCount(CounterMetric metric) { return _counters[(int) metric].Get(); }
/// <summary> /// A counter is a cumulative metric that represents a single monotonically increasing /// counter whose value can only increase or be reset to zero on restart. For example, you /// can use a counter to represent the number of requests served, tasks completed, or errors. /// </summary> private void IncrementCounter(CounterMetric metric) { Console.WriteLine($"[CounterMetric]: {metric.Name}, Value: {metric.Value}"); }