System.Diagnostics.PerformanceCounter 9.0.0

About

This package provides types that allow applications to interact with the Windows performance counters.

Windows allows you to examine how programs you run affect your computer's performance, both in real time and by collecting log data for later analysis. You can do this via the Windows Performance Monitor tool, which uses performance counters, among other features.

Windows performance counters provide a high-level abstraction layer that provides a consistent interface for collecting various kinds of system data such as CPU, memory, and disk usage. They can be included in the operating system or can be part of individual applications. Windows Performance Monitor requests the current value of performance counters at specifiedtime intervals.

System administrators often use performance counters to monitor systems for performance or behavior problems. Software developers often use performance counters to examine the resource usage of their programs.

Key Features

  • Can be used to read existing predefined or custom counters.
  • Can be used for publishing (writing) data to custom counters.
  • Can collect performance counters from the local machine or from a remote machine.

How to Use

using System;
using System.Collections.Generic;
using System.Diagnostics;

public class App
{
    public static void Main()
    {
        List<CounterSample> samples = [];

        // If the category does not exist, create the category and exit.
        // Performance counters should not be created and immediately used.
        // There is a latency time to enable the counters, they should be created
        // prior to executing the application that uses the counters.
        // Execute this sample a second time to use the category.
        if (SetupCategory())
        {
            return;
        }

        CollectSamples(samples);
        CalculateResults(samples);
    }

    private static bool SetupCategory()
    {
        if (PerformanceCounterCategory.Exists("AverageCounter64SampleCategory"))
        {
            Console.WriteLine("Category exists - AverageCounter64SampleCategory");
            return false;
        }

        CounterCreationDataCollection counterDataCollection = [];

        // Add the counter.
        CounterCreationData averageCount64 = new()
        {
            CounterType = PerformanceCounterType.AverageCount64,
            CounterName = "AverageCounter64Sample"
        };
        counterDataCollection.Add(averageCount64);

        // Add the base counter.
        CounterCreationData averageCount64Base = new()
        {
            CounterType = PerformanceCounterType.AverageBase,
            CounterName = "AverageCounter64SampleBase"
        };
        counterDataCollection.Add(averageCount64Base);

        // Create the category.
        PerformanceCounterCategory.Create("AverageCounter64SampleCategory",
            "Demonstrates usage of the AverageCounter64 performance counter type.",
            PerformanceCounterCategoryType.SingleInstance, counterDataCollection);

        return true;
    }

    private static void CollectSamples(List<CounterSample> samples)
    {
        // Create the counters

        PerformanceCounter avgCounter64Sample = new PerformanceCounter("AverageCounter64SampleCategory",
            "AverageCounter64Sample",
            false)
        {
            RawValue = 0
        };

        PerformanceCounter avgCounter64SampleBase = new PerformanceCounter("AverageCounter64SampleCategory",
            "AverageCounter64SampleBase",
            false)
        {
            RawValue = 0
        };

        Random r = new(DateTime.Now.Millisecond);

        for (int j = 0; j < 100; j++)
        {
            int value = r.Next(1, 10);
            Console.Write(j + " = " + value);

            avgCounter64Sample.IncrementBy(value);

            avgCounter64SampleBase.Increment();

            if ((j % 10) == 9)
            {
                OutputSample(avgCounter64Sample.NextSample());
                samples.Add(avgCounter64Sample.NextSample());
            }
            else
            {
                Console.WriteLine();
            }

            System.Threading.Thread.Sleep(50);
        }
    }

    private static void CalculateResults(List<CounterSample> samples)
    {
        for (int i = 0; i < (samples.Count - 1); i++)
        {
            // Output the sample.
            OutputSample(samples[i]);
            OutputSample(samples[i + 1]);

            // Use .NET to calculate the counter value.
            Console.WriteLine($".NET computed counter value = {CounterSampleCalculator.ComputeCounterValue(samples[i], samples[i + 1])}");

            // Calculate the counter value manually.
            Console.WriteLine($"My computed counter value = {MyComputeCounterValue(samples[i], samples[i + 1])}");
        }
    }

    //    Description - This counter type shows how many items are processed, on average,
    //        during an operation. Counters of this type display a ratio of the items
    //        processed (such as bytes sent) to the number of operations completed. The
    //        ratio is calculated by comparing the number of items processed during the
    //        last interval to the number of operations completed during the last interval.
    // Generic type - Average
    //      Formula - (N1 - N0) / (D1 - D0), where the numerator (N) represents the number
    //        of items processed during the last sample interval and the denominator (D)
    //        represents the number of operations completed during the last two sample
    //        intervals.
    //    Average (Nx - N0) / (Dx - D0)
    //    Example PhysicalDisk\ Avg. Disk Bytes/Transfer
    private static float MyComputeCounterValue(CounterSample s0, CounterSample s1)
    {
        float numerator = (float)s1.RawValue - s0.RawValue;
        float denomenator = (float)s1.BaseValue - s0.BaseValue;
        float counterValue = numerator / denomenator;
        return counterValue;
    }

