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

发布于 2024-12-28 14:45:58 字数 487 浏览 7 评论 0原文

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

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

我有以下 UnitTest:

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

    double requestDelay = testTimer.Elapsed.TotalSeconds;
}

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

这可能吗?

I am new on Load Testing (and in general, testing) with visual studio 2010 and I am dealing with several problems.

My question is, is there any way possible, to add a custom test variable on the Load Test Results?

I have the following UnitTest:

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

    double requestDelay = testTimer.Elapsed.TotalSeconds;
}

This UnitTest is used by many LoadTests and I want to add the requestDelay variable to the Load Test Result so I can get Min, Max and Avg values like all others Load Test Counters (e.g. Test Response Time).

Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以歌曲疗慰 2025-01-04 14:45:59

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

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

你可以找到很好的文档 此处

I think what you actually need is to use:

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

You can find good documentation here.

风筝有风,海豚有海 2025-01-04 14:45:59

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

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

Interesting question. Never tried this, but I have an idea.

Create 3 class level properties of MAX, MIN and AVG. during each test manipulate those values. And then write all final values once entire load test get executed via Classcleanup or assemblycleanup test attribute. You have to run the load test for 1-2 min and have to see which attribute method get called at the end. You can then print those final values in a flat file in local drive via textwriter.

以往的大感动 2025-01-04 14:45:58

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

首先,我创建了一个 负载测试插件< /a> 并使用了 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 并选择添加计数器... 在“选择性能计数器”窗口中,我选择了性能类别并将计数器添加到 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
    }
}

链接:

Using the link from the @Pritam Karmakar comment and the walkthroughs at the end of my post I finally managed to find a solution.

First I created a Load Test Plug-In and used the LoadTestStarting Event to create my Custom Counter Category and add to it all my counters:

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);
}

Then, in the LoadTest editor I right-clicked on the Agent CounterSet and selected Add Counters... In the Pick Performance Counters window I chose my performance category and add my counters to the CounterSet so the Load Test will gather their data:

enter image description here

Finally, every UnitTest creates instances of the Counters in the ClassInitialize method and then it updates the counters at the proper step:

[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
    }
}

Links:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文