Ejemplo n.º 1
0
        /// <summary>
        /// Snapshot cache metrics
        /// </summary>
        /// <param name="pagesLoaded"></param>
        public static void RecordCacheMetric(int pagesLoaded)
        {
            SampledMetricDefinition pageMetricDefinition;

            //since sampled metrics have only one value per metric, we have to create multiple metrics (one for every value)
            if (SampledMetricDefinition.TryGetValue("GibraltarSample", "Database.Engine", "Cache Pages", out pageMetricDefinition) == false)
            {
                //doesn't exist yet - add it in all of its glory.  This call is MT safe - we get back the object in cache even if registered on another thread.
                pageMetricDefinition = SampledMetricDefinition.Register("GibraltarSample", "Database.Engine", "cachePages", SamplingType.RawCount, "Pages", "Cache Pages", "The number of pages in the cache");
            }

            //now that we know we have the definitions, make sure we've defined the metric instances.
            SampledMetric pageMetric = SampledMetric.Register(pageMetricDefinition, null);

            //now go ahead and write those samples....
            pageMetric.WriteSample(pagesLoaded);

            //Continue for our second metric.
            SampledMetricDefinition sizeMetricDefinition;

            if (SampledMetricDefinition.TryGetValue("GibraltarSample", "Database.Engine", "Cache Size", out sizeMetricDefinition) == false)
            {
                //doesn't exist yet - add it in all of its glory  This call is MT safe - we get back the object in cache even if registered on another thread.
                sizeMetricDefinition = SampledMetricDefinition.Register("GibraltarSample", "Database.Engine", "cacheSize", SamplingType.RawCount, "Bytes", "Cache Size", "The number of bytes used by pages in the cache");
            }

            SampledMetric sizeMetric = SampledMetric.Register(sizeMetricDefinition, null);

            sizeMetric.WriteSample(pagesLoaded * 8196);
        }
Ejemplo n.º 2
0
        public void SampledMetricsByAttributesPerformanceTest()
        {
            SampledMetric.Register(typeof(UserPerformanceObject));
            Trace.TraceInformation("Sampled metrics registered by attributes.");

            UserPerformanceObject sampledObject = new UserPerformanceObject("AttributesPerformanceTest", 25, 100);

            //first, lets get everything to flush so we have our best initial state.
            Log.Information(LogWriteMode.WaitForCommit, "Test.Agent.Metrics.Performance", "Preparing for Test", "Flushing queue");

            //now that we know it's flushed everything, lets do our timed loop.
            DateTimeOffset startTime = DateTimeOffset.UtcNow;

            for (int curMessage = 0; curMessage < LoopsPerSampledTest; curMessage++)
            {
                SampledMetricDefinition.Write(sampledObject);
            }
            DateTimeOffset messageEndTime = DateTimeOffset.UtcNow;

            //one wait for commit message to force the buffer to flush.
            Log.Information(LogWriteMode.WaitForCommit, "Test.Agent.Metrics.Performance", "Waiting for Samples to Commit", null);

            //and store off our time
            DateTimeOffset endTime = DateTimeOffset.UtcNow;

            TimeSpan  testDuration    = endTime - startTime;
            TimeSpan  loopDuration    = messageEndTime - startTime;
            const int messagesPerTest = LoopsPerSampledTest * MessagesPerSampledLoop;

            Trace.TraceInformation("Sampled Metrics by Attributes Test committed {0:N0} samples in {1:F3} ms (average {2:F4} ms per message).  Average loop time {3:F4} ms ({4} samples per loop) and final flush time {5:F3} ms.",
                                   messagesPerTest, testDuration.TotalMilliseconds, (testDuration.TotalMilliseconds / messagesPerTest),
                                   (loopDuration.TotalMilliseconds / LoopsPerSampledTest), MessagesPerSampledLoop,
                                   (endTime - messageEndTime).TotalMilliseconds);
        }
Ejemplo n.º 3
0
        public void PerformanceTest()
        {
            //first, lets get everything to flush so we have our best initial state.
            Log.EndFile("Preparing for Performance Test");
            Log.Information(LogWriteMode.WaitForCommit, "Test.Agent.Metrics.Performance", "Preparing for Test", "Flushing queue");

            SampledMetric.Register(typeof(UserSampledObject));

            UserSampledObject sampledObject = new UserSampledObject(25, 100);

            //now that we know it's flushed everything, lets do our timed loop.
            DateTimeOffset startTime = DateTimeOffset.UtcNow;

            for (int curMessage = 0; curMessage < MessagesPerTest; curMessage++)
            {
                SampledMetricDefinition.Write(sampledObject);
            }
            DateTimeOffset messageEndTime = DateTimeOffset.UtcNow;

            //one wait for commit message to force the buffer to flush.
            Log.Information(LogWriteMode.WaitForCommit, "Test.Agent.Metrics.Performance", "Waiting for Samples to Commit", null);

            //and store off our time
            DateTimeOffset endTime = DateTimeOffset.UtcNow;

            TimeSpan duration = endTime - startTime;

            Trace.TraceInformation("Sampled Metrics by Attribute Test Completed in {0}ms .  {1} messages were written at an average duration of {2}ms per message.  The flush took {3}ms.",
                                   duration.TotalMilliseconds, MessagesPerTest, (duration.TotalMilliseconds) / MessagesPerTest, (endTime - messageEndTime).TotalMilliseconds);
        }
        /// <summary>
        /// Create a complete sampled metric packet
        /// </summary>
        /// <para>Metrics using a sample type of AverageFraction and DeltaFraction should not use this method because
        /// they require a base value as well as a raw value.</para>
        /// <param name="rawValue">The raw data value</param>
        /// <param name="rawTimeStamp">The exact date and time the raw value was determined</param>
        /// <param name="metric">The metric this sample is for</param>
        protected SampledMetricSamplePacket(SampledMetric metric, double rawValue, DateTimeOffset rawTimeStamp)
            : base(metric)
        {
            RawValue = rawValue;

            //we will fill in the other items if they are missing, so we don't check them for null.
            RawTimestamp = rawTimeStamp;
        }
        /// <summary>
        /// Create a complete sampled metric packet
        /// </summary>
        /// <para>Metrics using a sample type of AverageFraction and DeltaFraction should not use this method because
        /// they require a base value as well as a raw value.</para>
        /// <param name="rawValue">The raw data value</param>
        /// <param name="metric">The metric this sample is for</param>
        protected SampledMetricSamplePacket(SampledMetric metric, double rawValue)
            : base(metric)
        {
            RawValue = rawValue;

            //we will fill in the other items if they are missing, so we don't check them for null.
            RawTimestamp = DateTimeOffset.Now; //we convert to UTC during serialization, we want local time.
        }
