FPSLogger.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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() => new KeyValuePair<long, string[]>(timestamp, new[]
  18. {
  19. fps.ToString()
  20. });
  21. }
  22. public class FPSLogger : SensorDataLogger<FPSData>
  23. {
  24. public override string Key => "fps";
  25. private float lastTs = 0f;
  26. public float checkInterval = 1f;
  27. private int currentFps = -1;
  28. private int frameCount = 0;
  29. public override void Start()
  30. {
  31. base.Start();
  32. lastTs = Time.time;
  33. }
  34. private void Update()
  35. {
  36. var dif = Time.time - lastTs;
  37. frameCount++;
  38. if (dif >= checkInterval)
  39. {
  40. currentFps = (int) (frameCount / dif);
  41. lastTs = Time.time;
  42. frameCount = 0;
  43. }
  44. Log(new FPSData(Helpers.RoundToLong(Time.time * 1000f), currentFps));
  45. }
  46. public override IEnumerable<FPSData> ReadLog(IEnumerable<IEnumerable<string>> lines)
  47. {
  48. throw new NotImplementedException();
  49. }
  50. }
  51. }