Пример #1
0
        public virtual void TestMutableRates()
        {
            MetricsRecordBuilder rb       = MetricsAsserts.MockMetricsRecordBuilder();
            MetricsRegistry      registry = new MetricsRegistry("test");
            MutableRates         rates    = new MutableRates(registry);

            rates.Init(typeof(TestMutableMetrics.TestProtocol));
            registry.Snapshot(rb, false);
            MetricsAsserts.AssertCounter("FooNumOps", 0L, rb);
            MetricsAsserts.AssertGauge("FooAvgTime", 0.0, rb);
            MetricsAsserts.AssertCounter("BarNumOps", 0L, rb);
            MetricsAsserts.AssertGauge("BarAvgTime", 0.0, rb);
        }
Пример #2
0
        internal virtual MutableMetric NewForMethod(object source, MethodInfo method, Metric
                                                    annotation, MetricsRegistry registry)
        {
            if (Log.IsDebugEnabled())
            {
                Log.Debug("method " + method + " with annotation " + annotation);
            }
            MetricsInfo   info   = GetInfo(annotation, method);
            MutableMetric metric = NewForMethod(source, method, annotation);

            metric = metric != null ? metric : new MethodMetric(source, method, info, annotation
                                                                .Type());
            registry.Add(info.Name(), metric);
            return(metric);
        }
Пример #3
0
        public virtual void TestAddByName()
        {
            MetricsRecordBuilder rb = MetricsAsserts.MockMetricsRecordBuilder();
            MetricsRegistry      r  = new MetricsRegistry("test");

            r.Add("s1", 42);
            r.Get("s1").Snapshot(rb);
            Org.Mockito.Mockito.Verify(rb).AddCounter(Interns.Info("S1NumOps", "Number of ops for s1"
                                                                   ), 1L);
            Org.Mockito.Mockito.Verify(rb).AddGauge(Interns.Info("S1AvgTime", "Average time for s1"
                                                                 ), 42.0);
            r.NewCounter("c1", "test add", 1);
            r.NewGauge("g1", "test add", 1);
            ExpectMetricsException("Unsupported add", new _Runnable_114(r));
            ExpectMetricsException("Unsupported add", new _Runnable_119(r));
        }
Пример #4
0
        /// <summary>
        /// Ensure that quantile estimates from
        /// <see cref="MutableQuantiles"/>
        /// are within
        /// specified error bounds.
        /// </summary>
        /// <exception cref="System.Exception"/>
        public virtual void TestMutableQuantilesError()
        {
            MetricsRecordBuilder mb       = MetricsAsserts.MockMetricsRecordBuilder();
            MetricsRegistry      registry = new MetricsRegistry("test");
            // Use a 5s rollover period
            MutableQuantiles quantiles = registry.NewQuantiles("foo", "stat", "Ops", "Latency"
                                                               , 5);
            // Push some values in and wait for it to publish
            long start = Runtime.NanoTime() / 1000000;

            for (long i = 1; i <= 1000; i++)
            {
                quantiles.Add(i);
                quantiles.Add(1001 - i);
            }
            long end = Runtime.NanoTime() / 1000000;

            Thread.Sleep(6000 - (end - start));
            registry.Snapshot(mb, false);
            // Print out the snapshot
            IDictionary <Quantile, long> previousSnapshot = quantiles.previousSnapshot;

            foreach (KeyValuePair <Quantile, long> item in previousSnapshot)
            {
                System.Console.Out.WriteLine(string.Format("Quantile %.2f has value %d", item.Key
                                                           .quantile, item.Value));
            }
            // Verify the results are within our requirements
            Org.Mockito.Mockito.Verify(mb).AddGauge(Interns.Info("FooNumOps", "Number of ops for stat with 5s interval"
                                                                 ), (long)2000);
            Quantile[] quants = MutableQuantiles.quantiles;
            string     name   = "Foo%dthPercentileLatency";
            string     desc   = "%d percentile latency with 5 second interval for stat";

            foreach (Quantile q in quants)
            {
                int    percentile = (int)(100 * q.quantile);
                int    error      = (int)(1000 * q.error);
                string n          = string.Format(name, percentile);
                string d          = string.Format(desc, percentile);
                long   expected   = (long)(q.quantile * 1000);
                Org.Mockito.Mockito.Verify(mb).AddGauge(Matchers.Eq(Interns.Info(n, d)), AdditionalMatchers.Leq
                                                            (expected + error));
                Org.Mockito.Mockito.Verify(mb).AddGauge(Matchers.Eq(Interns.Info(n, d)), AdditionalMatchers.Geq
                                                            (expected - error));
            }
        }
