214 lines
6.8 KiB
C#
214 lines
6.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Protocol376Simulator.Interfaces;
|
|
|
|
namespace Protocol376Simulator.Services
|
|
{
|
|
/// <summary>
|
|
/// 统计服务类,用于收集和报告通信统计信息
|
|
/// </summary>
|
|
public class StatisticsService
|
|
{
|
|
private readonly string _deviceIdentifier;
|
|
private int _totalMessagesSent = 0;
|
|
private int _totalMessagesReceived = 0;
|
|
private int _failedMessages = 0;
|
|
private readonly Dictionary<MessageType, int> _messageTypeStats = new Dictionary<MessageType, int>();
|
|
private readonly List<TimeSpan> _responseTimes = new List<TimeSpan>();
|
|
private DateTime _lastMessageSentTime = DateTime.MinValue;
|
|
private readonly Dictionary<string, int> _errorStats = new Dictionary<string, int>();
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="deviceIdentifier">设备标识(用于日志和显示)</param>
|
|
public StatisticsService(string deviceIdentifier)
|
|
{
|
|
_deviceIdentifier = deviceIdentifier;
|
|
|
|
// 初始化消息类型统计
|
|
foreach (MessageType type in Enum.GetValues(typeof(MessageType)))
|
|
{
|
|
_messageTypeStats[type] = 0;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 总发送消息数
|
|
/// </summary>
|
|
public int TotalMessagesSent => _totalMessagesSent;
|
|
|
|
/// <summary>
|
|
/// 总接收消息数
|
|
/// </summary>
|
|
public int TotalMessagesReceived => _totalMessagesReceived;
|
|
|
|
/// <summary>
|
|
/// 失败消息数
|
|
/// </summary>
|
|
public int FailedMessages => _failedMessages;
|
|
|
|
/// <summary>
|
|
/// 消息类型统计
|
|
/// </summary>
|
|
public Dictionary<MessageType, int> MessageTypeStats => _messageTypeStats;
|
|
|
|
/// <summary>
|
|
/// 平均响应时间
|
|
/// </summary>
|
|
public TimeSpan AverageResponseTime => _responseTimes.Count > 0 ?
|
|
TimeSpan.FromMilliseconds(_responseTimes.Average(t => t.TotalMilliseconds)) :
|
|
TimeSpan.Zero;
|
|
|
|
/// <summary>
|
|
/// 最大响应时间
|
|
/// </summary>
|
|
public TimeSpan MaxResponseTime => _responseTimes.Count > 0 ?
|
|
_responseTimes.Max() : TimeSpan.Zero;
|
|
|
|
/// <summary>
|
|
/// 最小响应时间
|
|
/// </summary>
|
|
public TimeSpan MinResponseTime => _responseTimes.Count > 0 ?
|
|
_responseTimes.Min() : TimeSpan.Zero;
|
|
|
|
/// <summary>
|
|
/// 记录消息发送
|
|
/// </summary>
|
|
public void RecordMessageSent()
|
|
{
|
|
_totalMessagesSent++;
|
|
_lastMessageSentTime = DateTime.Now;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录消息接收
|
|
/// </summary>
|
|
public void RecordMessageReceived()
|
|
{
|
|
_totalMessagesReceived++;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录消息失败
|
|
/// </summary>
|
|
public void RecordMessageFailed()
|
|
{
|
|
_failedMessages++;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录消息类型统计
|
|
/// </summary>
|
|
/// <param name="messageType">消息类型</param>
|
|
public void RecordMessageType(MessageType messageType)
|
|
{
|
|
if (_messageTypeStats.ContainsKey(messageType))
|
|
{
|
|
_messageTypeStats[messageType]++;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录响应时间
|
|
/// </summary>
|
|
/// <param name="responseTime">响应时间</param>
|
|
public void RecordResponseTime(TimeSpan responseTime)
|
|
{
|
|
if (responseTime.TotalMilliseconds > 0)
|
|
{
|
|
_responseTimes.Add(responseTime);
|
|
|
|
// 保持响应时间列表在合理大小范围内,避免内存泄漏
|
|
if (_responseTimes.Count > 1000)
|
|
{
|
|
_responseTimes.RemoveAt(0);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录错误统计
|
|
/// </summary>
|
|
/// <param name="errorType">错误类型</param>
|
|
public void RecordError(string errorType)
|
|
{
|
|
if (string.IsNullOrEmpty(errorType))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (_errorStats.ContainsKey(errorType))
|
|
{
|
|
_errorStats[errorType]++;
|
|
}
|
|
else
|
|
{
|
|
_errorStats[errorType] = 1;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取统计信息的字符串表示
|
|
/// </summary>
|
|
/// <returns>格式化的统计信息</returns>
|
|
public string GetStatisticsReport()
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendLine($"设备 {_deviceIdentifier} 通信统计:");
|
|
sb.AppendLine($"总发送消息数: {_totalMessagesSent}");
|
|
sb.AppendLine($"总接收消息数: {_totalMessagesReceived}");
|
|
sb.AppendLine($"失败消息数: {_failedMessages}");
|
|
|
|
if (_responseTimes.Count > 0)
|
|
{
|
|
sb.AppendLine($"平均响应时间: {AverageResponseTime.TotalMilliseconds:F2}毫秒");
|
|
sb.AppendLine($"最大响应时间: {MaxResponseTime.TotalMilliseconds:F2}毫秒");
|
|
sb.AppendLine($"最小响应时间: {MinResponseTime.TotalMilliseconds:F2}毫秒");
|
|
}
|
|
else
|
|
{
|
|
sb.AppendLine("尚未记录响应时间");
|
|
}
|
|
|
|
sb.AppendLine("消息类型统计:");
|
|
foreach (var type in _messageTypeStats.Where(x => x.Value > 0).OrderByDescending(x => x.Value))
|
|
{
|
|
sb.AppendLine($" {type.Key}: {type.Value}");
|
|
}
|
|
|
|
if (_errorStats.Count > 0)
|
|
{
|
|
sb.AppendLine("错误统计:");
|
|
foreach (var error in _errorStats.OrderByDescending(x => x.Value))
|
|
{
|
|
sb.AppendLine($" {error.Key}: {error.Value}");
|
|
}
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重置所有统计数据
|
|
/// </summary>
|
|
public void ResetStatistics()
|
|
{
|
|
_totalMessagesSent = 0;
|
|
_totalMessagesReceived = 0;
|
|
_failedMessages = 0;
|
|
_responseTimes.Clear();
|
|
_lastMessageSentTime = DateTime.MinValue;
|
|
|
|
foreach (MessageType type in Enum.GetValues(typeof(MessageType)))
|
|
{
|
|
_messageTypeStats[type] = 0;
|
|
}
|
|
|
|
_errorStats.Clear();
|
|
}
|
|
}
|
|
} |