網站首頁 編程語言 正文
以下文章來源于微信公眾號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-31 Python無法用requests獲取網頁源碼的解決方法_python
- 2022-06-22 Git中tag標簽的使用教程_其它綜合
- 2022-04-24 C++的靜態成員變量和靜態成員函數你了解多少_C 語言
- 2022-08-15 數據結構之鏈式棧的實現與簡單運用
- 2023-04-18 Python中selenium獲取token的方法_python
- 2022-07-18 RabbitMQ集群負載均衡使用Haproxy的原因
- 2022-07-16 CMake下調用anaconda的pytorch及numpy傳參CV::Mat給python(多線程
- 2022-04-25 老生常談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同步修改后的遠程分支