diff --git a/perf-helpers/.gitignore b/perf-helpers/.gitignore new file mode 100644 index 0000000..3605c3e --- /dev/null +++ b/perf-helpers/.gitignore @@ -0,0 +1,3 @@ +/Cargo.lock +/target +/trace.svg diff --git a/perf-helpers/Cargo.toml b/perf-helpers/Cargo.toml new file mode 100644 index 0000000..458a3d5 --- /dev/null +++ b/perf-helpers/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "perf_helpers" +version = "0.1.0" +authors = ["Samuele Maci "] +edition = "2018" +publish = false + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[dependencies] +jsonschema = { path = "../" } +serde_json = "1" + +[profile.release] +debug = true diff --git a/perf-helpers/run b/perf-helpers/run new file mode 100755 index 0000000..fc2f69f --- /dev/null +++ b/perf-helpers/run @@ -0,0 +1,31 @@ +#!/bin/sh +set -euo pipefail +CURDIR=$(pwd) +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + +if [ $# -lt 2 ]; then + echo "Usage: $0 []" > /dev/stderr + exit 1 +fi + +export SCHEMA_PATH="$(realpath $1)" +export INSTANCE_PATH="$(realpath $2)" +export NUMBER_OF_ITERATIONS="${3:-1234}" + +# Ensure that flamegraph is installed +cargo flamegraph -h > /dev/null || cargo install flamegraph + +# Clear files before starting +rm -rf trace.svg + +# Move into perf-helpers package +pushd ${DIR} + +# Ensure that a new build is re-issued as the content of the +# files and/or CLI arguments (env variables) might change +cargo clean --release --package perf_helpers + +cargo flamegraph --root --output ${CURDIR}/trace.svg + +# Exits perf-helpers +popd diff --git a/perf-helpers/src/main.rs b/perf-helpers/src/main.rs new file mode 100644 index 0000000..ee6960e --- /dev/null +++ b/perf-helpers/src/main.rs @@ -0,0 +1,24 @@ +use jsonschema::JSONSchema; +use serde_json::{from_str, Error, Value}; + +/// This executable is supposed to be used via ../run sh scrpt +/// The script does trigger the build of the binary and is responsible +/// injects for injecting the needed environmental variables +fn main() -> Result<(), Error> { + let schema: Value = from_str(include_str!(env!("SCHEMA_PATH")))?; + let instance: Value = from_str(include_str!(env!("INSTANCE_PATH")))?; + let number_of_iterations: usize = env!("NUMBER_OF_ITERATIONS") + .parse() + .expect("NUMBER_OF_ITERATIONS is expected to be a positive integer"); + + eprintln!("Schema {}", schema); + eprintln!("Instance {}", instance); + eprintln!("Number of Iterations {}", number_of_iterations); + + let compiled = JSONSchema::compile(&schema, None).unwrap(); + for _ in 0..number_of_iterations { + compiled.is_valid(&instance); + } + + Ok(()) +}