Ejemplo n.º 6
0
        internal void Internalize(SampledMetric metric)
        {
            lock (m_Lock)
            {
                Monitor.CustomSampledMetric internalMetric = metric.WrappedMetric;

                m_Externalized[internalMetric] = metric;
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Create a new metric object with the provided instance name and add it to the collection
        /// </summary>
        /// <param name="instanceName">The instance name to use, or blank or null for the default metric.</param>
        /// <returns>The new metric object that was added to the collection</returns>
        public SampledMetric Add(string instanceName)
        {
            lock (m_Lock)
            {
                //Create a new metric object with the provided instance name (it will get added to us automatically)
                SampledMetric newMetric = new SampledMetric(m_MetricDefinition, instanceName);

                //finally, return the newly created metric object to our caller
                return(newMetric);
            }
        }
Ejemplo n.º 8
0
            protected virtual void Dispose(bool disposing) // Or override, if we had a base class implementing it also.
            {
                //base.Dispose(disposing); // But we have no base, so this doesn't exist.

                if (disposing)
                {
                    SampledMetric.Write(this); // Write one last sample when we're disposed.
                }

                m_EndThread = true; // This needs to be done by the finalizer as well as when disposed.
            }
Ejemplo n.º 9
0
        public void SampledAttributeReflectionExample()
        {
            //
            // General example usage.
            //

            // Optional registration in advance (time-consuming reflection walk to find attributes the first time).

            // Either of these two approaches will dig into base types and all interfaces seen at the top level.
            SampledMetric.Register(typeof(UserSampledAttributedClass)); // Can be registered from the Type itself.
            // Or...
            UserSampledAttributedClass anyUserSampledAttributedInstance = new UserSampledAttributedClass("any-prototype");

            SampledMetric.Register(anyUserSampledAttributedInstance); // Can register from a live instance (gets its Type automatically).

            // Elsewhere...

            UserSampledAttributedClass userSampledAttributedInstance = new UserSampledAttributedClass("automatic-instance-name"); // then...

            // To sample all valid sampled metrics defined by attributes (at all inheritance levels) for a data object instance:

            // This will determine the instance name automatically from the member marked with the EventMetricInstanceName attribute.
            // Null will be used as the instance name (the "default instance") if the attribute is not found for a particular metric.
            SampledMetric.Write(userSampledAttributedInstance); // The recommended typical usage.

            // Or... To specify a different fallback instance name if an EventMetricInstanceName attribute isn't found:
            SampledMetric.Write(userSampledAttributedInstance, "fallback-instance-if-not-assigned");



            //
            // Specific example usage for example class above.
            // Generate some meaningful data in the log to look at.
            //

            int[] testDataArray = new[] { 1, 5, 3, -4, 2, 7, -3, -2, 9, 4, -5, -1, 3, -7, 2, 4, -2, 8, 10, -4, 2 };

            // Using the "default" instance here.  This will Dispose it for us when it exits the block.
            using (UserSampledAttributedClass realUsageInstance = new UserSampledAttributedClass(null))
            {
                SampledMetric.Register(realUsageInstance); // Registering from the live object also registers the metric instance.
                realUsageInstance.StartPolling();          // Start polling thread for this one.

                foreach (int dataValue in testDataArray)
                {
                    realUsageInstance.ApplyDelta(dataValue); // This method also fires off an event metric sample for us.

                    Thread.Sleep(50 + (5 * dataValue));      // Sleep for a little while to space out the data, not entirely evenly for this example.
                }
            }

            Thread.Sleep(1000); // Give it some time to complete.
        }
Ejemplo n.º 10
0
            private void SamplePollingThreadStart()
            {
                Trace.TraceInformation("Example polling thread ({0}) started", m_InstanceName ?? "null");

                while (m_EndThread == false)
                {
                    SampledMetric.Write(this); // Write a sample of all sampled metrics defined by attributes on this object.

                    Thread.Sleep(100);         // Sleep for 0.1 seconds before sampling again.
                }

                Trace.TraceInformation("Example polling thread ({0}) ending", m_InstanceName ?? "null");
                m_PollingThread = null; // Exiting thread, mark us as no longer polling.
            }
Ejemplo n.º 11
0
        public void TestEventAttributeReflection()
        {
            SampledMetric.Register(typeof(UserSampledObject));

            UserSampledObject sampledObject = new UserSampledObject(25, 100);

            SampledMetricDefinition.Write(sampledObject);

            SampledMetricDefinition sampledMetricDefinition;

            Assert.IsTrue(SampledMetricDefinition.TryGetValue(typeof(UserSampledObject), "IncrementalCount", out sampledMetricDefinition));

            sampledObject.SetValue(35, 90);
            sampledMetricDefinition.WriteSample(sampledObject);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Retrieve an item from the collection by its key if present.  If not present, the default value of the object is returned.
        /// </summary>
        /// <param name="key">The key of the value to get.</param>
        /// <param name="value">When this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.</param>
        /// <returns>true if the collection contains an element with the specified key; otherwise false.</returns>
        public bool TryGetValue(Guid key, out SampledMetric value)
        {
            lock (m_Lock)
            {
                //We are playing a few games to get native typing here.  Because it's an OUt value, we
                //have to swap types around ourselves so we can cast.
                Monitor.CustomSampledMetric innerValue;

                //gateway to our inner dictionary try get value
                bool result = m_WrappedCollection.TryGetValue(key, out innerValue);

                value = result ? Externalize(innerValue) : null;

                return(result);
            }
        }
Ejemplo n.º 13
0
        internal SampledMetric Externalize(Monitor.CustomSampledMetric eventMetric)
        {
            if (eventMetric == null)
            {
                return(null);
            }

            lock (m_Lock)
            {
                SampledMetric externalDefinition;
                if (m_Externalized.TryGetValue(eventMetric, out externalDefinition) == false)
                {
                    externalDefinition          = new SampledMetric(m_MetricDefinition, eventMetric);
                    m_Externalized[eventMetric] = externalDefinition;
                }

                return(externalDefinition);
            }
        }
Ejemplo n.º 14
0
        private void HandleConnection(string name, ConnectionEndEventData eventData)
        {
            ConnectionMetric metric;

            if (name == RelationalEventId.ConnectionOpened.Name)
            {
                metric = new ConnectionMetric(eventData)
                {
                    Action          = "Open",
                    ConnectionDelta = 1,
                    Duration        = eventData.Duration
                };

                _connectionNames.TryAdd(eventData.ConnectionId, metric.InstanceName);
            }
            else if (name == RelationalEventId.ConnectionClosed.Name)
            {
                // *sigh*.  We've found the server names don't always match between open and close
                //so look up our cached value..
                if (_connectionNames.TryRemove(eventData.ConnectionId, out string instanceName))
                {
                    metric = new ConnectionMetric(eventData, instanceName)
                    {
                        Action          = "Closed",
                        ConnectionDelta = -1,
                        Duration        = eventData.Duration
                    };
                }
                else
                {
                    // we ignore the else clause because it's not a "matching" event.
                    return;
                }
            }
            else
            {
                return;
            }

            EventMetric.Write(metric);
            SampledMetric.Write(metric);
        }
Ejemplo n.º 15
0
        private void RecordStatusMetric(string ipAddress, bool isAccessible, bool accessibleChanged, long latency)
        {
            //first our sampled metric for latency...
            var metric = SampledMetric.Register("Loupe", MetricCategory, "latency", SamplingType.RawCount, "ms",
                                                "Latency", "The latency of the connection to this endpoint (if available)", ipAddress);

            if (isAccessible)
            {
                metric.WriteSample(latency);
            }
            else
            {
                //write a zero latency sample, but credit it to the timestamp where we initiated not now.
                metric.WriteSample(0, DateTimeOffset.Now.AddMilliseconds(-1 * latency));
            }

            if (accessibleChanged)
            {
                EventMetric.Write(new ConnectivityEventMetric(ipAddress, isAccessible));
            }
        }
Ejemplo n.º 16
0
        public void SimpleMetricUsage()
        {
            Log.TraceVerbose("Registering new sampled metric definition");
            //create one sampled metric definition using the "make a definition for the current log set" override
            SampledMetricDefinition temperatureTracking = SampledMetricDefinition.Register("SimpleMetricUsage", "Methods.Temperature", "Experiment Temperature", SamplingType.RawCount, null, "Temperature", "This is an example from iControl where we want to track the temperature of a reaction or some such thing.");

            //create a set of METRICS (definition + metric) using the static add metric capability
            Log.TraceVerbose("Registering metric instances directly");

            SampledMetric incrementalCountMetric = SampledMetric.Register("SimpleMetricUsage", "Methods.Unit Test Data.Direct",
                                                                          "IncrementalCount", SamplingType.IncrementalCount, null, "Incremental Count",
                                                                          "Unit test sampled metric using the incremental count calculation routine.", null);

            SampledMetric incrementalFractionMetric = SampledMetric.Register("SimpleMetricUsage", "Methods.Unit Test Data.Direct",
                                                                             "IncrementalFraction", SamplingType.IncrementalFraction, null, "Incremental Fraction",
                                                                             "Unit test sampled metric using the incremental fraction calculation routine.  Rare, but fun.", null);

            SampledMetric totalCountMetric = SampledMetric.Register("SimpleMetricUsage", "Methods.Unit Test Data.Direct",
                                                                    "TotalCount", SamplingType.TotalCount, null, "Total Count",
                                                                    "Unit test sampled metric using the Total Count calculation routine.  Very common.", null);

            SampledMetric totalFractionMetric = SampledMetric.Register("SimpleMetricUsage", "Methods.Unit Test Data.Direct",
                                                                       "TotalFraction", SamplingType.TotalFraction, null, "Total Fraction",
                                                                       "Unit test sampled metric using the Total Fraction calculation routine.  Rare, but rounds us out.", null);

            SampledMetric rawCountMetric = SampledMetric.Register("SimpleMetricUsage", "Methods.Unit Test Data.Direct",
                                                                  "RawCount", SamplingType.RawCount, null, "Raw Count",
                                                                  "Unit test sampled metric using the Raw Count calculation routine, which we will then average to create sample intervals.", null);

            SampledMetric rawFractionMetric = SampledMetric.Register("SimpleMetricUsage", "Methods.Unit Test Data.Direct",
                                                                     "RawFraction", SamplingType.RawFraction, null, "Raw Fraction",
                                                                     "Unit test sampled metric using the Raw Fraction calculation routine.  Fraction types aren't common.", null);

            // These should never be null, but let's check to confirm.
            Assert.IsNotNull(incrementalCountMetric);
            Assert.IsNotNull(incrementalFractionMetric);
            Assert.IsNotNull(totalCountMetric);
            Assert.IsNotNull(totalFractionMetric);
            Assert.IsNotNull(rawCountMetric);
            Assert.IsNotNull(rawFractionMetric);

            Log.TraceVerbose("And now lets log data");

            //lets add 10 values, a few milliseconds apart
            int curSamplePass = 0;

            while (curSamplePass < 10)
            {
                //the temperature tracking one is set up so we can write to instances directly from a definition.
                temperatureTracking.WriteSample(string.Format(CultureInfo.CurrentCulture, "Experiment {0}", 1), curSamplePass);
                temperatureTracking.WriteSample(string.Format(CultureInfo.CurrentCulture, "Experiment {0}", 2), curSamplePass);
                temperatureTracking.WriteSample(string.Format(CultureInfo.CurrentCulture, "Experiment {0}", 3), curSamplePass);
                temperatureTracking.WriteSample(string.Format(CultureInfo.CurrentCulture, "Experiment {0}", 4), curSamplePass);
                temperatureTracking.WriteSample(string.Format(CultureInfo.CurrentCulture, "Experiment {0}", 5), curSamplePass);
                temperatureTracking.WriteSample(string.Format(CultureInfo.CurrentCulture, "Experiment {0}", 6), curSamplePass);

                incrementalCountMetric.WriteSample(curSamplePass * 20);
                incrementalFractionMetric.WriteSample(curSamplePass * 20, curSamplePass * 30);
                totalCountMetric.WriteSample(curSamplePass * 20);
                totalFractionMetric.WriteSample(curSamplePass * 20, curSamplePass * 30);
                rawCountMetric.WriteSample(curSamplePass);
                rawFractionMetric.WriteSample(curSamplePass, 10.0);

                curSamplePass++;
                Thread.Sleep(100);
            }
            Log.TraceVerbose("Completed logging metric samples.");
        }
 /// <summary>
 /// Create an incomplete sampled metric with just the metric packet
 /// </summary>
 /// <remarks>Before the sampled metric packet is valid, a raw value, counter time stamp, and counter type will need to be supplied.</remarks>
 /// <param name="metric">The metric this sample is for</param>
 protected SampledMetricSamplePacket(SampledMetric metric)
     : base(metric)
 {
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Record a snapshot cache metric using an object
 /// </summary>
 /// <param name="pagesLoaded"></param>
 public static void RecordCacheMetricByObject(int pagesLoaded)
 {
     //by using an object with the appropriate attributes we can do it in one line - even though it writes multiple values.
     SampledMetric.Write(new CacheSampledMetric(pagesLoaded));
 }
Ejemplo n.º 19
0
        public void BasicMetricUsage()
        {
            Log.TraceVerbose("Registering new sampled metric definitions");
            //go ahead and register a few metrics
            //int curMetricDefinitionCount = Log.MetricDefinitions.Count;

            SampledMetricDefinition incrementalCountDefinition = SampledMetricDefinition.Register("BasicMetricUsage",
                                                                                                  "Methods.Unit Test Data.Long", "IncrementalCount", SamplingType.IncrementalCount, null, "Incremental Count",
                                                                                                  "Unit test sampled metric using the incremental count calculation routine.");

            SampledMetricDefinition incrementalFractionDefinition = SampledMetricDefinition.Register("BasicMetricUsage",
                                                                                                     "Methods.Unit Test Data.Long", "IncrementalFraction", SamplingType.IncrementalFraction, null, "Incremental Fraction",
                                                                                                     "Unit test sampled metric using the incremental fraction calculation routine.  Rare, but fun.");

            SampledMetricDefinition totalCountDefinition = SampledMetricDefinition.Register("BasicMetricUsage",
                                                                                            "Methods.Unit Test Data.Long", "TotalCount", SamplingType.TotalCount, null, "Total Count",
                                                                                            "Unit test sampled metric using the Total Count calculation routine.  Very common.");

            SampledMetricDefinition totalFractionDefinition = SampledMetricDefinition.Register("BasicMetricUsage",
                                                                                               "Methods.Unit Test Data.Long", "TotalFraction", SamplingType.TotalFraction, null, "Total Fraction",
                                                                                               "Unit test sampled metric using the Total Fraction calculation routine.  Rare, but rounds us out.");

            SampledMetricDefinition rawCountDefinition = SampledMetricDefinition.Register("BasicMetricUsage",
                                                                                          "Methods.Unit Test Data.Long", "RawCount", SamplingType.RawCount, null, "Raw Count",
                                                                                          "Unit test sampled metric using the Raw Count calculation routine, which we will then average to create sample intervals.");

            SampledMetricDefinition rawFractionDefinition = SampledMetricDefinition.Register("BasicMetricUsage",
                                                                                             "Methods.Unit Test Data.Long", "RawFraction", SamplingType.RawFraction, null, "Raw Fraction",
                                                                                             "Unit test sampled metric using the Raw Fraction calculation routine.  Fraction types aren't common.");

            //we should have added six new metric definitions
            //Assert.AreEqual(curMetricDefinitionCount + 6, Log.MetricDefinitions.Count, "The number of registered metric definitions hasn't increased by the right amount, tending to mean that one or more metrics didn't register.");

            // These should never be null, but let's check to confirm.
            Assert.IsNotNull(incrementalCountDefinition);
            Assert.IsNotNull(incrementalFractionDefinition);
            Assert.IsNotNull(totalCountDefinition);
            Assert.IsNotNull(totalFractionDefinition);
            Assert.IsNotNull(rawCountDefinition);
            Assert.IsNotNull(rawFractionDefinition);

            //and lets go ahead and create new metrics for each definition
            Log.TraceVerbose("Obtaining default metric instances from each definition");

            SampledMetric incrementalCountMetric    = SampledMetric.Register(incrementalCountDefinition, null);
            SampledMetric incrementalFractionMetric = SampledMetric.Register(incrementalFractionDefinition, null);
            SampledMetric totalCountMetric          = SampledMetric.Register(totalCountDefinition, null);
            SampledMetric totalFractionMetric       = SampledMetric.Register(totalFractionDefinition, null);
            SampledMetric rawCountMetric            = SampledMetric.Register(rawCountDefinition, null);
            SampledMetric rawFractionMetric         = SampledMetric.Register(rawFractionDefinition, null);

            // These should never be null, either, but let's check to confirm.
            Assert.IsNotNull(incrementalCountMetric);
            Assert.IsNotNull(incrementalFractionMetric);
            Assert.IsNotNull(totalCountMetric);
            Assert.IsNotNull(totalFractionMetric);
            Assert.IsNotNull(rawCountMetric);
            Assert.IsNotNull(rawFractionMetric);

            //and finally, lets go log some data!
            Log.TraceVerbose("And now lets log data");

            //lets add 10 values, a few milliseconds apart
            int curSamplePass = 0;

            while (curSamplePass < 10)
            {
                //We're putting in fairly bogus data, but it will produce a consistent output.
                incrementalCountMetric.WriteSample(curSamplePass * 20);
                incrementalFractionMetric.WriteSample(curSamplePass * 20, curSamplePass * 30);
                totalCountMetric.WriteSample(curSamplePass * 20);
                totalFractionMetric.WriteSample(curSamplePass * 20, curSamplePass * 30);
                rawCountMetric.WriteSample(curSamplePass);
                rawFractionMetric.WriteSample(curSamplePass, 10.0);

                curSamplePass++;
                Thread.Sleep(100);
            }

            Log.TraceVerbose("Completed logging metric samples.");
        }
Ejemplo n.º 20
0
 /// <summary>
 /// Snapshot cache metrics using fewest lines of code
 /// </summary>
 /// <param name="pagesLoaded"></param>
 public static void RecordCacheMetricShortestCode(int pagesLoaded)
 {
     //Alternately, it can be done in a single line of code each, although somewhat less readable.  Note the WriteSample call after the Register call.
     SampledMetric.Register("GibraltarSample", "Database.Engine", "cachePages", SamplingType.RawCount, "Pages", "Cache Pages", "The number of pages in the cache", null).WriteSample(pagesLoaded);
     SampledMetric.Register("GibraltarSample", "Database.Engine", "cacheSize", SamplingType.RawCount, "Bytes", "Cache Size", "The number of bytes used by pages in the cache", null).WriteSample(pagesLoaded * 8196);
 }
Ejemplo n.º 21
0
        public void SampledMetricsByMethodsPerformanceTest()
        {
            Log.TraceVerbose("Registering new sampled metric definitions");
            //go ahead and register a few metrics
            //int curMetricDefinitionCount = Log.MetricDefinitions.Count;

            SampledMetricDefinition incrementalCountDefinition =
                SampledMetricDefinition.Register("PerformanceTestsMetrics", "Performance.SampledMetrics.Methods", "IncrementalCount",
                                                 SamplingType.IncrementalCount, null, "Incremental Count",
                                                 "Unit test sampled metric using the incremental count calculation routine.");

            SampledMetricDefinition incrementalFractionDefinition =
                SampledMetricDefinition.Register("PerformanceTestsMetrics", "Performance.SampledMetrics.Methods", "IncrementalFraction",
                                                 SamplingType.IncrementalFraction, null, "Incremental Fraction",
                                                 "Unit test sampled metric using the incremental fraction calculation routine.  Rare, but fun.");

            SampledMetricDefinition totalCountDefinition =
                SampledMetricDefinition.Register("PerformanceTestsMetrics", "Performance.SampledMetrics.Methods", "TotalCount",
                                                 SamplingType.TotalCount, null, "Total Count",
                                                 "Unit test sampled metric using the Total Count calculation routine.  Very common.");

            SampledMetricDefinition totalFractionDefinition =
                SampledMetricDefinition.Register("PerformanceTestsMetrics", "Performance.SampledMetrics.Methods", "TotalFraction",
                                                 SamplingType.TotalFraction, null, "Total Fraction",
                                                 "Unit test sampled metric using the Total Fraction calculation routine.  Rare, but rounds us out.");

            SampledMetricDefinition rawCountDefinition =
                SampledMetricDefinition.Register("PerformanceTestsMetrics", "Performance.SampledMetrics.Methods", "RawCount",
                                                 SamplingType.RawCount, null, "Raw Count",
                                                 "Unit test sampled metric using the Raw Count calculation routine, which we will then average to create sample intervals.");

            SampledMetricDefinition rawFractionDefinition =
                SampledMetricDefinition.Register("PerformanceTestsMetrics", "Performance.SampledMetrics.Methods", "RawFraction",
                                                 SamplingType.RawFraction, null, "Raw Fraction",
                                                 "Unit test sampled metric using the Raw Fraction calculation routine.  Fraction types aren't common.");

            //we should have added six new metric definitions
            //Assert.AreEqual(curMetricDefinitionCount + 6, Log.MetricDefinitions.Count, "The number of registered metric definitions hasn't increased by the right amount, tending to mean that one or more metrics didn't register.");

            // These should never be null, but let's check to confirm.
            Assert.IsNotNull(incrementalCountDefinition);
            Assert.IsNotNull(incrementalFractionDefinition);
            Assert.IsNotNull(totalCountDefinition);
            Assert.IsNotNull(totalFractionDefinition);
            Assert.IsNotNull(rawCountDefinition);
            Assert.IsNotNull(rawFractionDefinition);

            Trace.TraceInformation("Sampled metric definitions registered by methods.");

            // These should never be null, but let's check to confirm.
            Assert.IsNotNull(incrementalCountDefinition);
            Assert.IsNotNull(incrementalFractionDefinition);
            Assert.IsNotNull(totalCountDefinition);
            Assert.IsNotNull(totalFractionDefinition);
            Assert.IsNotNull(rawCountDefinition);
            Assert.IsNotNull(rawFractionDefinition);

            //and lets go ahead and create new metrics for each definition
            Log.TraceVerbose("Obtaining default metric instances from each definition");

            SampledMetric incrementalCountMetric    = SampledMetric.Register(incrementalCountDefinition, null);
            SampledMetric incrementalFractionMetric = SampledMetric.Register(incrementalFractionDefinition, null);
            SampledMetric totalCountMetric          = SampledMetric.Register(totalCountDefinition, null);
            SampledMetric totalFractionMetric       = SampledMetric.Register(totalFractionDefinition, null);
            SampledMetric rawCountMetric            = SampledMetric.Register(rawCountDefinition, null);
            SampledMetric rawFractionMetric         = SampledMetric.Register(rawFractionDefinition, null);

            // These should never be null, either, but let's check to confirm.
            Assert.IsNotNull(incrementalCountMetric);
            Assert.IsNotNull(incrementalFractionMetric);
            Assert.IsNotNull(totalCountMetric);
            Assert.IsNotNull(totalFractionMetric);
            Assert.IsNotNull(rawCountMetric);
            Assert.IsNotNull(rawFractionMetric);

            // Now, lets get everything to flush so we have our best initial state.
            Log.Information(LogWriteMode.WaitForCommit, "Test.Agent.Metrics.Performance", "Preparing for Test", "Flushing queue");

            //now that we know it's flushed everything, lets do our timed loop.
            DateTimeOffset startTime = DateTimeOffset.UtcNow;

            for (int curMessage = 0; curMessage < LoopsPerSampledTest; curMessage++)
            {
                //We're putting in fairly bogus data, but it will produce a consistent output.
                incrementalCountMetric.WriteSample(20);
                incrementalFractionMetric.WriteSample(20, 30);
                totalCountMetric.WriteSample(20);
                totalFractionMetric.WriteSample(20, 30);
                rawCountMetric.WriteSample(20);
                rawFractionMetric.WriteSample(20, 30);
            }
            DateTimeOffset messageEndTime = DateTimeOffset.UtcNow;

            //one wait for commit message to force the buffer to flush.
            Log.Information(LogWriteMode.WaitForCommit, "Test.Agent.Metrics.Performance", "Waiting for Samples to Commit", null);

            //and store off our time
            DateTimeOffset endTime = DateTimeOffset.UtcNow;

            TimeSpan  testDuration    = endTime - startTime;
            TimeSpan  loopDuration    = messageEndTime - startTime;
            const int messagesPerTest = LoopsPerSampledTest * MessagesPerSampledLoop;

            Trace.TraceInformation("Sampled Metrics by Methods Test committed {0:N0} samples in {1:F3} ms (average {2:F4} ms per message).  Average loop time {3:F4} ms ({4} samples per loop) and final flush time {5:F3} ms.",
                                   messagesPerTest, testDuration.TotalMilliseconds, (testDuration.TotalMilliseconds / messagesPerTest),
                                   (loopDuration.TotalMilliseconds / LoopsPerSampledTest), MessagesPerSampledLoop,
                                   (endTime - messageEndTime).TotalMilliseconds);
        }
Ejemplo n.º 22
0
        public void RegistrationConsistency()
        {
            Assert.IsTrue(true);
            bool question = SampledMetricDefinition.IsValidDataType(typeof(int));

            Assert.IsTrue(question);
            question = SampledMetricDefinition.IsValidDataType(GetType());
            Assert.IsFalse(question);

            // Create a sampled metric definition to work with.
            SampledMetricDefinition createdDefinitionOne = SampledMetricDefinition.Register("RegistrationConsistency",
                                                                                            "Methods.Unit Test Data.Consistency", "Metric One", SamplingType.RawCount, null, "First metric",
                                                                                            "The first sampled metric definition for this test.");

            Assert.IsNotNull(createdDefinitionOne);

            // Get the same definition with different caption and description.
            SampledMetricDefinition obtainedDefinitionOne = SampledMetricDefinition.Register("RegistrationConsistency",
                                                                                             "Methods.Unit Test Data.Consistency", "Metric One", SamplingType.RawCount, null, "The first metric",
                                                                                             "This is the first sampled metric definition for this test.");

            Assert.IsNotNull(obtainedDefinitionOne);

            Assert.AreSame(createdDefinitionOne, obtainedDefinitionOne,
                           "Second call to SampledMetricDefinition.Register() did not get the same definition object as the first. \"{0}\" vs \"{1}\".",
                           createdDefinitionOne.Caption, obtainedDefinitionOne.Caption);
            Assert.AreEqual("First metric", createdDefinitionOne.Caption);
            Assert.AreEqual("The first sampled metric definition for this test.", createdDefinitionOne.Description);

            // Get an instance from the definition.
            SampledMetric createdInstanceOneA = SampledMetric.Register(createdDefinitionOne, "Instance One A");

            Assert.IsNotNull(createdInstanceOneA);

            Assert.AreSame(createdDefinitionOne, createdInstanceOneA.Definition,
                           "Created instance does not point to the same definition object used to create it. \"{0}\" vs \"{1}\".",
                           createdDefinitionOne.Caption, createdInstanceOneA.Definition.Caption);
            Assert.AreEqual("Instance One A", createdInstanceOneA.InstanceName);

            // Get the same instance the same way again.
            SampledMetric obtainedInstanceOneA = SampledMetric.Register(createdDefinitionOne, "Instance One A");

            Assert.IsNotNull(obtainedInstanceOneA);

            Assert.AreSame(createdInstanceOneA, obtainedInstanceOneA,
                           "Second call to SampledMetric.Register() did not get the same metric instance object as the first. \"{0}\" vs \"{1}\".",
                           createdInstanceOneA.InstanceName, obtainedInstanceOneA.InstanceName);

            // Get the same instance directly.
            obtainedInstanceOneA = SampledMetric.Register("RegistrationConsistency",
                                                          "Methods.Unit Test Data.Consistency", "Metric One", SamplingType.RawCount, null, "Metric #1",
                                                          "This is metric definition #1 for this test.", "Instance One A");
            Assert.IsNotNull(obtainedInstanceOneA);
            Assert.IsNotNull(obtainedInstanceOneA.Definition);

            Assert.AreSame(createdInstanceOneA, obtainedInstanceOneA,
                           "Third call to SampledMetric.Register() did not get the same metric instance object as the first. \"{0}\" vs \"{1}\".",
                           createdInstanceOneA.InstanceName, obtainedInstanceOneA.InstanceName);

            // Get a second instance from the definition.
            SampledMetric createdInstanceOneB = SampledMetric.Register(createdDefinitionOne, "Instance One B");

            Assert.IsNotNull(createdInstanceOneB);
            Assert.IsNotNull(createdInstanceOneB.Definition);

            Assert.AreSame(createdDefinitionOne, createdInstanceOneB.Definition,
                           "Created instance does not point to the same definition object used to create it. \"{0}\" vs \"{1}\".",
                           createdDefinitionOne.Caption, createdInstanceOneB.Definition.Caption);
            Assert.AreEqual("Instance One B", createdInstanceOneB.InstanceName);

            Assert.AreNotSame(createdInstanceOneA, createdInstanceOneB,
                              "Different metric instances should never be the same object.");
            Assert.AreNotEqual(createdInstanceOneA, createdInstanceOneB,
                               "Different metric instances should not test as being equal. \"{0}\" vs \"{1}\".",
                               createdInstanceOneA.InstanceName, createdInstanceOneB.InstanceName);

            Assert.IsTrue(createdInstanceOneA.Key.StartsWith(createdDefinitionOne.Key),
                          "Instance Key does not start with definition Key. \"{0}\" vs \"{1}\".",
                          createdInstanceOneA.Key, createdDefinitionOne.Key);
            Assert.IsTrue(createdInstanceOneA.Key.EndsWith(createdInstanceOneA.InstanceName),
                          "Instance Key does not end with instance name. \"{0}\" vs \"{1}\".",
                          createdInstanceOneA.Key, createdInstanceOneA.InstanceName);
            Assert.IsTrue(createdInstanceOneB.Key.StartsWith(createdDefinitionOne.Key),
                          "Instance Key does not start with definition Key. \"{0}\" vs \"{1}\".",
                          createdInstanceOneB.Key, createdDefinitionOne.Key);
            Assert.IsTrue(createdInstanceOneB.Key.EndsWith(createdInstanceOneB.InstanceName),
                          "Instance Key does not end with instance name. \"{0}\" vs \"{1}\".",
                          createdInstanceOneB.Key, createdInstanceOneB.InstanceName);



            // Try a different definition, directly to an instance first.
            SampledMetric createdInstanceTwoA = SampledMetric.Register("RegistrationConsistency",
                                                                       "Methods.Unit Test Data.Consistency", "Metric Two", SamplingType.RawCount, null, "Second metric",
                                                                       "The second sampled metric definition for this test.", "Instance Two A");

            Assert.IsNotNull(createdInstanceTwoA);

            // Check it's definition, created automatically.
            SampledMetricDefinition createdDefinitionTwo = createdInstanceTwoA.Definition;

            Assert.IsNotNull(createdDefinitionTwo);

            // Get the same instance, with a different caption and description.
            SampledMetric obtainedInstanceTwoA = SampledMetric.Register("RegistrationConsistency",
                                                                        "Methods.Unit Test Data.Consistency", "Metric Two", SamplingType.RawCount, null, "The second metric",
                                                                        "This is the second sampled metric definition for this test.", "Instance Two A");

            Assert.IsNotNull(obtainedInstanceTwoA);
            Assert.AreSame(createdDefinitionTwo, obtainedInstanceTwoA.Definition,
                           "Instances of the same metric do not point to the same definition object. \"{0}\" vs \"{1}\".",
                           createdDefinitionTwo.Caption, obtainedInstanceTwoA.Definition.Caption);

            // Get the same definition another way.
            SampledMetricDefinition obtainedDefinitionTwo = SampledMetricDefinition.Register("RegistrationConsistency",
                                                                                             "Methods.Unit Test Data.Consistency", "Metric Two", SamplingType.RawCount, null, "Metric #2",
                                                                                             "This is metric definition #2 for this test.");

            Assert.IsNotNull(obtainedDefinitionTwo);

            Assert.AreSame(createdDefinitionTwo, obtainedDefinitionTwo,
                           "Call to SampledMetricDefinition.Register() did not get the same definition as the direct instances. \"{0}\" vs \"{1}\".",
                           createdDefinitionTwo.Caption, obtainedDefinitionTwo.Caption);
            Assert.AreEqual("Second metric", obtainedDefinitionTwo.Caption);
            Assert.AreEqual("The second sampled metric definition for this test.", obtainedDefinitionTwo.Description);

            Assert.IsTrue(createdInstanceTwoA.Key.StartsWith(createdDefinitionTwo.Key),
                          "Instance Key does not start with definition Key. \"{0}\" vs \"{1}\".",
                          createdInstanceTwoA.Key, createdDefinitionTwo.Key);
            Assert.IsTrue(createdInstanceTwoA.Key.EndsWith(createdInstanceTwoA.InstanceName),
                          "Instance Key does not end with instance name. \"{0}\" vs \"{1}\".",
                          createdInstanceTwoA.Key, createdInstanceTwoA.InstanceName);

            Assert.AreNotSame(createdDefinitionOne, createdDefinitionTwo,
                              "Different metric definitions should never be the same object.");
            Assert.AreNotEqual(createdDefinitionOne, createdDefinitionTwo,
                               "Different metric definitions should not test as being equal. \"{0}\" vs \"{1}\".",
                               createdDefinitionOne.Key, createdDefinitionTwo.Key);



            // Create instance from null, then from empty string.
            SampledMetric instanceOneNull  = SampledMetric.Register(createdDefinitionOne, null);
            SampledMetric instanceOneEmpty = SampledMetric.Register(createdDefinitionOne, string.Empty);

            Assert.IsNotNull(instanceOneNull);
            Assert.IsNotNull(instanceOneEmpty);
            Assert.AreSame(instanceOneNull, instanceOneEmpty,
                           "Null instance and empty instance are not the same metric object. {0} vs {1}.",
                           (instanceOneNull.InstanceName == null) ? "(null)" : "\"" + instanceOneNull.InstanceName + "\"",
                           (instanceOneEmpty.InstanceName == null) ? "(null)" : "\"" + instanceOneEmpty.InstanceName + "\"");

            // Create instance from empty string, then from null.
            SampledMetric instanceTwoEmpty = SampledMetric.Register(createdDefinitionTwo, string.Empty);
            SampledMetric instanceTwoNull  = SampledMetric.Register(createdDefinitionTwo, null);

            Assert.IsNotNull(instanceTwoEmpty);
            Assert.IsNotNull(instanceTwoNull);
            Assert.AreSame(instanceTwoEmpty, instanceTwoNull,
                           "Empty instance and null instance are not the same metric object. {0} vs {1}.",
                           (instanceTwoEmpty.InstanceName == null) ? "(null)" : "\"" + instanceTwoEmpty.InstanceName + "\"",
                           (instanceTwoNull.InstanceName == null) ? "(null)" : "\"" + instanceTwoNull.InstanceName + "\"");

            Assert.IsTrue(instanceOneNull.IsDefault);
            Assert.IsTrue(instanceOneEmpty.IsDefault);
            Assert.IsTrue(instanceTwoEmpty.IsDefault);
            Assert.IsTrue(instanceTwoNull.IsDefault);

            Assert.IsNull(instanceOneNull.InstanceName);
            Assert.IsNull(instanceOneEmpty.InstanceName);
            Assert.IsNull(instanceTwoEmpty.InstanceName);
            Assert.IsNull(instanceTwoNull.InstanceName);



            SampledMetricDefinition createdDefinitionThree = SampledMetricDefinition.Register("RegistrationConsistency",
                                                                                              "Methods.Unit Test Data.Consistency", "Metric Three", SamplingType.IncrementalCount, null, "Third metric",
                                                                                              "An IncrementalCount metric.");

            Assert.IsNotNull(createdDefinitionThree);
            SampledMetricDefinition obtainedDefinitionThree;

            try
            {
                obtainedDefinitionThree = SampledMetricDefinition.Register("RegistrationConsistency",
                                                                           "Methods.Unit Test Data.Consistency", "Metric Three", SamplingType.RawCount, null, "Third metric",
                                                                           "A RawCount metric.");
                Assert.IsNotNull(obtainedDefinitionThree); // This should never actually be executed.
            }
            catch (ArgumentException ex)
            {
                Trace.TraceInformation("SampledMetricDefinition.Register() with inconsistent sampling type threw expected exception.", ex);
                obtainedDefinitionThree = null;
            }
            Assert.IsNull(obtainedDefinitionThree); // Confirm we went through the catch.

            SampledMetric createdInstanceThreeA;

            try
            {
                createdInstanceThreeA = SampledMetric.Register("RegistrationConsistency",
                                                               "Methods.Unit Test Data.Consistency", "Metric Three", SamplingType.TotalCount, null, "Third metric",
                                                               "A TotalCount metric.", "Instance Three A");
                Assert.IsNotNull(createdInstanceThreeA); // This should never actually be executed.
            }
            catch (ArgumentException ex)
            {
                Trace.TraceInformation("SampledMetric.Register() with inconsistent sampling type threw expected exception.", ex);
                createdInstanceThreeA = null;
            }
            Assert.IsNull(createdInstanceThreeA); // Confirm we went through the catch.

            obtainedDefinitionThree = SampledMetricDefinition.Register("RegistrationConsistency",
                                                                       "Methods.Unit Test Data.Consistency", "Metric Three", SamplingType.IncrementalCount, "seconds", "Third metric",
                                                                       "An IncrementalCount of seconds metric.");
            Assert.IsNotNull(obtainedDefinitionThree);
            //Assert.IsNull(obtainedDefinitionThree.UnitCaption); // Bug: This is getting "Count of items" instead of null?

            SampledMetric createdInstanceFour = SampledMetric.Register("RegistrationConsistency",
                                                                       "Methods.Unit Test Data.Consistency", "Metric Four", SamplingType.IncrementalFraction, "hits per minute",
                                                                       "Third metric", "An IncrementalCount of seconds metric.", null);

            Assert.IsNotNull(createdInstanceFour);
            Assert.AreEqual("hits per minute", createdInstanceFour.UnitCaption);

            SampledMetricDefinition obtainedDefinitionFour = SampledMetricDefinition.Register("RegistrationConsistency",
                                                                                              "Methods.Unit Test Data.Consistency", "Metric Four", SamplingType.IncrementalFraction, "hits per hour",
                                                                                              "Third metric", "An IncrementalCount of seconds metric.");

            Assert.IsNotNull(obtainedDefinitionFour);
            Assert.AreEqual("hits per minute", obtainedDefinitionFour.UnitCaption);
        }