Пример #5
0
        public virtual void TestSnapshot()
        {
            MetricsRecordBuilder mb       = MetricsAsserts.MockMetricsRecordBuilder();
            MetricsRegistry      registry = new MetricsRegistry("test");

            registry.NewCounter("c1", "int counter", 1);
            registry.NewCounter("c2", "long counter", 2L);
            registry.NewGauge("g1", "int gauge", 3);
            registry.NewGauge("g2", "long gauge", 4L);
            registry.NewStat("s1", "stat", "Ops", "Time", true).Add(0);
            registry.NewRate("s2", "stat", false).Add(0);
            registry.Snapshot(mb, true);
            MutableStat s2 = (MutableStat)registry.Get("s2");

            s2.Snapshot(mb, true);
            // should get the same back.
            s2.Add(1);
            s2.Snapshot(mb, true);
            // should get new interval values back
            Org.Mockito.Mockito.Verify(mb).AddCounter(Interns.Info("c1", "int counter"), 1);
            Org.Mockito.Mockito.Verify(mb).AddCounter(Interns.Info("c2", "long counter"), 2L);
            Org.Mockito.Mockito.Verify(mb).AddGauge(Interns.Info("g1", "int gauge"), 3);
            Org.Mockito.Mockito.Verify(mb).AddGauge(Interns.Info("g2", "long gauge"), 4L);
            Org.Mockito.Mockito.Verify(mb).AddCounter(Interns.Info("S1NumOps", "Number of ops for stat"
                                                                   ), 1L);
            Org.Mockito.Mockito.Verify(mb).AddGauge(Matchers.Eq(Interns.Info("S1AvgTime", "Average time for stat"
                                                                             )), AdditionalMatchers.Eq(0.0, Epsilon));
            Org.Mockito.Mockito.Verify(mb).AddGauge(Matchers.Eq(Interns.Info("S1StdevTime", "Standard deviation of time for stat"
                                                                             )), AdditionalMatchers.Eq(0.0, Epsilon));
            Org.Mockito.Mockito.Verify(mb).AddGauge(Matchers.Eq(Interns.Info("S1IMinTime", "Interval min time for stat"
                                                                             )), AdditionalMatchers.Eq(0.0, Epsilon));
            Org.Mockito.Mockito.Verify(mb).AddGauge(Matchers.Eq(Interns.Info("S1IMaxTime", "Interval max time for stat"
                                                                             )), AdditionalMatchers.Eq(0.0, Epsilon));
            Org.Mockito.Mockito.Verify(mb).AddGauge(Matchers.Eq(Interns.Info("S1MinTime", "Min time for stat"
                                                                             )), AdditionalMatchers.Eq(0.0, Epsilon));
            Org.Mockito.Mockito.Verify(mb).AddGauge(Matchers.Eq(Interns.Info("S1MaxTime", "Max time for stat"
                                                                             )), AdditionalMatchers.Eq(0.0, Epsilon));
            Org.Mockito.Mockito.Verify(mb, Org.Mockito.Mockito.Times(2)).AddCounter(Interns.Info
                                                                                        ("S2NumOps", "Number of ops for stat"), 1L);
            Org.Mockito.Mockito.Verify(mb, Org.Mockito.Mockito.Times(2)).AddGauge(Matchers.Eq
                                                                                      (Interns.Info("S2AvgTime", "Average time for stat")), AdditionalMatchers.Eq(0.0,
                                                                                                                                                                  Epsilon));
            Org.Mockito.Mockito.Verify(mb).AddCounter(Interns.Info("S2NumOps", "Number of ops for stat"
                                                                   ), 2L);
            Org.Mockito.Mockito.Verify(mb).AddGauge(Matchers.Eq(Interns.Info("S2AvgTime", "Average time for stat"
                                                                             )), AdditionalMatchers.Eq(1.0, Epsilon));
        }
