Private
Public Access
1
0

perf logging, cap catchup loops

This commit is contained in:
2026-03-29 09:41:08 +02:00
parent de86ddbeef
commit b8e8533699
9 changed files with 158 additions and 35 deletions

View File

@@ -13,7 +13,7 @@ using Vintagestory.API.MathTools;
namespace RoamingBees.Behaviors;
public class BlockEntityBehaviorBeeSwarm(BlockEntity blockEntity) : BlockEntityBehavior(blockEntity), IBeeSpawnHandler, IBeeSpawnCatchup
public class BlockEntityBehaviorBeeSwarm(BlockEntity blockEntity) : BlockEntityBehavior(blockEntity), IBeeSpawnHandler, IBeeSpawnCatchup, IModEntity
{
private const float spawn_cooldown_seconds = 2f;
private const float hover_spawn_cooldown_seconds = 0.1f;
@@ -41,6 +41,7 @@ public class BlockEntityBehaviorBeeSwarm(BlockEntity blockEntity) : BlockEntityB
private double migrationPhaseDurationHours = 5.0;
private Vec3d windVec = new(1, 0, 0);
private readonly Stopwatch stopwatch = new();
public int ActiveBeesCount => activeBees.Count;
public int ActiveHoveringCount => activeBees.Count(b => b.Role == BeeRole.Hovering);
@@ -56,6 +57,8 @@ public class BlockEntityBehaviorBeeSwarm(BlockEntity blockEntity) : BlockEntityB
public float TimeSinceLastHoverSpawn { get; private set; } = hover_spawn_cooldown_seconds;
public SwarmState SwarmState { get; private set; }
public Mod? Mod { get; private set; }
public override void Initialize(ICoreAPI api, JsonObject properties)
{
if (initialized)
@@ -65,6 +68,7 @@ public class BlockEntityBehaviorBeeSwarm(BlockEntity blockEntity) : BlockEntityB
initialized = true;
modSystem = api.ModLoader.GetModSystem<RoamingBeesModSystem>();
Mod = modSystem?.Mod;
modSystem?.BeeSpawnPacketDistributor?.Register(Blockentity.Pos, this);
modSystem?.ClientChannel?.SendPacket(new BeeCatchupRequestPacket { HivePosition = Pos });
if (modSystem?.Mod.Info.Version.Contains("dev") == true && modSystem?.ClientChannel != null)
@@ -287,6 +291,18 @@ public class BlockEntityBehaviorBeeSwarm(BlockEntity blockEntity) : BlockEntityB
if (packet.Path is null || packet.Path.Length == 0)
return;
if (catchup)
{
var totalDelta = Math.Abs(Api.World.Calendar.ElapsedSeconds - packet.TimeElapsedSeconds);
if (totalDelta > 240)
{
Mod?.Logger.Notification($"{nameof(BlockEntityBehaviorBeeSwarm)} {nameof(HandleBeeParticleSpawn)} totalDelta: {totalDelta}s, discarding");
return;
}
}
stopwatch.Restart();
var points = packet.Path.Select(x => x.ToPoint()).ToArray();
var bee = new InternalBeeParticle(packet.EntrancePosition, GetFrontDirection(), points, packet.Role, packet.DespawnPosition, packet);
if (packet.Role == BeeRole.Hovering)
@@ -296,24 +312,19 @@ public class BlockEntityBehaviorBeeSwarm(BlockEntity blockEntity) : BlockEntityB
{
var totalDelta = Math.Abs(Api.World.Calendar.ElapsedSeconds - packet.TimeElapsedSeconds);
if (modSystem?.Mod.Info.Version.Contains("dev") == true)
modSystem.Mod.Logger.Notification($"HandleBeeParticleSpawn catchup totalDelta: {totalDelta}s");
modSystem.Mod.Logger.Notification($"HandleBeeParticleSpawn totalDelta: {totalDelta}s");
var stopwatch = Stopwatch.StartNew();
for (var i = 0; i < totalDelta / 0.1f; i++)
bee.Step(0.1f, 0.1f); // todo
stopwatch.Stop();
if (stopwatch.Elapsed.TotalSeconds > 0.02)
modSystem?.Mod?.Logger.Warning($"{nameof(BlockEntityBehaviorBeeSwarm)} {nameof(HandleBeeParticleSpawn)} catchup took {stopwatch.Elapsed.TotalSeconds:F2}s");
}
stopwatch.StopAndLogTime(this, 0.01);
}
private void OnTick(float dt)
{
var stopwatch = Stopwatch.StartNew();
stopwatch.Restart();
Update(dt);
stopwatch.Stop();
if (stopwatch.Elapsed.TotalSeconds > 0.2)
modSystem?.Mod?.Logger.Warning($"{nameof(BlockEntityBehaviorBeeSwarm)} {nameof(OnTick)} took {stopwatch.Elapsed.TotalSeconds:F2}s");
stopwatch.StopAndLogTime(this, 0.02);
}
private void Update(float dt)

View File

@@ -15,7 +15,7 @@ using Vintagestory.GameContent;
namespace RoamingBees.Behaviors;
public class BlockEntityBehaviorRoamingBees(BlockEntity blockEntity) : BlockEntityBehavior(blockEntity), IBeeSpawnHandler, IBeeSpawnCatchup
public class BlockEntityBehaviorRoamingBees(BlockEntity blockEntity) : BlockEntityBehavior(blockEntity), IBeeSpawnHandler, IBeeSpawnCatchup, IModEntity
{
private const float spawn_cooldown_seconds = 1.5f;
private const string flower_count_attribute = "roamingbees_flowerCount";
@@ -39,7 +39,7 @@ public class BlockEntityBehaviorRoamingBees(BlockEntity blockEntity) : BlockEnti
private ClimateCondition? climate;
private Vec3d? windVec;
private readonly Stopwatch stopwatch = new();
private readonly List<StructVec3i> flowerPositions = [];
private readonly List<StructVec3i> cropPositions = [];
private readonly List<Vector3> relativePlantPositions = [];
@@ -59,6 +59,7 @@ public class BlockEntityBehaviorRoamingBees(BlockEntity blockEntity) : BlockEnti
public float InitialScanProgress { get; private set; }
public float RescanProgress { get; private set; }
public Mod? Mod { get; private set; }
public override void Initialize(ICoreAPI api, JsonObject properties)
{
@@ -73,6 +74,7 @@ public class BlockEntityBehaviorRoamingBees(BlockEntity blockEntity) : BlockEnti
frontDirections = VectorParsing.ParseVector3Map(properties["frontDirections"]);
modSystem = api.ModLoader.GetModSystem<RoamingBeesModSystem>();
Mod = modSystem?.Mod;
modSystem?.BeeSpawnPacketDistributor?.Register(Blockentity.Pos, this);
modSystem?.ClientChannel?.SendPacket(new BeeCatchupRequestPacket { HivePosition = Pos });
if (modSystem?.Mod.Info.Version.Contains("dev") == true && modSystem?.ClientChannel != null)
@@ -148,6 +150,7 @@ public class BlockEntityBehaviorRoamingBees(BlockEntity blockEntity) : BlockEnti
public void HandleBeeParticleSpawn(BeeSpawnPacket packet, bool catchup = false)
{
stopwatch.Restart();
if (Blockentity.Pos != packet.HivePosition)
return;
@@ -163,6 +166,16 @@ public class BlockEntityBehaviorRoamingBees(BlockEntity blockEntity) : BlockEnti
return;
}
if (catchup)
{
var totalDelta = Math.Abs(Api.World.Calendar.ElapsedSeconds - packet.TimeElapsedSeconds);
if (totalDelta > 240)
{
Mod?.Logger.Notification($"{nameof(BlockEntityBehaviorBeeSwarm)} {nameof(HandleBeeParticleSpawn)} totalDelta: {totalDelta}s, discarding");
return;
}
}
var facing = packet.Facing ?? GetFacing();
Vector3 entrancePosition = GetEntrancePosition(facing);
var bee = new InternalBeeParticle(entrancePosition, GetFrontDirection(facing), [.. packet.Path.Select(x => x.ToPoint())], BeeRole.Forager, entrancePosition, packet);
@@ -173,13 +186,10 @@ public class BlockEntityBehaviorRoamingBees(BlockEntity blockEntity) : BlockEnti
if (modSystem?.Mod.Info.Version.Contains("dev") == true)
modSystem.Mod.Logger.Notification($"HandleBeeParticleSpawn catchup totalDelta: {totalDelta}s");
var stopwatch = Stopwatch.StartNew();
for (var i = 0; i < totalDelta / 0.1f; i++)
bee.Step(0.1f, 0.1f); // todo
stopwatch.Stop();
if (stopwatch.Elapsed.TotalSeconds > 0.02)
modSystem?.Mod?.Logger.Warning($"{nameof(BlockEntityBehaviorRoamingBees)} {nameof(HandleBeeParticleSpawn)} catchup took {stopwatch.Elapsed.TotalSeconds:F2}s");
}
stopwatch.StopAndLogTime(this, 0.01);
}
public void Clear()
@@ -199,6 +209,7 @@ public class BlockEntityBehaviorRoamingBees(BlockEntity blockEntity) : BlockEnti
if (Block is BlockSkep && !Config.Instance.EnableOnVanillaSkeps)
return;
stopwatch.Restart();
var isFgc = Block?.Code?.Domain == "fromgoldencombs";
var path = Block?.Code?.Path ?? string.Empty;
var isFgcLangstroth = isFgc && path.Contains("langstrothstack", StringComparison.OrdinalIgnoreCase);
@@ -231,9 +242,13 @@ public class BlockEntityBehaviorRoamingBees(BlockEntity blockEntity) : BlockEnti
TargetParticleCount = Config.Instance.RoamingBeesPerFgcCeramic;
if (Api.Side == EnumAppSide.Server)
{
UpdatePlantInfo();
stopwatch.LogTime(this, 0.05, "after UpdatePlantInfo");
}
Update(dt);
stopwatch.StopAndLogTime(this, 0.05);
}
private void Update(float dt)

View File

@@ -7,7 +7,7 @@
"OrekiWoof"
],
"description": "Cute immersive roaming bees. Now on vanilla skeps and other mods' hives.",
"version": "2.0.0-dev.6",
"version": "2.0.0-dev.7",
"dependencies": {
"game": "1.21.0"
}

0
RoamingBees/build.sh Normal file → Executable file
View File