github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/metrics/example/collection/main.go (about)

     1  package main
     2  
     3  import (
     4  	"math/rand"
     5  	"time"
     6  
     7  	"github.com/rs/zerolog"
     8  
     9  	"github.com/onflow/flow-go/module/metrics"
    10  	"github.com/onflow/flow-go/module/metrics/example"
    11  	"github.com/onflow/flow-go/module/trace"
    12  	"github.com/onflow/flow-go/network/channels"
    13  	"github.com/onflow/flow-go/network/message"
    14  	"github.com/onflow/flow-go/network/queue"
    15  	"github.com/onflow/flow-go/utils/unittest"
    16  )
    17  
    18  func main() {
    19  	example.WithMetricsServer(func(logger zerolog.Logger) {
    20  		tracer, err := trace.NewTracer(logger, "collection", "test", trace.SensitivityCaptureAll)
    21  		if err != nil {
    22  			panic(err)
    23  		}
    24  		collector := struct {
    25  			*metrics.HotstuffCollector
    26  			*metrics.CollectionCollector
    27  			*metrics.NetworkCollector
    28  		}{
    29  			HotstuffCollector:   metrics.NewHotstuffCollector("some_chain_id"),
    30  			CollectionCollector: metrics.NewCollectionCollector(tracer),
    31  			NetworkCollector:    metrics.NewNetworkCollector(unittest.Logger()),
    32  		}
    33  
    34  		topic1 := channels.TestNetworkChannel.String()
    35  		topic2 := channels.TestMetricsChannel.String()
    36  		protocol1 := message.ProtocolTypeUnicast.String()
    37  		protocol2 := message.ProtocolTypePubSub.String()
    38  		message1 := "CollectionRequest"
    39  		message2 := "ClusterBlockProposal"
    40  
    41  		for i := 0; i < 100; i++ {
    42  			collector.TransactionIngested(unittest.IdentifierFixture())
    43  			collector.HotStuffBusyDuration(10, metrics.HotstuffEventTypeLocalTimeout)
    44  			collector.HotStuffWaitDuration(10, metrics.HotstuffEventTypeLocalTimeout)
    45  			collector.HotStuffIdleDuration(10)
    46  			collector.SetCurView(uint64(i))
    47  			collector.SetQCView(uint64(i))
    48  
    49  			collector.OutboundMessageSent(rand.Intn(1000), topic1, protocol1, message1)
    50  			collector.OutboundMessageSent(rand.Intn(1000), topic2, protocol2, message2)
    51  
    52  			collector.InboundMessageReceived(rand.Intn(1000), topic1, protocol1, message1)
    53  			collector.InboundMessageReceived(rand.Intn(1000), topic2, protocol2, message2)
    54  
    55  			priority1 := rand.Intn(int(queue.HighPriority-queue.LowPriority+1)) + int(queue.LowPriority)
    56  			collector.MessageRemoved(priority1)
    57  			collector.QueueDuration(time.Millisecond*time.Duration(rand.Intn(1000)), priority1)
    58  
    59  			priority2 := rand.Intn(int(queue.HighPriority-queue.LowPriority+1)) + int(queue.LowPriority)
    60  			collector.MessageAdded(priority2)
    61  			time.Sleep(1 * time.Second)
    62  		}
    63  	})
    64  }