From c69e5c790b2b277d9b11265b8f0883e9f90fe3b9 Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 22 Mar 2024 17:47:29 +0100 Subject: Changes --- MatrixUtils.Web/Shared/ResourceUsage.razor | 64 ++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 MatrixUtils.Web/Shared/ResourceUsage.razor (limited to 'MatrixUtils.Web/Shared/ResourceUsage.razor') diff --git a/MatrixUtils.Web/Shared/ResourceUsage.razor b/MatrixUtils.Web/Shared/ResourceUsage.razor new file mode 100644 index 0000000..2a6365f --- /dev/null +++ b/MatrixUtils.Web/Shared/ResourceUsage.razor @@ -0,0 +1,64 @@ +@using ArcaneLibs +@using System.Diagnostics +

ResourceUsage

+ +
+ Memory usage: @lastMemoryUsage +
+ +
+ +
+ Time jitter: @lastCpuJitter +
+ +
+
+ +@code { + private Dictionary MemoryUsage = new(); + private Dictionary CpuUsage = new(); + private string lastMemoryUsage = ""; + private string lastCpuJitter = ""; + + protected override async Task OnInitializedAsync() { + Task.Run(async () => { + try { + while (true) { + lastMemoryUsage = Util.BytesToString((long)(MemoryUsage[DateTime.Now] = GC.GetTotalMemory(false))); + if (MemoryUsage.Count > 60) + MemoryUsage.Remove(MemoryUsage.Keys.First()); + await Task.Delay(1000); + } + } + catch (Exception e) { + Console.WriteLine(e); + } + }); + + // calculate cpu usage estimate without Process or PerformanceCounter + Task.Run(async () => { + try { + var sw = new Stopwatch(); + while (true) { + sw.Restart(); + await Task.Delay(1000); + sw.Stop(); + // CpuUsage[DateTime.Now] = sw.ElapsedTicks - TimeSpan.TicksPerSecond; + var usage = sw.Elapsed - TimeSpan.FromSeconds(1); + CpuUsage[DateTime.Now] = usage.Ticks - TimeSpan.TicksPerSecond; + lastCpuJitter = usage.ToString(); + if (CpuUsage.Count > 60) + CpuUsage.Remove(MemoryUsage.Keys.First()); + StateHasChanged(); + } + } + catch (Exception e) { + Console.WriteLine(e); + } + }); + + await base.OnInitializedAsync(); + } + +} \ No newline at end of file -- cgit 1.4.1