/// <summary> /// 开始计数 /// </summary> /// <returns></returns> public void End(RouteData data) { data.End = DateTime.Now; try { var tm = (DateTime.Now - Start).TotalMilliseconds; if (tm > 200) { LogRecorder.Warning($"{data.HostName}/{data.ApiName}:执行时间异常({tm:F2}ms):"); } if (tm > AppConfig.Config.SystemConfig.WaringTime) { RuntimeWaring.Waring(data.HostName, data.ApiName, $"执行时间异常({tm:F0}ms)"); } long unit = DateTime.Today.Year * 1000000 + DateTime.Today.Month * 10000 + DateTime.Today.Day * 100 + DateTime.Now.Hour; if (unit != Unit) { Unit = unit; Save(); Station = new CountItem(); } Station.SetValue(tm, data); if (string.IsNullOrWhiteSpace(data.HostName)) { return; } CountItem host; lock (Station) { if (!Station.Items.TryGetValue(data.HostName, out host)) { Station.Items.Add(data.HostName, host = new CountItem()); } } host.SetValue(tm, data); if (string.IsNullOrWhiteSpace(data.ApiName)) { return; } CountItem api; lock (host) { if (!host.Items.TryGetValue(data.ApiName, out api)) { host.Items.Add(data.ApiName, api = new CountItem()); } } api.SetValue(tm, data); } catch (Exception e) { LogRecorder.Exception(e); } }
void RebuildItems(CountItem item) { if (item.Items == null) { item.Items = new System.Collections.Generic.Dictionary <string, CountItem>(); } if (item.Children == null) { item.Children = new System.Collections.Generic.List <CountItem>(); } else { foreach (var child in item.Children) { RebuildItems(child); item.Items.Add(item.Id, item); } } }
/// <summary> /// 保存为性能日志 /// </summary> void Load() { var file = Path.Combine(ZeroApplication.Config.DataFolder, "ApiCount.json"); if (!File.Exists(file)) { return; } try { var json = File.ReadAllText(file); if (!string.IsNullOrWhiteSpace(json)) { _root = JsonConvert.DeserializeObject <CountItem>(json) ?? new CountItem(); } } catch (Exception exception) { Console.WriteLine(exception); } }
/// <summary> /// 开始计数 /// </summary> /// <returns></returns> private void Handle(CountData data) { if (data == null || string.IsNullOrWhiteSpace(data.HostName) || data.End == 0) { return; } try { var tm = (data.End - data.Start) / 10; Root.SetValue(tm, data); if (!Root.Items.TryGetValue(data.Machine, out var machine)) { Root.Items.Add(data.Machine, machine = new CountItem { Id = data.Machine, Label = data.Machine }); Root.Children.Add(machine); } else { machine.SetValue(tm, data); } var stationName = data.Station ?? data.HostName; if (!machine.Items.TryGetValue(stationName, out var station)) { machine.Items.Add(stationName, station = new CountItem { Id = $"/{data.Machine}/{stationName}", Label = stationName }); machine.Children.Add(station); } else { station.SetValue(tm, data); } var tn = data.IsInner ? "api" : "call"; if (!station.Items.TryGetValue(tn, out var tag)) { station.Items.Add(tn, tag = new CountItem { Id = $"{station.Id}/{tn}", Label = tn }); station.Children.Add(tag); } else { tag.SetValue(tm, data); } if (!tag.Items.TryGetValue(data.HostName, out var host)) { var config = ZeroApplication.Config[data.HostName]; tag.Items.Add(data.HostName, host = new CountItem { Id = $"{tag.Id}/{data.HostName}", Label = $"{data.HostName}{(config == null ? "?" : "")}" }); tag.Children.Add(host); } else { host.SetValue(tm, data); } if (!host.Items.TryGetValue(data.ApiName, out var api)) { host.Items.Add(data.ApiName, api = new CountItem { Id = $"{host.Id}/{data.ApiName}", Label = data.ApiName }); host.Children.Add(api); } else { api.SetValue(tm, data); } } catch (Exception e) { LogRecorder.Exception(e); } }