Skip to content
This repository has been archived by the owner on Dec 21, 2019. It is now read-only.

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

Closed
henrikp-visma opened this issue Feb 14, 2017 · 1 comment

Comments

@henrikp-visma
Copy link

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
@edgarmueller
Copy link
Contributor

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.

edgarmueller added a commit to edgarmueller/play-json-schema-validator that referenced this issue Feb 21, 2017
edgarmueller added a commit that referenced this issue Feb 21, 2017
Fix for issue #103: schema passed as URL argument is not cached
edgarmueller added a commit to edgarmueller/play-json-schema-validator that referenced this issue Mar 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants