?
c代碼覆蓋率工具,go test
代碼覆蓋率:
性能測試需要寫代碼嗎。go test -coverprofile=c.out?
go tool cover -html=c.out // 通過html查看
go tool cover // 查看cover幫助
go test -coverprofile=c.out -v nonrepeating_test.go // 指定某個_test.go文件
性能測試:
go test -bench .
代碼:https://github.com/NIGHTFIGHTING/go_learning/blob/master/src/basic/nonrepeating/nonrepeating_test.go
package mainimport ("testing"
)
func lengthOfNonRepeatingSubStr(s string) int {//lastOccurred := make(map[byte]int)lastOccurred := make(map[rune]int)start := 0maxLength := 0//for i, ch := range []byte(s) {for i, ch := range []rune(s) {if lastI, ok := lastOccurred[ch]; ok && lastI >= start {start = lastOccurred[ch] + 1}if i-start+1 > maxLength {maxLength = i - start + 1}lastOccurred[ch] = i}return maxLength
}
func TestSubstr(t *testing.T) {tests := []struct {s stringans int} {// Normal cases{"abcabcbb", 3},{"pwwkew", 3},// Edge cases{"", 0},{"b", 1},{"bbbbbbbb", 1},{"abcabcabcd", 4},// Chinense cases{"這里是慕課網", 6},{"一二三二一", 3},{"黑化肥揮發發灰會花飛灰化肥揮發發黑會飛花", 8},}for _, tt := range tests {actual := lengthOfNonRepeatingSubStr(tt.s)if actual != tt.ans {t.Errorf("got %d for input %s; " +"expected %d",actual, tt.s, tt.ans)}}
}func BenchmarkSubstr(b *testing.B) {s := "黑化肥揮發發灰會花飛灰化肥揮發發黑會飛花"ans := 8for i := 0; i < b.N; i++ {actual := lengthOfNonRepeatingSubStr(s)if actual != ans {b.Errorf("got %d for input %s; " +"expected %d",actual, s, ans)}}
}
?
其中b.N
的值系統會幫我們設定
性能測試:go test -bench .
說明每次操作花了1194ns的時間,總共執行了1000000次。
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态