|
| 1 | +using System; |
| 2 | +using Improbable.Gdk.Core; |
| 3 | +using Improbable.Worker; |
| 4 | +using Improbable.Worker.Core; |
| 5 | +using Unity.Entities; |
| 6 | +using UnityEngine; |
| 7 | + |
| 8 | +namespace BlankProject |
| 9 | +{ |
| 10 | + public class MetricSendSystem : ComponentSystem |
| 11 | + { |
| 12 | + private Connection connection; |
| 13 | + |
| 14 | + private DateTime timeOfNextUpdate; |
| 15 | + private DateTime timeOfLastUpdate; |
| 16 | + |
| 17 | + private const double TimeBetweenMetricUpdatesSecs = 2; |
| 18 | + private const int DefaultTargetFrameRate = 60; |
| 19 | + |
| 20 | + private double targetFps; |
| 21 | + |
| 22 | + private int lastFrameCount; |
| 23 | + private double calculatedFps; |
| 24 | + |
| 25 | + // We use exponential smoothing for the FPS metric |
| 26 | + // larger value == more smoothing, 0 = no smoothing |
| 27 | + // 0 <= smoothing < 1 |
| 28 | + private const double smoothing = 0; |
| 29 | + |
| 30 | + private static readonly Metrics WorkerMetrics = new Metrics(); |
| 31 | + |
| 32 | + protected override void OnCreateManager() |
| 33 | + { |
| 34 | + base.OnCreateManager(); |
| 35 | + connection = World.GetExistingManager<WorkerSystem>().Connection; |
| 36 | + |
| 37 | + targetFps = Application.targetFrameRate == -1 |
| 38 | + ? DefaultTargetFrameRate |
| 39 | + : Application.targetFrameRate; |
| 40 | + |
| 41 | + lastFrameCount = Time.frameCount; |
| 42 | + calculatedFps = targetFps; |
| 43 | + |
| 44 | + timeOfLastUpdate = DateTime.Now; |
| 45 | + timeOfNextUpdate = timeOfLastUpdate.AddSeconds(TimeBetweenMetricUpdatesSecs); |
| 46 | + } |
| 47 | + |
| 48 | + protected override void OnUpdate() |
| 49 | + { |
| 50 | + if (connection == null) |
| 51 | + { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + if (DateTime.Now >= timeOfNextUpdate) |
| 56 | + { |
| 57 | + CalculateFps(); |
| 58 | + WorkerMetrics.GaugeMetrics["Dynamic.FPS"] = calculatedFps; |
| 59 | + WorkerMetrics.GaugeMetrics["Unity used heap size"] = GC.GetTotalMemory(false); |
| 60 | + WorkerMetrics.Load = CalculateLoad(); |
| 61 | + |
| 62 | + connection.SendMetrics(WorkerMetrics); |
| 63 | + |
| 64 | + timeOfLastUpdate = DateTime.Now; |
| 65 | + timeOfNextUpdate = timeOfLastUpdate.AddSeconds(TimeBetweenMetricUpdatesSecs); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Load defined as performance relative to target FPS. |
| 70 | + // i.e. a load of 0.5 means that the worker is hitting the target FPS |
| 71 | + // but achieving less than half the target FPS takes load above 1.0 |
| 72 | + private double CalculateLoad() |
| 73 | + { |
| 74 | + return Math.Max(0.0d, 0.5d * targetFps / calculatedFps); |
| 75 | + } |
| 76 | + |
| 77 | + private void CalculateFps() |
| 78 | + { |
| 79 | + var frameCount = Time.frameCount - lastFrameCount; |
| 80 | + lastFrameCount = Time.frameCount; |
| 81 | + var rawFps = frameCount / (DateTime.Now - timeOfLastUpdate).TotalSeconds; |
| 82 | + calculatedFps = (rawFps * (1 - smoothing)) + (calculatedFps * smoothing); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments