feat: upgrade to .NET 6, refactor everything

This commit is contained in:
spiral
2021-11-26 21:10:56 -05:00
parent d28e99ba43
commit 1918c56937
314 changed files with 27954 additions and 27966 deletions

View File

@@ -1,47 +1,45 @@
using System.Diagnostics;
using System.Threading.Tasks;
using Serilog;
namespace PluralKit.Bot
namespace PluralKit.Bot;
public class CpuStatService
{
public class CpuStatService
private readonly ILogger _logger;
public CpuStatService(ILogger logger)
{
private readonly ILogger _logger;
_logger = logger;
}
public double LastCpuMeasure { get; private set; }
public double LastCpuMeasure { get; private set; }
public CpuStatService(ILogger logger)
{
_logger = logger;
}
/// <summary>
/// Gets the current CPU usage. Estimation takes ~5 seconds (of mostly sleeping).
/// </summary>
public async Task<double> EstimateCpuUsage()
{
// We get the current processor time, wait 5 seconds, then compare
// https://medium.com/@jackwild/getting-cpu-usage-in-net-core-7ef825831b8b
/// <summary>
/// Gets the current CPU usage. Estimation takes ~5 seconds (of mostly sleeping).
/// </summary>
public async Task<double> EstimateCpuUsage()
{
// We get the current processor time, wait 5 seconds, then compare
// https://medium.com/@jackwild/getting-cpu-usage-in-net-core-7ef825831b8b
_logger.Debug("Estimating CPU usage...");
var stopwatch = new Stopwatch();
_logger.Debug("Estimating CPU usage...");
var stopwatch = new Stopwatch();
stopwatch.Start();
var cpuTimeBefore = Process.GetCurrentProcess().TotalProcessorTime;
stopwatch.Start();
var cpuTimeBefore = Process.GetCurrentProcess().TotalProcessorTime;
await Task.Delay(5000);
await Task.Delay(5000);
stopwatch.Stop();
var cpuTimeAfter = Process.GetCurrentProcess().TotalProcessorTime;
stopwatch.Stop();
var cpuTimeAfter = Process.GetCurrentProcess().TotalProcessorTime;
var cpuTimePassed = cpuTimeAfter - cpuTimeBefore;
var timePassed = stopwatch.Elapsed;
var cpuTimePassed = cpuTimeAfter - cpuTimeBefore;
var timePassed = stopwatch.Elapsed;
var percent = cpuTimePassed / timePassed;
_logger.Debug("CPU usage measured as {Percent:P}", percent);
LastCpuMeasure = percent;
return percent;
}
var percent = cpuTimePassed / timePassed;
_logger.Debug("CPU usage measured as {Percent:P}", percent);
LastCpuMeasure = percent;
return percent;
}
}