github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/x/contextx/value_test.go (about)

     1  package contextx_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
     8  )
     9  
    10  type key struct{}
    11  
    12  func getValue(ctx context.Context) bool {
    13  	v := ctx.Value(key{})
    14  	_ = v
    15  	return true
    16  }
    17  
    18  func BenchmarkWithValue(b *testing.B) {
    19  	parent := context.Background()
    20  
    21  	b.Run("std.Context", func(b *testing.B) {
    22  		for i := 0; i < b.N; i++ {
    23  			ctx := context.WithValue(parent, key{}, nil)
    24  			getValue(ctx)
    25  		}
    26  	})
    27  	b.Run("qkit.Context", func(b *testing.B) {
    28  		for i := 0; i < b.N; i++ {
    29  			ctx := contextx.WithValue(parent, key{}, nil)
    30  			getValue(ctx)
    31  		}
    32  	})
    33  }