Skip to content

Commit 3c26315

Browse files
committed
Add target gas param
1 parent 5494026 commit 3c26315

4 files changed

Lines changed: 36 additions & 9 deletions

File tree

config/settings.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Settings struct {
2525
RampUp bool `json:"rampUp,omitempty"`
2626
ReportPath string `json:"reportPath,omitempty"`
2727
TxsDir string `json:"txsDir,omitempty"`
28+
TargetGas uint64 `json:"targetGas,omitempty"`
2829
}
2930

3031
// DefaultSettings returns the default configuration values
@@ -43,6 +44,7 @@ func DefaultSettings() Settings {
4344
RampUp: false,
4445
ReportPath: "",
4546
TxsDir: "",
47+
TargetGas: 10_000_000,
4648
}
4749
}
4850

@@ -63,6 +65,7 @@ func InitializeViper(cmd *cobra.Command) error {
6365
"rampUp": "ramp-up",
6466
"reportPath": "report-path",
6567
"txsDir": "txs-dir",
68+
"targetGas": "target-gas",
6669
}
6770

6871
for viperKey, flagName := range flagBindings {
@@ -86,6 +89,7 @@ func InitializeViper(cmd *cobra.Command) error {
8689
viper.SetDefault("rampUp", defaults.RampUp)
8790
viper.SetDefault("reportPath", defaults.ReportPath)
8891
viper.SetDefault("txsDir", defaults.TxsDir)
92+
viper.SetDefault("targetGas", defaults.TargetGas)
8993
return nil
9094
}
9195

@@ -125,5 +129,6 @@ func ResolveSettings() Settings {
125129
RampUp: viper.GetBool("rampUp"),
126130
ReportPath: viper.GetString("reportPath"),
127131
TxsDir: viper.GetString("txsDir"),
132+
TargetGas: viper.GetUint64("targetGas"),
128133
}
129134
}

main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"syscall"
1313
"time"
1414

15+
"github.com/ethereum/go-ethereum/ethclient"
1516
"github.com/prometheus/client_golang/prometheus/promhttp"
1617
"github.com/spf13/cobra"
1718
"go.opentelemetry.io/otel"
@@ -66,6 +67,7 @@ func init() {
6667
rootCmd.Flags().Bool("ramp-up", false, "Ramp up loadtest")
6768
rootCmd.Flags().String("report-path", "", "Path to save the report")
6869
rootCmd.Flags().String("txs-dir", "", "Path to save the transactions")
70+
rootCmd.Flags().Uint64("target-gas", 10_000_000, "Target gas per block")
6971

7072
// Initialize Viper with proper error handling
7173
if err := config.InitializeViper(rootCmd); err != nil {
@@ -228,7 +230,18 @@ func runLoadTest(ctx context.Context, cmd *cobra.Command, args []string) error {
228230
// Create dispatcher
229231
var dispatcher *sender.Dispatcher
230232
if settings.TxsDir != "" {
231-
writer := sender.NewTxsWriter(10_000_000, settings.TxsDir)
233+
// get latest height
234+
ethclient, err := ethclient.Dial(cfg.Endpoints[0])
235+
if err != nil {
236+
return fmt.Errorf("failed to create ethclient: %w", err)
237+
}
238+
latestHeight, err := ethclient.BlockNumber(ctx)
239+
if err != nil {
240+
return fmt.Errorf("failed to get latest height: %w", err)
241+
}
242+
writerHeight := latestHeight + 10 // some buffer
243+
log.Printf("🔍 Latest height: %d, writer start height: %d", latestHeight, writerHeight)
244+
writer := sender.NewTxsWriter(settings.TargetGas, settings.TxsDir, writerHeight, 100)
232245
dispatcher = sender.NewDispatcher(gen, writer)
233246
} else {
234247
dispatcher = sender.NewDispatcher(gen, snd)

sender/writer.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,24 @@ import (
1414
// implements `Send`
1515

1616
type TxsWriter struct {
17-
gasPerBlock uint64
18-
nextHeight uint64
19-
txsDir string
17+
gasPerBlock uint64
18+
nextHeight uint64
19+
txsDir string
20+
blocksGenerated uint64
21+
numBlocks uint64
2022

2123
bufferGas uint64
2224
txBuffer []*types.LoadTx
2325
}
2426

25-
func NewTxsWriter(gasPerBlock uint64, txsDir string) *TxsWriter {
27+
func NewTxsWriter(gasPerBlock uint64, txsDir string, startHeight uint64, numBlocks uint64) *TxsWriter {
2628
// what height to start at?
2729
return &TxsWriter{
28-
gasPerBlock: gasPerBlock,
29-
nextHeight: 1,
30-
txsDir: txsDir,
30+
gasPerBlock: gasPerBlock,
31+
nextHeight: startHeight,
32+
txsDir: txsDir,
33+
blocksGenerated: 0,
34+
numBlocks: numBlocks,
3135

3236
bufferGas: 0,
3337
txBuffer: make([]*types.LoadTx, 0),
@@ -59,6 +63,7 @@ func (w *TxsWriter) Flush() error {
5963
w.txBuffer = make([]*types.LoadTx, 0)
6064
w.bufferGas = 0
6165
w.nextHeight++
66+
w.blocksGenerated++
6267
}()
6368
// write to dir `~/load_txs`
6469
// make dir if it doesn't exist
@@ -82,5 +87,9 @@ func (w *TxsWriter) Flush() error {
8287

8388
log.Printf("Flushed %d transactions to %s", len(w.txBuffer), txsFile)
8489

90+
if w.blocksGenerated >= w.numBlocks {
91+
return fmt.Errorf("reached max number of blocks: %d", w.numBlocks)
92+
}
93+
8594
return nil
8695
}

sender/writer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
func TestTxsWriter_Flush(t *testing.T) {
1515
// two evm transfer txs
16-
writer := NewTxsWriter(42000, "/tmp")
16+
writer := NewTxsWriter(42000, "/tmp", 1)
1717

1818
loadConfig := &config.LoadConfig{
1919
ChainID: 7777,

0 commit comments

Comments
 (0)