-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve performance of rdl.Validate via validator cache (#29)
* Add specific validator tests * Add code-generator * Prepare for adding validator cache * Implement validator cache * Run benchmarks after successful build
- Loading branch information
1 parent
1b0ac88
commit b83b97e
Showing
7 changed files
with
381 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
*~ | ||
src | ||
Makefile | ||
rdl/_gen | ||
*.iml | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package codegen | ||
|
||
import ( | ||
"github.com/ardielle/ardielle-go/rdl" | ||
TestA "github.com/ardielle/ardielle-go/rdl/_gen/A" | ||
"testing" | ||
) | ||
|
||
//go:generate go run generator.go | ||
|
||
func BenchmarkA(b *testing.B) { | ||
rdl.ValidatorUseCache(false) | ||
for i := 0; i < b.N; i++ { | ||
_ = rdl.Validate(TestA.CodegenSchema(), "StringStruct", TestA.Example) | ||
} | ||
} | ||
|
||
func BenchmarkB(b *testing.B) { | ||
rdl.ValidatorUseCache(true) | ||
for i := 0; i < b.N; i++ { | ||
_ = rdl.Validate(TestA.CodegenSchema(), "StringStruct", TestA.Example) | ||
} | ||
} | ||
|
||
func TestCodeGenModel(test *testing.T) { | ||
v := rdl.Validate(TestA.CodegenSchema(), "StringStruct", TestA.Example) | ||
if !v.Valid { | ||
test.Errorf("Validation error: %v, validation", v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Stub to satisfy go 1.6 complaint about no buildable source files | ||
// | ||
// Future documentation placeholder | ||
package codegen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
// +build ignore | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
"time" | ||
) | ||
|
||
type params struct { | ||
Timestamp time.Time | ||
Name string | ||
StringTypeNames []string | ||
} | ||
|
||
const numTypes = 500 | ||
|
||
// Quick example that demonstrates a benchmark between different implementations | ||
// of Validator | ||
func main() { | ||
p := params{Timestamp: time.Now(), Name: "codegen"} | ||
for i := 0; i < numTypes; i++ { | ||
p.StringTypeNames = append(p.StringTypeNames, fmt.Sprintf("Test%03d", i)) | ||
} | ||
|
||
baseDir, err := os.Getwd() | ||
die(err) | ||
baseDir = filepath.Join(baseDir, "..") | ||
|
||
fmt.Printf("Generating test files in %v\n", filepath.Join(baseDir, "_gen")) | ||
apath := filepath.Join(baseDir, "_gen", "A") | ||
|
||
_ = os.MkdirAll(apath, 0755) | ||
|
||
instantiateTemplate(modelTemplate, p, filepath.Join(apath, "codegen_model.go")) | ||
instantiateTemplate(schemaTemplate, p, filepath.Join(apath, "codegen_schema.go")) | ||
instantiateTemplate(dataTemplate, p, filepath.Join(apath, "codegen_data.go")) | ||
} | ||
|
||
func instantiateTemplate(tmpl *template.Template, p params, fn string) { | ||
f, err := os.Create(fn) | ||
die(err) | ||
defer f.Close() | ||
die(tmpl.Execute(f, p)) | ||
} | ||
|
||
func die(err error) { | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
var funcMap = template.FuncMap{ | ||
"ToLower": strings.ToLower, | ||
"Title": strings.Title, | ||
} | ||
|
||
var modelTemplate = template.Must(template.New("").Funcs(funcMap).Parse(`// go generate | ||
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT | ||
// This file was generated by robots at | ||
// {{ .Timestamp }} | ||
package {{ .Name }} | ||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/ardielle/ardielle-go/rdl" | ||
) | ||
var _ = rdl.Version | ||
var _ = json.Marshal | ||
var _ = fmt.Printf | ||
{{ range .StringTypeNames }} | ||
type {{ . }} string | ||
{{- end }} | ||
type StringStruct struct { | ||
{{- range $index, $type := .StringTypeNames }} | ||
{{ printf "%s%d" "Name" $index }} {{ $type }} ` + "`" + `json:{{- ToLower $type | printf "%q" -}}` + "`" + ` | ||
{{- end }} | ||
} | ||
type rawStringStruct StringStruct | ||
// UnmarshalJSON | ||
func (self *StringStruct) UnmarshalJSON(b []byte) error { | ||
var r rawStringStruct | ||
err := json.Unmarshal(b, &r) | ||
if err == nil { | ||
o := StringStruct(r) | ||
*self = o | ||
err = self.Validate() | ||
} | ||
return err | ||
} | ||
// | ||
// Validate - checks for missing required fields, etc | ||
// | ||
func (self *StringStruct) Validate() error { | ||
{{- range $index, $type := .StringTypeNames }} | ||
{{- with $fieldName := printf "%s%d" "Name" $index }} | ||
if self.{{- $fieldName }} == "" { | ||
return fmt.Errorf("StringStruct.{{- $fieldName }} is missing but is a required field") | ||
} else { | ||
val := rdl.Validate( {{ Title $.Name -}}Schema(), {{ printf "%q" $type }}, self.{{- $fieldName }} ) | ||
if !val.Valid { | ||
return fmt.Errorf("StringStruct.{{ $fieldName }} does not contain a valid {{ $type }} (%v)", val.Error) | ||
} | ||
} | ||
{{- end }} | ||
{{- end }} | ||
return nil | ||
} | ||
`)) | ||
|
||
var schemaTemplate = template.Must(template.New("").Funcs(funcMap).Parse(`// go generate | ||
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT | ||
// This file was generated by robots at | ||
// {{ .Timestamp }} | ||
package {{ .Name }} | ||
import ( | ||
rdl "github.com/ardielle/ardielle-go/rdl" | ||
) | ||
var ( | ||
schema *rdl.Schema | ||
) | ||
func init() { | ||
sb := rdl.NewSchemaBuilder({{- printf "%q" .Name }}) | ||
sb.Version(1) | ||
{{ range $index, $type := .StringTypeNames }} | ||
t{{- $type }} := rdl.NewStringTypeBuilder({{ printf "%q" $type}}) | ||
t{{- $type }}.Pattern("[a-zA-Z_][a-zA-Z_0-9]*") | ||
sb.AddType(t{{- $type }}.Build()) | ||
{{ end }} | ||
tStringStruct := rdl.NewStructTypeBuilder("Struct", "StringStruct") | ||
{{- range $index, $type := .StringTypeNames }} | ||
{{- with $fieldName := printf "%s%d" "Name" $index }} | ||
tStringStruct.Field({{ printf "%q" $fieldName }}, {{ printf "%q" $type }}, false, nil, "") | ||
{{- end }} | ||
{{- end }} | ||
sb.AddType(tStringStruct.Build()) | ||
schema = sb.Build() | ||
} | ||
func {{ Title .Name -}}Schema() *rdl.Schema { | ||
return schema | ||
} | ||
`)) | ||
|
||
var dataTemplate = template.Must(template.New("").Funcs(funcMap).Parse(`// go generate | ||
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT | ||
// This file was generated by robots at | ||
// {{ .Timestamp }} | ||
package {{ .Name }} | ||
var Example = &StringStruct{ | ||
{{- range $index, $type := .StringTypeNames }} | ||
{{- with $fieldName := printf "%s%d" "Name" $index }} | ||
{{ $fieldName }}: {{ printf "%q" $type }}, | ||
{{- end }} | ||
{{- end }} | ||
} | ||
`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.