Skip to content

Commit 5e9ff7b

Browse files
smyrmanrs
authored andcommitted
jsonschema: Encoder benchmarks (#66)
Added some benchmarks to test number of allocation / performance for the public API.
1 parent 5902268 commit 5e9ff7b

File tree

2 files changed

+127
-67
lines changed

2 files changed

+127
-67
lines changed

schema/encoding/jsonschema/all_test.go

Lines changed: 73 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,76 @@ import (
1212
"github.com/stretchr/testify/assert"
1313
)
1414

15+
// studentSchema serves as a complex nested schema example.
16+
var studentSchema = schema.Schema{
17+
Description: "Object with array of students",
18+
Fields: schema.Fields{
19+
"students": {
20+
Description: "Array of students",
21+
Validator: &schema.Array{
22+
ValuesValidator: &schema.Object{
23+
Schema: &schema.Schema{
24+
Description: "Student and class",
25+
Fields: schema.Fields{
26+
"student": {
27+
Description: "The student name",
28+
Required: true,
29+
Default: "Unknown",
30+
Validator: &schema.String{
31+
MinLen: 1,
32+
MaxLen: 10,
33+
},
34+
},
35+
"class": {
36+
Description: "The class name",
37+
Default: "Unassigned",
38+
Validator: &schema.String{
39+
MinLen: 0, // Default value.
40+
MaxLen: 10,
41+
},
42+
},
43+
},
44+
},
45+
},
46+
},
47+
},
48+
},
49+
}
50+
51+
// studentSchemaJSON contains the expected JSON serialization of studentSchema.
52+
const studentSchemaJSON = `{
53+
"type": "object",
54+
"description": "Object with array of students",
55+
"additionalProperties": false,
56+
"properties": {
57+
"students": {
58+
"type": "array",
59+
"description": "Array of students",
60+
"items": {
61+
"type": "object",
62+
"description": "Student and class",
63+
"additionalProperties": false,
64+
"properties": {
65+
"student": {
66+
"type": "string",
67+
"description": "The student name",
68+
"default": "Unknown",
69+
"minLength": 1,
70+
"maxLength": 10
71+
},
72+
"class": {
73+
"type": "string",
74+
"description": "The class name",
75+
"default": "Unassigned",
76+
"maxLength": 10
77+
}
78+
},
79+
"required": ["student"]
80+
}
81+
}
82+
}
83+
}`
84+
1585
type dummyValidator struct{}
1686

1787
func (v dummyValidator) Validate(value interface{}) (interface{}, error) {
@@ -340,73 +410,9 @@ func TestEncoder(t *testing.T) {
340410
}`,
341411
},
342412
{
343-
name: "Validator=Array,ValuesValidator=Object{Schema:Student}",
344-
schema: schema.Schema{
345-
Description: "Object with array of students",
346-
Fields: schema.Fields{
347-
"students": {
348-
Description: "Array of students",
349-
Validator: &schema.Array{
350-
ValuesValidator: &schema.Object{
351-
Schema: &schema.Schema{
352-
Description: "Student and class",
353-
Fields: schema.Fields{
354-
"student": {
355-
Description: "The student name",
356-
Required: true,
357-
Default: "Unknown",
358-
Validator: &schema.String{
359-
MinLen: 1,
360-
MaxLen: 10,
361-
},
362-
},
363-
"class": {
364-
Description: "The class name",
365-
Default: "Unassigned",
366-
Validator: &schema.String{
367-
MinLen: 0, // Default value.
368-
MaxLen: 10,
369-
},
370-
},
371-
},
372-
},
373-
},
374-
},
375-
},
376-
},
377-
},
378-
expect: `{
379-
"type": "object",
380-
"description": "Object with array of students",
381-
"additionalProperties": false,
382-
"properties": {
383-
"students": {
384-
"type": "array",
385-
"description": "Array of students",
386-
"items": {
387-
"type": "object",
388-
"description": "Student and class",
389-
"additionalProperties": false,
390-
"properties": {
391-
"student": {
392-
"type": "string",
393-
"description": "The student name",
394-
"default": "Unknown",
395-
"minLength": 1,
396-
"maxLength": 10
397-
},
398-
"class": {
399-
"type": "string",
400-
"description": "The class name",
401-
"default": "Unassigned",
402-
"maxLength": 10
403-
}
404-
},
405-
"required": ["student"]
406-
}
407-
}
408-
}
409-
}`,
413+
name: "Validator=Array,ValuesValidator=Object{Schema:Student}",
414+
schema: studentSchema,
415+
expect: studentSchemaJSON,
410416
},
411417
{
412418
name: `Validator=Object,Fields["location"].Validator=Object`,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package jsonschema_test
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/rs/rest-layer/schema"
8+
"github.com/rs/rest-layer/schema/encoding/jsonschema"
9+
)
10+
11+
func BenchmarkEncoder(b *testing.B) {
12+
testCases := []struct {
13+
Name string
14+
Schema schema.Schema
15+
}{
16+
{
17+
Name: `Schema={Fields:{"b":Bool{}}}`,
18+
Schema: schema.Schema{
19+
Fields: schema.Fields{
20+
"b": {Validator: &schema.Bool{}},
21+
},
22+
},
23+
},
24+
{
25+
Name: `Schema={Fields:{"s":String{}}}`,
26+
Schema: schema.Schema{
27+
Fields: schema.Fields{
28+
"s": {Validator: &schema.String{}},
29+
},
30+
},
31+
},
32+
{
33+
Name: `Schema={Fields:{"s":String{MaxLen:42}}}`,
34+
Schema: schema.Schema{
35+
Fields: schema.Fields{
36+
"s": {Validator: &schema.String{MaxLen: 42}},
37+
},
38+
},
39+
},
40+
{
41+
Name: `Schema=Student`,
42+
Schema: studentSchema,
43+
},
44+
}
45+
for i := range testCases {
46+
tc := testCases[i]
47+
b.Run(tc.Name, func(b *testing.B) {
48+
for i := 0; i < b.N; i++ {
49+
enc := jsonschema.NewEncoder(new(bytes.Buffer))
50+
enc.Encode(&tc.Schema)
51+
}
52+
})
53+
}
54+
}

0 commit comments

Comments
 (0)