Skip to content

Commit

Permalink
benchmark: add go jsonschema (#31)
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Gorez <[email protected]>
  • Loading branch information
tony-go authored Sep 4, 2024
1 parent be21eac commit 2c27c17
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ dist/results/json_schemer/%: \
| dist/results/json_schemer
docker run -v $(CURDIR):/workspace jsonschema-benchmark/json_schemer /workspace/$(dir $(word 3,$^)) > $@

# JSONSCHEMA
# PYTHON / JSONSCHEMA

implementations/python-jsonschema/.dockertimestamp: \
implementations/python-jsonschema/validate.py \
Expand All @@ -109,3 +109,20 @@ dist/results/python-jsonschema/%: \
schemas/%/instances.jsonl \
| dist/results/python-jsonschema
docker run -v $(CURDIR):/workspace jsonschema-benchmark/python-jsonschema /workspace/$(dir $(word 2,$^)) > $@

# GO / JSONSCHEMA

implementations/go-jsonschema/.dockertimestamp: \
implementations/go-jsonschema/go.mod \
implementations/go-jsonschema/go.sum \
implementations/go-jsonschema/main.go \
implementations/go-jsonschema/Dockerfile
docker build -t jsonschema-benchmark/go-jsonschema implementations/go-jsonschema
touch $@

dist/results/go-jsonschema/%: \
implementations/go-jsonschema/.dockertimestamp \
schemas/%/schema.json \
schemas/%/instances.jsonl \
| dist/results/go-jsonschema
docker run -v $(CURDIR):/workspace jsonschema-benchmark/go-jsonschema /workspace/$(dir $(word 2,$^)) > $@
19 changes: 19 additions & 0 deletions implementations/go-jsonschema/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Use the official Golang image as a parent image
FROM golang:1.23-alpine

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container
COPY . .

# Download dependencies
RUN go mod download

# Build the Go app
RUN go build -o go_jsonschema_validator .

# Run the binary
ENTRYPOINT ["/app/go_jsonschema_validator"]
CMD []

7 changes: 7 additions & 0 deletions implementations/go-jsonschema/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module implementations/go-jsonschema

go 1.23.0

require github.com/santhosh-tekuri/jsonschema/v6 v6.0.1

require golang.org/x/text v0.14.0 // indirect
6 changes: 6 additions & 0 deletions implementations/go-jsonschema/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 h1:PKK9DyHxif4LZo+uQSgXNqs0jj5+xZwwfKHgph2lxBw=
github.com/santhosh-tekuri/jsonschema/v6 v6.0.1/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
80 changes: 80 additions & 0 deletions implementations/go-jsonschema/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"time"
"encoding/json"
"io"

"github.com/santhosh-tekuri/jsonschema/v6"
)

func main() {
if len(os.Args) < 2 {
log.Fatal("Please provide the example folder path as an argument")
}

exampleFolder := os.Args[1]

// Construct and canonicalize file paths
schemaFile, err := filepath.Abs(filepath.Join(exampleFolder, "schema.json"))
if err != nil {
log.Fatalf("Error constructing schema file path: %v", err)
}

instanceFile, err := filepath.Abs(filepath.Join(exampleFolder, "instances.jsonl"))
if err != nil {
log.Fatalf("Error constructing instance file path: %v", err)
}

// Compile the JSON schema
c := jsonschema.NewCompiler()
sch, err := c.Compile(schemaFile)
if err != nil {
log.Fatal(err)
}

// Open the JSONL file
f, err := os.Open(instanceFile)
if err != nil {
log.Fatal(err)
}
defer f.Close()


// Step 1: Decode and store JSON objects
var instances []interface{}
reader := bufio.NewReader(f)
decoder := json.NewDecoder(reader)

for {
var inst interface{}
if err := decoder.Decode(&inst); err != nil {
if err == io.EOF {
break
}
log.Fatalf("Error decoding JSON: %v", err)
}
instances = append(instances, inst)
}

start := time.Now()

// Step 2: Validation loop
for _, inst := range instances {
err = sch.Validate(inst)
if err != nil {
log.Fatalf("Validation failed: %v", err)
}
}

// Stop timer and calculate duration
duration := time.Since(start)

// Print timing
fmt.Printf("%d\n", duration.Nanoseconds())
}
11 changes: 11 additions & 0 deletions implementations/go-jsonschema/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#!/bin/bash

set -o errexit
set -o nounset

# Extract the version of the jsonschema module from go.mod
jsonschema_version=$(grep 'github.com/santhosh-tekuri/jsonschema' implementations/go-jsonschema/go.mod | sed -E 's/.*\/jsonschema\/v[0-9]+ v([0-9\.]+)/\1/')

# Output the version
echo "$jsonschema_version"

0 comments on commit 2c27c17

Please sign in to comment.