github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/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  	"testing"
    10  )
    11  
    12  var errf error
    13  
    14  func errfn() error {
    15  	return errf
    16  }
    17  
    18  func errfn1() error {
    19  	return io.EOF
    20  }
    21  
    22  func BenchmarkIfaceCmp100(b *testing.B) {
    23  	for i := 0; i < b.N; i++ {
    24  		for j := 0; j < 100; j++ {
    25  			if errfn() == io.EOF {
    26  				b.Fatal("bad comparison")
    27  			}
    28  		}
    29  	}
    30  }
    31  
    32  func BenchmarkIfaceCmpNil100(b *testing.B) {
    33  	for i := 0; i < b.N; i++ {
    34  		for j := 0; j < 100; j++ {
    35  			if errfn1() == nil {
    36  				b.Fatal("bad comparison")
    37  			}
    38  		}
    39  	}
    40  }
    41  
    42  func BenchmarkDefer(b *testing.B) {
    43  	for i := 0; i < b.N; i++ {
    44  		defer1()
    45  	}
    46  }
    47  
    48  func defer1() {
    49  	defer func(x, y, z int) {
    50  		if recover() != nil || x != 1 || y != 2 || z != 3 {
    51  			panic("bad recover")
    52  		}
    53  	}(1, 2, 3)
    54  	return
    55  }
    56  
    57  func BenchmarkDefer10(b *testing.B) {
    58  	for i := 0; i < b.N/10; i++ {
    59  		defer2()
    60  	}
    61  }
    62  
    63  func defer2() {
    64  	for i := 0; i < 10; i++ {
    65  		defer func(x, y, z int) {
    66  			if recover() != nil || x != 1 || y != 2 || z != 3 {
    67  				panic("bad recover")
    68  			}
    69  		}(1, 2, 3)
    70  	}
    71  }
    72  
    73  func BenchmarkDeferMany(b *testing.B) {
    74  	for i := 0; i < b.N; i++ {
    75  		defer func(x, y, z int) {
    76  			if recover() != nil || x != 1 || y != 2 || z != 3 {
    77  				panic("bad recover")
    78  			}
    79  		}(1, 2, 3)
    80  	}
    81  }