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

fix: make test variables and correlation variables defined under a containing name #112

Merged
merged 6 commits into from
Aug 29, 2024

Conversation

Llandy3d
Copy link
Member

Closes https://github.com/grafana/k6-cloud/issues/2607

Test variables:
image

Correlation variable:

image

.join('\n')

return `const VARS = {\n${variables_lines}\n}\n`
Copy link
Member Author

Choose a reason for hiding this comment

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

This is currently defined outside of the default function, it makes it more readable but I'm not sure if it's the way we want to do it.

For comparison I left the the CORRELATION_VARS inside the default function to see how it would be when defined inside the default function, do you think it's better to have them defined outside of inside the default function ?

For correlation it doesn't change much as it's a single line, for test variables we are defining them on script generation so the list might be long 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd vote to keep outside of default

Copy link
Collaborator

Choose a reason for hiding this comment

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

Inside of the default function makes sense because it's the scope they're being used, however, as you pointed out that it might get long, I believe leaving then outside makes it cleaner.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't know whether the placement really makes any difference, but conceptually, it feels like correlation vars should definitely be inside the default function, as it is what k6 docs refer to as VU code (with the outside code being called init code). Correlated values are meant to be scoped to the VU/iteration, so having them inside the default function feels natural.

Copy link
Member Author

Choose a reason for hiding this comment

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

yap, if we also consider the logic, having it inside means that it is always reset to the empty state on start of iteration, while having it global means we would be leaving leftover status, and that would be a bug.

So globally defined makes sense only for the Test variables 👍

Copy link
Collaborator

@e-fisher e-fisher left a comment

Choose a reason for hiding this comment

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

Did some testing, it's assigns correlation value correctly but still uses old correl_x syntax when injecting value:

CleanShot 2024-08-28 at 16 43 17@2x

.join('\n')

return `const VARS = {\n${variables_lines}\n}\n`
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd vote to keep outside of default

@@ -30,12 +30,15 @@ describe('Code generation', () => {

export const options = {}

const VARS = {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we skip adding this line when there's no variable defined? Just to reduce noise

export default function() {
let params
let resp
let match
let regex
let url
const CORRELATION_VARS = {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

JS doesn't restrict you from modifying properties of an object defined as const, but perhaps let correlation_vars would be a better fit here, to indicate this value is expected to be mutated?

Copy link
Collaborator

@going-confetti going-confetti Aug 28, 2024

Choose a reason for hiding this comment

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

@e-fisher wouldn't using let allow the user to shoot themselves in the foot by reassigning it in one of the custom code snippets?

We can wrap it with Object.freeze to prevent reassignment as well 🤔 - disregard this, it only applies to test vars

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't have a strong preference but possibly sliding more towards const since we don't want that value to be reassigned, but just the inner structure to be mutated 🤔

@@ -208,9 +208,8 @@ const extractCorrelationRegex = (
}

const getCorrelationVariableSnippet = (uniqueId: number | undefined) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can it really be undefined? In this case, we need to add a check/throw an error in such cases, because correlation_vars[undefined] will throw

Copy link
Member Author

Choose a reason for hiding this comment

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

no, I don't think it can be undefined, the type system there will need to be improved because we will always be having the uniqueId at that point, but we are using a variable that it's allowed to be undefined :(

Copy link
Collaborator

Choose a reason for hiding this comment

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

Added a PR here: #114

Copy link
Collaborator

@going-confetti going-confetti left a comment

Choose a reason for hiding this comment

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

LGTM! One small nit is that we should probably agree on the casing: test variables are in VARS, but correlation vars are in correlation_vars

@Llandy3d
Copy link
Member Author

LGTM! One small nit is that we should probably agree on the casing: test variables are in VARS, but correlation vars are in correlation_vars

That is because VARS is a global variable, while correlation_vars is defined in the default function, so looking at edgar feedback I think it made sense to make it lowercase, but maybe we do want to keep it UpperCase to distinguish it 🤔

Copy link
Collaborator

@e-fisher e-fisher left a comment

Choose a reason for hiding this comment

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

LGTM

@Llandy3d Llandy3d merged commit 8c01d9e into main Aug 29, 2024
1 check passed
@Llandy3d Llandy3d deleted the global_vars branch August 29, 2024 09:15
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.

4 participants