在NET?Core?中獲取?CPU?使用率
以下文章來源于微信公眾號DotNetCore實戰(zhàn)
在 .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 屬性,它可以準實時的統(tǒng)計當(dāng)前進程所消耗的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 和 任務(wù)管理器 中的CPU利用率基本是一致的。
到此這篇關(guān)于在NET Core 中獲取 CPU 使用率的文章就介紹到這了,更多相關(guān)NET Core 中獲取 CPU 使用率內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
譯文鏈接:https://medium.com/@jackwild/getting-cpu-usage-in-net-core-7ef825831b8b
相關(guān)文章
一文透徹詳解.NET框架類型系統(tǒng)設(shè)計要點
這篇文章主要為大家透徹詳解了選擇.NET框架的n個理由,本系列的第一篇文章全面概述了平臺的支柱和設(shè)計要點,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
ASP.NET Core MVC 依賴注入View與Controller
本文重點給大家介紹的是ASP.NET Core MVC 之依賴注入 View 和ASP.NET Core MVC 之依賴注入 Controller的相關(guān)資料,需要的小伙伴可以參考下面文章具體內(nèi)容2021-09-09
.Net性能調(diào)優(yōu)-ArrayPool詳情
ArrayPool具有高性能 托管 數(shù)組緩沖池,可重復(fù)使用,用 租用 空間的方式代替 重新分配 數(shù)組空間的行為的特點及可以在頻繁創(chuàng)建和銷毀數(shù)組的情況下 提高性能 ,減少垃圾回收器的壓力的優(yōu)點,下面文章內(nèi)容將詳細對其做介紹,需要的朋友可以參考一下2021-09-09
ABP入門系列應(yīng)用BootstrapTable表格插件
Bootstrap table是一個開源的輕量級功能非常豐富的前端表格插件。下面通過本文給大家介紹ABP入門系列應(yīng)用BootstrapTable表格插件,感興趣的朋友一起學(xué)習(xí)吧2017-03-03
ASP.NET Web API教程 創(chuàng)建域模型的方法詳細介紹
本文將介紹幾種常見的創(chuàng)建域模型的方法,有需要的朋友可以適當(dāng)?shù)膮⒖?/div> 2012-11-11
.Net Core HttpClient處理響應(yīng)壓縮詳細
.Net Core作為后起之秀直接將HttpClient扶正,并且在此基礎(chǔ)上改良了HttpClientFactory,接下來我們就來探究一下在.Net Core中使用HttpClient處理響應(yīng)壓縮的機制。,需要的朋友可以參考下面文章的具體內(nèi)容2021-09-09
ASP.NET?Core使用功能開關(guān)控制路由訪問操作
這篇文章主要介紹了ASP.NET?Core使用功能開關(guān)控制路由訪問操作,而對于一些試驗性的功能,我們并不希望用密碼去控制是否允許訪問,而是想用一種開關(guān)的方式開放,下面文章我們就來試著實現(xiàn)這個功能,需要的小伙伴可以參考一下2022-02-02最新評論

