Skip to content

Commit

Permalink
Merge pull request #449 from NilFoundation/metrics-for-loadgen
Browse files Browse the repository at this point in the history
Added simple metrics for loadgen
  • Loading branch information
Zerg1996 authored Mar 4, 2025
2 parents 31e0ffb + b26bd67 commit 8156896
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
36 changes: 6 additions & 30 deletions nil/services/nil_load_generator/metrics/metrics_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"

"github.com/NilFoundation/nil/nil/internal/telemetry"
"github.com/NilFoundation/nil/nil/internal/types"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
Expand All @@ -14,12 +13,8 @@ type MetricsHandler struct {
attributes metric.MeasurementOption

// Counters
totalFromToCalls metric.Int64Counter
totalErrorsEncountered metric.Int64Counter
currentApproxSmartAccountBalance metric.Int64UpDownCounter

// Gauges
currentSmartAccountBalance metric.Int64Gauge
totalFromToCalls metric.Int64Counter
totalErrorsEncountered metric.Int64Counter
}

func NewMetricsHandler(name string) (*MetricsHandler, error) {
Expand Down Expand Up @@ -54,32 +49,13 @@ func (mh *MetricsHandler) initMetrics(name string, meter metric.Meter) error {
return err
}

mh.currentApproxSmartAccountBalance, err = meter.Int64UpDownCounter(name + "_current_approx_smart_account_balance")
if err != nil {
return err
}

// Initialize gauges
mh.currentSmartAccountBalance, err = meter.Int64Gauge(name + "_current_smart_account_balance")
if err != nil {
return err
}

return nil
}

func (mh *MetricsHandler) RecordFromToCall(shardFrom, shardTo int64) {
mh.totalFromToCalls.Add(context.Background(), 1, mh.attributes, metric.WithAttributes(attribute.Int64("from", shardFrom), attribute.Int64("to", shardTo)))
}

func (mh *MetricsHandler) RecordError() {
mh.totalErrorsEncountered.Add(context.Background(), 1, mh.attributes)
}