    private static void OutputSample(CounterSample s)
    {
        Console.WriteLine("\r\n+++++++++++");
        Console.WriteLine("Sample values - \r\n");
        Console.WriteLine($"   BaseValue        = {s.BaseValue}");
        Console.WriteLine($"   CounterFrequency = {s.CounterFrequency}");
        Console.WriteLine($"   CounterTimeStamp = {s.CounterTimeStamp}");
        Console.WriteLine($"   CounterType      = {s.CounterType}");
        Console.WriteLine($"   RawValue         = {s.RawValue}");
        Console.WriteLine($"   SystemFrequency  = {s.SystemFrequency}");
        Console.WriteLine($"   TimeStamp        = {s.TimeStamp}");
        Console.WriteLine($"   TimeStamp100nSec = {s.TimeStamp100nSec}");
        Console.WriteLine("++++++++++++++++++++++");
    }
}

Notes:

  • This assembly is only supported on Windows operating systems.
  • Only the administrator of the computer or users in the Performance Logs User Group can log counter data. Users in the Administrator group can log counter data only if the tool they use to log counter data is started from a Command Prompt window that is opened with Run as administrator.... Any users in interactive logon sessions can view counter data. However, users in non-interactive logon sessions must be in the Performance Monitoring Users group to view counter data.

Main Types

The main types provided by this library are:

Under the System.Diagnostics namespace, the main types are:

Under the System.Diagnostics.PerformanceData namespace, the main types are:

Additional Documentation

Feedback & Contributing

System.Diagnostics.PerformanceCounter is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.

Showing the top 20 packages that depend on System.Diagnostics.PerformanceCounter.

