在NET?Core?中獲取?CPU?使用率
以下文章來(lái)源于微信公眾號(hào)DotNetCore實(shí)戰(zhàn)
在 .NET Framework 中,很多人會(huì)用 PerformanceCounter 類(lèi)做這件事情,
如下代碼:
? ? 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 類(lèi)中有一個(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)文章
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
HttpResponse的Output與OutputStream、Filter關(guān)系與區(qū)別介紹
在網(wǎng)上經(jīng)??匆?jiàn)有這樣的代碼HttpResponse response = HttpContext.Current.Response;現(xiàn)在我也來(lái)說(shuō)說(shuō)這幾個(gè)東東是什么吧2012-11-11
利用.NET 開(kāi)發(fā)服務(wù)器 應(yīng)用管理工具
這篇文章主要介紹如何利用.NET 開(kāi)發(fā)一個(gè)應(yīng)用管理工具的服務(wù)器,文章回先聊背景接著其是喲美好方法,需要的的小伙伴可以參考一下小面文章的具體內(nèi)容2021-10-10
ASP.NET Core如何注入多個(gè)服務(wù)實(shí)現(xiàn)類(lèi)
這篇文章主要介紹了ASP.NET Core如何注入多個(gè)服務(wù)實(shí)現(xiàn)類(lèi)的相關(guān)資料,需要的朋友可以參考下面文章的具體內(nèi)容2021-09-09
.net中線程同步的典型場(chǎng)景和問(wèn)題剖析
在使用多線程進(jìn)行編程時(shí),有一些經(jīng)典的線程同步問(wèn)題,對(duì)于這些問(wèn)題,.net提供了多種不同的類(lèi)來(lái)解決2012-11-11
.Net 7函數(shù)Ctor與CCtor使用及區(qū)別詳解
這篇文章主要為大家介紹了.Net 7函數(shù)Ctor與CCtor使用及區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
ASP.NET?Core?6最小API中使用日志和DI示例詳解
這篇文章主要為大家介紹了ASP.NET?Core?6最小API中使用日志和DI示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

