Generic DOM: Add DomBackend abstraction and JSON support
This change adds a `DomBackend` interface and a `DomBackendRegistry` for backend discovery (currently not hooked up to anything) alongside a JSON backend implementation. The JSON backend comes with a small suite of unit and performance tests. The unit tests validate generic DOM conversion to and from serialized JSON and rapidjson::Document objects (the support for which lives in `JsonSerializationUtils.h`). The performance tests show a throughput decrease compared to directly using the rapidjson serializer when mirroring our current pattern of copying strings from the serialized JSON representation, but with the coming in-memory store we have the opportunity to keep the buffer in memory and deserialize in-situ using rapidjson's API, which is consistently at least 100MiB/s faster on my machine (Ryzen Threadripper 3970X). The first parameter is the nested object complexity (N*N, objects with N keys comprised of arrays with N values) and the second parameter is the base size of the strings within each entry of this object. ``` ---------------------------------------------------------------------------------------------------------------------- Benchmark Time CPU Iterations UserCounters... ---------------------------------------------------------------------------------------------------------------------- DomJsonBenchmark/DomDeserializeToDocumentInPlace/10/5 0.050 ms 0.050 ms 10000 bytes_per_second=355.816M/s DomJsonBenchmark/DomDeserializeToDocumentInPlace/10/50 0.057 ms 0.057 ms 11200 bytes_per_second=386.064M/s DomJsonBenchmark/DomDeserializeToDocumentInPlace/100/5 4.77 ms 4.88 ms 112 bytes_per_second=364.046M/s DomJsonBenchmark/DomDeserializeToDocumentInPlace/100/500 11.6 ms 11.7 ms 64 bytes_per_second=554.518M/s DomJsonBenchmark/DomDeserializeToDocumentWithCopies/10/5 0.084 ms 0.084 ms 7467 bytes_per_second=212.55M/s DomJsonBenchmark/DomDeserializeToDocumentWithCopies/10/50 0.099 ms 0.100 ms 6400 bytes_per_second=220.608M/s DomJsonBenchmark/DomDeserializeToDocumentWithCopies/100/5 8.22 ms 8.16 ms 90 bytes_per_second=217.847M/s DomJsonBenchmark/DomDeserializeToDocumentWithCopies/100/500 23.2 ms 22.9 ms 30 bytes_per_second=283.56M/s DomJsonBenchmark/JsonUtilsDeserializeToDocument/10/5 0.070 ms 0.070 ms 11200 bytes_per_second=255.049M/s DomJsonBenchmark/JsonUtilsDeserializeToDocument/10/50 0.086 ms 0.087 ms 8960 bytes_per_second=253.258M/s DomJsonBenchmark/JsonUtilsDeserializeToDocument/100/5 6.86 ms 6.84 ms 112 bytes_per_second=260.033M/s DomJsonBenchmark/JsonUtilsDeserializeToDocument/100/500 22.8 ms 22.9 ms 32 bytes_per_second=283.158M/s ``` For `AZ::DOM::Document`, the current plan is to offer helper methods that can load from a file path or string using a given backend that can take advantage of in-place parsing by internally storing the serialized buffer. Signed-off-by: Nicholas Van Sickle <nvsickle@amazon.com>
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* Copyright (c) Contributors to the Open 3D Engine Project.
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#if defined(HAVE_BENCHMARK)
|
||||
|
||||
#include <AzCore/DOM/Backends/JSON/JsonBackend.h>
|
||||
#include <AzCore/DOM/Backends/JSON/JsonSerializationUtils.h>
|
||||
#include <AzCore/JSON/document.h>
|
||||
#include <AzCore/Name/NameDictionary.h>
|
||||
#include <AzCore/Serialization/Json/JsonUtils.h>
|
||||
#include <AzCore/UnitTest/TestTypes.h>
|
||||
|
||||
namespace Benchmark
|
||||
{
|
||||
class DomJsonBenchmark : public UnitTest::AllocatorsBenchmarkFixture
|
||||
{
|
||||
public:
|
||||
void SetUp([[maybe_unused]] const ::benchmark::State& st) override
|
||||
{
|
||||
UnitTest::AllocatorsBenchmarkFixture::SetUp(st);
|
||||
AZ::NameDictionary::Create();
|
||||
}
|
||||
|
||||
void SetUp([[maybe_unused]] ::benchmark::State& st) override
|
||||
{
|
||||
UnitTest::AllocatorsBenchmarkFixture::SetUp(st);
|
||||
AZ::NameDictionary::Create();
|
||||
}
|
||||
|
||||
void TearDown([[maybe_unused]] ::benchmark::State& st) override
|
||||
{
|
||||
AZ::NameDictionary::Destroy();
|
||||
UnitTest::AllocatorsBenchmarkFixture::TearDown(st);
|
||||
}
|
||||
|
||||
void TearDown([[maybe_unused]] const ::benchmark::State& st) override
|
||||
{
|
||||
AZ::NameDictionary::Destroy();
|
||||
UnitTest::AllocatorsBenchmarkFixture::TearDown(st);
|
||||
}
|
||||
|
||||
AZStd::string GenerateDomJsonBenchmarkPayload(int64_t entryCount = 100, int64_t stringTemplateLength = 5)
|
||||
{
|
||||
rapidjson::Document document;
|
||||
document.SetObject();
|
||||
|
||||
AZStd::string entryTemplate;
|
||||
while (entryTemplate.size() < static_cast<size_t>(stringTemplateLength))
|
||||
{
|
||||
entryTemplate += "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor ";
|
||||
}
|
||||
entryTemplate.resize(stringTemplateLength);
|
||||
AZStd::string buffer;
|
||||
|
||||
auto createString = [&](int n) -> rapidjson::Value
|
||||
{
|
||||
buffer = AZStd::string::format("#%i %s", n, entryTemplate.c_str());
|
||||
return rapidjson::Value(buffer.data(), static_cast<rapidjson::SizeType>(buffer.size()), document.GetAllocator());
|
||||
};
|
||||
|
||||
auto createEntry = [&](int n) -> rapidjson::Value
|
||||
{
|
||||
rapidjson::Value entry(rapidjson::kObjectType);
|
||||
entry.AddMember("string", createString(n), document.GetAllocator());
|
||||
entry.AddMember("int", rapidjson::Value(n), document.GetAllocator());
|
||||
entry.AddMember("double", rapidjson::Value(static_cast<double>(n) * 0.5), document.GetAllocator());
|
||||
entry.AddMember("bool", rapidjson::Value(n % 2 == 0), document.GetAllocator());
|
||||
entry.AddMember("null", rapidjson::Value(rapidjson::kNullType), document.GetAllocator());
|
||||
return entry;
|
||||
};
|
||||
|
||||
auto createArray = [&]() -> rapidjson::Value
|
||||
{
|
||||
rapidjson::Value array;
|
||||
array.SetArray();
|
||||
for (int i = 0; i < entryCount; ++i)
|
||||
{
|
||||
array.PushBack(createEntry(i), document.GetAllocator());
|
||||
}
|
||||
return array;
|
||||
};
|
||||
|
||||
auto createObject = [&]() -> rapidjson::Value
|
||||
{
|
||||
rapidjson::Value object;
|
||||
object.SetObject();
|
||||
for (int i = 0; i < entryCount; ++i)
|
||||
{
|
||||
buffer = AZStd::string::format("Key%i", i);
|
||||
rapidjson::Value key;
|
||||
key.SetString(buffer.data(), static_cast<rapidjson::SizeType>(buffer.length()), document.GetAllocator());
|
||||
object.AddMember(key.Move(), createArray(), document.GetAllocator());
|
||||
}
|
||||
return object;
|
||||
};
|
||||
|
||||
document.SetObject();
|
||||
document.AddMember("entries", createObject(), document.GetAllocator());
|
||||
|
||||
AZStd::string serializedJson;
|
||||
auto result = AZ::JsonSerializationUtils::WriteJsonString(document, serializedJson);
|
||||
AZ_Assert(result.IsSuccess(), "Failed to serialize generated JSON");
|
||||
return serializedJson;
|
||||
}
|
||||
};
|
||||
|
||||
// Helper macro for registering JSON benchmarks
|
||||
#define BENCHMARK_REGISTER_JSON(BaseClass, Method) \
|
||||
BENCHMARK_REGISTER_F(BaseClass, Method) \
|
||||
->Args({ 10, 5 }) \
|
||||
->Args({ 10, 500 }) \
|
||||
->Args({ 100, 5 }) \
|
||||
->Args({ 100, 500 }) \
|
||||
->Unit(benchmark::kMillisecond);
|
||||
|
||||
BENCHMARK_DEFINE_F(DomJsonBenchmark, DomDeserializeToDocumentInPlace)(benchmark::State& state)
|
||||
{
|
||||
AZ::DOM::JsonBackend backend;
|
||||
AZStd::string serializedPayload = GenerateDomJsonBenchmarkPayload(state.range(0), state.range(1));
|
||||
|
||||
for (auto _ : state)
|
||||
{
|
||||
state.PauseTiming();
|
||||
AZStd::string payloadCopy = serializedPayload;
|
||||
state.ResumeTiming();
|
||||
|
||||
auto result = AZ::DOM::Json::WriteToRapidJsonDocument(
|
||||
[&](AZ::DOM::Visitor* visitor)
|
||||
{
|
||||
return backend.ReadFromStringInPlace(payloadCopy, visitor);
|
||||
});
|
||||
|
||||
benchmark::DoNotOptimize(result.GetValue());
|
||||
}
|
||||
|
||||
state.SetBytesProcessed(serializedPayload.size() * state.iterations());
|
||||
}
|
||||
BENCHMARK_REGISTER_JSON(DomJsonBenchmark, DomDeserializeToDocumentInPlace)
|
||||
|
||||
BENCHMARK_DEFINE_F(DomJsonBenchmark, DomDeserializeToDocument)(benchmark::State& state)
|
||||
{
|
||||
AZ::DOM::JsonBackend backend;
|
||||
AZStd::string serializedPayload = GenerateDomJsonBenchmarkPayload(state.range(0), state.range(1));
|
||||
|
||||
for (auto _ : state)
|
||||
{
|
||||
auto result = AZ::DOM::Json::WriteToRapidJsonDocument(
|
||||
[&](AZ::DOM::Visitor* visitor)
|
||||
{
|
||||
return backend.ReadFromString(serializedPayload, AZ::DOM::Lifetime::Temporary, visitor);
|
||||
});
|
||||
|
||||
benchmark::DoNotOptimize(result.GetValue());
|
||||
}
|
||||
|
||||
state.SetBytesProcessed(serializedPayload.size() * state.iterations());
|
||||
}
|
||||
BENCHMARK_REGISTER_JSON(DomJsonBenchmark, DomDeserializeToDocument)
|
||||
|
||||
BENCHMARK_DEFINE_F(DomJsonBenchmark, JsonUtilsDeserializeToDocument)(benchmark::State& state)
|
||||
{
|
||||
AZ::DOM::JsonBackend backend;
|
||||
AZStd::string serializedPayload = GenerateDomJsonBenchmarkPayload(state.range(0), state.range(1));
|
||||
|
||||
for (auto _ : state)
|
||||
{
|
||||
auto result = AZ::JsonSerializationUtils::ReadJsonString(serializedPayload);
|
||||
|
||||
benchmark::DoNotOptimize(result.GetValue());
|
||||
}
|
||||
|
||||
state.SetBytesProcessed(serializedPayload.size() * state.iterations());
|
||||
}
|
||||
BENCHMARK_REGISTER_JSON(DomJsonBenchmark, JsonUtilsDeserializeToDocument)
|
||||
|
||||
#undef BENCHMARK_REGISTER_JSON
|
||||
} // namespace Benchmark
|
||||
|
||||
#endif // defined(HAVE_BENCHMARK)
|
||||
@@ -0,0 +1,288 @@
|
||||
/*
|
||||
* Copyright (c) Contributors to the Open 3D Engine Project.
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include <AzCore/DOM/Backends/JSON/JsonBackend.h>
|
||||
#include <AzCore/DOM/Backends/JSON/JsonSerializationUtils.h>
|
||||
#include <AzCore/Name/NameDictionary.h>
|
||||
#include <AzCore/Serialization/Json/JsonUtils.h>
|
||||
#include <AzCore/UnitTest/TestTypes.h>
|
||||
|
||||
namespace AZ::DOM::Tests
|
||||
{
|
||||
class DomJsonTests : public UnitTest::AllocatorsFixture
|
||||
{
|
||||
public:
|
||||
void SetUp() override
|
||||
{
|
||||
UnitTest::AllocatorsFixture::SetUp();
|
||||
NameDictionary::Create();
|
||||
m_document = AZStd::make_unique<rapidjson::Document>();
|
||||
}
|
||||
|
||||
void TearDown() override
|
||||
{
|
||||
m_document.reset();
|
||||
NameDictionary::Destroy();
|
||||
UnitTest::AllocatorsFixture::TearDown();
|
||||
}
|
||||
|
||||
static bool DeepCompare(const rapidjson::Value& lhs, const rapidjson::Value& rhs)
|
||||
{
|
||||
if (lhs.GetType() != rhs.GetType())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (lhs.GetType())
|
||||
{
|
||||
case rapidjson::kNullType:
|
||||
return true;
|
||||
case rapidjson::kFalseType:
|
||||
return true;
|
||||
case rapidjson::kTrueType:
|
||||
return true;
|
||||
case rapidjson::kObjectType:
|
||||
{
|
||||
if (lhs.MemberCount() != rhs.MemberCount())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
auto lhsIt = lhs.MemberBegin();
|
||||
auto rhsIt = rhs.MemberBegin();
|
||||
while (lhsIt != lhs.MemberEnd())
|
||||
{
|
||||
if (lhsIt->name != rhsIt->name)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!DeepCompare(lhsIt->value, rhsIt->value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
++lhsIt;
|
||||
++rhsIt;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case rapidjson::kArrayType:
|
||||
{
|
||||
if (lhs.Size() != rhs.Size())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
auto lhsIt = lhs.Begin();
|
||||
auto rhsIt = rhs.Begin();
|
||||
while (lhsIt != lhs.End())
|
||||
{
|
||||
if (!DeepCompare(*lhsIt, *rhsIt))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
++lhsIt;
|
||||
++rhsIt;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case rapidjson::kStringType:
|
||||
return lhs == rhs;
|
||||
case rapidjson::kNumberType:
|
||||
return lhs == rhs;
|
||||
}
|
||||
|
||||
AZ_Assert(false, "Unexpected JSON value type");
|
||||
return false;
|
||||
}
|
||||
|
||||
rapidjson::Value CreateString(const AZStd::string& text)
|
||||
{
|
||||
rapidjson::Value key;
|
||||
key.SetString(text.c_str(), static_cast<rapidjson::SizeType>(text.length()), m_document->GetAllocator());
|
||||
return key;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void AddValue(const AZStd::string& key, T value)
|
||||
{
|
||||
m_document->AddMember(CreateString(key), rapidjson::Value(value), m_document->GetAllocator());
|
||||
}
|
||||
|
||||
// Validate round-trip serialization to and from rapidjson::Document and a UTF-8 encoded string
|
||||
void PerformSerializationChecks()
|
||||
{
|
||||
// Generate a canonical serializaed representation of this document using rapidjson
|
||||
// This will be pretty-printed using the same rapidjson pretty printer we use, so should be binary identical
|
||||
// to any output generated by the visitor API
|
||||
AZStd::string canonicalSerializedDocument;
|
||||
AZ::JsonSerializationUtils::WriteJsonString(*m_document, canonicalSerializedDocument);
|
||||
|
||||
auto visitDocumentFn = [this](AZ::DOM::Visitor* visitor)
|
||||
{
|
||||
return Json::VisitRapidJsonValue(*m_document, visitor, Lifetime::Persistent);
|
||||
};
|
||||
|
||||
// Document -> Document
|
||||
{
|
||||
auto result = Json::WriteToRapidJsonDocument(visitDocumentFn);
|
||||
EXPECT_TRUE(result.IsSuccess());
|
||||
EXPECT_TRUE(DeepCompare(*m_document, result.GetValue()));
|
||||
}
|
||||
|
||||
// Document -> string
|
||||
{
|
||||
AZStd::string serializedDocument;
|
||||
JsonBackend backend;
|
||||
auto result = backend.WriteToString(serializedDocument, visitDocumentFn);
|
||||
EXPECT_TRUE(result.IsSuccess());
|
||||
EXPECT_EQ(canonicalSerializedDocument, serializedDocument);
|
||||
}
|
||||
|
||||
// string -> Document
|
||||
{
|
||||
auto result = Json::WriteToRapidJsonDocument(
|
||||
[&canonicalSerializedDocument](AZ::DOM::Visitor* visitor)
|
||||
{
|
||||
JsonBackend backend;
|
||||
return backend.ReadFromString(canonicalSerializedDocument, AZ::DOM::Lifetime::Temporary, visitor);
|
||||
});
|
||||
EXPECT_TRUE(result.IsSuccess());
|
||||
EXPECT_TRUE(DeepCompare(*m_document, result.GetValue()));
|
||||
}
|
||||
|
||||
// string -> string
|
||||
{
|
||||
AZStd::string serializedDocument;
|
||||
JsonBackend backend;
|
||||
auto result = backend.WriteToString(
|
||||
serializedDocument,
|
||||
[&backend, &canonicalSerializedDocument](AZ::DOM::Visitor* visitor)
|
||||
{
|
||||
return backend.ReadFromString(canonicalSerializedDocument, AZ::DOM::Lifetime::Temporary, visitor);
|
||||
});
|
||||
EXPECT_TRUE(result.IsSuccess());
|
||||
EXPECT_EQ(canonicalSerializedDocument, serializedDocument);
|
||||
}
|
||||
}
|
||||
|
||||
AZStd::unique_ptr<rapidjson::Document> m_document;
|
||||
};
|
||||
|
||||
TEST_F(DomJsonTests, EmptyArray)
|
||||
{
|
||||
m_document->SetArray();
|
||||
PerformSerializationChecks();
|
||||
}
|
||||
|
||||
TEST_F(DomJsonTests, SimpleArray)
|
||||
{
|
||||
m_document->SetArray();
|
||||
for (int i = 0; i < 5; ++i)
|
||||
{
|
||||
m_document->PushBack(i, m_document->GetAllocator());
|
||||
}
|
||||
PerformSerializationChecks();
|
||||
}
|
||||
|
||||
TEST_F(DomJsonTests, NestedArrays)
|
||||
{
|
||||
m_document->SetArray();
|
||||
for (int j = 0; j < 7; ++j)
|
||||
{
|
||||
rapidjson::Value nestedArray(rapidjson::kArrayType);
|
||||
for (int i = 0; i < 5; ++i)
|
||||
{
|
||||
nestedArray.PushBack(i, m_document->GetAllocator());
|
||||
}
|
||||
m_document->PushBack(nestedArray.Move(), m_document->GetAllocator());
|
||||
}
|
||||
PerformSerializationChecks();
|
||||
}
|
||||
|
||||
TEST_F(DomJsonTests, EmptyObject)
|
||||
{
|
||||
m_document->SetObject();
|
||||
PerformSerializationChecks();
|
||||
}
|
||||
|
||||
TEST_F(DomJsonTests, SimpleObject)
|
||||
{
|
||||
m_document->SetObject();
|
||||
for (int i = 0; i < 5; ++i)
|
||||
{
|
||||
m_document->AddMember(CreateString(AZStd::string::format("Key%i", i)), rapidjson::Value(i), m_document->GetAllocator());
|
||||
}
|
||||
PerformSerializationChecks();
|
||||
}
|
||||
|
||||
TEST_F(DomJsonTests, NestedObjects)
|
||||
{
|
||||
m_document->SetObject();
|
||||
for (int j = 0; j < 7; ++j)
|
||||
{
|
||||
rapidjson::Value nestedObject(rapidjson::kObjectType);
|
||||
for (int i = 0; i < 5; ++i)
|
||||
{
|
||||
nestedObject.AddMember(CreateString(AZStd::string::format("Key%i", i)), rapidjson::Value(i), m_document->GetAllocator());
|
||||
}
|
||||
m_document->AddMember(CreateString(AZStd::string::format("Obj%i", j)), nestedObject.Move(), m_document->GetAllocator());
|
||||
}
|
||||
PerformSerializationChecks();
|
||||
}
|
||||
|
||||
TEST_F(DomJsonTests, Int64)
|
||||
{
|
||||
m_document->SetObject();
|
||||
AddValue("int64_min", AZStd::numeric_limits<int64_t>::min());
|
||||
AddValue("int64_max", AZStd::numeric_limits<int64_t>::max());
|
||||
PerformSerializationChecks();
|
||||
}
|
||||
|
||||
TEST_F(DomJsonTests, Uint64)
|
||||
{
|
||||
m_document->SetObject();
|
||||
AddValue("uint64_min", AZStd::numeric_limits<uint64_t>::min());
|
||||
AddValue("uint64_max", AZStd::numeric_limits<uint64_t>::max());
|
||||
PerformSerializationChecks();
|
||||
}
|
||||
|
||||
TEST_F(DomJsonTests, Double)
|
||||
{
|
||||
m_document->SetObject();
|
||||
AddValue("double_min", AZStd::numeric_limits<double>::min());
|
||||
AddValue("double_max", AZStd::numeric_limits<double>::max());
|
||||
PerformSerializationChecks();
|
||||
}
|
||||
|
||||
TEST_F(DomJsonTests, Null)
|
||||
{
|
||||
m_document->SetObject();
|
||||
m_document->AddMember(CreateString("null_value"), rapidjson::Value(rapidjson::kNullType), m_document->GetAllocator());
|
||||
PerformSerializationChecks();
|
||||
}
|
||||
|
||||
TEST_F(DomJsonTests, Bool)
|
||||
{
|
||||
m_document->SetObject();
|
||||
AddValue("true_value", true);
|
||||
AddValue("false_value", false);
|
||||
PerformSerializationChecks();
|
||||
}
|
||||
|
||||
TEST_F(DomJsonTests, String)
|
||||
{
|
||||
m_document->SetObject();
|
||||
m_document->AddMember(CreateString("empty_string"), CreateString(""), m_document->GetAllocator());
|
||||
m_document->AddMember(CreateString("short_string"), CreateString("test"), m_document->GetAllocator());
|
||||
m_document->AddMember(CreateString("long_string"), CreateString("abcdefghijklmnopqrstuvwxyz0123456789"), m_document->GetAllocator());
|
||||
PerformSerializationChecks();
|
||||
}
|
||||
} // namespace AZ::DOM::Tests
|
||||
Reference in New Issue
Block a user