家教网站模板下载,桂林市区,跨境电商培训,画册设计理念go-callvis-代码调用关系的可视化工具
go-callvis是一个代码调用关系的可视化工具#xff0c;它可以帮助我们了解指定项目代码的结构#xff0c;以达到更快的理解代码意图的目的。
工具使用简单#xff0c;步骤如下#xff1a;
// 1. 安装
git clone https://github.com…go-callvis-代码调用关系的可视化工具
go-callvis是一个代码调用关系的可视化工具它可以帮助我们了解指定项目代码的结构以达到更快的理解代码意图的目的。
工具使用简单步骤如下
// 1. 安装
git clone https://github.com/ofabry/go-callvis.git
cd go-callvis make install// 2. 以著名golang开源项目bigcache 的main函数为入口分析代码调用关系不打开浏览器 | 忽略标准库的方法
go-callvis -skipbrowser -nostd ./server/// 3. 访问http://localhost:7878查看调用关系矢量图 调用关系矢量图怎么看一共分为三个部分
Packages / Types包
RepresentsStylefocused需要关注的blue color蓝色stdlib标准库green color绿色other其他包yellow color黄色
Functions / Methods函数方法
RepresentsStyleexported导出的包bold border粗边框unexported未导出包normal border正常边框anonymous匿名包dotted border虚线边框
Calls调用
RepresentsStyleinternal内部black color黑色external外部brown color棕色static静态函数solid line实线dynamic动态函数dashed line虚线regular常规函数simple arrow简单箭头concurrent协程arrow with circle箭头带圆圈deferreddeferarrow with diamond箭头带菱形
gotests-自动生成单测用例框架
gotests工具可以帮我们自动生成单测用例框架这样以来我们只需要关注需要测试的业务代码逻辑即可省去了大量的拷贝复制的重复劳动。
gotest工具使用也是十分方便可以直接安装go get -u github.com/cweill/gotests/...后用命令行$ gotests [options] PATH ...的方式或者也可以作为IDE的插件直接使用如Emacs,Vim,Atom Editor,Visual Studio Code, andIntelliJ Goland. 这里以VS Code为例 // 一个简单工厂模式代码实现package simplefactoryimport fmt//API is interface
type API interface {Say(name string) string
}//NewAPI return Api instance by type
func NewAPI(t int) API {if t 1 {return hiAPI{}} else if t 2 {return helloAPI{}}return nil
}//hiAPI is one of API implement
type hiAPI struct{}//Say hi to name
func (*hiAPI) Say(name string) string {return fmt.Sprintf(Hi, %s, name)
}//HelloAPI is another API implement
type helloAPI struct{}//Say hello to name
func (*helloAPI) Say(name string) string {return fmt.Sprintf(Hello, %s, name)
}自动生成的测试用例框架如下
PS D:\code\golang-design-pattern\00_simple_factory gotests.exe -all .\simple.go
Generated TestNewAPI
Generated Test_hiAPI_Say
Generated Test_helloAPI_Say
package simplefactoryimport (reflecttesting
)func TestNewAPI(t *testing.T) {type args struct {t int}tests : []struct {name stringargs argswant API}{// TODO: Add test cases.}for _, tt : range tests {t.Run(tt.name, func(t *testing.T) {if got : NewAPI(tt.args.t); !reflect.DeepEqual(got, tt.want) {t.Errorf(NewAPI() %v, want %v, got, tt.want)}})}
}func Test_hiAPI_Say(t *testing.T) {})}
}func Test_helloAPI_Say(t *testing.T) {type args struct {name string}tests : []struct {name stringh *helloAPIargs argswant string}{// TODO: Add test cases.}for _, tt : range tests {t.Run(tt.name, func(t *testing.T) {h : helloAPI{}if got : h.Say(tt.args.name); got ! tt.want {t.Errorf(helloAPI.Say() %v, want %v, got, tt.want)}})}
}
PS D:\code\golang-design-pattern\00_simple_factorygo-multierror-多错误管理
在关于使用 Go 语言的时候开发者面对最大的挑战的年度调查中错误error管理总是能引起很多争论。在并发环境处理 error 的场景下或者在同一个 goroutine 中合并多个错误的场景下Go 提供了很不错的包可以让多个错误的处理变得简单来看看如何合并由单个 goroutine 生成的多个 error。
go-multierror提供了常用的多错误管理四种方式
Building a list of errors / Accessing the list of errors / Checking for an exact error value
构建错误返回列表 / 访问误返回列表 / 检查错误列表中是否包含某个错误
package mainimport (fmterrorsmultierrorgithub.com/hashicorp/go-multierror
)func step1() error {return errors.New(xhihu)
}func step2() error {return errors.New(yhihu)
}func main() {var result errorif err : step1(); err ! nil {result multierror.Append(result, err)}if err : step2(); err ! nil {result multierror.Append(result, err)}fmt.Printf(result.Error())if merr, ok : result.(*multierror.Error); ok {// Use merr.Errors// merr.Errors - []error }if errors.Is(result, os.ErrNotExist) {// err contains os.ErrNotExist}return
} Customizing the formatting of the errors / 自定义多错误时显示的整体的打印信息
var result *multierror.Error// ... accumulate errors here, maybe using Appendif result ! nil {result.ErrorFormat func([]error) string {return errors!}
}goleak-内存泄漏检查
goroutine 泄漏会导致内存中存活的 goroutine 数量不断上升直到把主机的CPU和内存全部吃爆最终以服务宕机为止。所以我们会想到有没有一种方法可以在代码部署之前来检查程序中是否存在goroutine 泄漏。
Uber 公司的 Go 团队在 GitHub 开源了他们的goroutine 泄漏检测器出来一个与单元测试结合使用的工具。 goleak 可以监控当前测试代码中泄漏的 goroutine。下面有一个 goroutine 泄漏的例子
//demo.go
func leak() error {go func() {time.Sleep(time.Minute)}()return nil
}//demo_test.go
func TestLeakFunction(t *testing.T) {defer goleak.VerifyNone(t)if err : leak(); err ! nil {t.Fatal(error not expected)}
}用例直接报错了从报错信息中我们可以看到泄露的goroutine 的堆栈信息以及 goroutine 的状态。
pprof性能分析火焰图
Pprof是一个用于采样数据可视化和分析的工具。主要分析服务运行过程产生的阻塞同步的堆栈信息所有的goroutine堆栈信息活动对象的内存分配信息互斥锁的竞争持有者的堆栈默认进行30s的CPU采样信息查看创建新OS线程的堆栈信息等等。
我们可以利用prof进行性能监控且可以生成监控信息文件方便后续分析性能瓶颈或者是内存泄漏情况。
package mainimport (fmttimelognet/http_ net/http/pprofosruntime
)func alloc(outCh chan- int) {buf : make([]byte, 1024)outCh - 0
}func Leak() {outCh : make(chan int)go func() {if false {-outCh}select {}}()tick : time.Tick(time.Second / 100)i : 0for range tick {ifmt.Println(i)//一直分配内存不释放放go alloc(outCh)
}func main() {log.SetFlags(log.Lshortfile | log.LstdFlags)log.SetOutput(os.Stdout)runtime.GOMAXPROCS(1)runtime.SetMutexProfileFraction(1)runtime.SetBlockProfileRate(1)// 需要性能分析的业务逻辑go Leak()go func() {// 通过http://locahost:6060/debug/pprof进行查看相关的监控信息文件if err : http.ListenAndServe(:6060, nil); err ! nil {log.Fatal(err)}os.Exit(0)}()select{}
}1. go build prof_demo.go2. ./prof_demo3. 手动登陆浏览器通过http://locahost:6060/debug/pprof进行查看相关的监控信息文件4. go install github.com/google/pproflatest5. yum install graphviz// 查看火焰图
6. pprof -http:6061 http://192.168.159.140:6060/debug/pprof/profilejsoniter-高性能json序列化工具
go语言多数用于云原生中的网咯服务因此一个常见的场景就是数据的序列化和反序列化一般都是利用json进行。这里推荐采用jsoniter替换掉go原生encoding/json两者接口一致但jsoniter的性能远远超过encoding/jsonBenchmark详见如下
ns/opallocation bytesallocation timesstd decode35510 ns/op1960 B/op99 allocs/opeasyjson decode8499 ns/op160 B/op4 allocs/opjsoniter decode5623 ns/op160 B/op3 allocs/op import jsoniter github.com/json-iterator/govar json jsoniter.ConfigCompatibleWithStandardLibrary
json.Marshal(data)json.Unmarshal(input, data)Reference
Go代码调用链路可视化工具—go-callvis - 知乎 (zhihu.com)
GoTests工具自动化test使用 - 掘金 (juejin.cn)
Go: Multiple Errors Management. Error management in Go is always prone… | by Vincent Blanchon | A Journey With Go | Medium
Fastest JSON parser ever (jsoniter.com)
Go多错误管理 - Go语言中文网 - Golang中文社区 (studygolang.com)
GitHub - hashicorp/go-multierror: A Go (golang) package for representing a list of errors as a single error
Go: Goroutine 泄漏检查器 - Go语言中文网 - Golang中文社区 (studygolang.com)
golang性能优化之pprof及其火焰图 - 简书 (jianshu.com)
Golang-PProf之性能剖析_-Xx.。的博客-CSDN博客_golang pprof allocs 解释