Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.0KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using UnityEngine;
  5. using Svelto.Ticker.Legacy;
  6. //This profiler is based on the Entitas Visual Debugging tool
  7. //https://github.com/sschmid/Entitas-CSharp
  8. namespace Svelto.ECS.Profiler
  9. {
  10. public sealed class EngineProfiler
  11. {
  12. static readonly Stopwatch _stopwatch = new Stopwatch();
  13. public static void MonitorAddDuration(Action<INodeEngine<INode>, INode> addingFunc, INodeEngine<INode> engine, INode node)
  14. {
  15. EngineInfo info;
  16. if (engineInfos.TryGetValue(engine.GetType(), out info))
  17. {
  18. _stopwatch.Reset();
  19. _stopwatch.Start();
  20. addingFunc(engine, node);
  21. _stopwatch.Stop();
  22. info.AddAddDuration(_stopwatch.Elapsed.TotalMilliseconds);
  23. }
  24. }
  25. public static void MonitorRemoveDuration(Action<INodeEngine<INode>, INode> removeFunc, INodeEngine<INode> engine, INode node)
  26. {
  27. EngineInfo info;
  28. if (engineInfos.TryGetValue(engine.GetType(), out info))
  29. {
  30. _stopwatch.Reset();
  31. _stopwatch.Start();
  32. removeFunc(engine, node);
  33. engine.Remove(node);
  34. _stopwatch.Stop();
  35. info.AddRemoveDuration(_stopwatch.Elapsed.TotalMilliseconds);
  36. }
  37. }
  38. public static void MonitorUpdateDuration(ITickable tickable)
  39. {
  40. if (tickable is INodeEngine<INode>)
  41. {
  42. EngineInfo info;
  43. if (engineInfos.TryGetValue((tickable as INodeEngine<INode>).GetType(), out info))
  44. {
  45. _stopwatch.Reset();
  46. _stopwatch.Start();
  47. tickable.Tick(Time.deltaTime);
  48. _stopwatch.Stop();
  49. info.AddUpdateDuration(_stopwatch.Elapsed.TotalMilliseconds);
  50. }
  51. }
  52. else
  53. {
  54. tickable.Tick(Time.deltaTime);
  55. }
  56. }
  57. public static void MonitorUpdateDuration(IPhysicallyTickable tickable)
  58. {
  59. if (tickable is INodeEngine<INode>)
  60. {
  61. EngineInfo info;
  62. if (engineInfos.TryGetValue((tickable as INodeEngine<INode>).GetType(), out info))
  63. {
  64. _stopwatch.Reset();
  65. _stopwatch.Start();
  66. tickable.PhysicsTick(Time.fixedDeltaTime);
  67. _stopwatch.Stop();
  68. info.AddFixedUpdateDuration(_stopwatch.Elapsed.TotalMilliseconds);
  69. }
  70. }
  71. else
  72. {
  73. tickable.PhysicsTick(Time.fixedDeltaTime);
  74. }
  75. }
  76. public static void MonitorUpdateDuration(ILateTickable tickable)
  77. {
  78. if (tickable is INodeEngine<INode>)
  79. {
  80. EngineInfo info;
  81. if (engineInfos.TryGetValue((tickable as INodeEngine<INode>).GetType(), out info))
  82. {
  83. _stopwatch.Reset();
  84. _stopwatch.Start();
  85. tickable.LateTick(Time.deltaTime);
  86. _stopwatch.Stop();
  87. info.AddLateUpdateDuration(_stopwatch.Elapsed.TotalMilliseconds);
  88. }
  89. }
  90. else
  91. {
  92. tickable.LateTick(Time.deltaTime);
  93. }
  94. }
  95. public static void AddEngine(IEngine engine)
  96. {
  97. if (engineInfos.ContainsKey(engine.GetType()) == false)
  98. {
  99. engineInfos.Add(engine.GetType(), new EngineInfo(engine));
  100. }
  101. }
  102. public static void ResetDurations()
  103. {
  104. foreach (var engine in engineInfos)
  105. {
  106. engine.Value.ResetDurations();
  107. }
  108. }
  109. public static readonly Dictionary<Type, EngineInfo> engineInfos = new Dictionary<Type, EngineInfo>();
  110. }
  111. }