Skip to content
This repository was archived by the owner on Oct 20, 2021. It is now read-only.

Commit 1231e3e

Browse files
authored
UTY-1376: added metric send system (#13)
1 parent 4ddf27d commit 1231e3e

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
## Unreleased
44

5+
### Added
6+
7+
- Added a metric sending system for load, FPS and Unity heap usage.
8+
59
### Fixed
10+
611
- Fixed an issue with the default snapshot that prevented the player lifecycle module from spawning a player.
712

813
## `0.1.2` - 2018-11-02
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}

workers/unity/Assets/Scripts/MetricSendSystem.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workers/unity/Assets/Scripts/Workers/UnityGameLogicConnector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ private async void Start()
2020

2121
protected override void HandleWorkerConnectionEstablished()
2222
{
23+
Worker.World.GetOrCreateManager<MetricSendSystem>();
2324
PlayerLifecycleHelper.AddServerSystems(Worker.World);
2425
}
2526

0 commit comments

Comments
 (0)