-
Notifications
You must be signed in to change notification settings - Fork 2
TestGen generating performance comparative tests (#93) #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
611b33b
chore: remove useless code
alexgarzao e324603
feat: testgen building end-to-end tests
alexgarzao 2cdca99
chore: turn public GenValidations api
alexgarzao c2142d2
feat: testgen bulding validation code tests
alexgarzao f6bdb38
test: add generated validation code test files
alexgarzao 581fa9a
chore: makefile rule to build generated tests and move to the correct
alexgarzao 98b323a
chore: add DO NOT EDIT to all templates
alexgarzao 5591942
chore: fix DO NOT EDIT header
alexgarzao 47d14f6
chore: better func name
alexgarzao 64050fa
doc: initial testgen readme
alexgarzao 2ee054c
chore: remove useless code
alexgarzao abc9fe5
feat: testgen generating tests to check function validate output
alexgarzao f06c1cb
test: commit tests generated by testgen
alexgarzao 7bb3ad6
Merge branch 'main' into 93-testgen-function-code-tests
alexgarzao e0325e6
chore: remove use slice[n] to avoid panic
alexgarzao 4f9fa12
chore: better way to chain calls in the makefile
alexgarzao 427754d
chore: use bitfield to exclude tests to be generated
alexgarzao b205a3a
chore: add govalidator tag to generated cmp perf tests
alexgarzao 8a8c8f8
feat: generate perf cmp tests in testgen
alexgarzao c09649e
chore: add generated perf tests
alexgarzao 8b5e2b3
Merge branch 'main' into 93-testgen-generating-cmp-perf-tests
alexgarzao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,48 @@ | ||
| // Code generated by TestGen. DO NOT EDIT. | ||
|
|
||
| package benchtests | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/go-playground/validator/v10" | ||
| ) | ||
|
|
||
| {{range .Tests}} | ||
| type ValidGen{{.TestName}}Struct struct { | ||
| Field {{.FieldType}} `valid:"{{.ValidGenTag}}"` | ||
| } | ||
|
|
||
| type Validator{{.TestName}}Struct struct { | ||
| Field {{.FieldType}} `validate:"{{.ValidatorTag}}"` | ||
| } | ||
| {{end}} | ||
|
|
||
| {{range .Tests}} | ||
| func BenchmarkValidGen{{.TestName}}(b *testing.B) { | ||
| data := &ValidGen{{.TestName}}Struct{ | ||
| Field: {{.ValidInput}}, | ||
| } | ||
|
|
||
| for b.Loop() { | ||
| if err := ValidGen{{.TestName}}StructValidate(data); len(err) > 0 { | ||
| b.FailNow() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkValidator{{.TestName}}(b *testing.B) { | ||
| var validate *validator.Validate | ||
|
|
||
| validate = validator.New(validator.WithRequiredStructEnabled()) | ||
| data := &Validator{{.TestName}}Struct{ | ||
| Field: {{.ValidInput}}, | ||
| } | ||
|
|
||
| for b.Loop() { | ||
alexgarzao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err := validate.Struct(data); err != nil { | ||
| b.FailNow() | ||
| } | ||
| } | ||
| } | ||
| {{end}} | ||
This file contains hidden or 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,52 @@ | ||
| // Code generated by TestGen. DO NOT EDIT. | ||
|
|
||
| package benchtests | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/go-playground/validator/v10" | ||
| ) | ||
|
|
||
| {{range .Tests}} | ||
| type ValidGen{{.TestName}}Struct struct { | ||
| Field {{.FieldType}} `valid:"{{.ValidGenTag}}"` | ||
| } | ||
|
|
||
| type Validator{{.TestName}}Struct struct { | ||
| Field {{.FieldType}} `validate:"{{.ValidatorTag}}"` | ||
| } | ||
| {{end}} | ||
|
|
||
| {{range .Tests}} | ||
| func BenchmarkValidGen{{.TestName}}(b *testing.B) { | ||
| var validInput {{.BasicType}} = {{.ValidInput}} | ||
| data := &ValidGen{{.TestName}}Struct{ | ||
| Field: &validInput, | ||
| } | ||
|
|
||
| for b.Loop() { | ||
alexgarzao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err := ValidGen{{.TestName}}StructValidate(data); len(err) > 0 { | ||
| b.FailNow() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkValidator{{.TestName}}(b *testing.B) { | ||
| var validate *validator.Validate | ||
|
|
||
| validate = validator.New(validator.WithRequiredStructEnabled()) | ||
|
|
||
| var validInput {{.BasicType}} = {{.ValidInput}} | ||
|
|
||
| data := &Validator{{.TestName}}Struct{ | ||
| Field: &validInput, | ||
| } | ||
|
|
||
| for b.Loop() { | ||
alexgarzao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err := validate.Struct(data); err != nil { | ||
| b.FailNow() | ||
| } | ||
| } | ||
| } | ||
| {{end}} | ||
This file contains hidden or 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,124 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "go/format" | ||
| "log" | ||
| "os" | ||
| "strings" | ||
| "text/template" | ||
|
|
||
| "github.com/opencodeco/validgen/internal/common" | ||
| "golang.org/x/text/cases" | ||
| "golang.org/x/text/language" | ||
| ) | ||
|
|
||
| type CmpBenchTests struct { | ||
| Tests []CmpBenchTest | ||
| } | ||
|
|
||
| type CmpBenchTest struct { | ||
| TestName string | ||
| FieldType string | ||
| BasicType string | ||
| ValidGenTag string | ||
| ValidatorTag string | ||
| ValidInput string | ||
| } | ||
|
|
||
| func generateComparativePerformanceTests() { | ||
| generateComparativePerformanceTest("cmp_perf_no_pointer_tests.tpl", "generated_cmp_perf_no_pointer_test.go", false) | ||
| generateComparativePerformanceTest("cmp_perf_pointer_tests.tpl", "generated_cmp_perf_pointer_test.go", true) | ||
| } | ||
|
|
||
| func generateComparativePerformanceTest(tpl, dest string, pointer bool) { | ||
| log.Printf("Generating comparative performance tests file: tpl[%s] dest[%s] pointer[%v]\n", tpl, dest, pointer) | ||
|
|
||
| benchTests := CmpBenchTests{} | ||
|
|
||
| for _, typeVal := range typesValidation { | ||
| if typeVal.validatorTag == "" { | ||
| log.Printf("Skipping tag %s: go-validator tag not defined\n", typeVal.tag) | ||
| continue | ||
| } | ||
|
|
||
| for _, testCase := range typeVal.testCases { | ||
| if testCase.excludeIf&cmpBenchTests != 0 { | ||
| log.Printf("Skipping test: tag %s type %s\n", typeVal.tag, testCase.typeClass) | ||
| continue | ||
| } | ||
| if testCase.excludeIf&noPointer != 0 && !pointer { | ||
| log.Printf("Skipping no pointer: tag %s type %s\n", typeVal.tag, testCase.typeClass) | ||
| continue | ||
| } | ||
|
|
||
| normalizedType := testCase.typeClass | ||
| if pointer { | ||
| normalizedType = "*" + normalizedType | ||
| } | ||
|
|
||
| fTypes := common.HelperFromNormalizedToBasicTypes(normalizedType) | ||
| sNames := common.HelperFromNormalizedToStringNames(normalizedType) | ||
|
|
||
| for i := range fTypes { | ||
| validGenTag := typeVal.tag | ||
| if typeVal.argsCount != common.ZeroValue { | ||
| validGenTag += "=" + testCase.validation | ||
| } | ||
| goValidatorTag := typeVal.validatorTag | ||
| if typeVal.argsCount != common.ZeroValue { | ||
| goValidatorTag += "=" + testCase.validation | ||
| } | ||
| testName := cases.Title(language.Und).String(typeVal.tag) + sNames[i] | ||
|
|
||
| basicType, _ := strings.CutPrefix(fTypes[i], "*") | ||
|
|
||
| benchTests.Tests = append(benchTests.Tests, CmpBenchTest{ | ||
| TestName: testName, | ||
| FieldType: fTypes[i], | ||
| BasicType: basicType, | ||
| ValidGenTag: validGenTag, | ||
| ValidatorTag: goValidatorTag, | ||
| ValidInput: strings.ReplaceAll(testCase.validCase, "{{.BasicType}}", basicType), | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| log.Printf("%d test cases were generated\n", len(benchTests.Tests)) | ||
|
|
||
| if err := benchTests.GenerateFile(tpl, dest); err != nil { | ||
| log.Fatalf("error generating comparative performance tests file %s", err) | ||
| } | ||
|
|
||
| log.Println("Generating done") | ||
| } | ||
|
|
||
| func (cbt *CmpBenchTests) GenerateFile(tplFile, output string) error { | ||
| tpl, err := os.ReadFile(tplFile) | ||
| if err != nil { | ||
| return fmt.Errorf("error reading %s: %s", tplFile, err) | ||
| } | ||
|
|
||
| tmpl, err := template.New("BenchTest").Parse(string(tpl)) | ||
| if err != nil { | ||
| return fmt.Errorf("error parsing template %s: %s", tplFile, err) | ||
| } | ||
|
|
||
| code := new(bytes.Buffer) | ||
| if err := tmpl.Execute(code, cbt); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| formattedCode, err := format.Source(code.Bytes()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := os.WriteFile(output, formattedCode, 0644); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.