我的一些UnitTests有一个在循环中定义的Sleep。我想不仅要描述测试的每次迭代,而且要描述所有迭代的总时间,以便显示任何非线性缩放。例如,如果我描述“整体”,它包括睡眠时间。我可以使用Stopwatch
启动/停止,以便它只包含doAction()。但是,我无法将Stopwatch结果写入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来调用,但我认为它是测试结束时的所有数据。
答案 0 :(得分:9)
好吧,我遇到了类似的问题。我想在Visual Studio的最终测试结果中报告我的测试中的一些额外的数据/报告/计数器,我找到了解决方案。
首先,这不能用你尝试的方式来完成。 Load Test和Test Test存在的单元测试之间没有直接的联系。
其次,您必须了解visual studio如何创建报告。它从操作系统的performance counters收集数据。您可以编辑这些计数器,删除不需要的计数器并添加其他所需的计数器。
负载测试配置有两个关于计数器的基本部分。这些是:
Counter Sets
。这些是计数器集,例如agent
,默认情况下会添加。如果打开此计数器集,您将看到它收集计数器,如Memory,Processor,PhysicalDisk e.t.c.因此,在测试结束时,您可以查看所有代理的所有这些数据。如果要在此计数器集中添加更多计数器,可以双击它(从负载测试编辑器,见下图)并选择Add Counters
。这将打开一个包含系统所有计数器的窗口,并选择您想要的那些。
Counter Set Mappings
。在这里,您可以将计数器组与您的机器相关联默认情况下,[CONTROLLER MACHINE]
和[AGENT MACHINES]
会添加一些默认计数器集。这意味着将从控制器计算机收集映射到[CONTROLLER MACHINE]
的计数器集中包含的所有计数器。这同样适用于所有代理商。
您可以添加更多计数器集和更多计算机。右键点击Counter Set Mappings
- &gt; Manage Counter Sets...
会打开一个新窗口,如下所示:
如您所见,我添加了一个名为db_1
的额外计算机。这是计算机的计算机名称,它必须与控制器位于同一个域中才能访问它并收集计数器。我还将其标记为database server
并选择了sql
计数器集(sql计数器的默认值,但您可以编辑它并添加您想要的任何计数器)。现在,每次执行此负载测试时,控制器将转到计算机名为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值报告。
我希望我帮助过。 :)
答案 1 :(得分:0)
我不知道如何将值添加到TestContext,因此通过该机制保存它。另一种方法是简单地将计时结果作为文本写入跟踪,调试或控制台输出流,以便将其保存在测试运行的日志中。要查看这些输出,需要考虑活动运行设置的三个 Logging 属性。它们的默认值仅保存前200次失败测试的日志。将将已完成测试的日志频率设置为 1 应保存所有测试的日志,直到达到最大测试日志。这些步骤在http://blogs.msdn.com/b/billbar/archive/2009/06/09/vsts-2010-load-test-feature-saving-test-logs.aspx
中有更详细的说明此方法的一个缺点是,只能在Visual Studio中一次看到一个日志文件,方法是单击其中一个结果窗口中的测试日志链接。我一直试图找到一种从测试结果的SQL数据库中提取Web测试日志的方法,而不是必须单击Visual Studio中每个日志的链接。我相信单元测试日志以相同的方式保存。我已经在https://stackoverflow.com/questions/16914487/how-do-i-extract-test-logs-from-visual-studios-load-test-results
中描述了这个问题以及我迄今为止所处理的问题更新。我相信在Visual Studio的负载测试环境中直接提供的API无法提供问题中的问题。数据和诊断适配器可以编写用于Web性能测试,也可能用于单元测试。通过使用这样的适配器代码,可以记录来自应用程序或测试套件的数据,并将其记录在测试结果中。有几个关于编写数据和诊断适配器的Microsoft博客和MSDN页面。
答案 2 :(得分:0)
最简单的方法是OP的原创方法,我似乎遇到了一些问题而其他问题似乎也是如此。一个是由于某种原因TestContext.BeginTimer(string);并不总是存在,请参阅this获取证据,但似乎没有解决方案。但另一个问题是错误地创建和使用该属性。
TestContext
的媒体资源并尝试使用TestContext.BeginTimer();
,则会收到"Cannot Access Non-Static Method 'BeginTimer' in a static context"
消息。有些人这样做的原因是因为大多数示例都将TestContext属性设置为`TestContext TestContext;&#39;有关示例使用此内容的原因,请参阅3. 如果您在TestContext
或ClassInitialize
中分配了AssemblyInitialize
属性,那么您似乎得到了一些不太正确的内容,那么您将获得一个测试上下文的实例,在过去,我对单元测试和编码UI测试没有任何问题,但是负载测试不能处理这个问题。如果您这样做,您会看到错误"There is already an active timer with the name 'TimerName' passed to BeginTimer"
。
所以最终解决方案,确保将TestContext设置为完整属性,如果这样做,属性将由测试执行引擎独立设置,以用于每次负载测试运行。这意味着您不必自己设置值。
所以你需要以下内容:
private TestContext m_testContext;
public TestContext TestContext
{
get { return m_testContext; }
set { m_testContext = value; }
}
如果你在setter上设置一个断点,你会在Class Initialize之后但TestInitialize
之前看到'TestContext setter&#39;调用并从UnitTestExecuter.SetTestContext()
分配值。
现在,测试完全按照您尝试的方式进行测试
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");
}
现在,当您查看负载测试结果时,您将在Scenario&gt;下看到计时器输出。 TestCaseName&gt;交易&gt; TimerName
这是我的输出看起来像我的定时器Cache,Create-,Login
其中包含
然后可以在图表上查看所有这些内容。
在OP的示例中,如果您运行负载测试,其中10个用户每次运行测试1次,DoWork花费0秒,您会看到:
我认为这是预期的结果。
这些问题花了我几个小时来弄清楚并经过一些测试以确切地确定和验证,但最终这似乎是最简单和最好的解决方案。
另一方面,这是TestContext的正确实现,使数据驱动测试能够正常工作,因为每个测试都可以从上下文中获取正确的数据。