feat(net): frame timing section in SERVER_TRAFFIC, profiling messages

An optional section after marker 3 carries the report's frame timing; older readers stop before it and reports without it still read. Phase times go with their count so readers with fewer or more phases read them. PROFILE_REQUEST and PROFILE_RESULT are appended to the master messages (44, 45). Task 96.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-09-29 22:06:29 -05:00
parent 06af6c3aaa
commit d0c7b089ff
5 changed files with 375 additions and 1 deletions

View File

@@ -2,6 +2,8 @@
#include <cstring>
#include "master/ServerTraffic.h"
#include "master/Profiling.h"
#include <array>
using namespace TrafficStats;
@@ -117,3 +119,146 @@ TEST(ServerTrafficTest, ReportsWithoutTheSplitStillRead) {
EXPECT_TRUE(got.report.seconds[0].peers[0].Empty());
EXPECT_EQ(got.report.gauges.size(), 1u);
}
namespace {
Profiler::Report SampleFrames() {
Profiler::Report frames;
frames.present = true;
frames.slowThresholdMs = 250;
Profiler::Second second{ .time = 1700000000, .ticks = 30, .totalUs = 90000, .maxUs = 12000 };
second.frames.Add(3000, 29);
second.frames.Add(12000);
second.phaseUs[static_cast<size_t>(Profiler::Phase::PACKETS)] = 40000;
second.phaseUs[static_cast<size_t>(Profiler::Phase::ENTITIES)] = 30000;
frames.seconds = { second, Profiler::Second{ .time = 1700000001 } };
frames.messages = { Profiler::MessageTime{ .key = TrafficStats::MessageKey{ false, 4, 5, 1234 }.Packed(), .count = 3, .totalUs = 700, .maxUs = 400 } };
Profiler::Frame slow{ .timeMs = 1700000000500, .durationUs = 600000 };
slow.phaseUs[static_cast<size_t>(Profiler::Phase::CDCLIENT)] = 550000;
slow.scopes = {
{ .name = "Frame", .depth = 0, .count = 1, .totalUs = 600000 },
{ .name = "LoadPlayer", .depth = 1, .count = 1, .totalUs = 590000, .startUs = 100 },
{ .name = "CDClient Objects", .depth = 2, .count = 9800, .totalUs = 550000, .startUs = 200 },
};
frames.slow = { slow };
frames.worst = { slow };
return frames;
}
}
TEST(ServerTrafficTest, FramesRoundTrip) {
auto sent = Sample(true);
sent.frames = SampleFrames();
RakNet::BitStream stream;
sent.WritePacket(stream);
ServerTraffic got;
ASSERT_TRUE(ReadBack(stream, stream.GetNumberOfBytesUsed(), got));
EXPECT_TRUE(got.report.peerSplit);
ASSERT_TRUE(got.frames.present);
EXPECT_EQ(got.frames.slowThresholdMs, 250u);
ASSERT_EQ(got.frames.seconds.size(), 2u);
const auto& s = got.frames.seconds[0];
EXPECT_EQ(s.time, 1700000000);
EXPECT_EQ(s.ticks, 30u);
EXPECT_EQ(s.totalUs, 90000u);
EXPECT_EQ(s.maxUs, 12000u);
EXPECT_EQ(s.frames.Count(), 30u);
EXPECT_EQ(s.frames.Sum(), 3000u * 29 + 12000u);
EXPECT_EQ(s.phaseUs[static_cast<size_t>(Profiler::Phase::PACKETS)], 40000u);
EXPECT_EQ(s.phaseUs[static_cast<size_t>(Profiler::Phase::ENTITIES)], 30000u);
EXPECT_EQ(got.frames.seconds[1].ticks, 0u);
ASSERT_EQ(got.frames.messages.size(), 1u);
EXPECT_EQ(got.frames.messages[0].count, 3u);
EXPECT_EQ(got.frames.messages[0].maxUs, 400u);
ASSERT_EQ(got.frames.slow.size(), 1u);
ASSERT_EQ(got.frames.worst.size(), 1u);
const auto& frame = got.frames.slow[0];
EXPECT_EQ(frame.timeMs, 1700000000500);
EXPECT_EQ(frame.durationUs, 600000u);
EXPECT_EQ(frame.phaseUs[static_cast<size_t>(Profiler::Phase::CDCLIENT)], 550000u);
EXPECT_EQ(frame.scopes, sent.frames.slow[0].scopes);
// A cut-off section is refused
ServerTraffic broken;
EXPECT_FALSE(ReadBack(stream, stream.GetNumberOfBytesUsed() - 2, broken));
}
TEST(ServerTrafficTest, ReportsWithoutFramesStillRead) {
// An older server's report stops before the frames section: it reads, without frames
auto withFrames = Sample(true);
withFrames.frames = SampleFrames();
RakNet::BitStream old, now;
Sample(true).WritePacket(old);
withFrames.WritePacket(now);
ASSERT_LT(old.GetNumberOfBytesUsed(), now.GetNumberOfBytesUsed());
EXPECT_EQ(0, std::memcmp(old.GetData(), now.GetData(), old.GetNumberOfBytesUsed()));
ServerTraffic got;
ASSERT_TRUE(ReadBack(old, old.GetNumberOfBytesUsed(), got));
EXPECT_FALSE(got.frames.present);
EXPECT_TRUE(got.report.peerSplit);
}
TEST(ServerTrafficTest, ReadersStopAtSectionsTheyDontKnow) {
// A newer server's section after the frames: this reader keeps what it knows and stops there
auto sent = Sample(true);
sent.frames = SampleFrames();
RakNet::BitStream stream;
sent.WritePacket(stream);
stream.Write(static_cast<uint8_t>(200));
stream.Write(static_cast<uint32_t>(0xDEADBEEF));
ServerTraffic got;
ASSERT_TRUE(ReadBack(stream, stream.GetNumberOfBytesUsed(), got));
EXPECT_TRUE(got.frames.present);
EXPECT_EQ(got.frames.seconds.size(), 2u);
}
TEST(ServerTrafficTest, PhasesOfANewerServerRead) {
// Phases go with their count: more phases than this reader knows still read, the extra ones skipped
RakNet::BitStream stream;
stream.Write(static_cast<uint8_t>(Profiler::PHASES + 2));
for (uint32_t i = 0; i < Profiler::PHASES + 2; i++) stream.Write(i + 1);
std::array<uint64_t, Profiler::PHASES> phases{};
ASSERT_TRUE(ServerTraffic::ReadPhases(stream, phases));
EXPECT_EQ(phases[0], 1u);
EXPECT_EQ(phases[Profiler::PHASES - 1], Profiler::PHASES);
EXPECT_EQ(stream.GetNumberOfUnreadBits(), 0u);
}
TEST(ServerTrafficTest, ProfileMessagesRoundTrip) {
ProfileRequest request;
request.sessionId = 7;
request.serverType = ServiceType::WORLD;
request.zoneId = 1200;
request.instanceId = 2;
request.durationMs = 10000;
RakNet::BitStream requestStream;
request.WritePacket(requestStream);
LUBitStream header;
ASSERT_TRUE(header.ReadHeader(requestStream));
EXPECT_EQ(header.internalPacketID, static_cast<uint32_t>(MessageType::Master::PROFILE_REQUEST));
ProfileRequest gotRequest;
ASSERT_TRUE(gotRequest.Deserialize(requestStream));
EXPECT_EQ(gotRequest.sessionId, 7u);
EXPECT_EQ(gotRequest.zoneId, 1200u);
EXPECT_EQ(gotRequest.durationMs, 10000u);
EXPECT_FALSE(gotRequest.stop);
ProfileResult result;
result.sessionId = 7;
result.serverType = ServiceType::WORLD;
result.status = eProfileStatus::DONE;
result.profile.durationMs = 10002;
result.profile.frames = 300;
result.profile.totalUs = 450000;
result.profile.nodes = { { .name = "All frames", .count = 300, .totalUs = 450000 }, { .name = "Entities", .depth = 1, .count = 300, .totalUs = 200000 } };
RakNet::BitStream resultStream;
result.WritePacket(resultStream);
LUBitStream resultHeader;
ASSERT_TRUE(resultHeader.ReadHeader(resultStream));
EXPECT_EQ(resultHeader.internalPacketID, static_cast<uint32_t>(MessageType::Master::PROFILE_RESULT));
ProfileResult got;
ASSERT_TRUE(got.Deserialize(resultStream));
EXPECT_EQ(got.status, eProfileStatus::DONE);
EXPECT_EQ(got.profile.id, 7u);
EXPECT_EQ(got.profile.frames, 300u);
EXPECT_EQ(got.profile.nodes, result.profile.nodes);
}