# gochecker **Repository Path**: fmldd/gochecker ## Basic Information - **Project Name**: gochecker - **Description**: 高性能的请求参数的校验器,简单校验结构体字段合法性 - **Primary Language**: Go - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2022-08-03 - **Last Updated**: 2024-04-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # go-checker #### Describe A detector for the validity of request parameters, which simply and efficiently checks the validity of structure fields, and supports recursive detection of internal elements of structslice. You can use this interface with confidence without worrying about excessive performance consumption caused by reflection. Refer to Performance Observation #### Install use go get ```javascript go get gitee.com/fmldd/gochecker ``` then gochecker imports the package into your own code ```javascript import "gitee.com/fmldd/gochecker" ``` #### use | label | describe | |----------|--------------------------------------------------------------------------| | notNull | Determine whether data of types such as references and pointers are null | | notEmpty | Determine whether the data of types such as string/slice/map... is empty | | notZero | Determine whether data such as int/float/complex... is 0 | #### Started Quickly ```javascript package main import ( "fmt" "gitee.com/fmldd/gochecker" ) type Info struct { Field1 string `checker:"notEmpty"` Field2 int `checker:"notZero"` Field3 *string `checker:"notNull"` Field4 map[string]struct{} `checker:"notEmpty"` } func main() { i := Info{ Field1: "Patrick Star", Field2: 0, Field3: nil, Field4: make(map[string]struct{}), } err := gochecker.Struct(&i) fmt.Println(err) } ``` For more usage examples, please refer to : checker_test.go #### Performance Observation Use the following configuration and system to verify the simple structure 2 million times, use pprof to record and observe the experimental results ```javascript Linux version 5.10.101-amd64-desktop (deepin@wh-k8snode-70) Intel(R) i5-7500 CPU @ 3.40GHz Core 4 Memory 16GB ``` ##### Native Manually determine whether the structure fields are normal one by one ```javascript Duration: 604.42ms, Total samples = 430ms (71.14%) Entering interactive mode (type "help" for commands, "o" for options) (pprof) top Showing nodes accounting for 330ms, 76.74% of 430ms total Showing top 10 nodes out of 58 flat flat% sum% cum cum% 90ms 20.93% 20.93% 300ms 69.77% runtime.concatstrings 50ms 11.63% 32.56% 170ms 39.53% runtime.mallocgc 50ms 11.63% 44.19% 50ms 11.63% runtime.memclrNoHeapPointers 40ms 9.30% 53.49% 60ms 13.95% runtime.scanobject 30ms 6.98% 60.47% 30ms 6.98% runtime.procyield 20ms 4.65% 65.12% 20ms 4.65% runtime.futex 20ms 4.65% 69.77% 200ms 46.51% runtime.rawstringtmp 10ms 2.33% 72.09% 10ms 2.33% runtime.(*consistentHeapStats).acquire 10ms 2.33% 74.42% 90ms 20.93% runtime.(*mcache).refill 10ms 2.33% 76.74% 10ms 2.33% runtime.(*mheap).freeSpanLocked ``` ##### gochecker ```javascript Duration: 3.82s, Total samples = 4.20s (109.97%) Entering interactive mode (type "help" for commands, "o" for options) (pprof) top Showing nodes accounting for 2340ms, 55.71% of 4200ms total Dropped 98 nodes (cum <= 21ms) Showing top 10 nodes out of 104 flat flat% sum% cum cum% 680ms 16.19% 16.19% 1700ms 40.48% runtime.mallocgc 470ms 11.19% 27.38% 810ms 19.29% reflect.StructTag.Lookup 320ms 7.62% 35.00% 460ms 10.95% runtime.heapBitsSetType 170ms 4.05% 39.05% 170ms 4.05% runtime.nextFreeFast (inline) 160ms 3.81% 42.86% 160ms 3.81% runtime.memclrNoHeapPointers 160ms 3.81% 46.67% 330ms 7.86% runtime.scanobject 100ms 2.38% 49.05% 100ms 2.38% indexbytebody 100ms 2.38% 51.43% 110ms 2.62% runtime.heapBitsForAddr (inline) 90ms 2.14% 53.57% 2720ms 64.76% gitee.com/fmldd/gochecker.(*t).structure 90ms 2.14% 55.71% 250ms 5.95% strconv.unquote ``` ##### [validator](https://github.com/go-playground/validator) validator is the default validator of the gin framework. It is very powerful and supports the verification of various complex parameters. It is not reasonable to compare it here. It aims to observe the impact of complexity on system performance. Appropriate components should be selected according to the application scenario. ```javascript Duration: 97.04s, Total samples = 134.33s (138.42%) Entering interactive mode (type "help" for commands, "o" for options) (pprof) top Showing nodes accounting for 68.87s, 51.27% of 134.33s total Dropped 270 nodes (cum <= 0.67s) Showing top 10 nodes out of 84 flat flat% sum% cum cum% 17.26s 12.85% 12.85% 44.62s 33.22% runtime.scanobject 10.17s 7.57% 20.42% 15.80s 11.76% runtime.mapassign_faststr 6.66s 4.96% 25.38% 29.40s 21.89% runtime.mallocgc 6.41s 4.77% 30.15% 12.39s 9.22% runtime.mapaccess2_faststr 5.87s 4.37% 34.52% 8.30s 6.18% runtime.mapiternext 4.93s 3.67% 38.19% 6.49s 4.83% runtime.heapBitsSetType 4.75s 3.54% 41.73% 4.91s 3.66% runtime.heapBits.next 4.65s 3.46% 45.19% 4.65s 3.46% aeshashbody 4.17s 3.10% 48.29% 5.05s 3.76% runtime.spanOf 4s 2.98% 51.27% 70.17s 52.24% github.com/go-playground/validator/v10.New ```