コード整理。デバッグ用の時間計測を分離

This commit is contained in:
ousttrue
2021-02-22 14:39:30 +09:00
parent 59129af006
commit 0f85964a50
5 changed files with 91 additions and 80 deletions

View File

@@ -12,76 +12,11 @@ using UnityEditor;
namespace UniGLTF
{
/// <summary>
/// GLTF importer
/// </summary>
public class ImporterContext : IDisposable
{
#region MeasureTime
bool m_showSpeedLog
#if VRM_DEVELOP
= true
#endif
;
public bool ShowSpeedLog
{
set { m_showSpeedLog = value; }
}
public struct KeyElapsed
{
public string Key;
public TimeSpan Elapsed;
public KeyElapsed(string key, TimeSpan elapsed)
{
Key = key;
Elapsed = elapsed;
}
}
public struct MeasureScope : IDisposable
{
Action m_onDispose;
public MeasureScope(Action onDispose)
{
m_onDispose = onDispose;
}
public void Dispose()
{
m_onDispose();
}
}
public List<KeyElapsed> m_speedReports = new List<KeyElapsed>();
public IDisposable MeasureTime(string key)
{
var sw = System.Diagnostics.Stopwatch.StartNew();
return new MeasureScope(() =>
{
m_speedReports.Add(new KeyElapsed(key, sw.Elapsed));
});
}
public string GetSpeedLog()
{
var total = TimeSpan.Zero;
var sb = new StringBuilder();
sb.AppendLine("【SpeedLog】");
foreach (var kv in m_speedReports)
{
sb.AppendLine(string.Format("{0}: {1}ms", kv.Key, (int)kv.Elapsed.TotalMilliseconds));
total += kv.Elapsed;
}
sb.AppendLine(string.Format("total: {0}ms", (int)total.TotalMilliseconds));
return sb.ToString();
}
#endregion
#region Animation
protected IAnimationImporter m_animationImporter;
public void SetAnimationImporter(IAnimationImporter animationImporter)
@@ -355,11 +290,16 @@ namespace UniGLTF
public bool EnableLoadBalancing;
public virtual async Awaitable LoadAsync()
public virtual async Awaitable LoadAsync(Func<string, IDisposable> MeasureTime = null)
{
if (MeasureTime == null)
{
MeasureTime = new ImporterContextSpeedLog().MeasureTime;
}
if (Root == null)
{
Root = new GameObject("_root_");
Root = new GameObject("GLTF");
}
// UniGLTF does not support draco
@@ -379,7 +319,7 @@ namespace UniGLTF
using (MeasureTime("ReadMesh"))
{
var x = meshImporter.ReadMesh(this, index);
var y = await BuildMeshAsync(x, index);
var y = await BuildMeshAsync(MeasureTime, x, index);
Meshes.Add(y);
}
}
@@ -423,21 +363,15 @@ namespace UniGLTF
AnimationImporter.Import(this);
}
await OnLoadModel();
if (m_showSpeedLog)
{
Debug.Log(GetSpeedLog());
}
await OnLoadModel(MeasureTime);
}
protected virtual async Awaitable OnLoadModel()
protected virtual async Awaitable OnLoadModel(Func<string, IDisposable> MeasureTime)
{
Root.name = "GLTF";
await LoopAwaitable.Create();
// do nothing
}
async Awaitable<MeshWithMaterials> BuildMeshAsync(MeshImporter.MeshContext x, int i)
async Awaitable<MeshWithMaterials> BuildMeshAsync(Func<string, IDisposable> MeasureTime, MeshImporter.MeshContext x, int i)
{
using (MeasureTime("BuildMesh"))
{

View File

@@ -1,5 +1,6 @@
using System.IO;
using UniGLTF.AltTask;
using UnityEngine;
namespace UniGLTF
{
@@ -32,9 +33,10 @@ namespace UniGLTF
/// </summary>
public static void Load(this ImporterContext self)
{
var meassureTime = new ImporterContextSpeedLog();
using (var queue = TaskQueue.Create())
{
var task = self.LoadAsync();
var task = self.LoadAsync(meassureTime.MeasureTime);
// 中断された await を消化する
while (!task.IsCompleted)
@@ -43,6 +45,10 @@ namespace UniGLTF
queue.ExecuteOneCallback();
}
}
#if VRM_DEVELOP
Debug.Log(meassureTime.GetSpeedLog());
#endif
}
}
}

View File

@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace UniGLTF
{
public class ImporterContextSpeedLog
{
public struct KeyElapsed
{
public string Key;
public TimeSpan Elapsed;
public KeyElapsed(string key, TimeSpan elapsed)
{
Key = key;
Elapsed = elapsed;
}
}
public struct MeasureScope : IDisposable
{
Action m_onDispose;
public MeasureScope(Action onDispose)
{
m_onDispose = onDispose;
}
public void Dispose()
{
m_onDispose();
}
}
public List<KeyElapsed> m_speedReports = new List<KeyElapsed>();
public IDisposable MeasureTime(string key)
{
var sw = System.Diagnostics.Stopwatch.StartNew();
return new MeasureScope(() =>
{
m_speedReports.Add(new KeyElapsed(key, sw.Elapsed));
});
}
public string GetSpeedLog()
{
var total = TimeSpan.Zero;
var sb = new StringBuilder();
sb.AppendLine("【SpeedLog】");
foreach (var kv in m_speedReports)
{
sb.AppendLine(string.Format("{0}: {1}ms", kv.Key, (int)kv.Elapsed.TotalMilliseconds));
total += kv.Elapsed;
}
sb.AppendLine(string.Format("total: {0}ms", (int)total.TotalMilliseconds));
return sb.ToString();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e99bb16028e30ce4b972caecca5343b1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -53,7 +53,7 @@ namespace VRM
}
#region OnLoad
protected override async Awaitable OnLoadModel()
protected override async Awaitable OnLoadModel(Func<string, IDisposable> MeasureTime)
{
Root.name = "VRM";