Skip to content

Commit 70922d9

Browse files
authored
Add implementation for kmp-json-schema-validator (#85)
1 parent 722645f commit 70922d9

File tree

15 files changed

+514
-0
lines changed

15 files changed

+514
-0
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ jobs:
5959
report-json_schemer: ${{ steps.save-output.outputs.report-json_schemer }}
6060
report-jsoncons: ${{ steps.save-output.outputs.report-jsoncons }}
6161
report-jsonschemadotnet: ${{ steps.save-output.outputs.report-jsonschemadotnet }}
62+
report-kmp-json-schema-validator: ${{ steps.save-output.outputs.report-kmp-json-schema-validator }}
6263
report-python-jsonschema: ${{ steps.save-output.outputs.report-python-jsonschema }}
6364
report-schemasafe: ${{ steps.save-output.outputs.report-schemasafe }}
6465

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,22 @@ dist/results/jsonschemadotnet/%: \
238238
schemas/%/instances.jsonl \
239239
| dist/results/jsonschemadotnet
240240
@$(call docker_run,jsonschemadotnet,/workspace/$(word 2,$^) /workspace/$(word 3,$^))
241+
242+
# kmp-json-schema-validator
243+
244+
implementations/kmp-json-schema-validator/.dockertimestamp: \
245+
implementations/kmp-json-schema-validator/app/src/main/kotlin/io/github/sourcemeta/App.kt \
246+
implementations/kmp-json-schema-validator/app/build.gradle.kts \
247+
implementations/kmp-json-schema-validator/gradle/libs.versions.toml \
248+
implementations/kmp-json-schema-validator/gradle/wrapper/gradle-wrapper.properties \
249+
implementations/kmp-json-schema-validator/run.sh \
250+
implementations/kmp-json-schema-validator/Dockerfile
251+
docker build -t jsonschema-benchmark/kmp-json-schema-validator implementations/kmp-json-schema-validator
252+
touch $@
253+
254+
dist/results/kmp-json-schema-validator/%: \
255+
implementations/kmp-json-schema-validator/.dockertimestamp \
256+
schemas/%/schema.json \
257+
schemas/%/instances.jsonl \
258+
| dist/results/kmp-json-schema-validator
259+
@$(call docker_run,kmp-json-schema-validator,/workspace/$(word 2,$^) /workspace/$(word 3,$^))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# Linux start script should use lf
5+
/gradlew text eol=lf
6+
7+
# These are Windows script files and should use crlf
8+
*.bat text eol=crlf
9+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore Gradle project-specific cache directory
2+
.gradle
3+
4+
# Ignore Gradle build output directory
5+
build
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# syntax=docker.io/docker/dockerfile:1.7-labs
2+
FROM gradle:8.11.1-jdk21
3+
4+
COPY --exclude=./app/src . /app
5+
6+
WORKDIR /app
7+
8+
# Just download dependencies first
9+
RUN gradle downloadDependencies
10+
11+
# Now copy in the source and compile
12+
COPY ./app/src /app/app/src
13+
RUN gradle compileKotlin
14+
15+
ENTRYPOINT ["/app/run.sh"]
16+
CMD []
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
plugins {
2+
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
3+
alias(libs.plugins.jvm)
4+
5+
// Apply the application plugin to add support for building a CLI application in Java.
6+
application
7+
}
8+
9+
repositories {
10+
// Use Maven Central for resolving dependencies.
11+
mavenCentral()
12+
}
13+
14+
dependencies {
15+
// This dependency is used by the application.
16+
implementation(libs.validator)
17+
}
18+
19+
// Apply a specific Java toolchain to ease working on different environments.
20+
java {
21+
toolchain {
22+
languageVersion = JavaLanguageVersion.of(21)
23+
}
24+
}
25+
26+
application {
27+
// Define the main class for the application.
28+
mainClass = "io.github.sourcemeta.AppKt"
29+
}
30+
31+
// See https://stackoverflow.com/a/38528497/123695
32+
fun ConfigurationContainer.resolveAll() = this
33+
.filter { it.isCanBeResolved }
34+
.forEach { it.resolve() }
35+
36+
tasks.register("downloadDependencies") {
37+
doLast {
38+
configurations.resolveAll()
39+
buildscript.configurations.resolveAll()
40+
}
41+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.github.sourcemeta
2+
3+
4+
import io.github.optimumcode.json.schema.OutputCollector
5+
import io.github.optimumcode.json.schema.JsonSchema
6+
import io.github.optimumcode.json.schema.ValidationError
7+
import java.io.File
8+
import kotlinx.serialization.json.Json
9+
import kotlinx.serialization.json.JsonElement
10+
11+
12+
val WARMUP_ITERATIONS: ULong = 1000.toULong()
13+
val MAX_WARMUP_TIME: ULong = (1e9 * 10).toULong()
14+
15+
16+
fun validateAll(schema: JsonSchema, docs: List<JsonElement>): Boolean {
17+
var valid = true
18+
for (doc in docs) {
19+
valid = valid && schema.validate(doc, OutputCollector.flag()).valid
20+
}
21+
return valid
22+
}
23+
24+
fun main(args: Array<String>) {
25+
val json = Json { ignoreUnknownKeys = true }
26+
27+
// Prepare the schema
28+
val compileStart = System.nanoTime()
29+
val schema = JsonSchema.fromDefinition(File(args[0]).readText())
30+
val compileEnd = System.nanoTime()
31+
32+
// Load all documents
33+
val docs = File(args[1]).readLines().map { json.parseToJsonElement(it) }
34+
35+
val coldStart = System.nanoTime()
36+
val valid = validateAll(schema, docs)
37+
val coldEnd = System.nanoTime()
38+
39+
if (!valid) {
40+
System.exit(1)
41+
}
42+
43+
// Run some warmup iterations
44+
val iterations: ULong = kotlin.math.ceil(MAX_WARMUP_TIME.toDouble() / (coldEnd - coldStart)).toULong()
45+
repeat(kotlin.math.min(iterations, WARMUP_ITERATIONS).toInt()) {
46+
validateAll(schema, docs)
47+
}
48+
49+
val warmStart = System.nanoTime()
50+
validateAll(schema, docs)
51+
val warmEnd = System.nanoTime()
52+
53+
println("${coldEnd - coldStart},${warmEnd - warmStart},${compileEnd - compileStart}")
54+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[versions]
2+
validator = "0.3.0"
3+
4+
[libraries]
5+
validator = { module = "io.github.optimumcode:json-schema-validator", version.ref = "validator" }
6+
7+
[plugins]
8+
jvm = { id = "org.jetbrains.kotlin.jvm", version = "1.9.22" }
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)