github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/state/impl_records_storage_benchmark_test.go (about)

     1  /*
     2   * Copyright (c) 2022-present unTill Pro, Ltd.
     3   */
     4  
     5  package state
     6  
     7  import (
     8  	"context"
     9  	"testing"
    10  
    11  	"github.com/voedger/voedger/pkg/appdef"
    12  	"github.com/voedger/voedger/pkg/istructs"
    13  )
    14  
    15  /*
    16  Before:
    17  BenchmarkRecordsGet-20    	 3476623	       332.4 ns/op	     104 B/op	       5 allocs/op
    18  After:
    19  BenchmarkRecordsGet-20    	23072106	        55.72 ns/op	      16 B/op	       1 allocs/op
    20  */
    21  func BenchmarkRecordsGet(b *testing.B) {
    22  	mockRec = &mockBenchRec{}
    23  	appStructs := &mockAppStr{}
    24  	s := ProvideQueryProcessorStateFactory()(context.Background(), appStructsFunc(appStructs), nil, SimpleWSIDFunc(istructs.WSID(1)), nil, nil, nil, nil, nil, nil, nil, nil)
    25  	k1, err := s.KeyBuilder(Record, appdef.NullQName)
    26  	if err != nil {
    27  		panic(err)
    28  	}
    29  	k1.PutRecordID(Field_ID, 2)
    30  	k1.PutInt64(Field_WSID, 1)
    31  
    32  	for i := 0; i < b.N; i++ {
    33  		value, ok, err := s.CanExist(k1)
    34  		if err != nil {
    35  			panic(err)
    36  		}
    37  		if value == nil {
    38  			panic("value is nil")
    39  		}
    40  		if !ok {
    41  			panic("!ok")
    42  		}
    43  	}
    44  }
    45  
    46  type mockBenchRecs struct {
    47  	istructs.IRecords
    48  }
    49  
    50  type mockBenchRec struct {
    51  	istructs.IRecord
    52  }
    53  
    54  func (r *mockBenchRec) QName() appdef.QName {
    55  	return testRecordQName1
    56  }
    57  
    58  var mockRec istructs.IRecord
    59  
    60  type mockAppStr struct {
    61  	istructs.IAppStructs
    62  	recs mockBenchRecs
    63  	vr   nilViewRecords
    64  }
    65  
    66  func (s *mockAppStr) Records() istructs.IRecords         { return &s.recs }
    67  func (s *mockAppStr) ViewRecords() istructs.IViewRecords { return &s.vr }
    68  
    69  //	func (r *mockBenchRecs) GetBatch(workspace istructs.WSID, highConsistency bool, ids []istructs.RecordGetBatchItem) (err error) {
    70  //		return r.Called(workspace, highConsistency, ids).Error(0)
    71  //	}
    72  func (r *mockBenchRecs) Get(workspace istructs.WSID, highConsistency bool, id istructs.RecordID) (record istructs.IRecord, err error) {
    73  	return mockRec, nil
    74  }
    75  func (r *mockBenchRecs) GetBatch(workspace istructs.WSID, highConsistency bool, ids []istructs.RecordGetBatchItem) (err error) {
    76  	for i := range ids {
    77  		ids[i].Record = mockRec
    78  	}
    79  	return nil
    80  }