Skip to content

Commit

Permalink
Fixed: "SystemRoot.GetPerfLog() throws ArgumentOutOfRange when system…
Browse files Browse the repository at this point in the history
… names are too long"

#55
Length of system name column can now be customized. Default: 30
  • Loading branch information
friflo committed Jan 26, 2025
1 parent e1f2cfd commit 2ed91b6
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 18 deletions.
37 changes: 27 additions & 10 deletions src/ECS/Systems/BaseSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,29 +254,41 @@ private static void AddSubSystems(ref ReadOnlyList<BaseSystem> readOnlyList, Bas
#endregion

#region perf
private const int DefaultNameColLen = 30;

/// <summary>
/// Returns performance statistics formatted as a table intended for logging.
/// </summary>
public string GetPerfLog()
public string GetPerfLog(int nameColLen = 0)
{
nameColLen = nameColLen <= 0 ? DefaultNameColLen : nameColLen;
var sb = stringBuffer ??= new StringBuilder();
sb.Clear();
AppendPerfLog (sb);
AppendPerfLog (sb, nameColLen);
return sb.ToString();
}

/// <summary>
/// Add performance statistics formatted as a table to the given <see cref="StringBuilder"/> without memory allocations.
/// </summary>
public void AppendPerfLog(StringBuilder stringBuilder) {
public void AppendPerfLog(StringBuilder sb, int nameColLen = 0)
{
nameColLen = nameColLen <= 0 ? DefaultNameColLen : nameColLen;
var stores = SystemRoot?.stores.count ?? 0;
stringBuilder.Append($"stores: {stores,-3} E M last ms sum ms updates last mem sum mem entities\n");
stringBuilder.Append("--------------------- --- -------- -------- -------- -------- -------- --------\n");
AppendPerfStats(stringBuilder, 0);
stringBuilder.Replace(',', '.'); // no more patience with NumberFormatInfo
var start = sb.Length;
sb.Append($"stores: {stores,-3} ");
var len = nameColLen - (sb.Length - start);
if (len > 0) {
sb.Append(' ', len);
}
sb.Append("E M last ms sum ms updates last mem sum mem entities\n");
sb.Append('-', nameColLen - 1);
sb.Append(" --- -------- -------- -------- -------- -------- --------\n");
AppendPerfStats(sb, 0, nameColLen);
sb.Replace(',', '.'); // no more patience with NumberFormatInfo
}

internal virtual void AppendPerfStats(StringBuilder sb, int depth)
internal virtual void AppendPerfStats(StringBuilder sb, int depth, int nameColLen)
{
var start = sb.Length;
if (depth > 0) {
Expand All @@ -297,8 +309,13 @@ internal virtual void AppendPerfStats(StringBuilder sb, int depth)
} else {
monitored = parentGroup?.MonitorPerf ?? false;
}
var len = 30 - (sb.Length - start);
sb.Append(' ', len);
var len = nameColLen - (sb.Length - start) - 1; // -1 for additional ' '
if (len > 0) {
sb.Append(' ', len);
} else {
sb.Length += len; // truncate if too long
}
sb.Append(' ');
sb.Append(enabled ? "+ " : "- ");
sb.Append(monitored ? "m" : " ");
sb.Append($" {(double)Perf.LastMs,12:0.000}"); // (double) prevents allocation
Expand Down
6 changes: 3 additions & 3 deletions src/ECS/Systems/SystemGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,11 @@ private void ClearPerfTicks(BaseSystem system)
}
}

internal override void AppendPerfStats(StringBuilder sb, int depth)
internal override void AppendPerfStats(StringBuilder sb, int depth, int nameColLen)
{
base.AppendPerfStats(sb, depth);
base.AppendPerfStats(sb, depth, nameColLen);
foreach (var child in childSystems) {
child.AppendPerfStats(sb, depth + 1);
child.AppendPerfStats(sb, depth + 1, nameColLen);
}
}
#endregion
Expand Down
2 changes: 1 addition & 1 deletion src/Tests-internal/ECS/Test_Systems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static void Test_Systems_AllSystems()
var log = sb.ToString();
AreEqual(
@"stores: 0 E M last ms sum ms updates last mem sum mem entities
--------------------- --- -------- -------- -------- -------- -------- --------
----------------------------- --- -------- -------- -------- -------- -------- --------
Systems [1] + m -1.000 0.000 0 0 0
| Update [2] + m -1.000 0.000 0 0 0
| TestSystem1 + m -1.000 0.000 0 0 0 0
Expand Down
5 changes: 5 additions & 0 deletions src/Tests/ECS/Systems/Systems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,9 @@ protected override void OnUpdateGroup() {
}
}
}

