about summary refs log tree commit diff
path: root/MatrixUtils.Web/Shared/ResourceUsage.razor
blob: 2a6365f9b17ae491453c56c3b6b8f2ad3e681f69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
@using ArcaneLibs
@using System.Diagnostics
<h3>ResourceUsage</h3>
<ModalWindow Title="Resource usage">
    <div style="background-color: white; color: black;">
        <span>Memory usage: @lastMemoryUsage</span>
        <br/>
        <TimelineGraph Data="MemoryUsage" ValueFormatter="@((double val) => Util.BytesToString((long)val))" Width="400"></TimelineGraph>
    </div>
    
    <div style="background-color: white; color: black;">
        <span>Time jitter: @lastCpuJitter</span>
        <br/>
        <TimelineGraph Data="CpuUsage" ValueFormatter="@(val => TimeSpan.FromTicks((long)val).ToString())" Width="400"></TimelineGraph>
    </div>
</ModalWindow>

@code {
    private Dictionary<DateTime, double> MemoryUsage = new();
    private Dictionary<DateTime, double> 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();
    }

}