using Masuit.Tools; using Masuit.Tools.DateTimeExt; using Masuit.Tools.Hardware; using Masuit.Tools.Systems; using Microsoft.AspNetCore.SignalR; using System; using System.Collections.Concurrent; using System.Linq; using System.Threading; using System.Threading.Channels; using System.Threading.Tasks; namespace Masuit.MyBlogs.Core.Hubs { /// /// 性能计数器 /// public class PerformanceCounter { /// /// 当前时间戳 /// public double Time { get; set; } /// /// CPU当前负载 /// public double CpuLoad { get; set; } /// /// CPU核心温度 /// public double Temperature { get; set; } /// /// 内存使用率 /// public double MemoryUsage { get; set; } /// /// 磁盘读 /// public double DiskRead { get; set; } /// /// 磁盘写 /// public double DiskWrite { get; set; } /// /// 网络上行 /// public double Upload { get; set; } /// /// 网络下行 /// public double Download { get; set; } } /// /// hub /// public class MyHub : Hub { /// /// 性能计数器缓存 /// public static ConcurrentLimitedQueue PerformanceCounter { get; set; } = new ConcurrentLimitedQueue(5000); static MyHub() { Task.Run(() => { int errorCount = 0; while (true) { try { PerformanceCounter.Enqueue(GetCurrentPerformanceCounter()); } catch (Exception e) { if (errorCount > 20) { break; } Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e.Message); Console.ForegroundColor = ConsoleColor.White; errorCount++; } Thread.Sleep(10000); } }); } /// /// 初始化 /// public static void Init() { } /// /// 当前连接客户端 /// public static ConcurrentDictionary Connections { get; set; } = new ConcurrentDictionary(); /// /// 连接事件 /// /// public override Task OnConnectedAsync() { Connections.TryAdd(Context.ConnectionId, false); return base.OnConnectedAsync(); } /// /// 注销事件 /// /// /// public override Task OnDisconnectedAsync(Exception exception) { Connections.TryRemove(Context.ConnectionId, out _); return Task.CompletedTask; } /// /// 性能统计 /// /// /// /// public ChannelReader Counter(int delay, CancellationToken cancellationToken) { var channel = Channel.CreateUnbounded(); _ = WriteItemsAsync(channel.Writer, delay, cancellationToken); return channel.Reader; } private async Task WriteItemsAsync(ChannelWriter writer, int delay, CancellationToken cancellationToken) { if (Connections[Context.ConnectionId]) { return; } byte errCount = 0; while (Connections.Any(s => s.Key.Equals(Context.ConnectionId))) { Connections[Context.ConnectionId] = true; try { cancellationToken.ThrowIfCancellationRequested(); await writer.WriteAsync(GetCurrentPerformanceCounter(), cancellationToken); } catch (Exception e) { if (errCount > 20) { break; } Console.WriteLine("WebSocket出现错误:" + e.Message); errCount++; } if (cancellationToken.IsCancellationRequested) { break; } await Task.Delay(delay, cancellationToken); } writer.TryComplete(); } private static PerformanceCounter GetCurrentPerformanceCounter() { double time = DateTime.Now.GetTotalMilliseconds(); // - 28800000; float load = SystemInfo.CpuLoad; double temperature = SystemInfo.GetCPUTemperature(); double mem = (1 - SystemInfo.MemoryAvailable.To() / SystemInfo.PhysicalMemory.To()) * 100; var read = SystemInfo.GetDiskData(DiskData.Read) / 1024; var write = SystemInfo.GetDiskData(DiskData.Write) / 1024; var up = SystemInfo.GetNetData(NetData.Received) / 1024; var down = SystemInfo.GetNetData(NetData.Sent) / 1024; var counter = new PerformanceCounter() { Time = time, CpuLoad = load, Temperature = temperature, MemoryUsage = mem, DiskRead = read, DiskWrite = write, Download = down, Upload = up }; return counter; } } }