[关闭]
@mSolo 2015-05-07T09:05:21.000000Z 字数 3479 阅读 1514

Golang 自学之路(二):测试、调试与构建

Go Golang


测试与调试

Simulate Breakpoint

  1. where := func() {
  2. _, file, line, _ := runtime.Caller(1)
  3. log.Printf(“%s:%d”, file, line)
  4. }

Garbage Collection

  1. runtime.GC()
  2. fmt.Printf(“%d\n”, runtime.MemStats.Alloc/1024)
  3. runtime.SetFinalizer(obj, func(obj *typeObj))

test functions

a concrete example

  1. package main
  2. import (
  3. fmt
  4. “./even/even
  5. )
  6. func main() {
  7. for i:=0; i<=100; i++ {
  8. fmt.Printf(“Is the integer %d even? %v\n”, i, even.Even(i))
  9. }
  10. }
  11. package even
  12. func Even(i int) bool { // Exported functions
  13. return i%2 == 0
  14. }
  15. func Odd(i int) bool {
  16. return i%2 != 0
  17. }
  18. package even
  19. import testing
  20. func TestEven(t *testing.T) {
  21. if !Even(10) {
  22. t.Log(“10 must be even!”)
  23. t.Fail()
  24. }
  25. if Even(7) {
  26. t.Log(“7 is not even!”)
  27. t.Fail()
  28. }
  29. }
  30. func TestOdd(t *testing.T) {
  31. if !Odd(11) {
  32. t.Log(“11 must be odd!”)
  33. t.Fail()
  34. }
  35. if Odd(10) {
  36. t.Log(“10 is not odd!”)
  37. t.Fail()
  38. }
  39. }

Using table-driven tests

  1. var tests = []struct{ // Test table
  2. in string
  3. out string
  4. }{
  5. {“in1”, exp1”},
  6. {“in2”, exp2”},
  7. // ...
  8. }
  9. func TestFunction(t *testing.T) {
  10. for i, tt := range tests {
  11. s := FuncToBeTested(tt.in)
  12. verify(t, i, FuncToBeTested: “, tt.in, s, tt.out)
  13. }
  14. }
  15. func verify(t *testing.T, testnum int, testcase, input, output, expected string) {
  16. if expected != output {
  17. t.Errorf(“%d. %s with input = %s: output %s != %s”,
  18. testnum, testcase, input, output, expected)
  19. }
  20. }

investigating performance

  1. var cpuprofile = flag.String(“cpuprofile”, “”, write cpu profile to file”)
  2. func main() {
  3. flag.Parse()
  4. if *cpuprofile != “” {
  5. f, err := os.Create(*cpuprofile)
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. pprof.StartCPUProfile(f)
  10. defer pprof.StopCPUProfile()
  11. }
  12. ...

If it is seen that the function runtime.mallocgc (which both allocates and runs periodic garbage collections) is heavily used, then it is time for memory profiling.

  1. var memprofile = flag.String(“memprofile”, “”, write memory profile to this file”)
  2. // ...
  3. CallToFunctionWhichAllocatesLotsOfMemory()
  4. if *memprofile != “” {
  5. f, err := os.Create(*memprofile)
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. pprof.WriteHeapProfile(f)
  10. f.Close()
  11. return
  12. }

For web applications:

构建 package

  1. $ go build -x crypto/hmac
  2. WORK=/tmp/go-build249279931
  3. mkdir -p $WORK/crypto/hmac/_obj/
  4. mkdir -p $WORK/crypto/
  5. $ cd /home/dfc/go/src/pkg/crypto/hmac
  6. $ /home/dfc/go/pkg/tool/linux_arm/5g -o $WORK/crypto/hmac/_obj/_go_.5 -p crypto/hmac -complete -D _/home/dfc/go/src/pkg/crypto/hmac -I $WORK ./hmac.go
  7. $ /home/dfc/go/pkg/tool/linux_arm/pack grcP $WORK $WORK/crypto/hmac.a $WORK/crypto/hmac/_obj/_go_.5
  1. $ go install x crypto/hmac
  2. # ...
  3. $ mkdir -p /home/dfc/go/pkg/linux_arm/crypto/
  4. $ cp $WORK/crypto/hmac.a /home/dfc/go/pkg/linux_arm/crypto/hmac.a
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注