using System.Text; namespace SHH.MjpegPlayer { /// /// 按时间统计 /// public class SumByTime { #region Defines /// 最近刷新在哪一秒 private int LastRefreshSecond = DateTime.Now.Second; /// 最近刷新在哪一分钟 private int LastRefreshMinute = DateTime.Now.Minute; /// 最近刷新在哪一小时 private int LastRefreshHour = DateTime.Now.Minute; /// 秒统计 private Dictionary _second = new Dictionary(); /// 分钟统计 private Dictionary _minute = new Dictionary(); /// 小时统计 private Dictionary _hour = new Dictionary(); /// 累计统计 public Dictionary All { get; init; } = new Dictionary(); #endregion #region TotalSecond /// 秒统计 public Dictionary TotalSecond { get; init; } = new Dictionary(); #endregion #region TotalMinute /// 分统计 public Dictionary TotalMinute { get; init; } = new Dictionary(); #endregion #region TotalHour /// 小时统计 public Dictionary TotalHour { get; init; } = new Dictionary(); #endregion #region Refresh /// /// 刷新方法调用次数 /// /// /// /// public void Refresh(string methodName, uint count = 1) { try { #region 加入集合 // 加入集合 lock (_second) { if (!_second.ContainsKey(methodName)) _second.Add(methodName, 0); } // 加入集合 lock (_minute) { if (!_minute.ContainsKey(methodName)) _minute.Add(methodName, 0); } lock (_hour) { if (!_hour.ContainsKey(methodName)) _hour.Add(methodName, 0); } // 加入集合 lock (All) { if (!All.ContainsKey(methodName)) All.Add(methodName, 0); } #endregion #region 时间变更统计 // 秒刷新 if (!LastRefreshSecond.Equals(DateTime.Now.Second)) { LastRefreshSecond = DateTime.Now.Second; var sb = new StringBuilder(); foreach (var de in _second) { // 更新输出用统计信息 if (!TotalSecond.ContainsKey(de.Key)) TotalSecond.Add(de.Key, de.Value); else TotalSecond[de.Key] = de.Value; sb.Append($"\r\n\t{de.Key} => 执行 {de.Value} 次"); _second[de.Key] = 0; } var logMsg = $"统计 => SumBySecond 统计时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm")}{sb.ToString()}"; //Logs.LogInformation(EIdSys.TotalBySecond, logMsg); } // 分钟刷新 if (!LastRefreshMinute.Equals(DateTime.Now.Minute)) { LastRefreshMinute = DateTime.Now.Minute; var sb = new StringBuilder(); foreach (var de in _minute) { // 更新输出用统计信息 if (!TotalMinute.ContainsKey(de.Key)) TotalMinute.Add(de.Key, de.Value); else TotalMinute[de.Key] = de.Value; sb.Append($"\r\n\t{de.Key} => 执行 {de.Value} 次, 平均每秒 {Math.Round((double)de.Value / 60, 2)} 次"); _minute[de.Key] = 0; } var logMsg = $"统计 => SumByMinute 统计时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm")}{sb.ToString()}"; //Logs.LogInformation(EIdSys.TotalByMinute, logMsg); } // 小时刷新 if (!LastRefreshHour.Equals(DateTime.Now.Hour)) { LastRefreshHour = DateTime.Now.Hour; var sb = new StringBuilder(); foreach (var de in _hour) { // 更新输出用统计信息 if (!TotalHour.ContainsKey(de.Key)) TotalHour.Add(de.Key, de.Value); else TotalHour[de.Key] = de.Value; sb.Append($"\r\n\t{de.Key} => 执行 {de.Value} 次, 平均每秒 {Math.Round((double)de.Value / 60, 2)} 次"); _hour[de.Key] = 0; } var logMsg = $"统计 => SumByHour 统计时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm")}{sb.ToString()}"; //Logs.LogInformation(EIdSys.TotalByHour, logMsg); } #endregion #region 数值更新 _second[methodName] += count; _minute[methodName] += count; _hour[methodName] += count; All[methodName] += count; #endregion } catch (Exception ex) { //Logs.LogWarning(ex.Message); } } #endregion } }