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

Refactor new command #4153

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open

Conversation

dgzlopes
Copy link
Member

@dgzlopes dgzlopes commented Dec 27, 2024

Early draft PR. Issue with more context: #4154

What?

Why?

Checklist

  • I have performed a self-review of my code.
  • I have added tests for my changes.
  • I have run linter locally (make lint) and all checks pass.
  • I have run tests locally (make tests) and all tests pass.
  • I have commented on my code, particularly in hard-to-understand areas.

Related PR(s)/Issue(s)

Signed-off-by: Daniel González Lopes <[email protected]>
Signed-off-by: Daniel González Lopes <[email protected]>
Signed-off-by: Daniel González Lopes <[email protected]>
Signed-off-by: Daniel González Lopes <[email protected]>
@dgzlopes
Copy link
Member Author

Happy to fix the linter things if we feel this approach makes sense 👍

Signed-off-by: Daniel González Lopes <[email protected]>
cmd/newtemplates/protocol.js Outdated Show resolved Hide resolved
Signed-off-by: Daniel González Lopes <[email protected]>
Signed-off-by: Daniel González Lopes <[email protected]>
Signed-off-by: Daniel González Lopes <[email protected]>
Signed-off-by: Daniel González Lopes <[email protected]>
@dgzlopes dgzlopes marked this pull request as ready for review January 3, 2025 13:35
@dgzlopes dgzlopes requested a review from a team as a code owner January 3, 2025 13:35
@dgzlopes dgzlopes requested review from oleiade and joanlopez and removed request for a team January 3, 2025 13:35
oleiade
oleiade previously approved these changes Jan 7, 2025
Copy link
Member

@oleiade oleiade left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍🏻 🚀
Honestly, I'd be happy for us to move forward with this. Have you considered moving back to an engineering career Daniel ? 🙉

cmd/new.go Outdated Show resolved Hide resolved
cmd/newtemplates/browser.js Outdated Show resolved Hide resolved
Signed-off-by: Daniel González Lopes <[email protected]>
Signed-off-by: Daniel González Lopes <[email protected]>
@joanlopez
Copy link
Contributor

joanlopez commented Jan 13, 2025

Great job @dgzlopes! 🚀

What do you think about adding code comments on every relevant line present on each template? I know that's very common nowadays across frameworks or similar tools that offer features for setting up a skeleton of your project. In fact, that's how our static template looks like now, and I think it's a nice-to-keep.

Especially considering that, I guess, the main target of this feature is new users of k6 - I suspect experienced users have their own scripts they duplicate and adjust.

