Skip to content

Commit

Permalink
Show validate timing with a --benchmark option (#133)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Jul 24, 2024
1 parent c0421d4 commit 7139df4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
8 changes: 7 additions & 1 deletion docs/validate.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Validating
```sh
jsonschema validate <schema.json> <instance.json|.jsonl> [--http/-h]
[--verbose/-v] [--resolve/-r <schemas-or-directories> ...]
[--verbose/-v] [--resolve/-r <schemas-or-directories> ...] [--benchmark/-b]
```

The most popular use case of JSON Schema is to validate JSON documents. The
Expand Down Expand Up @@ -80,3 +80,9 @@ jsonschema validate path/to/my/schema.json path/to/my/instance.json \
jsonschema validate path/to/my/schema.json path/to/my/instance.json \
--resolve path/to/schemas --extension schema.json
```

### Validate a JSON instance against a schema printing timing information

```sh
jsonschema validate path/to/my/schema.json path/to/my/instance.json --benchmark
```
56 changes: 44 additions & 12 deletions src/command_validate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <sourcemeta/jsontoolkit/jsonl.h>
#include <sourcemeta/jsontoolkit/jsonschema.h>

#include <chrono> // std::chrono
#include <cstdlib> // EXIT_SUCCESS, EXIT_FAILURE
#include <iostream> // std::cerr
#include <set> // std::set
Expand All @@ -15,7 +16,7 @@
// TODO: Add a flag to take a pre-compiled schema as input
auto intelligence::jsonschema::cli::validate(
const std::span<const std::string> &arguments) -> int {
const auto options{parse_options(arguments, {"h", "http"})};
const auto options{parse_options(arguments, {"h", "http", "b", "benchmark"})};

if (options.at("").size() < 1) {
std::cerr
Expand Down Expand Up @@ -52,6 +53,8 @@ auto intelligence::jsonschema::cli::validate(
schema, sourcemeta::jsontoolkit::default_schema_walker, custom_resolver,
sourcemeta::jsontoolkit::default_schema_compiler)};

const auto benchmark{options.contains("b") || options.contains("benchmark")};

if (instance_path.extension() == ".jsonl") {
log_verbose(options) << "Interpreting input as JSONL\n";
std::size_t index{0};
Expand All @@ -60,11 +63,27 @@ auto intelligence::jsonschema::cli::validate(
try {
for (const auto &instance : sourcemeta::jsontoolkit::JSONL{stream}) {
std::ostringstream error;
const auto subresult = sourcemeta::jsontoolkit::evaluate(
schema_template, instance,
sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast,
pretty_evaluate_callback(error,
sourcemeta::jsontoolkit::empty_pointer));
bool subresult = true;
if (benchmark) {
const auto timestamp_start{std::chrono::high_resolution_clock::now()};
subresult =
sourcemeta::jsontoolkit::evaluate(schema_template, instance);
const auto timestamp_end{std::chrono::high_resolution_clock::now()};
const auto duration_us{
std::chrono::duration_cast<std::chrono::microseconds>(
timestamp_end - timestamp_start)};
if (subresult) {
std::cout << "took: " << duration_us.count() << "us\n";
} else {
error << "error: Schema validation failure\n";
}
} else {
subresult = sourcemeta::jsontoolkit::evaluate(
schema_template, instance,
sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast,
pretty_evaluate_callback(error,
sourcemeta::jsontoolkit::empty_pointer));
}

if (subresult) {
log_verbose(options)
Expand Down Expand Up @@ -93,13 +112,26 @@ auto intelligence::jsonschema::cli::validate(
}
} else {
const auto instance{sourcemeta::jsontoolkit::from_file(instance_path)};

std::ostringstream error;
result = sourcemeta::jsontoolkit::evaluate(
schema_template, instance,
sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast,
pretty_evaluate_callback(error,
sourcemeta::jsontoolkit::empty_pointer));
if (benchmark) {
const auto timestamp_start{std::chrono::high_resolution_clock::now()};
result = sourcemeta::jsontoolkit::evaluate(schema_template, instance);
const auto timestamp_end{std::chrono::high_resolution_clock::now()};
const auto duration_us{
std::chrono::duration_cast<std::chrono::microseconds>(
timestamp_end - timestamp_start)};
if (result) {
std::cout << "took: " << duration_us.count() << "us\n";
} else {
error << "error: Schema validation failure\n";
}
} else {
result = sourcemeta::jsontoolkit::evaluate(
schema_template, instance,
sourcemeta::jsontoolkit::SchemaCompilerEvaluationMode::Fast,
pretty_evaluate_callback(error,
sourcemeta::jsontoolkit::empty_pointer));
}

if (result) {
log_verbose(options)
Expand Down
2 changes: 1 addition & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Global Options:
Commands:
validate <schema.json> <instance.json|.jsonl> [--http/-h]
validate <schema.json> <instance.json|.jsonl> [--http/-h] [--benchmark/-b]
Validate an instance against the given schema.
Expand Down

0 comments on commit 7139df4

Please sign in to comment.