github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/metrics/example/execution/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" 10 "github.com/onflow/flow-go/module/metrics" 11 "github.com/onflow/flow-go/module/metrics/example" 12 "github.com/onflow/flow-go/module/trace" 13 "github.com/onflow/flow-go/utils/unittest" 14 ) 15 16 // main runs a local tracer server on the machine and starts monitoring some metrics for sake of execution, which 17 // increases result approvals counter and checked chunks counter 100 times each 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.ExecutionCollector 27 *metrics.NetworkCollector 28 }{ 29 HotstuffCollector: metrics.NewHotstuffCollector("some_chain_id"), 30 ExecutionCollector: metrics.NewExecutionCollector(tracer), 31 NetworkCollector: metrics.NewNetworkCollector(unittest.Logger()), 32 } 33 diskTotal := rand.Int63n(1024 * 1024 * 1024) 34 for i := 0; i < 1000; i++ { 35 blockID := unittest.BlockFixture().ID() 36 collector.StartBlockReceivedToExecuted(blockID) 37 38 duration := time.Duration(rand.Int31n(2000)) * time.Millisecond 39 // adds a random delay for execution duration, between 0 and 2 seconds 40 time.Sleep(duration) 41 42 collector.ExecutionBlockExecuted( 43 duration, 44 module.ExecutionResultStats{ 45 ComputationUsed: uint64(rand.Int63n(1e6)), 46 MemoryUsed: uint64(rand.Int63n(1e6)), 47 EventCounts: 2, 48 EventSize: 100, 49 NumberOfCollections: 1, 50 NumberOfTransactions: 1, 51 }) 52 53 diskIncrease := rand.Int63n(1024 * 1024) 54 diskTotal += diskIncrease 55 collector.ExecutionStateStorageDiskTotal(diskTotal) 56 collector.ExecutionStorageStateCommitment(diskIncrease) 57 58 collector.FinishBlockReceivedToExecuted(blockID) 59 } 60 }) 61 }