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.

EngineProfiler.cs 1.9KB

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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<IHandleEntityViewEngine, IEntityView> addingFunc, IHandleEntityViewEngine engine, IEntityView entityView)
  13. {
  14. EngineInfo info;
  15. if (engineInfos.TryGetValue(engine.GetType(), out info))
  16. {
  17. _stopwatch.Start();
  18. addingFunc(engine, entityView);
  19. _stopwatch.Reset();
  20. info.AddAddDuration(_stopwatch.Elapsed.TotalMilliseconds);
  21. }
  22. }
  23. public static void MonitorRemoveDuration(Action<IHandleEntityViewEngine, IEntityView> removeFunc, IHandleEntityViewEngine engine, IEntityView entityView)
  24. {
  25. EngineInfo info;
  26. if (engineInfos.TryGetValue(engine.GetType(), out info))
  27. {
  28. _stopwatch.Start();
  29. removeFunc(engine, entityView);
  30. engine.Remove(entityView);
  31. _stopwatch.Reset();
  32. info.AddRemoveDuration(_stopwatch.Elapsed.TotalMilliseconds);
  33. }
  34. }
  35. public static void AddEngine(IEngine engine)
  36. {
  37. if (engineInfos.ContainsKey(engine.GetType()) == false)
  38. {
  39. engineInfos.Add(engine.GetType(), new EngineInfo(engine));
  40. }
  41. }
  42. public static void ResetDurations()
  43. {
  44. foreach (var engine in engineInfos)
  45. {
  46. engine.Value.ResetDurations();
  47. }
  48. }
  49. public static readonly Dictionary<Type, EngineInfo> engineInfos = new Dictionary<Type, EngineInfo>();
  50. }
  51. }