Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ if(SOURCEMETA_CORE_MARKDOWN)
list(APPEND BENCHMARK_SOURCES spec/markdown.cc)
endif()

if(SOURCEMETA_CORE_YAML)
list(APPEND BENCHMARK_SOURCES spec/yaml.cc)
endif()

if(SOURCEMETA_CORE_GZIP)
list(APPEND BENCHMARK_SOURCES spec/gzip.cc)
endif()
Expand Down Expand Up @@ -108,6 +112,11 @@ if(BENCHMARK_SOURCES)
sourcemeta::core::jsonpointer)
endif()

if(SOURCEMETA_CORE_YAML)
target_link_libraries(sourcemeta_core_benchmarks
PRIVATE sourcemeta::core::yaml sourcemeta::core::json)
endif()

if(SOURCEMETA_CORE_URITEMPLATE)
target_link_libraries(sourcemeta_core_benchmarks
PRIVATE sourcemeta::core::uritemplate)
Expand Down
93 changes: 93 additions & 0 deletions benchmark/spec/jsonld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,49 @@ static auto populate_annotation_list(
}
}

// Expansion branches on how a term is defined, so the context states a
// language map, an index map, set containers, a date and a decimal coercion,
// a typed identifier, a property scoped context and a reverse property, and
// the catalog carries a value of every one of those shapes
// Parsing this at namespace scope would be a static initialiser that throws,
// so it is built on first use the way the keys above are
static auto catalog_context() -> const sourcemeta::core::JSON & {
static const auto INSTANCE{sourcemeta::core::parse_json(R"JSON({
"@vocab": "https://example.com/vocab/",
"schema": "https://schema.org/",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"id": "@id",
"members": { "@id": "schema:hasPart", "@container": "@set" },
"isbn": "schema:isbn",
"title": { "@id": "schema:name", "@container": "@language" },
"abstract": "schema:abstract",
"datePublished": { "@id": "schema:datePublished", "@type": "xsd:date" },
"authors": { "@id": "schema:author", "@container": "@set" },
"name": "schema:name",
"keywords": { "@id": "schema:keywords", "@container": "@set" },
"identifiers": { "@id": "schema:identifier", "@container": "@index" },
"price": {
"@id": "schema:offers",
"@context": {
"value": { "@id": "schema:price", "@type": "xsd:decimal" },
"currency": "schema:priceCurrency"
}
},
"publisher": "schema:publisher",
"url": { "@id": "schema:url", "@type": "@id" },
"series": "schema:isPartOf",
"metadata": { "@id": "schema:additionalProperty", "@container": "@index" },
"provenance": { "@reverse": "schema:about" }
})JSON")};
return INSTANCE;
}

static auto make_described_catalog() -> sourcemeta::core::JSON {
auto catalog{make_catalog()};
catalog.assign("@context", catalog_context());
return catalog;
}

BENCHMARK(JSONLD_Catalog_Annotation_List_Populate) {
for (auto iteration : state) {
sourcemeta::core::JSONLDWeakAnnotationList annotations;
Expand All @@ -371,3 +414,53 @@ BENCHMARK(JSONLD_Catalog_Materialize) {
sourcemeta::core::benchmark_do_not_optimize(result);
}
}

BENCHMARK(JSONLD_Catalog_Expand) {
const auto instance{make_described_catalog()};

for (auto iteration : state) {
auto result{sourcemeta::core::jsonld_expand(instance)};
assert(result.is_array());
assert(result.size() == 1);
assert(result.at(0).defines("https://schema.org/hasPart"));
assert(result.at(0).at("https://schema.org/hasPart").size() ==
CATALOG_MEMBER_COUNT);
sourcemeta::core::benchmark_do_not_optimize(result);
}
}

// Processing the context happens once per document, so a catalog of one member
// is what shows a change to it, while the full catalog amortises it away
BENCHMARK(JSONLD_Member_Expand) {
auto members{sourcemeta::core::JSON::make_array()};
members.push_back(make_member(0));
auto instance{sourcemeta::core::JSON::make_object()};
instance.assign("@context", catalog_context());
instance.assign("members", std::move(members));

for (auto iteration : state) {
auto result{sourcemeta::core::jsonld_expand(instance)};
assert(result.is_array());
assert(result.size() == 1);
assert(result.at(0).at("https://schema.org/hasPart").size() == 1);
[[maybe_unused]] const auto &member{
result.at(0).at("https://schema.org/hasPart").at(0)};
assert(member.defines("@reverse"));
assert(member.at("https://schema.org/name").size() == 3);
assert(member.at("https://schema.org/identifier").size() == 3);
sourcemeta::core::benchmark_do_not_optimize(result);
}
}

BENCHMARK(JSONLD_Catalog_Compact) {
const auto expanded{
sourcemeta::core::jsonld_expand(make_described_catalog())};
const auto &context{catalog_context()};

for (auto iteration : state) {
auto result{sourcemeta::core::jsonld_compact(expanded, context)};
assert(result.is_object());
assert(result.at("members").size() == CATALOG_MEMBER_COUNT);
sourcemeta::core::benchmark_do_not_optimize(result);
}
}
158 changes: 157 additions & 1 deletion benchmark/spec/openapi.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#include <sourcemeta/core/benchmark.h>

#include <filesystem> // std::filesystem
#include <cassert> // assert
#include <cstddef> // std::size_t
#include <filesystem> // std::filesystem
#include <optional> // std::nullopt
#include <string> // std::string, std::to_string
#include <string_view> // std::string_view
#include <utility> // std::move, std::pair
#include <vector> // std::vector

#include <sourcemeta/core/openapi.h>

Expand Down Expand Up @@ -43,3 +50,152 @@ BENCHMARK(OpenAPI_Format_Synthetic_3_1) {
sourcemeta::core::benchmark_do_not_optimize(description);
}
}

