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.

63 lines
1.9KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using Svelto.ECS.Internal;
  5. //This profiler is based on the Entitas Visual Debugging tool
  6. //https://github.com/sschmid/Entitas-CSharp
  7. namespace Svelto.ECS.Profiler
  8. {
  9. public sealed class EngineProfiler
  10. {
  11. static readonly Stopwatch _stopwatch = new Stopwatch();
  12. public static void MonitorAddDuration(Action<INodeEngine, INode> addingFunc, INodeEngine engine, INode node)
  13. {
  14. EngineInfo info;
  15. if (engineInfos.TryGetValue(engine.GetType(), out info))
  16. {
  17. _stopwatch.Reset();
  18. _stopwatch.Start();
  19. addingFunc(engine, node);
  20. _stopwatch.Stop();
  21. info.AddAddDuration(_stopwatch.Elapsed.TotalMilliseconds);
  22. }
  23. }
  24. public static void MonitorRemoveDuration(Action<INodeEngine, INode> removeFunc, INodeEngine engine, INode node)
  25. {
  26. EngineInfo info;
  27. if (engineInfos.TryGetValue(engine.GetType(), out info))
  28. {
  29. _stopwatch.Reset();
  30. _stopwatch.Start();
  31. removeFunc(engine, node);
  32. engine.Remove(node);
  33. _stopwatch.Stop();
  34. info.AddRemoveDuration(_stopwatch.Elapsed.TotalMilliseconds);
  35. }
  36. }
  37. public static void AddEngine(IEngine engine)
  38. {
  39. if (engineInfos.ContainsKey(engine.GetType()) == false)
  40. {
  41. engineInfos.Add(engine.GetType(), new EngineInfo(engine));
  42. }
  43. }
  44. public static void ResetDurations()
  45. {
  46. foreach (var engine in engineInfos)
  47. {
  48. engine.Value.ResetDurations();
  49. }
  50. }
  51. public static readonly Dictionary<Type, EngineInfo> engineInfos = new Dictionary<Type, EngineInfo>();
  52. }
  53. }