Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print GPS #59

Merged
merged 3 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ awscpu
bin/
build/

cmd/simulator/.simulator/keys/*
cmd/simulator/.simulator/*
46 changes: 27 additions & 19 deletions cmd/simulator/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,19 @@ const (
checkInterval = 2 * time.Second
)

// MonitorTPS periodically prints metrics related to transaction activity on
// Monitor periodically prints metrics related to transaction activity on
// a given network.
func MonitorTPS(ctx context.Context, client ethclient.Client) error {
func Monitor(ctx context.Context, client ethclient.Client) error {
lastBlockNumber, err := client.BlockNumber(ctx)
if err != nil {
return fmt.Errorf("failed to get block number: %w", err)
}
block, err := client.BlockByNumber(ctx, new(big.Int).SetUint64(lastBlockNumber))
if err != nil {
return fmt.Errorf("failed to get block at number %d: %w", lastBlockNumber, err)
}

startTime := block.Time()
startTime := uint64(time.Now().Unix())
currentTime := startTime
totalTxs := 0
timeTxs := make(map[uint64]int)
totalGas := uint64(0)
timeGas := make(map[uint64]uint64)
for ctx.Err() == nil {
newBlockNumber, err := client.BlockNumber(ctx)
if err != nil {
Expand All @@ -57,28 +54,39 @@ func MonitorTPS(ctx context.Context, client ethclient.Client) error {
time.Sleep(checkInterval)
continue
}
log.Printf("[block created] index: %d base fee: %d block gas cost: %d block txs: %d\n", i, block.BaseFee().Div(block.BaseFee(), big.NewInt(params.GWei)), block.BlockGasCost(), len(block.Body().Transactions))
timeTxs[block.Time()] += len(block.Transactions())
totalTxs += len(block.Transactions())
if i == 1 { // genesis is usually the unix epoch
startTime = block.Time()
}
txs := len(block.Transactions())
gas := block.GasUsed()
t := block.Time()

log.Printf("[block created] t: %v index: %d base fee: %d block gas cost: %d block txs: %d gas used: %d\n", time.Unix(int64(t), 0), i, block.BaseFee().Div(block.BaseFee(), big.NewInt(params.GWei)), block.BlockGasCost(), txs, gas)

// Update Tx Count
timeTxs[t] += txs
totalTxs += txs

// Update Gas Usage
timeGas[t] += gas
totalGas += gas
break
}
}
lastBlockNumber = newBlockNumber
currentTime = block.Time()

// log TPS
// log 10s
tenStxs := 0
tenSgas := uint64(0)
for i := uint64(currentTime - 10); i < currentTime; i++ { // ensure only looking at closed times
tenStxs += timeTxs[i]
tenSgas += timeGas[i]
}
if currentTime-startTime > 0 {
diff := currentTime - startTime
if diff > 0 {
log.Printf(
"[stats] historical TPS: %f last 10s TPS: %f total txs: %d total time(s): %v\n",
float64(totalTxs)/float64(currentTime-startTime), float64(tenStxs)/float64(10),
totalTxs, currentTime-startTime,
"[stats] historical TPS: %.2f last 10s TPS: %.2f total txs: %d historical GPS: %.1f, last 10s GPS: %.1f elapsed: %v\n",
float64(totalTxs)/float64(diff), float64(tenStxs)/float64(10), totalTxs,
float64(totalGas)/float64(diff), float64(tenSgas)/float64(10),
time.Duration(diff)*time.Second,
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/simulator/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func Run(ctx context.Context) error {

g, gctx := errgroup.WithContext(ctx)
g.Go(func() error {
return metrics.MonitorTPS(gctx, rclient)
return metrics.Monitor(gctx, rclient)
})
fundRequest := make(chan common.Address)
g.Go(func() error {
Expand Down