github.com/evdatsion/aphelion-dpos-bft@v0.32.1/tools/tm-bench/transacter_test.go (about)

     1  package main
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"encoding/hex"
     6  	"encoding/json"
     7  	"fmt"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/pkg/errors"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  // This test tests that the output of generate tx and update tx is consistent
    17  func TestGenerateTxUpdateTxConsistentency(t *testing.T) {
    18  	cases := []struct {
    19  		connIndex        int
    20  		startingTxNumber int
    21  		txSize           int
    22  		hostname         string
    23  		numTxsToTest     int
    24  	}{
    25  		{0, 0, 40, "localhost:26657", 1000},
    26  		{70, 300, 10000, "localhost:26657", 1000},
    27  		{0, 50, 100000, "localhost:26657", 1000},
    28  	}
    29  
    30  	for tcIndex, tc := range cases {
    31  		hostnameHash := sha256.Sum256([]byte(tc.hostname))
    32  		// Tx generated from update tx. This is defined outside of the loop, since we have
    33  		// to a have something initially to update
    34  		updatedTx := generateTx(tc.connIndex, tc.startingTxNumber, tc.txSize, hostnameHash)
    35  		updatedHex := make([]byte, len(updatedTx)*2)
    36  		hex.Encode(updatedHex, updatedTx)
    37  		for i := 0; i < tc.numTxsToTest; i++ {
    38  			expectedTx := generateTx(tc.connIndex, tc.startingTxNumber+i, tc.txSize, hostnameHash)
    39  			expectedHex := make([]byte, len(expectedTx)*2)
    40  			hex.Encode(expectedHex, expectedTx)
    41  
    42  			updateTx(updatedTx, updatedHex, tc.startingTxNumber+i)
    43  
    44  			// after first 32 bytes is 8 bytes of time, then purely random bytes
    45  			require.Equal(t, expectedTx[:32], updatedTx[:32],
    46  				"First 32 bytes of the txs differed. tc #%d, i #%d", tcIndex, i)
    47  			require.Equal(t, expectedHex[:64], updatedHex[:64],
    48  				"First 64 bytes of the hex differed. tc #%d, i #%d", tcIndex, i)
    49  			// Test the lengths of the txs are as expected
    50  			require.Equal(t, tc.txSize, len(expectedTx),
    51  				"Length of expected Tx differed. tc #%d, i #%d", tcIndex, i)
    52  			require.Equal(t, tc.txSize, len(updatedTx),
    53  				"Length of expected Tx differed. tc #%d, i #%d", tcIndex, i)
    54  			require.Equal(t, tc.txSize*2, len(expectedHex),
    55  				"Length of expected hex differed. tc #%d, i #%d", tcIndex, i)
    56  			require.Equal(t, tc.txSize*2, len(updatedHex),
    57  				"Length of updated hex differed. tc #%d, i #%d", tcIndex, i)
    58  		}
    59  	}
    60  }
    61  
    62  func BenchmarkIterationOfSendLoop(b *testing.B) {
    63  	var (
    64  		connIndex = 0
    65  		txSize    = 25000
    66  	)
    67  
    68  	now := time.Now()
    69  	// something too far away to matter
    70  	endTime := now.Add(time.Hour)
    71  	txNumber := 0
    72  	hostnameHash := sha256.Sum256([]byte{0})
    73  	tx := generateTx(connIndex, txNumber, txSize, hostnameHash)
    74  	txHex := make([]byte, len(tx)*2)
    75  	hex.Encode(txHex, tx)
    76  	b.ResetTimer()
    77  	for i := 0; i < b.N; i++ {
    78  		updateTx(tx, txHex, txNumber)
    79  		paramsJSON, err := json.Marshal(map[string]interface{}{"tx": txHex})
    80  		if err != nil {
    81  			fmt.Printf("failed to encode params: %v\n", err)
    82  			os.Exit(1)
    83  		}
    84  		_ = json.RawMessage(paramsJSON)
    85  		_ = now.Add(sendTimeout)
    86  
    87  		if err != nil {
    88  			err = errors.Wrap(err,
    89  				fmt.Sprintf("txs send failed on connection #%d", connIndex))
    90  			logger.Error(err.Error())
    91  			return
    92  		}
    93  
    94  		// Cache the now operations
    95  		if i%5 == 0 {
    96  			now = time.Now()
    97  			if now.After(endTime) {
    98  				break
    99  			}
   100  		}
   101  
   102  		txNumber++
   103  	}
   104  }