public class TestSystem_long_____123456789_123456789_ : QuerySystem<Position>
{
protected override void OnUpdate() { }
}
}
2 changes: 1 addition & 1 deletion src/Tests/ECS/Systems/Test_SystemGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public static void Test_SystemGroup_Perf()
var emptyLog = perfSystem1.GetPerfLog();
AreEqual(
@"stores: 0 E M last ms sum ms updates last mem sum mem entities
--------------------- --- -------- -------- -------- -------- -------- --------
----------------------------- --- -------- -------- -------- -------- -------- --------
PerfSystem + -1.000 0.000 0 0 0
", emptyLog);

Expand Down
44 changes: 41 additions & 3 deletions src/Tests/ECS/Systems/Test_SystemRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static void Test_SystemRoot_Add_System()
Console.WriteLine(root.GetPerfLog());
AreEqual(
@"stores: 1 E M last ms sum ms updates last mem sum mem entities
--------------------- --- -------- -------- -------- -------- -------- --------
----------------------------- --- -------- -------- -------- -------- -------- --------
Systems [1] + -1.000 0.000 0 0 0
| Update [1] + -1.000 0.000 0 0 0
| TestSystem1 + -1.000 0.000 0 0 0 1
Expand All @@ -124,15 +124,15 @@ Systems [1] + -1.000 0.000 0
Console.WriteLine(root.GetPerfLog());
AreEqual(
@"stores: 1 E M last ms sum ms updates last mem sum mem entities
--------------------- --- -------- -------- -------- -------- -------- --------
----------------------------- --- -------- -------- -------- -------- -------- --------
Systems [1] + m -1.000 0.000 0 0 0
| Update [1] + m -1.000 0.000 0 0 0
| TestSystem1 + m -1.000 0.000 0 0 0 1
", root.GetPerfLog());

AreEqual(
@"stores: 1 E M last ms sum ms updates last mem sum mem entities
--------------------- --- -------- -------- -------- -------- -------- --------
----------------------------- --- -------- -------- -------- -------- -------- --------
TestSystem1 + m -1.000 0.000 0 0 0 1
", testSystem1.GetPerfLog());

Expand All @@ -145,6 +145,44 @@ Systems [1] + m -1.000 0.000 0
AreEqual(42, root.Tick.deltaTime);
}

[Test]
public static void Test_SystemRoot_PerfLog_truncate_long_names()
{
var store = new EntityStore();
var root = new SystemRoot(store, "SystemRoot_long_____123456789_123456789_");
var testGroup = new SystemGroup("SystemGroup_long____123456789_123456789_");
root.Add(testGroup);
var testSystem1 = new TestSystem_long_____123456789_123456789_();
testGroup.Add(testSystem1);
AreEqual("TestSystem_long_____123456789_123456789_ - [Position]", testSystem1.ToString());
Console.WriteLine(root.GetPerfLog());
AreEqual(
@"stores: 1 E M last ms sum ms updates last mem sum mem entities
----------------------------- --- -------- -------- -------- -------- -------- --------
SystemRoot_long_____123456789 + -1.000 0.000 0 0 0
| SystemGroup_long____1234567 + -1.000 0.000 0 0 0
| TestSystem_long_____12345 + -1.000 0.000 0 0 0 0
", root.GetPerfLog());

Console.WriteLine(root.GetPerfLog(50));
AreEqual(
@"stores: 1 E M last ms sum ms updates last mem sum mem entities
------------------------------------------------- --- -------- -------- -------- -------- -------- --------
SystemRoot_long_____123456789_123456789_ [1] + -1.000 0.000 0 0 0
| SystemGroup_long____123456789_123456789_ [1] + -1.000 0.000 0 0 0
| TestSystem_long_____123456789_123456789_ + -1.000 0.000 0 0 0 0
", root.GetPerfLog(50));

Console.WriteLine(root.GetPerfLog(1));
AreEqual(
@"stores: 1 E M last ms sum ms updates last mem sum mem entities
--- -------- -------- -------- -------- -------- --------
+ -1.000 0.000 0 0 0
+ -1.000 0.000 0 0 0
+ -1.000 0.000 0 0 0 0
", root.GetPerfLog(1));
}

[Test]
public static void Test_SystemRoot_Add_RemoveStore()
{
Expand Down

0 comments on commit 2ed91b6

Please sign in to comment.