예제 #1
1
    public void should_report_the_rate_of_change() {
      var minute = TimeSpan.FromMinutes(1);
      var config = new MetricConfig("counter1");
      var clock = new StepClock(TimeSpan.FromMinutes(1));
      var context = new MetricContext(clock);
      var counter = new StepCounter(config, context);
      
      clock.TickNow(1);
      counter.Increment(10);

      Measure measure = Testing.Sync<Measure>(counter, counter.GetMeasure,
        counter.context_);
      Assert.That(measure.Value, Is.EqualTo(10d/minute.Ticks));
      
      counter.OnStep();
      clock.TickNow(1);
      counter.Increment(10);

      measure = Testing.Sync<Measure>(counter, counter.GetMeasure,
        counter.context_);
      Assert.That(measure.Value, Is.EqualTo(10d/minute.Ticks),
        "Should report the same value as previously, since the rate was the same.");
      
      counter.OnStep();
      clock.TickNow(1);
      counter.Increment(20);

      measure = Testing.Sync<Measure>(counter, counter.GetMeasure,
        counter.context_);
      Assert.That(measure.Value, Is.EqualTo(10d/minute.Ticks*2),
        "Should report the double of the previously value, since the rate was doubled.");
    }
예제 #2
0
        public void should_report_the_rate_of_change()
        {
            var minute  = TimeSpan.FromMinutes(1);
            var config  = new MetricConfig("counter1");
            var clock   = new StepClock(TimeSpan.FromMinutes(1));
            var context = new MetricContext(clock);
            var counter = new StepCounter(config, context);

            clock.TickNow(1);
            counter.Increment(10);

            Measure measure = Testing.Sync <Measure>(counter, counter.GetMeasure,
                                                     counter.context_);

            Assert.That(measure.Value, Is.EqualTo(10d / minute.Ticks));

            counter.OnStep();
            clock.TickNow(1);
            counter.Increment(10);

            measure = Testing.Sync <Measure>(counter, counter.GetMeasure,
                                             counter.context_);
            Assert.That(measure.Value, Is.EqualTo(10d / minute.Ticks),
                        "Should report the same value as previously, since the rate was the same.");

            counter.OnStep();
            clock.TickNow(1);
            counter.Increment(20);

            measure = Testing.Sync <Measure>(counter, counter.GetMeasure,
                                             counter.context_);
            Assert.That(measure.Value, Is.EqualTo(10d / minute.Ticks * 2),
                        "Should report the double of the previously value, since the rate was doubled.");
        }
예제 #3
0
        public static T Sync <T>(IMetric metric, Action <Action <T> > async,
                                 MetricContext context)
        {
            var signaler = new ManualResetEventSlim(false);
            T   result   = default(T);

            async(arg1 => result = arg1);
            context.Send(signaler.Set);
            signaler.Wait();
            return(result);
        }
예제 #4
0
        public async Task TestTimerReadWithoutCreation()
        {
            MetricEvent?metricEvent   = null;
            var         metricContext = new MetricContext(new MetricContextConfig(new AdHocMetricEventSender(e => metricEvent = e)));
            var         storage       = GetStorage(metricContext);
            var         entity        = GetEntity();
            await storage.TryReadAsync(entity.Id);

            metricEvent.Should().NotBeNull();
            metricEvent !.Tags.Count.Should().Be(1);
            metricEvent !.Tags.First().Value.Should().Be("Read.SingleEntry.Time");
            metricEvent !.Unit.Should().Be("seconds");
            metricEvent !.Value.Should().BeLessThan(1);
        }
예제 #5
0
        public async Task TestTimerCreateMany()
        {
            MetricEvent?metricEvent   = null;
            var         metricContext = new MetricContext(new MetricContextConfig(new AdHocMetricEventSender(e => metricEvent = e)));
            var         storage       = GetStorage(metricContext);
            var         entities      = GetEntities();
            await storage.CreateOrUpdateAsync(entities);

            metricEvent.Should().NotBeNull();
            metricEvent !.Tags.Count.Should().Be(1);
            metricEvent !.Tags.First().Value.Should().Be("CreateOrUpdate.Entries.Time");
            metricEvent !.Unit.Should().Be("seconds");
            metricEvent !.Value.Should().BeLessThan(1);
        }
예제 #6
0
        public void Should_report_host_metrics(Action <HostMetricsSettings> settingsSetup)
        {
            var settings  = HostMetricsSettings.CreateDisabled();
            var exception = null as Exception;
            var context   = new MetricContext(new MetricContextConfig(new DevNullMetricEventSender())
            {
                ErrorCallback = contextException => exception = contextException
            });

            settingsSetup(settings);
            collector = new HostMetricsCollector(settings);
            collector.ReportMetrics(context, 1.Milliseconds());
            Task.Delay(100).Wait();

            exception.Should().BeNull();
        }
예제 #7
0
        public async Task TestTimerFind()
        {
            MetricEvent?metricEvent   = null;
            var         metricContext = new MetricContext(new MetricContextConfig(new AdHocMetricEventSender(e => metricEvent = e)));
            var         storage       = GetStorage(metricContext);

            var entities = GetEntities();
            await storage.CreateOrUpdateAsync(entities);

            await storage.FindAsync(e => e.StringProperty == stringProperty, entitiesCount);

            metricEvent.Should().NotBeNull();
            metricEvent !.Tags.Count.Should().Be(1);
            metricEvent !.Tags.First().Value.Should().Be("Find.ByCriterion.Time");
            metricEvent !.Unit.Should().Be("seconds");
            metricEvent !.Value.Should().BeLessThan(1);
        }
예제 #8
0
        public void should_measure_the_total_time_per_step()
        {
            var minute  = TimeSpan.FromMinutes(1);
            var config  = new MetricConfig("counter1");
            var clock   = new StepClock(TimeSpan.FromMinutes(1));
            var context = new MetricContext(clock);
            var timer   =
                new BucketTimer.Builder(config)
                .WithBuckets(new long[] { 60, 120, 180 })
                .WithTimeUnit(TimeUnit.Seconds)
                .WithMeasureUnit(TimeUnit.Minutes)
                .WithContext(context)
                .Build();

            IMetric total =
                timer
                .Metrics
                .First(
                    x => x.Config.Tags.FirstOrDefault(t => t.Value == "total") != null);

            clock.TickNow(1);

            timer.Update(TimeSpan.FromSeconds(10));
            timer.Update(TimeSpan.FromSeconds(10));
            timer.Update(TimeSpan.FromSeconds(10));

            var measure = Testing.Sync <Measure>(total, total.GetMeasure, context);

            Assert.That(measure.Value, Is.EqualTo(30d / 60));

            OnStep(total);
            clock.TickNow(1);

            timer.Update(TimeSpan.FromSeconds(30));

            measure = Testing.Sync <Measure>(total, total.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(30d / 60));

            OnStep(total);
            clock.TickNow(1);

            timer.Update(TimeSpan.FromSeconds(60));

            measure = Testing.Sync <Measure>(total, total.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(1));
        }
예제 #9
0
    public void should_measure_the_total_time_per_step() {
      var minute = TimeSpan.FromMinutes(1);
      var config = new MetricConfig("counter1");
      var clock = new StepClock(TimeSpan.FromMinutes(1));
      var context = new MetricContext(clock);
      var timer =
        new BucketTimer.Builder(config)
          .WithBuckets(new long[] {60, 120, 180})
          .WithTimeUnit(TimeUnit.Seconds)
          .WithMeasureUnit(TimeUnit.Minutes)
          .WithContext(context)
          .Build();

      IMetric total =
        timer
          .Metrics
          .First(
            x => x.Config.Tags.FirstOrDefault(t => t.Value == "total") != null);

      clock.TickNow(1);

      timer.Update(TimeSpan.FromSeconds(10));
      timer.Update(TimeSpan.FromSeconds(10));
      timer.Update(TimeSpan.FromSeconds(10));

      var measure = Testing.Sync<Measure>(total, total.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(30d/60));
      
      OnStep(total);
      clock.TickNow(1);

      timer.Update(TimeSpan.FromSeconds(30));

      measure = Testing.Sync<Measure>(total, total.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(30d / 60));

      OnStep(total);
      clock.TickNow(1);

      timer.Update(TimeSpan.FromSeconds(60));

      measure = Testing.Sync<Measure>(total, total.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(1));
    }
예제 #10
0
        public IVostokApplicationMetrics Build(BuildContext context)
        {
            var sender = BuildCompositeMetricEventSender(context);

            if (sender == null)
            {
                return(new VostokApplicationMetrics(new DevNullMetricContext(), context.ApplicationIdentity));
            }

            var settings = new MetricContextConfig(sender)
            {
                ErrorCallback = e => context.Log.ForContext <MetricContext>().Error(e, "Failed to send metrics.")
            };

            settingsCustomization.Customize(settings);

            var root = new MetricContext(settings);

            return(new VostokApplicationMetrics(root, context.ApplicationIdentity));
        }
예제 #11
0
 private ConcurrentSqlStorage <TestValueTypedPropertiesStorageElement, Guid> GetStorage(MetricContext metricContext) =>
 new ConcurrentSqlStorage <TestValueTypedPropertiesStorageElement, Guid>(createDbContext, metricContext);
예제 #12
0
 public MetricItemsController(MetricContext context)
 {
     _context = context;
 }
예제 #13
0
    public void should_count_the_number_of_times_a_bucket_was_hit() {
      var minute = TimeSpan.FromMinutes(1);
      var config = new MetricConfig("counter1");
      var clock = new StepClock(TimeSpan.FromMinutes(3));
      var context = new MetricContext(clock);
      var timer =
        new BucketTimer.Builder(config)
          .WithBuckets(new long[] { 60, 120, 180 })
          .WithTimeUnit(TimeUnit.Seconds)
          .WithMeasureUnit(TimeUnit.Minutes)
          .WithContext(context)
          .Build();

      IMetric b60 = GetMetricWithTag(timer, "bucket=060s");
      IMetric b120 = GetMetricWithTag(timer, "bucket=120s");
      IMetric b180 = GetMetricWithTag(timer, "bucket=180s");

      clock.TickNow(1);

      timer.Update(TimeSpan.FromSeconds(10));
      timer.Update(TimeSpan.FromSeconds(10));
      timer.Update(TimeSpan.FromSeconds(10));

      var measure = Testing.Sync<Measure>(b60, b60.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(1));

      measure = Testing.Sync<Measure>(b120, b120.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(0));

      measure = Testing.Sync<Measure>(b180, b180.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(0));

      OnStep(b60, b120, b180);
      clock.TickNow(1);

      timer.Update(TimeSpan.FromSeconds(30));
      timer.Update(TimeSpan.FromSeconds(61));
      timer.Update(TimeSpan.FromSeconds(65));

      measure = Testing.Sync<Measure>(b60, b60.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(1/3d));

      measure = Testing.Sync<Measure>(b120, b120.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(2/3d));

      measure = Testing.Sync<Measure>(b180, b180.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(0));

      OnStep(b60, b120, b180);
      clock.TickNow(1);

      timer.Update(TimeSpan.FromSeconds(180));

      measure = Testing.Sync<Measure>(b60, b60.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(0));

      measure = Testing.Sync<Measure>(b120, b120.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(0));

      measure = Testing.Sync<Measure>(b180, b180.GetMeasure, context);
      Assert.That(measure.Value, Is.EqualTo(1/3d));
    }
예제 #14
0
 public static double Sync(IMetric metric, Action <Action <Measure> > async,
                           MetricContext context)
 {
     return(Sync <Measure>(metric, async, context).Value);
 }
예제 #15
0
        public void should_count_the_number_of_times_a_bucket_was_hit()
        {
            var minute  = TimeSpan.FromMinutes(1);
            var config  = new MetricConfig("counter1");
            var clock   = new StepClock(TimeSpan.FromMinutes(3));
            var context = new MetricContext(clock);
            var timer   =
                new BucketTimer.Builder(config)
                .WithBuckets(new long[] { 60, 120, 180 })
                .WithTimeUnit(TimeUnit.Seconds)
                .WithMeasureUnit(TimeUnit.Minutes)
                .WithContext(context)
                .Build();

            IMetric b60  = GetMetricWithTag(timer, "bucket=060s");
            IMetric b120 = GetMetricWithTag(timer, "bucket=120s");
            IMetric b180 = GetMetricWithTag(timer, "bucket=180s");

            clock.TickNow(1);

            timer.Update(TimeSpan.FromSeconds(10));
            timer.Update(TimeSpan.FromSeconds(10));
            timer.Update(TimeSpan.FromSeconds(10));

            var measure = Testing.Sync <Measure>(b60, b60.GetMeasure, context);

            Assert.That(measure.Value, Is.EqualTo(1));

            measure = Testing.Sync <Measure>(b120, b120.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(0));

            measure = Testing.Sync <Measure>(b180, b180.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(0));

            OnStep(b60, b120, b180);
            clock.TickNow(1);

            timer.Update(TimeSpan.FromSeconds(30));
            timer.Update(TimeSpan.FromSeconds(61));
            timer.Update(TimeSpan.FromSeconds(65));

            measure = Testing.Sync <Measure>(b60, b60.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(1 / 3d));

            measure = Testing.Sync <Measure>(b120, b120.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(2 / 3d));

            measure = Testing.Sync <Measure>(b180, b180.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(0));

            OnStep(b60, b120, b180);
            clock.TickNow(1);

            timer.Update(TimeSpan.FromSeconds(180));

            measure = Testing.Sync <Measure>(b60, b60.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(0));

            measure = Testing.Sync <Measure>(b120, b120.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(0));

            measure = Testing.Sync <Measure>(b180, b180.GetMeasure, context);
            Assert.That(measure.Value, Is.EqualTo(1 / 3d));
        }