github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/runtime/runtime_test.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package runtime_test 6 7 import ( 8 "io" 9 "io/ioutil" 10 "os" 11 "os/exec" 12 . "runtime" 13 "strconv" 14 "strings" 15 "testing" 16 ) 17 18 var errf error 19 20 func errfn() error { 21 return errf 22 } 23 24 func errfn1() error { 25 return io.EOF 26 } 27 28 func BenchmarkIfaceCmp100(b *testing.B) { 29 for i := 0; i < b.N; i++ { 30 for j := 0; j < 100; j++ { 31 if errfn() == io.EOF { 32 b.Fatal("bad comparison") 33 } 34 } 35 } 36 } 37 38 func BenchmarkIfaceCmpNil100(b *testing.B) { 39 for i := 0; i < b.N; i++ { 40 for j := 0; j < 100; j++ { 41 if errfn1() == nil { 42 b.Fatal("bad comparison") 43 } 44 } 45 } 46 } 47 48 func BenchmarkDefer(b *testing.B) { 49 for i := 0; i < b.N; i++ { 50 defer1() 51 } 52 } 53 54 func defer1() { 55 defer func(x, y, z int) { 56 if recover() != nil || x != 1 || y != 2 || z != 3 { 57 panic("bad recover") 58 } 59 }(1, 2, 3) 60 return 61 } 62 63 func BenchmarkDefer10(b *testing.B) { 64 for i := 0; i < b.N/10; i++ { 65 defer2() 66 } 67 } 68 69 func defer2() { 70 for i := 0; i < 10; i++ { 71 defer func(x, y, z int) { 72 if recover() != nil || x != 1 || y != 2 || z != 3 { 73 panic("bad recover") 74 } 75 }(1, 2, 3) 76 } 77 } 78 79 func BenchmarkDeferMany(b *testing.B) { 80 for i := 0; i < b.N; i++ { 81 defer func(x, y, z int) { 82 if recover() != nil || x != 1 || y != 2 || z != 3 { 83 panic("bad recover") 84 } 85 }(1, 2, 3) 86 } 87 } 88 89 // The profiling signal handler needs to know whether it is executing runtime.gogo. 90 // The constant RuntimeGogoBytes in arch_*.h gives the size of the function; 91 // we don't have a way to obtain it from the linker (perhaps someday). 92 // Test that the constant matches the size determined by 'go tool nm -S'. 93 // The value reported will include the padding between runtime.gogo and the 94 // next function in memory. That's fine. 95 func TestRuntimeGogoBytes(t *testing.T) { 96 dir, err := ioutil.TempDir("", "go-build") 97 if err != nil { 98 t.Fatalf("failed to create temp directory: %v", err) 99 } 100 defer os.RemoveAll(dir) 101 102 out, err := exec.Command("go", "build", "-o", dir+"/hello", "../../../test/helloworld.go").CombinedOutput() 103 if err != nil { 104 t.Fatalf("building hello world: %v\n%s", err, out) 105 } 106 107 out, err = exec.Command("go", "tool", "nm", "-S", dir+"/hello").CombinedOutput() 108 if err != nil { 109 t.Fatalf("go tool nm: %v\n%s", err, out) 110 } 111 112 for _, line := range strings.Split(string(out), "\n") { 113 f := strings.Fields(line) 114 if len(f) == 4 && f[3] == "runtime.gogo" { 115 size, _ := strconv.Atoi(f[1]) 116 if GogoBytes() != int32(size) { 117 t.Fatalf("RuntimeGogoBytes = %d, should be %d", GogoBytes(), size) 118 } 119 return 120 } 121 } 122 123 t.Fatalf("go tool nm did not report size for runtime.gogo") 124 }