Json telemetry
This commit is contained in:
123
tests/RenderCadenceCompositorJsonWriterTests.cpp
Normal file
123
tests/RenderCadenceCompositorJsonWriterTests.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
#include "JsonWriter.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
int gFailures = 0;
|
||||
|
||||
void Expect(bool condition, const std::string& message)
|
||||
{
|
||||
if (condition)
|
||||
return;
|
||||
|
||||
++gFailures;
|
||||
std::cerr << "FAILED: " << message << "\n";
|
||||
}
|
||||
|
||||
void ExpectEquals(const std::string& actual, const std::string& expected, const std::string& message)
|
||||
{
|
||||
if (actual == expected)
|
||||
return;
|
||||
|
||||
++gFailures;
|
||||
std::cerr << "FAILED: " << message << "\n"
|
||||
<< "expected: " << expected << "\n"
|
||||
<< "actual: " << actual << "\n";
|
||||
}
|
||||
|
||||
void TestEscapesStrings()
|
||||
{
|
||||
using RenderCadenceCompositor::JsonWriter;
|
||||
|
||||
ExpectEquals(
|
||||
JsonWriter::EscapeString("quote\" slash\\ newline\n tab\t"),
|
||||
"quote\\\" slash\\\\ newline\\n tab\\t",
|
||||
"string escape handles common escaped characters");
|
||||
|
||||
std::string control;
|
||||
control.push_back(static_cast<char>(0x01));
|
||||
ExpectEquals(JsonWriter::EscapeString(control), "\\u0001", "string escape handles control characters");
|
||||
}
|
||||
|
||||
void TestObjectSerialization()
|
||||
{
|
||||
using RenderCadenceCompositor::JsonWriter;
|
||||
|
||||
JsonWriter writer;
|
||||
writer.BeginObject();
|
||||
writer.KeyString("name", "cadence");
|
||||
writer.KeyDouble("renderFps", 59.94);
|
||||
writer.KeyBool("healthy", true);
|
||||
writer.KeyNull("error");
|
||||
writer.EndObject();
|
||||
|
||||
ExpectEquals(
|
||||
writer.StringValue(),
|
||||
"{\"name\":\"cadence\",\"renderFps\":59.94,\"healthy\":true,\"error\":null}",
|
||||
"object serialization is compact and ordered");
|
||||
}
|
||||
|
||||
void TestNestedArrays()
|
||||
{
|
||||
using RenderCadenceCompositor::JsonWriter;
|
||||
|
||||
JsonWriter writer;
|
||||
writer.BeginObject();
|
||||
writer.Key("levels");
|
||||
writer.BeginArray();
|
||||
writer.String("log");
|
||||
writer.String("warning");
|
||||
writer.String("error");
|
||||
writer.EndArray();
|
||||
writer.Key("counts");
|
||||
writer.BeginObject();
|
||||
writer.KeyUInt("queued", 3);
|
||||
writer.KeyInt("delta", -1);
|
||||
writer.EndObject();
|
||||
writer.EndObject();
|
||||
|
||||
ExpectEquals(
|
||||
writer.StringValue(),
|
||||
"{\"levels\":[\"log\",\"warning\",\"error\"],\"counts\":{\"queued\":3,\"delta\":-1}}",
|
||||
"nested arrays and objects serialize correctly");
|
||||
}
|
||||
|
||||
void TestMisuseThrows()
|
||||
{
|
||||
using RenderCadenceCompositor::JsonWriter;
|
||||
|
||||
JsonWriter writer;
|
||||
bool threw = false;
|
||||
try
|
||||
{
|
||||
writer.BeginObject();
|
||||
writer.Key("missing");
|
||||
writer.EndObject();
|
||||
}
|
||||
catch (const std::logic_error&)
|
||||
{
|
||||
threw = true;
|
||||
}
|
||||
Expect(threw, "ending an object with a key missing a value throws");
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
TestEscapesStrings();
|
||||
TestObjectSerialization();
|
||||
TestNestedArrays();
|
||||
TestMisuseThrows();
|
||||
|
||||
if (gFailures != 0)
|
||||
{
|
||||
std::cerr << gFailures << " RenderCadenceCompositorJsonWriter test failure(s).\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "RenderCadenceCompositorJsonWriter tests passed.\n";
|
||||
return 0;
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "CadenceTelemetry.h"
|
||||
#include "CadenceTelemetryJson.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
@@ -128,12 +129,60 @@ void TestTelemetryComputesRatesFromDeltas()
|
||||
Expect(snapshot.renderFps > 0.0, "render fps is computed from completed frame delta");
|
||||
Expect(snapshot.scheduleFps > 0.0, "schedule fps is computed from scheduled frame delta");
|
||||
}
|
||||
|
||||
void TestTelemetrySerializesToJson()
|
||||
{
|
||||
RenderCadenceCompositor::CadenceTelemetrySnapshot snapshot;
|
||||
snapshot.sampleSeconds = 1.0;
|
||||
snapshot.renderFps = 59.94;
|
||||
snapshot.scheduleFps = 60.0;
|
||||
snapshot.freeFrames = 7;
|
||||
snapshot.completedFrames = 1;
|
||||
snapshot.scheduledFrames = 4;
|
||||
snapshot.renderedTotal = 120;
|
||||
snapshot.scheduledTotal = 118;
|
||||
snapshot.completedPollMisses = 3;
|
||||
snapshot.scheduleFailures = 0;
|
||||
snapshot.completions = 117;
|
||||
snapshot.displayedLate = 1;
|
||||
snapshot.dropped = 2;
|
||||
snapshot.shaderBuildsCommitted = 1;
|
||||
snapshot.shaderBuildFailures = 0;
|
||||
snapshot.deckLinkBufferedAvailable = true;
|
||||
snapshot.deckLinkBuffered = 4;
|
||||
snapshot.deckLinkScheduleCallMilliseconds = 1.25;
|
||||
|
||||
const std::string json = RenderCadenceCompositor::CadenceTelemetryToJson(snapshot);
|
||||
const std::string expected =
|
||||
"{\"sampleSeconds\":1,\"renderFps\":59.94,\"scheduleFps\":60,"
|
||||
"\"free\":7,\"completed\":1,\"scheduled\":4,"
|
||||
"\"renderedTotal\":120,\"scheduledTotal\":118,"
|
||||
"\"completedPollMisses\":3,\"scheduleFailures\":0,"
|
||||
"\"completions\":117,\"late\":1,\"dropped\":2,"
|
||||
"\"shaderCommitted\":1,\"shaderFailures\":0,"
|
||||
"\"deckLinkBufferedAvailable\":true,\"deckLinkBuffered\":4,"
|
||||
"\"scheduleCallMs\":1.25}";
|
||||
Expect(json == expected, "telemetry snapshot serializes to stable JSON");
|
||||
}
|
||||
|
||||
void TestUnavailableDeckLinkBufferSerializesAsNull()
|
||||
{
|
||||
RenderCadenceCompositor::CadenceTelemetrySnapshot snapshot;
|
||||
snapshot.deckLinkBufferedAvailable = false;
|
||||
|
||||
const std::string json = RenderCadenceCompositor::CadenceTelemetryToJson(snapshot);
|
||||
Expect(
|
||||
json.find("\"deckLinkBufferedAvailable\":false,\"deckLinkBuffered\":null") != std::string::npos,
|
||||
"unavailable DeckLink buffer depth serializes as null");
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
TestTelemetrySamplesCompletedPollMissesAndShaderCounts();
|
||||
TestTelemetryComputesRatesFromDeltas();
|
||||
TestTelemetrySerializesToJson();
|
||||
TestUnavailableDeckLinkBufferSerializesAsNull();
|
||||
|
||||
if (gFailures != 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user