<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 使用率

          共 2476字,需瀏覽 5分鐘

           ·

          2021-11-16 01:44

          這篇文章我們分享一種如何在 .NETCore 中獲取 CPU使用率的方法, 它所報告的這個值和 任務(wù)管理器 中報告的 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 中是沒有的,所以只能采用其他方式了,其實在 System.Diagnostics.Process 類中有一個 TotalProcessorTime 屬性,它可以準(zhǔn)實時的統(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利用率基本是一致的。

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

          瀏覽 67
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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>
                  波多野吉衣无码AV | 国产麻传媒一区二区三区网站入口 | 色婷婷中文字幕 | 啪啪AV网站 | 青春草视频在线免费观看 |