export const options = {
scenarios: {
ui: {
executor: "shared-iterations",
Copy link
Contributor

Choose a reason for hiding this comment

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

I see in other examples we set duration/vus, should we set some "scope" here as well?
Otherwise, does it bring any value to define the executor?

Copy link
Member

Choose a reason for hiding this comment

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

👍🏻 Furthermore, my understanding is that in general users tend to run browser tests with 1 VU/1 iteration, at least when they get started. I know it's the default anyway, but as we're all addressing beginners with this command, it could be helpful to remove as much implicit as possible?

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense! I have added VUs/Iterations.

Otherwise, does it bring any value to define the executor?

It is needed to be able to run a browser test. You must use scenarios, and scenarios must have an executor.

Btw... we do this same thing in lots of other places; maybe it is something we should change? cc @ankur22
e.g. https://grafana.com/docs/k6/latest/using-k6-browser/#a-simple-browser-test

Copy link
Contributor

Choose a reason for hiding this comment

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

You mean adding the VU/iteration to all the other examples too? The situation with scenarios and browser tests isn't great tbh, and being forced to add an executor. I think we wanted to avoid making a very large options block, and the minimal is:

export const options = {
  scenarios: {
    ui: {
      executor: 'shared-iterations',
      options: {
        browser: {
          type: 'chromium',
        },
      },
    },
  },
};

Adding iteration and vus adds a couple more lines to it.

If the general consensus is that it makes is clearer for new users then it's something to consider. CC @inancgumus

Copy link
Member

Choose a reason for hiding this comment

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

I like being explicit in this k6 new command’s output and including VUs and iterations as outputted examples. Sure, we can also mention VUs and iterations in k6-docs here and there, but in general (all k6-docs, if that’s what we mean), I don’t believe we should add more clutter to that already crowded browser options block.

let checkData;
const page = await browser.newPage();

try {
Copy link
Contributor

@joanlopez joanlopez Jan 13, 2025

Choose a reason for hiding this comment

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

Would adding a catch here be a recommended practice? 🤔
If so, I'd prefer to have it in place, as this not being a simple test, but likely an entrypoint for k6 newcomers.

Copy link
Member

Choose a reason for hiding this comment

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

I believe in this case, if the error is not caught, the iteration will be aborted with an error anyway. But it would make sense indeed, if possible to catch the error in order to maybe explicitly call fail or exec.test.abort with a custom message guiding the user through what failed, why, and what they can do about it 👍🏻

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 have added catch + fail 👀

Also, another thing we can maybe use in all examples cc @ankur22

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 have also changed the setup() of the browser and protocol tests to use exec.test.abort instead of throw


// GetTemplate selects the appropriate template based on the type
func (tm *TemplateManager) GetTemplate(templateType string) (*template.Template, error) {
switch templateType {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit; I guess we could define the available "types" as exposed constants from the package, so it's a bit more explicit. But feel free to disregard this comment, cause for now it's only used by new, and very concretely.

Copy link
Member

Choose a reason for hiding this comment

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

I'd pretty much like that too 👍🏻 Having them as constants, would help defining them as keys we explicitly own (and also help with later refactorings).

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense. Done!

cmd/new.go Outdated Show resolved Hide resolved
cmd/new.go Outdated
Comment on lines 50 to 54
if cerr := fd.Close(); cerr != nil {
if _, err := fmt.Fprintf(c.gs.Stderr, "error closing file: %v\n", cerr); err != nil {
panic(fmt.Sprintf("error writing error message to stderr: %v", err))
}
}
Copy link
Contributor

@joanlopez joanlopez Jan 13, 2025

Choose a reason for hiding this comment

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

I'd try to avoid the use of panic here, but return whatever error happened (or the result of joining both if more than one happened). I think errors are handled (likely by calling panic as well) in a "centralized" place, so I think it's better if we can just return, from here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Gotcha! I removed the panic 👀

Signed-off-by: Daniel González Lopes <[email protected]>
Signed-off-by: Daniel González Lopes <[email protected]>
Signed-off-by: Daniel González Lopes <[email protected]>
Signed-off-by: Daniel González Lopes <[email protected]>
Signed-off-by: Daniel González Lopes <[email protected]>
Signed-off-by: Daniel González Lopes <[email protected]>
Signed-off-by: Daniel González Lopes <[email protected]>
@dgzlopes
Copy link
Member Author

What do you think about adding code comments on every relevant line present on each template? I know that's very common nowadays across frameworks or similar tools that offer features for setting up a skeleton of your project. In fact, that's how our static template looks like now, and I think it's a nice-to-keep.

Especially considering that, I guess, the main target of this feature is new users of k6 - I suspect experienced users have their own scripts they duplicate and adjust.

@joanlopez My feeling is that the scripts are small/simple enough to be understandable out of the box. Also, after checking our docs, I saw that we have a weird mix of commented/un-commented examples.

With that said, I have no strong opinions on the topic, so yeah, happy to add comments if you think it would provide a better experience for newcomers 💪

Signed-off-by: Daniel González Lopes <[email protected]>
@dgzlopes dgzlopes requested a review from joanlopez January 15, 2025 12:36
@joanlopez
Copy link
Contributor

joanlopez commented Jan 15, 2025

@joanlopez My feeling is that the scripts are small/simple enough to be understandable out of the box. Also, after checking our docs, I saw that we have a weird mix of commented/un-commented examples.

With that said, I have no strong opinions on the topic, so yeah, happy to add comments if you think it would provide a better experience for newcomers 💪

If you ask me, I prefer it with comments, as we have now. But I don't think it's (only) me who should take that decision.
So, approving for now -as all the other comments are resolved now I think- we can always add the comments later if that's what we decided as a group consensus.

joanlopez
joanlopez previously approved these changes Jan 15, 2025
oleiade
oleiade previously approved these changes Jan 20, 2025
@dgzlopes dgzlopes dismissed stale reviews from oleiade and joanlopez via 5e08739 January 20, 2025 10:59
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.

5 participants