Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use k6 events to handle browser lifecycle #944

Merged
merged 16 commits into from
Jul 5, 2023
Merged

Use k6 events to handle browser lifecycle #944

merged 16 commits into from
Jul 5, 2023

Conversation

ka3de
Copy link
Collaborator

@ka3de ka3de commented Jun 20, 2023

Description of changes

This PR integrates k6 browser, and more specifically our implementation of the browser registry, with the events generated from k6 (see grafana/k6#3112) in order to handle the browser lifecycle based on these events. Specifically:

  • Initializes each iteration's browser instance as a reaction to the IterStart event.
  • Closes each iteration's browser instance as a reaction to the IterEnd event.
  • Cleans any pending browser instances on Exit event (which is the event guaranteed to be fired even on case of unexpected error).

Closes #926.

Checklist

  • Written tests for the changes

@ka3de ka3de self-assigned this Jun 20, 2023
@ka3de ka3de force-pushed the feat/core-events branch 5 times, most recently from 07702be to a8145bc Compare June 23, 2023 07:53
@ka3de ka3de force-pushed the feat/core-events branch 4 times, most recently from f10d8e7 to 334c8b3 Compare June 27, 2023 07:30
ka3de added 5 commits June 30, 2023 14:39
In this way we reduce contention on sync.Map and avoid repeated string
concatenation calls when generating the browser registry index, which
was built based on VUID-scenario-iteration. Instead, tiying the browser
registry scope to a single VU, we can use just the iteration as index
for the registry.
This will allow for more flexibility on browser operations, for example
during cleanup, on which all browser entries in the registry must be
closed and deleted.
@ka3de ka3de force-pushed the feat/core-events branch 6 times, most recently from 8986bef to ebaae44 Compare July 4, 2023 09:52
@ka3de ka3de marked this pull request as ready for review July 4, 2023 10:55
@ka3de ka3de requested a review from ankur22 July 4, 2023 10:56
Copy link
Collaborator

@ankur22 ankur22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pushing this through and working with the k6 changes to get it to this final stage 👏

I think it mostly makes sense. I've left some questions that would help me understand the behaviour of the event system a bit more. Once those have been clarified though, i think we're good to go 🙌

browser/mapping.go Outdated Show resolved Hide resolved
browser/registry.go Show resolved Hide resolved
browser/registry.go Show resolved Hide resolved
browser/registry.go Outdated Show resolved Hide resolved
browser/registry.go Show resolved Hide resolved
browser/registry.go Show resolved Hide resolved
@ka3de ka3de force-pushed the feat/core-events branch from 1180ce6 to 93f3c4f Compare July 5, 2023 09:02
ka3de added 4 commits July 5, 2023 12:07
This commit modifies the browser registry to subscribe to k6 'IterStart'
and 'IterEnd' events in order to initialize and close the browser
instances when receiving these events.
Adds a new event handler for the 'Exit' event, which is guaranteed to be
fired before k6 execution exits, even in case of panic. Therefor this
event is used to ensure all browser instances held at the VU registries
are closed before finishing execution.
Instead, now browsers are only initialized and indexed in the registry
as a reaction to 'IterStart' events. Therefor we can remove the
getOrInitBrowser method from the mapping layer, and only call
vu.getBrowser method in order to retrieve the previously initialized
browser for the iteration.
ka3de added 7 commits July 5, 2023 12:07
As now it is only called from within the browser registry, and not from
the mapping layer.
Now that browsers are not initialized or closed from the mapping layer,
but as a reaction to iteration events from browser registry, we need
helper functions for tests that require testing through the mapping
layer so the browser for the executed iteration during the test can be
previously initialized, and closed at the end of the iteration if
necessary.
Use the vu.StartIteration helper in order to pre initialize the browser
required for the iteration executed during the test, so once the JS and
mapping layer code is executed, the browser instance can be correctly
retrieved.
Add a higher level helper method to VU in order to retrieve the current
VU iteration's browser instance. This improves the repetitive calls to
getBrowser() method passing the current iteration as input from the
mapping layer.
This will match our own go.mod version
Also, this test was failing due to the usage of URL.JoinPath method in
k6, which is only available since go v1.19.
@ka3de ka3de force-pushed the feat/core-events branch from 93f3c4f to ce0a711 Compare July 5, 2023 10:08
Copy link
Collaborator

@ankur22 ankur22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀 Thanks for taking the time to iterate with k6 core on this 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use IterStart to start browser process for iteration
2 participants