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

Improve quality of generated scripts #448

Open
allansson opened this issue Feb 3, 2025 · 0 comments
Open

Improve quality of generated scripts #448

allansson opened this issue Feb 3, 2025 · 0 comments
Labels
debt Technical debt

Comments

@allansson
Copy link
Collaborator

What

We should improve the quality of the generated scripts.

The code currently relies on string concatenation and limited analysis on the intended output. This approach has, for convenience, lead to emitting non-idiomatic code, e.g.:

  • Importing things that aren't needed.
  • Single let top-level declarations for e.g. response bodies.
  • Using an object for storing variables instead of normal variable declarations.
  • Using computed property names (vars["abc"]) instead of normal dot syntax vars.abc.

String concatenation provides no type-safety, so producing valid output is largely a trial-and-error process. Strings need to be properly escaped on a case-by-case basis.

A common approach to code generation is to build an Abstract Syntax Tree (AST) and use that to generate a script. An AST is a tree structure that represents the semantics of the program without the complexities of syntax, e.g. blocks being contained between {  and }. This is the way popular tools such as ESLint, Prettier and TypeScript generate JS.

This approach has already been adopted for the experimental browser recording functionality, and the idea is to use the same code for protocol level tests.

Why

Generating readable scripts will help users understand and troubleshoot their scripts. It can be easier to understand why something happened by looking at the script than it is by looking that the requests list.

The quality of the scripts is also important when using a recording as a starting point. If the quality is poor and requires too much cleanup to be passed off as production-grade, users will be reluctant to use it.

The approach describe here also makes it easier to have configurable output, e.g. generating typescript instead of javascript or emitting different scripts depending on the target version of k6.

@going-confetti going-confetti added the debt Technical debt label Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
debt Technical debt
Projects
None yet
Development

No branches or pull requests

2 participants