Skip to content

Commit

Permalink
refactor: use context.AfterFunc around VU management
Browse files Browse the repository at this point in the history
This cuts down on the amount of goroutines needed to be up for each VU.
  • Loading branch information
mstoykov committed Oct 22, 2024
1 parent 844a496 commit 09a7684
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
17 changes: 7 additions & 10 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,9 @@ func (r *Runner) HandleSummary(ctx context.Context, summary *lib.Summary) (map[s
return nil, err
}

go func() {
<-summaryCtx.Done()
_ = context.AfterFunc(summaryCtx, func() {
vu.Runtime.Interrupt(context.Canceled)
}()
})
vu.moduleVUImpl.ctx = summaryCtx

callbackResult := sobek.Undefined()
Expand Down Expand Up @@ -540,10 +539,9 @@ func (r *Runner) runPart(
return sobek.Undefined(), nil
}

go func() {
<-ctx.Done()
_ = context.AfterFunc(ctx, func() {
vu.Runtime.Interrupt(context.Canceled)
}()
})
vu.moduleVUImpl.ctx = ctx

groupPath, err := lib.NewGroupPath(lib.RootGroupPath, name)
Expand Down Expand Up @@ -706,9 +704,8 @@ func (u *VU) Activate(params *lib.VUActivationParams) lib.ActiveVU {
return avu.scIterGlobal
}

go func() {
// Wait for the run context to be over
<-ctx.Done()
// Wait for the run context to be over
context.AfterFunc(ctx, func() {
// Interrupt the JS runtime
u.Runtime.Interrupt(context.Canceled)
// Wait for the VU to stop running, if it was, and prevent it from
Expand All @@ -718,7 +715,7 @@ func (u *VU) Activate(params *lib.VUActivationParams) lib.ActiveVU {
if params.DeactivateCallback != nil {
params.DeactivateCallback(u)
}
}()
})

return avu
}
Expand Down
6 changes: 2 additions & 4 deletions lib/testutils/minirunner/minirunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,15 @@ func (vu *VU) Activate(params *lib.VUActivationParams) lib.ActiveVU {
return avu.scIterGlobal
}

go func() {
<-ctx.Done()

context.AfterFunc(ctx, func() {
// Wait for the VU to stop running, if it was, and prevent it from
// running again for this activation
avu.busy <- struct{}{}

if params.DeactivateCallback != nil {
params.DeactivateCallback(vu)
}
}()
})

return avu
}
Expand Down

0 comments on commit 09a7684

Please sign in to comment.