# 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 # gochecker #### 描述 请求参数合法性的检测器,简单高效校验结构体字段合法性,支持struct/slice内部元素的递归检测,您可以放心的使用该接口而不必担心因反射造成的过多的性能消耗,参考性能观测 #### 安装 使用 go get ```javascript go get gitee.com/fmldd/gochecker ``` 然后gochecker将包导入您自己的代码中 ```javascript import "gitee.com/fmldd/gochecker" ``` #### 使用 | 标签 | 描述 | | -------- | ------------------------------------------ | | notNull | 判断引用及指针等类型数据是否为null | | notEmpty | 判断string/slice/map等类型数据数据是否为空 | | notZero | 判断int/float/complex等数据是否为0 | #### 快速上手 ```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) } ``` 更多使用示例请参考:checker_test.go #### 性能观测 使用如下配置和系统对简单结构体进行2百万次校验,使用pprof记录并观测实验结果 ```javascript Linux version 5.10.101-amd64-desktop (deepin@wh-k8snode-70) Intel(R) i5-7500 CPU @ 3.40GHz Core 4 Memory 16GB ``` ##### 原生 通过手动逐个判断结构字段是否正常 ```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是gin框架的默认验证器功能十分强悍,支持各复杂参数的校验,这里拿来对比并不合理,旨在观测复杂度对系统性能影响,应当根据应用场景选择合适组件 ```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 ```