Skip to content

Commit

Permalink
Add SpawnEquipDeleteBenchmark (#19566)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectroJr authored Sep 2, 2023
1 parent 946958f commit 182b526
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Content.Benchmarks/DeviceNetworkingBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class DeviceNetworkingBenchmark
public async Task SetupAsync()
{
ProgramShared.PathOffset = "../../../../";
PoolManager.Startup(typeof(DeviceNetworkingBenchmark).Assembly);
_pair = await PoolManager.GetServerClient();
var server = _pair.Server;

Expand Down Expand Up @@ -91,6 +92,7 @@ await server.WaitPost(() =>
public async Task Cleanup()
{
await _pair.DisposeAsync();
PoolManager.Shutdown();
}

[Benchmark(Baseline = true, Description = "Entity Events")]
Expand Down
2 changes: 2 additions & 0 deletions Content.Benchmarks/MapLoadBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class MapLoadBenchmark
public void Setup()
{
ProgramShared.PathOffset = "../../../../";
PoolManager.Startup(null);

_pair = PoolManager.GetServerClient().GetAwaiter().GetResult();
var server = _pair.Server;
Expand All @@ -42,6 +43,7 @@ public void Setup()
public async Task Cleanup()
{
await _pair.DisposeAsync();
PoolManager.Shutdown();
}

public static IEnumerable<string> MapsSource { get; set; }
Expand Down
8 changes: 5 additions & 3 deletions Content.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
using Content.IntegrationTests;
using Content.Server.Maps;
#if DEBUG
using BenchmarkDotNet.Configs;
#else
using Robust.Benchmarks.Configs;
#endif
using Robust.Shared.Prototypes;

namespace Content.Benchmarks
Expand All @@ -25,6 +28,7 @@ public static async Task MainAsync(string[] args)
var gameMaps = pair.Server.ResolveDependency<IPrototypeManager>().EnumeratePrototypes<GameMapPrototype>().ToList();
MapLoadBenchmark.MapsSource = gameMaps.Select(x => x.ID);
await pair.CleanReturnAsync();
PoolManager.Shutdown();

#if DEBUG
Console.ForegroundColor = ConsoleColor.Red;
Expand All @@ -35,8 +39,6 @@ public static async Task MainAsync(string[] args)
var config = Environment.GetEnvironmentVariable("ROBUST_BENCHMARKS_ENABLE_SQL") != null ? DefaultSQLConfig.Instance : null;
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
#endif

PoolManager.Shutdown();
}
}
}
66 changes: 66 additions & 0 deletions Content.Benchmarks/SpawnEquipDeleteBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Content.IntegrationTests;
using Content.IntegrationTests.Pair;
using Content.Server.Station.Systems;
using Content.Shared.Roles;
using Robust.Shared;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;

namespace Content.Benchmarks;

/// <summary>
/// This benchmarks spawns several humans, gives them captain equipment and then deletes them.
/// This measures performance for spawning, deletion, containers, and inventory code.
/// </summary>
[Virtual, MemoryDiagnoser]
public class SpawnEquipDeleteBenchmark
{
private TestPair _pair = default!;
private StationSpawningSystem _spawnSys = default!;
private const string Mob = "MobHuman";
private StartingGearPrototype _gear = default!;
private EntityUid _entity;
private EntityCoordinates _coords;

[Params(1, 4, 16, 64)]
public int N;

[GlobalSetup]
public async Task SetupAsync()
{
ProgramShared.PathOffset = "../../../../";
PoolManager.Startup(null);
_pair = await PoolManager.GetServerClient();
var server = _pair.Server;

var mapData = await _pair.CreateTestMap();
_coords = mapData.GridCoords;
_spawnSys = server.System<StationSpawningSystem>();
_gear = server.ProtoMan.Index<StartingGearPrototype>("CaptainGear");
}

[GlobalCleanup]
public async Task Cleanup()
{
await _pair.DisposeAsync();
PoolManager.Shutdown();
}

[Benchmark]
public async Task SpawnDeletePlayer()
{
await _pair.Server.WaitPost(() =>
{
var server = _pair.Server;
for (var i = 0; i < N; i++)
{
_entity = server.EntMan.SpawnAttachedTo(Mob, _coords);
_spawnSys.EquipStartingGear(_entity, _gear, null);
server.EntMan.DeleteEntity(_entity);
}
});
}
}
6 changes: 5 additions & 1 deletion Content.IntegrationTests/Pair/TestPair.Prototypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ private async Task LoadPrototypes(RobustIntegrationTest.IntegrationInstance inst
instance.ProtoMan.LoadString(file, changed: changed);
}

await instance.WaitPost(() => instance.ProtoMan.ReloadPrototypes(changed));
await instance.WaitPost(() =>
{
instance.ProtoMan.ResolveResults();
instance.ProtoMan.ReloadPrototypes(changed);
});

foreach (var (kind, ids) in changed)
{
Expand Down

0 comments on commit 182b526

Please sign in to comment.