如何:生成性能统计信息

更新:2007 年 11 月

.NET Compact Framework 为创建有关应用程序性能的统计报告提供了相应的性能计数器。这些计数器可以测量对象分配、垃圾回收、集合及其他功能和进程。通过启用或禁用某个注册表设置可以控制是否生成有关应用程序的报告。

有关性能计数器的信息,请参见 .NET Compact Framework 中的性能计数器

生成性能统计信息

  1. 将以下注册表子项的值设置为 1:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETCompactFramework\PerfMonitor

    请参见此过程后的代码列表,查看设置该注册表值的示例。

  2. 运行想要分析其性能的应用程序。请勿同时运行其他任何 .NET Compact Framework 应用程序。

  3. 分析设备根目录下生成的统计信息文件。该文件与当前运行的 .NET Compact Framework 应用程序同名,但扩展名为 .stat。

    您可以将数据导入文本编辑器,或通过在 Excel 的“文本导入向导”对话框中选择“固定宽度”,将数据导入 Microsoft Excel。

  4. 将该注册表子项值设置为零可关闭性能计数器。

示例

下面的方法根据布尔型 perfOn 参数的值设置该注册表子项,以打开或关闭性能计数器。

' Call this method with True to 
' turn on the peformance counters, 
' or with False to turn them off.
Private Sub SetPerfCounters(perfOn As Boolean)

    ' Specify values for setting the registry.
    Dim userRoot As String = "HKEY_LOCAL_MACHINE"
    Dim subKey As String = "SOFTWARE\\Microsoft\\.NETCompactFramework\\PerfMonitor"
    Dim keyName As String = userRoot & "\" & subKey

    Dim PCset As Integer

    If perfOn = True Then
        PCset = 1
    Else
        PCset = 0
    End If

    ' Set the registry value.       
    Try
        Registry.SetValue(keyName, "Counters", PCset)
        If perfOn = True Then
            MessageBox.Show("Performance Counters On")
        Else
            MessageBox.Show("Performance Counters Off")
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub
// Call this method with True to 
// turn on the peformance counters, 
// or with False to turn them off.
private void SetPerfCounters(bool perfOn)
{
    // Specify values for setting the registry.
    string userRoot = "HKEY_LOCAL_MACHINE";
    string subkey = "SOFTWARE\\Microsoft\\.NETCompactFramework\\PerfMonitor";
    string keyName = userRoot + "\\" + subkey;

    int PCset;
    if(perfOn == true)
        PCset = 1;
    else
        PCset = 0;

    // Set the the registry value.
    try
    {
        Registry.SetValue(keyName, "Counters", PCset);
        if(perfOn == true)
            MessageBox.Show("Performance Counters On");
        else
            MessageBox.Show("Performance Counters Off");
    }
    catch(System.Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

编译代码

此示例需要引用下面的命名空间:

请参见

概念

.NET Compact Framework 中的性能计数器