Пример #6
0
        /// <summary>
        /// Test that
        /// <see cref="MutableQuantiles"/>
        /// rolls the window over at the specified
        /// interval.
        /// </summary>
        /// <exception cref="System.Exception"/>
        public virtual void TestMutableQuantilesRollover()
        {
            MetricsRecordBuilder mb       = MetricsAsserts.MockMetricsRecordBuilder();
            MetricsRegistry      registry = new MetricsRegistry("test");
            // Use a 5s rollover period
            MutableQuantiles quantiles = registry.NewQuantiles("foo", "stat", "Ops", "Latency"
                                                               , 5);

            Quantile[] quants = MutableQuantiles.quantiles;
            string     name   = "Foo%dthPercentileLatency";
            string     desc   = "%d percentile latency with 5 second interval for stat";
            // Push values for three intervals
            long start = Runtime.NanoTime() / 1000000;

            for (int i = 1; i <= 3; i++)
            {
                // Insert the values
                for (long j = 1; j <= 1000; j++)
                {
                    quantiles.Add(i);
                }
                // Sleep until 1s after the next 5s interval, to let the metrics
                // roll over
                long sleep = (start + (5000 * i) + 1000) - (Runtime.NanoTime() / 1000000);
                Thread.Sleep(sleep);
                // Verify that the window reset, check it has the values we pushed in
                registry.Snapshot(mb, false);
                foreach (Quantile q in quants)
                {
                    int    percentile = (int)(100 * q.quantile);
                    string n          = string.Format(name, percentile);
                    string d          = string.Format(desc, percentile);
                    Org.Mockito.Mockito.Verify(mb).AddGauge(Interns.Info(n, d), (long)i);
                }
            }
            // Verify the metrics were added the right number of times
            Org.Mockito.Mockito.Verify(mb, Org.Mockito.Mockito.Times(3)).AddGauge(Interns.Info
                                                                                      ("FooNumOps", "Number of ops for stat with 5s interval"), (long)1000);
            foreach (Quantile q_1 in quants)
            {
                int    percentile = (int)(100 * q_1.quantile);
                string n          = string.Format(name, percentile);
                string d          = string.Format(desc, percentile);
                Org.Mockito.Mockito.Verify(mb, Org.Mockito.Mockito.Times(3)).AddGauge(Matchers.Eq
                                                                                          (Interns.Info(n, d)), Matchers.AnyLong());
            }
        }
Пример #7
0
        /// <summary>
        /// Test that
        /// <see cref="MutableQuantiles"/>
        /// rolls over correctly even if no items
        /// have been added to the window
        /// </summary>
        /// <exception cref="System.Exception"/>
        public virtual void TestMutableQuantilesEmptyRollover()
        {
            MetricsRecordBuilder mb       = MetricsAsserts.MockMetricsRecordBuilder();
            MetricsRegistry      registry = new MetricsRegistry("test");
            // Use a 5s rollover period
            MutableQuantiles quantiles = registry.NewQuantiles("foo", "stat", "Ops", "Latency"
                                                               , 5);

            // Check it initially
            quantiles.Snapshot(mb, true);
            Org.Mockito.Mockito.Verify(mb).AddGauge(Interns.Info("FooNumOps", "Number of ops for stat with 5s interval"
                                                                 ), (long)0);
            Thread.Sleep(6000);
            quantiles.Snapshot(mb, false);
            Org.Mockito.Mockito.Verify(mb, Org.Mockito.Mockito.Times(2)).AddGauge(Interns.Info
                                                                                      ("FooNumOps", "Number of ops for stat with 5s interval"), (long)0);
        }
Пример #8
0
        public virtual void TestNewMetrics()
        {
            MetricsRegistry r = new MetricsRegistry("test");

            r.NewCounter("c1", "c1 desc", 1);
            r.NewCounter("c2", "c2 desc", 2L);
            r.NewGauge("g1", "g1 desc", 3);
            r.NewGauge("g2", "g2 desc", 4L);
            r.NewStat("s1", "s1 desc", "ops", "time");
            Assert.Equal("num metrics in registry", 5, r.Metrics().Count);
            Assert.True("c1 found", r.Get("c1") is MutableCounterInt);
            Assert.True("c2 found", r.Get("c2") is MutableCounterLong);
            Assert.True("g1 found", r.Get("g1") is MutableGaugeInt);
            Assert.True("g2 found", r.Get("g2") is MutableGaugeLong);
            Assert.True("s1 found", r.Get("s1") is MutableStat);
            ExpectMetricsException("Metric name c1 already exists", new _Runnable_54(r));
        }
Пример #9
0
        private MetricsRegistry InitRegistry(object source)
        {
            Type            cls = source.GetType();
            MetricsRegistry r   = null;

            // Get the registry if it already exists.
            foreach (FieldInfo field in ReflectionUtils.GetDeclaredFieldsIncludingInherited(cls
                                                                                            ))
            {
                if (field.FieldType != typeof(MetricsRegistry))
                {
                    continue;
                }
                try
                {
                    r           = (MetricsRegistry)field.GetValue(source);
                    hasRegistry = r != null;
                    break;
                }
                catch (Exception e)
                {
                    Log.Warn("Error accessing field " + field, e);
                    continue;
                }
            }
            // Create a new registry according to annotation
            foreach (Annotation.Annotation annotation in cls.GetAnnotations())
            {
                if (annotation is Metrics)
                {
                    Metrics ma = (Metrics)annotation;
                    info = factory.GetInfo(cls, ma);
                    if (r == null)
                    {
                        r = new MetricsRegistry(info);
                    }
                    r.SetContext(ma.Context());
                }
            }
            if (r == null)
            {
                return(new MetricsRegistry(cls.Name));
            }
            return(r);
        }
Пример #10
0
        internal MetricsSourceBuilder(object source, MutableMetricsFactory factory)
        {
            this.source  = Preconditions.CheckNotNull(source, "source");
            this.factory = Preconditions.CheckNotNull(factory, "mutable metrics factory");
            Type cls = source.GetType();

            registry = InitRegistry(source);
            foreach (FieldInfo field in ReflectionUtils.GetDeclaredFieldsIncludingInherited(cls
                                                                                            ))
            {
                Add(source, field);
            }
            foreach (MethodInfo method in ReflectionUtils.GetDeclaredMethodsIncludingInherited
                         (cls))
            {
                Add(source, method);
            }
        }
Пример #11
0
        public virtual void TestMetricsRegistryIllegalMetricNames()
        {
            MetricsRegistry r = new MetricsRegistry("test");

            // Fill up with some basics
            r.NewCounter("c1", "c1 desc", 1);
            r.NewGauge("g1", "g1 desc", 1);
            r.NewQuantiles("q1", "q1 desc", "q1 name", "q1 val type", 1);
            // Add some illegal names
            ExpectMetricsException("Metric name 'badcount 2' contains " + "illegal whitespace character"
                                   , new _Runnable_72(r));
            ExpectMetricsException("Metric name 'badcount3  ' contains " + "illegal whitespace character"
                                   , new _Runnable_77(r));
            ExpectMetricsException("Metric name '  badcount4' contains " + "illegal whitespace character"
                                   , new _Runnable_82(r));
            ExpectMetricsException("Metric name 'withtab5	' contains "+ "illegal whitespace character"
                                   , new _Runnable_87(r));
            ExpectMetricsException("Metric name 'withnewline6\n' contains " + "illegal whitespace character"
                                   , new _Runnable_92(r));
            // Final validation
            Assert.Equal("num metrics in registry", 3, r.Metrics().Count);
        }
Пример #12
0
 internal MutableRates(MetricsRegistry registry)
 {
     this.registry = Preconditions.CheckNotNull(registry, "metrics registry");
 }
Пример #13
0
        internal virtual MutableMetric NewForField(FieldInfo field, Metric annotation, MetricsRegistry
                                                   registry)
        {
            if (Log.IsDebugEnabled())
            {
                Log.Debug("field " + field + " with annotation " + annotation);
            }
            MetricsInfo   info   = GetInfo(annotation, field);
            MutableMetric metric = NewForField(field, annotation);

            if (metric != null)
            {
                registry.Add(info.Name(), metric);
                return(metric);
            }
            Type cls = field.FieldType;

            if (cls == typeof(MutableCounterInt))
            {
                return(registry.NewCounter(info, 0));
            }
            if (cls == typeof(MutableCounterLong))
            {
                return(registry.NewCounter(info, 0L));
            }
            if (cls == typeof(MutableGaugeInt))
            {
                return(registry.NewGauge(info, 0));
            }
            if (cls == typeof(MutableGaugeLong))
            {
                return(registry.NewGauge(info, 0L));
            }
            if (cls == typeof(MutableRate))
            {
                return(registry.NewRate(info.Name(), info.Description(), annotation.Always()));
            }
            if (cls == typeof(MutableRates))
            {
                return(new MutableRates(registry));
            }
            if (cls == typeof(MutableStat))
            {
                return(registry.NewStat(info.Name(), info.Description(), annotation.SampleName(),
                                        annotation.ValueName(), annotation.Always()));
            }
            throw new MetricsException("Unsupported metric field " + field.Name + " of type "
                                       + field.FieldType.FullName);
        }
Пример #14
0
 public _Runnable_77(MetricsRegistry r)
 {
     this.r = r;
 }