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.

64 lines
1.9KB

  1. #if asdDEBUG
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using Svelto.ECS.Internal;
  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, INodeWrapper> addingFunc, INodeEngine engine, INodeWrapper node)
  14. {
  15. EngineInfo info;
  16. if (engineInfos.TryGetValue(engine.GetType(), out info))
  17. {
  18. _stopwatch.Start();
  19. addingFunc(engine, node);
  20. _stopwatch.Reset();
  21. info.AddAddDuration(_stopwatch.Elapsed.TotalMilliseconds);
  22. }
  23. }
  24. public static void MonitorRemoveDuration(Action<INodeEngine, INodeWrapper> removeFunc, INodeEngine engine, INodeWrapper node)
  25. {
  26. EngineInfo info;
  27. if (engineInfos.TryGetValue(engine.GetType(), out info))
  28. {
  29. _stopwatch.Start();
  30. removeFunc(engine, node);
  31. // engine.Remove(node);
  32. _stopwatch.Reset();
  33. info.AddRemoveDuration(_stopwatch.Elapsed.TotalMilliseconds);
  34. }
  35. }
  36. public static void AddEngine(IEngine engine)
  37. {
  38. if (engineInfos.ContainsKey(engine.GetType()) == false)
  39. {
  40. engineInfos.Add(engine.GetType(), new EngineInfo(engine));
  41. }
  42. }
  43. public static void ResetDurations()
  44. {
  45. foreach (var engine in engineInfos)
  46. {
  47. engine.Value.ResetDurations();
  48. }
  49. }
  50. public static readonly Dictionary<Type, EngineInfo> engineInfos = new Dictionary<Type, EngineInfo>();
  51. }
  52. }
  53. #endif