gpt4 book ai didi

c# - 我可以在Visual Studio中为单元测试/负载测试创建自定义TestContext计时器吗?

转载 作者:可可西里 更新时间:2023-11-01 09:09:00 25 4
gpt4 key购买 nike

我的一些UnitTest具有在循环中定义的Sleep。我不仅要描述测试的每个迭代,还要描述所有迭代的总时间,以显示任何非线性缩放比例。例如,如果我分析“总体”,则它包括 sleep 时间。我可以使用Stopwatch Start/Stop,以便它仅包含doAction()。但是,我无法将秒表结果写入TestContext结果。

[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可以继承并重新定义。但是,我看不到任何将其写回到事务存储的示例。

有没有我可以引用的实现或其他想法。我希望在Visual Studio为LoadTest提供的同一份报告中看到它。否则,我必须编写自己的报告。

另外,我尝试嗅探将这些内容写入LoadTest数据库的SQL,但未能成功找出方法。应该有一个SPROC来调用,但是我认为这是测试结束时的所有数据。

最佳答案

好吧,我有一个类似的问题。我想像Visual Studio一样在最终测试结果中报告测试中的一些额外数据/报告/计数器,因此我找到了解决方案。

首先,这无法通过您尝试的方式完成。在存在TestContext的负载测试和单元测试之间没有直接链接。

其次,您必须了解Visual Studio如何创建报告。它从操作系统的performance counters收集数据。您可以编辑这些计数器,删除不需要的计数器,然后添加所需的其他计数器。

如何编辑计数器

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

  • Counter Sets。这些是计数器集,例如默认添加的agent。如果您打开此计数器集,您将看到它收集了诸如内存,处理器,PhysicalDisk e.t.c之类的计数器。因此,在测试结束时,您可以查看所有代理商的所有这些数据。如果要向此计数器集添加更多计数器,则可以双击它(在负载测试编辑器中,请参见下图),然后选择Add Counters。这将打开一个窗口,其中包含系统的所有计数器,然后选择所需的计数器。
  • Counter Set Mappings。在这里,您将计数器集与您的机器关联。默认情况下,[CONTROLLER MACHINE][AGENT MACHINES]带有一些默认计数器集。这意味着映射到[CONTROLLER MACHINE]的计数器集中包含的所有计数器将从您的 Controller 计算机中收集。同样适用于您的所有代理。


  • 您可以添加更多的计数器集和更多的计算机。右键单击 Counter Set Mappings-> Manage Counter Sets...,将打开一个新窗口,如下所示:

    如您所见,我添加了另一台名称为 db_1的计算机。这是机器的计算机名称,并且必须与 Controller 位于同一域中,才能访问它并收集计数器。我也已将其标记为 database server并选择了 sql计数器集(sql计数器的默认值,但您可以对其进行编辑并添加所需的任何计数器)。现在,每次执行此负载测试时, Controller 将转到计算机名称为db_1的计算机上,并收集将在最终测试结果中报告的数据。

    现在是编码部分

    好的,在进行了此大介绍之后,该看看如何将数据添加到最终测试结果中了。为此,您必须创建自己的 custom performance counters。这意味着必须在收集这些数据所需的计算机中创建一个新的 Performance Counter Category。在您的情况下,在您的所有代理中,因为这是执行UnitTests的地方。

    在代理中创建计数器后,可以编辑 Agents计数器集,如上所示,并选择其他自定义计数器。

    这是有关如何执行此操作的示例代码。

    首先为您的所有座席创建绩效指标。在每台代理计算机上仅运行一次此代码(或者您可以将其添加到 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);
    }

    这是您测试的代码:
    [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值进行报告。

    结论
  • 我认为(经过数月的研究),这是将测试中的自定义数据添加到最终负载测试报告中的唯一方法。
  • 似乎很难做到这一点。好吧,如果您理解了这一点,则对其进行优化并不困难。我将此功能包装在一个类中,以便于初始化,更新和管理计数器。
  • 这是非常非常有用的。现在,我可以从测试中看到统计信息,即使用默认计数器是不可能的。这样的我们,当对Web服务的Web请求失败时,我可以捕获该错误并更新相应的计数器(例如,超时,ServiceUnavailable,RequestRejected ...)。

  • 希望我能帮上忙。 :)

    关于c# - 我可以在Visual Studio中为单元测试/负载测试创建自定义TestContext计时器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16901453/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com