System.Diagnostics.PerformanceCounter 9.0.0

Prefix Reserved
dotnet add package System.Diagnostics.PerformanceCounter --version 9.0.0                
NuGet\Install-Package System.Diagnostics.PerformanceCounter -Version 9.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="9.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add System.Diagnostics.PerformanceCounter --version 9.0.0                
#r "nuget: System.Diagnostics.PerformanceCounter, 9.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install System.Diagnostics.PerformanceCounter as a Cake Addin
#addin nuget:?package=System.Diagnostics.PerformanceCounter&version=9.0.0

// Install System.Diagnostics.PerformanceCounter as a Cake Tool
#tool nuget:?package=System.Diagnostics.PerformanceCounter&version=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.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (317)

Showing the top 5 NuGet packages that depend on System.Diagnostics.PerformanceCounter:

Package Downloads
Microsoft.ApplicationInsights.PerfCounterCollector

Application Insights Performance Counters Collector allows you to send data collected by Performance Counters to Application Insights. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156

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

Oracle.ManagedDataAccess.Core

Oracle Data Provider for .NET (ODP.NET) Core is a multi-platform ADO.NET driver that provides fast data access from Microsoft .NET (Core) clients to Oracle databases. ODP.NET Core consists of a single 100% managed code dynamic-link library.

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.

Elastic.Apm

Elastic APM .NET Agent base package. This package provides core functionality for transmitting of all Elastic APM types and is a dependent package for all other Elastic APM package. Additionally this package contains the public Agent API that allows you to manually capture transactions and spans. Please install the platform specific package for the best experience. See: https://github.com/elastic/apm-agent-dotnet/tree/main/docs

GitHub repositories (64)

Showing the top 5 popular GitHub repositories that depend on System.Diagnostics.PerformanceCounter:

Repository Stars
microsoft/PowerToys
Windows system utilities to maximize productivity
PowerShell/PowerShell
PowerShell for every system!
rocksdanister/lively
Free and open-source software that allows users to set animated desktop wallpapers and screensavers powered by WinUI 3.
dotnet/orleans
Cloud Native application framework for .NET
JeffreySu/WeiXinMPSDK
微信全平台 .NET SDK, Senparc.Weixin for C#,支持 .NET Framework 及 .NET Core、.NET 8.0。已支持微信公众号、小程序、小游戏、微信支付、企业微信/企业号、开放平台、JSSDK、微信周边等全平台。 WeChat SDK for C#.
Version Downloads Last updated
9.0.0 162,620 11/12/2024
9.0.0-rc.2.24473.5 23,188 10/8/2024
9.0.0-rc.1.24431.7 10,289 9/10/2024
9.0.0-preview.7.24405.7 11,356 8/13/2024
9.0.0-preview.6.24327.7 16,064 7/9/2024
9.0.0-preview.5.24306.7 6,041 6/11/2024
9.0.0-preview.4.24266.19 4,413 5/21/2024
9.0.0-preview.3.24172.9 9,251 4/11/2024
9.0.0-preview.2.24128.5 2,055 3/12/2024
9.0.0-preview.1.24080.9 17,250 2/13/2024
8.0.1 1,076,345 10/8/2024
8.0.0 11,527,269 11/14/2023
8.0.0-rc.2.23479.6 25,075 10/10/2023
8.0.0-rc.1.23419.4 15,946 9/12/2023
8.0.0-preview.7.23375.6 13,455 8/8/2023
8.0.0-preview.6.23329.7 14,974 7/11/2023
8.0.0-preview.5.23280.8 4,459 6/13/2023
8.0.0-preview.4.23259.5 10,778 5/16/2023
8.0.0-preview.3.23174.8 31,393 4/11/2023
8.0.0-preview.2.23128.3 6,787 3/14/2023
8.0.0-preview.1.23110.8 36,564 2/21/2023
7.0.0 10,011,329 11/7/2022
7.0.0-rc.2.22472.3 44,367 10/11/2022
7.0.0-rc.1.22426.10 67,245 9/14/2022
7.0.0-preview.7.22375.6 14,529 8/9/2022
7.0.0-preview.6.22324.4 2,168 7/12/2022
7.0.0-preview.5.22301.12 3,310 6/14/2022
7.0.0-preview.4.22229.4 12,250 5/10/2022
7.0.0-preview.3.22175.4 3,281 4/13/2022
7.0.0-preview.2.22152.2 12,992 3/14/2022
7.0.0-preview.1.22076.8 3,968 2/17/2022
6.0.2 8,135 11/12/2024
6.0.1 28,620,335 4/12/2022
6.0.0 75,689,560 11/8/2021
6.0.0-rc.2.21480.5 5,546 10/12/2021
6.0.0-rc.1.21451.13 13,690 9/14/2021
6.0.0-preview.7.21377.19 4,783 8/10/2021
6.0.0-preview.6.21352.12 2,868 7/14/2021
6.0.0-preview.5.21301.5 6,707 6/15/2021
6.0.0-preview.4.21253.7 27,942 5/24/2021
6.0.0-preview.3.21201.4 9,868 4/8/2021
6.0.0-preview.2.21154.6 19,168 3/11/2021
6.0.0-preview.1.21102.12 6,819 2/12/2021
5.0.1 14,731,651 2/9/2021 5.0.1 is deprecated because it is no longer maintained.
5.0.0 179,300,464 11/9/2020 5.0.0 is deprecated because it is no longer maintained.
5.0.0-rc.2.20475.5 15,612 10/13/2020
5.0.0-rc.1.20451.14 7,043 9/14/2020
5.0.0-preview.8.20407.11 9,206 8/25/2020
5.0.0-preview.7.20364.11 49,443 7/21/2020
5.0.0-preview.6.20305.6 5,779 6/25/2020
5.0.0-preview.5.20278.1 4,950 6/10/2020
5.0.0-preview.4.20251.6 12,505 5/18/2020
5.0.0-preview.3.20214.6 49,555 4/23/2020
5.0.0-preview.2.20160.6 20,260 4/2/2020
5.0.0-preview.1.20120.5 7,343 3/16/2020
4.7.0 255,732,562 12/3/2019
4.7.0-preview3.19551.4 24,717 11/13/2019
4.7.0-preview2.19523.17 14,108 11/1/2019
4.7.0-preview1.19504.10 18,437 10/15/2019
4.6.0 4,771,608 9/23/2019
4.6.0-rc1.19456.4 9,241 9/16/2019
4.6.0-preview9.19421.4 4,012 9/4/2019
4.6.0-preview9.19416.11 579 9/4/2019
4.6.0-preview8.19405.3 26,323 8/13/2019
4.6.0-preview7.19362.9 14,220 7/23/2019
4.6.0-preview6.19303.8 1,120,674 6/12/2019
4.6.0-preview6.19264.9 619 9/4/2019
4.6.0-preview5.19224.8 25,075 5/6/2019
4.6.0-preview4.19212.13 2,103 4/18/2019
4.6.0-preview3.19128.7 12,762 3/6/2019
4.6.0-preview.19073.11 14,560 1/29/2019
4.6.0-preview.18571.3 20,055 12/3/2018
4.5.0 166,938,306 5/29/2018
4.5.0-rc1 57,818 5/6/2018
4.5.0-preview2-26406-04 20,372 4/10/2018
4.5.0-preview1-26216-02 124,702 2/26/2018
4.5.0-preview1-25914-04 334,257 11/15/2017