如何阅读“.NET Data Provider for SqlServer” MS Test 的性能计数器?
如何从 MS Test 中的单元测试中读取类别“.NET Data Provider for SqlServer”的性能计数器“NumberOfActiveConnections”?
我正在尝试以下操作,但似乎我弄错了实例名称。 MSDN 文档 声称这是获取实例名称的正确方法WinForms 应用程序的,但这不适用于 MS Test:
string instanceName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
当从 MS Test 运行上面的代码时,我从对 GetEntryAssembly()
的调用中得到 null
我已经还尝试使用 MS 测试过程的名称和其他变体,但没有任何运气。
这是当我使用上面的任何实例名称时将引发异常的示例代码:
PerformanceCounter counter = new PerformanceCounter(
".NET Data Provider for SqlServer",
"NumberOfActiveConnections",
instanceName,
true);
Assert.AreEqual<long>(0, counter.RawValue);
我通过将其添加到 app.config 来启用“NumberOfActiveConnections”计数器,按照 MSDN 文档:
<system.diagnostics>
<switches>
<add name="ConnectionPoolPerformanceCounterDetail" value="4"/>
</switches>
</system.diagnostics>
也许问题是为 MS Test 主机域启用了性能计数器,但没有为 MS Test 主机域启用了性能计数器。测试实际运行的域?
How do I read from the performance counter "NumberOfActiveConnections" of the category ".NET Data Provider for SqlServer" from a unit test in MS Test?
I'm trying the following but it seems like I get the instance name wrong. The MSDN documentation claims this is a correct way of getting the instance name of a WinForms app but this won't work with MS Test:
string instanceName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
When running the code above from MS Test I get null
back from the call to GetEntryAssembly()
I've also tried using the name of the MS Test process and other variations but without any luck.
This is the sample code that will throw exception when i'm using any of the instance names from above:
PerformanceCounter counter = new PerformanceCounter(
".NET Data Provider for SqlServer",
"NumberOfActiveConnections",
instanceName,
true);
Assert.AreEqual<long>(0, counter.RawValue);
I'm enabling the "NumberOfActiveConnections" counter by adding this to the app.config as per MSDN documentation:
<system.diagnostics>
<switches>
<add name="ConnectionPoolPerformanceCounterDetail" value="4"/>
</switches>
</system.diagnostics>
Perhaps the problem is that the performance counters are enabled for the MS Test host domain but not for the domain in which the tests actually run?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说 VSTestHost 的实例名称往往是一个长字符串,看起来有点类似于 GUID 后跟 PID,因此弄清楚实例名称有点麻烦,但如果您知道 PID(我会得到),然后可以通过执行以下操作来完成:
由于在单元测试期间调用 GetEntryAssembly() 返回 null,因此获取 VSTestHost.exe 的 PID 也是一种痛苦。 但是,您应该能够按照以下方式做一些事情:
The instance name for VSTestHost for me tends to be a long string that looks vaguely similar to a GUID followed by the PID, so figuring out the instance name is a little bit of a pain, though if you know the PID (I'll get to that in a second), then it can be done by doing something along these lines:
Since calling GetEntryAssembly() during a unit test returns null, it's also kind of a pain to get ahold of the PID for VSTestHost.exe. However, you should be able to do something along these lines: