using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpTest.Helpers;
/// <summary>
/// 耗时统计帮助类
/// </summary>
public class StopWatchHelper
{
    /// <summary>
    /// 创建新实例
    /// </summary>
    /// <param name="sign">当前耗时统计的标识,默认为空字符串,可用于区分不同耗时统计</param>
    /// <param name="enable">是否启用调试实例,默认true,如果设置为false 则不启用调试实例,无任何耗时统计代码执行</param>
    /// <returns></returns>
    public static StopWatchHelper CreateNew(string sign = "", bool enable = true) => new StopWatchHelper(sign, enable);
    /// <summary>
    /// 当前耗时统计的标识 
    /// </summary>
    private string _sign = "";
    /// <summary>
    /// 是否输出日志
    /// </summary>
    private bool _enable = true;
    /// <summary>
    /// 获取消息模板
    /// </summary>
    /// <param name="sign"></param>
    /// <param name="tms"></param>
    /// <param name="ms"></param>
    /// <param name="message"></param>
    /// <returns></returns>
    private string GetMessageTemplate(string sign, long tms, long ms, string message = "") 
        => $"[STOPWATCH]-[{sign}]-[total cost time:{tms}ms]-[from last cost time:{ms}ms]:[{message}]\r\n";
    /// <summary>
    /// 创建新实例
    /// </summary>
    /// <param name="sign">当前耗时统计的标识,默认为空字符串,可用于区分不同耗时统计</param>
    /// <param name="enable">是否启用调试实例,默认true,如果设置为false 则不启用调试实例,无任何耗时统计代码执行</param>
    /// <returns></returns>
    private StopWatchHelper(string sign = "", bool enable = true)
    {
        _enable = enable;
        if (!enable)
        {
            return;
        }
        _sign = sign;
        StopwatchStep = Stopwatch.StartNew();
        StopwatchTotal = Stopwatch.StartNew();
        this.LogText = GetMessageTemplate(_sign, 0, 0, "START");
    }
    /// <summary>
    /// 两个锚点之间的耗时统计
    /// </summary>
    public Stopwatch StopwatchStep { get; set; }
    /// <summary>
    /// 整个调试流程间的耗时统计
    /// </summary>
    public Stopwatch StopwatchTotal { get; set; }
    /// <summary>
    /// 日志内容
    /// </summary>
    public string LogText { get; set; }
    /// <summary>
    /// 添加日志锚点
    /// </summary>
    /// <param name="message">锚点额外的消息</param>
    public void Ding(string message = "")
    {
        if (!_enable)
        {
            return;
        }
        LogText += GetMessageTemplate(_sign, StopwatchTotal.ElapsedMilliseconds, StopwatchStep.ElapsedMilliseconds, message);
        StopwatchStep.Restart();
    }
    /// <summary>
    /// 重置记录器
    /// </summary>
    public void Reset()
    {
        if (!_enable)
        {
            return;
        }
        StopwatchStep.Reset();
        StopwatchTotal.Reset();
    }
    /// <summary>
    /// 重启记录器
    /// </summary>
    public void Restart()
    {
        if (!_enable)
        {
            return;
        }
        StopwatchStep.Start();
        StopwatchTotal.Start();
        this.LogText = GetMessageTemplate(_sign, 0, 0, "START");
    }
    /// <summary>
    /// 输出统计结果,调用完后就结束了,想要再次统计需要调用方法 <see cref="Restart"/> 才行。
    /// </summary>
    /// <param name="whenGreatThanMs">当总耗时大于等于 whenGreatThanMs 时才输出日志,默认0,即总耗时大于等于0才输出日志</param>
    public void PrintResult(int whenGreatThanMs = 0)
    {
        if (!_enable)
        {
            return;
        }
        if (StopwatchTotal.ElapsedMilliseconds >= whenGreatThanMs)
        {
            LogText += GetMessageTemplate(_sign, StopwatchTotal.ElapsedMilliseconds, StopwatchStep.ElapsedMilliseconds, "END");
            Console.WriteLine(LogText);
        }
        this.Reset();
    }
}													CSharp-StopWatchHelper
							
								声明:本站所有文章除特别声明外,均采用CC BY-NC-SA 4.0许可协议。转载请注明来自
								染青的博客!
							
						
					相关文章
还没有评论, 告诉我们你的想法
				
	
            
发表评论