Skip to content

Commit b3713a1

Browse files
authored
Separate Spam Broadcast Functionality (#1653)
1 parent 182e023 commit b3713a1

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

throughput/spam.go

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ func (s *Spammer) Spam(ctx context.Context, sh SpamHelper, terminate bool, symbo
135135
return err
136136
}
137137

138-
var fundsL sync.Mutex
139138
// distribute funds
140139
accounts, funds, factories, err := s.distributeFunds(ctx, cli, parser, feePerTx, sh)
141140
if err != nil {
@@ -148,23 +147,47 @@ func (s *Spammer) Spam(ctx context.Context, sh SpamHelper, terminate bool, symbo
148147
return err
149148
}
150149

151-
// make sure we can exit gracefully & return funds
152-
signals := make(chan os.Signal, 2)
153-
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
154-
155150
cctx, cancel := context.WithCancel(ctx)
156151
defer cancel()
152+
157153
for _, issuer := range issuers {
158154
issuer.Start(cctx)
159155
}
160156

161157
// set logging
162158
issuers[0].logStats(cctx)
163159

164-
// Broadcast txs
160+
// broadcast transactions
161+
s.broadcast(cctx, cancel, sh, accounts, funds, factories, issuers, feePerTx, terminate)
162+
163+
maxUnits, err = chain.EstimateUnits(parser.Rules(time.Now().UnixMilli()), actions, factory)
164+
if err != nil {
165+
return err
166+
}
167+
return s.returnFunds(ctx, cli, parser, maxUnits, sh, accounts, factories, funds, symbol)
168+
}
169+
170+
func (s Spammer) broadcast(
171+
ctx context.Context,
172+
cancel context.CancelFunc,
173+
sh SpamHelper,
174+
accounts []*auth.PrivateKey,
175+
176+
funds map[codec.Address]uint64,
177+
factories []chain.AuthFactory,
178+
issuers []*issuer,
179+
180+
feePerTx uint64,
181+
terminate bool,
182+
) {
183+
// make sure we can exit gracefully & return funds
184+
signals := make(chan os.Signal, 2)
185+
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
186+
165187
var (
166188
// Do not call this function concurrently (math.Rand is not safe for concurrent use)
167-
z = rand.NewZipf(s.zipfSeed, s.sZipf, s.vZipf, uint64(s.numAccounts)-1)
189+
z = rand.NewZipf(s.zipfSeed, s.sZipf, s.vZipf, uint64(s.numAccounts)-1)
190+
fundsL = sync.Mutex{}
168191

169192
it = time.NewTimer(0)
170193
currentTarget = min(s.txsPerSecond, s.minTxsPerSecond)
@@ -226,7 +249,7 @@ func (s *Spammer) Spam(ctx context.Context, sh SpamHelper, terminate bool, symbo
226249

227250
// Send transaction
228251
actions := sh.GetTransfer(recipient, amountToTransfer, uniqueBytes())
229-
return issuer.Send(cctx, actions, factory, feePerTx)
252+
return issuer.Send(ctx, actions, factory, feePerTx)
230253
})
231254
}
232255

@@ -256,7 +279,7 @@ func (s *Spammer) Spam(ctx context.Context, sh SpamHelper, terminate bool, symbo
256279
utils.Outf("{{cyan}}increasing target tps:{{/}} %d\n", currentTarget)
257280
consecutiveUnderBacklog = 0
258281
}
259-
case <-cctx.Done():
282+
case <-ctx.Done():
260283
stop = true
261284
utils.Outf("{{yellow}}context canceled{{/}}\n")
262285
case <-signals:
@@ -265,14 +288,10 @@ func (s *Spammer) Spam(ctx context.Context, sh SpamHelper, terminate bool, symbo
265288
cancel()
266289
}
267290
}
291+
268292
// Wait for all issuers to finish
269293
utils.Outf("{{yellow}}waiting for issuers to return{{/}}\n")
270294
issuerWg.Wait()
271-
maxUnits, err = chain.EstimateUnits(parser.Rules(time.Now().UnixMilli()), actions, factory)
272-
if err != nil {
273-
return err
274-
}
275-
return s.returnFunds(ctx, cli, parser, maxUnits, sh, accounts, factories, funds, symbol)
276295
}
277296

278297
func (s *Spammer) logZipf(zipfSeed *rand.Rand) {

0 commit comments

Comments
 (0)