github.com/koko1123/flow-go-1@v0.29.6/module/trace/trace_test.go (about)

     1  package trace
     2  
     3  import (
     4  	"context"
     5  	"math/rand"
     6  	"testing"
     7  
     8  	"github.com/rs/zerolog"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/koko1123/flow-go-1/model/flow"
    12  )
    13  
    14  func BenchmarkStartSpanFromParent(b *testing.B) {
    15  	tracer, err := NewTracer(zerolog.Logger{}, "test", string(flow.Localnet), 0)
    16  	require.NoError(b, err)
    17  
    18  	tracer.Ready()
    19  	defer tracer.Done()
    20  
    21  	span, _ := tracer.StartTransactionSpan(context.Background(), flow.Identifier{}, "test")
    22  	defer span.End()
    23  
    24  	b.ResetTimer()
    25  	for i := 0; i < b.N; i++ {
    26  		s := tracer.StartSpanFromParent(span, SpanName("test"))
    27  		s.End()
    28  	}
    29  	b.StopTimer()
    30  }
    31  
    32  func BenchmarkStartTransactionSpan(b *testing.B) {
    33  	tracer, err := NewTracer(zerolog.Logger{}, "test", string(flow.Localnet), 0)
    34  	require.NoError(b, err)
    35  
    36  	tracer.Ready()
    37  	defer tracer.Done()
    38  
    39  	for _, t := range []struct {
    40  		name string
    41  		n    int
    42  	}{
    43  		{name: "cacheHit", n: 100},
    44  		{name: "cacheMiss", n: 100000},
    45  	} {
    46  		t := t
    47  		b.Run(t.name, func(b *testing.B) {
    48  			randomIDs := make([]flow.Identifier, 0, t.n)
    49  			for i := 0; i < t.n; i++ {
    50  				var randomBytes [flow.IdentifierLen]byte
    51  				_, err := rand.Read(randomBytes[:])
    52  				require.NoError(b, err)
    53  				randomIDs = append(randomIDs, flow.Identifier(randomBytes))
    54  			}
    55  
    56  			b.ResetTimer()
    57  			for i := 0; i < b.N; i++ {
    58  				span, _ := tracer.StartTransactionSpan(context.Background(), randomIDs[i%t.n], "test")
    59  				span.End()
    60  			}
    61  			b.StopTimer()
    62  		})
    63  	}
    64  }