Skip to content

Commit 19b5e98

Browse files
committed
main wip
1 parent ade5446 commit 19b5e98

1 file changed

Lines changed: 61 additions & 82 deletions

File tree

main.go

Lines changed: 61 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ to multiple endpoints with account pooling management.
4444
4545
Use --dry-run to test configuration and view transaction details
4646
without actually sending requests or deploying contracts.`,
47-
Run: runLoadTest,
47+
Run: func(cmd *cobra.Command, args []string) {
48+
if err:=runLoadTest(context.Background(),cmd,args); err != nil {
49+
log.Fatal(err)
50+
}
51+
},
4852
}
4953

5054
func init() {
@@ -74,45 +78,48 @@ func main() {
7478
}
7579
}
7680

77-
func runLoadTest(cmd *cobra.Command, args []string) {
78-
ctx := context.Background()
79-
err := service.Run(ctx, func(ctx context.Context, s service.Scope) error {
80-
// Parse the config file into a config.LoadConfig struct
81-
cfg, err := loadConfig(configFile)
82-
if err != nil {
83-
return fmt.Errorf("Failed to load config: %w", err)
84-
}
81+
func runLoadTest(ctx context.Context, cmd *cobra.Command, args []string) error {
82+
// Parse the config file into a config.LoadConfig struct
83+
cfg, err := loadConfig(configFile)
84+
if err != nil {
85+
return fmt.Errorf("Failed to load config: %w", err)
86+
}
8587

86-
log.Printf("🚀 Starting Sei Chain Load Test v2")
87-
log.Printf("📁 Config file: %s", configFile)
88-
log.Printf("🎯 Endpoints: %d", len(cfg.Endpoints))
89-
log.Printf("👥 Workers per endpoint: %d", workers)
90-
log.Printf("🔧 Total workers: %d", len(cfg.Endpoints)*workers)
91-
log.Printf("📊 Scenarios: %d", len(cfg.Scenarios))
92-
log.Printf("⏱️ Stats interval: %v", statsInterval)
93-
log.Printf("📦 Buffer size per worker: %d", bufferSize)
94-
if tps > 0 {
95-
log.Printf("📈 Transactions per second: %.2f", tps)
96-
}
97-
if dryRun {
98-
log.Printf("📝 Dry run: enabled")
99-
}
100-
if trackReceipts {
101-
log.Printf("📝 Track receipts: enabled")
102-
}
103-
if trackBlocks {
104-
log.Printf("📝 Track blocks: enabled")
105-
}
106-
if prewarm {
107-
log.Printf("📝 Prewarm: enabled")
108-
}
109-
log.Println()
88+
log.Printf("🚀 Starting Sei Chain Load Test v2")
89+
log.Printf("📁 Config file: %s", configFile)
90+
log.Printf("🎯 Endpoints: %d", len(cfg.Endpoints))
91+
log.Printf("👥 Workers per endpoint: %d", workers)
92+
log.Printf("🔧 Total workers: %d", len(cfg.Endpoints)*workers)
93+
log.Printf("📊 Scenarios: %d", len(cfg.Scenarios))
94+
log.Printf("⏱️ Stats interval: %v", statsInterval)
95+
log.Printf("📦 Buffer size per worker: %d", bufferSize)
96+
if tps > 0 {
97+
log.Printf("📈 Transactions per second: %.2f", tps)
98+
}
99+
if dryRun {
100+
log.Printf("📝 Dry run: enabled")
101+
}
102+
if trackReceipts {
103+
log.Printf("📝 Track receipts: enabled")
104+
}
105+
if trackBlocks {
106+
log.Printf("📝 Track blocks: enabled")
107+
}
108+
if prewarm {
109+
log.Printf("📝 Prewarm: enabled")
110+
}
111+
log.Println()
110112

111-
// Enable mock deployment in dry-run mode
112-
if dryRun {
113-
cfg.MockDeploy = true
114-
}
113+
// Enable mock deployment in dry-run mode
114+
if dryRun {
115+
cfg.MockDeploy = true
116+
}
115117

118+
// Create statistics collector and logger
119+
collector := stats.NewCollector()
120+
logger := stats.NewLogger(collector, statsInterval, debug)
121+
122+
err = service.Run(ctx, func(ctx context.Context, s service.Scope) error {
116123
// Create the generator from the config struct
117124
gen, err := generator.NewConfigBasedGenerator(cfg)
118125
if err != nil {
@@ -125,19 +132,14 @@ func runLoadTest(cmd *cobra.Command, args []string) {
125132
return fmt.Errorf("Failed to create sender: %w", err)
126133
}
127134

128-
// Create statistics collector and logger
129-
collector := stats.NewCollector()
130-
logger := stats.NewLogger(collector, statsInterval, debug)
131-
132135
// Create and start block collector if endpoints are available
133136
var blockCollector *stats.BlockCollector
134137
if len(cfg.Endpoints) > 0 && trackBlocks {
135138
blockCollector = stats.NewBlockCollector(cfg.Endpoints[0])
136139
collector.SetBlockCollector(blockCollector)
137-
// Start block collector
138-
if err := blockCollector.Start(); err != nil {
139-
log.Printf("⚠️ Failed to start block collector: %v", err)
140-
}
140+
s.SpawnBgNamed("block collector", func() error {
141+
return blockCollector.Run(ctx)
142+
})
141143
}
142144

143145
// Enable dry-run mode in sender if specified
@@ -170,40 +172,38 @@ func runLoadTest(cmd *cobra.Command, args []string) {
170172

171173
// Set up prewarming if enabled
172174
if prewarm {
173-
fmt.Println("🔥 Creating prewarm generator...")
175+
log.Println("🔥 Creating prewarm generator...")
174176
prewarmGen := generator.NewPrewarmGenerator(cfg, gen)
175177
dispatcher.SetPrewarmGenerator(prewarmGen)
176178
log.Println("✅ Prewarm generator ready")
177179
log.Printf("📝 Prewarm mode: Accounts will be prewarmed")
178180
}
179181

180182
// Start the sender (starts all workers)
181-
snd.Start()
183+
s.SpawnBgNamed("sender", func() error { return snd.Run(ctx) })
182184
log.Printf("✅ Connected to %d endpoints", snd.GetNumShards())
183185

184186
// Start block collector if enabled
185187
if trackBlocks {
186188
blockCollector = stats.NewBlockCollector(cfg.Endpoints[0])
187189
collector.SetBlockCollector(blockCollector)
188-
if err := blockCollector.Start(); err != nil {
189-
return fmt.Errorf("Failed to start block collector: %w", err)
190-
}
190+
s.SpawnBgNamed("block collector", func() error { return blockCollector.Run(ctx) })
191191
log.Println("✅ Started block collector")
192192
}
193193

194194
// Perform prewarming if enabled (before starting logger to avoid logging prewarm transactions)
195195
if prewarm {
196-
if err := dispatcher.Prewarm(); err != nil {
196+
if err := dispatcher.Prewarm(ctx); err != nil {
197197
return fmt.Errorf("Failed to prewarm accounts: %w", err)
198198
}
199199
}
200200

201201
// Start logger (after prewarming to capture only main load test metrics)
202-
logger.Start()
202+
s.SpawnBgNamed("logger", func() error { return logger.Run(ctx) })
203203
log.Println("✅ Started statistics logger")
204204

205205
// Start dispatcher for main load test
206-
dispatcher.Start()
206+
s.SpawnBgNamed("dispatcher", func() error { return dispatcher.Run(ctx) })
207207
log.Println("✅ Started dispatcher")
208208

209209
// Set up signal handling for graceful shutdown
@@ -223,40 +223,19 @@ func runLoadTest(cmd *cobra.Command, args []string) {
223223
if trackBlocks {
224224
log.Printf("📝 Track blocks mode: Block data will be collected")
225225
}
226-
fmt.Println(strings.Repeat("=", 60))
226+
log.Println(strings.Repeat("=", 60))
227227

228228
// Main loop - wait for shutdown signal
229-
<-sigChan
230-
231-
log.Println("🛑 Received shutdown signal, stopping gracefully...")
232-
233-
// Stop block collector first
234-
if blockCollector != nil {
235-
blockCollector.Stop()
236-
log.Println("✅ Stopped block collector")
229+
if _,err:=utils.Recv(ctx, sigChan); err!=nil {
230+
return err
237231
}
238-
239-
// Stop statistics logger first
240-
logger.Stop()
241-
log.Println("✅ Stopped statistics logger")
242-
243-
// Stop dispatcher
244-
dispatcher.Stop()
245-
log.Println("✅ Stopped dispatcher")
246-
247-
// Stop sender and all workers
248-
snd.Stop()
249-
log.Println("✅ Stopped sender and workers")
250-
251-
// Print final statistics
252-
logger.LogFinalStats()
253-
254-
log.Println("👋 Shutdown complete")
232+
log.Print("\n🛑 Received shutdown signal, stopping gracefully...")
255233
return nil
256234
})
257-
if err != nil {
258-
log.Fatal(err)
259-
}
235+
// Print final statistics
236+
logger.LogFinalStats()
237+
log.Printf("👋 Shutdown complete")
238+
return err
260239
}
261240

262241
// loadConfig reads and parses the configuration file

0 commit comments

Comments
 (0)