// The path count sets how many references bundling has to gather for, and the
// shared count how many distinct documents it reaches, so they are the knobs
// to turn when stress testing this functionality
static constexpr std::size_t PATH_COUNT{64};
static constexpr std::size_t SHARED_COUNT{8};
static constexpr std::size_t FIELDS_PER_SCHEMA{4};

static auto shared_identifier(const std::size_t index) -> std::string {
return "https://example.com/shared-" + std::to_string(index) + ".json";
}

static auto make_shared_schema(const std::size_t index)
-> sourcemeta::core::JSON {
auto properties{sourcemeta::core::JSON::make_object()};
for (std::size_t field = 0; field < FIELDS_PER_SCHEMA; field += 1) {
auto property{sourcemeta::core::JSON::make_object()};
property.assign("type", sourcemeta::core::JSON{"string"});
property.assign("title",
sourcemeta::core::JSON{"Field " + std::to_string(field) +
" of " + std::to_string(index)});
properties.assign("field-" + std::to_string(field), std::move(property));
}

auto schema{sourcemeta::core::JSON::make_object()};
schema.assign("type", sourcemeta::core::JSON{"object"});
schema.assign("properties", std::move(properties));
return schema;
}

static auto make_shared_document(const std::size_t index)
-> sourcemeta::core::JSON {
const auto number{std::to_string(index)};
auto schema{make_shared_schema(index)};

auto media{sourcemeta::core::JSON::make_object()};
media.assign("schema", schema);
auto content{sourcemeta::core::JSON::make_object()};
content.assign("application/json", std::move(media));
auto response{sourcemeta::core::JSON::make_object()};
response.assign("description", sourcemeta::core::JSON{"Shared " + number});
response.assign("content", std::move(content));

auto responses{sourcemeta::core::JSON::make_object()};
responses.assign("Thing", std::move(response));
auto schemas{sourcemeta::core::JSON::make_object()};
schemas.assign("Thing", std::move(schema));
auto components{sourcemeta::core::JSON::make_object()};
components.assign("responses", std::move(responses));
components.assign("schemas", std::move(schemas));

auto info{sourcemeta::core::JSON::make_object()};
info.assign("title", sourcemeta::core::JSON{"Shared " + number});
info.assign("version", sourcemeta::core::JSON{"1.0.0"});

auto document{sourcemeta::core::JSON::make_object()};
document.assign("openapi", sourcemeta::core::JSON{"3.1.1"});
document.assign("info", std::move(info));
document.assign("components", std::move(components));
return document;
}

using SharedDocument = std::pair<std::string, sourcemeta::core::JSON>;

static auto make_shared_documents() -> std::vector<SharedDocument> {
std::vector<SharedDocument> result;
result.reserve(SHARED_COUNT);
for (std::size_t index = 0; index < SHARED_COUNT; index += 1) {
result.emplace_back(shared_identifier(index), make_shared_document(index));
}
return result;
}

// The resolver below runs inside the timed loop, so each document is stored
// against the identifier it answers to rather than having one built per
// candidate on every lookup
static auto shared_documents() -> const std::vector<SharedDocument> & {
static const std::vector<SharedDocument> INSTANCE{make_shared_documents()};
return INSTANCE;
}

static auto bundle_resolver(const std::string_view identifier)
-> sourcemeta::core::OpenAPIResolverResult {
for (const auto &entry : shared_documents()) {
if (identifier == entry.first) {
return entry.second;
}
}

return std::nullopt;
}

