VS 2010使用自定义计数器加载测试结果

我是Visual Studio 2010的负载测试(通常是测试)的新手,我正在处理几个问题。

我的问题是,有没有可能在负载测试结果上添加自定义测试变量?

我有以下UnitTest:

[TestMethod] public void Test() { Stopwatch testTimer = new Stopwatch(); testTimer.Start(); httpClient.SendRequest(); testTimer.Stop(); double requestDelay = testTimer.Elapsed.TotalSeconds; } 

许多LoadTests使用此UnitTest,我想将requestDelay变量添加到负载测试结果,因此我可以像所有其他负载测试计数器一样获得Min,Max和Avg值(例如测试响应时间)。

那可能吗?

使用@Pritam Karmakar评论中的链接和post末尾的演练,我终于找到了解决方案。

首先,我创建了一个Load Test Plug-In并使用LoadTestStarting事件创建我的自定义计数器类别并将其添加到我的所有计数器:

 void m_loadTest_LoadTestStarting(object sender, System.EventArgs e) { // Delete the category if already exists if (PerformanceCounterCategory.Exists("CustomCounterSet")) { PerformanceCounterCategory.Delete("CustomCounterSet"); } //Create the Counters collection and add my custom counters CounterCreationDataCollection counters = new CounterCreationDataCollection(); counters.Add(new CounterCreationData(Counters.RequestDelayTime.ToString(), "Keeps the actual request delay time", PerformanceCounterType.AverageCount64)); // .... Add the rest counters // Create the custom counter category PerformanceCounterCategory.Create("CustomCounterSet", "Custom Performance Counters", PerformanceCounterCategoryType.MultiInstance, counters); } 

然后,在LoadTest编辑器中,我右键单击Agent CounterSet并选择Add Counters …Pick Performance Counters窗口中,我选择了我的性能类别并将我的计数器添加到CounterSet,以便Load Test将收集他们的数据:

在此处输入图像描述

最后,每个UnitTest在ClassInitialize方法中创建计数器的实例,然后在适当的步骤更新计数器:

 [TestClass] public class UnitTest1 { PerformanceCounter RequestDelayTime; [ClassInitialize] public static void ClassInitialize(TestContext TestContext) { // Create the instances of the counters for the current test RequestDelaytime = new PerformanceCounter("CustomCounterSet", "RequestDelayTime", "UnitTest1", false)); // .... Add the rest counters instances } [TestCleanup] public void CleanUp() { RequestDelayTime.RawValue = 0; RequestDelayTime.EndInit(); RequestDelayTime.RemoveInstance(); RequestDelayTime.Dispose(); } [TestMethod] public void TestMethod1() { // ... Testing // update counters RequestDelayTime.Incerement(time); // ... Continue Testing } } 

链接:

  • 以编程方式创建性能计数器
  • 设置性能计数器
  • 在负载测试结果中包含unit testing变量值

我认为你真正需要的是使用:

 [TestMethod] public void Test() { TestContext.BeginTimer("mytimer"); httpClient.SendRequest(); TestContext.EndTimer("mytimer"); } 

你可以在这里找到好的文档。

有趣的问题。 从来没有试过这个,但我有个主意。

创建MAX,MIN和AVG的3个类级属性。 在每次测试期间操纵这些值。 然后在通过Classcleanup或assemblycleanup 测试属性执行整个负载测试后写入所有最终值。 您必须运行负载测试1-2分钟,并且必须查看最后调用哪个属性方法。 然后,您可以通过文本编写器在本地驱动器中的平面文件中打印这些最终值。