github.com/koko1123/flow-go-1@v0.29.6/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/koko1123/flow-go-1/module/metrics"
    10  	"github.com/koko1123/flow-go-1/module/metrics/example"
    11  	"github.com/koko1123/flow-go-1/module/trace"
    12  	"github.com/koko1123/flow-go-1/network"
    13  	"github.com/koko1123/flow-go-1/network/channels"
    14  	"github.com/koko1123/flow-go-1/network/queue"
    15  	"github.com/koko1123/flow-go-1/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 := network.ProtocolTypeUnicast.String()
    37  		protocol2 := network.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.HotstuffEventTypeTimeout)
    44  			collector.HotStuffWaitDuration(10, metrics.HotstuffEventTypeTimeout)
    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  }