github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/integration_test/bench/debug_bench_test.go (about)

     1  package bench
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"testing"
     7  )
     8  
     9  // These benchmark guard usage at various scopes.
    10  const (
    11  	const_debug_true  = true
    12  	const_debug_false = false
    13  )
    14  
    15  var (
    16  	var_debug_true  = true
    17  	var_debug_false = false
    18  	arg             = map[string]string{"foo": "bar"}
    19  )
    20  
    21  type obj struct {
    22  	debug bool
    23  }
    24  
    25  func (o *obj) fprintf() {
    26  	if o.debug {
    27  		fmt.Fprintf(io.Discard, "arg: %v", arg)
    28  	}
    29  }
    30  
    31  func BenchmarkFprintf(b *testing.B) {
    32  	// should be the same as NoOp without a const false guard as the compiler should delete the block
    33  	b.Run("NoOp const false", func(b *testing.B) {
    34  		for i := 0; i < b.N; i++ {
    35  			if const_debug_false {
    36  				fmt.Fprintf(io.Discard, "arg: %v", arg)
    37  			}
    38  		}
    39  	})
    40  	b.Run("NoOp var false", func(b *testing.B) {
    41  		for i := 0; i < b.N; i++ {
    42  			if var_debug_false {
    43  				fmt.Fprintf(io.Discard, "arg: %v", arg)
    44  			}
    45  		}
    46  	})
    47  	objFalse := obj{false}
    48  	b.Run("Fprintf obj false", func(b *testing.B) {
    49  		for i := 0; i < b.N; i++ {
    50  			objFalse.fprintf()
    51  		}
    52  	})
    53  	// should be the same as NoOp without a const true guard as the compiler should delete the if
    54  	b.Run("Fprintf const true", func(b *testing.B) {
    55  		for i := 0; i < b.N; i++ {
    56  			if const_debug_true {
    57  				fmt.Fprintf(io.Discard, "arg: %v", arg)
    58  			}
    59  		}
    60  	})
    61  	b.Run("Fprintf var true", func(b *testing.B) {
    62  		for i := 0; i < b.N; i++ {
    63  			if var_debug_true {
    64  				fmt.Fprintf(io.Discard, "arg: %v", arg)
    65  			}
    66  		}
    67  	})
    68  	objTrue := obj{true}
    69  	b.Run("Fprintf obj true", func(b *testing.B) {
    70  		for i := 0; i < b.N; i++ {
    71  			objTrue.fprintf()
    72  		}
    73  	})
    74  }