1
0

FPSLogger.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 readonly struct FPSData : ISerializable
  9. {
  10. private readonly long timestamp;
  11. private readonly int fps;
  12. public FPSData(long timestamp, int fps)
  13. {
  14. this.timestamp = timestamp;
  15. this.fps = fps;
  16. }
  17. public KeyValuePair<long, string[]> Serialize()
  18. {
  19. return new KeyValuePair<long, string[]>(timestamp, new[]
  20. {
  21. fps.ToString()
  22. });
  23. }
  24. }
  25. public class FPSLogger : SensorDataLogger<FPSData>
  26. {
  27. private const float FPSMeasurePeriod = 0.5f;
  28. private int currentFps;
  29. private int fpsAccumulator;
  30. private float fpsNextPeriod;
  31. public override string Key => "fps";
  32. public override void Start()
  33. {
  34. base.Start();
  35. fpsNextPeriod = Time.realtimeSinceStartup + FPSMeasurePeriod;
  36. }
  37. private void Update()
  38. {
  39. fpsAccumulator++;
  40. if (Time.realtimeSinceStartup > fpsNextPeriod)
  41. {
  42. currentFps = (int) (fpsAccumulator / FPSMeasurePeriod);
  43. fpsAccumulator = 0;
  44. fpsNextPeriod += FPSMeasurePeriod;
  45. }
  46. //Debug.Log($"Current FPS = {currentFps}");
  47. Log(new FPSData(Helpers.RoundToLong(Time.time * 1000f), currentFps));
  48. }
  49. public override IEnumerable<FPSData> ReadLog(IEnumerable<IEnumerable<string>> lines)
  50. {
  51. throw new NotImplementedException();
  52. }
  53. }
  54. }