FPSLogger.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using Logging.Base;
  4. using Logging.Data;
  5. using UnityEngine;
  6. namespace Logging
  7. {
  8. public enum RouteEvent
  9. {
  10. None,
  11. Left,
  12. Right,
  13. Straight,
  14. Start,
  15. Finish
  16. }
  17. public readonly struct FPSData : ISerializable
  18. {
  19. private readonly long timestamp;
  20. private readonly int fps;
  21. public FPSData(long timestamp, int fps)
  22. {
  23. this.timestamp = timestamp;
  24. this.fps = fps;
  25. }
  26. public KeyValuePair<long, string[]> Serialize() => new KeyValuePair<long, string[]>(timestamp, new[]
  27. {
  28. fps.ToString()
  29. });
  30. }
  31. public class FPSLogger : SensorDataLogger<FPSData>
  32. {
  33. public override string Key => "route_events";
  34. private float lastTs = 0f;
  35. public float checkInterval = 1f;
  36. private int currentFps = -1;
  37. private int frameCount = 0;
  38. public override void Start()
  39. {
  40. base.Start();
  41. lastTs = Time.time;
  42. }
  43. private void Update()
  44. {
  45. var dif = Time.time - lastTs;
  46. frameCount++;
  47. if (dif >= checkInterval)
  48. {
  49. currentFps = (int) (frameCount / dif);
  50. lastTs = Time.time;
  51. frameCount = 0;
  52. }
  53. Log(new FPSData(Helpers.RoundToLong(Time.time * 1000f), currentFps));
  54. }
  55. public override IEnumerable<FPSData> ReadLog(IEnumerable<IEnumerable<string>> lines)
  56. {
  57. throw new NotImplementedException();
  58. }
  59. }
  60. }