Skip to content

Commit

Permalink
Merge pull request #1712 from space-syndicate/upstream-sync
Browse files Browse the repository at this point in the history
Upstream sync
  • Loading branch information
Morb0 authored Jan 4, 2024
2 parents 7f43383 + e5049ec commit 369e651
Show file tree
Hide file tree
Showing 1,042 changed files with 868,342 additions and 855,409 deletions.
2 changes: 1 addition & 1 deletion Content.Benchmarks/MapLoadBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task Cleanup()
PoolManager.Shutdown();
}

public static IEnumerable<string> MapsSource { get; set; }
public static readonly string[] MapsSource = { "Empty", "Box", "Aspid", "Bagel", "Dev", "CentComm", "Atlas", "Core", "TestTeg", "Saltern", "Packed", "Omega", "Cluster", "Gemini", "Reach", "Origin", "Meta", "Marathon", "Europa", "MeteorArena", "Fland", "Barratry" };

[ParamsSource(nameof(MapsSource))]
public string Map;
Expand Down
7 changes: 0 additions & 7 deletions Content.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ public static void Main(string[] args)

public static async Task MainAsync(string[] args)
{
PoolManager.Startup(typeof(Program).Assembly);
var pair = await PoolManager.GetServerClient();
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;
Console.WriteLine("\nWARNING: YOU ARE RUNNING A DEBUG BUILD, USE A RELEASE BUILD FOR AN ACCURATE BENCHMARK");
Expand Down
187 changes: 187 additions & 0 deletions Content.Benchmarks/PvsBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using Content.IntegrationTests;
using Content.IntegrationTests.Pair;
using Content.Server.Warps;
using Robust.Server.GameObjects;
using Robust.Shared;
using Robust.Shared.Analyzers;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Player;
using Robust.Shared.Random;

namespace Content.Benchmarks;

// This benchmark probably benefits from some accidental cache locality. I,e. the order in which entities in a pvs
// chunk are sent to players matches the order in which the entities were spawned.
//
// in a real mid-late game round, this is probably no longer the case.
// One way to somewhat offset this is to update the NetEntity assignment to assign random (but still unique) NetEntity uids to entities.
// This makes the benchmark run noticeably slower.

[Virtual]
public class PvsBenchmark
{
public const string Map = "Maps/box.yml";

[Params(1, 8, 80)]
public int PlayerCount { get; set; }

private TestPair _pair = default!;
private IEntityManager _entMan = default!;
private MapId _mapId = new(10);
private ICommonSession[] _players = default!;
private EntityCoordinates[] _spawns = default!;
public int _cycleOffset = 0;
private SharedTransformSystem _sys = default!;
private EntityCoordinates[] _locations = default!;

[GlobalSetup]
public void Setup()
{
#if !DEBUG
ProgramShared.PathOffset = "../../../../";
#endif
PoolManager.Startup(null);

_pair = PoolManager.GetServerClient().GetAwaiter().GetResult();
_entMan = _pair.Server.ResolveDependency<IEntityManager>();
_pair.Server.CfgMan.SetCVar(CVars.NetPVS, true);
_pair.Server.CfgMan.SetCVar(CVars.ThreadParallelCount, 0);
_pair.Server.CfgMan.SetCVar(CVars.NetPvsAsync, false);
_sys = _entMan.System<SharedTransformSystem>();

// Spawn the map
_pair.Server.ResolveDependency<IRobustRandom>().SetSeed(42);
_pair.Server.WaitPost(() =>
{
var success = _entMan.System<MapLoaderSystem>().TryLoad(_mapId, Map, out _);
if (!success)
throw new Exception("Map load failed");
_pair.Server.MapMan.DoMapInitialize(_mapId);
}).Wait();

// Get list of ghost warp positions
_spawns = _entMan.AllComponentsList<WarpPointComponent>()
.OrderBy(x => x.Component.Location)
.Select(x => _entMan.GetComponent<TransformComponent>(x.Uid).Coordinates)
.ToArray();

Array.Resize(ref _players, PlayerCount);

// Spawn "Players".
_pair.Server.WaitPost(() =>
{
for (var i = 0; i < PlayerCount; i++)
{
var pos = _spawns[i % _spawns.Length];
var uid =_entMan.SpawnEntity("MobHuman", pos);
_pair.Server.ConsoleHost.ExecuteCommand($"setoutfit {_entMan.GetNetEntity(uid)} CaptainGear");
_players[i] = new DummySession{AttachedEntity = uid};
}
}).Wait();

// Repeatedly move players around so that they "explore" the map and see lots of entities.
// This will populate their PVS data with out-of-view entities.
var rng = new Random(42);
ShufflePlayers(rng, 100);

_pair.Server.PvsTick(_players);
_pair.Server.PvsTick(_players);

var ents = _players.Select(x => x.AttachedEntity!.Value).ToArray();
_locations = ents.Select(x => _entMan.GetComponent<TransformComponent>(x).Coordinates).ToArray();
}

private void ShufflePlayers(Random rng, int count)
{
while (count > 0)
{
ShufflePlayers(rng);
count--;
}
}

private void ShufflePlayers(Random rng)
{
_pair.Server.PvsTick(_players);

var ents = _players.Select(x => x.AttachedEntity!.Value).ToArray();
var locations = ents.Select(x => _entMan.GetComponent<TransformComponent>(x).Coordinates).ToArray();

// Shuffle locations
var n = locations.Length;
while (n > 1)
{
n -= 1;
var k = rng.Next(n + 1);
(locations[k], locations[n]) = (locations[n], locations[k]);
}

_pair.Server.WaitPost(() =>
{
for (var i = 0; i < PlayerCount; i++)
{
_sys.SetCoordinates(ents[i], locations[i]);
}
}).Wait();

_pair.Server.PvsTick(_players);
}

/// <summary>
/// Basic benchmark for PVS in a static situation where nothing moves or gets dirtied..
/// This effectively provides a lower bound on "real" pvs tick time, as it is missing:
/// - PVS chunks getting dirtied and needing to be rebuilt
/// - Fetching component states for dirty components
/// - Compressing & sending network messages
/// - Sending PVS leave messages
/// </summary>
[Benchmark]
public void StaticTick()
{
_pair.Server.PvsTick(_players);
}

/// <summary>
/// Basic benchmark for PVS in a situation where players are teleporting all over the place. This isn't very
/// realistic, but unlike <see cref="StaticTick"/> this will actually also measure the speed of processing dirty
/// chunks and sending PVS leave messages.
/// </summary>
[Benchmark]
public void CycleTick()
{
_cycleOffset = (_cycleOffset + 1) % _players.Length;
_pair.Server.WaitPost(() =>
{
for (var i = 0; i < PlayerCount; i++)
{
_sys.SetCoordinates(_players[i].AttachedEntity!.Value, _locations[(i + _cycleOffset) % _players.Length]);
}
}).Wait();
_pair.Server.PvsTick(_players);
}

private sealed class DummySession : ICommonSession
{
public SessionStatus Status => SessionStatus.InGame;
public EntityUid? AttachedEntity {get; set; }
public NetUserId UserId => default;
public string Name => string.Empty;
public short Ping => default;
public INetChannel Channel { get; set; } = default!;
public LoginType AuthType => default;
public HashSet<EntityUid> ViewSubscriptions { get; } = new();
public DateTime ConnectedTime { get; set; }
public SessionState State => default!;
public SessionData Data => default!;
public bool ClientSide { get; set; }
}
}
10 changes: 10 additions & 0 deletions Content.Client/Administration/UI/Bwoink/BwoinkControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
using Content.Client.Administration.UI.CustomControls;
using Content.Client.UserInterface.Systems.Bwoink;
using Content.Shared.Administration;
using Content.Shared.CCVar;
using Robust.Client.AutoGenerated;
using Robust.Client.Console;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Network;
using Robust.Shared.Utility;
using Robust.Shared.Timing;
using Robust.Shared.Configuration;

namespace Content.Client.Administration.UI.Bwoink
{
Expand All @@ -23,6 +26,7 @@ public sealed partial class BwoinkControl : Control
[Dependency] private readonly IClientAdminManager _adminManager = default!;
[Dependency] private readonly IClientConsoleHost _console = default!;
[Dependency] private readonly IUserInterfaceManager _ui = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
public AdminAHelpUIHandler AHelpHelper = default!;

private PlayerInfo? _currentPlayer;
Expand Down Expand Up @@ -71,6 +75,9 @@ public BwoinkControl()
if (info.Antag && info.ActiveThisRound)
sb.Append(new Rune(0x1F5E1)); // 🗡

if (info.OverallPlaytime <= TimeSpan.FromSeconds(_cfg.GetCVar(CCVars.NewPlayerThreshold)))
sb.Append(new Rune(0x23F2)); // ⏲

sb.AppendFormat("\"{0}\"", text);

return sb.ToString();
Expand Down Expand Up @@ -219,6 +226,9 @@ private string FormatTabTitle(ItemList.Item li, PlayerInfo? pl = default)
if (pl.Antag)
sb.Append(new Rune(0x1F5E1)); // 🗡

if (pl.OverallPlaytime <= TimeSpan.FromSeconds(_cfg.GetCVar(CCVars.NewPlayerThreshold)))
sb.Append(new Rune(0x23F2)); // ⏲

sb.AppendFormat("\"{0}\"", pl.CharacterName);

if (pl.IdentityName != pl.CharacterName && pl.IdentityName != string.Empty)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Content.Client.Cargo.UI;
using Content.Shared.Cargo.Components;
using JetBrains.Annotations;
using Robust.Client.GameObjects;

namespace Content.Client.Cargo.BUI;

Expand Down
3 changes: 1 addition & 2 deletions Content.Client/Cargo/UI/BountyEntry.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
HorizontalExpand="True">
<BoxContainer Orientation="Horizontal">
<BoxContainer Orientation="Vertical" HorizontalExpand="True">
<RichTextLabel Name="TimeLabel"/>
<RichTextLabel Name="RewardLabel"/>
<RichTextLabel Name="ManifestLabel"/>
</BoxContainer>
<Control MinWidth="10"/>
<BoxContainer Orientation="Vertical" MinWidth="120">
<Button Name="PrintButton" Text="{Loc 'bounty-console-label-button-text'}" HorizontalExpand="False" HorizontalAlignment="Right"/>
<Label Name="IdLabel" HorizontalAlignment="Right" Margin="0 0 5 0"/>
<RichTextLabel Name="IdLabel" HorizontalAlignment="Right" Margin="0 0 5 0"/>
</BoxContainer>
</BoxContainer>
<customControls:HSeparator Margin="5 10 5 10"/>
Expand Down
12 changes: 1 addition & 11 deletions Content.Client/Cargo/UI/BountyEntry.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public BountyEntry(CargoBountyData bounty)
if (!_prototype.TryIndex<CargoBountyPrototype>(bounty.Bounty, out var bountyPrototype))
return;

EndTime = bounty.EndTime;

var items = new List<string>();
foreach (var entry in bountyPrototype.Entries)
{
Expand All @@ -39,16 +37,8 @@ public BountyEntry(CargoBountyData bounty)
ManifestLabel.SetMarkup(Loc.GetString("bounty-console-manifest-label", ("item", string.Join(", ", items))));
RewardLabel.SetMarkup(Loc.GetString("bounty-console-reward-label", ("reward", bountyPrototype.Reward)));
DescriptionLabel.SetMarkup(Loc.GetString("bounty-console-description-label", ("description", Loc.GetString(bountyPrototype.Description))));
IdLabel.Text = Loc.GetString("bounty-console-id-label", ("id", bounty.Id));
IdLabel.SetMarkup(Loc.GetString("bounty-console-id-label", ("id", bounty.Id)));

PrintButton.OnPressed += _ => OnButtonPressed?.Invoke();
}

protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);

var remaining = TimeSpan.FromSeconds(Math.Max((EndTime - _timing.CurTime).TotalSeconds, 0));
TimeLabel.SetMarkup(Loc.GetString("bounty-console-time-label", ("time", remaining.ToString("mm':'ss"))));
}
}
2 changes: 1 addition & 1 deletion Content.Client/Cargo/UI/CargoBountyMenu.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Content.Client.Cargo.UI;
[GenerateTypedNameReferences]
public sealed partial class CargoBountyMenu : FancyWindow
{
public Action<int>? OnLabelButtonPressed;
public Action<string>? OnLabelButtonPressed;

public CargoBountyMenu()
{
Expand Down
Loading

0 comments on commit 369e651

Please sign in to comment.