Packages Downloads
Microsoft.Windows.Compatibility
This Windows Compatibility Pack provides access to APIs that were previously available only for .NET Framework. It can be used from both .NET as well as .NET Standard.
87
Microsoft.Windows.Compatibility
This Windows Compatibility Pack provides access to APIs that were previously available only for .NET Framework. It can be used from both .NET as well as .NET Standard.
90
Microsoft.Windows.Compatibility
This Windows Compatibility Pack provides access to APIs that were previously available only for .NET Framework. It can be used from both .NET Core as well as .NET Standard.
78
Microsoft.Windows.Compatibility
This Windows Compatibility Pack provides access to APIs that were previously available only for .NET Framework. It can be used from both .NET Core as well as .NET Standard. 8f968b9e79c5721d78cc88e46fe57457fe9d490d When using NuGet 3.x this package requires at least version 3.4.
78
Microsoft.Windows.Compatibility
This Windows Compatibility Pack provides access to APIs that were previously available only for .NET Framework. It can be used from both .NET Core as well as .NET Standard. When using NuGet 3.x this package requires at least version 3.4.
81
Microsoft.Windows.Compatibility
This Windows Compatibility Pack provides access to APIs that were previously available only for .NET Framework. It can be used from both .NET Core as well as .NET Standard. When using NuGet 3.x this package requires at least version 3.4.
83
Microsoft.Windows.Compatibility
This Windows Compatibility Pack provides access to APIs that were previously available only for .NET Framework. It can be used from both .NET Core as well as .NET Standard. When using NuGet 3.x this package requires at least version 3.4.
89
System.Data.OleDb
Provides a collection of classes for OLEDB. Commonly Used Types: System.Data.OleDb.OleDbCommand System.Data.OleDb.OleDbCommandBuilder System.Data.OleDb.OleDbConnection System.Data.OleDb.OleDbDataAdapter System.Data.OleDb.OleDbDataReader System.Data.OleDb.OleDbParameter System.Data.OleDb.OleDbParameterCollection System.Data.OleDb.OleDbTransaction
84
System.Data.OleDb
Provides a collection of classes for OLEDB. Commonly Used Types: System.Data.OleDb.OleDbCommand System.Data.OleDb.OleDbCommandBuilder System.Data.OleDb.OleDbConnection System.Data.OleDb.OleDbDataAdapter System.Data.OleDb.OleDbDataReader System.Data.OleDb.OleDbParameter System.Data.OleDb.OleDbParameterCollection System.Data.OleDb.OleDbTransaction
89
System.Data.OleDb
Provides a collection of classes for OLEDB. Commonly Used Types: System.Data.OleDb.OleDbCommand System.Data.OleDb.OleDbCommandBuilder System.Data.OleDb.OleDbConnection System.Data.OleDb.OleDbDataAdapter System.Data.OleDb.OleDbDataReader System.Data.OleDb.OleDbParameter System.Data.OleDb.OleDbParameterCollection System.Data.OleDb.OleDbTransaction
91
System.Data.OleDb
Provides a collection of classes for OLEDB. Commonly Used Types: System.Data.OleDb.OleDbCommand System.Data.OleDb.OleDbCommandBuilder System.Data.OleDb.OleDbConnection System.Data.OleDb.OleDbDataAdapter System.Data.OleDb.OleDbDataReader System.Data.OleDb.OleDbParameter System.Data.OleDb.OleDbParameterCollection System.Data.OleDb.OleDbTransaction
95
System.Data.OleDb
Provides a collection of classes for OLEDB. Commonly Used Types: System.Data.OleDb.OleDbCommand System.Data.OleDb.OleDbCommandBuilder System.Data.OleDb.OleDbConnection System.Data.OleDb.OleDbDataAdapter System.Data.OleDb.OleDbDataReader System.Data.OleDb.OleDbParameter System.Data.OleDb.OleDbParameterCollection System.Data.OleDb.OleDbTransaction
101
System.Data.OleDb
Provides a collection of classes for OLEDB. Commonly Used Types: System.Data.OleDb.OleDbCommand System.Data.OleDb.OleDbCommandBuilder System.Data.OleDb.OleDbConnection System.Data.OleDb.OleDbDataAdapter System.Data.OleDb.OleDbDataReader System.Data.OleDb.OleDbParameter System.Data.OleDb.OleDbParameterCollection System.Data.OleDb.OleDbTransaction When using NuGet 3.x this package requires at least version 3.4.
78
System.Data.OleDb
Provides a collection of classes for OLEDB. Commonly Used Types: System.Data.OleDb.OleDbCommand System.Data.OleDb.OleDbCommandBuilder System.Data.OleDb.OleDbConnection System.Data.OleDb.OleDbDataAdapter System.Data.OleDb.OleDbDataReader System.Data.OleDb.OleDbParameter System.Data.OleDb.OleDbParameterCollection System.Data.OleDb.OleDbTransaction When using NuGet 3.x this package requires at least version 3.4.
80
System.Data.OleDb
Provides a collection of classes for OLEDB. Commonly Used Types: System.Data.OleDb.OleDbCommand System.Data.OleDb.OleDbCommandBuilder System.Data.OleDb.OleDbConnection System.Data.OleDb.OleDbDataAdapter System.Data.OleDb.OleDbDataReader System.Data.OleDb.OleDbParameter System.Data.OleDb.OleDbParameterCollection System.Data.OleDb.OleDbTransaction When using NuGet 3.x this package requires at least version 3.4.
81
System.Data.OleDb
Provides a collection of classes for OLEDB. Commonly Used Types: System.Data.OleDb.OleDbCommand System.Data.OleDb.OleDbCommandBuilder System.Data.OleDb.OleDbConnection System.Data.OleDb.OleDbDataAdapter System.Data.OleDb.OleDbDataReader System.Data.OleDb.OleDbParameter System.Data.OleDb.OleDbParameterCollection System.Data.OleDb.OleDbTransaction When using NuGet 3.x this package requires at least version 3.4.
86
System.Data.OleDb
Provides a collection of classes for OLEDB. Commonly Used Types: System.Data.OleDb.OleDbCommand System.Data.OleDb.OleDbCommandBuilder System.Data.OleDb.OleDbConnection System.Data.OleDb.OleDbDataAdapter System.Data.OleDb.OleDbDataReader System.Data.OleDb.OleDbParameter System.Data.OleDb.OleDbParameterCollection System.Data.OleDb.OleDbTransaction When using NuGet 3.x this package requires at least version 3.4.
87
System.Data.OleDb
Provides a collection of classes for OLEDB. Commonly Used Types: System.Data.OleDb.OleDbCommand System.Data.OleDb.OleDbCommandBuilder System.Data.OleDb.OleDbConnection System.Data.OleDb.OleDbDataAdapter System.Data.OleDb.OleDbDataReader System.Data.OleDb.OleDbParameter System.Data.OleDb.OleDbParameterCollection System.Data.OleDb.OleDbTransaction When using NuGet 3.x this package requires at least version 3.4.
89

https://go.microsoft.com/fwlink/?LinkID=799421

.NET Framework 4.6.2

  • No dependencies.

.NET 8.0

.NET 9.0

.NET Standard 2.0

  • No dependencies.

