feat(go): generate unique load simulator frames
This commit is contained in:
@@ -33,12 +33,13 @@ func (r Runner) Run(ctx context.Context, cfg Config) (Stats, error) {
|
||||
}).Build(); err != nil {
|
||||
return Stats{}, err
|
||||
}
|
||||
payload, err := FrameTemplate(cfg.Protocol, cfg.Template)
|
||||
if err != nil {
|
||||
return Stats{}, err
|
||||
}
|
||||
if !cfg.SendFrames {
|
||||
payload = nil
|
||||
var factory *FrameFactory
|
||||
if cfg.SendFrames {
|
||||
var err error
|
||||
factory, err = NewFrameFactory(cfg.Protocol, cfg.Template)
|
||||
if err != nil {
|
||||
return Stats{}, err
|
||||
}
|
||||
}
|
||||
dial := r.Dial
|
||||
if dial == nil {
|
||||
@@ -51,6 +52,7 @@ func (r Runner) Run(ctx context.Context, cfg Config) (Stats, error) {
|
||||
|
||||
var stats Stats
|
||||
var wg sync.WaitGroup
|
||||
connectionIndex := 0
|
||||
for i, batch := range ConnectionBatches(cfg.Connections, cfg.ConnectRatePerSecond) {
|
||||
if i > 0 {
|
||||
if !sleepOrDone(runCtx, time.Second) {
|
||||
@@ -68,11 +70,13 @@ func (r Runner) Run(ctx context.Context, cfg Config) (Stats, error) {
|
||||
}
|
||||
atomic.AddInt64(&stats.ConnectionsOpened, 1)
|
||||
wg.Add(1)
|
||||
connIndex := connectionIndex
|
||||
connectionIndex++
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
defer conn.Close()
|
||||
if cfg.SendFrames {
|
||||
writeLoop(runCtx, conn, payload, cfg.SendInterval, &stats)
|
||||
writeLoop(runCtx, conn, factory, connIndex, cfg.SendInterval, &stats)
|
||||
return
|
||||
}
|
||||
<-runCtx.Done()
|
||||
@@ -84,8 +88,13 @@ func (r Runner) Run(ctx context.Context, cfg Config) (Stats, error) {
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func writeLoop(ctx context.Context, conn net.Conn, payload []byte, interval time.Duration, stats *Stats) {
|
||||
for {
|
||||
func writeLoop(ctx context.Context, conn net.Conn, factory *FrameFactory, connectionIndex int, interval time.Duration, stats *Stats) {
|
||||
for frameIndex := int64(0); ; frameIndex++ {
|
||||
payload, err := factory.Frame(connectionIndex, frameIndex)
|
||||
if err != nil {
|
||||
atomic.AddInt64(&stats.WriteErrors, 1)
|
||||
return
|
||||
}
|
||||
_ = conn.SetWriteDeadline(time.Now().Add(3 * time.Second))
|
||||
if _, err := conn.Write(payload); err != nil {
|
||||
atomic.AddInt64(&stats.WriteErrors, 1)
|
||||
|
||||
Reference in New Issue
Block a user