Skip to content

Instantly share code, notes, and snippets.

@yaliv
Last active November 8, 2020 16:06
Show Gist options
  • Save yaliv/0f1f0efaddbccf70acc70ad9f0826848 to your computer and use it in GitHub Desktop.
Save yaliv/0f1f0efaddbccf70acc70ad9f0826848 to your computer and use it in GitHub Desktop.
Benchmark gojsonschema validation in parallel
package jscvalidator
import (
"testing"
"github.com/xeipuuv/gojsonschema"
)
const (
passwordSchema = `{
"type": "object",
"required": ["oldPassword", "newPassword"],
"properties": {
"oldPassword": {
"type": "string",
"minLength": 1
},
"newPassword": {
"type": "string",
"minLength": 1
}
}
}`
changePasswordBody = `{
"oldPassword": "12QWaszx",
"newPassword": "omkopi"
}`
)
func BenchmarkValidate(b *testing.B) {
schema, _ := gojsonschema.NewSchema(gojsonschema.NewStringLoader(passwordSchema))
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
schema.Validate(gojsonschema.NewStringLoader(changePasswordBody))
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment