github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/test/benchmarks/sequencer/common/metrics/metrics.go (about)

     1  package metrics
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"os/exec"
     7  	"time"
     8  
     9  	"github.com/0xPolygon/supernets2-node/log"
    10  	metricsLib "github.com/0xPolygon/supernets2-node/metrics"
    11  	"github.com/0xPolygon/supernets2-node/sequencer/metrics"
    12  	metricsState "github.com/0xPolygon/supernets2-node/state/metrics"
    13  	"github.com/0xPolygon/supernets2-node/test/benchmarks/sequencer/common/params"
    14  	"github.com/0xPolygon/supernets2-node/test/testutils"
    15  )
    16  
    17  const (
    18  	oneHundred    = 100
    19  	profilingPort = 6060
    20  )
    21  
    22  // CalculateAndPrint calculates and prints the results
    23  func CalculateAndPrint(prometheusResp *http.Response, profilingResult string, elapsed time.Duration, sequencerTimeSub, executorTimeSub float64, nTxs int) {
    24  	var (
    25  		sequencerTime, executorTime, workerTime float64
    26  		err                                     error
    27  	)
    28  	if prometheusResp != nil {
    29  		sequencerTime, executorTime, workerTime, err = GetValues(prometheusResp)
    30  		if err != nil {
    31  			log.Fatalf("error getting prometheus metrics: %v", err)
    32  		}
    33  	}
    34  
    35  	log.Info("##########")
    36  	log.Info("# Result #")
    37  	log.Info("##########")
    38  	log.Infof("Total time (including setup of environment and starting containers): %v", elapsed)
    39  
    40  	if prometheusResp != nil {
    41  		log.Info("######################")
    42  		log.Info("# Prometheus Metrics #")
    43  		log.Info("######################")
    44  		actualTotalTime := sequencerTime - sequencerTimeSub
    45  		actualExecutorTime := executorTime - executorTimeSub
    46  		PrintPrometheus(actualTotalTime, actualExecutorTime, workerTime)
    47  		log.Infof("[Transactions per second]: %v", float64(nTxs)/actualTotalTime)
    48  	}
    49  	if profilingResult != "" {
    50  		log.Info("#####################")
    51  		log.Info("# Profiling Metrics #")
    52  		log.Info("#####################")
    53  		log.Infof("%v", profilingResult)
    54  	}
    55  }
    56  
    57  // PrintPrometheus prints the prometheus metrics
    58  func PrintPrometheus(totalTime float64, executorTime float64, workerTime float64) {
    59  	log.Infof("[TOTAL Processing Time]: %v s", totalTime)
    60  	log.Infof("[EXECUTOR Processing Time]: %v s", executorTime)
    61  	log.Infof("[SEQUENCER Processing Time]: %v s", totalTime-executorTime)
    62  	log.Infof("[WORKER Processing Time]: %v s", workerTime)
    63  	log.Infof("[EXECUTOR Time Percentage from TOTAL]: %.2f %%", (executorTime/totalTime)*oneHundred)
    64  	log.Infof("[WORKER Time Percentage from TOTAL]: %.2f %%", (workerTime/totalTime)*oneHundred)
    65  }
    66  
    67  // GetValues gets the prometheus metric values
    68  func GetValues(metricsResponse *http.Response) (float64, float64, float64, error) {
    69  	var err error
    70  	if metricsResponse == nil {
    71  		metricsResponse, err = FetchPrometheus()
    72  		if err != nil {
    73  			log.Fatalf("error getting prometheus metrics: %v", err)
    74  		}
    75  	}
    76  
    77  	mf, err := testutils.ParseMetricFamilies(metricsResponse.Body)
    78  	if err != nil {
    79  		return 0, 0, 0, err
    80  	}
    81  	sequencerTotalProcessingTimeHisto := mf[metrics.ProcessingTimeName].Metric[0].Histogram
    82  	sequencerTotalProcessingTime := sequencerTotalProcessingTimeHisto.GetSampleSum()
    83  
    84  	workerTotalProcessingTimeHisto := mf[metrics.WorkerProcessingTimeName].Metric[0].Histogram
    85  	workerTotalProcessingTime := workerTotalProcessingTimeHisto.GetSampleSum()
    86  
    87  	executorTotalProcessingTimeHisto := mf[metricsState.ExecutorProcessingTimeName].Metric[0].Histogram
    88  	executorTotalProcessingTime := executorTotalProcessingTimeHisto.GetSampleSum()
    89  	return sequencerTotalProcessingTime, executorTotalProcessingTime, workerTotalProcessingTime, nil
    90  }
    91  
    92  // FetchPrometheus fetches the prometheus metrics
    93  func FetchPrometheus() (*http.Response, error) {
    94  	log.Infof("Fetching prometheus metrics ...")
    95  	return http.Get(fmt.Sprintf("http://localhost:%d%s", params.PrometheusPort, metricsLib.Endpoint))
    96  }
    97  
    98  // FetchProfiling fetches the profiling metrics
    99  func FetchProfiling() (string, error) {
   100  	fullUrl := fmt.Sprintf("http://localhost:%d%s", profilingPort, metricsLib.ProfileEndpoint)
   101  	log.Infof("Fetching profiling metrics from: %s ...", fullUrl)
   102  	cmd := exec.Command("go", "tool", "pprof", "-show=sequencer", "-top", fullUrl)
   103  	out, err := cmd.CombinedOutput()
   104  	if err != nil {
   105  		log.Fatalf("Error running pprof: %v\n%s", err, out)
   106  	}
   107  	return string(out), err
   108  }