FPSLogger.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections.Generic;
  2. using Logging.Base;
  3. using Logging.Data;
  4. using UnityEngine;
  5. namespace Logging
  6. {
  7. public readonly struct FPSData : ISerializable
  8. {
  9. private readonly long timestamp;
  10. private readonly int fps;
  11. public FPSData(long timestamp, int fps)
  12. {
  13. this.timestamp = timestamp;
  14. this.fps = fps;
  15. }
  16. public KeyValuePair<long, string[]> Serialize() => new KeyValuePair<long, string[]>(timestamp, new[]
  17. {
  18. fps.ToString()
  19. });
  20. }
  21. public class FPSLogger : SensorDataLogger<FPSData>
  22. {
  23. public override string Key => "fps";
  24. const float FPSMeasurePeriod = 0.5f;
  25. private int fpsAccumulator = 0;
  26. private float fpsNextPeriod = 0;
  27. private int currentFps;
  28. public override void Start()
  29. {
  30. base.Start();
  31. fpsNextPeriod = Time.realtimeSinceStartup + FPSMeasurePeriod;
  32. }
  33. private void Update()
  34. {
  35. fpsAccumulator++;
  36. if (Time.realtimeSinceStartup > fpsNextPeriod)
  37. {
  38. currentFps = (int) (fpsAccumulator / FPSMeasurePeriod);
  39. fpsAccumulator = 0;
  40. fpsNextPeriod += FPSMeasurePeriod;
  41. }
  42. Debug.Log($"Current FPS = {currentFps}");
  43. Log(new FPSData(Helpers.RoundToLong(Time.time * 1000f), currentFps));
  44. }
  45. public override IEnumerable<FPSData> ReadLog(IEnumerable<IEnumerable<string>> lines)
  46. {
  47. throw new System.NotImplementedException();
  48. }
  49. }
  50. }