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