Version Downloads Last updated
9.0.0 7 11/13/2024
9.0.0-rc.2.24473.5 8 10/15/2024
9.0.0-rc.1.24431.7 13 09/11/2024
9.0.0-preview.7.24405.7 18 08/16/2024
9.0.0-preview.6.24327.7 13 07/09/2024
9.0.0-preview.5.24306.7 8 06/14/2024
9.0.0-preview.4.24266.19 10 05/23/2024
9.0.0-preview.3.24172.9 9 04/25/2024
9.0.0-preview.2.24128.5 14 03/14/2024
9.0.0-preview.1.24080.9 13 03/01/2024
8.0.1 10 10/15/2024
8.0.0 12 11/16/2023
8.0.0-rc.2.23479.6 11 10/19/2023
8.0.0-rc.1.23419.4 29 09/20/2023
8.0.0-preview.7.23375.6 12 09/04/2023
8.0.0-preview.6.23329.7 13 09/20/2023
8.0.0-preview.5.23280.8 13 06/18/2023
8.0.0-preview.4.23259.5 31 06/04/2023
8.0.0-preview.3.23174.8 30 04/14/2023
8.0.0-preview.2.23128.3 27 04/14/2023
8.0.0-preview.1.23110.8 20 04/14/2023
7.0.0 45 12/21/2022
7.0.0-rc.2.22472.3 87 10/15/2022
7.0.0-rc.1.22426.10 105 09/21/2022
7.0.0-preview.7.22375.6 86 08/13/2022
7.0.0-preview.6.22324.4 46 09/24/2022
7.0.0-preview.5.22301.12 39 09/26/2022
7.0.0-preview.4.22229.4 86 09/24/2022
7.0.0-preview.3.22175.4 44 09/22/2022
7.0.0-preview.2.22152.2 54 09/24/2022
7.0.0-preview.1.22076.8 44 09/24/2022
6.0.2 5 11/13/2024
6.0.2-mauipre.1.22102.15 68 09/26/2022
6.0.2-mauipre.1.22054.8 80 09/21/2022
6.0.1 46 09/23/2022
6.0.0 38 09/23/2022
6.0.0-rc.2.21480.5 40 09/21/2022
6.0.0-rc.1.21451.13 86 09/24/2022
6.0.0-preview.7.21377.19 102 09/24/2022
6.0.0-preview.6.21352.12 38 09/24/2022
6.0.0-preview.5.21301.5 42 09/23/2022
6.0.0-preview.4.21253.7 75 09/25/2022
6.0.0-preview.3.21201.4 59 10/15/2022
6.0.0-preview.2.21154.6 53 10/15/2022
6.0.0-preview.1.21102.12 49 09/24/2022
5.0.1 63 09/21/2022
5.0.0 90 09/21/2022
5.0.0-rc.2.20475.5 53 09/22/2022
5.0.0-rc.1.20451.14 47 09/24/2022
5.0.0-preview.8.20407.11 63 09/24/2022
5.0.0-preview.7.20364.11 38 09/24/2022
5.0.0-preview.6.20305.6 66 09/24/2022
5.0.0-preview.5.20278.1 49 09/24/2022
5.0.0-preview.4.20251.6 89 09/21/2022
5.0.0-preview.3.20214.6 35 10/15/2022
5.0.0-preview.2.20160.6 70 09/24/2022
5.0.0-preview.1.20120.5 24 09/23/2022
4.7.0 50 06/27/2022
4.7.0-preview3.19551.4 32 09/24/2022
4.7.0-preview2.19523.17 76 09/24/2022
4.7.0-preview1.19504.10 77 09/26/2022
4.6.0 75 09/24/2022
4.6.0-rc1.19456.4 31 09/23/2022
4.6.0-preview9.19421.4 25 10/15/2022
4.6.0-preview9.19416.11 64 09/25/2022
4.6.0-preview8.19405.3 93 09/24/2022
4.6.0-preview7.19362.9 42 09/24/2022
4.6.0-preview6.19303.8 25 09/21/2022
4.6.0-preview6.19264.9 53 09/25/2022
4.6.0-preview5.19224.8 77 09/24/2022
4.6.0-preview4.19212.13 24 09/22/2022
4.6.0-preview3.19128.7 38 09/24/2022
4.6.0-preview.19073.11 83 10/15/2022
4.6.0-preview.18571.3 79 09/23/2022
4.5.0 82 09/23/2022
4.5.0-rc1 26 09/24/2022
4.5.0-preview2-26406-04 47 09/26/2022
4.5.0-preview1-26216-02 69 09/24/2022
4.5.0-preview1-25914-04 84 09/25/2022