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); }
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> /// 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); }
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. }
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); }
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)); } }
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); }
/// <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); }
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."); }
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."); }
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); }