如何阅读“.NET Data Provider for SqlServer” MS Test 的性能计数器?

发布于 2024-07-08 18:31:16 字数 1239 浏览 4 评论 0原文

如何从 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 技术交流群。

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

发布评论

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

评论(1

浮华 2024-07-15 18:31:16

对我来说 VSTestHost 的实例名称往往是一个长字符串,看起来有点类似于 GUID 后跟 PID,因此弄清楚实例名称有点麻烦,但如果您知道 PID(我会得到),然后可以通过执行以下操作来完成:

PerformanceCounterCategory category = new PerformanceCounterCategory(
    ".NET Data Provider for SqlServer");
string testInstanceName = null;
foreach(string instanceName in category.GetInstanceNames())
{
    if (instanceName.EndsWith(string.Format("[{0}]", pid)))
    {
        testInstanceName = instanceName;
        break;
    }
}

if(testInstanceName != null)
{
    PerformanceCounter counter = new PerformanceCounter(
        category.CategoryName,
        "Number of Active Connections",
        testInstanceName,
        true);
}

由于在单元测试期间调用 GetEntryAssembly() 返回 null,因此获取 VSTestHost.exe 的 PID 也是一种痛苦。 但是,您应该能够按照以下方式做一些事情:

Process[] testProcesses = Process.GetProcessesByName("VSTestHost");
if (testProcesses.Length == 1)
{
    pid = testProcesses[0].Id;
}

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:

PerformanceCounterCategory category = new PerformanceCounterCategory(
    ".NET Data Provider for SqlServer");
string testInstanceName = null;
foreach(string instanceName in category.GetInstanceNames())
{
    if (instanceName.EndsWith(string.Format("[{0}]", pid)))
    {
        testInstanceName = instanceName;
        break;
    }
}

if(testInstanceName != null)
{
    PerformanceCounter counter = new PerformanceCounter(
        category.CategoryName,
        "Number of Active Connections",
        testInstanceName,
        true);
}

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:

Process[] testProcesses = Process.GetProcessesByName("VSTestHost");
if (testProcesses.Length == 1)
{
    pid = testProcesses[0].Id;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文