網站首頁 編程語言 正文
以下文章來源于微信公眾號DotNetCore實戰?
在 .NET Framework
中,很多人會用 PerformanceCounter
類做這件事情,
如下代碼:
? ? public class Program ? ? { ? ? ? ? public static void Main(string[] args) ? ? ? ? { ? ? ? ? ? ? while (true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? var cpuUsage = GetCpuUsageForProcess(); ? ? ? ? ? ? ? ? Console.WriteLine(cpuUsage); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private static int GetCpuUsageForProcess() ? ? ? ? { ? ? ? ? ? ? var currentProcessName = Process.GetCurrentProcess().ProcessName; ? ? ? ? ? ? var cpuCounter = new PerformanceCounter("Process", "% Processor Time", currentProcessName); ? ? ? ? ? ? cpuCounter.NextValue(); ? ? ? ? ? ? return (int)cpuCounter.NextValue(); ? ? ? ? } ? ? }
但 PerformanceCounter
在.NETCore
中是沒有的,所以只能采用其他方式了,其實在 System.Diagnostics.Process
類中有一個 TotalProcessorTime
屬性,它可以準實時的統計當前進程所消耗的CPU處理器時間,
如下代碼:
? ?
class Program ? ? { ? ? ? ? public static async Task Main(string[] args) ? ? ? ? { ? ? ? ? ? ? var task = Task.Run(() => ConsumeCPU(50)); ? ? ? ? ? ? while (true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? await Task.Delay(2000); ? ? ? ? ? ? ? ? var cpuUsage = await GetCpuUsageForProcess(); ? ? ? ? ? ? ? ? Console.WriteLine(cpuUsage); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? public static void ConsumeCPU(int percentage) ? ? ? ? { ? ? ? ? ? ? Stopwatch watch = new Stopwatch(); ? ? ? ? ? ? watch.Start(); ? ? ? ? ? ? while (true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (watch.ElapsedMilliseconds > percentage) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? Thread.Sleep(100 - percentage); ? ? ? ? ? ? ? ? ? ? watch.Reset(); ? ? ? ? ? ? ? ? ? ? watch.Start(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private static async Task<double> GetCpuUsageForProcess() ? ? ? ? { ? ? ? ? ? ? var startTime = DateTime.UtcNow; ? ? ? ? ? ? var startCpuUsage = Process.GetCurrentProcess().TotalProcessorTime; ? ? ? ? ? ? await Task.Delay(500); ? ? ? ? ? ? var endTime = DateTime.UtcNow; ? ? ? ? ? ? var endCpuUsage = Process.GetCurrentProcess().TotalProcessorTime; ? ? ? ? ? ? var cpuUsedMs = (endCpuUsage - startCpuUsage).TotalMilliseconds; ? ? ? ? ? ? var totalMsPassed = (endTime - startTime).TotalMilliseconds; ? ? ? ? ? ? var cpuUsageTotal = cpuUsedMs / (Environment.ProcessorCount * totalMsPassed); ? ? ? ? ? ? return cpuUsageTotal * 100; ? ? ? ? } ? ? }
可以看到程序每2s輸出一次,觀察到 output
和 任務管理器 中的CPU
利用率基本是一致的。
到此這篇關于在NET Core 中獲取 CPU 使用率的文章就介紹到這了,更多相關NET Core 中獲取 CPU 使用率內容請搜索AB教程網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持AB教程網!
相關推薦
- 2022-08-19 android九宮格鎖屏控件使用詳解_Android
- 2023-10-09 注冊頁面編寫
- 2022-07-27 Docker-Compose搭建Spark集群的實現方法_docker
- 2022-08-10 淺談pandas關于查看庫或依賴庫版本的API原理_python
- 2022-06-01 Python?中enum的使用方法總結_python
- 2022-12-15 Android?NotificationListenerService?通知服務原理解析_Andro
- 2022-07-29 PyTorch實現手寫數字的識別入門小白教程_python
- 2022-03-13 C語言之直接插入排序算法的方法_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支