Skip to content

Commit

Permalink
Re-organize Google Benchmark cases
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Oct 14, 2024
1 parent 1d2aeb8 commit d666d29
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 121 deletions.
6 changes: 5 additions & 1 deletion benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ if(JSONTOOLKIT_JSON)
list(APPEND BENCHMARK_SOURCES json.cc)
endif()
if(JSONTOOLKIT_EVALUATOR AND JSONTOOLKIT_JSONSCHEMA)
list(APPEND BENCHMARK_SOURCES jsonschema_validator.cc jsonschema_compiler.cc)
list(APPEND BENCHMARK_SOURCES
compiler_draft6.cc
evaluator_draft4.cc
evaluator_draft6.cc
evaluator_draft7.cc)
endif()

if(BENCHMARK_SOURCES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <sourcemeta/jsontoolkit/json.h>
#include <sourcemeta/jsontoolkit/jsonschema.h>

static void JSONSchema_Compiler_Draft6_AdaptiveCard(benchmark::State &state) {
static void Compiler_Draft6_AdaptiveCard(benchmark::State &state) {
const auto schema{sourcemeta::jsontoolkit::from_file(
std::filesystem::path{CURRENT_DIRECTORY} / "schemas" /
"draft6_adaptivecard.json")};
Expand All @@ -20,4 +20,4 @@ static void JSONSchema_Compiler_Draft6_AdaptiveCard(benchmark::State &state) {
}
}

BENCHMARK(JSONSchema_Compiler_Draft6_AdaptiveCard);
BENCHMARK(Compiler_Draft6_AdaptiveCard);
146 changes: 28 additions & 118 deletions benchmark/jsonschema_validator.cc → benchmark/evaluator_draft4.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
#include <sourcemeta/jsontoolkit/json.h>
#include <sourcemeta/jsontoolkit/jsonschema.h>

static void
JSONSchema_Validate_Draft4_Meta_1_No_Callback(benchmark::State &state) {
static void Evaluator_Draft4_Meta_1_No_Callback(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
Expand All @@ -30,8 +29,7 @@ JSONSchema_Validate_Draft4_Meta_1_No_Callback(benchmark::State &state) {
}
}

static void
JSONSchema_Validate_Draft4_Required_Properties(benchmark::State &state) {
static void Evaluator_Draft4_Required_Properties(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -112,7 +110,7 @@ JSONSchema_Validate_Draft4_Required_Properties(benchmark::State &state) {
}
}

static void JSONSchema_Validate_Draft4_Many_Optional_Properties_Minimal_Match(
static void Evaluator_Draft4_Many_Optional_Properties_Minimal_Match(
benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
Expand Down Expand Up @@ -164,7 +162,7 @@ static void JSONSchema_Validate_Draft4_Many_Optional_Properties_Minimal_Match(
}
}

static void JSONSchema_Validate_Draft4_Few_Optional_Properties_Minimal_Match(
static void Evaluator_Draft4_Few_Optional_Properties_Minimal_Match(
benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
Expand Down Expand Up @@ -216,7 +214,7 @@ static void JSONSchema_Validate_Draft4_Few_Optional_Properties_Minimal_Match(
}
}

static void JSONSchema_Validate_Draft4_Items_Schema(benchmark::State &state) {
static void Evaluator_Draft4_Items_Schema(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -295,7 +293,7 @@ static void JSONSchema_Validate_Draft4_Items_Schema(benchmark::State &state) {
}
}

static void JSONSchema_Validate_Draft4_Nested_Object(benchmark::State &state) {
static void Evaluator_Draft4_Nested_Object(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON(
{
Expand Down Expand Up @@ -433,7 +431,7 @@ static void JSONSchema_Validate_Draft4_Nested_Object(benchmark::State &state) {
}

static void
JSONSchema_Validate_Draft4_Properties_Triad_Optional(benchmark::State &state) {
Evaluator_Draft4_Properties_Triad_Optional(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -477,7 +475,7 @@ JSONSchema_Validate_Draft4_Properties_Triad_Optional(benchmark::State &state) {
}

static void
JSONSchema_Validate_Draft4_Properties_Triad_Required(benchmark::State &state) {
Evaluator_Draft4_Properties_Triad_Required(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -521,8 +519,7 @@ JSONSchema_Validate_Draft4_Properties_Triad_Required(benchmark::State &state) {
}
}

static void
JSONSchema_Validate_Draft4_Properties_Triad_Closed(benchmark::State &state) {
static void Evaluator_Draft4_Properties_Triad_Closed(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -562,8 +559,7 @@ JSONSchema_Validate_Draft4_Properties_Triad_Closed(benchmark::State &state) {
}
}

static void
JSONSchema_Validate_Draft4_Non_Recursive_Ref(benchmark::State &state) {
static void Evaluator_Draft4_Non_Recursive_Ref(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -609,8 +605,7 @@ JSONSchema_Validate_Draft4_Non_Recursive_Ref(benchmark::State &state) {
}
}

static void
JSONSchema_Validate_Draft4_Pattern_Properties_True(benchmark::State &state) {
static void Evaluator_Draft4_Pattern_Properties_True(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -651,8 +646,7 @@ JSONSchema_Validate_Draft4_Pattern_Properties_True(benchmark::State &state) {
}
}

static void
JSONSchema_Validate_Draft4_Ref_To_Single_Property(benchmark::State &state) {
static void Evaluator_Draft4_Ref_To_Single_Property(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -712,7 +706,7 @@ JSONSchema_Validate_Draft4_Ref_To_Single_Property(benchmark::State &state) {
}

static void
JSONSchema_Validate_Draft4_Additional_Properties_Type(benchmark::State &state) {
Evaluator_Draft4_Additional_Properties_Type(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -761,7 +755,7 @@ JSONSchema_Validate_Draft4_Additional_Properties_Type(benchmark::State &state) {
}
}

static void JSONSchema_Validate_Draft4_Nested_Oneof(benchmark::State &state) {
static void Evaluator_Draft4_Nested_Oneof(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -803,101 +797,17 @@ static void JSONSchema_Validate_Draft4_Nested_Oneof(benchmark::State &state) {
}
}

static void JSONSchema_Validate_Draft6_Property_Names(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-06/schema#",
"propertyNames": {
"minLength": 2
}
})JSON")};

const sourcemeta::jsontoolkit::JSON instance{
sourcemeta::jsontoolkit::parse(R"JSON({
"aa": true,
"bb": true,
"cc": true,
"dd": true,
"ee": true,
"ff": true,
"gg": true,
"hh": true,
"ii": true,
"jj": true,
"kk": true,
"ll": true,
"mm": true,
"nn": true,
"oo": true,
"pp": true,
"qq": true,
"rr": true,
"ss": true,
"tt": true,
"uu": true,
"vv": true,
"ww": true,
"xx": true,
"yy": true,
"zz": true
})JSON")};

const auto schema_template{sourcemeta::jsontoolkit::compile(
schema, sourcemeta::jsontoolkit::default_schema_walker,
sourcemeta::jsontoolkit::official_resolver,
sourcemeta::jsontoolkit::default_schema_compiler)};
for (auto _ : state) {
auto result{sourcemeta::jsontoolkit::evaluate(schema_template, instance)};
assert(result);
benchmark::DoNotOptimize(result);
}
}

static void JSONSchema_Validate_Draft7_If_Then_Else(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#",
"if": {
"type": "string"
},
"then": {
"if": { "minLength": 2 },
"then": { "maxLength": 5 },
"else": { "maxLength": 3 }
},
"else": {
"if": { "type": "integer" },
"then": { "minimum": 4 },
"else": { "maxProperties": 4 }
}
})JSON")};

const sourcemeta::jsontoolkit::JSON instance{"foo"};

const auto schema_template{sourcemeta::jsontoolkit::compile(
schema, sourcemeta::jsontoolkit::default_schema_walker,
sourcemeta::jsontoolkit::official_resolver,
sourcemeta::jsontoolkit::default_schema_compiler)};
for (auto _ : state) {
auto result{sourcemeta::jsontoolkit::evaluate(schema_template, instance)};
assert(result);
benchmark::DoNotOptimize(result);
}
}

BENCHMARK(JSONSchema_Validate_Draft4_Meta_1_No_Callback);
BENCHMARK(JSONSchema_Validate_Draft4_Required_Properties);
BENCHMARK(JSONSchema_Validate_Draft4_Many_Optional_Properties_Minimal_Match);
BENCHMARK(JSONSchema_Validate_Draft4_Few_Optional_Properties_Minimal_Match);
BENCHMARK(JSONSchema_Validate_Draft4_Items_Schema);
BENCHMARK(JSONSchema_Validate_Draft4_Nested_Object);
BENCHMARK(JSONSchema_Validate_Draft4_Properties_Triad_Optional);
BENCHMARK(JSONSchema_Validate_Draft4_Properties_Triad_Closed);
BENCHMARK(JSONSchema_Validate_Draft4_Properties_Triad_Required);
BENCHMARK(JSONSchema_Validate_Draft4_Non_Recursive_Ref);
BENCHMARK(JSONSchema_Validate_Draft4_Pattern_Properties_True);
BENCHMARK(JSONSchema_Validate_Draft4_Ref_To_Single_Property);
BENCHMARK(JSONSchema_Validate_Draft4_Additional_Properties_Type);
BENCHMARK(JSONSchema_Validate_Draft4_Nested_Oneof);
BENCHMARK(JSONSchema_Validate_Draft6_Property_Names);
BENCHMARK(JSONSchema_Validate_Draft7_If_Then_Else);
BENCHMARK(Evaluator_Draft4_Meta_1_No_Callback);
BENCHMARK(Evaluator_Draft4_Required_Properties);
BENCHMARK(Evaluator_Draft4_Many_Optional_Properties_Minimal_Match);
BENCHMARK(Evaluator_Draft4_Few_Optional_Properties_Minimal_Match);
BENCHMARK(Evaluator_Draft4_Items_Schema);
BENCHMARK(Evaluator_Draft4_Nested_Object);
BENCHMARK(Evaluator_Draft4_Properties_Triad_Optional);
BENCHMARK(Evaluator_Draft4_Properties_Triad_Closed);
BENCHMARK(Evaluator_Draft4_Properties_Triad_Required);
BENCHMARK(Evaluator_Draft4_Non_Recursive_Ref);
BENCHMARK(Evaluator_Draft4_Pattern_Properties_True);
BENCHMARK(Evaluator_Draft4_Ref_To_Single_Property);
BENCHMARK(Evaluator_Draft4_Additional_Properties_Type);
BENCHMARK(Evaluator_Draft4_Nested_Oneof);
58 changes: 58 additions & 0 deletions benchmark/evaluator_draft6.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <benchmark/benchmark.h>

#include <cassert> // assert

#include <sourcemeta/jsontoolkit/json.h>
#include <sourcemeta/jsontoolkit/jsonschema.h>

static void Evaluator_Draft6_Property_Names(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-06/schema#",
"propertyNames": {
"minLength": 2
}
})JSON")};

const sourcemeta::jsontoolkit::JSON instance{
sourcemeta::jsontoolkit::parse(R"JSON({
"aa": true,
"bb": true,
"cc": true,
"dd": true,
"ee": true,
"ff": true,
"gg": true,
"hh": true,
"ii": true,
"jj": true,
"kk": true,
"ll": true,
"mm": true,
"nn": true,
"oo": true,
"pp": true,
"qq": true,
"rr": true,
"ss": true,
"tt": true,
"uu": true,
"vv": true,
"ww": true,
"xx": true,
"yy": true,
"zz": true
})JSON")};

const auto schema_template{sourcemeta::jsontoolkit::compile(
schema, sourcemeta::jsontoolkit::default_schema_walker,
sourcemeta::jsontoolkit::official_resolver,
sourcemeta::jsontoolkit::default_schema_compiler)};
for (auto _ : state) {
auto result{sourcemeta::jsontoolkit::evaluate(schema_template, instance)};
assert(result);
benchmark::DoNotOptimize(result);
}
}

BENCHMARK(Evaluator_Draft6_Property_Names);
40 changes: 40 additions & 0 deletions benchmark/evaluator_draft7.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <benchmark/benchmark.h>

#include <cassert> // assert

#include <sourcemeta/jsontoolkit/json.h>
#include <sourcemeta/jsontoolkit/jsonschema.h>

static void Evaluator_Draft7_If_Then_Else(benchmark::State &state) {
const sourcemeta::jsontoolkit::JSON schema{
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#",
"if": {
"type": "string"
},
"then": {
"if": { "minLength": 2 },
"then": { "maxLength": 5 },
"else": { "maxLength": 3 }
},
"else": {
"if": { "type": "integer" },
"then": { "minimum": 4 },
"else": { "maxProperties": 4 }
}
})JSON")};

const sourcemeta::jsontoolkit::JSON instance{"foo"};

const auto schema_template{sourcemeta::jsontoolkit::compile(
schema, sourcemeta::jsontoolkit::default_schema_walker,
sourcemeta::jsontoolkit::official_resolver,
sourcemeta::jsontoolkit::default_schema_compiler)};
for (auto _ : state) {
auto result{sourcemeta::jsontoolkit::evaluate(schema_template, instance)};
assert(result);
benchmark::DoNotOptimize(result);
}
}

BENCHMARK(Evaluator_Draft7_If_Then_Else);

0 comments on commit d666d29

Please sign in to comment.