//--------------------------------------------------
// Motion Framework
// Copyright©2021-2021 何冠峰
// Licensed under the MIT license
//--------------------------------------------------
using System;
using System.Text;
using System.Diagnostics;
namespace MotionFramework.Utility
{
public static class ProfilerUtility
{
private static string _watchName;
private static long _limitMilliseconds;
private static Stopwatch _watch;
///
/// 开启性能测试
///
/// 测试名称
/// 极限毫秒数
[Conditional("DEBUG")]
public static void BeginWatch(string name, long limitMilliseconds = long.MaxValue)
{
if (_watch != null)
{
UnityEngine.Debug.LogError($"Last watch is not end : {_watchName}");
}
_watchName = name;
_limitMilliseconds = limitMilliseconds;
_watch = new Stopwatch();
_watch.Start();
}
///
/// 结束性能测试
/// 说明:当耗费的毫秒数超过极限值则输出为警告
///
[Conditional("DEBUG")]
public static void EndWatch()
{
if (_watch != null)
{
_watch.Stop();
string logInfo = $"[Profiler] {_watchName} took {_watch.ElapsedMilliseconds} ms";
if (_watch.ElapsedMilliseconds > _limitMilliseconds)
UnityEngine.Debug.LogWarning(logInfo);
else
UnityEngine.Debug.Log(logInfo);
_watch = null;
}
}
}
}