static auto make_split_description() -> sourcemeta::core::JSON {
auto paths{sourcemeta::core::JSON::make_object()};
for (std::size_t index = 0; index < PATH_COUNT; index += 1) {
const auto shared{shared_identifier(index % SHARED_COUNT)};

auto response{sourcemeta::core::JSON::make_object()};
response.assign("$ref", sourcemeta::core::JSON{
shared + "#/components/responses/Thing"});
auto responses{sourcemeta::core::JSON::make_object()};
responses.assign("200", std::move(response));

auto schema{sourcemeta::core::JSON::make_object()};
schema.assign(
"$ref", sourcemeta::core::JSON{shared + "#/components/schemas/Thing"});
auto media{sourcemeta::core::JSON::make_object()};
media.assign("schema", std::move(schema));
auto content{sourcemeta::core::JSON::make_object()};
content.assign("application/json", std::move(media));
auto body{sourcemeta::core::JSON::make_object()};
body.assign("content", std::move(content));

auto operation{sourcemeta::core::JSON::make_object()};
operation.assign("operationId",
sourcemeta::core::JSON{"thing" + std::to_string(index)});
operation.assign("requestBody", std::move(body));
operation.assign("responses", std::move(responses));

auto item{sourcemeta::core::JSON::make_object()};
item.assign("post", std::move(operation));
paths.assign("/thing-" + std::to_string(index), std::move(item));
}

auto info{sourcemeta::core::JSON::make_object()};
info.assign("title", sourcemeta::core::JSON{"Split"});
info.assign("version", sourcemeta::core::JSON{"1.0.0"});

auto document{sourcemeta::core::JSON::make_object()};
document.assign("openapi", sourcemeta::core::JSON{"3.1.1"});
document.assign("info", std::move(info));
document.assign("paths", std::move(paths));
return document;
}

BENCHMARK(OpenAPI_Bundle_Split_3_1) {
const auto document{make_split_description()};

for (auto iteration : state) {
auto result{sourcemeta::core::openapi_bundle(
document, sourcemeta::core::schema_walker,
sourcemeta::core::schema_resolver, bundle_resolver)};
assert(result.is_object());
assert(result.at("paths").size() == PATH_COUNT);
assert(result.at("components").at("responses").size() == SHARED_COUNT);
assert(result.at("components").at("schemas").size() == SHARED_COUNT);
sourcemeta::core::benchmark_do_not_optimize(result);
}
}
92 changes: 92 additions & 0 deletions benchmark/spec/yaml.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <sourcemeta/core/benchmark.h>

#include <cassert> // assert
#include <cstddef> // std::size_t
#include <string> // std::string, std::to_string

#include <sourcemeta/core/json.h>
#include <sourcemeta/core/yaml.h>

// The record count sets how much of each construct the parser walks over, so
// it is the knob to turn when stress testing this functionality
static constexpr std::size_t RECORD_COUNT{128};
static constexpr std::size_t PARAGRAPH_COUNT{256};

// A record carries one of every shape the block parser branches on: plain,
// single quoted and double quoted scalars, a literal and a folded block
// scalar, a flow sequence, a flow mapping, and a nested block sequence of
// block mappings
static auto append_record(std::string &result, const std::size_t index)
-> void {
const auto number{std::to_string(index)};
result.append(" - name: record-");
result.append(number);
result.append("\n identifier: ");
result.append(number);
result.append("\n enabled: true\n ratio: 0.");
result.append(number);
result.append("\n title: \"Quoted title ");
result.append(number);
result.append("\"\n summary: 'Single quoted summary ");
result.append(number);
result.append("'\n description: |\n A literal block scalar for "
"record ");
result.append(number);
result.append(".\n"
" It keeps every line break it was given, and carries\n"
" on for several lines so that scanning one costs\n"
" something worth measuring.\n"
" abstract: >\n"
" A folded block scalar whose lines are joined into a\n"
" single paragraph unless one of them is indented\n"
" further than the rest.\n"
" tags: [ alpha, beta, gamma ]\n"
" limits: { soft: 10, hard: 20 }\n"
" history:\n"
" - at: 2020-01-01\n"
" note: created\n"
" - at: 2021-06-15\n"
" note: updated\n");
}

BENCHMARK(YAML_Parse_Records) {
std::string input{"records:\n"};
for (std::size_t index = 0; index < RECORD_COUNT; index += 1) {
append_record(input, index);
}

for (auto iteration : state) {
auto result{sourcemeta::core::parse_yaml(input)};
assert(result.is_object());
assert(result.at("records").size() == RECORD_COUNT);
assert(result.at("records").at(0).at("history").size() == 2);
assert(result.at("records").at(0).at("tags").size() == 3);
assert(result.at("records").at(0).at("limits").size() == 2);
sourcemeta::core::benchmark_do_not_optimize(result);
}
}

// A document that is almost entirely block scalars, so that what this measures
// is scanning them rather than the surrounding mapping
BENCHMARK(YAML_Parse_Block_Scalars) {
std::string input{"paragraphs:\n"};
for (std::size_t index = 0; index < PARAGRAPH_COUNT; index += 1) {
const auto number{std::to_string(index)};
input.append(" paragraph-")
.append(number)
.append(": |-\n The first line of paragraph ")
.append(number)
.append(".\n"
" A second line that carries on from it.\n"
" A third line, indented the same as the rest.\n"
" A fourth line indented further than the rest.\n"
" A fifth line back at the original indentation.\n");
}

for (auto iteration : state) {
auto result{sourcemeta::core::parse_yaml(input)};
assert(result.is_object());
assert(result.at("paragraphs").size() == PARAGRAPH_COUNT);
sourcemeta::core::benchmark_do_not_optimize(result);
}
}
Loading
Loading