github.com/aergoio/aergo@v1.3.1/p2p/mofactory_test.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2p
     7  
     8  import (
     9  	"github.com/aergoio/aergo/p2p/p2pcommon"
    10  	"github.com/gofrs/uuid"
    11  	"math/rand"
    12  	"testing"
    13  
    14  	"github.com/aergoio/aergo/types"
    15  )
    16  
    17  var sampleTxIDs []types.TxID
    18  var Tx5000, Tx1000, Tx100, Tx10, Tx1 []types.TxID
    19  
    20  const hashCnt = 5000
    21  
    22  func init() {
    23  	buf := make([]byte, types.HashIDLength)
    24  	sampleTxIDs = make([]types.TxID, hashCnt)
    25  	for i := 0; i < hashCnt; i++ {
    26  		rand.Read(buf)
    27  		sampleTxIDs[i] = types.ToTxID(buf)
    28  	}
    29  	Tx5000 = sampleTxIDs
    30  	Tx1000 = make([]types.TxID, 1000)
    31  	copy(Tx1000, sampleTxIDs)
    32  	Tx100 = make([]types.TxID, 100)
    33  	copy(Tx100, sampleTxIDs)
    34  	Tx10 = make([]types.TxID, 10)
    35  	copy(Tx10, sampleTxIDs)
    36  	Tx1 = make([]types.TxID, 1)
    37  	copy(Tx1, sampleTxIDs)
    38  }
    39  
    40  func BenchmarkBaseMOFactory_NewMsgTxBroadcastOrder(b *testing.B) {
    41  	dummyP2PS := &P2P{}
    42  	dummyTNT := &txNoticeTracer{}
    43  
    44  	benchmarks := []struct {
    45  		name string
    46  		in   []types.TxID
    47  	}{
    48  		{"B1", Tx1},
    49  		{"B10", Tx10},
    50  		{"B100", Tx100},
    51  		{"B1000", Tx1000},
    52  		{"B5000", Tx5000},
    53  	}
    54  	for _, bm := range benchmarks {
    55  		b.Run(bm.name, func(b *testing.B) {
    56  			for i := 0; i < b.N; i++ {
    57  				mf := &baseMOFactory{
    58  					p2ps: dummyP2PS,
    59  					tnt:  dummyTNT,
    60  				}
    61  				in := bm.in
    62  				hashes := make([][]byte, 0, len(in))
    63  				for _, hash := range in {
    64  					hashes = append(hashes, hash[:])
    65  				}
    66  
    67  				_ = mf.NewMsgTxBroadcastOrder(&types.NewTransactionsNotice{TxHashes: hashes})
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  func BenchmarkBaseMOFactory_DiffFunc(b *testing.B) {
    74  	dummyP2PS := &P2P{}
    75  	dummyTNT := &txNoticeTracer{}
    76  
    77  	benchmarks := []struct {
    78  		name string
    79  		in   []types.TxID
    80  	}{
    81  		{"B1", Tx1},
    82  		{"B10", Tx10},
    83  		{"B100", Tx100},
    84  		{"B1000", Tx1000},
    85  		{"B5000", Tx5000},
    86  	}
    87  	for _, bm := range benchmarks {
    88  		b.Run(bm.name, func(b *testing.B) {
    89  			for i := 0; i < b.N; i++ {
    90  				mf := &baseMOFactory{
    91  					p2ps: dummyP2PS,
    92  					tnt:  dummyTNT,
    93  				}
    94  				_ = mf.diffMsgTxBroadcastOrder(bm.in)
    95  			}
    96  		})
    97  	}
    98  }
    99  
   100  func (mf *baseMOFactory) diffMsgTxBroadcastOrder(ids []types.TxID) p2pcommon.MsgOrder {
   101  	rmo := &pbTxNoticeOrder{}
   102  	reqID := uuid.Must(uuid.NewV4())
   103  	hashes := make([][]byte, len(ids))
   104  	for i, hash := range ids {
   105  		hashes[i] = hash[:]
   106  	}
   107  	message := &types.NewTransactionsNotice{TxHashes: hashes}
   108  	if mf.fillUpMsgOrder(&rmo.pbMessageOrder, reqID, uuid.Nil, p2pcommon.NewTxNotice, message) {
   109  		rmo.tnt = mf.tnt
   110  		rmo.txHashes = ids
   111  		return rmo
   112  	}
   113  	return nil
   114  }