{"id":26891991,"url":"https://github.com/dnv-opensource/k6-script-from-har","last_synced_at":"2025-07-31T11:39:00.559Z","repository":{"id":237511823,"uuid":"794318393","full_name":"dnv-opensource/k6-script-from-har","owner":"dnv-opensource","description":"k6 script from har - dead simple k6 test generator","archived":false,"fork":false,"pushed_at":"2024-05-03T15:27:25.000Z","size":31,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-17T19:55:47.597Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dnv-opensource.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-30T22:17:34.000Z","updated_at":"2025-02-18T18:19:02.000Z","dependencies_parsed_at":"2024-05-03T16:47:48.977Z","dependency_job_id":"8973a813-ec1a-4b30-bcac-90d47ba97e42","html_url":"https://github.com/dnv-opensource/k6-script-from-har","commit_stats":null,"previous_names":["dnv-opensource/k6-script-from-har"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnv-opensource%2Fk6-script-from-har","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnv-opensource%2Fk6-script-from-har/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnv-opensource%2Fk6-script-from-har/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnv-opensource%2Fk6-script-from-har/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dnv-opensource","download_url":"https://codeload.github.com/dnv-opensource/k6-script-from-har/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246552882,"owners_count":20795838,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-31T22:48:48.331Z","updated_at":"2025-03-31T22:48:48.875Z","avatar_url":"https://github.com/dnv-opensource.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# k6-script-from-har\r\n\r\nDead simple k6 test generator.  \r\nGiven a har file, this script parses and generates cleanly factored k6 test code.  \r\n[Only `fetch` calls]  \r\n\r\nThis is an alternative to the [har-to-k6](https://k6.io/docs/test-authoring/create-tests-from-recordings/using-the-har-converter/\r\n) converter which in our experience created messy generated \"write only\" code that required a lot of clean up every time we used it.\r\n\r\n## Who this is for\r\nWe use this for **workflow** load testing of a backend heavy SPA with 10s or 100s of users.\r\n\r\nWe wanted to measure\r\n1. Realistic load characteristics (real-world usage conditions)\r\n2. Workflow completion time\r\n    * baseline time for one virtual user\r\n    * how far can we push virtual user count until the time is unacceptable (e.g. 20s for a multi-page workflow)\r\n3. Infrastructure reliability\r\n    * at how many virtual users does something in the infrastructure break (follow up - what broke, how do we harden our infra)\r\n\r\nThe record and playback approach was a good fit for us. We also wanted basic checks in place e.g. failure response status should show up red instead of green in results.\r\n\r\n## Installation\r\nEnsure k6 is [installed](https://grafana.com/docs/k6/latest/set-up/install-k6/)\r\n\r\n``` sh\r\nnpm install --save-dev @dnvgl/k6-script-from-har\r\n```\r\n\u003e note: files `testTemplate.js` and `testCommon.js` will be copied into repository root post install\r\nthese should be committed with the repo\r\n\r\n## Usage\r\nGiven a .har file, run k6-script-from-har with the input har filename and output test filename.  \r\n\u003e note: we recommend filtering out *.har files in `.gitignore`\r\n\u003e har files can include secrets, and are very large\r\n\r\nExample:  \r\ngenerate the test file from har\r\n``` sh\r\nnpx k6-script-from-har createTimeEntryWorkflow.har tests/createTimeEntryWorkflow.js\r\n```\r\n\r\nrun the test\r\n``` sh\r\nk6 run tests/createTimeEntryWorkflow.js\r\n```\r\n\r\n## Har file creation\r\nManually\r\n- Use a browser, open up dev tools, network tab\r\n- Navigate and perform actions for workflow test\r\n- Export har [documentation: chrome export har](https://developer.chrome.com/docs/devtools/network/reference/#export)\r\n\r\nPlaywright\r\n- Given a playwright workflow test\r\n- Toggle on `recordHAR` on browser object at start of test [documentation: playwright record har](https://playwright.dev/docs/mock#recording-a-har-file)\r\n- This file can be piped into this script and keep your performance tests up-to-date with the workflow covered by the automated test.\r\n\r\n\r\n## Advanced usage\r\n`testCommon.js` will fetch an Authorization Bearer token, and attach the header automatically on all `httpRequest` if the environment variable `AT_AUTH_URL`  is set.  \r\nnote: to set an environment variable in a command prompt wrap the command with double quotes if the value includes \u0026 characters.  \r\ne.g. `set \"AT_AUTH_URL=https://your.auth.url/oauth2/v2.0/token?username=myUserName\u0026password=myPassword\u0026etc_etc_etc\"`\r\n\r\nthe following variables are also available\r\n* `AT_VU_COUNT` - defaults to 15\r\n* `AT_ITERATIONS` - defaults to AT_VU_COUNT * 3\r\n*  k6 reference details [here](https://grafana.com/docs/k6/latest/using-k6/k6-options/how-to/#where-to-set-options)\r\nby default the testTemplate defines options to run a constant number of VUs a given number of iterations. More executor details (here)[https://grafana.com/docs/k6/latest/using-k6/scenarios/executors/]\r\n\r\n`testTemplate.js` and `testCommon.js` are used by the script.  \r\nThese files can be modified for your specific purposes e.g. hardcoded auth information, set the default iterations, etc.\r\n\r\n## Additional notes\r\nThe included testCommon.js and testTemplate.js files include helpers and reasonable defaults for problems we initially ran into when we started using k6 to performance test our single page app.\r\n\r\n`testTemplate.js`\r\n* includes an `options` block with a default of a fixed number of concurrent users (VUs) and a fixed number of iterations (per VU).\r\n    * This was a good fit for us since our workflows were long running (10+ seconds), and we try testing our infrastructure with different fixed numbers of users, and ensure the infrastructure didn't fail or slow down terribly in any spots.\r\n    * We used a fixed number of iterations (per VU) since the average times were skewed lower when iterations were forcably exited because they took beyond the grace period.\r\n* includes handleSummary with custom junit reporter that provides duration and good visibility around call failures\r\n    * we consume this in Azure Pipelines with the `PublishTestResults@2` task, and pass `testResultsFiles` of `*.junit.xml`\r\n* Review the contents of testTemplate.js for more details\r\n\r\n\r\n`testCommon.js`\r\n* includes `commonSetup` helper that delegates to `_ensureLoggedIn(url)`\r\n    * `_ensureLoggedIn` makes a single call and expects the response to be an oauth token response, it saves the access_token to `state`. `state` is used by `httpRequest` to add an Authorization Bearer token when present.\r\n    * `httpRequest` adds checks to each call that it return successfully (status \u003e= 200 \u0026\u0026 status \u003c 400) and timing \u003c 10s\r\n        * without these checks the performance tests could appear to succeed even though individual calls were beginning to fail, or take a long time.\r\n    * Review the contents of testCommon.js for more details\r\n\r\n## Example code output\r\n\r\n```ts\r\nimport { httpRequest, commonSetup, state } from '/testCommon.js'; //hack to reference the executing folder without needing relative path\r\n\r\nconst given_vus = __ENV.AT_VU_COUNT === undefined ? 15 : __ENV.AT_VU_COUNT;\r\nconst given_iterations = __ENV.AT_ITERATIONS === undefined ? given_vus * 3 : __ENV.AT_ITERATIONS;\r\n\r\nexport const options = {\r\n\tvus: given_vus,\r\n\titerations: given_iterations,\r\n};\r\n\r\nexport function setup() {\r\n\tcommonSetup();\r\n\treturn state;\r\n}\r\n\r\nexport default function (setup_state) {\r\n\tObject.assign(state, setup_state);\r\n\thttpRequest('GET', 'https://status.k6.io/api/v2/status.json');\r\n\thttpRequest('GET', 'https://k6.io/data/jobs-positions.json');\r\n}\r\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdnv-opensource%2Fk6-script-from-har","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdnv-opensource%2Fk6-script-from-har","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdnv-opensource%2Fk6-script-from-har/lists"}