minigame-tuanjie-transform-sdk/Runtime/WXSDKPerf/WXPerfEngine.cs

240 lines
8.0 KiB
C#
Raw Normal View History

2024-10-08 15:06:37 +08:00
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Xml;
using UnityEngine;
using UnityEngine.Scripting;
2025-01-07 20:59:25 +08:00
using System.IO;
2025-07-25 17:52:02 +08:00
using Unity.Profiling;
using UnityEngine.Profiling;
using Debug = UnityEngine.Debug;
2024-10-18 16:25:29 +08:00
#if PLATFORM_WEIXINMINIGAME || PLATFORM_WEBGL || UNITY_EDITOR
2024-10-21 10:40:01 +08:00
2024-10-08 15:06:37 +08:00
2024-10-18 16:25:29 +08:00
#if ENABLE_WX_PERF_FEATURE
2024-10-08 15:06:37 +08:00
namespace WXSDKPerf
{
[Preserve]
[ComVisible(false)]
public class WXPerfEngine
{
2025-01-07 20:59:25 +08:00
#if !UNITY_EDITOR
2025-07-25 17:52:02 +08:00
static WXPerfEngine_Implementation m_PerfEngineImplementation = null;
2025-01-07 20:59:25 +08:00
#endif
2024-10-08 15:06:37 +08:00
[RuntimeInitializeOnLoadMethod]
public static void StartWXPerfEngine()
{
#if UNITY_EDITOR
2025-07-25 17:52:02 +08:00
return;
2024-11-14 19:47:42 +08:00
#else
2024-10-08 15:06:37 +08:00
m_PerfEngineImplementation = new WXPerfEngine_Implementation();
2025-07-25 17:52:02 +08:00
m_PerfEngineImplementation.StartPerfEngine();
2024-11-14 19:47:42 +08:00
#endif
2024-10-08 15:06:37 +08:00
}
2025-01-07 20:59:25 +08:00
/// <summary>
/// This method is used to add an annotation to the performance data.
/// The annotation string is uploaded to the server along with the current frame ID.
/// </summary>
/// <param name="InAnnotationString">The annotation string to be added. It should not be null or empty.</param>
/// <remarks>
/// If the provided annotation string is null or empty, an error message will be logged.
/// </remarks>
2024-10-08 15:06:37 +08:00
public static void Annotation(string InAnnotationString)
{
#if UNITY_EDITOR
2025-07-25 17:52:02 +08:00
return;
2024-11-14 19:47:42 +08:00
#else
2025-03-20 19:30:11 +08:00
// Don't record annotation if we are not recording.
if (!IsRecording())
{
return;
}
2024-10-08 15:06:37 +08:00
if (m_PerfEngineImplementation == null)
{
UnityEngine.Debug.LogError("Annotation: Invalid m_PerfEngineImplementation! ");
return;
}
2025-01-07 20:59:25 +08:00
if (InAnnotationString.Contains("CaptureUnityMemorySnapshot"))
{
TakeAndUploadUnityMemorySnapshot();
}
2024-10-08 15:06:37 +08:00
m_PerfEngineImplementation.Annotation(InAnnotationString);
2024-11-14 19:47:42 +08:00
#endif
2024-10-08 15:06:37 +08:00
}
2025-07-25 17:52:02 +08:00
2025-01-07 20:59:25 +08:00
/// <summary>
/// 检查是否正在录制性能数据
/// </summary>
/// <returns>如果正在录制返回true否则返回false</returns>
public static bool IsRecording()
{
#if UNITY_EDITOR
return false;
#else
return m_PerfEngineImplementation != null && m_PerfEngineImplementation.IsRecording();
#endif
}
private static void TakeAndUploadUnityMemorySnapshot()
{
#if UNITY_EDITOR
return;
#else
DateTime timestamp = DateTime.Now;
var dateString = timestamp.ToLocalTime().ToString("yyyy-MM-dd_HH-mm-ss", System.Globalization.CultureInfo.InvariantCulture);
2025-07-25 17:52:02 +08:00
var snapshotFileName = $"{dateString}.snap";
2025-01-07 20:59:25 +08:00
#if UNITY_2018_3_OR_NEWER && !UNITY_2022_2_OR_NEWER
2025-07-25 17:52:02 +08:00
UnityEngine.Profiling.Memory.Experimental.MemoryProfiler.TakeSnapshot(Path.Combine(Application.persistentDataPath, snapshotFileName),
2025-01-07 20:59:25 +08:00
WXPerfEngine_Implementation.CaptureSnapshotCallback, (UnityEngine.Profiling.Memory.Experimental.CaptureFlags)31);
2025-07-25 17:52:02 +08:00
2025-01-07 20:59:25 +08:00
#elif UNITY_2022_2_OR_NEWER
2025-07-25 17:52:02 +08:00
Unity.Profiling.Memory.MemoryProfiler.TakeSnapshot(Path.Combine(Application.persistentDataPath, snapshotFileName),
2025-01-07 20:59:25 +08:00
WXPerfEngine_Implementation.CaptureSnapshotCallback, (Unity.Profiling.Memory.CaptureFlags)31);
#endif
#endif
}
/// <summary>
/// 指定luaState
/// </summary>
/// <param name="L">luaState</param>
public static void SetLuaState(IntPtr L)
{
#if UNITY_EDITOR
2025-07-25 17:52:02 +08:00
return;
2025-01-07 20:59:25 +08:00
#else
if (m_PerfEngineImplementation == null)
{
UnityEngine.Debug.LogError("SetLuaState: WXPerfEngine Not Started yet! Please Call WXSDKPerf.StartWXPerfEngine first! ");
return;
}
m_PerfEngineImplementation.SetLuaState(L);
#endif
}
2025-07-25 17:52:02 +08:00
2025-01-07 20:59:25 +08:00
/// <summary>
/// 声明自定义性能指标
/// </summary>
/// <param name="inStatName">性能指标名称</param>
/// <param name="inStatCategory">性能指标类别</param>
/// <param name="inStatInterpType">性能指标展示方式0. 不插值. 1. 线性插值2. Step插值</param>
public static void DeclareCustomStatInfo(string inStatName, string inStatCategory, int inStatInterpType = 1)
{
#if UNITY_EDITOR
2025-07-25 17:52:02 +08:00
return;
2025-01-07 20:59:25 +08:00
#else
if (m_PerfEngineImplementation == null)
{
UnityEngine.Debug.LogError("DeclareCustomStatInfo: Invalid m_PerfEngineImplementation! ");
return;
}
m_PerfEngineImplementation.DeclareCustomStatInfo(inStatName, inStatCategory, inStatInterpType);
#endif
}
/// <summary>
/// 设置自定义性能指标,目前只支持浮点数
/// 若该指标未通过DeclareCustomStatInfo进行类别的声明则将被归为默认自定义类别以及使用默认线性插值
/// </summary>
/// <param name="inStatName">性能指标名称</param>
/// <param name="inValue">性能指标数值</param>
public static void SetCustomStatValue(string inStatName, float inValue)
{
#if UNITY_EDITOR
2025-07-25 17:52:02 +08:00
return;
2025-01-07 20:59:25 +08:00
#else
if (m_PerfEngineImplementation == null)
{
UnityEngine.Debug.LogError("SetCustomStatInfo: Invalid m_PerfEngineImplementation! ");
return;
}
m_PerfEngineImplementation.SetCustomStatInfo(inStatName, inValue);
#endif
}
/// 在自定义性能指标值的基础上增加一段数值。
/// 如果未进行指标声明将自动声明该指标该指标将出现在报告的“Project Default Stat Category”中
/// </summary>
/// <param name="inStatName">性能指标名称</param>
/// <param name="inValue">性能指标数值</param>
public static void AddCustomStatInfoBy(string inStatName, float inValue)
{
#if UNITY_EDITOR
2025-07-25 17:52:02 +08:00
return;
2025-01-07 20:59:25 +08:00
#else
if (m_PerfEngineImplementation == null)
{
UnityEngine.Debug.LogError("AddCustomStatInfoBy: Invalid m_PerfEngineImplementation! ");
return;
}
2025-07-25 17:52:02 +08:00
m_PerfEngineImplementation.AddCustomStatInfoBy(inStatName, inValue);
2025-01-07 20:59:25 +08:00
#endif
2025-07-25 17:52:02 +08:00
2025-01-07 20:59:25 +08:00
}
/// <summary>
/// 手动开始记录
/// </summary>
/// <param name="inEnableStackTrace">是否启用堆栈跟踪</param>
/// <param name="inEnableStatInfo">是否启用统计信息</param>
/// <param name="inFrequentScreenShot">是否频繁截图</param>
/// <param name="inEnablebRenderInst">是否记录渲染指令</param>
/// <param name="inEnableCaptureResource">是否启用资源捕获</param>
/// <param name="inEnableLuaMemoryMonitor">是否启用Lua内存监控</param>
/// <param name="inEnableLuaFunctionMemoryTracking">是否启用Lua函数内存跟踪</param>
2025-07-25 17:52:02 +08:00
public static void StartRecordManually(bool inEnableStackTrace, bool inEnableStatInfo, bool inFrequentScreenShot, bool inEnablebRenderInst,
2025-01-07 20:59:25 +08:00
bool inEnableCaptureResource, bool inEnableLuaMemoryMonitor, bool inEnableLuaFunctionMemoryTracking)
{
#if UNITY_EDITOR
2025-07-25 17:52:02 +08:00
return;
2025-01-07 20:59:25 +08:00
#else
if (m_PerfEngineImplementation == null)
{
UnityEngine.Debug.LogError("StartRecordManually: Invalid m_PerfEngineImplementation! ");
return;
}
2025-07-25 17:52:02 +08:00
m_PerfEngineImplementation.StartRecordManually(inEnableStackTrace, inEnableStatInfo, inFrequentScreenShot, inEnablebRenderInst,
2025-01-07 20:59:25 +08:00
inEnableCaptureResource, inEnableLuaMemoryMonitor, inEnableLuaFunctionMemoryTracking);
#endif
}
/// <summary>
/// 手动停止记录
/// </summary>
public static void StopRecordManually()
{
#if UNITY_EDITOR
2025-07-25 17:52:02 +08:00
return;
2025-01-07 20:59:25 +08:00
#else
if (m_PerfEngineImplementation == null)
{
UnityEngine.Debug.LogError("StartRecordManually: Invalid m_PerfEngineImplementation! ");
return;
}
m_PerfEngineImplementation.StopRecordManually();
#endif
}
}
2024-10-08 15:06:37 +08:00
}
2024-10-18 16:25:29 +08:00
#endif
2024-10-08 15:06:37 +08:00
#endif // ENABLE_WX_PERF_FEATURE