t *testing.T
package main
import "fmt"
func main() {
sum := Add(1, 2)
fmt.Println(sum)
}
func Add(a int, b int) int {
return a + b
}
package main
import (
"testing"
)
func TestAdd(t *testing.T) {
testData := []struct {
a int
b int
c int
}{
{1, 2, 3},
{4, 5, 9},
{50, 5, 5},
}
ans := 0
for _, data := range testData {
if ans = Add(data.a, data.b); ans != data.c {
t.Errorf("%d + %d expected %d,but %d got", data.a, data.b, data.c, ans)
}
}
}
Run 执行脚本
Debug 开启Debug模式
coverprofile 代码覆盖率
CPU Profiler cup分析
Memory Profiler 内存分析 对象、堆的分析和内存泄漏等
Blocking Profiler 记录 goroutine的阻塞情况,等待和同步情况,timer/channel通讯等各项细节
Mutex Profiler 互斥锁分析,包括各种竞争情况
我通过ide运行一下
我们也可以使用ide的debug功能打断点调试
命令行测试,首先我们进入add.go目录
zhangguofu@zhangguofudeMacBook-Pro add (master) $ go test -coverprofile=c.out
--- FAIL: TestAdd (0.00s)
add_test.go:20: 50 + 5 expected 5,but 55 got
FAIL
coverage: 33.3% of statements
exit status 1
FAIL goapp/src/learngo/add 0.502s
zhangguofu@zhangguofudeMacBook-Pro add (master) $ ls
add.go add_test.go c.out
zhangguofu@zhangguofudeMacBook-Pro add (master) $ cat c.out
mode: set
goapp/src/learngo/add/add.go:5.13,8.2 2 0
goapp/src/learngo/add/add.go:10.28,12.2 1 1
我们发现多了一个c.out 文件,但是里面的内容我们看不太懂,我们借助工具go tool cover ,查看代码覆盖的相关命令帮助
我们使用` go tool cover -html=c.out``,此时会打开一个html页面,上面标注了我们代码覆盖的情况
/**
最大字符串不重复
*/
func Repeat(s string) int {
//保持最大不重复串
lastOccur := make(map[string]int)
start := 0
max := 0
for k, v := range []rune(s) {
if index, ok := lastOccur[string(v)]; ok && start <= index {
start = index + 1
}
if max < k-start+1 {
max = k - start + 1
}
lastOccur[string(v)] = k
}
return max
}
func BenchmarkRepeat(b *testing.B) {
str := "不经历风雨,怎么见彩虹?没有人能随随便便成功"
data := struct {
content string
res int
}{
str,
17,
}
for i := 0; i < b.N; i++ {
if len := Repeat(data.content); len != 17 {
b.Error("the program is wrong")
}
}
}
func BenchmarkRepeat(b *testing.B) {
str := "不经历风雨,怎么见彩虹?没有人能随随便便"
for i := 0; i < 20; i++ {
str += str
}
b.Logf("the len of str is %d", len(str))
b.ResetTimer() //忽略上面的计算时间
for i := 0; i < b.N; i++ {
Repeat(str)
}
}
zhangguofu@zhangguofudeMacBook-Pro add (master) $ go test -bench . -cpuprofile cpu.out
goos: darwin
goarch: amd64
pkg: goapp/src/learngo/add
BenchmarkRepeat-8 1 1541179761 ns/op
--- BENCH: BenchmarkRepeat-8
add_test.go:33: the len of str is 62914560
PASS
ok goapp/src/learngo/add 2.077s
zhangguofu@zhangguofudeMacBook-Pro add (master) $ ls
add.go add.test add_test.go c.out cpu.out
zhangguofu@zhangguofudeMacBook-Pro add (master) $ less cpu.out
"cpu.out" may be a binary file. See it anyway?
发现生成的cpu.out 是一个二进制文件,我们使用go tool pprof cpu.out
查看,进入交互模式
我输入web格式的,报错说需要安装Graphviz ,那就安装吧 下载地址 如果安装失败 可以参考我这篇文章解决brew 安装软件失败的问题
go tool pprof cpu.out
输入web
没有报错了,但是此处我碰到一个个问题。就是我的虚拟机(mac上面装的win10)会默认打开svg 文件,很烦人
解决方式
t *testing.T
package main
import "fmt"
func main() {
sum := Add(1, 2)
fmt.Println(sum)
}
func Add(a int, b int) int {
return a + b
}
package main
import (
"testing"
)
func TestAdd(t *testing.T) {
testData := []struct {
a int
b int
c int
}{
{1, 2, 3},
{4, 5, 9},
{50, 5, 5},
}
ans := 0
for _, data := range testData {
if ans = Add(data.a, data.b); ans != data.c {
t.Errorf("%d + %d expected %d,but %d got", data.a, data.b, data.c, ans)
}
}
}
Run 执行脚本
Debug 开启Debug模式
coverprofile 代码覆盖率
CPU Profiler cup分析
Memory Profiler 内存分析 对象、堆的分析和内存泄漏等
Blocking Profiler 记录 goroutine的阻塞情况,等待和同步情况,timer/channel通讯等各项细节
Mutex Profiler 互斥锁分析,包括各种竞争情况
我通过ide运行一下
我们也可以使用ide的debug功能打断点调试
命令行测试,首先我们进入add.go目录
zhangguofu@zhangguofudeMacBook-Pro add (master) $ go test -coverprofile=c.out
--- FAIL: TestAdd (0.00s)
add_test.go:20: 50 + 5 expected 5,but 55 got
FAIL
coverage: 33.3% of statements
exit status 1
FAIL goapp/src/learngo/add 0.502s
zhangguofu@zhangguofudeMacBook-Pro add (master) $ ls
add.go add_test.go c.out
zhangguofu@zhangguofudeMacBook-Pro add (master) $ cat c.out
mode: set
goapp/src/learngo/add/add.go:5.13,8.2 2 0
goapp/src/learngo/add/add.go:10.28,12.2 1 1
我们发现多了一个c.out 文件,但是里面的内容我们看不太懂,我们借助工具go tool cover ,查看代码覆盖的相关命令帮助
我们使用` go tool cover -html=c.out``,此时会打开一个html页面,上面标注了我们代码覆盖的情况
/**
最大字符串不重复
*/
func Repeat(s string) int {
//保持最大不重复串
lastOccur := make(map[string]int)
start := 0
max := 0
for k, v := range []rune(s) {
if index, ok := lastOccur[string(v)]; ok && start <= index {
start = index + 1
}
if max < k-start+1 {
max = k - start + 1
}
lastOccur[string(v)] = k
}
return max
}
func BenchmarkRepeat(b *testing.B) {
str := "不经历风雨,怎么见彩虹?没有人能随随便便成功"
data := struct {
content string
res int
}{
str,
17,
}
for i := 0; i < b.N; i++ {
if len := Repeat(data.content); len != 17 {
b.Error("the program is wrong")
}
}
}
func BenchmarkRepeat(b *testing.B) {
str := "不经历风雨,怎么见彩虹?没有人能随随便便"
for i := 0; i < 20; i++ {
str += str
}
b.Logf("the len of str is %d", len(str))
b.ResetTimer() //忽略上面的计算时间
for i := 0; i < b.N; i++ {
Repeat(str)
}
}
zhangguofu@zhangguofudeMacBook-Pro add (master) $ go test -bench . -cpuprofile cpu.out
goos: darwin
goarch: amd64
pkg: goapp/src/learngo/add
BenchmarkRepeat-8 1 1541179761 ns/op
--- BENCH: BenchmarkRepeat-8
add_test.go:33: the len of str is 62914560
PASS
ok goapp/src/learngo/add 2.077s
zhangguofu@zhangguofudeMacBook-Pro add (master) $ ls
add.go add.test add_test.go c.out cpu.out
zhangguofu@zhangguofudeMacBook-Pro add (master) $ less cpu.out
"cpu.out" may be a binary file. See it anyway?
发现生成的cpu.out 是一个二进制文件,我们使用go tool pprof cpu.out
查看,进入交互模式
我输入web格式的,报错说需要安装Graphviz ,那就安装吧 下载地址 如果安装失败 可以参考我这篇文章解决brew 安装软件失败的问题
go tool pprof cpu.out
输入web
没有报错了,但是此处我碰到一个个问题。就是我的虚拟机(mac上面装的win10)会默认打开svg 文件,很烦人
解决方式
abc我爱中国
,那么再计算中,来回转换是很麻烦的,我们是不是可以转换为int32来计算呢。
abc我爱中国
,那么再计算中,来回转换是很麻烦的,我们是不是可以转换为int32来计算呢。