eclipsesource/play-json-schema-validator

Validating instances by passing the schema url to the validator is slower than passing it the preloaded schema

Closed this issue · 1 comments

Validating instances with validator.validate(schemaUrl, instance) is measurably slower than validator.validate(schema, instance), where schema is the preloaded schema.

This can be measured with the following setup (pseudo code).

/* resources/test.schema.json */
{ "type": "object",
  "properties": {
    "mything": {"$ref": "#thing"}
  },
  "definitions": {
    "thing": {
      "id": "#thing",
      "type": "string"
    }
  }
}
def timed(name: String)(body: => Unit) {
  val start = System.currentTimeMillis();
  body;
  println(name + ": " + (System.currentTimeMillis() - start) + " ms")
}

val validator = SchemaValidator()
val schemaUrl = getClass.getResource("test.schema.json")
val schema = JsonSource.schemaFromUrl(schemaUrl).get

val instance = Json.parse(
      """{
        |"mything": "the thing"
        |}""".stripMargin)

timed("preloaded") {
  for (_ <- 1000) validator.validate(schema, instance)
}
timed("url based") {
  for (_ <- 1000) validator.validate(schemaUrl, instance)
}

An example result would be

preloaded: 260 ms
url based: 855 ms

Thanks for the report. I'll tyr to check whether that performance hit is solely due the URL resolution or whether there's something else.