如何在 .NET Core 中获取 CPU 使用率

商务办公
这篇文章我们分享一种如何在 .NETCore 中获取 CPU使用率的方法, 它所报告的这个值和 任务管理器 中报告的 CPU 使用值 差不多是一致的。

[[434460]]

这篇文章我们分享一种如何在 .NETCore 中获取 CPU使用率的方法, 它所报告的这个值和 任务管理器 中报告的 CPU 使用值 差不多是一致的。

在 .NET Framework 中,很多人会用 PerformanceCounter 类做这件事情,参考如下代码:

  1. public class Program 
  2.   { 
  3.       public static void Main(string[] args) 
  4.       { 
  5.           while (true
  6.           { 
  7.               var cpuUsage = GetCpuUsageForProcess(); 
  8.  
  9.               Console.WriteLine(cpuUsage); 
  10.           } 
  11.       } 
  12.  
  13.       private static int GetCpuUsageForProcess() 
  14.       { 
  15.           var currentProcessName = Process.GetCurrentProcess().ProcessName; 
  16.           var cpuCounter = new PerformanceCounter("Process""% Processor Time", currentProcessName); 
  17.           cpuCounter.NextValue(); 
  18.           return (int)cpuCounter.NextValue(); 
  19.       } 
  20.   } 

但 PerformanceCounter 在 .NETCore 中是没有的,所以只能采用其他方式了,其实在 System.Diagnostics.Process 类中有一个 TotalProcessorTime 属性,它可以准实时的统计当前进程所消耗的CPU处理器时间,参考如下代码:

  1. class Program 
  2.     public static async Task Main(string[] args) 
  3.     { 
  4.         var task = Task.Run(() => ConsumeCPU(50)); 
  5.  
  6.         while (true
  7.         { 
  8.             await Task.Delay(2000); 
  9.             var cpuUsage = await GetCpuUsageForProcess(); 
  10.  
  11.             Console.WriteLine(cpuUsage); 
  12.         } 
  13.     } 
  14.  
  15.     public static void ConsumeCPU(int percentage) 
  16.     { 
  17.         Stopwatch watch = new Stopwatch(); 
  18.         watch.Start(); 
  19.         while (true
  20.         { 
  21.             if (watch.ElapsedMilliseconds > percentage) 
  22.             { 
  23.                 Thread.Sleep(100 - percentage); 
  24.                 watch.Reset(); 
  25.                 watch.Start(); 
  26.             } 
  27.         } 
  28.     } 
  29.  
  30.     private static async Task<double> GetCpuUsageForProcess() 
  31.     { 
  32.         var startTime = DateTime.UtcNow; 
  33.         var startCpuUsage = Process.GetCurrentProcess().TotalProcessorTime; 
  34.  
  35.         await Task.Delay(500); 
  36.  
  37.         var endTime = DateTime.UtcNow; 
  38.         var endCpuUsage = Process.GetCurrentProcess().TotalProcessorTime; 
  39.  
  40.         var cpuUsedMs = (endCpuUsage - startCpuUsage).TotalMilliseconds; 
  41.         var totalMsPassed = (endTime - startTime).TotalMilliseconds; 
  42.  
  43.         var cpuUsageTotal = cpuUsedMs / (Environment.ProcessorCount * totalMsPassed); 
  44.  
  45.         return cpuUsageTotal * 100; 
  46.     } 

可以看到程序每2s输出一次,观察到 output 和 任务管理器 中的CPU利用率基本是一致的。

 

译文链接:https://medium.com/@jackwild/getting-cpu-usage-in-net-core-7ef825831b8b

 

责任编辑:武晓燕 来源: 一线码农聊技术
相关推荐

2010-01-18 10:13:08

VB.NET获取CPU

2022-07-23 21:31:24

KubernetesLinux开源

2021-05-31 15:53:57

CPU Top命令

2021-08-10 11:45:57

topCPULinux

2020-07-08 07:00:00

LinuxCPU应用程序

2009-11-16 17:03:20

Oracle优化CPU

2024-04-11 13:27:19

Linuxtop命令

2019-01-15 15:04:54

CPU电脑使用率

2014-12-01 13:44:03

cgroupscpulimitlinux

2022-04-06 06:35:58

进程命令CPU

2023-03-06 08:41:32

CPU使用率排查

2021-03-17 09:45:31

LazyCacheWindows

2021-02-02 16:19:08

Serilog日志框架

2021-02-06 21:40:13

SignalR通讯TypeScript

2021-03-10 09:40:43

LamarASP容器

2021-01-07 07:39:07

工具接口 Swagger

2021-02-03 13:35:25

ASPweb程序

2021-01-28 22:39:35

LoggerMessa开源框架

2021-03-03 22:37:16

MediatR中介者模式

2021-01-31 22:56:50

FromServiceASP
点赞
收藏

51CTO技术栈公众号