Skip to content

Commit

Permalink
Add hyperjump implementation (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmior authored Oct 2, 2024
1 parent 6f5e4c8 commit 097701a
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,21 @@ dist/results/go-jsonschema/%: \
schemas/%/instances.jsonl \
| dist/results/go-jsonschema
@$(call docker_run,go-jsonschema,/workspace/$(dir $(word 2,$^)))

# HYPERJUMP

implementations/hyperjump/.dockertimestamp: \
implementations/hyperjump/main.mjs \
implementations/hyperjump/package.json \
implementations/hyperjump/package-lock.json \
implementations/hyperjump/Dockerfile
docker build -t jsonschema-benchmark/hyperjump implementations/hyperjump
touch $@

dist/results/hyperjump/%: \
implementations/hyperjump/.dockertimestamp \
schemas/%/schema.json \
schemas/%/instances.jsonl \
| dist/results/hyperjump
@$(call docker_run,hyperjump,/workspace/$(word 2,$^) /workspace/$(word 3,$^))

6 changes: 6 additions & 0 deletions implementations/hyperjump/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM node:22-alpine

COPY . /app
RUN npm ci --prefix /app
ENTRYPOINT ["node", "--disable-warning=ExperimentalWarning", "/app/main.mjs"]
CMD []
61 changes: 61 additions & 0 deletions implementations/hyperjump/main.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { registerSchema, validate } from "@hyperjump/json-schema/draft-07";
import fs from 'fs';
import readline from 'readline';
import { performance } from 'perf_hooks';

function readJSONFile(filePath) {
try {
const fileContent = fs.readFileSync(filePath, 'utf8');
return JSON.parse(fileContent);
} catch (error) {
process.exit(1);
}
}

async function* readJSONLines(filePath) {
const rl = readline.createInterface({
input: fs.createReadStream(filePath),
});
for await (const line of rl) {
yield JSON.parse(line);
}
}

async function validateSchema(schemaPath, instancePath) {
const schema = readJSONFile(schemaPath);

const schemaId = schema["$id"] || "https://example.com" + schemaPath;
registerSchema(schema, schemaId);

const instances = [];
for await (const instance of readJSONLines(instancePath)) {
instances.push(instance);
}
let failed = false;
const startTime = performance.now();
for (const instance of instances) {
const output = await validate(schemaId, instance);
if (!output.valid) {
failed = true;
}
}

const endTime = performance.now();

const durationNs = (endTime - startTime) * 1e6;
console.log(durationNs.toFixed(0));

// Exit with non-zero status on validation failure
if (failed) {
process.exit(1);
}
}

if (process.argv.length !== 4) {
process.exit(1);
}

const schemaPath = process.argv[2];
const instancePath = process.argv[3];

await validateSchema(schemaPath, instancePath);
127 changes: 127 additions & 0 deletions implementations/hyperjump/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions implementations/hyperjump/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "jsonschema-benchmark",
"version": "1.0.0",
"author": "Juan Cruz Viotti <[email protected]>",
"dependencies": {
"@hyperjump/json-schema": "^1.9.8"
}
}
6 changes: 6 additions & 0 deletions implementations/hyperjump/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

set -o errexit
set -o nounset

jq --raw-output '.packages["node_modules/ajv"].version' < implementations/ajv/package-lock.json

0 comments on commit 097701a

Please sign in to comment.