我可以建立在Visual Studio中的UnitTest / LoadTest一个自定义的TestContext计时器? [英] Can I create a custom TestContext timer for UnitTest/LoadTest in Visual Studio?

查看:686
本文介绍了我可以建立在Visual Studio中的UnitTest / LoadTest一个自定义的TestContext计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一些单元测试代码中有一个在一个循环中定义的睡眠。我要分析不仅试验的每次迭代,但对于所有迭代的总时间,以显示任何非线性缩放。例如,如果我分析的总体,它包括睡眠时间。我可以使用秒表启动/停止,使其只包含doAction()。不过,我不会写的秒表结果给的TestContext结果。

A few of my UnitTests have a Sleep that is defined in a loop. I want to profile not only each iteration of the test, but the overall time for all iterations, in order to show any non linear scaling. For example, if I profile the "Overall", it includes the time for the sleep. I can use Stopwatch Start/Stop so that it only includes the doAction(). However, I can't write the Stopwatch results to the TestContext results.

[TestMethod]
    public void TestMethod1()
    {
        TestContext.BeginTimer("Overall");
        for (int i = 0; i < 5; i++)
        {
            TestContext.BeginTimer("Per");
            doAction();
            TestContext.EndTimer("Per");
            Sleep(1000);
        }
        TestContext.EndTimer("Overall");
    }



似乎可以的TestContext从被继承和重新定义。不过,我不明白怎么写这回事务存储任何的例子。

It seems that TestContext can be inherited from and redefined. However, I do not see any examples for how to write this back to the transaction store.

对此有一个实现可以参考我,或其他的想法。我想看看它在同一份报告说Visual Studio中呈现的LoadTest。否则,我必须写我自己的报告。

Is there an implementation for this I can refer to, or another idea. I would like to see it in the same report that Visual Studio presents for the LoadTest. Otherwise I have to write my own reporting.

另外,我已经试过嗅探,这些写入LoadTest数据库的SQL,但在搞清楚如何并不成功。应该有一个存储过程调用,但我想这是所有的数据在测试结束。

Also, I have tried sniffing the SQL that writes these to the LoadTest database, but was not successful in figuring out how. There should be a SPROC to call but I am thinking it is all of the data at the end of the test.

推荐答案

好,我有一个类似的问题。我想报告一些额外的数据/报告/从我的测试专柜如Visual Studio最终的测试结果确实,我找到了一个解决方案。

Well, I had a similar problem. I wanted to report some extra data/reports/counters from my tests in the final test result like Visual Studio does and I found a solution.

首先,这不能做用你的方式尝试。还有就是负载测试以及其中存在的TestContext单元测试之间没有直接的联系。

First, this cannot be done with the way you are trying. There is no direct link between the Load Test and the Unit Test where the TestContext exists.

二,你要明白视觉工作室是如何创建的报告。它从性能计数器的收集数据操作系统。您可以编辑这些计数器,删除那些你不想,并添加你希望别人。

Second, you have to understand how visual studio creates the reports. It collects data from the performance counters of the OS. You can edit these counters, remove those you don't want and add others you want.

负载测试配置都有关于计数器两个基本部分。它们是:

The load test configuration has two basic sections regarding the counters. These are:


  • 计数器集。这些都是套柜,例如这是默认添加。如果您打开此计数器设定你会看到,它收集计数器,如内存,处理器,物理磁盘e.t.c.因此,在测试结束时,你可以看到你所有的代理所有这些数据。如果您想更多的柜台到这个柜台设置,你可以双击它(从负载测试编辑器,见下图),然后选择添加计数器。这将打开一个窗口,你的系统的所有柜台,选择那些你想要的。

  • The Counter Sets. These are sets of counters, for example agent which is added by default. If you open this counter set you will see that it collects counters such as Memory, Processor, PhysicalDisk e.t.c. So, at the end of the test you can see all these data from all your agents. If you you want to add more counters to this counter set you can double click on it (from the load test editor, see picture below) and select Add Counters. This will open a window with all the counters of your system and select those you want.

计数器集映射。在这里,您柜台台与你的机器联系起来。默认情况下, [控制器机器] 添加[代理机器] 一些默认的计数器集。这意味着,其分别对应的 [控制器机器]中包含的计数器集所有柜台将会从您的控制器机器收集的。这同样适用于所有的代理商

The Counter Set Mappings. Here you associate the counters sets with your machines. By default the [CONTROLLER MACHINE] and [AGENT MACHINES] are added with some default counter sets. This means that all the counters contained in the counter sets which are mapped to the [CONTROLLER MACHINE] will be gathered from your controller machine. The same applies for all your agents.

您可以添加更多的柜台套及以上的机器。通过右键点击计数器集映射 - > 管理​​计数器集... 一个新的窗口,如下打开:

You can add more counters sets and more machines. By right clicking on the Counter Set Mappings --> Manage Counter Sets... a new window opens as below:

如你所见,我已经添加了一个额外的机器名 DB_1 。这是机器的计算机名称和它必须在与控制器相同的域,以便能够访问它与收集的计数器。我也标记为数据库服务器,并选择了 SQL 计数器设置(默认为SQL计数器,但你可以对它进行编辑并添加任何你想要的计数器)。现在每次执行此负载测试时,控制器会去用计算机名DB_1一台机器,并收集,将在最终测试结果报告的数据。

As you can see, I have added an extra machine with name db_1. This is the computer name of the machine and it must be at the same domain with the controller in order to have access to it and collect counters. I have also tagged it as database server and selected the sql counter set (default for sql counters but you can edit it and add any counter you want). Now every time this load test is executed, the controller will go to a machine with computer name db_1 and collect data which will be reported at the final test results.

好吧,这(大)出台后,它的时间来看看如何将数据添加到最终的测试结果。为了做到这一点,你必须创建自己的定制的性能计数器的。这意味着新的性能计数器类别必须创建机器需要收集这些数据。在你的情况,在所有的代理商,因为这是在单元测试中执行。

Ok, after this (big) introduction it's time to see how to add your data into the final test results. In order to do this you must create your own custom performance counters. This means that a new Performance Counter Category must be created in the machines you need to collect these data. In your case, in all of your agents because this is where the UnitTests are executed.

您已经在代理商创建的柜台后,您可以编辑代理计数器设置如上图所示,选择您额外的自定义计数器。

After you have created the counters in the agents, you can edit the Agents counter set as shown above and select your extra custom counters.

下面是如何做到这一点的示例代码

Here is a sample code on how to do this.

首先创建性能计数器所有的代理商。每个代理的机器上运行此代码只有一次(或者你可以在一个的负载测试插件):

First create the performance counters to all your agents. Run this code only once on every agent machine (or you can add it in a load test plugin):

void CreateCounter() 
{
    if (PerformanceCounterCategory.Exists("MyCounters"))
    {
        PerformanceCounterCategory.Delete("MyCounters");
    }

    //Create the Counters collection and add your custom counters 
    CounterCreationDataCollection counters = new CounterCreationDataCollection();
    // The name of the counter is Delay
    counters.Add(new CounterCreationData("Delay", "Keeps the actual delay", PerformanceCounterType.AverageCount64));
    // .... Add the rest counters

    // Create the custom counter category
    PerformanceCounterCategory.Create("MyCounters", "Custom Performance Counters", PerformanceCounterCategoryType.MultiInstance, counters);
}



在这里,你的测试代码:

And here the code of your test:

[TestClass]
public class UnitTest1
{
    PerformanceCounter OverallDelay;
    PerformanceCounter PerDelay;

    [ClassInitialize]
    public static void ClassInitialize(TestContext TestContext)
    {
        // Create the instances of the counters for the current test
        // Initialize it here so it will created only once for this test class
        OverallDelay= new PerformanceCounter("MyCounters", "Delay", "Overall", false));
        PerDelay= new PerformanceCounter("MyCounters", "Delay", "Per", false));
        // .... Add the rest counters instances
    }

    [ClassCleanup]
    public void CleanUp()
    {
        // Reset the counters and remove the counter instances
        OverallDelay.RawValue = 0;
        OverallDelay.EndInit();
        OverallDelay.RemoveInstance();
        OverallDelay.Dispose();
        PerDelay.RawValue = 0;
        PerDelay.EndInit();
        PerDelay.RemoveInstance();
        PerDelay.Dispose();
    }

    [TestMethod]
    public void TestMethod1()
    {
         // Use stopwatch to keep track of the the delay
         Stopwatch overall = new Stopwatch();
         Stopwatch per = new Stopwatch();

         overall.Start();

         for (int i = 0; i < 5; i++)
         {
             per.Start();
             doAction();
             per.Stop();

             // Update the "Per" instance of the "Delay" counter for each doAction on every test
             PerDelay.Incerement(per.ElapsedMilliseconds);
             Sleep(1000);

             per.Reset();
         }

         overall.Stop();

         // Update the "Overall" instance of the "Delay" counter on every test
         OverallDelay.Incerement(overall.ElapsedMilliseconds);
     }
}

现在,你的测试执行时,他们会报告于计数器的数据。在负载测试结束时,您将可以看到每个代理机柜台,并把它添加到图。它将与MIN,MAX和AVG值进行报告。

Now, when your tests are executed, they will report to the counter their data. At the end of the load test you will be able to see the counter in every agent machine and add it to the graphs. It will be reported with MIN, MAX and AVG values.


  1. 我认为(数月的研究之后),这是只有这样,才能从测试中添加自定义数据到最终负载测试报告。

  2. 它可能看起来很难做到这一点。好吧,如果你懂点这不是很难优化。我有一类包装这个功能更容易进行初始化,更新毕竟管理的计数器。

  3. 这是非常非常有用的。我现在可以看到从我的测试统计,这是不可能用默认计数器。这样的我们,当Web请求发送到Web服务失败,我可以捕获错误,更新相应的计数器(如超时,ServiceUnavailable,RequestRejected ...)。

我希望我帮助。 :)

这篇关于我可以建立在Visual Studio中的UnitTest / LoadTest一个自定义的TestContext计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