Skip to content

Commit

Permalink
Add PerfMonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
VirxEC committed Aug 10, 2024
1 parent 433cec6 commit 13ecbe8
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 11 deletions.
23 changes: 13 additions & 10 deletions RLBotCS/ManagerTools/MatchStarter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,23 +295,26 @@ private void SpawnCars(MatchSettingsT matchSettings)
switch (playerConfig.Variety.Type)
{
case PlayerClass.RLBot:
_expectedConnections++;

if (!doSpawning)
break;

Logger.LogInformation(
"Spawning player "
+ playerConfig.Name
+ " with spawn id "
+ playerConfig.SpawnId
);
_expectedConnections++;

if (doSpawning)
bridge.TryWrite(
new SpawnBot(
playerConfig,
BotSkill.Custom,
(uint)(i - indexOffset),
true
)
);
bridge.TryWrite(
new SpawnBot(
playerConfig,
BotSkill.Custom,
(uint)(i - indexOffset),
true
)
);

break;
case PlayerClass.Psyonix when doSpawning:
Expand Down
96 changes: 96 additions & 0 deletions RLBotCS/ManagerTools/PerfMonitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using Bridge.State;
using rlbot.flat;

namespace RLBotCS.ManagerTools;

// https://github.com/RLBotPrivate/RLBot/blob/psyonix/src/main/cpp/RLBotInterface/src/RLBot/Performance/PerformanceMonitor.cpp
public class PerfMonitor
{
private const int ClientId = 0;
private const int RenderGroupId = 1;
private const int _maxSamples = 120;
private const int _tickSkip = 60;

private static readonly ColorT TextColor = new ColorT()
{
A = 255,
R = 255,
G = 255,
B = 255,
};
private static readonly ColorT BackColor = new ColorT()
{
A = 100,
R = 0,
G = 0,
B = 0,
};

private readonly SortedDictionary<string, LinkedList<bool>> _samples = new();
private int tick = 0;

public void AddSample(string name, bool gotInput)
{
if (!_samples.ContainsKey(name))
{
_samples[name] = new();
}

_samples[name].AddLast(gotInput);

if (_samples[name].Count > _maxSamples)
{
_samples[name].RemoveFirst();
}
}

public void RemoveBot(string name)
{
_samples.Remove(name);
}

public void RenderSummary(Rendering rendering, GameState gameState)
{
tick = (tick + 1) % _tickSkip;
if (tick != 0)
return;

string message = "RLBot";
bool shouldRender = false;

foreach (var (name, samples) in _samples)
{
int gotInputCount = samples.Count(sample => sample);
float gotInputPercentage = (float)gotInputCount / samples.Count;

message += $"\n{name}: {gotInputPercentage * 100:0.0}%";

if (gotInputPercentage < 0.999)
shouldRender = true;
}

var renderText = new String2DT()
{
Y = 10f / 1920f,
X = 200f / 1080f,
Text = message,
Foreground = TextColor,
Background = BackColor,
Scale = 1,
HAlign = TextHAlign.Left,
VAlign = TextVAlign.Top,
};

var renderMessages = new List<RenderMessageT>()
{
new RenderMessageT() { Variety = RenderTypeUnion.FromString2D(renderText), },
};

if (shouldRender)
rendering.AddRenderGroup(ClientId, RenderGroupId, renderMessages, gameState);
else
rendering.RemoveRenderGroup(ClientId, RenderGroupId);
}

public void ClearAll() => _samples.Clear();
}
1 change: 1 addition & 0 deletions RLBotCS/Server/BridgeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ TcpMessenger messenger
public PlayerInputSender PlayerInputSender { get; } = new(messenger);
public Rendering RenderingMgmt { get; } = new(messenger);
public QuickChat QuickChat { get; } = new();
public PerfMonitor PerfMonitor { get; } = new();

public bool GotFirstMessage { get; set; }
public bool MatchHasStarted { get; set; }
Expand Down
17 changes: 16 additions & 1 deletion RLBotCS/Server/BridgeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,27 @@ private async Task HandleServer()
if (!_context.LastMatchEnded)
{
_context.QuickChat.ClearChats();
_context.PerfMonitor.ClearAll();
_context.RenderingMgmt.ClearAllRenders();
_context.Writer.TryWrite(new StopMatch(false));
}
}
else
else if (
_context.GameState.GameStateType != GameStateType.Replay
&& _context.GameState.GameStateType != GameStateType.Paused
)
{
if (
_context.GameState.GameStateType != GameStateType.GoalScored
&& _context.GameState.GameStateType != GameStateType.Ended
)
_context.PerfMonitor.RenderSummary(
_context.RenderingMgmt,
_context.GameState
);
else
_context.PerfMonitor.ClearAll();

_context.QuickChat.RenderChats(_context.RenderingMgmt, _context.GameState);
}
_context.LastMatchEnded = matchEnded;
Expand Down
19 changes: 19 additions & 0 deletions RLBotCS/Server/BridgeMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,22 @@ internal record ShowQuickChat(MatchCommT MatchComm) : IBridgeMessage
public void HandleMessage(BridgeContext context) =>
context.QuickChat.AddChat(MatchComm, context.GameState.SecondsElapsed);
}

