package gotest
import (
"errors"
)
func Division(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("除数不能为0")
}
return a / b, nil
}
package gotest
import (
"testing"
)
func Test_Division_1(t *testing.T) {
if i, e := Division(6, 2); i != 3 || e != nil { //try a unit test on function
t.Error("除法函数测试没通过") // 如果不是如预期的那么就报错
} else {
t.Log("第一个测试通过了") //记录一些你期望记录的信息
}
}
func Test_Division_2(t *testing.T) {
t.Error("就是不通过")
}
--- FAIL: Test_Division_2 (0.00 seconds) gotest_test.go:16: 就是不通过 FAIL exit status 1 FAIL gotest 0.013s
=== RUN Test_Division_1 --- PASS: Test_Division_1 (0.00 seconds) gotest_test.go:11: 第一个测试通过了 === RUN Test_Division_2 --- FAIL: Test_Division_2 (0.00 seconds) gotest_test.go:16: 就是不通过 FAIL exit status 1 FAIL gotest 0.012s
func Test_Division_2(t *testing.T) {
if _, e := Division(6, 0); e == nil { //try a unit test on function
t.Error("Division did not work as expected.") // 如果不是如预期的那么就报错
} else {
t.Log("one test passed.", e) //记录一些你期望记录的信息
}
}
=== RUN Test_Division_1 --- PASS: Test_Division_1 (0.00 seconds) gotest_test.go:11: 第一个测试通过了 === RUN Test_Division_2 --- PASS: Test_Division_2 (0.00 seconds) gotest_test.go:20: one test passed. 除数不能为0 PASS ok gotest 0.013s
func BenchmarkXXX(b *testing.B) { ... }
package gotest
import (
"testing"
)
func Benchmark_Division(b *testing.B) {
for i := 0; i < b.N; i++ { //use b.N for looping
Division(4, 5)
}
}
func Benchmark_TimeConsumingFunction(b *testing.B) {
b.StopTimer() //调用该函数停止压力测试的时间计数
//做一些初始化的工作,例如读取文件数据,数据库连接之类的,
//这样这些时间不影响我们测试函数本身的性能
b.StartTimer() //重新开始时间
for i := 0; i < b.N; i++ {
Division(4, 5)
}
}
PASS Benchmark_Division 500000000 7.76 ns/op Benchmark_TimeConsumingFunction 500000000 7.80 ns/op ok gotest 9.364s
PASS Benchmark_Division-2 300000000 4.60 ns/op Benchmark_Division-2 300000000 4.57 ns/op Benchmark_Division-2 300000000 4.63 ns/op Benchmark_Division-2 300000000 4.60 ns/op Benchmark_Division-2 300000000 4.63 ns/op Benchmark_TimeConsumingFunction-2 300000000 4.64 ns/op Benchmark_TimeConsumingFunction-2 300000000 4.61 ns/op Benchmark_TimeConsumingFunction-2 300000000 4.60 ns/op Benchmark_TimeConsumingFunction-2 300000000 4.59 ns/op Benchmark_TimeConsumingFunction-2 300000000 4.60 ns/op ok _/home/diego/GoWork/src/app/testing 18.546s
➜ testBenchMark ls popcnt
ackage popcnt
import (
"testing"
)
const m1 = 0x5555555555555555
const m2 = 0x3333333333333333
const m4 = 0x0f0f0f0f0f0f0f0f
const h01 = 0x0101010101010101
func popcnt(x uint64) uint64 {
x -= (x >> 1) & m1
x = (x & m2) + ((x >> 2) & m2)
x = (x + (x >> 4)) & m4
return (x * h01) >> 56
}
func BenchmarkPopcnt(b *testing.B) {
for i := 0; i < b.N; i++ {
x := i
x -= (x >> 1) & m1
x = (x & m2) + ((x >> 2) & m2)
x = (x + (x >> 4)) & m4
_ = (x * h01) >> 56
}
}
➜ testBenchMark go test -bench=".*" -cpuprofile=cpu.profile ./popcnt testing: warning: no tests to run PASS BenchmarkPopcnt-8 1000000000 2.01 ns/op ok app/testBenchMark/popcnt 2.219s ➜ testBenchMark ll total 6704 drwxr-xr-x 5 diego staff 170 5 6 13:57 . drwxr-xr-x 3 diego staff 102 5 6 11:12 .. -rw-r--r-- 1 diego staff 5200 5 6 13:57 cpu.profile drwxr-xr-x 4 diego staff 136 5 6 11:47 popcnt -rwxr-xr-x 1 diego staff 3424176 5 6 13:57 popcnt.test ➜ testBenchMark
➜ testBenchMark ll total 6704 drwxr-xr-x 5 diego staff 170 5 6 13:57 . drwxr-xr-x 3 diego staff 102 5 6 11:12 .. -rw-r--r-- 1 diego staff 5200 5 6 13:57 cpu.profile drwxr-xr-x 3 diego staff 102 5 6 14:01 popcnt -rwxr-xr-x 1 diego staff 3424176 5 6 13:57 popcnt.test ➜ testBenchMark
go tool pprof popcnt.test cpu.profile 进入交互模式
➜ testBenchMark go tool pprof popcnt.test cpu.profile Entering interactive mode (type "help" for commands) (pprof) top 1880ms of 1880ms total ( 100%) flat flat% sum% cum cum% 1790ms 95.21% 95.21% 1790ms 95.21% app/testBenchMark/popcnt.BenchmarkPopcnt 90ms 4.79% 100% 90ms 4.79% runtime.usleep 0 0% 100% 1790ms 95.21% runtime.goexit 0 0% 100% 90ms 4.79% runtime.mstart 0 0% 100% 90ms 4.79% runtime.mstart1 0 0% 100% 90ms 4.79% runtime.sysmon 0 0% 100% 1790ms 95.21% testing.(*B).launch 0 0% 100% 1790ms 95.21% testing.(*B).runN (pprof)
$ go tool pprof --text mybin http://myserver:6060:/debug/pprof/profile
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有