func (mh *MetricsHandler) SetCurrentSmartAccountBalance(balance uint64, smartAccount types.Address) {
mh.currentSmartAccountBalance.Record(context.Background(), int64(balance), mh.attributes, metric.WithAttributes(attribute.Stringer("smart-account", smartAccount)))
func (mh *MetricsHandler) RecordFromToCall(ctx context.Context, shardFrom, shardTo int64) {
mh.totalFromToCalls.Add(ctx, 1, mh.attributes, metric.WithAttributes(attribute.Int64("from", shardFrom), attribute.Int64("to", shardTo)))
}

func (mh *MetricsHandler) SetCurrentApproxSmartAccountBalance(balance uint64, smartAccount types.Address) {
mh.currentApproxSmartAccountBalance.Add(context.Background(), int64(balance), mh.attributes, metric.WithAttributes(attribute.Stringer("smart-account", smartAccount)))
func (mh *MetricsHandler) RecordError(ctx context.Context) {
mh.totalErrorsEncountered.Add(ctx, 1, mh.attributes)
}
23 changes: 22 additions & 1 deletion nil/services/nil_load_generator/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/NilFoundation/nil/nil/services/cliservice"
"github.com/NilFoundation/nil/nil/services/faucet"
uniswap "github.com/NilFoundation/nil/nil/services/nil_load_generator/contracts"
"github.com/NilFoundation/nil/nil/services/nil_load_generator/metrics"
"github.com/NilFoundation/nil/nil/services/rpc"
"github.com/NilFoundation/nil/nil/services/rpc/httpcfg"
"github.com/NilFoundation/nil/nil/services/rpc/transport"
Expand Down Expand Up @@ -69,7 +70,8 @@ func calculateOutputAmount(amountIn, reserveIn, reserveOut *big.Int) *big.Int {
}

func randomPermutation(shardIdList []types.ShardId, amount uint64) ([]types.ShardId, error) {
arr := shardIdList
arr := make([]types.ShardId, len(shardIdList))
copy(arr, shardIdList)
for i := len(arr) - 1; i > 0; i-- {
jBig, err := rand.Int(rand.Reader, big.NewInt(int64(i+1)))
if err != nil {
Expand Down Expand Up @@ -285,38 +287,50 @@ func Run(ctx context.Context, cfg Config, logger zerolog.Logger) error {
signalCtx, cancel := signal.NotifyContext(ctx, syscall.SIGTERM, syscall.SIGINT)
defer cancel()
ctx = signalCtx
handler, err := metrics.NewMetricsHandler("nil_load_generator")

go func() {
if err := startRpcServer(ctx, cfg.OwnEndpoint); err != nil {
handler.RecordError(ctx)
logger.Error().Err(err).Msg("Failed to start RPC server")
panic(err)
}
}()

if err != nil {
handler.RecordError(ctx)
return err
}

faucet := faucet.NewClient(cfg.FaucetEndpoint)
client = rpc_client.NewClient(cfg.Endpoint, logger)
logging.SetupGlobalLogger(cfg.LogLevel)

if err := rpcSwapLimit.Set(cfg.RpcSwapLimit); err != nil {
handler.RecordError(ctx)
return err
}

mainPrivateKey, err := execution.LoadMainKeys(cfg.MainKeysPath)
if err != nil {
handler.RecordError(ctx)
return err
}

service := cliservice.NewService(ctx, client, mainPrivateKey, faucet)
shardIdList, err := client.GetShardIdList(ctx)
if err != nil {
handler.RecordError(ctx)
return err
}
if err := setDefaultVars(cfg, len(shardIdList)); err != nil {
handler.RecordError(ctx)
return err
}
logger.Info().Msg("Creating smart accounts...")
smartAccounts, err = initializeSmartAccountsAndServices(ctx, cfg.UniswapAccounts, shardIdList, client, service, faucet)
if err != nil {
handler.RecordError(ctx)
return err
}
logger.Info().Msg("Smart accounts created successfully.")
Expand All @@ -325,6 +339,7 @@ func Run(ctx context.Context, cfg Config, logger zerolog.Logger) error {
if err := parallelizeAcrossN(len(shardIdList), func(i int) error {
return deployPairs(ctx, i, shardIdList, logger)
}); err != nil {
handler.RecordError(ctx)
logger.Error().Err(err).Msg("Deployment and initialization error")
return err
}
Expand All @@ -340,32 +355,38 @@ func Run(ctx context.Context, cfg Config, logger zerolog.Logger) error {
checkBalanceCounterDownInt = int(cfg.CheckBalance)
logger.Info().Msg("Checking balance and minting tokens.")
if err := uniswap.TopUpBalance(thresholdAmount, append(services, uniswapServices...), append(smartAccounts, uniswapSmartAccounts...)); err != nil {
handler.RecordError(ctx)
return err
}
}
checkBalanceCounterDownInt--
if err := parallelizeAcrossN(len(shardIdList), func(i int) error {
return mint(ctx, i, shardIdList, logger)
}); err != nil {
handler.RecordError(ctx)
logger.Error().Err(err).Msg("Minting error")
return err
}

for range cfg.SwapPerIteration {
pairsToCall, smartAccountsToCall, err := selectPairAndAccount(shardIdList)
if err != nil {
handler.RecordError(ctx)
return err
}
if err := parallelizeAcrossN(len(pairsToCall), func(i int) error {
handler.RecordFromToCall(ctx, int64(smartAccountsToCall[i]-1), int64(pairsToCall[i]-1))
return swap(ctx, smartAccountsToCall[i]-1, pairsToCall[i]-1, logger)
}); err != nil {
handler.RecordError(ctx)
logger.Error().Err(err).Msg("Swap error")
}
}

if err := parallelizeAcrossN(len(shardIdList), func(i int) error {
return burn(ctx, i, shardIdList, logger)
}); err != nil {
handler.RecordError(ctx)
logger.Error().Err(err).Msg("Burn error")
return err
}
Expand Down

0 comments on commit 8156896

Please sign in to comment.