github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/sequencer/efficiencylist_test.go (about)

     1  package sequencer
     2  
     3  import (
     4  	"crypto/rand"
     5  	"fmt"
     6  	"math/big"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  // randomFloat64 is a shortcut for generating a random float between 0 and 1 using crypto/rand.
    12  func randomFloat64() float64 {
    13  	nBig, err := rand.Int(rand.Reader, big.NewInt(1<<53))
    14  	if err != nil {
    15  		panic(err)
    16  	}
    17  	return float64(nBig.Int64()) / (1 << 53)
    18  }
    19  
    20  func TestEfficiencyListSort(t *testing.T) {
    21  	el := newEfficiencyList()
    22  	nItems := 100
    23  
    24  	for i := 0; i < nItems; i++ {
    25  		el.add(&TxTracker{HashStr: fmt.Sprintf("0x%d", i), Efficiency: randomFloat64()})
    26  	}
    27  
    28  	for i := 0; i < nItems-1; i++ {
    29  		if !(el.getByIndex(i).Efficiency > el.getByIndex(i+1).Efficiency) {
    30  			t.Fatalf("Sort error. [%d].Efficiency(%f) < [%d].Efficiency(%f)", i, el.getByIndex(i).Efficiency, i+1, el.getByIndex(i+1).Efficiency)
    31  		}
    32  	}
    33  
    34  	// el.print()
    35  
    36  	if el.len() != nItems {
    37  		t.Fatalf("Length error. Length %d. Expected %d", el.len(), nItems)
    38  	}
    39  }
    40  
    41  func TestEfficiencyListDelete(t *testing.T) {
    42  	el := newEfficiencyList()
    43  
    44  	el.add(&TxTracker{HashStr: "0x01", Efficiency: 1})
    45  	el.add(&TxTracker{HashStr: "0x02", Efficiency: 2})
    46  	el.add(&TxTracker{HashStr: "0x03", Efficiency: 2})
    47  	el.add(&TxTracker{HashStr: "0x04", Efficiency: 3})
    48  	el.add(&TxTracker{HashStr: "0x05", Efficiency: 10})
    49  	el.add(&TxTracker{HashStr: "0x06", Efficiency: 1.5})
    50  	el.add(&TxTracker{HashStr: "0x07", Efficiency: 1.5})
    51  
    52  	deltxs := []string{"0x03", "0x07", "0x01", "0x05"}
    53  
    54  	for _, deltx := range deltxs {
    55  		count := el.len()
    56  		el.delete(&TxTracker{HashStr: deltx})
    57  
    58  		for i := 0; i < el.len(); i++ {
    59  			if el.getByIndex(i).HashStr == deltx {
    60  				t.Fatalf("Delete error. %s tx was not deleted", deltx)
    61  			}
    62  		}
    63  
    64  		if el.len() != count-1 {
    65  			t.Fatalf("Length error. Length %d. Expected %d", el.len(), count)
    66  		}
    67  	}
    68  }
    69  
    70  func TestEfficiencyListBench(t *testing.T) {
    71  	el := newEfficiencyList()
    72  
    73  	start := time.Now()
    74  	for i := 0; i < 10000; i++ {
    75  		el.add(&TxTracker{HashStr: fmt.Sprintf("0x%d", i), Efficiency: randomFloat64()})
    76  	}
    77  	elapsed := time.Since(start)
    78  	t.Logf("EfficiencyList adding 10000 items took %s", elapsed)
    79  
    80  	start = time.Now()
    81  	el.add(&TxTracker{HashStr: fmt.Sprintf("0x%d", 10001), Efficiency: randomFloat64()})
    82  	elapsed = time.Since(start)
    83  	t.Logf("EfficiencyList adding the 10001 item (efficiency=random) took %s", elapsed)
    84  
    85  	start = time.Now()
    86  	el.add(&TxTracker{HashStr: fmt.Sprintf("0x%d", 10002), Efficiency: 0})
    87  	elapsed = time.Since(start)
    88  	t.Logf("EfficiencyList adding the 10002 item (efficiency=0) took %s", elapsed)
    89  
    90  	start = time.Now()
    91  	el.add(&TxTracker{HashStr: fmt.Sprintf("0x%d", 10003), Efficiency: 1000})
    92  	elapsed = time.Since(start)
    93  	t.Logf("EfficiencyList adding the 10003 item (efficiency=1000) took %s", elapsed)
    94  }