github.com/koko1123/flow-go-1@v0.29.6/module/metrics/example/consensus/main.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/binary"
     5  	"math/rand"
     6  	"time"
     7  
     8  	"github.com/prometheus/client_golang/prometheus"
     9  	"github.com/rs/zerolog"
    10  
    11  	"github.com/koko1123/flow-go-1/model/flow"
    12  	"github.com/koko1123/flow-go-1/module/metrics"
    13  	"github.com/koko1123/flow-go-1/module/metrics/example"
    14  	"github.com/koko1123/flow-go-1/module/trace"
    15  	"github.com/koko1123/flow-go-1/network"
    16  	"github.com/koko1123/flow-go-1/network/channels"
    17  	"github.com/koko1123/flow-go-1/utils/unittest"
    18  )
    19  
    20  func main() {
    21  	example.WithMetricsServer(func(logger zerolog.Logger) {
    22  		tracer, err := trace.NewTracer(logger, "collection", "test", trace.SensitivityCaptureAll)
    23  		if err != nil {
    24  			panic(err)
    25  		}
    26  		collector := struct {
    27  			*metrics.HotstuffCollector
    28  			*metrics.ConsensusCollector
    29  			*metrics.NetworkCollector
    30  			*metrics.ComplianceCollector
    31  			*metrics.MempoolCollector
    32  		}{
    33  			HotstuffCollector:   metrics.NewHotstuffCollector("some_chain_id"),
    34  			ConsensusCollector:  metrics.NewConsensusCollector(tracer, prometheus.DefaultRegisterer),
    35  			NetworkCollector:    metrics.NewNetworkCollector(unittest.Logger()),
    36  			ComplianceCollector: metrics.NewComplianceCollector(),
    37  			MempoolCollector:    metrics.NewMempoolCollector(5 * time.Second),
    38  		}
    39  
    40  		for i := 0; i < 100; i++ {
    41  			block := unittest.BlockFixture()
    42  			collector.MempoolEntries(metrics.ResourceGuarantee, 22)
    43  			collector.BlockFinalized(&block)
    44  			collector.HotStuffBusyDuration(10, metrics.HotstuffEventTypeTimeout)
    45  			collector.HotStuffWaitDuration(10, metrics.HotstuffEventTypeTimeout)
    46  			collector.HotStuffIdleDuration(10)
    47  			collector.SetCurView(uint64(i))
    48  			collector.SetQCView(uint64(i))
    49  
    50  			entityID := make([]byte, 32)
    51  			binary.LittleEndian.PutUint32(entityID, uint32(i/6))
    52  
    53  			entity2ID := make([]byte, 32)
    54  			binary.LittleEndian.PutUint32(entity2ID, uint32(i/6+100000))
    55  			if i%6 == 0 {
    56  				collector.StartCollectionToFinalized(flow.HashToID(entityID))
    57  			} else if i%6 == 3 {
    58  				collector.FinishCollectionToFinalized(flow.HashToID(entityID))
    59  			}
    60  
    61  			if i%5 == 0 {
    62  				collector.StartBlockToSeal(flow.HashToID(entityID))
    63  			} else if i%6 == 3 {
    64  				collector.FinishBlockToSeal(flow.HashToID(entityID))
    65  			}
    66  
    67  			collProvider := channels.TestNetworkChannel.String()
    68  			collIngest := channels.TestMetricsChannel.String()
    69  			protocol1 := network.ProtocolTypeUnicast.String()
    70  			protocol2 := network.ProtocolTypePubSub.String()
    71  			message1 := "CollectionRequest"
    72  			message2 := "ClusterBlockProposal"
    73  
    74  			collector.OutboundMessageSent(rand.Intn(1000), collProvider, protocol1, message1)
    75  			collector.OutboundMessageSent(rand.Intn(1000), collIngest, protocol2, message2)
    76  
    77  			collector.InboundMessageReceived(rand.Intn(1000), collProvider, protocol1, message1)
    78  			collector.InboundMessageReceived(rand.Intn(1000), collIngest, protocol2, message2)
    79  
    80  			time.Sleep(1 * time.Second)
    81  		}
    82  	})
    83  }