code.vegaprotocol.io/vega@v0.79.0/core/processor/stats.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package processor
    17  
    18  import (
    19  	"time"
    20  
    21  	"code.vegaprotocol.io/vega/logging"
    22  )
    23  
    24  const (
    25  	// Maximum sample size for average calculation, used in statistics (average tx per block etc).
    26  	statsSampleSize = 5000
    27  )
    28  
    29  // setBatchStats is used to calculate any statistics that should be
    30  // recorded once per batch, typically called from commit.
    31  func (app *App) setBatchStats() {
    32  	// Calculate the average total txn per batch, over n blocks
    33  	app.txTotals = append(app.txTotals, app.stats.TotalTxLastBatch())
    34  	totalTx := uint64(0)
    35  	for _, itx := range app.txTotals {
    36  		totalTx += itx
    37  	}
    38  	averageTxTotal := totalTx / uint64(len(app.txTotals))
    39  
    40  	app.stats.SetAverageTxPerBatch(averageTxTotal)
    41  	app.stats.SetTotalTxLastBatch(app.stats.TotalTxCurrentBatch())
    42  	app.stats.SetTotalTxCurrentBatch(0)
    43  
    44  	// MAX sample size for avg calculation is defined as const.
    45  	if len(app.txTotals) == statsSampleSize {
    46  		app.txTotals = app.txTotals[:0]
    47  	}
    48  }
    49  
    50  func (app *App) updateStats() {
    51  	app.stats.IncTotalBatches()
    52  	avg := app.stats.TotalOrders() / app.stats.TotalBatches()
    53  	app.stats.SetAverageOrdersPerBatch(avg)
    54  	duration := time.Duration(app.currentTimestamp.UnixNano() - app.previousTimestamp.UnixNano()).Seconds()
    55  	var currentOrders, currentTrades, eps uint64
    56  	app.stats.SetBlockDuration(uint64(duration * float64(time.Second.Nanoseconds())))
    57  	if duration > 0 {
    58  		currentOrders, currentTrades, eps = uint64(float64(app.stats.CurrentOrdersInBatch())/duration),
    59  			uint64(float64(app.stats.CurrentTradesInBatch())/duration), uint64(float64(app.stats.CurrentEventsInBatch())/duration)
    60  	}
    61  	app.stats.SetOrdersPerSecond(currentOrders)
    62  	app.stats.SetTradesPerSecond(currentTrades)
    63  	app.stats.SetEventsPerSecond(eps)
    64  	// log stats
    65  	app.log.Debug("Processor batch stats",
    66  		logging.Int64("previousTimestamp", app.previousTimestamp.UnixNano()),
    67  		logging.Int64("currentTimestamp", app.currentTimestamp.UnixNano()),
    68  		logging.Float64("duration", duration),
    69  		logging.Uint64("currentOrdersInBatch", app.stats.CurrentOrdersInBatch()),
    70  		logging.Uint64("currentTradesInBatch", app.stats.CurrentTradesInBatch()),
    71  		logging.Uint64("total-batches", app.stats.TotalBatches()),
    72  		logging.Uint64("avg-orders-batch", avg),
    73  		logging.Uint64("orders-per-sec", currentOrders),
    74  		logging.Uint64("trades-per-sec", currentTrades),
    75  		logging.Uint64("events-per-sec", eps),
    76  	)
    77  	app.stats.NewBatch() // sets previous batch orders/trades to current, zeroes current tally
    78  }
    79  
    80  func (app *App) setTxStats(txLength int) {
    81  	app.stats.IncTotalTxCurrentBatch()
    82  	app.txSizes = append(app.txSizes, txLength)
    83  	totalTx := 0
    84  	for _, itx := range app.txSizes {
    85  		totalTx += itx
    86  	}
    87  	averageTxBytes := totalTx / len(app.txSizes)
    88  
    89  	app.log.Debug("Transaction stats for height",
    90  		logging.Uint64("height", app.stats.Height()),
    91  		logging.Int("average-tx-bytes", averageTxBytes))
    92  
    93  	app.stats.SetAverageTxSizeBytes(uint64(averageTxBytes))
    94  
    95  	// MAX sample size for avg calculation is defined as const.
    96  	if len(app.txSizes) == statsSampleSize {
    97  		app.txSizes = app.txSizes[:0]
    98  	}
    99  }