github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/vecmt/index_test.go (about)

     1  package vecmt
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/unicornultrafoundation/go-helios/hash"
     7  	"github.com/unicornultrafoundation/go-helios/native/dag"
     8  	"github.com/unicornultrafoundation/go-helios/native/dag/tdag"
     9  	"github.com/unicornultrafoundation/go-helios/native/pos"
    10  	"github.com/unicornultrafoundation/go-helios/u2udb/memorydb"
    11  
    12  	"github.com/unicornultrafoundation/go-u2u/native"
    13  )
    14  
    15  var (
    16  	testASCIIScheme = `
    17  a1.0   b1.0   c1.0   d1.0   e1.0
    18  ║      ║      ║      ║      ║
    19  ║      ╠──────╫───── d2.0   ║
    20  ║      ║      ║      ║      ║
    21  ║      b2.1 ──╫──────╣      e2.1
    22  ║      ║      ║      ║      ║
    23  ║      ╠──────╫───── d3.1   ║
    24  a2.1 ──╣      ║      ║      ║
    25  ║      ║      ║      ║      ║
    26  ║      b3.2 ──╣      ║      ║
    27  ║      ║      ║      ║      ║
    28  ║      ╠──────╫───── d4.2   ║
    29  ║      ║      ║      ║      ║
    30  ║      ╠───── c2.2   ║      e3.2
    31  ║      ║      ║      ║      ║
    32  `
    33  )
    34  
    35  type eventWithCreationTime struct {
    36  	dag.Event
    37  	creationTime native.Timestamp
    38  }
    39  
    40  func (e *eventWithCreationTime) CreationTime() native.Timestamp {
    41  	return e.creationTime
    42  }
    43  
    44  func BenchmarkIndex_Add(b *testing.B) {
    45  	b.StopTimer()
    46  	ordered := make(dag.Events, 0)
    47  	nodes, _, _ := tdag.ASCIIschemeForEach(testASCIIScheme, tdag.ForEachEvent{
    48  		Process: func(e dag.Event, name string) {
    49  			ordered = append(ordered, e)
    50  		},
    51  	})
    52  	validatorsBuilder := pos.NewBuilder()
    53  	for _, peer := range nodes {
    54  		validatorsBuilder.Set(peer, 1)
    55  	}
    56  	validators := validatorsBuilder.Build()
    57  	events := make(map[hash.Event]dag.Event)
    58  	getEvent := func(id hash.Event) dag.Event {
    59  		return events[id]
    60  	}
    61  	for _, e := range ordered {
    62  		events[e.ID()] = e
    63  	}
    64  
    65  	vecClock := NewIndex(func(err error) { panic(err) }, LiteConfig())
    66  	vecClock.Reset(validators, memorydb.New(), getEvent)
    67  
    68  	for i := 0; i < b.N; i++ {
    69  		b.StopTimer()
    70  		vecClock.Reset(validators, memorydb.New(), getEvent)
    71  		b.StartTimer()
    72  		for _, e := range ordered {
    73  			err := vecClock.Add(&eventWithCreationTime{e, native.Timestamp(e.Seq())})
    74  			if err != nil {
    75  				panic(err)
    76  			}
    77  			i++
    78  			if i >= b.N {
    79  				break
    80  			}
    81  		}
    82  	}
    83  }