internal record AddPerfSample(int SpawnId, bool GotInput) : IBridgeMessage
{
public void HandleMessage(BridgeContext context)
{
var player = context
.GameState.PlayerMapping.GetKnownPlayers()
.FirstOrDefault(p => p.SpawnId == SpawnId);
if (player is null)
return;

uint? index = context.GameState.PlayerMapping.PlayerIndexFromActorId(player.ActorId);
if (index is null)
return;

string name = context.GameState.GameCars[(uint)index].Name;
context.PerfMonitor.AddSample(name, GotInput);
}
}
6 changes: 6 additions & 0 deletions RLBotCS/Server/FlatBuffersSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ internal class FlatBuffersSession
private bool _renderingIsEnabled;

private int _spawnId;
private bool _gotInput;
private bool _sessionForceClosed;
private bool _closed;

Expand Down Expand Up @@ -138,6 +139,8 @@ private async Task<bool> ParseClientMessage(TypedPayload message)

case DataType.PlayerInput:
var playerInputMsg = PlayerInput.GetRootAsPlayerInput(byteBuffer).UnPack();
_gotInput = true;

await _bridge.WriteAsync(new Input(playerInputMsg));
break;

Expand Down Expand Up @@ -265,6 +268,9 @@ await SendPayloadToClientAsync(
_messageBuilder
)
);

await _bridge.WriteAsync(new AddPerfSample(_spawnId, _gotInput));
_gotInput = false;
break;
case SessionMessage.RendersAllowed m:
_renderingIsEnabled = m.Allowed;
Expand Down
1 change: 1 addition & 0 deletions RLBotCSTests/FlatbufferTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static List<ScriptConfigurationT> RandomScriptConfigurations()
scriptConfigurations.Add(
new ScriptConfigurationT()
{
Name = RandomString(64),
Location = RandomString(64),
RunCommand = RandomString(64),
}
Expand Down
8 changes: 8 additions & 0 deletions RLBotCSTests/PlayerMappingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ public void TestSpawnProcess()
var metadata2 = _playerMapping.ApplyCarSpawn(
new CarSpawn() { ActorId = 111, CommandId = 222, }
);
uint? index = _playerMapping.PlayerIndexFromActorId(111);

Assert.AreEqual(0u, _playerMapping.PlayerIndexFromActorId(111));
Assert.IsNotNull(index);
Assert.AreEqual(index, 0u);
Assert.AreEqual(desiredIndex, _playerMapping.PlayerIndexFromActorId(actorId));
Assert.IsTrue(!metadata2.IsBot);
Assert.IsTrue(!metadata2.IsCustomBot);

uint? index2 = _playerMapping.PlayerIndexFromActorId(456);

Assert.IsNull(index2);
Assert.AreNotEqual(index2, 0u);
}
}

0 comments on commit 13ecbe8

Please sign in to comment.