<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          如何在 .NET Core 中獲取 CPU 使用率

          共 2796字,需瀏覽 6分鐘

           ·

          2021-11-12 01:54


          這篇文章我們分享一種如何在?.NETCore?中獲取 CPU使用率的方法, 它所報(bào)告的這個(gè)值和?任務(wù)管理器?中報(bào)告的 CPU 使用值 差不多是一致的。

          在?.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 中是沒有的,所以只能采用其他方式了,其實(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利用率基本是一致的。

          譯文鏈接:https://medium.com/@jackwild/getting-cpu-usage-in-net-core-7ef825831b8b


          往期精彩回顧




          【推薦】.NET Core開發(fā)實(shí)戰(zhàn)視頻課程?★★★

          .NET Core實(shí)戰(zhàn)項(xiàng)目之CMS 第一章 入門篇-開篇及總體規(guī)劃

          【.NET Core微服務(wù)實(shí)戰(zhàn)-統(tǒng)一身份認(rèn)證】開篇及目錄索引

          Redis基本使用及百億數(shù)據(jù)量中的使用技巧分享(附視頻地址及觀看指南)

          .NET Core中的一個(gè)接口多種實(shí)現(xiàn)的依賴注入與動(dòng)態(tài)選擇看這篇就夠了

          10個(gè)小技巧助您寫出高性能的ASP.NET Core代碼

          用abp vNext快速開發(fā)Quartz.NET定時(shí)任務(wù)管理界面

          在ASP.NET Core中創(chuàng)建基于Quartz.NET托管服務(wù)輕松實(shí)現(xiàn)作業(yè)調(diào)度

          現(xiàn)身說法:實(shí)際業(yè)務(wù)出發(fā)分析百億數(shù)據(jù)量下的多表查詢優(yōu)化

          關(guān)于C#異步編程你應(yīng)該了解的幾點(diǎn)建議

          C#異步編程看這篇就夠了


          瀏覽 54
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  黄色电影网站在线观看中文字幕 | 性拍拍视频 | 一级a一级a爱片免费 | 国产精品久久久久久视频 | 大香蕉黄色网址 |