github.com/mailgun/holster/v4@v4.20.0/errors/bench_test.go (about)

     1  //go:build go1.7
     2  // +build go1.7
     3  
     4  package errors
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	stderrors "errors"
    11  )
    12  
    13  func noErrors(at, depth int) error {
    14  	if at >= depth {
    15  		return stderrors.New("no error")
    16  	}
    17  	return noErrors(at+1, depth)
    18  }
    19  
    20  func yesErrors(at, depth int) error {
    21  	if at >= depth {
    22  		return New("ye error")
    23  	}
    24  	return yesErrors(at+1, depth)
    25  }
    26  
    27  func BenchmarkErrors(b *testing.B) {
    28  	type run struct {
    29  		stack int
    30  		std   bool
    31  	}
    32  	runs := []run{
    33  		{10, false},
    34  		{10, true},
    35  		{100, false},
    36  		{100, true},
    37  		{1000, false},
    38  		{1000, true},
    39  	}
    40  	for _, r := range runs {
    41  		part := "pkg/errors"
    42  		if r.std {
    43  			part = "errors"
    44  		}
    45  		name := fmt.Sprintf("%s-stack-%d", part, r.stack)
    46  		b.Run(name, func(b *testing.B) {
    47  			f := yesErrors
    48  			if r.std {
    49  				f = noErrors
    50  			}
    51  			b.ReportAllocs()
    52  			for i := 0; i < b.N; i++ {
    53  				_ = f(0, r.stack)
    54  			}
    55  			b.StopTimer()
    56  		})
    57  	}
    58  }