Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何阅读;。“SqlServer的NET数据提供程序”;MS测试的性能计数器?_Unit Testing_Mstest_Performancecounter - Fatal编程技术网

Unit testing 如何阅读;。“SqlServer的NET数据提供程序”;MS测试的性能计数器?

Unit testing 如何阅读;。“SqlServer的NET数据提供程序”;MS测试的性能计数器?,unit-testing,mstest,performancecounter,Unit Testing,Mstest,Performancecounter,如何从MS测试中的单元测试中读取.NET Data Provider for SqlServer类别的性能计数器“NumberOfActiveConnections” 我正在尝试以下操作,但似乎实例名称搞错了。声明这是获取WinForms应用程序实例名称的正确方法,但这不适用于MS Test: string instanceName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name; 从MS测试运行上述代码时,我从调用

如何从MS测试中的单元测试中读取.NET Data Provider for SqlServer类别的性能计数器“NumberOfActiveConnections”

我正在尝试以下操作,但似乎实例名称搞错了。声明这是获取WinForms应用程序实例名称的正确方法,但这不适用于MS Test:

string instanceName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
从MS测试运行上述代码时,我从调用
GetEntryAssembly()

我也尝试使用MS测试过程的名称和其他变体,但没有任何运气

这是当我使用上述任何实例名称时将引发异常的示例代码:

PerformanceCounter counter = new PerformanceCounter(
    ".NET Data Provider for SqlServer", 
    "NumberOfActiveConnections", 
    instanceName, 
    true);            

Assert.AreEqual<long>(0, counter.RawValue);
PerformanceCounter计数器=新的PerformanceCounter(
“.NET SqlServer数据提供程序”,
“NumberOfActiveConnections”,
instanceName,
正确的);
Assert.AreEqual(0,counter.RawValue);
我正在启用“NumberOfActiveConnections”计数器,方法是将其添加到app.config,如下所示:



可能问题在于性能计数器是为MS测试主机域启用的,而不是为实际运行测试的域启用的?

我的VSTestHost实例名往往是一个长字符串,看起来与后跟PID的GUID有点类似,因此找出实例名有点麻烦,不过,如果您知道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);
}
Process[] testProcesses = Process.GetProcessesByName("VSTestHost");
if (testProcesses.Length == 1)
{
    pid = testProcesses[0].Id;
}
由于在单元测试期间调用GetEntryAssembly()会返回null,所以获取VSTestHost.exe的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);
}
Process[] testProcesses = Process.GetProcessesByName("VSTestHost");
if (testProcesses.Length == 1)
{
    pid = testProcesses[0].Id;
}