github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/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/onflow/flow-go/model/flow"
    12  	"github.com/onflow/flow-go/module/metrics"
    13  	"github.com/onflow/flow-go/module/metrics/example"
    14  	"github.com/onflow/flow-go/module/trace"
    15  	"github.com/onflow/flow-go/network/channels"
    16  	"github.com/onflow/flow-go/network/message"
    17  	"github.com/onflow/flow-go/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.HotstuffEventTypeLocalTimeout)
    45  			collector.HotStuffWaitDuration(10, metrics.HotstuffEventTypeLocalTimeout)
    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 := message.ProtocolTypeUnicast.String()
    70  			protocol2 := message.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  }