【问题标题】:VS 2010 Load Tests Results with custom counters带有自定义计数器的 VS 2010 负载测试结果
【发布时间】:2012-02-17 21:54:53
【问题描述】:

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

我的问题是,有没有办法在负载测试结果中添加自定义测试变量?

我有以下单元测试:

[TestMethod]
public void Test()
{
    Stopwatch testTimer = new Stopwatch();
    testTimer.Start();
    httpClient.SendRequest();
    testTimer.Stop();

    double requestDelay = testTimer.Elapsed.TotalSeconds;
}

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

这可能吗?

【问题讨论】:

    标签: c# visual-studio-2010 unit-testing load-testing performancecounter


    【解决方案1】:

    有趣的问题。从来没有试过这个,但我有一个想法。

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

    【讨论】:

    【解决方案2】:

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

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

    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,以便负载测试收集它们的数据:

    最后,每个 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
        }
    }
    

    链接:

    【讨论】:

      【解决方案3】:

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

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

      你可以找到很好的文档here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 2018-05-18
        • 2017-03-23
        • 2012-12-04
        • 1970-01-01
        • 2021-06-09
        • 1970-01-01
        相关资源
        最近更新 更多