github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/stack/record_test.go (about)

     1  package stack
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  type testStruct struct {
    10  	depth int
    11  	opts  []recordOption
    12  }
    13  
    14  func (s testStruct) TestFunc() string {
    15  	return func() string {
    16  		return Record(s.depth, s.opts...)
    17  	}()
    18  }
    19  
    20  func (s *testStruct) TestPointerFunc() string {
    21  	return func() string {
    22  		return Record(s.depth, s.opts...)
    23  	}()
    24  }
    25  
    26  func TestRecord(t *testing.T) {
    27  	for _, tt := range []struct {
    28  		act string
    29  		exp string
    30  	}{
    31  		{
    32  			act: Record(0),
    33  			exp: "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack.TestRecord(record_test.go:32)",
    34  		},
    35  		{
    36  			act: func() string {
    37  				return Record(1)
    38  			}(),
    39  			exp: "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack.TestRecord(record_test.go:38)",
    40  		},
    41  		{
    42  			act: func() string {
    43  				return func() string {
    44  					return Record(2)
    45  				}()
    46  			}(),
    47  			exp: "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack.TestRecord(record_test.go:46)",
    48  		},
    49  		{
    50  			act: testStruct{depth: 0, opts: []recordOption{
    51  				PackagePath(false),
    52  				PackageName(false),
    53  				StructName(false),
    54  				FunctionName(false),
    55  				Lambda(false),
    56  				FileName(false),
    57  				Line(false),
    58  			}}.TestFunc(),
    59  			exp: "",
    60  		},
    61  		{
    62  			act: testStruct{depth: 0, opts: []recordOption{
    63  				// PackagePath(false),
    64  				PackageName(false),
    65  				StructName(false),
    66  				FunctionName(false),
    67  				Lambda(false),
    68  				FileName(false),
    69  				Line(false),
    70  			}}.TestFunc(),
    71  			exp: "github.com/ydb-platform/ydb-go-sdk/v3/internal",
    72  		},
    73  		{
    74  			act: testStruct{depth: 0, opts: []recordOption{
    75  				PackagePath(false),
    76  				// PackageName(false),
    77  				StructName(false),
    78  				FunctionName(false),
    79  				Lambda(false),
    80  				FileName(false),
    81  				Line(false),
    82  			}}.TestFunc(),
    83  			exp: "stack",
    84  		},
    85  		{
    86  			act: testStruct{depth: 0, opts: []recordOption{
    87  				PackagePath(false),
    88  				PackageName(false),
    89  				// StructName(false),
    90  				FunctionName(false),
    91  				Lambda(false),
    92  				FileName(false),
    93  				Line(false),
    94  			}}.TestFunc(),
    95  			exp: "testStruct",
    96  		},
    97  		{
    98  			act: testStruct{depth: 0, opts: []recordOption{
    99  				PackagePath(false),
   100  				PackageName(false),
   101  				StructName(false),
   102  				// FunctionName(false),
   103  				Lambda(false),
   104  				FileName(false),
   105  				Line(false),
   106  			}}.TestFunc(),
   107  			exp: "TestFunc",
   108  		},
   109  		{
   110  			act: testStruct{depth: 0, opts: []recordOption{
   111  				PackagePath(false),
   112  				PackageName(false),
   113  				StructName(false),
   114  				FunctionName(false),
   115  				// Lambda(false),
   116  				FileName(false),
   117  				Line(false),
   118  			}}.TestFunc(),
   119  			exp: "",
   120  		},
   121  		{
   122  			act: testStruct{depth: 0, opts: []recordOption{
   123  				PackagePath(false),
   124  				PackageName(false),
   125  				StructName(false),
   126  				// FunctionName(false),
   127  				// Lambda(false),
   128  				FileName(false),
   129  				Line(false),
   130  			}}.TestFunc(),
   131  			exp: "TestFunc.func1",
   132  		},
   133  		{
   134  			act: testStruct{depth: 0, opts: []recordOption{
   135  				PackagePath(false),
   136  				PackageName(false),
   137  				StructName(false),
   138  				FunctionName(false),
   139  				Lambda(false),
   140  				// FileName(false),
   141  				Line(false),
   142  			}}.TestFunc(),
   143  			exp: "record_test.go",
   144  		},
   145  		{
   146  			act: testStruct{depth: 0, opts: []recordOption{
   147  				PackagePath(false),
   148  				PackageName(false),
   149  				StructName(false),
   150  				FunctionName(false),
   151  				Lambda(false),
   152  				FileName(false),
   153  				// Line(false),
   154  			}}.TestFunc(),
   155  			exp: "",
   156  		},
   157  		{
   158  			act: testStruct{depth: 0, opts: []recordOption{
   159  				PackagePath(false),
   160  				PackageName(false),
   161  				StructName(false),
   162  				FunctionName(false),
   163  				Lambda(false),
   164  				// FileName(false),
   165  				// Line(false),
   166  			}}.TestFunc(),
   167  			exp: "record_test.go:16",
   168  		},
   169  		{
   170  			act: testStruct{depth: 0, opts: []recordOption{
   171  				// PackagePath(false),
   172  				// PackageName(false),
   173  				StructName(false),
   174  				FunctionName(false),
   175  				Lambda(false),
   176  				FileName(false),
   177  				Line(false),
   178  			}}.TestFunc(),
   179  			exp: "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack",
   180  		},
   181  		{
   182  			act: testStruct{depth: 0, opts: []recordOption{
   183  				// PackagePath(false),
   184  				// PackageName(false),
   185  				// StructName(false),
   186  				FunctionName(false),
   187  				Lambda(false),
   188  				FileName(false),
   189  				Line(false),
   190  			}}.TestFunc(),
   191  			exp: "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack.testStruct",
   192  		},
   193  		{
   194  			act: testStruct{depth: 0, opts: []recordOption{
   195  				// PackagePath(false),
   196  				// PackageName(false),
   197  				// StructName(false),
   198  				// FunctionName(false),
   199  				Lambda(false),
   200  				FileName(false),
   201  				Line(false),
   202  			}}.TestFunc(),
   203  			exp: "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack.testStruct.TestFunc",
   204  		},
   205  		{
   206  			act: testStruct{depth: 0, opts: []recordOption{
   207  				// PackagePath(false),
   208  				// PackageName(false),
   209  				// StructName(false),
   210  				// FunctionName(false),
   211  				// Lambda(false),
   212  				FileName(false),
   213  				Line(false),
   214  			}}.TestFunc(),
   215  			exp: "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack.testStruct.TestFunc.func1",
   216  		},
   217  		{
   218  			act: testStruct{depth: 0, opts: []recordOption{
   219  				// PackagePath(false),
   220  				// PackageName(false),
   221  				// StructName(false),
   222  				// FunctionName(false),
   223  				// Lambda(false),
   224  				// FileName(false),
   225  				Line(false),
   226  			}}.TestFunc(),
   227  			exp: "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack.testStruct.TestFunc.func1(record_test.go)",
   228  		},
   229  		{
   230  			act: testStruct{depth: 0, opts: []recordOption{
   231  				// PackagePath(false),
   232  				// PackageName(false),
   233  				// StructName(false),
   234  				// FunctionName(false),
   235  				// Lambda(false),
   236  				// FileName(false),
   237  				// Line(false),
   238  			}}.TestFunc(),
   239  			exp: "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack.testStruct.TestFunc.func1(record_test.go:16)",
   240  		},
   241  		{
   242  			act: (&testStruct{depth: 0, opts: []recordOption{
   243  				// PackagePath(false),
   244  				// PackageName(false),
   245  				// StructName(false),
   246  				// FunctionName(false),
   247  				// Lambda(false),
   248  				// FileName(false),
   249  				// Line(false),
   250  			}}).TestPointerFunc(),
   251  			exp: "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack.(*testStruct).TestPointerFunc.func1(record_test.go:22)",
   252  		},
   253  	} {
   254  		t.Run("", func(t *testing.T) {
   255  			require.Equal(t, tt.exp, tt.act)
   256  		})
   257  	}
   258  }
   259  
   260  func BenchmarkCall(b *testing.B) {
   261  	b.ReportAllocs()
   262  	for i := 0; i < b.N; i++ {
   263  		_ = Call(0)
   264  	}
   265  }
   266  
   267  func BenchmarkRecord(b *testing.B) {
   268  	b.ReportAllocs()
   269  	for i := 0; i < b.N; i++ {
   270  		_ = Record(0)
   271  	}
   272  }
   273  
   274  func BenchmarkCallRecord(b *testing.B) {
   275  	b.ReportAllocs()
   276  	for i := 0; i < b.N; i++ {
   277  		_ = Call(0).Record()
   278  	}
   279  }
   280  
   281  func BenchmarkCallFuncionID(b *testing.B) {
   282  	b.ReportAllocs()
   283  	for i := 0; i < b.N; i++ {
   284  		_ = Call(0).FunctionID()
   285  	}
   286  }