ASP.NET CORE LINUX获取CPU使用率

游戏冠军制作

使用此代码,此方法适用于Windows。对于Linux(Ubuntu)“ Linux中未提供PerformanceCounter”

PerformanceCounter counter = GetPerfCounterForProcessId(process.Id); //Just gets process by id dont worry...
var processUsages = counter.NextValue();
double processUsage = counter.NextValue() / Environment.ProcessorCount;

如何通过ID在Linux上正常运行时转移此方法以从进程中获取CPU使用率?

约翰娜·勒(Johnathan Le)

PerformanceCounter 表示Windows NT性能计数器组件,这意味着它仅可在Windows上运行并在Windows Performance Monitor上侦听。

对于Linux,您应该使用System.Diagnostics.Process.GetCurrentProcess()来计算CPU使用率。例:

var startTime = DateTime.UtcNow;
var startCpuUsage = Process.GetCurrentProcess().TotalProcessorTime;
var stopWatch = new Stopwatch();
// Start watching CPU
stopWatch.Start();

// Meansure something else, such as .Net Core Middleware
await _next.Invoke(httpContext);

// Stop watching to meansure
stopWatch.Stop();
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);

var cpuUsagePercentage = cpuUsageTotal * 100;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ASP.NET Core 2.2 遇到高 CPU 使用率

来自分类Dev

ASP.NET Core令牌获取异常

来自分类Dev

.NET BlockingCollection <T> CPU使用率

来自分类Dev

ASP.NET Core是否仍使用OWIN?

来自分类Dev

在ASP.NET Core中使用MimeMapping

来自分类Dev

在ASP.net Core中使用BeginCollectionItem

来自分类Dev

在ASP.NET Core中使用DirectoryServices

来自分类Dev

使用设置配置ASP.NET Core

来自分类Dev

使用ASP.NET Core MVC获取用户输入

来自分类Dev

使用Cookie的ASP.NET Core .NET Framework测试

来自分类Dev

asp.net core 1.0在webapi中获取Windows身份

来自分类Dev

获取ASP.NET Core 1中的所有缓存

来自分类Dev

如何在ASP.NET Core中获取IIS版本

来自分类Dev

在ASP.NET Core WebAPI中获取OData计数

来自分类Dev

从ASP.NET Core视图代码获取JSON

来自分类Dev

从ASP.NET Core中的ActionExecutionContext的HttpContext获取RawBody

来自分类Dev

ASP.NET Core通过ID获取数据列表

来自分类Dev

ASP.NET Core-如何从URL获取参数

来自分类Dev

JS获取cookie ASP.NET Core标识

来自分类Dev

ASP.NET Core 获取时传递表单值

来自分类Dev

获取 RedirectToPage 生成的 URL ASP.NET Core

来自分类Dev

如何在ASP.NET MVC 6(ASP.NET Core)中获取returnUrl AccessDeniedPath

来自分类Dev

ASP.NET Core执行Linux Shell命令

来自分类Dev

ASP .NET Core - Linux nginx ASPNETCORE_ENVIROMENT 错误

来自分类Dev

Asp.net Core Websocket 与 linux/docker 兼容吗?

来自分类Dev

ASP.NET Core返回

来自分类Dev

ASP.NET Core WebSockets

来自分类Dev

ASP.NET-Core嵌套的ASP TagHelper

来自分类Dev

在ASP.NET Core 1.0中使用SignalR