From 4cad6d54a258d9d53f1969d50da7ce5af2342570 Mon Sep 17 00:00:00 2001 From: oleiade Date: Mon, 11 Nov 2024 12:18:28 +0100 Subject: [PATCH] Freeze next folder's content as v0.55.x --- docs/sources/k6/v0.55.x/_index.md | 87 + docs/sources/k6/v0.55.x/examples/_index.md | 10 + .../v0.55.x/examples/api-crud-operations.md | 261 +++ .../examples/bundling-and-transpiling.md | 12 + .../k6/v0.55.x/examples/cookies-example.md | 198 ++ .../examples/correlation-and-dynamic-data.md | 149 ++ .../k6/v0.55.x/examples/crawl-webpage.md | 12 + .../k6/v0.55.x/examples/data-generation.md | 10 + .../v0.55.x/examples/data-parameterization.md | 256 +++ .../k6/v0.55.x/examples/data-uploads.md | 150 ++ .../v0.55.x/examples/distribute-workloads.md | 160 ++ .../k6/v0.55.x/examples/error-handler.md | 74 + .../k6/v0.55.x/examples/functional-testing.md | 1650 +++++++++++++++++ .../k6/v0.55.x/examples/generating-uuids.md | 82 + .../examples/get-started-with-k6/_index.md | 46 + .../get-started-with-k6/analyze-results.md | 357 ++++ .../reuse-and-re-run-tests.md | 416 +++++ .../test-for-functional-behavior.md | 159 ++ .../test-for-performance.md | 302 +++ .../get-timings-for-an-http-metric.md | 47 + .../sources/k6/v0.55.x/examples/html-forms.md | 39 + .../v0.55.x/examples/http-authentication.md | 186 ++ docs/sources/k6/v0.55.x/examples/http2.md | 28 + .../v0.55.x/examples/instant-load-increase.md | 74 + .../v0.55.x/examples/oauth-authentication.md | 332 ++++ .../sources/k6/v0.55.x/examples/parse-html.md | 63 + .../k6/v0.55.x/examples/single-request.md | 24 + docs/sources/k6/v0.55.x/examples/soap.md | 49 + docs/sources/k6/v0.55.x/examples/tls.md | 34 + .../k6/v0.55.x/examples/tracking-data-per.md | 72 + .../k6/v0.55.x/examples/tutorials/_index.md | 18 + .../v0.55.x/examples/url-query-parameters.md | 91 + .../sources/k6/v0.55.x/examples/websockets.md | 84 + docs/sources/k6/v0.55.x/extensions/_index.md | 80 + .../build-k6-binary-using-docker.md | 180 ++ .../extensions/build-k6-binary-using-go.md | 126 ++ .../k6/v0.55.x/extensions/create/_index.md | 43 + .../create/javascript-extensions.md | 348 ++++ .../extensions/create/output-extensions.md | 248 +++ .../v0.55.x/extensions/explanations/_index.md | 10 + .../explanations/extension-graduation.md | 56 + .../explanations/extensions-registry.md | 103 + .../extensions/explanations/go-js-bridge.md | 41 + docs/sources/k6/v0.55.x/extensions/explore.md | 296 +++ docs/sources/k6/v0.55.x/get-started/_index.md | 10 + .../k6/v0.55.x/get-started/resources.md | 46 + .../k6/v0.55.x/get-started/results-output.md | 105 ++ .../k6/v0.55.x/get-started/running-k6.md | 241 +++ .../k6/v0.55.x/grafana-cloud-k6/_index.md | 10 + .../k6/v0.55.x/javascript-api/_index.md | 86 + .../k6/v0.55.x/javascript-api/error-codes.md | 25 + .../javascript-api/import.meta/_index.md | 9 + .../javascript-api/import.meta/resolve.md | 32 + .../javascript-api/init-context/_index.md | 9 + .../javascript-api/init-context/open.md | 118 ++ .../k6/v0.55.x/javascript-api/jslib/_index.md | 9 + .../jslib/aws/EventBridgeClient/_index.md | 71 + .../jslib/aws/EventBridgeClient/putEvents.md | 76 + .../jslib/aws/KMSClient/KMSDataKey.md | 59 + .../jslib/aws/KMSClient/KMSKey.md | 53 + .../jslib/aws/KMSClient/_index.md | 75 + .../jslib/aws/KMSClient/generateDataKey.md | 64 + .../jslib/aws/KMSClient/listKeys.md | 54 + .../jslib/aws/LambdaClient/_index.md | 61 + .../jslib/aws/LambdaClient/invoke.md | 79 + .../jslib/aws/S3Client/Bucket.md | 45 + .../jslib/aws/S3Client/Object.md | 61 + .../jslib/aws/S3Client/S3MultipartUpload.md | 50 + .../jslib/aws/S3Client/S3Part.md | 86 + .../jslib/aws/S3Client/_index.md | 184 ++ .../aws/S3Client/abortMultipartUpload.md | 59 + .../aws/S3Client/completeMultipartUpload.md | 104 ++ .../jslib/aws/S3Client/copyObject.md | 62 + .../aws/S3Client/createMultipartUpload.md | 56 + .../jslib/aws/S3Client/deleteObject.md | 61 + .../jslib/aws/S3Client/getObject.md | 108 ++ .../jslib/aws/S3Client/listBuckets.md | 53 + .../jslib/aws/S3Client/listObjects.md | 61 + .../jslib/aws/S3Client/putObject.md | 73 + .../jslib/aws/S3Client/uploadPart.md | 94 + .../jslib/aws/SQSClient/_index.md | 67 + .../jslib/aws/SQSClient/listQueues.md | 61 + .../jslib/aws/SQSClient/sendMessage.md | 85 + .../jslib/aws/SQSClient/sendMessageBatch.md | 81 + .../jslib/aws/SecretsManagerClient/Secret.md | 63 + .../jslib/aws/SecretsManagerClient/_index.md | 93 + .../aws/SecretsManagerClient/createSecret.md | 64 + .../aws/SecretsManagerClient/deleteSecret.md | 60 + .../aws/SecretsManagerClient/getSecret.md | 59 + .../aws/SecretsManagerClient/listSecrets.md | 53 + .../SecretsManagerClient/putSecretValue.md | 69 + .../jslib/aws/SignatureV4/_index.md | 171 ++ .../jslib/aws/SignatureV4/presign.md | 208 +++ .../jslib/aws/SignatureV4/sign.md | 167 ++ .../SystemsManagerParameter.md | 75 + .../jslib/aws/SystemsManagerClient/_index.md | 81 + .../aws/SystemsManagerClient/getParameter.md | 68 + .../javascript-api/jslib/aws/_index.md | 18 + .../javascript-api/jslib/aws/awsconfig.md | 67 + .../http-instrumentation-pyroscope/_index.md | 62 + .../http-instrumentation-pyroscope/client.md | 75 + .../instrumenthttp.md | 50 + .../http-instrumentation-tempo/_index.md | 74 + .../http-instrumentation-tempo/client.md | 89 + .../instrumenthttp.md | 57 + .../http-instrumentation-tempo/options.md | 18 + .../javascript-api/jslib/httpx/_index.md | 114 ++ .../javascript-api/jslib/httpx/addheader.md | 31 + .../javascript-api/jslib/httpx/addheaders.md | 34 + .../javascript-api/jslib/httpx/addtag.md | 32 + .../javascript-api/jslib/httpx/addtags.md | 34 + .../jslib/httpx/asyncrequest.md | 55 + .../javascript-api/jslib/httpx/batch.md | 62 + .../javascript-api/jslib/httpx/clearheader.md | 30 + .../javascript-api/jslib/httpx/cleartag.md | 30 + .../javascript-api/jslib/httpx/delete.md | 41 + .../v0.55.x/javascript-api/jslib/httpx/get.md | 41 + .../javascript-api/jslib/httpx/head.md | 42 + .../javascript-api/jslib/httpx/options.md | 42 + .../javascript-api/jslib/httpx/patch.md | 47 + .../javascript-api/jslib/httpx/post.md | 45 + .../v0.55.x/javascript-api/jslib/httpx/put.md | 48 + .../javascript-api/jslib/httpx/request.md | 49 + .../javascript-api/jslib/httpx/setbaseurl.md | 30 + .../javascript-api/jslib/httpx/trace.md | 42 + .../javascript-api/jslib/k6chaijs/_index.md | 83 + .../javascript-api/jslib/k6chaijs/config.md | 53 + .../javascript-api/jslib/k6chaijs/describe.md | 92 + .../jslib/k6chaijs/error-handling.md | 96 + .../javascript-api/jslib/k6chaijs/expect.md | 168 ++ .../javascript-api/jslib/utils/_index.md | 62 + .../javascript-api/jslib/utils/check.md | 61 + .../javascript-api/jslib/utils/findbetween.md | 43 + .../jslib/utils/normaldistributionstages.md | 44 + .../jslib/utils/randomintbetween.md | 37 + .../javascript-api/jslib/utils/randomitem.md | 36 + .../jslib/utils/randomstring.md | 41 + .../javascript-api/jslib/utils/uuidv4.md | 36 + .../javascript-api/k6-browser/_index.md | 155 ++ .../k6-browser/browsercontext/_index.md | 31 + .../k6-browser/browsercontext/addcookies.md | 95 + .../browsercontext/addinitscript.md | 74 + .../k6-browser/browsercontext/clearcookies.md | 50 + .../browsercontext/clearpermissions.md | 44 + .../k6-browser/browsercontext/close.md | 44 + .../k6-browser/browsercontext/cookie.md | 22 + .../k6-browser/browsercontext/cookies.md | 82 + .../browsercontext/grantpermissions.md | 54 + .../k6-browser/browsercontext/newpage.md | 35 + .../k6-browser/browsercontext/pages.md | 52 + .../setdefaultnavigationtimeout.md | 47 + .../browsercontext/setdefaulttimeout.md | 42 + .../browsercontext/setgeolocation.md | 60 + .../k6-browser/browsercontext/setoffline.md | 56 + .../k6-browser/browsercontext/waitforevent.md | 124 ++ .../javascript-api/k6-browser/closecontext.md | 53 + .../k6-browser/console-message.md | 19 + .../javascript-api/k6-browser/context.md | 51 + .../k6-browser/elementhandle/_index.md | 169 ++ .../k6-browser/elementhandle/boundingbox.md | 60 + .../k6-browser/elementhandle/check.md | 68 + .../k6-browser/elementhandle/click.md | 110 ++ .../k6-browser/elementhandle/contentframe.md | 48 + .../k6-browser/elementhandle/dblclick.md | 65 + .../k6-browser/elementhandle/dispatchevent.md | 76 + .../k6-browser/elementhandle/fill.md | 65 + .../k6-browser/elementhandle/focus.md | 53 + .../k6-browser/elementhandle/getattribute.md | 62 + .../k6-browser/elementhandle/hover.md | 69 + .../k6-browser/elementhandle/innerhtml.md | 53 + .../k6-browser/elementhandle/innertext.md | 53 + .../k6-browser/elementhandle/inputvalue.md | 57 + .../k6-browser/elementhandle/ischecked.md | 56 + .../k6-browser/elementhandle/isdisabled.md | 56 + .../k6-browser/elementhandle/iseditable.md | 56 + .../k6-browser/elementhandle/isenabled.md | 56 + .../k6-browser/elementhandle/ishidden.md | 56 + .../k6-browser/elementhandle/isvisible.md | 56 + .../k6-browser/elementhandle/ownerframe.md | 48 + .../k6-browser/elementhandle/press.md | 67 + .../k6-browser/elementhandle/query.md | 60 + .../k6-browser/elementhandle/queryall.md | 57 + .../k6-browser/elementhandle/screenshot.md | 62 + .../elementhandle/scrollintoviewifneeded.md | 56 + .../k6-browser/elementhandle/selectoption.md | 65 + .../k6-browser/elementhandle/selecttext.md | 57 + .../k6-browser/elementhandle/setchecked.md | 69 + .../k6-browser/elementhandle/setinputfiles.md | 160 ++ .../k6-browser/elementhandle/tap.md | 70 + .../k6-browser/elementhandle/textcontent.md | 53 + .../k6-browser/elementhandle/type.md | 65 + .../k6-browser/elementhandle/uncheck.md | 69 + .../elementhandle/waitforelementstate.md | 56 + .../elementhandle/waitforselector.md | 66 + .../javascript-api/k6-browser/frame/_index.md | 58 + .../javascript-api/k6-browser/frame/check.md | 37 + .../k6-browser/frame/childframes.md | 16 + .../javascript-api/k6-browser/frame/click.md | 41 + .../k6-browser/frame/content.md | 14 + .../k6-browser/frame/dblclick.md | 40 + .../k6-browser/frame/dispatchevent.md | 45 + .../javascript-api/k6-browser/frame/dollar.md | 21 + .../k6-browser/frame/doubledollar.md | 21 + .../k6-browser/frame/evaluate.md | 23 + .../k6-browser/frame/evaluatehandle.md | 25 + .../javascript-api/k6-browser/frame/fill.md | 34 + .../javascript-api/k6-browser/frame/focus.md | 31 + .../k6-browser/frame/frameelement.md | 16 + .../k6-browser/frame/getattribute.md | 32 + .../javascript-api/k6-browser/frame/goto.md | 42 + .../javascript-api/k6-browser/frame/hover.md | 38 + .../k6-browser/frame/innerhtml.md | 31 + .../k6-browser/frame/innertext.md | 31 + .../k6-browser/frame/inputvalue.md | 31 + .../k6-browser/frame/ischecked.md | 31 + .../k6-browser/frame/isdetached.md | 16 + .../k6-browser/frame/isdisabled.md | 31 + .../k6-browser/frame/iseditable.md | 31 + .../k6-browser/frame/isenabled.md | 31 + .../k6-browser/frame/ishidden.md | 62 + .../k6-browser/frame/isvisible.md | 65 + .../k6-browser/frame/locator.md | 18 + .../javascript-api/k6-browser/frame/name.md | 16 + .../javascript-api/k6-browser/frame/page.md | 16 + .../k6-browser/frame/parentframe.md | 16 + .../javascript-api/k6-browser/frame/press.md | 43 + .../k6-browser/frame/selectoption.md | 34 + .../k6-browser/frame/setchecked.md | 70 + .../k6-browser/frame/setcontent.md | 25 + .../k6-browser/frame/setinputfiles.md | 155 ++ .../javascript-api/k6-browser/frame/tap.md | 65 + .../k6-browser/frame/textcontent.md | 31 + .../javascript-api/k6-browser/frame/title.md | 14 + .../javascript-api/k6-browser/frame/type.md | 34 + .../k6-browser/frame/uncheck.md | 37 + .../javascript-api/k6-browser/frame/url.md | 14 + .../k6-browser/frame/waitforfunction.md | 26 + .../k6-browser/frame/waitforloadstate.md | 44 + .../k6-browser/frame/waitfornavigation.md | 38 + .../k6-browser/frame/waitforselector.md | 41 + .../k6-browser/frame/waitfortimeout.md | 20 + .../javascript-api/k6-browser/isconnected.md | 49 + .../k6-browser/jshandle/_index.md | 60 + .../k6-browser/jshandle/aselement.md | 53 + .../k6-browser/jshandle/dispose.md | 54 + .../k6-browser/jshandle/evaluate.md | 62 + .../k6-browser/jshandle/evaluatehandle.md | 66 + .../k6-browser/jshandle/getproperties.md | 55 + .../k6-browser/jshandle/jsonvalue.md | 55 + .../k6-browser/keyboard/_index.md | 60 + .../k6-browser/keyboard/down.md | 18 + .../k6-browser/keyboard/inserttext.md | 18 + .../k6-browser/keyboard/press.md | 20 + .../k6-browser/keyboard/type.md | 20 + .../javascript-api/k6-browser/keyboard/up.md | 18 + .../k6-browser/locator/_index.md | 114 ++ .../k6-browser/locator/check.md | 66 + .../k6-browser/locator/clear.md | 77 + .../k6-browser/locator/click.md | 102 + .../k6-browser/locator/dblclick.md | 68 + .../k6-browser/locator/dispatchevent.md | 57 + .../javascript-api/k6-browser/locator/fill.md | 56 + .../k6-browser/locator/focus.md | 53 + .../k6-browser/locator/getattribute.md | 55 + .../k6-browser/locator/hover.md | 67 + .../k6-browser/locator/innerhtml.md | 53 + .../k6-browser/locator/innertext.md | 53 + .../k6-browser/locator/inputvalue.md | 54 + .../k6-browser/locator/ischecked.md | 56 + .../k6-browser/locator/isdisabled.md | 56 + .../k6-browser/locator/iseditable.md | 56 + .../k6-browser/locator/isenabled.md | 57 + .../k6-browser/locator/ishidden.md | 47 + .../k6-browser/locator/isvisible.md | 47 + .../k6-browser/locator/press.md | 58 + .../k6-browser/locator/selectoption.md | 63 + .../k6-browser/locator/setchecked.md | 63 + .../javascript-api/k6-browser/locator/tap.md | 57 + .../k6-browser/locator/textcontent.md | 53 + .../javascript-api/k6-browser/locator/type.md | 56 + .../k6-browser/locator/uncheck.md | 67 + .../k6-browser/locator/waitfor.md | 64 + .../k6-browser/metricmessage.md | 59 + .../javascript-api/k6-browser/mouse/_index.md | 55 + .../javascript-api/k6-browser/mouse/click.md | 23 + .../k6-browser/mouse/dblclick.md | 22 + .../javascript-api/k6-browser/mouse/down.md | 20 + .../javascript-api/k6-browser/mouse/move.md | 21 + .../javascript-api/k6-browser/mouse/up.md | 20 + .../javascript-api/k6-browser/newcontext.md | 95 + .../javascript-api/k6-browser/newpage.md | 100 + .../javascript-api/k6-browser/page/_index.md | 75 + .../k6-browser/page/bringtofront.md | 46 + .../javascript-api/k6-browser/page/check.md | 66 + .../javascript-api/k6-browser/page/click.md | 103 + .../javascript-api/k6-browser/page/close.md | 44 + .../javascript-api/k6-browser/page/content.md | 44 + .../javascript-api/k6-browser/page/context.md | 44 + .../k6-browser/page/dblclick.md | 70 + .../k6-browser/page/dispatchevent.md | 75 + .../javascript-api/k6-browser/page/dollar.md | 53 + .../k6-browser/page/doubledollar.md | 52 + .../k6-browser/page/emulatemedia.md | 57 + .../page/emulatevisiondeficiency.md | 53 + .../k6-browser/page/evaluate.md | 69 + .../k6-browser/page/evaluatehandle.md | 58 + .../javascript-api/k6-browser/page/fill.md | 64 + .../javascript-api/k6-browser/page/focus.md | 61 + .../javascript-api/k6-browser/page/frames.md | 44 + .../k6-browser/page/getattribute.md | 63 + .../javascript-api/k6-browser/page/goto.md | 71 + .../javascript-api/k6-browser/page/hover.md | 67 + .../k6-browser/page/innerhtml.md | 62 + .../k6-browser/page/innertext.md | 62 + .../k6-browser/page/inputvalue.md | 63 + .../k6-browser/page/ischecked.md | 64 + .../k6-browser/page/isclosed.md | 52 + .../k6-browser/page/isdisabled.md | 64 + .../k6-browser/page/iseditable.md | 64 + .../k6-browser/page/isenabled.md | 64 + .../k6-browser/page/ishidden.md | 63 + .../k6-browser/page/isvisible.md | 63 + .../k6-browser/page/keyboard.md | 44 + .../javascript-api/k6-browser/page/locator.md | 49 + .../k6-browser/page/mainframe.md | 44 + .../javascript-api/k6-browser/page/mouse.md | 44 + .../javascript-api/k6-browser/page/on.md | 129 ++ .../javascript-api/k6-browser/page/opener.md | 50 + .../javascript-api/k6-browser/page/press.md | 73 + .../javascript-api/k6-browser/page/reload.md | 68 + .../k6-browser/page/screenshot.md | 62 + .../k6-browser/page/selectoption.md | 64 + .../k6-browser/page/setchecked.md | 69 + .../k6-browser/page/setcontent.md | 64 + .../page/setdefaultnavigationtimeout.md | 47 + .../k6-browser/page/setdefaulttimeout.md | 42 + .../k6-browser/page/setextrahttpheaders.md | 50 + .../k6-browser/page/setinputfiles.md | 157 ++ .../k6-browser/page/setviewportsize.md | 57 + .../javascript-api/k6-browser/page/tap.md | 68 + .../k6-browser/page/textcontent.md | 61 + .../k6-browser/page/throttlecpu.md | 55 + .../k6-browser/page/throttlenetwork.md | 65 + .../javascript-api/k6-browser/page/title.md | 44 + .../k6-browser/page/touchscreen.md | 14 + .../javascript-api/k6-browser/page/type.md | 64 + .../javascript-api/k6-browser/page/uncheck.md | 68 + .../javascript-api/k6-browser/page/url.md | 44 + .../k6-browser/page/viewportsize.md | 49 + .../k6-browser/page/waitforfunction.md | 77 + .../k6-browser/page/waitforloadstate.md | 90 + .../k6-browser/page/waitfornavigation.md | 86 + .../k6-browser/page/waitforselector.md | 70 + .../k6-browser/page/waitfortimeout.md | 50 + .../javascript-api/k6-browser/page/workers.md | 44 + .../k6-browser/request/_index.md | 64 + .../k6-browser/request/allheaders.md | 57 + .../k6-browser/request/frame.md | 51 + .../k6-browser/request/headers.md | 51 + .../k6-browser/request/headersarray.md | 51 + .../k6-browser/request/headervalue.md | 51 + .../k6-browser/request/isnavigationrequest.md | 51 + .../k6-browser/request/method.md | 51 + .../k6-browser/request/postdata.md | 51 + .../k6-browser/request/postdatabuffer.md | 51 + .../k6-browser/request/resourcetype.md | 51 + .../k6-browser/request/response.md | 51 + .../javascript-api/k6-browser/request/size.md | 58 + .../k6-browser/request/timing.md | 65 + .../javascript-api/k6-browser/request/url.md | 51 + .../k6-browser/response/_index.md | 67 + .../k6-browser/response/allheaders.md | 56 + .../k6-browser/response/body.md | 50 + .../k6-browser/response/frame.md | 50 + .../k6-browser/response/headers.md | 50 + .../k6-browser/response/headersarray.md | 50 + .../k6-browser/response/headervalue.md | 58 + .../k6-browser/response/headervalues.md | 58 + .../k6-browser/response/json.md | 50 + .../javascript-api/k6-browser/response/ok.md | 49 + .../k6-browser/response/request.md | 50 + .../k6-browser/response/securitydetails.md | 61 + .../k6-browser/response/serveraddr.md | 57 + .../k6-browser/response/size.md | 57 + .../k6-browser/response/status.md | 49 + .../k6-browser/response/statusText.md | 49 + .../k6-browser/response/text.md | 50 + .../javascript-api/k6-browser/response/url.md | 50 + .../k6-browser/touchscreen/_index.md | 13 + .../k6-browser/touchscreen/tap.md | 49 + .../javascript-api/k6-browser/version.md | 42 + .../k6-browser/worker/_index.md | 49 + .../javascript-api/k6-browser/worker/url.md | 52 + .../javascript-api/k6-crypto/_index.md | 11 + .../javascript-api/k6-crypto/createhash.md | 47 + .../javascript-api/k6-crypto/createhmac.md | 48 + .../javascript-api/k6-crypto/hasher.md | 53 + .../v0.55.x/javascript-api/k6-crypto/hmac.md | 54 + .../v0.55.x/javascript-api/k6-crypto/md4.md | 48 + .../v0.55.x/javascript-api/k6-crypto/md5.md | 48 + .../javascript-api/k6-crypto/randombytes.md | 38 + .../javascript-api/k6-crypto/ripemd160.md | 48 + .../v0.55.x/javascript-api/k6-crypto/sha1.md | 48 + .../javascript-api/k6-crypto/sha256.md | 48 + .../javascript-api/k6-crypto/sha384.md | 48 + .../javascript-api/k6-crypto/sha512.md | 48 + .../javascript-api/k6-crypto/sha512_224.md | 48 + .../javascript-api/k6-crypto/sha512_256.md | 48 + .../v0.55.x/javascript-api/k6-data/_index.md | 9 + .../javascript-api/k6-data/sharedarray.md | 138 ++ .../javascript-api/k6-encoding/_index.md | 9 + .../b64decode--input---encoding---format.md | 50 + .../b64encode--input---encoding.md | 40 + .../k6/v0.55.x/javascript-api/k6-execution.md | 255 +++ .../javascript-api/k6-experimental/_index.md | 11 + .../k6-experimental/csv/Options.md | 53 + .../k6-experimental/csv/Parser.md | 92 + .../k6-experimental/csv/_index.md | 107 ++ .../k6-experimental/csv/parse.md | 78 + .../k6-experimental/fs/FileInfo.md | 36 + .../k6-experimental/fs/SeekMode.md | 40 + .../k6-experimental/fs/_index.md | 77 + .../k6-experimental/fs/file/_index.md | 71 + .../k6-experimental/fs/file/read.md | 106 ++ .../k6-experimental/fs/file/seek.md | 49 + .../k6-experimental/fs/file/stat.md | 35 + .../javascript-api/k6-experimental/fs/open.md | 45 + .../k6-experimental/redis/_index.md | 35 + .../k6-experimental/redis/client/_index.md | 352 ++++ .../redis/client/client-decr.md | 47 + .../redis/client/client-decrby.md | 43 + .../redis/client/client-del.md | 47 + .../redis/client/client-exists.md | 49 + .../redis/client/client-expire.md | 44 + .../redis/client/client-get.md | 47 + .../redis/client/client-getdel.md | 43 + .../redis/client/client-getset.md | 40 + .../redis/client/client-hdel.md | 40 + .../redis/client/client-hget.md | 40 + .../redis/client/client-hgetall.md | 40 + .../redis/client/client-hincrby.md | 40 + .../redis/client/client-hkeys.md | 45 + .../redis/client/client-hlen.md | 39 + .../redis/client/client-hset.md | 41 + .../redis/client/client-hsetnx.md | 45 + .../redis/client/client-hvals.md | 45 + .../redis/client/client-incr.md | 45 + .../redis/client/client-incrby.md | 46 + .../redis/client/client-lindex.md | 46 + .../redis/client/client-llen.md | 46 + .../redis/client/client-lpop.md | 42 + .../redis/client/client-lpush.md | 43 + .../redis/client/client-lrange.md | 48 + .../redis/client/client-lrem.md | 50 + .../redis/client/client-lset.md | 43 + .../redis/client/client-mget.md | 42 + .../redis/client/client-persist.md | 45 + .../redis/client/client-randomkey.md | 36 + .../redis/client/client-rpop.md | 42 + .../redis/client/client-rpush.md | 43 + .../redis/client/client-sadd.md | 44 + .../redis/client/client-sendcommand.md | 41 + .../redis/client/client-set.md | 49 + .../redis/client/client-sismember.md | 44 + .../redis/client/client-smembers.md | 44 + .../redis/client/client-spop.md | 44 + .../redis/client/client-srandmember.md | 43 + .../redis/client/client-srem.md | 45 + .../redis/client/client-ttl.md | 45 + .../k6-experimental/redis/redis-options.md | 66 + .../k6-experimental/streams/_index.md | 128 ++ .../readablestream/CountQueuingStrategy.md | 65 + .../streams/readablestream/_index.md | 226 +++ .../streams/readablestream/cancel.md | 74 + .../streams/readablestream/getreader.md | 65 + .../readablestreamdefaultcontroller/_index.md | 17 + .../readablestreamdefaultcontroller/close.md | 61 + .../enqueue.md | 126 ++ .../readablestreamdefaultcontroller/error.md | 65 + .../readablestreamdefaultreader/_index.md | 17 + .../readablestreamdefaultreader/cancel.md | 77 + .../readablestreamdefaultreader/read.md | 68 + .../releaseLock.md | 66 + .../k6-experimental/webcrypto/_index.md | 95 + .../k6-experimental/webcrypto/aescbcparams.md | 66 + .../k6-experimental/webcrypto/aesctrparams.md | 68 + .../k6-experimental/webcrypto/aesgcmparams.md | 68 + .../webcrypto/aeskeygenparams.md | 37 + .../webcrypto/crypto/_index.md | 22 + .../webcrypto/crypto/getrandomvalues.md | 50 + .../webcrypto/crypto/randomuuid.md | 35 + .../k6-experimental/webcrypto/cryptokey.md | 18 + .../webcrypto/cryptokeypair.md | 18 + .../webcrypto/ecdhkeyderiveparams.md | 18 + .../k6-experimental/webcrypto/ecdsaparams.md | 16 + .../webcrypto/eckeygenparams.md | 39 + .../webcrypto/hmackeygenparams.md | 39 + .../k6-experimental/webcrypto/jsonwebkey.md | 59 + .../webcrypto/rsahashedimportparams.md | 16 + .../webcrypto/rsahashedkeygenparams.md | 41 + .../webcrypto/rsaoaepparams.md | 19 + .../k6-experimental/webcrypto/rsapssparams.md | 22 + .../webcrypto/subtlecrypto/_index.md | 94 + .../webcrypto/subtlecrypto/decrypt.md | 109 ++ .../webcrypto/subtlecrypto/derivebits.md | 90 + .../webcrypto/subtlecrypto/digest.md | 56 + .../webcrypto/subtlecrypto/encrypt.md | 109 ++ .../webcrypto/subtlecrypto/exportkey.md | 83 + .../webcrypto/subtlecrypto/generatekey.md | 67 + .../webcrypto/subtlecrypto/importkey.md | 230 +++ .../webcrypto/subtlecrypto/sign.md | 133 ++ .../webcrypto/subtlecrypto/verify.md | 146 ++ .../k6-experimental/websockets/_index.md | 123 ++ .../k6-experimental/websockets/blob/_index.md | 27 + .../k6-experimental/websockets/params.md | 53 + .../websockets/websocket/_index.md | 58 + .../websocket/websocket-addeventlistener.md | 66 + .../websockets/websocket/websocket-close.md | 44 + .../websockets/websocket/websocket-onclose.md | 46 + .../websockets/websocket/websocket-onerror.md | 41 + .../websocket/websocket-onmessage.md | 43 + .../websockets/websocket/websocket-onopen.md | 41 + .../websockets/websocket/websocket-onping.md | 42 + .../websockets/websocket/websocket-onpong.md | 45 + .../websockets/websocket/websocket-ping.md | 46 + .../websockets/websocket/websocket-send.md | 45 + .../v0.55.x/javascript-api/k6-html/_index.md | 9 + .../javascript-api/k6-html/element/_index.md | 164 ++ .../k6-html/element/element-selection.md | 34 + .../javascript-api/k6-html/parsehtml.md | 38 + .../k6-html/selection/_index.md | 73 + .../k6-html/selection/selection-attr.md | 36 + .../k6-html/selection/selection-children.md | 58 + .../k6-html/selection/selection-closest.md | 57 + .../k6-html/selection/selection-contents.md | 54 + .../k6-html/selection/selection-data.md | 45 + .../k6-html/selection/selection-each.md | 52 + .../k6-html/selection/selection-eq.md | 60 + .../k6-html/selection/selection-filter.md | 69 + .../k6-html/selection/selection-find.md | 38 + .../k6-html/selection/selection-first.md | 54 + .../k6-html/selection/selection-get.md | 59 + .../k6-html/selection/selection-has.md | 53 + .../k6-html/selection/selection-html.md | 54 + .../k6-html/selection/selection-is.md | 71 + .../k6-html/selection/selection-last.md | 54 + .../k6-html/selection/selection-map.md | 63 + .../k6-html/selection/selection-next.md | 50 + .../k6-html/selection/selection-nextall.md | 49 + .../k6-html/selection/selection-nextuntil.md | 61 + .../k6-html/selection/selection-not.md | 64 + .../k6-html/selection/selection-parent.md | 49 + .../k6-html/selection/selection-parents.md | 49 + .../selection/selection-parentsuntil.md | 61 + .../k6-html/selection/selection-prev.md | 48 + .../k6-html/selection/selection-prevall.md | 49 + .../k6-html/selection/selection-prevuntil.md | 61 + .../k6-html/selection/selection-serialize.md | 40 + .../selection/selection-serializearray.md | 40 + .../selection/selection-serializeobject.md | 39 + .../k6-html/selection/selection-size.md | 54 + .../k6-html/selection/selection-slice.md | 62 + .../k6-html/selection/selection-text.md | 32 + .../k6-html/selection/selection-toarray.md | 58 + .../k6-html/selection/selection-val.md | 56 + .../v0.55.x/javascript-api/k6-http/_index.md | 9 + .../javascript-api/k6-http/asyncrequest.md | 91 + .../v0.55.x/javascript-api/k6-http/batch.md | 182 ++ .../javascript-api/k6-http/cookiejar.md | 28 + .../k6-http/cookiejar/_index.md | 43 + .../k6-http/cookiejar/cookiejar-clear.md | 47 + .../cookiejar/cookiejar-cookiesforurl.md | 39 + .../k6-http/cookiejar/cookiejar-delete.md | 47 + .../k6-http/cookiejar/cookiejar-set.md | 42 + .../k6/v0.55.x/javascript-api/k6-http/del.md | 39 + .../k6-http/expected-statuses.md | 37 + .../k6/v0.55.x/javascript-api/k6-http/file.md | 43 + .../javascript-api/k6-http/filedata.md | 39 + .../k6/v0.55.x/javascript-api/k6-http/get.md | 36 + .../k6/v0.55.x/javascript-api/k6-http/head.md | 36 + .../v0.55.x/javascript-api/k6-http/options.md | 38 + .../v0.55.x/javascript-api/k6-http/params.md | 124 ++ .../v0.55.x/javascript-api/k6-http/patch.md | 41 + .../k6/v0.55.x/javascript-api/k6-http/post.md | 57 + .../k6/v0.55.x/javascript-api/k6-http/put.md | 41 + .../v0.55.x/javascript-api/k6-http/request.md | 50 + .../javascript-api/k6-http/response/_index.md | 76 + .../response/response-clicklink---params.md | 40 + .../k6-http/response/response-html.md | 36 + .../k6-http/response/response-json.md | 36 + .../response/response-submitform---params.md | 47 + .../k6-http/set-response-callback.md | 55 + .../k6/v0.55.x/javascript-api/k6-http/url.md | 37 + .../javascript-api/k6-metrics/_index.md | 9 + .../k6-metrics/counter/_index.md | 106 ++ .../counter/counter-add-value---tags.md | 16 + .../javascript-api/k6-metrics/gauge/_index.md | 68 + .../gauge/gauge-add-value---tags.md | 16 + .../javascript-api/k6-metrics/rate/_index.md | 78 + .../k6-metrics/rate/rate-add-value---tags.md | 16 + .../javascript-api/k6-metrics/trend/_index.md | 90 + .../trend/trend-add-value---tags.md | 16 + .../javascript-api/k6-net-grpc/_index.md | 48 + .../k6-net-grpc/client/_index.md | 107 ++ .../k6-net-grpc/client/client-async-invoke.md | 56 + .../k6-net-grpc/client/client-close.md | 29 + .../client-connect-connect-address-params.md | 135 ++ .../k6-net-grpc/client/client-invoke.md | 55 + .../client/client-load-protoset.md | 31 + .../k6-net-grpc/client/client-load.md | 48 + .../javascript-api/k6-net-grpc/constants.md | 61 + .../javascript-api/k6-net-grpc/params.md | 47 + .../javascript-api/k6-net-grpc/response.md | 48 + .../k6-net-grpc/stream/_index.md | 208 +++ .../k6-net-grpc/stream/event-handler.md | 49 + .../k6-net-grpc/stream/message-metadata.md | 48 + .../k6-net-grpc/stream/stream-end.md | 49 + .../k6-net-grpc/stream/stream-error.md | 18 + .../k6-net-grpc/stream/stream-on.md | 69 + .../k6-net-grpc/stream/stream-write.md | 45 + .../javascript-api/k6-timers/_index.md | 29 + .../k6/v0.55.x/javascript-api/k6-ws/_index.md | 11 + .../v0.55.x/javascript-api/k6-ws/connect.md | 52 + .../k6/v0.55.x/javascript-api/k6-ws/params.md | 45 + .../javascript-api/k6-ws/socket/_index.md | 92 + .../k6-ws/socket/socket-close.md | 33 + .../javascript-api/k6-ws/socket/socket-on.md | 86 + .../k6-ws/socket/socket-ping.md | 34 + .../k6-ws/socket/socket-send.md | 37 + .../k6-ws/socket/socket-sendbinary.md | 41 + .../k6-ws/socket/socket-setinterval.md | 48 + .../k6-ws/socket/socket-settimeout.md | 47 + .../k6/v0.55.x/javascript-api/k6/_index.md | 9 + .../k6/v0.55.x/javascript-api/k6/check.md | 74 + .../k6/v0.55.x/javascript-api/k6/fail.md | 43 + .../k6/v0.55.x/javascript-api/k6/group.md | 72 + .../v0.55.x/javascript-api/k6/random-seed.md | 36 + .../k6/v0.55.x/javascript-api/k6/sleep.md | 50 + docs/sources/k6/v0.55.x/k6-studio/_index.md | 20 + docs/sources/k6/v0.55.x/misc/_index.md | 10 + docs/sources/k6/v0.55.x/misc/archive.md | 247 +++ docs/sources/k6/v0.55.x/misc/glossary.md | 247 +++ docs/sources/k6/v0.55.x/misc/integrations.md | 88 + docs/sources/k6/v0.55.x/misc/k6-rest-api.md | 612 ++++++ .../k6/v0.55.x/release-notes/_index.md | 14 + .../k6/v0.55.x/release-notes/v0.47.0.md | 157 ++ .../k6/v0.55.x/release-notes/v0.48.0.md | 310 ++++ .../k6/v0.55.x/release-notes/v0.49.0.md | 209 +++ .../k6/v0.55.x/release-notes/v0.50.0.md | 183 ++ .../k6/v0.55.x/release-notes/v0.51.0.md | 295 +++ .../k6/v0.55.x/release-notes/v0.52.0.md | 145 ++ .../k6/v0.55.x/release-notes/v0.53.0.md | 322 ++++ .../k6/v0.55.x/release-notes/v0.54.0.md | 326 ++++ .../k6/v0.55.x/results-output/_index.md | 28 + .../results-output/end-of-test/_index.md | 119 ++ .../end-of-test/custom-summary.md | 446 +++++ .../results-output/grafana-dashboards.md | 35 + .../results-output/real-time/_index.md | 53 + .../real-time/amazon-cloudwatch.md | 96 + .../results-output/real-time/apache-kafka.md | 100 + .../v0.55.x/results-output/real-time/cloud.md | 147 ++ .../v0.55.x/results-output/real-time/csv.md | 85 + .../results-output/real-time/datadog.md | 103 + .../results-output/real-time/dynatrace.md | 83 + .../results-output/real-time/elasticsearch.md | 100 + .../real-time/grafana-cloud-prometheus.md | 93 + .../results-output/real-time/influxdb.md | 80 + .../v0.55.x/results-output/real-time/json.md | 148 ++ .../results-output/real-time/netdata.md | 54 + .../results-output/real-time/newrelic.md | 176 ++ .../results-output/real-time/opentelemetry.md | 61 + .../real-time/prometheus-remote-write.md | 258 +++ .../results-output/real-time/statsd.md | 60 + .../results-output/real-time/timescaledb.md | 74 + .../results-output/web-dashboard/_index.md | 85 + docs/sources/k6/v0.55.x/set-up/_index.md | 10 + .../set-up/configure-your-code-editor.md | 47 + .../sources/k6/v0.55.x/set-up/fine-tune-os.md | 352 ++++ .../k6/v0.55.x/set-up/install-k6/_index.md | 90 + .../set-up/install-k6/troubleshooting.md | 40 + .../set-up/set-up-distributed-k6/_index.md | 19 + .../install-k6-operator.md | 114 ++ .../set-up-distributed-k6/troubleshooting.md | 260 +++ .../upgrade-k6-operator.md | 10 + .../set-up-distributed-k6/usage/_index.md | 10 + .../usage/common-options.md | 57 + .../executing-k6-scripts-with-testrun-crd.md | 219 +++ .../set-up-distributed-k6/usage/extensions.md | 61 + .../usage/k6-operator-to-gck6.md | 73 + .../set-up-distributed-k6/usage/reference.md | 12 + .../usage/scheduling-tests.md | 106 ++ .../k6/v0.55.x/set-up/usage-collection.md | 35 + .../v0.55.x/shared/blocking-aws-blockquote.md | 12 + .../k6/v0.55.x/shared/browser-module-wip.md | 9 + .../v0.55.x/shared/browser/env-var-options.md | 45 + .../shared/browser/web-vital-core-metrics.md | 13 + .../shared/browser/web-vital-other-metrics.md | 13 + .../k6/v0.55.x/shared/crypto-module.md | 12 + .../k6/v0.55.x/shared/experimental-module.md | 15 + .../shared/experimental-timers-module.md | 11 + docs/sources/k6/v0.55.x/shared/index.md | 3 + .../shared/javascript-api/import.meta.md | 9 + .../shared/javascript-api/init-context.md | 11 + .../k6/v0.55.x/shared/javascript-api/jslib.md | 14 + .../shared/javascript-api/k6-browser-api.md | 18 + .../javascript-api/k6-browser-module.md | 12 + .../shared/javascript-api/k6-crypto.md | 25 + .../v0.55.x/shared/javascript-api/k6-data.md | 9 + .../shared/javascript-api/k6-encoding.md | 11 + .../shared/javascript-api/k6-errors-list.md | 33 + .../javascript-api/k6-execution-intro.md | 12 + .../shared/javascript-api/k6-experimental.md | 12 + .../v0.55.x/shared/javascript-api/k6-html.md | 14 + .../v0.55.x/shared/javascript-api/k6-http.md | 30 + .../shared/javascript-api/k6-metrics.md | 12 + .../shared/javascript-api/k6-net-grpc.md | 23 + .../shared/javascript-api/k6-timers.md | 18 + .../k6/v0.55.x/shared/javascript-api/k6-ws.md | 21 + .../k6/v0.55.x/shared/javascript-api/k6.md | 13 + .../k6/v0.55.x/shared/statsd-output.md | 11 + .../webcrypto/supported-encrypt-decrypt.md | 7 + .../supported-key-methods-formats.md | 7 + .../shared/webcrypto/supported-key-methods.md | 7 + .../shared/webcrypto/supported-sign-verify.md | 7 + docs/sources/k6/v0.55.x/shared/ws-module.md | 15 + .../k6/v0.55.x/testing-guides/_index.md | 11 + .../testing-guides/api-load-testing.md | 629 +++++++ .../automated-performance-testing.md | 326 ++++ .../_index.md | 59 + .../examples/_index.md | 18 + .../examples/demo-environment.md | 8 + .../inject-grpc-faults-into-service.md | 439 +++++ .../examples/inject-http-faults-into-pod.md | 423 +++++ .../expose-your-application.md | 107 ++ .../first-steps.md | 42 + .../how-it-works.md | 38 + .../installation.md | 44 + .../requirements.md | 29 + .../xk6-disruptor/_index.md | 16 + .../xk6-disruptor/faults/_index.md | 17 + .../xk6-disruptor/faults/grpc.md | 41 + .../xk6-disruptor/faults/http.md | 41 + .../xk6-disruptor/faults/pod-termination.md | 35 + .../xk6-disruptor/poddisruptor/_index.md | 65 + .../xk6-disruptor/poddisruptor/constructor.md | 58 + .../poddisruptor/injectgrpcfaults.md | 38 + .../poddisruptor/injecthttpfaults.md | 48 + .../poddisruptor/terminate-pods.md | 26 + .../xk6-disruptor/servicedisruptor/_index.md | 57 + .../servicedisruptor/constructor.md | 33 + .../servicedisruptor/injectgrpcfaults.md | 38 + .../servicedisruptor/injecthttpfaults.md | 48 + .../servicedisruptor/terminate-pods.md | 26 + .../testing-guides/load-testing-websites.md | 345 ++++ .../running-distributed-tests.md | 416 +++++ .../testing-guides/running-large-tests.md | 369 ++++ .../testing-guides/synthetic-monitoring.md | 14 + .../testing-guides/test-types/_index.md | 95 + .../test-types/breakpoint-testing.md | 130 ++ .../testing-guides/test-types/load-testing.md | 109 ++ .../test-types/smoke-testing.md | 81 + .../testing-guides/test-types/soak-testing.md | 101 + .../test-types/spike-testing.md | 94 + .../test-types/stress-testing.md | 108 ++ .../k6/v0.55.x/using-k6-browser/_index.md | 129 ++ .../k6/v0.55.x/using-k6-browser/metrics.md | 178 ++ .../using-k6-browser/migrating-to-k6-v0-52.md | 324 ++++ .../k6/v0.55.x/using-k6-browser/options.md | 89 + .../recommended-practices/_index.md | 14 + .../hybrid-approach-to-performance.md | 213 +++ .../page-object-model-pattern.md | 97 + .../recommended-practices/select-elements.md | 33 + .../simulate-user-input-delay.md | 317 ++++ .../using-k6-browser/running-browser-tests.md | 356 ++++ docs/sources/k6/v0.55.x/using-k6/_index.md | 10 + docs/sources/k6/v0.55.x/using-k6/checks.md | 154 ++ docs/sources/k6/v0.55.x/using-k6/cookies.md | 284 +++ .../v0.55.x/using-k6/environment-variables.md | 126 ++ .../using-k6/execution-context-variables.md | 138 ++ .../k6/v0.55.x/using-k6/http-debugging.md | 102 + .../k6/v0.55.x/using-k6/http-requests.md | 224 +++ ...avascript-typescript-compatibility-mode.md | 123 ++ .../k6/v0.55.x/using-k6/k6-options/_index.md | 16 + .../k6/v0.55.x/using-k6/k6-options/how-to.md | 229 +++ .../v0.55.x/using-k6/k6-options/reference.md | 1559 ++++++++++++++++ .../k6/v0.55.x/using-k6/metrics/_index.md | 117 ++ .../using-k6/metrics/create-custom-metrics.md | 108 ++ .../k6/v0.55.x/using-k6/metrics/reference.md | 92 + docs/sources/k6/v0.55.x/using-k6/modules.md | 436 +++++ .../k6/v0.55.x/using-k6/protocols/_index.md | 39 + .../k6/v0.55.x/using-k6/protocols/grpc.md | 342 ++++ .../k6/v0.55.x/using-k6/protocols/http-2.md | 50 + .../using-k6/protocols/ssl-tls/_index.md | 38 + ...online-certificate-status-protocol-ocsp.md | 79 + .../ssl-tls/ssl-tls-client-certificates.md | 90 + .../ssl-tls/ssl-tls-version-and-ciphers.md | 137 ++ .../v0.55.x/using-k6/protocols/websockets.md | 206 ++ .../k6/v0.55.x/using-k6/scenarios/_index.md | 208 +++ .../using-k6/scenarios/advanced-examples.md | 264 +++ .../using-k6/scenarios/concepts/_index.md | 20 + .../concepts/arrival-rate-vu-allocation.md | 124 ++ .../scenarios/concepts/dropped-iterations.md | 44 + .../scenarios/concepts/graceful-stop.md | 97 + .../scenarios/concepts/open-vs-closed.md | 127 ++ .../using-k6/scenarios/executors/_index.md | 50 + .../executors/constant-arrival-rate.md | 108 ++ .../scenarios/executors/constant-vus.md | 71 + .../executors/externally-controlled.md | 68 + .../scenarios/executors/per-vu-iterations.md | 77 + .../executors/ramping-arrival-rate.md | 144 ++ .../scenarios/executors/ramping-vus.md | 109 ++ .../scenarios/executors/shared-iterations.md | 89 + .../k6/v0.55.x/using-k6/tags-and-groups.md | 341 ++++ .../v0.55.x/using-k6/test-authoring/_index.md | 10 + .../create-tests-from-recordings/_index.md | 66 + .../using-the-browser-recorder.md | 48 + .../using-the-devtools-recorder.md | 31 + .../using-the-har-converter.md | 163 ++ .../using-k6/test-authoring/test-builder.md | 21 + .../k6/v0.55.x/using-k6/test-lifecycle.md | 284 +++ .../sources/k6/v0.55.x/using-k6/thresholds.md | 530 ++++++ .../using-k6/workaround-iteration-duration.md | 69 + 822 files changed, 62830 insertions(+) create mode 100644 docs/sources/k6/v0.55.x/_index.md create mode 100644 docs/sources/k6/v0.55.x/examples/_index.md create mode 100644 docs/sources/k6/v0.55.x/examples/api-crud-operations.md create mode 100644 docs/sources/k6/v0.55.x/examples/bundling-and-transpiling.md create mode 100644 docs/sources/k6/v0.55.x/examples/cookies-example.md create mode 100644 docs/sources/k6/v0.55.x/examples/correlation-and-dynamic-data.md create mode 100644 docs/sources/k6/v0.55.x/examples/crawl-webpage.md create mode 100644 docs/sources/k6/v0.55.x/examples/data-generation.md create mode 100644 docs/sources/k6/v0.55.x/examples/data-parameterization.md create mode 100644 docs/sources/k6/v0.55.x/examples/data-uploads.md create mode 100644 docs/sources/k6/v0.55.x/examples/distribute-workloads.md create mode 100644 docs/sources/k6/v0.55.x/examples/error-handler.md create mode 100644 docs/sources/k6/v0.55.x/examples/functional-testing.md create mode 100644 docs/sources/k6/v0.55.x/examples/generating-uuids.md create mode 100644 docs/sources/k6/v0.55.x/examples/get-started-with-k6/_index.md create mode 100644 docs/sources/k6/v0.55.x/examples/get-started-with-k6/analyze-results.md create mode 100644 docs/sources/k6/v0.55.x/examples/get-started-with-k6/reuse-and-re-run-tests.md create mode 100644 docs/sources/k6/v0.55.x/examples/get-started-with-k6/test-for-functional-behavior.md create mode 100644 docs/sources/k6/v0.55.x/examples/get-started-with-k6/test-for-performance.md create mode 100644 docs/sources/k6/v0.55.x/examples/get-timings-for-an-http-metric.md create mode 100644 docs/sources/k6/v0.55.x/examples/html-forms.md create mode 100644 docs/sources/k6/v0.55.x/examples/http-authentication.md create mode 100644 docs/sources/k6/v0.55.x/examples/http2.md create mode 100644 docs/sources/k6/v0.55.x/examples/instant-load-increase.md create mode 100644 docs/sources/k6/v0.55.x/examples/oauth-authentication.md create mode 100644 docs/sources/k6/v0.55.x/examples/parse-html.md create mode 100644 docs/sources/k6/v0.55.x/examples/single-request.md create mode 100644 docs/sources/k6/v0.55.x/examples/soap.md create mode 100644 docs/sources/k6/v0.55.x/examples/tls.md create mode 100644 docs/sources/k6/v0.55.x/examples/tracking-data-per.md create mode 100644 docs/sources/k6/v0.55.x/examples/tutorials/_index.md create mode 100644 docs/sources/k6/v0.55.x/examples/url-query-parameters.md create mode 100644 docs/sources/k6/v0.55.x/examples/websockets.md create mode 100644 docs/sources/k6/v0.55.x/extensions/_index.md create mode 100644 docs/sources/k6/v0.55.x/extensions/build-k6-binary-using-docker.md create mode 100644 docs/sources/k6/v0.55.x/extensions/build-k6-binary-using-go.md create mode 100644 docs/sources/k6/v0.55.x/extensions/create/_index.md create mode 100644 docs/sources/k6/v0.55.x/extensions/create/javascript-extensions.md create mode 100644 docs/sources/k6/v0.55.x/extensions/create/output-extensions.md create mode 100644 docs/sources/k6/v0.55.x/extensions/explanations/_index.md create mode 100644 docs/sources/k6/v0.55.x/extensions/explanations/extension-graduation.md create mode 100644 docs/sources/k6/v0.55.x/extensions/explanations/extensions-registry.md create mode 100644 docs/sources/k6/v0.55.x/extensions/explanations/go-js-bridge.md create mode 100644 docs/sources/k6/v0.55.x/extensions/explore.md create mode 100644 docs/sources/k6/v0.55.x/get-started/_index.md create mode 100644 docs/sources/k6/v0.55.x/get-started/resources.md create mode 100644 docs/sources/k6/v0.55.x/get-started/results-output.md create mode 100644 docs/sources/k6/v0.55.x/get-started/running-k6.md create mode 100644 docs/sources/k6/v0.55.x/grafana-cloud-k6/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/error-codes.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/import.meta/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/import.meta/resolve.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/init-context/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/init-context/open.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/EventBridgeClient/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/EventBridgeClient/putEvents.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/KMSDataKey.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/KMSKey.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/generateDataKey.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/listKeys.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/LambdaClient/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/LambdaClient/invoke.md create mode 100755 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/Bucket.md create mode 100755 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/Object.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/S3MultipartUpload.md create mode 100755 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/S3Part.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/_index.md create mode 100755 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/abortMultipartUpload.md create mode 100755 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/completeMultipartUpload.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/copyObject.md create mode 100755 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/createMultipartUpload.md create mode 100755 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/deleteObject.md create mode 100755 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/getObject.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/listBuckets.md create mode 100755 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/listObjects.md create mode 100755 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/putObject.md create mode 100755 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/uploadPart.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/listQueues.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/sendMessage.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/sendMessageBatch.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/Secret.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/createSecret.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/deleteSecret.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/getSecret.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/listSecrets.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/putSecretValue.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SignatureV4/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SignatureV4/presign.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SignatureV4/sign.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SystemsManagerClient/SystemsManagerParameter.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SystemsManagerClient/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SystemsManagerClient/getParameter.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/aws/awsconfig.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-pyroscope/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-pyroscope/client.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-pyroscope/instrumenthttp.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/client.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/instrumenthttp.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/options.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addheader.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addheaders.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addtag.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addtags.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/asyncrequest.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/batch.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/clearheader.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/cleartag.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/delete.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/get.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/head.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/options.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/patch.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/post.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/put.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/request.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/setbaseurl.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/trace.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/config.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/describe.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/error-handling.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/expect.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/utils/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/utils/check.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/utils/findbetween.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/utils/normaldistributionstages.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/utils/randomintbetween.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/utils/randomitem.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/utils/randomstring.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/jslib/utils/uuidv4.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/addcookies.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/addinitscript.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/clearcookies.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/clearpermissions.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/close.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/cookie.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/cookies.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/grantpermissions.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/newpage.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/pages.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setdefaultnavigationtimeout.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setdefaulttimeout.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setgeolocation.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setoffline.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/waitforevent.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/closecontext.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/console-message.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/context.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/boundingbox.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/check.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/click.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/contentframe.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/dblclick.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/dispatchevent.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/fill.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/focus.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/getattribute.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/hover.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/innerhtml.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/innertext.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/inputvalue.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/ischecked.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/isdisabled.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/iseditable.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/isenabled.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/ishidden.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/isvisible.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/ownerframe.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/press.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/query.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/queryall.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/screenshot.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/scrollintoviewifneeded.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/selectoption.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/selecttext.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/setchecked.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/setinputfiles.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/tap.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/textcontent.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/type.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/uncheck.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/waitforelementstate.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/waitforselector.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/check.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/childframes.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/click.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/content.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/dblclick.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/dispatchevent.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/dollar.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/doubledollar.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/evaluate.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/evaluatehandle.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/fill.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/focus.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/frameelement.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/getattribute.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/goto.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/hover.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/innerhtml.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/innertext.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/inputvalue.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/ischecked.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/isdetached.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/isdisabled.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/iseditable.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/isenabled.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/ishidden.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/isvisible.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/locator.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/name.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/page.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/parentframe.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/press.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/selectoption.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/setchecked.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/setcontent.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/setinputfiles.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/tap.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/textcontent.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/title.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/type.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/uncheck.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/url.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitforfunction.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitforloadstate.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitfornavigation.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitforselector.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitfortimeout.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/isconnected.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/aselement.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/dispose.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/evaluate.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/evaluatehandle.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/getproperties.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/jsonvalue.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/down.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/inserttext.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/press.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/type.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/up.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/check.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/clear.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/click.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/dblclick.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/dispatchevent.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/fill.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/focus.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/getattribute.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/hover.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/innerhtml.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/innertext.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/inputvalue.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/ischecked.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/isdisabled.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/iseditable.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/isenabled.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/ishidden.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/isvisible.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/press.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/selectoption.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/setchecked.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/tap.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/textcontent.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/type.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/uncheck.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/waitfor.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/metricmessage.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/click.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/dblclick.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/down.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/move.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/up.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/newcontext.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/newpage.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/bringtofront.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/check.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/click.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/close.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/content.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/context.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/dblclick.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/dispatchevent.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/dollar.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/doubledollar.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/emulatemedia.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/emulatevisiondeficiency.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/evaluate.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/evaluatehandle.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/fill.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/focus.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/frames.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/getattribute.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/goto.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/hover.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/innerhtml.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/innertext.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/inputvalue.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/ischecked.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/isclosed.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/isdisabled.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/iseditable.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/isenabled.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/ishidden.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/isvisible.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/keyboard.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/locator.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/mainframe.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/mouse.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/on.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/opener.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/press.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/reload.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/screenshot.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/selectoption.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setchecked.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setcontent.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setdefaultnavigationtimeout.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setdefaulttimeout.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setextrahttpheaders.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setinputfiles.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setviewportsize.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/tap.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/textcontent.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/throttlecpu.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/throttlenetwork.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/title.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/touchscreen.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/type.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/uncheck.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/url.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/viewportsize.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitforfunction.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitforloadstate.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitfornavigation.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitforselector.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitfortimeout.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/workers.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/allheaders.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/frame.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/headers.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/headersarray.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/headervalue.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/isnavigationrequest.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/method.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/postdata.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/postdatabuffer.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/resourcetype.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/response.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/size.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/timing.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/url.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/allheaders.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/body.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/frame.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headers.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headersarray.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headervalue.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headervalues.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/json.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/ok.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/request.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/securitydetails.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/serveraddr.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/size.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/status.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/statusText.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/text.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/url.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/touchscreen/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/touchscreen/tap.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/version.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/worker/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-browser/worker/url.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-crypto/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-crypto/createhash.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-crypto/createhmac.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-crypto/hasher.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-crypto/hmac.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-crypto/md4.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-crypto/md5.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-crypto/randombytes.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-crypto/ripemd160.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha1.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha256.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha384.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha512.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha512_224.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha512_256.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-data/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-data/sharedarray.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-encoding/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-encoding/b64decode--input---encoding---format.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-encoding/b64encode--input---encoding.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-execution.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/Options.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/Parser.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/parse.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/FileInfo.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/SeekMode.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/read.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/seek.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/stat.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/open.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-decr.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-decrby.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-del.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-exists.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-expire.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-get.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-getdel.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-getset.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hdel.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hget.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hgetall.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hincrby.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hkeys.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hlen.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hset.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hsetnx.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hvals.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-incr.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-incrby.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lindex.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-llen.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lpop.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lpush.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lrange.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lrem.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lset.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-mget.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-persist.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-randomkey.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-rpop.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-rpush.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-sadd.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-sendcommand.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-set.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-sismember.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-smembers.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-spop.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-srandmember.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-srem.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-ttl.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/redis-options.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/CountQueuingStrategy.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/cancel.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/getreader.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/close.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/enqueue.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/error.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/cancel.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/read.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/releaseLock.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aescbcparams.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aesctrparams.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aesgcmparams.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aeskeygenparams.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/crypto/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/crypto/getrandomvalues.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/crypto/randomuuid.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/cryptokey.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/cryptokeypair.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/ecdhkeyderiveparams.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/ecdsaparams.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/eckeygenparams.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/hmackeygenparams.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/jsonwebkey.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsahashedimportparams.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsahashedkeygenparams.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsaoaepparams.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsapssparams.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/decrypt.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/derivebits.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/digest.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/encrypt.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/exportkey.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/generatekey.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/importkey.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/sign.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/verify.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/blob/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/params.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-addeventlistener.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-close.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onclose.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onerror.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onmessage.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onopen.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onping.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onpong.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-ping.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-send.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/element/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/element/element-selection.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/parsehtml.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-attr.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-children.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-closest.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-contents.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-data.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-each.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-eq.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-filter.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-find.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-first.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-get.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-has.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-html.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-is.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-last.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-map.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-next.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-nextall.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-nextuntil.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-not.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-parent.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-parents.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-parentsuntil.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-prev.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-prevall.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-prevuntil.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-serialize.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-serializearray.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-serializeobject.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-size.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-slice.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-text.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-toarray.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-val.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/asyncrequest.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/batch.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-clear.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-cookiesforurl.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-delete.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-set.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/del.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/expected-statuses.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/file.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/filedata.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/get.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/head.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/options.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/params.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/patch.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/post.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/put.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/request.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/response/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-clicklink---params.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-html.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-json.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-submitform---params.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/set-response-callback.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-http/url.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-metrics/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-metrics/counter/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-metrics/counter/counter-add-value---tags.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-metrics/gauge/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-metrics/gauge/gauge-add-value---tags.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-metrics/rate/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-metrics/rate/rate-add-value---tags.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-metrics/trend/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-metrics/trend/trend-add-value---tags.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-async-invoke.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-close.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-connect-connect-address-params.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-invoke.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-load-protoset.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-load.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/constants.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/params.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/response.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/event-handler.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/message-metadata.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-end.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-error.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-on.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-write.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-timers/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-ws/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-ws/connect.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-ws/params.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-close.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-on.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-ping.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-send.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-sendbinary.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-setinterval.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-settimeout.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6/_index.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6/check.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6/fail.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6/group.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6/random-seed.md create mode 100644 docs/sources/k6/v0.55.x/javascript-api/k6/sleep.md create mode 100644 docs/sources/k6/v0.55.x/k6-studio/_index.md create mode 100644 docs/sources/k6/v0.55.x/misc/_index.md create mode 100644 docs/sources/k6/v0.55.x/misc/archive.md create mode 100644 docs/sources/k6/v0.55.x/misc/glossary.md create mode 100644 docs/sources/k6/v0.55.x/misc/integrations.md create mode 100644 docs/sources/k6/v0.55.x/misc/k6-rest-api.md create mode 100644 docs/sources/k6/v0.55.x/release-notes/_index.md create mode 100644 docs/sources/k6/v0.55.x/release-notes/v0.47.0.md create mode 100644 docs/sources/k6/v0.55.x/release-notes/v0.48.0.md create mode 100644 docs/sources/k6/v0.55.x/release-notes/v0.49.0.md create mode 100644 docs/sources/k6/v0.55.x/release-notes/v0.50.0.md create mode 100644 docs/sources/k6/v0.55.x/release-notes/v0.51.0.md create mode 100644 docs/sources/k6/v0.55.x/release-notes/v0.52.0.md create mode 100644 docs/sources/k6/v0.55.x/release-notes/v0.53.0.md create mode 100644 docs/sources/k6/v0.55.x/release-notes/v0.54.0.md create mode 100644 docs/sources/k6/v0.55.x/results-output/_index.md create mode 100644 docs/sources/k6/v0.55.x/results-output/end-of-test/_index.md create mode 100644 docs/sources/k6/v0.55.x/results-output/end-of-test/custom-summary.md create mode 100644 docs/sources/k6/v0.55.x/results-output/grafana-dashboards.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/_index.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/amazon-cloudwatch.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/apache-kafka.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/cloud.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/csv.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/datadog.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/dynatrace.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/elasticsearch.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/grafana-cloud-prometheus.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/influxdb.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/json.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/netdata.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/newrelic.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/opentelemetry.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/prometheus-remote-write.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/statsd.md create mode 100644 docs/sources/k6/v0.55.x/results-output/real-time/timescaledb.md create mode 100644 docs/sources/k6/v0.55.x/results-output/web-dashboard/_index.md create mode 100644 docs/sources/k6/v0.55.x/set-up/_index.md create mode 100644 docs/sources/k6/v0.55.x/set-up/configure-your-code-editor.md create mode 100644 docs/sources/k6/v0.55.x/set-up/fine-tune-os.md create mode 100644 docs/sources/k6/v0.55.x/set-up/install-k6/_index.md create mode 100644 docs/sources/k6/v0.55.x/set-up/install-k6/troubleshooting.md create mode 100644 docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/_index.md create mode 100644 docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/install-k6-operator.md create mode 100644 docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/troubleshooting.md create mode 100644 docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/upgrade-k6-operator.md create mode 100644 docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/_index.md create mode 100644 docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/common-options.md create mode 100644 docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/executing-k6-scripts-with-testrun-crd.md create mode 100644 docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/extensions.md create mode 100644 docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/k6-operator-to-gck6.md create mode 100644 docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/reference.md create mode 100644 docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/scheduling-tests.md create mode 100644 docs/sources/k6/v0.55.x/set-up/usage-collection.md create mode 100644 docs/sources/k6/v0.55.x/shared/blocking-aws-blockquote.md create mode 100644 docs/sources/k6/v0.55.x/shared/browser-module-wip.md create mode 100644 docs/sources/k6/v0.55.x/shared/browser/env-var-options.md create mode 100644 docs/sources/k6/v0.55.x/shared/browser/web-vital-core-metrics.md create mode 100644 docs/sources/k6/v0.55.x/shared/browser/web-vital-other-metrics.md create mode 100644 docs/sources/k6/v0.55.x/shared/crypto-module.md create mode 100644 docs/sources/k6/v0.55.x/shared/experimental-module.md create mode 100644 docs/sources/k6/v0.55.x/shared/experimental-timers-module.md create mode 100644 docs/sources/k6/v0.55.x/shared/index.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/import.meta.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/init-context.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/jslib.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/k6-browser-api.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/k6-browser-module.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/k6-crypto.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/k6-data.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/k6-encoding.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/k6-errors-list.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/k6-execution-intro.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/k6-experimental.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/k6-html.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/k6-http.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/k6-metrics.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/k6-net-grpc.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/k6-timers.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/k6-ws.md create mode 100644 docs/sources/k6/v0.55.x/shared/javascript-api/k6.md create mode 100644 docs/sources/k6/v0.55.x/shared/statsd-output.md create mode 100644 docs/sources/k6/v0.55.x/shared/webcrypto/supported-encrypt-decrypt.md create mode 100644 docs/sources/k6/v0.55.x/shared/webcrypto/supported-key-methods-formats.md create mode 100644 docs/sources/k6/v0.55.x/shared/webcrypto/supported-key-methods.md create mode 100644 docs/sources/k6/v0.55.x/shared/webcrypto/supported-sign-verify.md create mode 100644 docs/sources/k6/v0.55.x/shared/ws-module.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/_index.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/api-load-testing.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/automated-performance-testing.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/_index.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/_index.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/demo-environment.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/inject-grpc-faults-into-service.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/inject-http-faults-into-pod.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/expose-your-application.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/first-steps.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/how-it-works.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/installation.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/requirements.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/_index.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/_index.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/grpc.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/http.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/pod-termination.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/_index.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/constructor.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/injectgrpcfaults.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/injecthttpfaults.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/terminate-pods.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/_index.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/constructor.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/injectgrpcfaults.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/injecthttpfaults.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/terminate-pods.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/load-testing-websites.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/running-distributed-tests.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/running-large-tests.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/synthetic-monitoring.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/test-types/_index.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/test-types/breakpoint-testing.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/test-types/load-testing.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/test-types/smoke-testing.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/test-types/soak-testing.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/test-types/spike-testing.md create mode 100644 docs/sources/k6/v0.55.x/testing-guides/test-types/stress-testing.md create mode 100644 docs/sources/k6/v0.55.x/using-k6-browser/_index.md create mode 100644 docs/sources/k6/v0.55.x/using-k6-browser/metrics.md create mode 100644 docs/sources/k6/v0.55.x/using-k6-browser/migrating-to-k6-v0-52.md create mode 100644 docs/sources/k6/v0.55.x/using-k6-browser/options.md create mode 100644 docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/_index.md create mode 100644 docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/hybrid-approach-to-performance.md create mode 100644 docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/page-object-model-pattern.md create mode 100644 docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/select-elements.md create mode 100644 docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/simulate-user-input-delay.md create mode 100644 docs/sources/k6/v0.55.x/using-k6-browser/running-browser-tests.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/_index.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/checks.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/cookies.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/environment-variables.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/execution-context-variables.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/http-debugging.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/http-requests.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/javascript-typescript-compatibility-mode.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/k6-options/_index.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/k6-options/how-to.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/k6-options/reference.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/metrics/_index.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/metrics/create-custom-metrics.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/metrics/reference.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/modules.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/protocols/_index.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/protocols/grpc.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/protocols/http-2.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/_index.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/online-certificate-status-protocol-ocsp.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/ssl-tls-client-certificates.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/ssl-tls-version-and-ciphers.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/protocols/websockets.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/scenarios/_index.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/scenarios/advanced-examples.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/_index.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/arrival-rate-vu-allocation.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/dropped-iterations.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/graceful-stop.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/open-vs-closed.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/scenarios/executors/_index.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/scenarios/executors/constant-arrival-rate.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/scenarios/executors/constant-vus.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/scenarios/executors/externally-controlled.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/scenarios/executors/per-vu-iterations.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/scenarios/executors/ramping-arrival-rate.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/scenarios/executors/ramping-vus.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/scenarios/executors/shared-iterations.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/tags-and-groups.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/test-authoring/_index.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/_index.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/using-the-browser-recorder.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/using-the-devtools-recorder.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/using-the-har-converter.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/test-authoring/test-builder.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/test-lifecycle.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/thresholds.md create mode 100644 docs/sources/k6/v0.55.x/using-k6/workaround-iteration-duration.md diff --git a/docs/sources/k6/v0.55.x/_index.md b/docs/sources/k6/v0.55.x/_index.md new file mode 100644 index 000000000..35bd1d82d --- /dev/null +++ b/docs/sources/k6/v0.55.x/_index.md @@ -0,0 +1,87 @@ +--- +aliases: + - /docs/k6/ +description: 'The k6 documentation covers everything you need to know about k6 OSS, load testing, and performance testing.' +menuTitle: Grafana k6 +title: Grafana k6 +weight: -10 +hero: + title: Grafana k6 + level: 1 + image: /media/docs/k6/GrafanaLogo_k6_orange_icon.svg + width: 100 + height: 100 + description: Grafana k6 is an open-source, developer-friendly, and extensible load testing tool. k6 allows you to prevent performance issues and proactively improve reliability. +cards: + title_class: pt-0 lh-1 + items: + - title: Run your first k6 test + href: ./get-started/ + description: Learn how to install the k6 CLI, run your first k6 test, and view metric results in the terminal. + height: 24 + - title: Using k6 + href: ./using-k6/ + description: Learn about k6 options and concepts such as thresholds, metrics, lifecycle hooks, and more. + height: 24 + - title: Testing guides + href: ./testing-guides/ + description: Discover how to plan and define your performance testing strategy with these guides. + height: 24 + - title: k6 JavaScript API + href: ./javascript-api/ + description: Explore the k6 APIs through their documentation and examples. + height: 24 + - title: Explore k6 extensions + href: ./extensions/ + description: Have a particular testing need? Find k6 extensions that extend the native k6 functionality. + height: 24 + - title: k6 script examples + href: ./extensions/ + description: Learn how to script your tests with this list of common k6 examples. + height: 24 +--- + +{{< docs/hero-simple key="hero" >}} + +--- + +## Overview + +Using k6, you can test the reliability and performance of your application and infrastructure. + +k6 helps engineering teams prevent errors and SLO breaches, enabling them to build resilient and high-performing applications that scale. + +Engineering teams, including Developers, QA Engineers, SDETs, and SREs, commonly use k6 for: + +- **Load and performance testing** + + k6 is optimized for minimal resource consumption and designed for running high-load performance tests such as + [spike](https://grafana.com/docs/k6//testing-guides/test-types/spike-testing), [stress](https://grafana.com/docs/k6//testing-guides/test-types/stress-testing), or [soak tests](https://grafana.com/docs/k6//testing-guides/test-types/soak-testing). + +- **Browser performance testing** + + Through the [k6 browser API](https://grafana.com/docs/k6//using-k6-browser), you can run browser-based performance tests and collect browser metrics to identify performance issues related to browsers. Additionally, you can mix browser tests with other performance tests to get a comprehensive view of your website's performance. + +- **Performance and synthetic monitoring** + + You can schedule tests to run with minimal load very frequently, continuously validating the performance and availability of your production environment. For this, you can also use [Grafana Cloud Synthetic Monitoring](https://grafana.com/docs/grafana-cloud/testing/synthetic-monitoring/create-checks/checks/k6/), which supports running k6 scripts. + +- **Automation of performance tests** + + k6 integrates seamlessly with CI/CD and automation tools, enabling engineering teams to [automate performance testing](https://grafana.com/docs/k6//testing-guides/automated-performance-testing/) as part of their development and release cycle. + +- **Chaos and resilience testing** + + You can use k6 to simulate traffic as part of your chaos experiments, trigger them from your k6 tests or inject different types of faults in Kubernetes with [xk6-disruptor](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor). + +- **Infrastructure testing** + + With [k6 extensions](https://grafana.com/docs/k6//extensions/), you can add support to k6 for new protocols or use a particular client to directly test individual systems within your infrastructure. + +Watch the video below to learn more about k6 and why it could be the missing puzzle in your Grafana stack. + +{{< youtube id="1mtYVDA2_iQ" >}} + +## Explore + +{{< card-grid key="cards" type="simple" >}} diff --git a/docs/sources/k6/v0.55.x/examples/_index.md b/docs/sources/k6/v0.55.x/examples/_index.md new file mode 100644 index 000000000..bfc23ce97 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/_index.md @@ -0,0 +1,10 @@ +--- +title: Examples +weight: 800 +--- + +# Examples + + + +{{< section >}} diff --git a/docs/sources/k6/v0.55.x/examples/api-crud-operations.md b/docs/sources/k6/v0.55.x/examples/api-crud-operations.md new file mode 100644 index 000000000..2339ad529 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/api-crud-operations.md @@ -0,0 +1,261 @@ +--- +title: 'API CRUD Operations' +description: 'This example covers the usage of k6 to test a REST API CRUD operations.' +weight: 10 +--- + +# API CRUD Operations + +The examples showcase the testing of CRUD operations on a REST API. + +CRUD refers to the basic operations in a database: Create, Read, Update, and Delete. We can map these operations to HTTP methods in REST APIs: + +- _Create_: HTTP `POST` operation to create a new resource. +- _Read_: HTTP `GET` to retrieve a resource. +- _Update_: HTTP `PUT`or `PATCH` to change an existing resource. +- _Delete_: HTTP `DELETE` to remove a resource. + +This document has two examples, one that uses the core k6 APIs (`k6/http` and `checks`) and another to show the more recent APIs [`httpx`](https://grafana.com/docs/k6//javascript-api/jslib/httpx) and [`k6chaijs`](https://grafana.com/docs/k6//javascript-api/jslib/k6chaijs)). + +## Test steps + +In the [setup() stage](https://grafana.com/docs/k6//using-k6/test-lifecycle#setup-and-teardown-stages) we create a user for the [k6 HTTP REST API](https://test-api.k6.io/). We then retrieve and return a bearer token to authenticate the next CRUD requests. + +The steps implemented in the [VU stage](https://grafana.com/docs/k6//using-k6/test-lifecycle#the-vu-stage) are as follows: + +1. _Create_ a new resource, a "croc". +2. _Read_ the list of "crocs". +3. _Update_ the name of the "croc" and _read_ the "croc" to confirm the update operation. +4. _Delete_ the "croc" resource. + +## Core k6 APIs example + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check, group, fail } from 'k6'; + +export const options = { + vus: 1, + iterations: 1, +}; + +// Create a random string of given length +function randomString(length, charset = '') { + if (!charset) charset = 'abcdefghijklmnopqrstuvwxyz'; + let res = ''; + while (length--) res += charset[(Math.random() * charset.length) | 0]; + return res; +} + +const USERNAME = `${randomString(10)}@example.com`; // Set your own email or `${randomString(10)}@example.com`; +const PASSWORD = 'superCroc2019'; + +const BASE_URL = 'https://test-api.k6.io'; + +// Register a new user and retrieve authentication token for subsequent API requests +export function setup() { + const res = http.post(`${BASE_URL}/user/register/`, { + first_name: 'Crocodile', + last_name: 'Owner', + username: USERNAME, + password: PASSWORD, + }); + + check(res, { 'created user': (r) => r.status === 201 }); + + const loginRes = http.post(`${BASE_URL}/auth/token/login/`, { + username: USERNAME, + password: PASSWORD, + }); + + const authToken = loginRes.json('access'); + check(authToken, { 'logged in successfully': () => authToken !== '' }); + + return authToken; +} + +export default (authToken) => { + // set the authorization header on the session for the subsequent requests + const requestConfigWithTag = (tag) => ({ + headers: { + Authorization: `Bearer ${authToken}`, + }, + tags: Object.assign( + {}, + { + name: 'PrivateCrocs', + }, + tag + ), + }); + + let URL = `${BASE_URL}/my/crocodiles/`; + + group('01. Create a new crocodile', () => { + const payload = { + name: `Name ${randomString(10)}`, + sex: 'F', + date_of_birth: '2023-05-11', + }; + + const res = http.post(URL, payload, requestConfigWithTag({ name: 'Create' })); + + if (check(res, { 'Croc created correctly': (r) => r.status === 201 })) { + URL = `${URL}${res.json('id')}/`; + } else { + console.log(`Unable to create a Croc ${res.status} ${res.body}`); + return; + } + }); + + group('02. Fetch private crocs', () => { + const res = http.get(`${BASE_URL}/my/crocodiles/`, requestConfigWithTag({ name: 'Fetch' })); + check(res, { 'retrieved crocs status': (r) => r.status === 200 }); + check(res.json(), { 'retrieved crocs list': (r) => r.length > 0 }); + }); + + group('03. Update the croc', () => { + const payload = { name: 'New name' }; + const res = http.patch(URL, payload, requestConfigWithTag({ name: 'Update' })); + const isSuccessfulUpdate = check(res, { + 'Update worked': () => res.status === 200, + 'Updated name is correct': () => res.json('name') === 'New name', + }); + + if (!isSuccessfulUpdate) { + console.log(`Unable to update the croc ${res.status} ${res.body}`); + return; + } + }); + + group('04. Delete the croc', () => { + const delRes = http.del(URL, null, requestConfigWithTag({ name: 'Delete' })); + + const isSuccessfulDelete = check(null, { + 'Croc was deleted correctly': () => delRes.status === 204, + }); + + if (!isSuccessfulDelete) { + console.log(`Croc was not deleted properly`); + return; + } + }); +}; +``` + +{{< /code >}} + +## httpx and k6chaijs example + +{{< code >}} + +```javascript +import { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js'; +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; +import { + randomIntBetween, + randomItem, + randomString, +} from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; + +export const options = { + // for the example, let's run only 1 VU with 1 iteration + vus: 1, + iterations: 1, +}; + +const USERNAME = `user${randomIntBetween(1, 100000)}@example.com`; // Set your own email; +const PASSWORD = 'superCroc2019'; + +const session = new Httpx({ baseURL: 'https://test-api.k6.io' }); + +// Register a new user and retrieve authentication token for subsequent API requests +export function setup() { + let authToken = null; + + describe(`setup - create a test user ${USERNAME}`, () => { + const resp = session.post(`/user/register/`, { + first_name: 'Crocodile', + last_name: 'Owner', + username: USERNAME, + password: PASSWORD, + }); + + expect(resp.status, 'User create status').to.equal(201); + expect(resp, 'User create valid json response').to.have.validJsonBody(); + }); + + describe(`setup - Authenticate the new user ${USERNAME}`, () => { + const resp = session.post(`/auth/token/login/`, { + username: USERNAME, + password: PASSWORD, + }); + + expect(resp.status, 'Authenticate status').to.equal(200); + expect(resp, 'Authenticate valid json response').to.have.validJsonBody(); + authToken = resp.json('access'); + expect(authToken, 'Authentication token').to.be.a('string'); + }); + + return authToken; +} + +export default function (authToken) { + // set the authorization header on the session for the subsequent requests + session.addHeader('Authorization', `Bearer ${authToken}`); + + describe('01. Create a new crocodile', (t) => { + const payload = { + name: `Croc name ${randomString(10)}`, + sex: randomItem(['M', 'F']), + date_of_birth: '2023-05-11', + }; + + session.addTag('name', 'Create'); + const resp = session.post(`/my/crocodiles/`, payload); + + expect(resp.status, 'Croc creation status').to.equal(201); + expect(resp, 'Croc creation valid json response').to.have.validJsonBody(); + + session.newCrocId = resp.json('id'); + }); + + describe('02. Fetch private crocs', (t) => { + session.clearTag('name'); + const resp = session.get('/my/crocodiles/'); + + expect(resp.status, 'Fetch croc status').to.equal(200); + expect(resp, 'Fetch croc valid json response').to.have.validJsonBody(); + expect(resp.json().length, 'Number of crocs').to.be.above(0); + }); + + describe('03. Update the croc', (t) => { + const payload = { + name: `New croc name ${randomString(10)}`, + }; + + const resp = session.patch(`/my/crocodiles/${session.newCrocId}/`, payload); + + expect(resp.status, 'Croc patch status').to.equal(200); + expect(resp, 'Fetch croc valid json response').to.have.validJsonBody(); + expect(resp.json('name'), 'Croc name').to.equal(payload.name); + + // read "croc" again to verify the update worked + const resp1 = session.get(`/my/crocodiles/${session.newCrocId}/`); + + expect(resp1.status, 'Croc fetch status').to.equal(200); + expect(resp1, 'Fetch croc valid json response').to.have.validJsonBody(); + expect(resp1.json('name'), 'Croc name').to.equal(payload.name); + }); + + describe('04. Delete the croc', (t) => { + const resp = session.delete(`/my/crocodiles/${session.newCrocId}/`); + + expect(resp.status, 'Croc delete status').to.equal(204); + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/examples/bundling-and-transpiling.md b/docs/sources/k6/v0.55.x/examples/bundling-and-transpiling.md new file mode 100644 index 000000000..bc9f7ec01 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/bundling-and-transpiling.md @@ -0,0 +1,12 @@ +--- +aliases: + - ./bundling-and-transpilation # docs/k6//examples/bundling-and-transpilation +title: 'Bundling and transpiling' +description: | + Refer to the JavaScript and TypeScript compatibility mode page for more details on how to use TypeScript with k6 +weight: 18 +--- + +# Bundling and transpiling + +For more details about bundling and transpiling, refer to [JavaScript and TypeScript compatibility mode](https://grafana.com/docs/k6//using-k6/javascript-typescript-compatibility-mode/). diff --git a/docs/sources/k6/v0.55.x/examples/cookies-example.md b/docs/sources/k6/v0.55.x/examples/cookies-example.md new file mode 100644 index 000000000..81d65d9c1 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/cookies-example.md @@ -0,0 +1,198 @@ +--- +title: 'Cookies Example' +description: 'Scripting examples on how you can interact with cookies during your load test if required.' +weight: 08 +--- + +# Cookies Example + +Scripting examples on how you can interact with cookies during your load test if required. + +## Cookies + +As HTTP is a stateless protocol, cookies are used by server-side applications to persist data +on client machines. This is used more or less everywhere on the web, commonly for user session +tracking purposes. In k6 cookies are managed automatically by default, however, there are use +cases where access to read and manipulate cookies are required. + +## From the response headers + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check, group } from 'k6'; + +export default function () { + // Since this request redirects the `res.cookies` property won't contain the cookies + const res = http.get('https://httpbin.test.k6.io/cookies/set?name1=value1&name2=value2'); + check(res, { + 'status is 200': (r) => r.status === 200, + }); + + // Make sure cookies have been added to VU cookie jar + const vuJar = http.cookieJar(); + const cookiesForURL = vuJar.cookiesForURL(res.url); + check(null, { + "vu jar has cookie 'name1'": () => cookiesForURL.name1.length > 0, + "vu jar has cookie 'name2'": () => cookiesForURL.name2.length > 0, + }); +} +``` + +{{< /code >}} + +## Log all the cookies in a response + +> ### ⚠️ Note that this only works when using k6 locally +> +> The `console.log()` family of APIs are currently only usable when running k6 locally. +> When running k6 tests with LoadImpact Cloud Execution the logs will be discarded. + +{{< code >}} + +```javascript +// Example showing two methods how to log all cookies (with attributes) from a HTTP response. + +import http from 'k6/http'; + +function logCookie(cookie) { + // Here we log the name and value of the cookie along with additional attributes. + // For full list of attributes see: https:///grafana.com/docs/k6/using-k6/cookies#properties-of-a-response-cookie-object + console.log( + `${cookie.name}: ${cookie.value}\n\tdomain: ${cookie.domain}\n\tpath: ${cookie.path}\n\texpires: ${cookie.expires}\n\thttpOnly: ${cookie.http_only}` + ); +} + +export default function () { + const res = http.get('https://www.google.com/'); + + // Method 1: Use for-loop and check for non-inherited properties + for (const name in res.cookies) { + if (res.cookies.hasOwnProperty(name) !== undefined) { + logCookie(res.cookies[name][0]); + } + } + + // Method 2: Use ES6 Map to loop over Object entries + new Map(Object.entries(res.cookies)).forEach((v, k) => logCookie(v[0])); +} +``` + +{{< /code >}} + +## Setting a cookie in the VU cookie jar + +To set a cookie that should be sent with every request matching a particular domain, path, etc. +you'd do something like this: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + // Get VU cookie jar and add a cookie to it providing the parameters + // that a request must match (domain, path, HTTPS or not etc.) + // to have the cookie attached to it when sent to the server. + const jar = http.cookieJar(); + jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie', 'hello world', { + domain: 'httpbin.test.k6.io', + path: '/cookies', + secure: true, + max_age: 600, + }); + + // As the following request is matching the above cookie in terms of domain, + // path, HTTPS (secure) and will happen within the specified "age" limit, the + // cookie will be attached to this request. + const res = http.get('https://httpbin.test.k6.io/cookies'); + check(res, { + 'has status 200': (r) => r.status === 200, + "has cookie 'my_cookie'": (r) => r.json().cookies.my_cookie !== null, + 'cookie has correct value': (r) => r.json().cookies.my_cookie == 'hello world', + }); +} +``` + +{{< /code >}} + +## Delete a cookie in the VU cookie jar + +To delete a cookie in the jar for a specific URL and name, use the `delete` method. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const jar = http.cookieJar(); + jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie_1', 'hello world_1'); + jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie_2', 'hello world_2'); + + const res1 = http.get('https://httpbin.test.k6.io/cookies'); + check(res1, { + 'res1 has status 200': (r) => r.status === 200, + "res1 has cookie 'my_cookie_1'": (r) => r.json().cookies.my_cookie_1 !== null, + 'res1 cookie has correct value_1': (r) => r.json().cookies.my_cookie_1 == 'hello world_1', + "res1 has cookie 'my_cookie_2'": (r) => r.json().cookies.my_cookie_2 !== null, + 'res1 cookie has correct value_2': (r) => r.json().cookies.my_cookie_2 == 'hello world_2', + }); + + jar.delete('https://httpbin.test.k6.io/cookies', 'my_cookie_1'); + + const res2 = http.get('https://httpbin.test.k6.io/cookies'); + check(res2, { + 'res2 has status 200': (r) => r.status === 200, + "res2 hasn't cookie 'my_cookie_1'": (r) => r.json().cookies.my_cookie_1 == null, + "res2 has cookie 'my_cookie_2'": (r) => r.json().cookies.my_cookie_2 !== null, + 'res2 cookie has correct value_2': (r) => r.json().cookies.my_cookie_2 == 'hello world_2', + }); +} +``` + +{{< /code >}} + +## Clear all cookies in the VU cookie jar + +To clear all cookies in the jar by specifying url, use the `clear` method. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const jar = http.cookieJar(); + jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie', 'hello world'); + const res1 = http.get('https://httpbin.test.k6.io/cookies'); + check(res1, { + 'has status 200': (r) => r.status === 200, + "has cookie 'my_cookie'": (r) => r.json().cookies.my_cookie !== null, + 'cookie has correct value': (r) => r.json().cookies.my_cookie == 'hello world', + }); + + jar.clear('https://httpbin.test.k6.io/cookies'); + + const res2 = http.get('https://httpbin.test.k6.io/cookies'); + check(res2, { + 'has status 200': (r) => r.status === 200, + "hasn't cookie 'my_cookie'": (r) => r.json().cookies.my_cookie == null, + }); +} +``` + +{{< /code >}} + +**Relevant k6 APIs**: + +- [http.cookieJar()](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar-method) +- [http.CookieJar](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar) + +- [set(url, name, value, [options])](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar/cookiejar-set) +- [delete(url, name)](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar/cookiejar-delete) +- [clear(url)](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar/cookiejar-clear) diff --git a/docs/sources/k6/v0.55.x/examples/correlation-and-dynamic-data.md b/docs/sources/k6/v0.55.x/examples/correlation-and-dynamic-data.md new file mode 100644 index 000000000..d5cbe1dd9 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/correlation-and-dynamic-data.md @@ -0,0 +1,149 @@ +--- +title: 'Correlation and Dynamic Data' +slug: '/correlation-and-dynamic-data' +description: | + Scripting examples on how to correlate dynamic data in your test script. Correlation is + often required when using the Chrome Extension or HAR converter to generate your test script. + This is due to the fact that those tools will capture session IDs, CSRF tokens, VIEWSTATE, + wpnonce, and other dynamic values from your specific session. +weight: 04 +--- + +# Correlation and Dynamic Data + +Scripting examples on how to correlate dynamic data in your test script. Correlation is often +required when using the Chrome Extension or HAR converter to generate your test script. This +is because those tools will capture session IDs, CSRF tokens, VIEWSTATE, wpnonce, and other +dynamic values from your specific session. These tokens typically expire very quickly. This +is one of the most common things that users will script for when testing user journeys across +websites or web apps. + +### Correlation + +In a load testing scenario, correlation means extracting one or more values from the response +of one request and then reusing them in subsequent requests. Often, this could be getting +a token or some sort of ID necessary to fulfill a sequence of steps in a user journey. + +A [recording](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings) will capture session data such as CSRF tokens, +VIEWSTATES, nonce, etc. This type of data is unlikely to be valid when +you run your test, meaning you'll need to handle the extraction of this data from the HTML/form +to include it in subsequent requests. This issue is fairly common with any site that has forms +and can be handled with a little bit of scripting. + +### Extracting values/tokens from a JSON response + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + // Make a request that returns some JSON data + const res = http.get('https://httpbin.test.k6.io/json'); + + // Extract data from that JSON data by first parsing it + // using a call to "json()" and then accessing properties by + // navigating the JSON data as a JS object with dot notation. + const slide1 = res.json().slideshow.slides[0]; + check(slide1, { + 'slide 1 has correct title': (s) => s.title === 'Wake up to WonderWidgets!', + 'slide 1 has correct type': (s) => s.type === 'all', + }); + + // Now we could use the "slide1" variable in subsequent requests... +} +``` + +{{< /code >}} + +**Relevant k6 APIs**: + +- [Response.json()](https://grafana.com/docs/k6//javascript-api/k6-http/response) + +- [JSON.parse()](https://developer.mozilla.org/en-US/Web/JavaScript/Reference/Global_Objects/JSON/parse) + (An alternative API that can be used for parsing JSON data) + +### Extracting values/tokens from form fields + +You can choose from two different approaches when deciding how to handle form submissions. +Either you use the higher-level [Response.submitForm([params])](https://grafana.com/docs/k6//javascript-api/k6-http/response/response-submitform) API +or you extract necessary hidden fields etc. and build a request yourself and then send it using the +appropriate `http.*` family of APIs, like [http.post(url, [body], [params])](https://grafana.com/docs/k6//javascript-api/k6-http/post). + +#### Extracting .NET ViewStates, CSRF tokens and other hidden input fields + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export default function () { + // Request the page containing a form and save the response. This gives you access + //to the response object, `res`. + const res = http.get('https://test.k6.io/my_messages.php', { responseType: 'text' }); + + // Query the HTML for an input field named "redir". We want the value or "redir" + const elem = res.html().find('input[name=redir]'); + + // Get the value of the attribute "value" and save it to a variable + const val = elem.attr('value'); + + // Now you can concatenate this extracted value in subsequent requests that require it. + // ... + // console.log() works when executing k6 scripts locally and is handy for debugging purposes + console.log('The value of the hidden field redir is: ' + val); + + sleep(1); +} +``` + +{{< /code >}} + +> ### ⚠️ Did you know? +> +> Take note if `discardResponseBodies` is set to true in the options +> section of your script. If it is, you can either make it `false` or save the response per +> request with `{"responseType": "text"}` as shown in the example. + +**Relevant k6 APIs**: + +- [Selection.find(selector)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-find) (the [jQuery Selector API](http://api.jquery.com/category/selectors/) + docs are also a good resource on what possible selector queries can be made) +- [Selection.attr(name)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-attr) + +### Generic extraction of values/tokens + +Sometimes, responses may be neither JSON nor HTML, in which case the above extraction methods would not apply. In these situations, you would likely want to operate directly on the `Response.body` string using a simple function capable of extracting a string at some known location. This is typically achieved by looking for the string "boundaries" immediately before (left) and after (right) the value needing extraction. + +The [jslib utils](https://grafana.com/docs/k6//javascript-api/jslib/utils) library contains an example of this kind of function,[findBetween](https://grafana.com/docs/k6//javascript-api/jslib/utils/findbetween). The function uses the JavaScript built-in [String.indexOf](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) and therefore doesn't depend on potentially expensive regular-expression operations. + +#### Extracting a value/token using findBetween + +{{< code >}} + +```javascript +import { findBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; +import { check } from 'k6'; +import http from 'k6/http'; + +export default function () { + // This request returns XML: + const res = http.get('https://httpbin.test.k6.io/xml'); + + // Use findBetween to extract the first title encountered: + const title = findBetween(res.body, '', ''); + + check(title, { + 'title is correct': (t) => t === 'Wake up to WonderWidgets!', + }); +} +``` + +{{< /code >}} + +**Relevant k6 APIs**: + +- [Response.body](https://grafana.com/docs/k6//javascript-api/k6-http/response) +- [findBetween(content, left, right)](https://grafana.com/docs/k6//javascript-api/jslib/utils/findbetween) diff --git a/docs/sources/k6/v0.55.x/examples/crawl-webpage.md b/docs/sources/k6/v0.55.x/examples/crawl-webpage.md new file mode 100644 index 000000000..13c744401 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/crawl-webpage.md @@ -0,0 +1,12 @@ +--- +title: 'Crawl a web page' +description: | + An example of how to use k6 to crawl a web page +weight: 17 +--- + +# Crawl a web page + +Refer to this [Stack Overflow answer](https://stackoverflow.com/questions/60927653/downloading-whole-websites-with-k6/) for details on how you can use k6 to crawl a web page. + +You can also use the [Grafana k6 Browser Recorder](https://chromewebstore.google.com/detail/grafana-k6-browser-record/fbanjfonbcedhifbgikmjelkkckhhidl), or the [k6 DevTools Recorder](https://chromewebstore.google.com/detail/k6-devtools-recorder/fkajbajcclbdgaoanencnhpfnigfipgc) to record a browser session and export it as a k6 test script. diff --git a/docs/sources/k6/v0.55.x/examples/data-generation.md b/docs/sources/k6/v0.55.x/examples/data-generation.md new file mode 100644 index 000000000..89026ab76 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/data-generation.md @@ -0,0 +1,10 @@ +--- +title: 'Generating realistic data' +description: | + Reference project demonstrating how to generate data with realistic traits at runtime using Faker +weight: 16 +--- + +# Generating realistic data + +Refer to the [grafana/k6-example-data-generation](https://github.com/grafana/k6-example-data-generation) GitHub repository for an example of how to utilize k6 and Faker to generate realistic data for a load test. diff --git a/docs/sources/k6/v0.55.x/examples/data-parameterization.md b/docs/sources/k6/v0.55.x/examples/data-parameterization.md new file mode 100644 index 000000000..f084b2ee6 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/data-parameterization.md @@ -0,0 +1,256 @@ +--- +title: 'Data Parameterization' +description: | + Scripting examples on how to parameterize data in a test script. Parameterization is typically + necessary when Virtual Users(VUs) will make a POST, PUT, or PATCH request in a test. + + Parameterization helps to prevent server-side caching from impacting your load test. + This will, in turn, make your test more realistic. +weight: 05 +--- + +# Data Parameterization + +_Data parameterization_ is the process of turning test values into reusable parameters, for example, through variables and shared arrays. + +This page gives some examples of how to parameterize data in a test script. +Parameterization is typically necessary when Virtual Users (VUs) will make a POST, PUT, or PATCH request in a test. +You can also use parameterization when you need to add test data from a separate file. + +Parameterization helps to prevent server-side caching from impacting your load test. +This will, in turn, make your test more realistic. + +## Performance implications of `SharedArray` + +Each VU in k6 is a separate JS VM. To prevent multiple copies of the whole data file, +[SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) was added. It does have some CPU overhead in accessing elements compared to a normal non shared +array, but the difference is negligible compared to the time it takes to make requests. This becomes +even less of an issue compared to not using it with large files, as k6 would otherwise use too much memory to run, which might lead to your script not being able to run at all or aborting in the middle if the system resources are exhausted. + +For example, the Cloud service allocates 8GB of memory for every 300 VUs. So if your files are large +enough and you are not using [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray), that might mean that your script will run out of memory at +some point. Additionally even if there is enough memory, k6 has a garbage collector (as it's written +in golang) and it will walk through all accessible objects (including JS ones) and figure out which +need to be garbage collected. For big JS arrays copied hundreds of times this adds quite a lot of +additional work. + +A note on performance characteristics of `SharedArray` can be found within its [API documentation](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray#performance-characteristics). + +## From a JSON file + +{{< code >}} + +```json +{ + "users": [ + { "username": "test", "password": "qwerty" }, + { "username": "test", "password": "qwerty" } + ] +} +``` + +{{< /code >}} + +{{< code >}} + +```javascript +import { SharedArray } from 'k6/data'; +// not using SharedArray here will mean that the code in the function call (that is what loads and +// parses the json) will be executed per each VU which also means that there will be a complete copy +// per each VU +const data = new SharedArray('some data name', function () { + return JSON.parse(open('./data.json')).users; +}); + +export default function () { + const user = data[0]; + console.log(data[0].username); +} +``` + +{{< /code >}} + +## From a CSV file + +k6 doesn't parse CSV files natively, but you can use an external library, [Papa Parse](https://www.papaparse.com/). + +You can download the library and import it locally like this: + +{{< code >}} + +```javascript +import papaparse from './papaparse.js'; +import { SharedArray } from 'k6/data'; +// not using SharedArray here will mean that the code in the function call (that is what loads and +// parses the csv) will be executed per each VU which also means that there will be a complete copy +// per each VU +const csvData = new SharedArray('another data name', function () { + // Load CSV file and parse it using Papa Parse + return papaparse.parse(open('./data.csv'), { header: true }).data; +}); + +export default function () { + // ... +} +``` + +{{< /code >}} + +Or you can grab it directly from [jslib.k6.io](https://jslib.k6.io/) like this. + +{{< code >}} + +```javascript +import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; +import { SharedArray } from 'k6/data'; + +// not using SharedArray here will mean that the code in the function call (that is what loads and +// parses the csv) will be executed per each VU which also means that there will be a complete copy +// per each VU +const csvData = new SharedArray('another data name', function () { + // Load CSV file and parse it using Papa Parse + return papaparse.parse(open('./data.csv'), { header: true }).data; +}); + +export default function () { + // ... +} +``` + +{{< /code >}} + +Here's an example using Papa Parse to parse a CSV file of username/password pairs and using that +data to login to the test.k6.io test site: + +{{< code >}} + +```javascript +/* Where contents of data.csv is: +username,password +admin,123 +test_user,1234 +*/ +import http from 'k6/http'; +import { check, sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; + +// not using SharedArray here will mean that the code in the function call (that is what loads and +// parses the csv) will be executed per each VU which also means that there will be a complete copy +// per each VU +const csvData = new SharedArray('another data name', function () { + // Load CSV file and parse it using Papa Parse + return papaparse.parse(open('./data.csv'), { header: true }).data; +}); + +export default function () { + // Now you can use the CSV data in your test logic below. + // Below are some examples of how you can access the CSV data. + + // Loop through all username/password pairs + for (const userPwdPair of csvData) { + console.log(JSON.stringify(userPwdPair)); + } + + // Pick a random username/password pair + const randomUser = csvData[Math.floor(Math.random() * csvData.length)]; + console.log('Random user: ', JSON.stringify(randomUser)); + + const params = { + login: randomUser.username, + password: randomUser.password, + }; + console.log('Random user: ', JSON.stringify(params)); + + const res = http.post('https://test.k6.io/login.php', params); + check(res, { + 'login succeeded': (r) => r.status === 200 && r.body.indexOf('successfully authorized') !== -1, + }); + + sleep(1); +} +``` + +{{< /code >}} + +## Retrieving unique data + +It is often a requirement not to use the same data more than once in a test. With the help of [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution), which includes a property `scenario.iterationInTest`, you can retrieve unique rows from your data set. + +> ### ⚠️ Multiple scenarios +> +> `scenario.iterationInTest` property is unique **per scenario**, not the overall test. +> That means if you have multiple scenarios in your test you might need to split your data per scenario. + +{{< code >}} + +```javascript +import { SharedArray } from 'k6/data'; +import { scenario } from 'k6/execution'; + +const data = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +export const options = { + scenarios: { + 'use-all-the-data': { + executor: 'shared-iterations', + vus: 10, + iterations: data.length, + maxDuration: '1h', + }, + }, +}; + +export default function () { + // this is unique even in the cloud + const user = data[scenario.iterationInTest]; + console.log(`user: ${JSON.stringify(user)}`); +} +``` + +{{< /code >}} + +Alternatively, if your use case requires using a unique data set per VU, you could leverage a property called `vu.idInTest`. + +In the following example we're going to be using `per-vu-iterations` executor to ensure that every VU completes +a fixed amount of iterations. + +{{< code >}} + +```javascript +import { sleep } from 'k6'; +import { SharedArray } from 'k6/data'; +import { vu } from 'k6/execution'; + +const users = new SharedArray('users', function () { + return JSON.parse(open('./data.json')).users; +}); + +export const options = { + scenarios: { + login: { + executor: 'per-vu-iterations', + vus: users.length, + iterations: 20, + maxDuration: '1h30m', + }, + }, +}; + +export default function () { + // VU identifiers are one-based and arrays are zero-based, thus we need - 1 + console.log(`Users name: ${users[vu.idInTest - 1].username}`); + sleep(1); +} +``` + +{{< /code >}} + +## Generating data using faker.js + +The following articles show how to use faker.js in k6 to generate realistic data during the test execution: + +- [Performance Testing with Generated Data using k6 and Faker](https://dev.to/k6/performance-testing-with-generated-data-using-k6-and-faker-2e) +- [Load Testing Made Easy with K6: Using Faker Library and CSV Files](https://farhan-labib.medium.com/load-testing-made-easy-with-k6-using-faker-library-and-csv-files-c997d48fb6e2) diff --git a/docs/sources/k6/v0.55.x/examples/data-uploads.md b/docs/sources/k6/v0.55.x/examples/data-uploads.md new file mode 100644 index 000000000..211cb8cc2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/data-uploads.md @@ -0,0 +1,150 @@ +--- +title: 'Data Uploads' +description: 'Scripting examples on how to execute a load test that will upload a file to the System Under Test (SUT).' +weight: 09 +--- + +# Data Uploads + +Example to execute a load test that will upload a file to the System Under Test (SUT). + +## The open() function + +Using the built-in function [`open()`](https://grafana.com/docs/k6//javascript-api/init-context/open), +we can read the contents of a file given a filename or URL. + +Below is a simple example showing how to load the contents of a local file `data.json`. + +{{< code >}} + +```json +{ + "my_key": "has a value" +} +``` + +{{< /code >}} + +{{< code >}} + +```javascript +const data = JSON.parse(open('./data.json')); + +export default function () { + console.log(data.my_key); +} +``` + +{{< /code >}} + +If you want to open a binary file you need to pass in `"b"` as the second argument. + +{{< code >}} + +```javascript +const binFile = open('./image.png', 'b'); + +export default function () { + //... +} +``` + +{{< /code >}} + +## Multipart request (uploading a file) + +Now that you know how to load a local file, let's look at a script that creates a POST request +to upload this data to an API endpoint along with a regular text field (`field` in the example +below): + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +const binFile = open('/path/to/file.bin', 'b'); + +export default function () { + const data = { + field: 'this is a standard form field', + file: http.file(binFile, 'test.bin'), + }; + + const res = http.post('https://example.com/upload', data); + sleep(3); +} +``` + +{{< /code >}} + +In the example above we use the [http.file()](https://grafana.com/docs/k6//javascript-api/k6-http/file) +API to wrap the file contents in a [FileData](https://grafana.com/docs/k6//javascript-api/k6-http/filedata) object. +When passing a JS object as the body parameter to [http.post()](https://grafana.com/docs/k6//javascript-api/k6-http/post), +or any of the other HTTP request functions, where one of the property values is a +[FileData](https://grafana.com/docs/k6//javascript-api/k6-http/filedata) a multipart request will be constructed +and sent. + +### Relevant k6 APIs + +- [open(filePath, [mode])](https://grafana.com/docs/k6//javascript-api/init-context/open) +- [http.file(data, [filename], [contentType])](https://grafana.com/docs/k6//javascript-api/k6-http/file) + +## Advanced multipart request + +The previous multipart request example has some limitations: + +- It's not possible to assemble the parts in a specific order, because of the + unordered nature of JS objects when they're converted to Golang maps, which k6 uses internally. + Uploading files in a specific order is a requirement for some APIs (e.g. AWS S3). +- It's not possible to upload multiple files as part of the same form field, because + JS object keys must be unique. + +To address this we suggest using the [`FormData` polyfill for k6](https://jslib.k6.io/formdata/0.0.2/index.js). + +Here's an example of uploading several binary files and a text file using the polyfill: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; +import { FormData } from 'https://jslib.k6.io/formdata/0.0.2/index.js'; + +const img1 = open('/path/to/image1.png', 'b'); +const img2 = open('/path/to/image2.jpg', 'b'); +const txt = open('/path/to/text.txt'); + +export default function () { + const fd = new FormData(); + fd.append('someTextField', 'someValue'); + fd.append('aBinaryFile', { + data: new Uint8Array(img1).buffer, + filename: 'logo.png', + content_type: 'image/png', + }); + fd.append('anotherTextField', 'anotherValue'); + fd.append('images', http.file(img1, 'image1.png', 'image/png')); + fd.append('images', http.file(img2, 'image2.jpg', 'image/jpeg')); + fd.append('text', http.file(txt, 'text.txt', 'text/plain')); + + const res = http.post('https://httpbin.test.k6.io/post', fd.body(), { + headers: { 'Content-Type': 'multipart/form-data; boundary=' + fd.boundary }, + }); + check(res, { + 'is status 200': (r) => r.status === 200, + }); +} +``` + +{{< /code >}} + +Note that: + +- Both binary files will be uploaded under the `images` form field, and the text file + will appear last in the request under the `text` form field. +- It's required to specify the multipart boundary in the `Content-Type` header, + so you must assemble the header manually as shown. +- Blob is not supported or implemented. For the same functionality, use + a simple object with the fields `data`, `content_type` (defaulting to "application/octet-stream") and optionally + `filename` as shown for `aBinaryFile` above. diff --git a/docs/sources/k6/v0.55.x/examples/distribute-workloads.md b/docs/sources/k6/v0.55.x/examples/distribute-workloads.md new file mode 100644 index 000000000..5f4d7ef98 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/distribute-workloads.md @@ -0,0 +1,160 @@ +--- +title: Distribute workloads across VUs +description: How to configure different amounts of traffic for different VU behaviors +slug: /distribute-workloads +weight: 24 +--- + +# Distribute workloads across VUs + +k6 can schedule different load patterns for different VU functions. +A test with multiple workloads might better simulate traffic in the real world, where user behavior is rarely uniform. +For example, most traffic to an e-commerce site might come from users who only search for items and read reviews. A small percentage of users might actively shop, performing actions that involve writes to the database and calls to different APIs. + +The following sections provide examples of how to structure k6 scripts to split logic across VUs. +To inspect the results for a certain behavior, you can [create a custom metric](https://grafana.com/docs/k6//using-k6/metrics/create-custom-metrics) or use [Tags](https://grafana.com/docs/k6//using-k6/tags-and-groups) to filter by scenario, code block, or individual request. + +{{< admonition type="note" >}} + +These techniques can create very complex configurations. +However, more complexity creates more ambiguity in result interpretation + +{{< /admonition >}} + +## Split logic across scenarios + +{{< admonition type="note" >}} + +In this context, _workload_ refers to the traffic pattern simulated by a scenario. + +{{< /admonition >}} + +One way to distribute traffic is to use scenarios to schedule different workloads for different functions. + +1. Define multiple scenarios in your [options](https://grafana.com/docs/k6//using-k6/k6-options). +1. Use the scenario `exec` property to execute different VU functions with a workload. + +For example, imagine a social media site that typically receives 100 concurrent users. +Of those, 80 might visit their contacts page, and 20 might view the news. +To configure such a distribution, make two scenarios with different throughput or VUs: + +```javascript +import http from 'k6/http'; + +export const options = { + //scenario to view contacts + scenarios: { + contacts: { + executor: 'shared-iterations', + exec: 'contacts', + vus: 80, + iterations: 100, + }, + //scenario to view news + news: { + executor: 'shared-iterations', + exec: 'news', + vus: 20, + iterations: 100, + }, + }, +}; + +//use the exec property to run different scenarios for different functions + +export function contacts() { + http.get('https://test.k6.io/contacts.php'); +} + +export function news() { + http.get('https://test.k6.io/news.php'); +} +``` + +To view granular results for a specific scenario, you can filter by the built-in scenario [tag](https://grafana.com/docs/k6//using-k6/tags-and-groups). + +## Distribute logic by VU ID + +In some cases, writing a scenario for each behavior might be inconvenient or impractical. +As an alternative, you can distribute logic across a range of VUs with the [execution context variables](https://grafana.com/docs/k6//using-k6/execution-context-variables) from the [`k6/execution`](https://grafana.com/docs/k6//javascript-api/k6-execution) module. +With the `exec` object, you can scope logic to a specific instance, scenario, or across all VUs. + +For example, this statement assigns behavior to the first 25 VUs in a test. + +```bash +if (exec.vu.idInTest <= 25) { + //do something; + } +``` + +For more flexibility, you can use modulo expressions to distribute VUs according to percentages. +For example, the following script distributes logic according to different user profiles: + +- 40 percent of users check the news. +- 60 percent play a coinflip game. + - Half bet `heads`, and half bet `tails`. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import exec from 'k6/execution'; + +export const options = { + scenarios: { + quickRamp: { + executor: 'ramping-arrival-rate', + startRate: 0, + timeUnit: '1s', + preAllocatedVUs: 100, + stages: [ + { target: 10, duration: '10s' }, + { target: 10, duration: '15s' }, + { target: 0, duration: '5s' }, + ], + }, + }, +}; + +export default function () { + if (exec.vu.idInTest % 10 < 4) { + // 0-3 range, read the news + http.get('http://test.k6.io/news'); + } else if (exec.vu.idInTest % 10 < 7) { + // 4-6 range, bet heads + http.get('http://test.k6.io/flip_coin.php?bet=heads'); + } else { + // 7-9 range, bet tails + http.get('http://test.k6.io/flip_coin.php?bet=tails'); + } +} +``` + +To view results for a specific request or group, you can define [tags](https://grafana.com/docs/k6//using-k6/tags-and-groups). + +{{< /code >}} + +## Randomize behavior + +To add a degree of random behavior, consider one of the randomizing functions from the [k6 utils](https://grafana.com/docs/k6//javascript-api/jslib/utils). + +For example, this script randomly assigns one behavior to happen one-third of the time, and another to happen all other times. + +{{< code >}} + +```javascript +import { sleep } from 'k6'; +import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; + +export default function () { + if (randomIntBetween(1, 3) % 3 > 1) { + console.log('1 in 3 times'); + } else { + console.log('2 in 3 times'); + } +} +``` + +{{< /code >}} + +For a more sophisticated example of randomizing, read this [forum post](https://community.grafana.com/t/how-to-distribute-vus-across-different-scenarios-with-k6/97698/17). diff --git a/docs/sources/k6/v0.55.x/examples/error-handler.md b/docs/sources/k6/v0.55.x/examples/error-handler.md new file mode 100644 index 000000000..9b1a6cfec --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/error-handler.md @@ -0,0 +1,74 @@ +--- +title: 'Error handler' +description: Using a custom error handler to store errors in custom metrics or logs. +weight: 25 +--- + +# Error handler + +When encountering errors from an application's backend, gathering additional error information, such as error messages, tracing data, or request and response bodies, is often necessary. The initial suggestion is to leverage your observability solution to find these errors. However, capturing error details directly in k6 can also be useful for troubleshooting. + +In k6, there are two common approaches to store additional error information: + +- Console logs and output [k6 logs to Loki](https://k6.io/blog/using-loki-to-store-and-query-k6-logs/), a file, or use [Grafana Cloud k6](https://grafana.com/docs/grafana-cloud/k6/analyze-results/inspect-test-results/inspect-logs/). +- Using [Tags](https://grafana.com/docs/k6//using-k6/tags-and-groups/) in a [custom counter metric](https://grafana.com/docs/k6//using-k6/metrics/create-custom-metrics) to track error data. + +When deciding what error data to store, consider whether it's a general or specific error, and be aware that high-load tests may generate a substantial volume of error data. + +Below is an example using an `ErrorHandler` class to log error information. It accepts a callback that instructs how to log errors. The example provides instructions for both previously mentioned options: console logs and custom metrics. + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +// General error handler to log error details. +class ErrorHandler { + // Instruct the error handler how to log errors + constructor(logErrorDetails) { + this.logErrorDetails = logErrorDetails; + } + + // Logs response error details if isError is true. + logError(isError, res, tags = {}) { + if (!isError) return; + + // the Traceparent header is a W3C Trace Context + const traceparentHeader = res.request.headers['Traceparent']; + + // Add any other useful information + const errorData = Object.assign( + { + url: res.url, + status: res.status, + error_code: res.error_code, + traceparent: traceparentHeader && traceparentHeader.toString(), + }, + tags + ); + this.logErrorDetails(errorData); + } +} + +// Set up the error handler to log errors to the console +const errorHandler = new ErrorHandler((error) => { + console.error(error); +}); + +/* Alternatively, you may log errors using a custom metric or any other option. +const errors = new CounterMetric('errors'); +const errorHandler = new ErrorHandler((error) => {errors.add(1, error);}); +*/ + +// Basic example to demonstrate the error handler +export default function () { + let res, checkStatus; + + res = http.get('https://httpbin.org/status/200'); + checkStatus = check(res, { 'status is 200': (res) => res.status === 200 }); + errorHandler.logError(!checkStatus, res); + + res = http.get('https://httpbin.org/status/300'); + checkStatus = check(res, { 'status is 200': (res) => res.status === 200 }); + errorHandler.logError(!checkStatus, res); +} +``` diff --git a/docs/sources/k6/v0.55.x/examples/functional-testing.md b/docs/sources/k6/v0.55.x/examples/functional-testing.md new file mode 100644 index 000000000..eb661827c --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/functional-testing.md @@ -0,0 +1,1650 @@ +--- +title: 'Functional testing' +description: | + Use Chaijs library for functional and integration testing. +weight: 19 +--- + +# Functional testing + +### Most basic integration test + +{{< code >}} + + + +```javascript +import http from 'k6/http'; +import { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js'; + +export const options = { + thresholds: { + checks: ['rate == 1.00'], + }, +}; + +export default function () { + describe('Hello world!', () => { + const response = http.get('https://test-api.k6.io/public/crocodiles/'); + + expect(response.status, 'response status').to.equal(200); + expect(response).to.have.validJsonBody(); + expect(response.json(), 'croc list').to.be.an('array'); + }); +} +``` + +{{< /code >}} + +### Sample integration test + +This test goes through several steps. It creates a new user account, authenticates, and interacts with protected resources. + +{{< code >}} + + + +```javascript +import chai, { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js'; +import { Httpx, Get } from 'https://jslib.k6.io/httpx/0.0.6/index.js'; +import { randomString } from 'https://jslib.k6.io/k6-utils/1.0.0/index.js'; + +chai.config.logFailures = true; + +export let options = { + thresholds: { + // fail the test if any checks fail or any requests fail + checks: ['rate == 1.00'], + http_req_failed: ['rate == 0.00'], + }, + vus: 1, + iterations: 1, +}; + +let session = new Httpx({ baseURL: 'https://test-api.k6.io' }); + +function retrieveIndividualCrocodilesInABatch() { + describe('[Crocs service] Fetch public crocs one by one', () => { + let responses = session.batch([ + new Get('/public/crocodiles/1/'), + new Get('/public/crocodiles/2/'), + new Get('/public/crocodiles/3/'), + ]); + + expect(responses, 'responses').to.be.an('array'); + + responses.forEach((response) => { + expect(response.status, 'response status').to.equal(200); + expect(response).to.have.validJsonBody(); + expect(response.json(), 'crocodile').to.be.an('object'); + expect(response.json(), 'crocodile').to.include.keys('age', 'name', 'id', 'sex'); + expect(response.json(), 'crocodile').to.not.have.a.property('outfit'); + }); + }); +} + +function retrieveAllPublicCrocodiles() { + describe('[Crocs service] Fetch a list of crocs', () => { + let response = session.get('/public/crocodiles'); + + expect(response.status, 'response status').to.equal(200); + expect(response).to.have.validJsonBody(); + expect(response.json(), 'croc list').to.be.an('array').lengthOf.above(5); + }); +} + +function validateAuthService() { + const USERNAME = `${randomString(10)}@example.com`; + const PASSWORD = 'superCroc2021'; + + describe('[Registration service] user registration', () => { + let sampleUser = { + username: USERNAME, + password: PASSWORD, + email: USERNAME, + first_name: 'John', + last_name: 'Smith', + }; + + let response = session.post(`/user/register/`, sampleUser); + + expect(response.status, 'registration status').to.equal(201); + expect(response).to.have.validJsonBody(); + }); + + describe('[Auth service] user authentication', () => { + let authData = { + username: USERNAME, + password: PASSWORD, + }; + + let resp = session.post(`/auth/token/login/`, authData); + + expect(resp.status, 'Auth status').to.be.within(200, 204); + expect(resp).to.have.validJsonBody(); + expect(resp.json()).to.have.a.property('access'); + expect(resp.json('access'), 'auth token').to.be.a('string'); + + let authToken = resp.json('access'); + // set the authorization header on the session for the subsequent requests. + session.addHeader('Authorization', `Bearer ${authToken}`); + }); +} + +function validateCrocodileCreation() { + // authentication happened before this call. + + describe('[Croc service] Create a new crocodile', () => { + let payload = { + name: `Croc Name`, + sex: 'M', + date_of_birth: '2019-01-01', + }; + + let resp = session.post(`/my/crocodiles/`, payload); + + expect(resp.status, 'Croc creation status').to.equal(201); + expect(resp).to.have.validJsonBody(); + + session.newCrocId = resp.json('id'); // caching croc ID for the future. + }); + + describe('[Croc service] Fetch private crocs', () => { + let response = session.get('/my/crocodiles/'); + + expect(response.status, 'response status').to.equal(200); + expect(response, 'private crocs').to.have.validJsonBody(); + expect(response.json(), 'private crocs').to.not.be.empty; + }); +} + +export default function testSuite() { + retrieveIndividualCrocodilesInABatch(); + retrieveAllPublicCrocodiles(); + validateAuthService(); + validateCrocodileCreation(); +} +``` + +{{< /code >}} + +### Full example showcasing all functionality + +Here's an auto-generated k6 test script showcasing all examples from the [Chaijs API documentation](https://www.chaijs.com/api/bdd/). + +{{< code >}} + + + +```javascript +import { describe, expect, chai } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js'; + +chai.config.aggregateChecks = false; +chai.config.logFailures = true; + +export default function testSuite() { + describe('docs example 0', () => { + expect(function () {}).to.not.throw(); + expect({ a: 1 }).to.not.have.property('b'); + expect([1, 2]).to.be.an('array').that.does.not.include(3); + }); + + describe('docs example 1', () => { + expect(2).to.equal(2); // Recommended + expect(2).to.not.equal(1); // Not recommended + }); + + describe('docs example 2', () => { + // Target object deeply (but not strictly) equals `{a: 1}` + expect({ a: 1 }).to.deep.equal({ a: 1 }); + expect({ a: 1 }).to.not.equal({ a: 1 }); + + // Target array deeply (but not strictly) includes `{a: 1}` + expect([{ a: 1 }]).to.deep.include({ a: 1 }); + expect([{ a: 1 }]).to.not.include({ a: 1 }); + + // Target object deeply (but not strictly) includes `x: {a: 1}` + expect({ x: { a: 1 } }).to.deep.include({ x: { a: 1 } }); + expect({ x: { a: 1 } }).to.not.include({ x: { a: 1 } }); + + // Target array deeply (but not strictly) has member `{a: 1}` + expect([{ a: 1 }]).to.have.deep.members([{ a: 1 }]); + expect([{ a: 1 }]).to.not.have.members([{ a: 1 }]); + + // Target set deeply (but not strictly) has key `{a: 1}` + expect(new Set([{ a: 1 }])).to.have.deep.keys([{ a: 1 }]); + expect(new Set([{ a: 1 }])).to.not.have.keys([{ a: 1 }]); + + // Target object deeply (but not strictly) has property `x: {a: 1}` + expect({ x: { a: 1 } }).to.have.deep.property('x', { a: 1 }); + expect({ x: { a: 1 } }).to.not.have.property('x', { a: 1 }); + }); + + describe('docs example 3', () => { + expect({ a: { b: ['x', 'y'] } }).to.have.nested.property('a.b[1]'); + expect({ a: { b: ['x', 'y'] } }).to.nested.include({ 'a.b[1]': 'y' }); + }); + + describe('docs example 4', () => { + expect({ '.a': { '[b]': 'x' } }).to.have.nested.property('\\.a.\\[b\\]'); + expect({ '.a': { '[b]': 'x' } }).to.nested.include({ '\\.a.\\[b\\]': 'x' }); + }); + + describe('docs example 5', () => { + Object.prototype.b = 2; + + expect({ a: 1 }).to.have.own.property('a'); + expect({ a: 1 }).to.have.property('b'); + expect({ a: 1 }).to.not.have.own.property('b'); + + expect({ a: 1 }).to.own.include({ a: 1 }); + expect({ a: 1 }).to.include({ b: 2 }).but.not.own.include({ b: 2 }); + }); + + describe('docs example 6', () => { + expect([1, 2]).to.have.ordered.members([1, 2]).but.not.have.ordered.members([2, 1]); + }); + + describe('docs example 7', () => { + expect([1, 2, 3]).to.include.ordered.members([1, 2]).but.not.include.ordered.members([2, 3]); + }); + + describe('docs example 8', () => { + expect({ a: 1, b: 2 }).to.not.have.any.keys('c', 'd'); + }); + + describe('docs example 9', () => { + expect({ a: 1, b: 2 }).to.have.all.keys('a', 'b'); + }); + + describe('docs example 10', () => { + expect('foo').to.be.a('string'); + expect({ a: 1 }).to.be.an('object'); + expect(null).to.be.a('null'); + expect(undefined).to.be.an('undefined'); + expect(new Error()).to.be.an('error'); + expect(Promise.resolve()).to.be.a('promise'); + expect(new Float32Array()).to.be.a('float32array'); + expect(Symbol()).to.be.a('symbol'); + }); + + describe('docs example 11', () => { + var myObj = { + [Symbol.toStringTag]: 'myCustomType', + }; + + expect(myObj).to.be.a('myCustomType').but.not.an('object'); + }); + + describe('docs example 12', () => { + expect([1, 2, 3]).to.be.an('array').that.includes(2); + expect([]).to.be.an('array').that.is.empty; + }); + + describe('docs example 13', () => { + expect('foo').to.be.a('string'); // Recommended + expect('foo').to.not.be.an('array'); // Not recommended + }); + + describe('docs example 14', () => { + expect(1).to.be.a('string', 'nooo why fail??'); + expect(1, 'nooo why fail??').to.be.a('string'); + }); + + describe('docs example 15', () => { + expect({ b: 2 }).to.have.a.property('b'); + }); + + describe('docs example 16', () => { + expect('foobar').to.include('foo'); + }); + + describe('docs example 17', () => { + expect([1, 2, 3]).to.include(2); + }); + + describe('docs example 18', () => { + expect({ a: 1, b: 2, c: 3 }).to.include({ a: 1, b: 2 }); + }); + + describe('docs example 19', () => { + expect(new Set([1, 2])).to.include(2); + }); + + describe('docs example 20', () => { + expect( + new Map([ + ['a', 1], + ['b', 2], + ]) + ).to.include(2); + }); + + describe('docs example 21', () => { + expect([1, 2, 3]).to.be.an('array').that.includes(2); + }); + + describe('docs example 22', () => { + // Target array deeply (but not strictly) includes `{a: 1}` + expect([{ a: 1 }]).to.deep.include({ a: 1 }); + expect([{ a: 1 }]).to.not.include({ a: 1 }); + + // Target object deeply (but not strictly) includes `x: {a: 1}` + expect({ x: { a: 1 } }).to.deep.include({ x: { a: 1 } }); + expect({ x: { a: 1 } }).to.not.include({ x: { a: 1 } }); + }); + + describe('docs example 23', () => { + Object.prototype.b = 2; + + expect({ a: 1 }).to.own.include({ a: 1 }); + expect({ a: 1 }).to.include({ b: 2 }).but.not.own.include({ b: 2 }); + }); + + describe('docs example 24', () => { + expect({ a: { b: 2 } }).to.deep.own.include({ a: { b: 2 } }); + }); + + describe('docs example 25', () => { + expect({ a: { b: ['x', 'y'] } }).to.nested.include({ 'a.b[1]': 'y' }); + }); + + describe('docs example 26', () => { + expect({ '.a': { '[b]': 2 } }).to.nested.include({ '\\.a.\\[b\\]': 2 }); + }); + + describe('docs example 27', () => { + expect({ a: { b: [{ c: 3 }] } }).to.deep.nested.include({ 'a.b[0]': { c: 3 } }); + }); + + describe('docs example 28', () => { + expect('foobar').to.not.include('taco'); + expect([1, 2, 3]).to.not.include(4); + }); + + describe('docs example 29', () => { + expect({ c: 3 }).to.not.have.any.keys('a', 'b'); // Recommended + expect({ c: 3 }).to.not.include({ a: 1, b: 2 }); // Not recommended + }); + + describe('docs example 30', () => { + expect({ a: 3, b: 4 }).to.include({ a: 3, b: 4 }); // Recommended + expect({ a: 3, b: 4 }).to.not.include({ a: 1, b: 2 }); // Not recommended + }); + + describe('docs example 31', () => { + expect([1, 2, 3]).to.include(4, 'nooo why fail??'); + expect([1, 2, 3], 'nooo why fail??').to.include(4); + }); + + describe('docs example 32', () => { + // Target object's keys are a superset of ['a', 'b'] but not identical + expect({ a: 1, b: 2, c: 3 }).to.include.all.keys('a', 'b'); + expect({ a: 1, b: 2, c: 3 }).to.not.have.all.keys('a', 'b'); + + // Target array is a superset of [1, 2] but not identical + expect([1, 2, 3]).to.include.members([1, 2]); + expect([1, 2, 3]).to.not.have.members([1, 2]); + + // Duplicates in the subset are ignored + expect([1, 2, 3]).to.include.members([1, 2, 2, 2]); + }); + + describe('docs example 33', () => { + // Both assertions are identical + expect({ a: 1 }).to.include.any.keys('a', 'b'); + expect({ a: 1 }).to.have.any.keys('a', 'b'); + }); + + describe('docs example 34', () => { + expect(1).to.equal(1); // Recommended + expect(1).to.be.ok; // Not recommended + + expect(true).to.be.true; // Recommended + expect(true).to.be.ok; // Not recommended + }); + + describe('docs example 35', () => { + expect(0).to.equal(0); // Recommended + expect(0).to.not.be.ok; // Not recommended + + expect(false).to.be.false; // Recommended + expect(false).to.not.be.ok; // Not recommended + + expect(null).to.be.null; // Recommended + expect(null).to.not.be.ok; // Not recommended + + expect(undefined).to.be.undefined; // Recommended + expect(undefined).to.not.be.ok; // Not recommended + }); + + describe('docs example 36', () => { + expect(false, 'nooo why fail??').to.be.ok; + }); + + describe('docs example 37', () => { + expect(true).to.be.true; + }); + + describe('docs example 38', () => { + expect(false).to.be.false; // Recommended + expect(false).to.not.be.true; // Not recommended + + expect(1).to.equal(1); // Recommended + expect(1).to.not.be.true; // Not recommended + }); + + describe('docs example 39', () => { + expect(false, 'nooo why fail??').to.be.true; + }); + + describe('docs example 40', () => { + expect(false).to.be.false; + }); + + describe('docs example 41', () => { + expect(true).to.be.true; // Recommended + expect(true).to.not.be.false; // Not recommended + + expect(1).to.equal(1); // Recommended + expect(1).to.not.be.false; // Not recommended + }); + + describe('docs example 42', () => { + expect(true, 'nooo why fail??').to.be.false; + }); + + describe('docs example 43', () => { + expect(null).to.be.null; + }); + + describe('docs example 44', () => { + expect(1).to.equal(1); // Recommended + expect(1).to.not.be.null; // Not recommended + }); + + describe('docs example 45', () => { + expect(42, 'nooo why fail??').to.be.null; + }); + + describe('docs example 46', () => { + expect(undefined).to.be.undefined; + }); + + describe('docs example 47', () => { + expect(1).to.equal(1); // Recommended + expect(1).to.not.be.undefined; // Not recommended + }); + + describe('docs example 48', () => { + expect(42, 'nooo why fail??').to.be.undefined; + }); + + describe('docs example 49', () => { + expect(NaN).to.be.NaN; + }); + + describe('docs example 50', () => { + expect('foo').to.equal('foo'); // Recommended + expect('foo').to.not.be.NaN; // Not recommended + }); + + describe('docs example 51', () => { + expect(42, 'nooo why fail??').to.be.NaN; + }); + + describe('docs example 52', () => { + expect(1).to.equal(1); // Recommended + expect(1).to.exist; // Not recommended + + expect(0).to.equal(0); // Recommended + expect(0).to.exist; // Not recommended + }); + + describe('docs example 53', () => { + expect(null).to.be.null; // Recommended + expect(null).to.not.exist; // Not recommended + + expect(undefined).to.be.undefined; // Recommended + expect(undefined).to.not.exist; // Not recommended + }); + + describe('docs example 54', () => { + expect(null, 'nooo why fail??').to.exist; + }); + + describe('docs example 55', () => { + expect([]).to.be.empty; + expect('').to.be.empty; + }); + + describe('docs example 56', () => { + expect(new Set()).to.be.empty; + expect(new Map()).to.be.empty; + }); + + describe('docs example 57', () => { + expect({}).to.be.empty; + }); + + describe('docs example 58', () => { + expect([]).to.be.an('array').that.is.empty; + }); + + describe('docs example 59', () => { + expect([1, 2, 3]).to.have.lengthOf(3); // Recommended + expect([1, 2, 3]).to.not.be.empty; // Not recommended + + expect(new Set([1, 2, 3])).to.have.property('size', 3); // Recommended + expect(new Set([1, 2, 3])).to.not.be.empty; // Not recommended + + expect(Object.keys({ a: 1 })).to.have.lengthOf(1); // Recommended + expect({ a: 1 }).to.not.be.empty; // Not recommended + }); + + describe('docs example 60', () => { + expect([1, 2, 3], 'nooo why fail??').to.be.empty; + }); + + describe('docs example 61', () => { + function test() { + expect(arguments).to.be.arguments; + } + + test(); + }); + + describe('docs example 62', () => { + expect('foo').to.be.a('string'); // Recommended + expect('foo').to.not.be.arguments; // Not recommended + }); + + describe('docs example 63', () => { + expect({}, 'nooo why fail??').to.be.arguments; + }); + + describe('docs example 64', () => { + expect(1).to.equal(1); + expect('foo').to.equal('foo'); + }); + + describe('docs example 65', () => { + // Target object deeply (but not strictly) equals `{a: 1}` + expect({ a: 1 }).to.deep.equal({ a: 1 }); + expect({ a: 1 }).to.not.equal({ a: 1 }); + + // Target array deeply (but not strictly) equals `[1, 2]` + expect([1, 2]).to.deep.equal([1, 2]); + expect([1, 2]).to.not.equal([1, 2]); + }); + + describe('docs example 66', () => { + expect(1).to.equal(1); // Recommended + expect(1).to.not.equal(2); // Not recommended + }); + + describe('docs example 67', () => { + expect(1).to.equal(2, 'nooo why fail??'); + expect(1, 'nooo why fail??').to.equal(2); + }); + + describe('docs example 68', () => { + // Target object is deeply (but not strictly) equal to {a: 1} + expect({ a: 1 }).to.eql({ a: 1 }).but.not.equal({ a: 1 }); + + // Target array is deeply (but not strictly) equal to [1, 2] + expect([1, 2]).to.eql([1, 2]).but.not.equal([1, 2]); + }); + + describe('docs example 69', () => { + expect({ a: 1 }).to.eql({ a: 1 }); // Recommended + expect({ a: 1 }).to.not.eql({ b: 2 }); // Not recommended + }); + + describe('docs example 70', () => { + expect({ a: 1 }).to.eql({ b: 2 }, 'nooo why fail??'); + expect({ a: 1 }, 'nooo why fail??').to.eql({ b: 2 }); + }); + + describe('docs example 71', () => { + expect(2).to.equal(2); // Recommended + expect(2).to.be.above(1); // Not recommended + }); + + describe('docs example 72', () => { + expect('foo').to.have.lengthOf(3); // Recommended + expect('foo').to.have.lengthOf.above(2); // Not recommended + + expect([1, 2, 3]).to.have.lengthOf(3); // Recommended + expect([1, 2, 3]).to.have.lengthOf.above(2); // Not recommended + }); + + describe('docs example 73', () => { + expect(2).to.equal(2); // Recommended + expect(1).to.not.be.above(2); // Not recommended + }); + + describe('docs example 74', () => { + expect(1).to.be.above(2, 'nooo why fail??'); + expect(1, 'nooo why fail??').to.be.above(2); + }); + + describe('docs example 75', () => { + expect(2).to.equal(2); // Recommended + expect(2).to.be.at.least(1); // Not recommended + expect(2).to.be.at.least(2); // Not recommended + }); + + describe('docs example 76', () => { + expect('foo').to.have.lengthOf(3); // Recommended + expect('foo').to.have.lengthOf.at.least(2); // Not recommended + + expect([1, 2, 3]).to.have.lengthOf(3); // Recommended + expect([1, 2, 3]).to.have.lengthOf.at.least(2); // Not recommended + }); + + describe('docs example 77', () => { + expect(1).to.equal(1); // Recommended + expect(1).to.not.be.at.least(2); // Not recommended + }); + + describe('docs example 78', () => { + expect(1).to.be.at.least(2, 'nooo why fail??'); + expect(1, 'nooo why fail??').to.be.at.least(2); + }); + + describe('docs example 79', () => { + expect(1).to.equal(1); // Recommended + expect(1).to.be.below(2); // Not recommended + }); + + describe('docs example 80', () => { + expect('foo').to.have.lengthOf(3); // Recommended + expect('foo').to.have.lengthOf.below(4); // Not recommended + + expect([1, 2, 3]).to.have.length(3); // Recommended + expect([1, 2, 3]).to.have.lengthOf.below(4); // Not recommended + }); + + describe('docs example 81', () => { + expect(2).to.equal(2); // Recommended + expect(2).to.not.be.below(1); // Not recommended + }); + + describe('docs example 82', () => { + expect(2).to.be.below(1, 'nooo why fail??'); + expect(2, 'nooo why fail??').to.be.below(1); + }); + + describe('docs example 83', () => { + expect(1).to.equal(1); // Recommended + expect(1).to.be.at.most(2); // Not recommended + expect(1).to.be.at.most(1); // Not recommended + }); + + describe('docs example 84', () => { + expect('foo').to.have.lengthOf(3); // Recommended + expect('foo').to.have.lengthOf.at.most(4); // Not recommended + + expect([1, 2, 3]).to.have.lengthOf(3); // Recommended + expect([1, 2, 3]).to.have.lengthOf.at.most(4); // Not recommended + }); + + describe('docs example 85', () => { + expect(2).to.equal(2); // Recommended + expect(2).to.not.be.at.most(1); // Not recommended + }); + + describe('docs example 86', () => { + expect(2).to.be.at.most(1, 'nooo why fail??'); + expect(2, 'nooo why fail??').to.be.at.most(1); + }); + + describe('docs example 87', () => { + expect(2).to.equal(2); // Recommended + expect(2).to.be.within(1, 3); // Not recommended + expect(2).to.be.within(2, 3); // Not recommended + expect(2).to.be.within(1, 2); // Not recommended + }); + + describe('docs example 88', () => { + expect('foo').to.have.lengthOf.within(2, 4); // Not recommended + + expect([1, 2, 3]).to.have.lengthOf(3); // Recommended + expect([1, 2, 3]).to.have.lengthOf.within(2, 4); // Not recommended + }); + + describe('docs example 89', () => { + expect(1).to.equal(1); // Recommended + expect(1).to.not.be.within(2, 4); // Not recommended + }); + + describe('docs example 90', () => { + expect(4).to.be.within(1, 3, 'nooo why fail??'); + expect(4, 'nooo why fail??').to.be.within(1, 3); + }); + + describe('docs example 91', () => { + function Cat() {} + + expect(new Cat()).to.be.an.instanceof(Cat); + expect([1, 2]).to.be.an.instanceof(Array); + }); + + describe('docs example 92', () => { + expect({ a: 1 }).to.not.be.an.instanceof(Array); + }); + + describe('docs example 93', () => { + expect(1).to.be.an.instanceof(Array, 'nooo why fail??'); + expect(1, 'nooo why fail??').to.be.an.instanceof(Array); + }); + + describe('docs example 94', () => { + expect({ a: 1 }).to.have.property('a'); + }); + + describe('docs example 95', () => { + expect({ a: 1 }).to.have.property('a', 1); + }); + + describe('docs example 96', () => { + // Target object deeply (but not strictly) has property `x: {a: 1}` + expect({ x: { a: 1 } }).to.have.deep.property('x', { a: 1 }); + expect({ x: { a: 1 } }).to.not.have.property('x', { a: 1 }); + }); + + describe('docs example 97', () => { + Object.prototype.b = 2; + + expect({ a: 1 }).to.have.own.property('a'); + expect({ a: 1 }).to.have.own.property('a', 1); + expect({ a: 1 }).to.have.property('b'); + expect({ a: 1 }).to.not.have.own.property('b'); + }); + + describe('docs example 98', () => { + expect({ x: { a: 1 } }).to.have.deep.own.property('x', { a: 1 }); + }); + + describe('docs example 99', () => { + expect({ a: { b: ['x', 'y'] } }).to.have.nested.property('a.b[1]'); + expect({ a: { b: ['x', 'y'] } }).to.have.nested.property('a.b[1]', 'y'); + }); + + describe('docs example 100', () => { + expect({ '.a': { '[b]': 'x' } }).to.have.nested.property('\\.a.\\[b\\]'); + }); + + describe('docs example 101', () => { + expect({ a: { b: [{ c: 3 }] } }).to.have.deep.nested.property('a.b[0]', { c: 3 }); + }); + + describe('docs example 102', () => { + expect({ a: 1 }).to.not.have.property('b'); + }); + + describe('docs example 103', () => { + expect({ b: 2 }).to.not.have.property('a'); // Recommended + expect({ b: 2 }).to.not.have.property('a', 1); // Not recommended + }); + + describe('docs example 104', () => { + expect({ a: 3 }).to.have.property('a', 3); // Recommended + expect({ a: 3 }).to.not.have.property('a', 1); // Not recommended + }); + + describe('docs example 105', () => { + expect({ a: 1 }).to.have.property('a').that.is.a('number'); + }); + + describe('docs example 106', () => { + // Recommended + expect({ a: 1 }).to.have.property('a', 2, 'nooo why fail??'); + expect({ a: 1 }, 'nooo why fail??').to.have.property('a', 2); + expect({ a: 1 }, 'nooo why fail??').to.have.property('b'); + + // Not recommended + expect({ a: 1 }).to.have.property('b', undefined, 'nooo why fail??'); + }); + + describe('docs example 107', () => { + expect({ a: 1 }).to.have.ownPropertyDescriptor('a'); + }); + + describe('docs example 108', () => { + expect({ a: 1 }).to.have.ownPropertyDescriptor('a', { + configurable: true, + enumerable: true, + writable: true, + value: 1, + }); + }); + + describe('docs example 109', () => { + expect({ a: 1 }).to.not.have.ownPropertyDescriptor('b'); + }); + + describe('docs example 110', () => { + // Recommended + expect({ b: 2 }).to.not.have.ownPropertyDescriptor('a'); + + // Not recommended + expect({ b: 2 }).to.not.have.ownPropertyDescriptor('a', { + configurable: true, + enumerable: true, + writable: true, + value: 1, + }); + }); + + describe('docs example 111', () => { + // Recommended + expect({ a: 3 }).to.have.ownPropertyDescriptor('a', { + configurable: true, + enumerable: true, + writable: true, + value: 3, + }); + + // Not recommended + expect({ a: 3 }).to.not.have.ownPropertyDescriptor('a', { + configurable: true, + enumerable: true, + writable: true, + value: 1, + }); + }); + + describe('docs example 112', () => { + expect({ a: 1 }).to.have.ownPropertyDescriptor('a').that.has.property('enumerable', true); + }); + + describe('docs example 113', () => { + // Recommended + expect({ a: 1 }).to.have.ownPropertyDescriptor( + 'a', + { + configurable: true, + enumerable: true, + writable: true, + value: 2, + }, + 'nooo why fail??' + ); + + // Recommended + expect({ a: 1 }, 'nooo why fail??').to.have.ownPropertyDescriptor('a', { + configurable: true, + enumerable: true, + writable: true, + value: 2, + }); + + // Recommended + expect({ a: 1 }, 'nooo why fail??').to.have.ownPropertyDescriptor('b'); + + // Not recommended + expect({ a: 1 }).to.have.ownPropertyDescriptor('b', undefined, 'nooo why fail??'); + }); + + describe('docs example 114', () => { + expect([1, 2, 3]).to.have.lengthOf(3); + expect('foo').to.have.lengthOf(3); + expect(new Set([1, 2, 3])).to.have.lengthOf(3); + expect( + new Map([ + ['a', 1], + ['b', 2], + ['c', 3], + ]) + ).to.have.lengthOf(3); + }); + + describe('docs example 115', () => { + expect('foo').to.have.lengthOf(3); // Recommended + expect('foo').to.not.have.lengthOf(4); // Not recommended + }); + + describe('docs example 116', () => { + expect([1, 2, 3]).to.have.lengthOf(2, 'nooo why fail??'); + expect([1, 2, 3], 'nooo why fail??').to.have.lengthOf(2); + }); + + describe('docs example 117', () => { + // Recommended + expect([1, 2, 3]).to.have.lengthOf(3); + + // Not recommended + expect([1, 2, 3]).to.have.lengthOf.above(2); + expect([1, 2, 3]).to.have.lengthOf.below(4); + expect([1, 2, 3]).to.have.lengthOf.at.least(3); + expect([1, 2, 3]).to.have.lengthOf.at.most(3); + expect([1, 2, 3]).to.have.lengthOf.within(2, 4); + }); + + describe('docs example 118', () => { + expect([1, 2, 3]).to.have.a.length(3); // incompatible; throws error + expect([1, 2, 3]).to.have.a.lengthOf(3); // passes as expected + }); + + describe('docs example 119', () => { + expect('foobar').to.match(/^foo/); + }); + + describe('docs example 120', () => { + expect('foobar').to.not.match(/taco/); + }); + + describe('docs example 121', () => { + expect('foobar').to.match(/taco/, 'nooo why fail??'); + expect('foobar', 'nooo why fail??').to.match(/taco/); + }); + + describe('docs example 122', () => { + expect('foobar').to.have.string('bar'); + }); + + describe('docs example 123', () => { + expect('foobar').to.not.have.string('taco'); + }); + + describe('docs example 124', () => { + expect('foobar').to.have.string('taco', 'nooo why fail??'); + expect('foobar', 'nooo why fail??').to.have.string('taco'); + }); + + describe('docs example 125', () => { + expect({ a: 1, b: 2 }).to.have.all.keys('a', 'b'); + expect(['x', 'y']).to.have.all.keys(0, 1); + + expect({ a: 1, b: 2 }).to.have.all.keys(['a', 'b']); + expect(['x', 'y']).to.have.all.keys([0, 1]); + + expect({ a: 1, b: 2 }).to.have.all.keys({ a: 4, b: 5 }); // ignore 4 and 5 + expect(['x', 'y']).to.have.all.keys({ 0: 4, 1: 5 }); // ignore 4 and 5 + }); + + describe('docs example 126', () => { + expect( + new Map([ + ['a', 1], + ['b', 2], + ]) + ).to.have.all.keys('a', 'b'); + expect(new Set(['a', 'b'])).to.have.all.keys('a', 'b'); + }); + + describe('docs example 127', () => { + expect({ a: 1, b: 2 }).to.be.an('object').that.has.all.keys('a', 'b'); + }); + + describe('docs example 128', () => { + // Target set deeply (but not strictly) has key `{a: 1}` + expect(new Set([{ a: 1 }])).to.have.all.deep.keys([{ a: 1 }]); + expect(new Set([{ a: 1 }])).to.not.have.all.keys([{ a: 1 }]); + }); + + describe('docs example 129', () => { + // Recommended; asserts that target doesn't have any of the given keys + expect({ a: 1, b: 2 }).to.not.have.any.keys('c', 'd'); + + // Not recommended; asserts that target doesn't have all of the given + // keys but may or may not have some of them + expect({ a: 1, b: 2 }).to.not.have.all.keys('c', 'd'); + }); + + describe('docs example 130', () => { + // Recommended; asserts that target has all the given keys + expect({ a: 1, b: 2 }).to.have.all.keys('a', 'b'); + + // Not recommended; asserts that target has at least one of the given + // keys but may or may not have more of them + expect({ a: 1, b: 2 }).to.have.any.keys('a', 'b'); + }); + + describe('docs example 131', () => { + // Both assertions are identical + expect({ a: 1, b: 2 }).to.have.all.keys('a', 'b'); // Recommended + expect({ a: 1, b: 2 }).to.have.keys('a', 'b'); // Not recommended + }); + + describe('docs example 132', () => { + // Target object's keys are a superset of ['a', 'b'] but not identical + expect({ a: 1, b: 2, c: 3 }).to.include.all.keys('a', 'b'); + expect({ a: 1, b: 2, c: 3 }).to.not.have.all.keys('a', 'b'); + }); + + describe('docs example 133', () => { + // Both assertions are identical + expect({ a: 1 }).to.have.any.keys('a', 'b'); + expect({ a: 1 }).to.include.any.keys('a', 'b'); + }); + + describe('docs example 134', () => { + expect({ a: 1 }, 'nooo why fail??').to.have.key('b'); + }); + + describe('docs example 135', () => { + var badFn = function () { + throw new TypeError('Illegal salmon!'); + }; + + expect(badFn).to.throw(); + }); + + describe('docs example 136', () => { + var badFn = function () { + throw new TypeError('Illegal salmon!'); + }; + + expect(badFn).to.throw(TypeError); + }); + + describe('docs example 137', () => { + var err = new TypeError('Illegal salmon!'); + var badFn = function () { + throw err; + }; + + expect(badFn).to.throw(err); + }); + + describe('docs example 138', () => { + var badFn = function () { + throw new TypeError('Illegal salmon!'); + }; + + expect(badFn).to.throw('salmon'); + }); + + describe('docs example 139', () => { + var badFn = function () { + throw new TypeError('Illegal salmon!'); + }; + + expect(badFn).to.throw(/salmon/); + }); + + describe('docs example 140', () => { + var err = new TypeError('Illegal salmon!'); + var badFn = function () { + throw err; + }; + + expect(badFn).to.throw(TypeError, 'salmon'); + expect(badFn).to.throw(TypeError, /salmon/); + expect(badFn).to.throw(err, 'salmon'); + expect(badFn).to.throw(err, /salmon/); + }); + + describe('docs example 141', () => { + var goodFn = function () {}; + + expect(goodFn).to.not.throw(); + }); + + describe('docs example 142', () => { + var goodFn = function () {}; + + expect(goodFn).to.not.throw(); // Recommended + expect(goodFn).to.not.throw(ReferenceError, 'x'); // Not recommended + }); + + describe('docs example 143', () => { + var badFn = function () { + throw new TypeError('Illegal salmon!'); + }; + + expect(badFn).to.throw(TypeError, 'salmon'); // Recommended + expect(badFn).to.not.throw(ReferenceError, 'x'); // Not recommended + }); + + describe('docs example 144', () => { + var err = new TypeError('Illegal salmon!'); + err.code = 42; + var badFn = function () { + throw err; + }; + + expect(badFn).to.throw(TypeError).with.property('code', 42); + }); + + describe('docs example 145', () => { + var goodFn = function () {}; + + expect(goodFn).to.throw(TypeError, 'x', 'nooo why fail??'); + expect(goodFn, 'nooo why fail??').to.throw(); + }); + + describe('docs example 146', () => { + var fn = function () { + throw new TypeError('Illegal salmon!'); + }; + + expect(fn).to.throw(); // Good! Tests `fn` as desired + expect(fn()).to.throw(); // Bad! Tests result of `fn()`, not `fn` + }); + + describe('docs example 147', () => { + expect(function () { + fn(42); + }).to.throw(); // Function expression + expect(() => fn(42)).to.throw(); // ES6 arrow function + }); + + describe('docs example 148', () => { + expect(function () { + cat.meow(); + }).to.throw(); // Function expression + expect(() => cat.meow()).to.throw(); // ES6 arrow function + expect(cat.meow.bind(cat)).to.throw(); // Bind + }); + + describe('docs example 149', () => { + function Cat() {} + Cat.prototype.meow = function () {}; + + expect(new Cat()).to.respondTo('meow'); + }); + + describe('docs example 150', () => { + function Cat() {} + Cat.prototype.meow = function () {}; + + expect(Cat).to.respondTo('meow'); + }); + + describe('docs example 151', () => { + function Cat() {} + Cat.prototype.meow = function () {}; + Cat.hiss = function () {}; + + expect(Cat).itself.to.respondTo('hiss').but.not.respondTo('meow'); + }); + + describe('docs example 152', () => { + function Cat() {} + Cat.prototype.meow = function () {}; + + expect(new Cat()).to.be.an('object').that.respondsTo('meow'); + }); + + describe('docs example 153', () => { + function Dog() {} + Dog.prototype.bark = function () {}; + + expect(new Dog()).to.not.respondTo('meow'); + }); + + describe('docs example 154', () => { + expect({}).to.respondTo('meow', 'nooo why fail??'); + expect({}, 'nooo why fail??').to.respondTo('meow'); + }); + + describe('docs example 155', () => { + function Cat() {} + Cat.prototype.meow = function () {}; + Cat.hiss = function () {}; + + expect(Cat).itself.to.respondTo('hiss').but.not.respondTo('meow'); + }); + + describe('docs example 156', () => { + expect(1).to.satisfy(function (num) { + return num > 0; + }); + }); + + describe('docs example 157', () => { + expect(1).to.not.satisfy(function (num) { + return num > 2; + }); + }); + + describe('docs example 158', () => { + expect(1).to.satisfy(function (num) { + return num > 2; + }, 'nooo why fail??'); + + expect(1, 'nooo why fail??').to.satisfy(function (num) { + return num > 2; + }); + }); + + describe('docs example 159', () => { + // Recommended + expect(1.5).to.equal(1.5); + + // Not recommended + expect(1.5).to.be.closeTo(1, 0.5); + expect(1.5).to.be.closeTo(2, 0.5); + expect(1.5).to.be.closeTo(1, 1); + }); + + describe('docs example 160', () => { + expect(1.5).to.equal(1.5); // Recommended + expect(1.5).to.not.be.closeTo(3, 1); // Not recommended + }); + + describe('docs example 161', () => { + expect(1.5).to.be.closeTo(3, 1, 'nooo why fail??'); + expect(1.5, 'nooo why fail??').to.be.closeTo(3, 1); + }); + + describe('docs example 162', () => { + expect([1, 2, 3]).to.have.members([2, 1, 3]); + expect([1, 2, 2]).to.have.members([2, 1, 2]); + }); + + describe('docs example 163', () => { + // Target array deeply (but not strictly) has member `{a: 1}` + expect([{ a: 1 }]).to.have.deep.members([{ a: 1 }]); + expect([{ a: 1 }]).to.not.have.members([{ a: 1 }]); + }); + + describe('docs example 164', () => { + expect([1, 2, 3]).to.have.ordered.members([1, 2, 3]); + expect([1, 2, 3]).to.have.members([2, 1, 3]).but.not.ordered.members([2, 1, 3]); + }); + + describe('docs example 165', () => { + // Target array is a superset of [1, 2] but not identical + expect([1, 2, 3]).to.include.members([1, 2]); + expect([1, 2, 3]).to.not.have.members([1, 2]); + + // Duplicates in the subset are ignored + expect([1, 2, 3]).to.include.members([1, 2, 2, 2]); + }); + + describe('docs example 166', () => { + expect([{ a: 1 }, { b: 2 }, { c: 3 }]) + .to.include.deep.ordered.members([{ a: 1 }, { b: 2 }]) + .but.not.include.deep.ordered.members([{ b: 2 }, { c: 3 }]); + }); + + describe('docs example 167', () => { + expect([1, 2]).to.not.include(3).and.not.include(4); // Recommended + expect([1, 2]).to.not.have.members([3, 4]); // Not recommended + }); + + describe('docs example 168', () => { + expect([1, 2]).to.have.members([1, 2, 3], 'nooo why fail??'); + expect([1, 2], 'nooo why fail??').to.have.members([1, 2, 3]); + }); + + describe('docs example 169', () => { + expect(1).to.equal(1); // Recommended + expect(1).to.be.oneOf([1, 2, 3]); // Not recommended + }); + + describe('docs example 170', () => { + expect(1).to.equal(1); // Recommended + expect(1).to.not.be.oneOf([2, 3, 4]); // Not recommended + }); + + describe('docs example 171', () => { + expect('Today is sunny').to.contain.oneOf(['sunny', 'cloudy']); + expect('Today is rainy').to.not.contain.oneOf(['sunny', 'cloudy']); + expect([1, 2, 3]).to.contain.oneOf([3, 4, 5]); + expect([1, 2, 3]).to.not.contain.oneOf([4, 5, 6]); + }); + + describe('docs example 172', () => { + expect(1).to.be.oneOf([2, 3, 4], 'nooo why fail??'); + expect(1, 'nooo why fail??').to.be.oneOf([2, 3, 4]); + }); + + describe('docs example 173', () => { + var dots = '', + addDot = function () { + dots += '.'; + }, + getDots = function () { + return dots; + }; + + // Recommended + expect(getDots()).to.equal(''); + addDot(); + expect(getDots()).to.equal('.'); + + // Not recommended + expect(addDot).to.change(getDots); + }); + + describe('docs example 174', () => { + var myObj = { dots: '' }, + addDot = function () { + myObj.dots += '.'; + }; + + // Recommended + expect(myObj).to.have.property('dots', ''); + addDot(); + expect(myObj).to.have.property('dots', '.'); + + // Not recommended + expect(addDot).to.change(myObj, 'dots'); + }); + + describe('docs example 175', () => { + var dots = '', + noop = function () {}, + getDots = function () { + return dots; + }; + + expect(noop).to.not.change(getDots); + + var myObj = { dots: '' }, + noop = function () {}; + + expect(noop).to.not.change(myObj, 'dots'); + }); + + describe('docs example 176', () => { + var myObj = { dots: '' }, + addDot = function () { + myObj.dots += '.'; + }; + + expect(addDot).to.not.change(myObj, 'dots', 'nooo why fail??'); + + var dots = '', + addDot = function () { + dots += '.'; + }, + getDots = function () { + return dots; + }; + + expect(addDot, 'nooo why fail??').to.not.change(getDots); + }); + + describe('docs example 177', () => { + var myObj = { val: 1 }, + addTwo = function () { + myObj.val += 2; + }, + subtractTwo = function () { + myObj.val -= 2; + }; + + expect(addTwo).to.increase(myObj, 'val').by(2); // Recommended + expect(addTwo).to.change(myObj, 'val').by(2); // Not recommended + + expect(subtractTwo).to.decrease(myObj, 'val').by(2); // Recommended + expect(subtractTwo).to.change(myObj, 'val').by(2); // Not recommended + }); + + describe('docs example 178', () => { + var val = 1, + addTwo = function () { + val += 2; + }, + getVal = function () { + return val; + }; + + expect(addTwo).to.increase(getVal).by(2); // Recommended + expect(addTwo).to.increase(getVal); // Not recommended + }); + + describe('docs example 179', () => { + var myObj = { val: 1 }, + addTwo = function () { + myObj.val += 2; + }; + + expect(addTwo).to.increase(myObj, 'val').by(2); // Recommended + expect(addTwo).to.increase(myObj, 'val'); // Not recommended + }); + + describe('docs example 180', () => { + var myObj = { val: 1 }, + subtractTwo = function () { + myObj.val -= 2; + }; + + expect(subtractTwo).to.decrease(myObj, 'val').by(2); // Recommended + expect(subtractTwo).to.not.increase(myObj, 'val'); // Not recommended + }); + + describe('docs example 181', () => { + var myObj = { val: 1 }, + noop = function () {}; + + expect(noop).to.not.change(myObj, 'val'); // Recommended + expect(noop).to.not.increase(myObj, 'val'); // Not recommended + }); + + describe('docs example 182', () => { + var myObj = { val: 1 }, + noop = function () {}; + + expect(noop).to.increase(myObj, 'val', 'nooo why fail??'); + + var val = 1, + noop = function () {}, + getVal = function () { + return val; + }; + + expect(noop, 'nooo why fail??').to.increase(getVal); + }); + + describe('docs example 183', () => { + var val = 1, + subtractTwo = function () { + val -= 2; + }, + getVal = function () { + return val; + }; + + expect(subtractTwo).to.decrease(getVal).by(2); // Recommended + expect(subtractTwo).to.decrease(getVal); // Not recommended + }); + + describe('docs example 184', () => { + var myObj = { val: 1 }, + subtractTwo = function () { + myObj.val -= 2; + }; + + expect(subtractTwo).to.decrease(myObj, 'val').by(2); // Recommended + expect(subtractTwo).to.decrease(myObj, 'val'); // Not recommended + }); + + describe('docs example 185', () => { + var myObj = { val: 1 }, + addTwo = function () { + myObj.val += 2; + }; + + expect(addTwo).to.increase(myObj, 'val').by(2); // Recommended + expect(addTwo).to.not.decrease(myObj, 'val'); // Not recommended + }); + + describe('docs example 186', () => { + var myObj = { val: 1 }, + noop = function () {}; + + expect(noop).to.not.change(myObj, 'val'); // Recommended + expect(noop).to.not.decrease(myObj, 'val'); // Not recommended + }); + + describe('docs example 187', () => { + var myObj = { val: 1 }, + noop = function () {}; + + expect(noop).to.decrease(myObj, 'val', 'nooo why fail??'); + + var val = 1, + noop = function () {}, + getVal = function () { + return val; + }; + + expect(noop, 'nooo why fail??').to.decrease(getVal); + }); + + describe('docs example 188', () => { + var myObj = { val: 1 }, + addTwo = function () { + myObj.val += 2; + }; + + expect(addTwo).to.increase(myObj, 'val').by(2); + }); + + describe('docs example 189', () => { + var myObj = { val: 1 }, + subtractTwo = function () { + myObj.val -= 2; + }; + + expect(subtractTwo).to.decrease(myObj, 'val').by(2); + }); + + describe('docs example 190', () => { + var myObj = { val: 1 }, + addTwo = function () { + myObj.val += 2; + }, + subtractTwo = function () { + myObj.val -= 2; + }; + + expect(addTwo).to.increase(myObj, 'val').by(2); // Recommended + expect(addTwo).to.change(myObj, 'val').by(2); // Not recommended + + expect(subtractTwo).to.decrease(myObj, 'val').by(2); // Recommended + expect(subtractTwo).to.change(myObj, 'val').by(2); // Not recommended + }); + + describe('docs example 191', () => { + var myObj = { val: 1 }, + addTwo = function () { + myObj.val += 2; + }; + + // Recommended + expect(addTwo).to.increase(myObj, 'val').by(2); + + // Not recommended + expect(addTwo).to.increase(myObj, 'val').but.not.by(3); + }); + + describe('docs example 192', () => { + var myObj = { val: 1 }, + addTwo = function () { + myObj.val += 2; + }; + + expect(addTwo).to.increase(myObj, 'val').by(3, 'nooo why fail??'); + expect(addTwo, 'nooo why fail??').to.increase(myObj, 'val').by(3); + }); + + describe('docs example 193', () => { + expect({ a: 1 }).to.be.extensible; + }); + + describe('docs example 194', () => { + var nonExtensibleObject = Object.preventExtensions({}), + sealedObject = Object.seal({}), + frozenObject = Object.freeze({}); + + expect(nonExtensibleObject).to.not.be.extensible; + expect(sealedObject).to.not.be.extensible; + expect(frozenObject).to.not.be.extensible; + expect(1).to.not.be.extensible; + }); + + describe('docs example 195', () => { + expect(1, 'nooo why fail??').to.be.extensible; + }); + + describe('docs example 196', () => { + var sealedObject = Object.seal({}); + var frozenObject = Object.freeze({}); + + expect(sealedObject).to.be.sealed; + expect(frozenObject).to.be.sealed; + expect(1).to.be.sealed; + }); + + describe('docs example 197', () => { + expect({ a: 1 }).to.not.be.sealed; + }); + + describe('docs example 198', () => { + expect({ a: 1 }, 'nooo why fail??').to.be.sealed; + }); + + describe('docs example 199', () => { + var frozenObject = Object.freeze({}); + + expect(frozenObject).to.be.frozen; + expect(1).to.be.frozen; + }); + + describe('docs example 200', () => { + expect({ a: 1 }).to.not.be.frozen; + }); + + describe('docs example 201', () => { + expect({ a: 1 }, 'nooo why fail??').to.be.frozen; + }); + + describe('docs example 202', () => { + expect(1).to.be.finite; + }); + + describe('docs example 203', () => { + expect('foo').to.be.a('string'); // Recommended + expect('foo').to.not.be.finite; // Not recommended + }); + + describe('docs example 204', () => { + expect(NaN).to.be.NaN; // Recommended + expect(NaN).to.not.be.finite; // Not recommended + }); + + describe('docs example 205', () => { + expect(Infinity).to.equal(Infinity); // Recommended + expect(Infinity).to.not.be.finite; // Not recommended + }); + + describe('docs example 206', () => { + expect(-Infinity).to.equal(-Infinity); // Recommended + expect(-Infinity).to.not.be.finite; // Not recommended + }); + + describe('docs example 207', () => { + expect('foo', 'nooo why fail??').to.be.finite; + }); + + describe('docs example 208', () => { + expect.fail(); + expect.fail('custom error message'); + expect.fail(1, 2); + expect.fail(1, 2, 'custom error message'); + expect.fail(1, 2, 'custom error message', '>'); + expect.fail(1, 2, undefined, '>'); + }); + + // describe('docs example 209', () => { + // should.fail(); + // should.fail("custom error message"); + // should.fail(1, 2); + // should.fail(1, 2, "custom error message"); + // should.fail(1, 2, "custom error message", ">"); + // should.fail(1, 2, undefined, ">"); + + // }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/examples/generating-uuids.md b/docs/sources/k6/v0.55.x/examples/generating-uuids.md new file mode 100644 index 000000000..9c736c9de --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/generating-uuids.md @@ -0,0 +1,82 @@ +--- +title: 'Generating UUIDs' +description: 'Scripting example on how to generate UUIDs in your load test.' +weight: 11 +--- + +# Generating UUIDs + +If you want to make a version 4 UUID, +you can use the [`uuidv4` function](https://grafana.com/docs/k6//javascript-api/jslib/utils/uuidv4) from the [k6 JS lib repository](https://jslib.k6.io/). + +{{< code >}} + +```javascript +import { uuidv4 } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js'; + +export default function () { + const randomUUID = uuidv4(); + console.log(randomUUID); // 35acae14-f7cb-468a-9866-1fc45713149a +} +``` + +{{< /code >}} + +If you really need other UUID versions, you must rely on an external library. + +## Generate v1 UUIDs + +As k6 doesn't have built-in support +for version 1 UUID, you'll have to use a third-party library. + +This example uses a Node.js library called [uuid](https://www.npmjs.com/package/uuid) +and [Browserify](http://browserify.org/) (to make it work in k6). +For this to work, we first need to go through a few required steps: + +1. Make sure you have the necessary prerequisites installed: + [Node.js](https://nodejs.org/en/download/) and [Browserify](http://browserify.org/) + +2. Install the `uuid` library: + {{< code >}} + + ```bash + $ npm install uuid@3.4.0 + ``` + + {{< /code >}} + +3. Run it through browserify: + {{< code >}} + + ```bash + $ browserify node_modules/uuid/index.js -s uuid > uuid.js + ``` + + {{< /code >}} + +4. Move the `uuid.js` file to the same folder as your script file. Now you can import + it into your test script: + + {{< code >}} + + ```javascript + import uuid from './uuid.js'; + ``` + + {{< /code >}} + +This example generates a v1 UUID: + +{{< code >}} + +```javascript +import uuid from './uuid.js'; + +export default function () { + // Generate a UUID v1 + const uuid1 = uuid.v1(); + console.log(uuid1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/examples/get-started-with-k6/_index.md b/docs/sources/k6/v0.55.x/examples/get-started-with-k6/_index.md new file mode 100644 index 000000000..168fbb8cb --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/get-started-with-k6/_index.md @@ -0,0 +1,46 @@ +--- +title: 'Get started with k6' +description: A series of docs to learn how to use the major features of k6 +weight: 01 +weight: -10 +--- + +# Get started with k6 + +This tutorial provides some procedures for common real-life uses of k6. +They assume no prior knowledge of k6 or of JavaScript. + +These tasks are all reproducible, so open up your favorite editor and follow along. +Imagine that you are a developer or tester who is responsible for performance in your development team. + +## Context: test a new endpoint + +Your development team has just developed a new login endpoint. +Before releasing, you must test the endpoint for the following questions: + +- Does it work? +- Does it perform within the service-level objectives under average load? +- At what load does performance degrade beyond objectives? + +After you test the endpoint, your team wants you to compare different components of the user-facing application. + +Finally, after you test the API and web application, break your scripts down into reusable parts. + +## Before you start + +We encourage you to open your terminal and actively experiment with these examples. +The tutorial requires the following: + +- [ ][k6 installed](https://grafana.com/docs/k6//set-up/install-k6) +- [ ] A clean directory to experiment in. +- [ ] Something to do during the minute or two when k6 runs the longest example tests +- [ ] Optional: [`jq`](https://stedolan.github.io/jq/) to filter some results + +## Tutorials + +| In this tutorial | Learn how to | +| ---------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------ | +| [Test for functional behavior](https://grafana.com/docs/k6//examples/get-started-with-k6/test-for-functional-behavior) | Use k6 to script requests and evaluate that performance is correct | +| [Test for performance](https://grafana.com/docs/k6//examples/get-started-with-k6/test-for-performance) | Use k6 to increase load and find faults | +| [Analyze results](https://grafana.com/docs/k6//examples/get-started-with-k6/analyze-results) | Filter results and make custom metrics | +| [Reuse and re-run](https://grafana.com/docs/k6//examples/get-started-with-k6/reuse-and-re-run-tests) | Modularize and re-run tests | diff --git a/docs/sources/k6/v0.55.x/examples/get-started-with-k6/analyze-results.md b/docs/sources/k6/v0.55.x/examples/get-started-with-k6/analyze-results.md new file mode 100644 index 000000000..f447af313 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/get-started-with-k6/analyze-results.md @@ -0,0 +1,357 @@ +--- +title: Analyze results +description: Use k6 to write custom metrics and filter results. +weight: 300 +--- + +# Analyze results + +In this tutorial, learn how to: + +- Apply tags to filter specific results +- Learn about k6 metrics +- Use [jq](https://jqlang.github.io/jq/) to filter JSON results +- Define groups to organize the test +- Create custom metrics + +## Context: k6 result outputs + +k6 provides many [result outputs](https://grafana.com/docs/k6//results-output/). +By default, the [end-of-test summary](https://grafana.com/docs/k6//results-output/end-of-test) provides the aggregated results of the test metrics. + +{{< code >}} + +```bash +checks.........................: 50.00% ✓ 45 ✗ 45 +data_received..................: 1.3 MB 31 kB/s +data_sent......................: 81 kB 2.0 kB/s +group_duration.................: avg=6.45s min=4.01s med=6.78s max=10.15s p(90)=9.29s p(95)=9.32s +http_req_blocked...............: avg=57.62ms min=7µs med=12.25µs max=1.35s p(90)=209.41ms p(95)=763.61ms +http_req_connecting............: avg=20.51ms min=0s med=0s max=1.1s p(90)=100.76ms p(95)=173.41ms +http_req_duration..............: avg=144.56ms min=104.11ms med=110.47ms max=1.14s p(90)=203.54ms p(95)=215.95ms + { expected_response:true }...: avg=144.56ms min=104.11ms med=110.47ms max=1.14s p(90)=203.54ms p(95)=215.95ms +http_req_failed................: 0.00% ✓ 0 ✗ 180 +http_req_receiving.............: avg=663.96µs min=128.46µs med=759.82µs max=1.66ms p(90)=1.3ms p(95)=1.46ms +http_req_sending...............: avg=88.01µs min=43.07µs med=78.03µs max=318.81µs p(90)=133.15µs p(95)=158.3µs +http_req_tls_handshaking.......: avg=29.25ms min=0s med=0s max=458.71ms p(90)=108.31ms p(95)=222.46ms +http_req_waiting...............: avg=143.8ms min=103.5ms med=109.5ms max=1.14s p(90)=203.19ms p(95)=215.56ms +http_reqs......................: 180 4.36938/s +iteration_duration.............: avg=12.91s min=12.53s med=12.77s max=14.35s p(90)=13.36s p(95)=13.37s +iterations.....................: 45 1.092345/s +vus............................: 1 min=1 max=19 +vus_max........................: 20 min=20 max=20 +``` + +{{< /code >}} + +For simplicity to learn about [k6 metric results](https://grafana.com/docs/k6//using-k6/metrics/reference), this tutorial uses the [JSON output](https://grafana.com/docs/k6//results-output/real-time/json) and [jq](https://jqlang.github.io/jq/) to filter results. + +For other options to analyze test results such as storage and time-series visualizations in real-time, refer to: + +- [Results output](https://grafana.com/docs/k6//results-output/) + +- [Ways to visualize k6 results](https://k6.io/blog/ways-to-visualize-k6-results/) + +## Write time-series results to a JSON file + +To output results to a JSON file, use the `--out` flag. + +```bash +k6 run --out json=results.json api-test.js +``` + +Then run this `jq` command to filter the latency results; `http_req_duration` metric. + +```bash +jq '. | select(.type == "Point" and .metric == "http_req_duration")' results.json +``` + +k6 results have a number of [built-in tags](https://grafana.com/docs/k6//using-k6/tags-and-groups#system-tags). For example, filter results to only results where the status is 200. + +```bash +jq '. | select(.type == "Point" and .data.tags.status == "200")' results.json +``` + +Or calculate the aggregated value of any metric with any particular tags. + +{{< code >}} + +```average +jq '. | select(.type == "Point" and .metric == "http_req_duration") | .data.value' results.json | jq -s 'add/length' +``` + +```min +jq '. | select(.type == "Point" and .metric == "http_req_duration") | .data.value' results.json | jq -s min +``` + +```max +jq '. | select(.type == "Point" and .metric == "http_req_duration") | .data.value' results.json | jq -s max +``` + +{{< /code >}} + +## Apply custom tags + +You can also apply [_Tags_](https://grafana.com/docs/k6//using-k6/tags-and-groups#tags) to requests or code blocks. For example, this is how you can add a [`tags`](https://grafana.com/docs/k6//using-k6/tags-and-groups#tags) to the [request params](https://grafana.com/docs/k6//javascript-api/k6-http/params). + +```javascript +const params = { + headers: { + 'Content-Type': 'application/json', + }, + tags: { + 'my-custom-tag': 'auth-api', + }, +}; +``` + +Create a new script named "tagged-login.js", and add a custom tag to it. + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + const url = 'https://test-api.k6.io'; + const payload = JSON.stringify({ + username: 'test_case', + password: '1234', + }); + + const params = { + headers: { + 'Content-Type': 'application/json', + }, + //apply tags + tags: { + 'my-custom-tag': 'auth-api', + }, + }; + + //Login with tags + http.post(`${url}/auth/basic/login`, payload, params); +} +``` + +{{< /code >}} + +Run the test: + +```bash +k6 run --out json=results.json tagged-login.js +``` + +Filter the results for this custom tag: + +```bash +jq '. | select(.type == "Point" and .metric == "http_req_duration" and .data.tags."my-custom-tag" == "auth-api")' results.json +``` + +## Organize requests in groups + +You can also organize your test logic into [Groups](https://grafana.com/docs/k6//using-k6/tags-and-groups#groups). Test logic inside a `group` tags all requests and metrics within its block. +Groups can help you organize the test as a series of logical transactions or blocks. + +### Context: a new test to group test logic + +Results filtering isn't very meaningful in a test that makes one request. +And the API test script is getting long. +To learn more about how to compare results and other k6 APIs, write a test for the following situation: + +> **A dummy example**: your development team wants to evaluate the performance of two user-facing flows. +> +> - visit an endpoint, then another one +> - A GET request to `https://test.k6.io/contacts.php` +> - A GET to `https://test.k6.io/` +> - play the coinflip game: +> - A POST request to `https://test.k6.io/flip_coin.php` with the query param `bet=heads` +> - Another POST to `https://test.k6.io/flip_coin.php` with the query param `bet=tails` + +Can you figure out how to [script the requests](https://grafana.com/docs/k6//using-k6/http-requests)? +If not, use the following script. Since this example simulates a human user rather than an API call, it has a sleep between each request. Run with `k6 run multiple-flows.js`. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { group, sleep } from 'k6'; + +//set URL as variable +const baseUrl = 'https://test.k6.io'; + +export default function () { + // visit contacts + http.get(`${baseUrl}/contacts.php`); + sleep(1); + // return to the home page + http.get(`${baseUrl}/`); + sleep(1); + + //play coinflip game + http.get(`${baseUrl}/flip_coin.php?bet=heads`); + sleep(1); + http.get(`${baseUrl}/flip_coin.php?bet=tails`); + sleep(1); +} +``` + +{{< /code >}} + +### Add Group functions + +Wrap the two endpoints in different groups. +Name one group `Contacts flow` and another `Coinflip game`. + +{{< code >}} + +```javascript +//import necessary modules +import http from 'k6/http'; +import { group, sleep } from 'k6'; + +//set baseURL +const baseUrl = 'https://test.k6.io'; + +export default function () { + // visit some endpoints in one group + group('Contacts flow', function () { + http.get(`${baseUrl}/contacts.php`); + sleep(1); + // return to the home page + http.get(`${baseUrl}/`); + sleep(1); + }); + + // Coinflip players in another group + group('Coinflip game', function () { + http.get(`${baseUrl}/flip_coin.php?bet=heads`); + sleep(1); + http.get(`${baseUrl}/flip_coin.php?bet=tails`); + sleep(1); + }); +} +``` + +{{< /code >}} + +### Run and filter + +Inspect the results for only the `Coinflip game` group. +To do so: + +1. Save the preceding script as `multiple-flows.js`. +1. Run the script with the command: + +```bash +k6 run multiple-flows.js --out json=results.json --iterations 10 +``` + +1. Inspect the results with `jq`. Group names have a `::` prefix. + +```bash +jq '. | select(.data.tags.group == "::Coinflip game")' results.json +``` + +## Add a custom metric + +As you have seen in the output, all k6 tests emit metrics. +However, if the built-in metrics aren't enough, you can [create custom metrics](https://grafana.com/docs/k6//using-k6/metrics/create-custom-metrics). +A common use case is to collect metrics of a particular scope of your test. + +As an example, create a metric that collects latency results for each group: + +1. Import [`Trend`](https://grafana.com/docs/k6//javascript-api/k6-metrics/trend) from the k6 metrics module. +1. Create two duration trend metric functions. +1. In each group, add the `duration` time to the trend for requests to `contacts` and the `coin_flip` endpoints. + +{{< code >}} + +```javascript +//import necessary modules +import http from 'k6/http'; +import { group, sleep } from 'k6'; +import { Trend } from 'k6/metrics'; + +//set baseURL +const baseUrl = 'https://test.k6.io'; + +// Create custom trend metrics +const contactsLatency = new Trend('contacts_duration'); +const coinflipLatency = new Trend('coinflip_duration'); + +export default function () { + // Put visits to contact page in one group + let res; + group('Contacts flow', function () { + // save response as variable + res = http.get(`${baseUrl}/contacts.php`); + // add duration property to metric + contactsLatency.add(res.timings.duration); + sleep(1); + + res = http.get(`${baseUrl}/`); + // add duration property to metric + contactsLatency.add(res.timings.duration); + sleep(1); + }); + + // Coinflip players in another group + + group('Coinflip game', function () { + // save response as variable + let res = http.get(`${baseUrl}/flip_coin.php?bet=heads`); + // add duration property to metric + coinflipLatency.add(res.timings.duration); + sleep(1); + + res = http.get(`${baseUrl}/flip_coin.php?bet=tails`); + // add duration property to metric + coinflipLatency.add(res.timings.duration); + sleep(1); + }); +} +``` + +{{< /code >}} + +Run the test with small number of iterations and output the results to `results.json`. + +```bash +k6 run multiple-flows.js --out json=results.json --iterations 10 +``` + +Look for the custom trend metrics in the end-of-test console summary. + +```bash +coinflip_duration..............: avg=119.6438 min=116.481 med=118.4755 max=135.498 p(90)=121.8459 p(95)=123.89565 +contacts_duration..............: avg=125.76985 min=116.973 med=120.6735 max=200.507 p(90)=127.9271 p(95)=153.87245 +``` + +You can also query custom metric results from the JSON results. For example, to get the aggregated results as. + +{{< code >}} + +```avg +jq '. | select(.type == "Point" and .metric == "coinflip_duration") | .data.value' results.json | jq -s 'add/length' +``` + +```min +jq '. | select(.type == "Point" and .metric == "coinflip_duration") | .data.value' results.json | jq -s min +``` + +```max +jq '. | select(.type == "Point" and .metric == "coinflip_duration") | .data.value' results.json | jq -s max +``` + +{{< /code >}} + +## Next steps + +In this tutorial, you looked at granular output and filtered by built-in and custom tags. +Then you made a new script with groups. +Finally, you added a new metric for each group. +A next step would be to create a [Custom end-of-test summary](https://grafana.com/docs/k6//results-output/end-of-test/custom-summary) or to [stream the results to a database](https://grafana.com/docs/k6//results-output/real-time#service). + +For ongoing operations, you can modularize your logic and configuration. +That's the subject of the [next step of this tutorial](https://grafana.com/docs/k6//examples/get-started-with-k6/reuse-and-re-run-tests). diff --git a/docs/sources/k6/v0.55.x/examples/get-started-with-k6/reuse-and-re-run-tests.md b/docs/sources/k6/v0.55.x/examples/get-started-with-k6/reuse-and-re-run-tests.md new file mode 100644 index 000000000..6da4765b7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/get-started-with-k6/reuse-and-re-run-tests.md @@ -0,0 +1,416 @@ +--- +title: Reuse and re-run tests +description: Modularize your k6 test logic and workload configuration. +weight: 400 +--- + +# Reuse and re-run tests + +In the previous tutorials, you designed k6 scripts to assert performance and make comparing results easy. + +In this tutorial, learn how to: + +- Modularize test scripts into reusable components +- Dynamically configure scripts with environment variables + +## Example script + +For fun, let's combine the scripts from the previous tutorials. +Use the logic of the `multiple-flows.js` test with the thresholds and scenario of the `api-test.js` (feel free to add more checks, requests, groups, and so on). +Take note of the features of this script: + +- The `default` function has two groups, `Contacts flow`, and `Coinflip game` +- The `options` object has two properties, `thresholds` and `scenarios` + +In the following sections, learn how to split these components into separate files, and combine them dynamically at run time. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { group, sleep } from 'k6'; +import { Trend } from 'k6/metrics'; + +//define configuration +export const options = { + scenarios: { + //arbitrary name of scenario: + breaking: { + executor: 'ramping-vus', + stages: [ + { duration: '10s', target: 20 }, + { duration: '50s', target: 20 }, + { duration: '50s', target: 40 }, + { duration: '50s', target: 60 }, + { duration: '50s', target: 80 }, + { duration: '50s', target: 100 }, + { duration: '50s', target: 120 }, + { duration: '50s', target: 140 }, + //.... + ], + }, + }, + //define thresholds + thresholds: { + http_req_failed: [{ threshold: 'rate<0.01', abortOnFail: true }], // availability threshold for error rate + http_req_duration: ['p(99)<1000'], // Latency threshold for percentile + }, +}; + +//set baseURL +const baseUrl = 'https://test.k6.io'; + +// Create custom trends +const contactsLatency = new Trend('contacts_duration'); +const coinflipLatency = new Trend('coinflip_duration'); + +export default function () { + // Put visits to contact page in one group + let res; + group('Contacts flow', function () { + // save response as variable + res = http.get(`${baseUrl}/contacts.php`); + // add duration property to metric + contactsLatency.add(res.timings.duration); + sleep(1); + + res = http.get(`${baseUrl}/`); + // add duration property to metric + contactsLatency.add(res.timings.duration); + sleep(1); + }); + + // Coinflip players in another group + + group('Coinflip game', function () { + // save response as variable + let res = http.get(`${baseUrl}/flip_coin.php?bet=heads`); + // add duration property to metric + coinflipLatency.add(res.timings.duration); + sleep(1); + + res = http.get(`${baseUrl}/flip_coin.php?bet=tails`); + // add duration property to metric + coinflipLatency.add(res.timings.duration); + sleep(1); + }); +} +``` + +{{< /code >}} + +## Modularize logic + +With [modules](https://grafana.com/docs/k6//using-k6/modules), you can use logic and variables from other files. +Use modules to extract the functions to their own files. + +To do so, follow these steps: + +1. Copy the previous script (`whole-tutorial.js`) and save it as `main.js`. +1. Extract the `Contacts flow` group function from `main.js` script file and paste it into a new file called `contacts.js` + + {{< code >}} + + + + ```javascript + export function contacts() { + group('Contacts flow', function () { + // save response as variable + let res = http.get(`${baseUrl}/contacts.php`); + // add duration property to metric + contactsLatency.add(res.timings.duration); + sleep(1); + + res = http.get(`${baseUrl}/`); + // add duration property to metric + contactsLatency.add(res.timings.duration); + sleep(1); + }); + } + ``` + + + + {{< /code >}} + + As is, this script won't work, since it has undeclared functions and variables. + +1. Add the necessary imports and variables. This script uses the `group`, `sleep`, and `http` functions or libraries. It also has a custom metric. Since this metric is specific to the group, you can add it `contacts.js`. + +1. Finally, pass `baseUrl` as a parameter of the `contacts` function. + + {{< code >}} + + ```javascript + import http from 'k6/http'; + import { Trend } from 'k6/metrics'; + import { group, sleep } from 'k6'; + + const contactsLatency = new Trend('contact_duration'); + + export function contacts(baseUrl) { + group('Contacts flow', function () { + // save response as variable + let res = http.get(`${baseUrl}/contacts.php`); + // add duration property to metric + contactsLatency.add(res.timings.duration); + sleep(1); + + res = http.get(`${baseUrl}/`); + // add duration property to metric + contactsLatency.add(res.timings.duration); + sleep(1); + }); + } + ``` + + {{< /code >}} + +1. Repeat the process with the `coinflip` group in a file called `coinflip.js`. + Use the tabs to see the final three files should (`options` moved to the bottom of `main.js` for better readability). + + {{< code >}} + + ```main + import { contacts } from './contacts.js'; + import { coinflip } from './coinflip.js'; + + const baseUrl = 'https://test.k6.io'; + + export default function () { + // Put visits to contact page in one group + contacts(baseUrl); + // Coinflip players in another group + coinflip(baseUrl); + } + + //define configuration + export const options = { + scenarios: { + //arbitrary name of scenario: + breaking: { + executor: 'ramping-vus', + stages: [ + { duration: '10s', target: 20 }, + { duration: '50s', target: 20 }, + { duration: '50s', target: 40 }, + { duration: '50s', target: 60 }, + { duration: '50s', target: 80 }, + { duration: '50s', target: 100 }, + { duration: '50s', target: 120 }, + { duration: '50s', target: 140 }, + //.... + ], + }, + }, + //define thresholds + thresholds: { + http_req_failed: [{ threshold: 'rate<0.01', abortOnFail: true }], // availability threshold for error rate + http_req_duration: ['p(99)<1000'], // Latency threshold for percentile + }, + }; + ``` + + ```contacts + import http from 'k6/http'; + import { Trend } from 'k6/metrics'; + import { group, sleep } from 'k6'; + + const contactsLatency = new Trend('contact_duration'); + + export function contacts(baseUrl) { + group('Contacts flow', function () { + // save response as variable + let res = http.get(`${baseUrl}/contacts.php`); + // add duration property to metric + contactsLatency.add(res.timings.duration); + sleep(1); + + res = http.get(`${baseUrl}/`); + // add duration property to metric + contactsLatency.add(res.timings.duration); + sleep(1); + }); + } + ``` + + ```coinflip + import http from 'k6/http'; + import { Trend } from 'k6/metrics'; + import { group, sleep } from 'k6'; + + const coinflipLatency = new Trend('coinflip_duration'); + + export function coinflip(baseUrl) { + group('Coinflip game', function () { + // save response as variable + let res = http.get(`${baseUrl}/flip_coin.php?bet=heads`); + // add duration property to metric + coinflipLatency.add(res.timings.duration); + sleep(1); + // mutate for new request + res = http.get(`${baseUrl}/flip_coin.php?bet=tails`); + // add duration property to metric + coinflipLatency.add(res.timings.duration); + sleep(1); + }); + } + ``` + + {{< /code >}} + + Run the test: + + ```bash + # setting the workload to 10 iterations to limit run time + k6 run main.js --iterations 10 + ``` + + The results should be very similar to running the script in a combined file, since these are the same test. + +## Modularize workload + +Now that the iteration code is totally modularized, you might modularize your `options`, too. + +The following example creates a module `config.js` to export the threshold and workload settings. + +{{< code >}} + +```main +import { coinflip } from './coinflip.js'; +import { contacts } from './contacts.js'; +import { thresholdsSettings, breakingWorkload } from './config.js'; + +export const options = { + scenarios: { breaking: breakingWorkload }, + thresholds: thresholdsSettings, +}; + +const baseUrl = 'https://test.k6.io'; + +export default function () { + contacts(baseUrl); + coinflip(baseUrl); +} +``` + +```config +export const thresholdsSettings = { + http_req_failed: [{ threshold: 'rate<0.01', abortOnFail: true }], + http_req_duration: ['p(99)<1000'], +}; + +export const breakingWorkload = { + executor: 'ramping-vus', + stages: [ + { duration: '10s', target: 20 }, + { duration: '50s', target: 20 }, + { duration: '50s', target: 40 }, + { duration: '50s', target: 60 }, + { duration: '50s', target: 80 }, + { duration: '50s', target: 100 }, + { duration: '50s', target: 120 }, + { duration: '50s', target: 140 }, + //.... + ], +}; +``` + +{{< /code >}} + +Notice the length of this final script and compare it with the script at the beginning of this page. +Though the final execution is the same, it's half the size and more readable. + +Besides shortness, this modularity lets you compose scripts from many parts, or dynamically configure scripts at run time. + +## Mix and match logic + +With modularized configuration and logic, you can mix and match logic. +An easy way to configure this is through [environment variables](https://grafana.com/docs/k6//using-k6/environment-variables). + +Change `main.js` and `config.js` so that it: + +- _By default_ runs a smoke test with 5 iterations +- With the right environment variable value, runs a breaking test + +To do this, follow these steps: + +1. Add the workload settings for configuring the smoke test to `config.js`: + + {{< code >}} + + ```javascript + export const smokeWorkload = { + executor: 'shared-iterations', + iterations: 5, + vus: 1, + }; + + export const thresholdsSettings = { + http_req_failed: [{ threshold: 'rate<0.01', abortOnFail: true }], + http_req_duration: ['p(99)<1000'], + }; + + export const breakingWorkload = { + executor: 'ramping-vus', + stages: [ + { duration: '10s', target: 20 }, + { duration: '50s', target: 20 }, + { duration: '50s', target: 40 }, + { duration: '50s', target: 60 }, + { duration: '50s', target: 80 }, + { duration: '50s', target: 100 }, + { duration: '50s', target: 120 }, + { duration: '50s', target: 140 }, + //.... + ], + }; + ``` + + {{< /code >}} + +1. Edit `main.js` to choose the workload settings depending on the `WORKLOAD` environment variable. For example: + + {{< code >}} + + ```javascript + import { coinflip } from './coinflip.js'; + import { contacts } from './contacts.js'; + import { thresholdsSettings, breakingWorkload, smokeWorkload } from './config.js'; + + export const options = { + scenarios: { + my_scenario: __ENV.WORKLOAD === 'breaking' ? breakingWorkload : smokeWorkload, + }, + thresholds: thresholdsSettings, + }; + + const baseUrl = 'https://test.k6.io'; + + export default function () { + contacts(baseUrl); + coinflip(baseUrl); + } + ``` + + {{< /code >}} + +1. Run the script with and without the `-e` flag. + + - What happens when you run `k6 run main.js`? + - What about `k6 run main.js -e WORKLOAD=breaking`? + +This was a simple example to showcase how you can modularize a test. +As your test suite grows and more people are involved in performance testing, your modularization strategy becomes essential to building and maintaining an efficient testing suite. + +## Next steps + +Now you've seen examples to write tests, assert for performance, filter results, and modularize scripts. +Notice how the tests progress in complexity: from single endpoints to holistic tests, from small to large loads, and from single tests to reusable modules. These progressions are typical in testing, with the next step being to automate. It might be impractical to automate a tutorial, but if you are interested, +read the [Automated performance testing](https://grafana.com/docs/k6//testing-guides/automated-performance-testing) guide. + +More likely, you want to learn more about k6. The [k6-learn repository](https://github.com/grafana/k6-learn) has more details to practice. +Or, you can read and explore the [testing guides](https://grafana.com/docs/k6//testing-guides) and try to build out your testing strategy. + +Happy testing! diff --git a/docs/sources/k6/v0.55.x/examples/get-started-with-k6/test-for-functional-behavior.md b/docs/sources/k6/v0.55.x/examples/get-started-with-k6/test-for-functional-behavior.md new file mode 100644 index 000000000..4fe0ae22b --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/get-started-with-k6/test-for-functional-behavior.md @@ -0,0 +1,159 @@ +--- +title: Test for functional behavior +description: Use k6 to write requests and assert that they respond correctly +weight: 100 +--- + +# Test for functional behavior + +In this tutorial, learn how to write a test that does the following: + +- Sends a POST request to the new endpoint +- Creates a check for the response status + +## Script the Request + +The first thing to do is to add logic for the endpoint. +To do that, you need to make an [HTTP request](https://grafana.com/docs/k6//using-k6/http-requests): + +1. Import the HTTP module. +2. Create a payload to authenticate the user. +3. Use the [`http.post`](https://grafana.com/docs/k6//javascript-api/k6-http/post) method to send your request with the payload to an endpoint. + +To test, copy this file and save it as `api-test.js`. + +{{< code >}} + +```javascript +// import necessary module +import http from 'k6/http'; + +export default function () { + // define URL and payload + const url = 'https://test-api.k6.io/auth/basic/login/'; + const payload = JSON.stringify({ + username: 'test_case', + password: '1234', + }); + + const params = { + headers: { + 'Content-Type': 'application/json', + }, + }; + + // send a post request and save response as a variable + const res = http.post(url, payload, params); +} +``` + +{{< /code >}} + +Run the script using the `k6 run` command: + +```bash +k6 run api-test.js +``` + +After the test finishes, k6 reports the [default result summary](https://grafana.com/docs/k6//results-output/end-of-test#the-default-summary). + +```bash + /\ |‾‾| /‾‾/ /‾‾/ + /\ / \ | |/ / / / + / \/ \ | ( / ‾‾\ + / \ | |\ \ | (‾) | + / __________ \ |__| \__\ \_____/ .io + + execution: local + script: api-test.js + output: - + ... +``` + +As an optional step, you can log the response body to the console to make sure you're getting the right response. + +{{< code >}} + + + +```javascript +export default function () { + // ... + + const res = http.post(url, payload, params); + + // Log the request body + console.log(res.body); +} +``` + + + +{{< /code >}} + +## Add response checks + +Once you're sure the request is well-formed, add a [check](https://grafana.com/docs/k6//using-k6/checks) that validates whether the system responds with the expected status code. + +1. Update your script so it has the following check function. + +{{< code >}} + +```javascript +// Import necessary modules +import { check } from 'k6'; +import http from 'k6/http'; + +export default function () { + // define URL and request body + const url = 'https://test-api.k6.io/auth/basic/login/'; + const payload = JSON.stringify({ + username: 'test_case', + password: '1234', + }); + const params = { + headers: { + 'Content-Type': 'application/json', + }, + }; + + // send a post request and save response as a variable + const res = http.post(url, payload, params); + + // check that response is 200 + check(res, { + 'response code was 200': (res) => res.status == 200, + }); +} +``` + +{{< /code >}} + +1. Run the script again. + +```bash +k6 run api-test.js +``` + +1. Inspect the result output for your check. + It should look something like this. + + ``` + ✓ response code was 200 + ``` + +{{< admonition type="note" >}} + +Under larger loads, this check will fail in some iterations. +**Failed checks do not stop tests.** + +Rather, k6 tracks the success rate and presents it in your [end of test](https://grafana.com/docs/k6//results-output/end-of-test) summary. + +{{< /admonition >}} + +## Next steps + +In this tutorial, you've used k6 to make a POST request and check that it responds with a `200` status. + +However, these tests make only one request, which doesn't say much about how the system will respond under load. +For that, you need to [test under load](https://grafana.com/docs/k6//examples/get-started-with-k6/test-for-performance). diff --git a/docs/sources/k6/v0.55.x/examples/get-started-with-k6/test-for-performance.md b/docs/sources/k6/v0.55.x/examples/get-started-with-k6/test-for-performance.md new file mode 100644 index 000000000..acc56aa80 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/get-started-with-k6/test-for-performance.md @@ -0,0 +1,302 @@ +--- +title: Test for performance +description: Write thresholds to evaluate performance criteria, then increase load to see how the system performs. +weight: 200 +--- + +# Test for performance + +In the previous section, you made a working script to test an endpoint functionality. +The next step is to test how this system responds under load. +This requires setting up a few [`options`](https://grafana.com/docs/k6//using-k6/k6-options) to configure the parts of the test that don't deal with test logic. + +In this tutorial, learn how to: + +- Use [thresholds](https://grafana.com/docs/k6//using-k6/thresholds) to assert for performance criteria +- Configure load increases through [scenarios](https://grafana.com/docs/k6//using-k6/scenarios) + +These examples build on the script from the [previous section](https://grafana.com/docs/k6//examples/get-started-with-k6/test-for-functional-behavior). + +## Context: meet service-level objectives + +To assess the login endpoint's performance, your team may have defined [service level objectives](https://sre.google/sre-book/service-level-objectives/) (SLOs). For example: + +- 99% of requests should be successful +- 99% of requests should have a latency of 1000ms or less + +The service must meet these SLOs under different types of usual traffic. + +## Assert for performance with thresholds + +To codify the SLOs, add [_thresholds_](https://grafana.com/docs/k6//using-k6/thresholds) to test that your system performs to its goal criteria. + +Thresholds are set in the [`options`](https://grafana.com/docs/k6//using-k6/k6-options) object. + +```javascript +export const options = { + // define thresholds + thresholds: { + http_req_failed: ['rate<0.01'], // http errors should be less than 1% + http_req_duration: ['p(99)<1000'], // 99% of requests should be below 1s + }, +}; +``` + +Add this `options` object with thresholds to your script `api-test.js`. + +{{< code >}} + +```javascript +// import necessary modules +import { check } from 'k6'; +import http from 'k6/http'; + +// define configuration +export const options = { + // define thresholds + thresholds: { + http_req_failed: ['rate<0.01'], // http errors should be less than 1% + http_req_duration: ['p(99)<1000'], // 99% of requests should be below 1s + }, +}; + +export default function () { + // define URL and request body + const url = 'https://test-api.k6.io/auth/basic/login/'; + const payload = JSON.stringify({ + username: 'test_case', + password: '1234', + }); + const params = { + headers: { + 'Content-Type': 'application/json', + }, + }; + + // send a post request and save response as a variable + const res = http.post(url, payload, params); + + // check that response is 200 + check(res, { + 'response code was 200': (res) => res.status == 200, + }); +} +``` + +{{< /code >}} + +Run the test. + +```bash +k6 run api-test.js +``` + +Inspect the console output to determine whether performance crossed a threshold. + +``` +✓ http_req_duration..............: avg=66.14ms min=0s med=0s max=198.42ms p(90)=158.73ms p(95)=178.58ms + { expected_response:true }...: avg=198.42ms min=198.42ms med=198.42ms max=198.42ms p(90)=198.42ms p(95)=198.42ms +✗ http_req_failed................: 0.00% ✓ 0 ✗ 1 +``` + +The ✓ and ✗ symbols indicate whether the performance thresholds passed or failed. + +## Test performance under increasing load + +Now your script has logic to simulate user behavior, and assertions for functionality (checks) and performance (thresholds). + +It's time to increase the load to see how it performs. +To increase the load, use the scenarios property. +Scenarios schedule load according to the number of [virtual users (VUs)](https://grafana.com/docs/k6//get-started/running-k6/#add-vus), number of iterations, VUs, or by iteration rate. + +### Run a smoke test + +Start small. Run a [smoke test](https://grafana.com/docs/k6//testing-guides/test-types/smoke-testing 'a small test to confirm the script works properly') to check that your script can handle a minimal load. + +To do so, use the [`--iterations`](https://grafana.com/docs/k6//using-k6/k6-options/reference#iterations) flag with an argument of 10 or fewer. + +```bash +k6 run --iterations 10 api-test.js +``` + +If the service can't receive 10 iterations, the system has some serious performance issues to debug. +Good thing you ran the test early! + +### Run a test against an average load + +Generally, traffic doesn't arrive all at once. +Rather, it gradually increases to a peak load. +To simulate this, testers increase the load in _stages_. + +Add the following `scenario` property to your `options` object and rerun the test. + +```javascript +export const options = { + // define thresholds + thresholds: { + http_req_failed: ['rate<0.01'], // http errors should be less than 1% + http_req_duration: ['p(99)<1000'], // 99% of requests should be below 1s + }, + // define scenarios + scenarios: { + // arbitrary name of scenario + average_load: { + executor: 'ramping-vus', + stages: [ + // ramp up to average load of 20 virtual users + { duration: '10s', target: 20 }, + // maintain load + { duration: '50s', target: 20 }, + // ramp down to zero + { duration: '5s', target: 0 }, + ], + }, + }, +}; +``` + +Since this is a learning environment, the stages are still quite short. +Where the smoke test defined the load in terms of iterations, this configuration uses the [`ramping-vus` executor](https://grafana.com/docs/k6//using-k6/scenarios/executors/ramping-vus) to express load through virtual users and duration. + +Run the test with no command-line flags: + +```bash +k6 run api-test.js +``` + +The load is small, so the server should perform within thresholds. +However, this test server may be under load by many k6 learners, so the results are unpredictable. + +{{< admonition type="note" >}} + +At this point, it'd be nice to have a graphical interface to visualize metrics as they occur. +k6 has many output formats, which can serve as inputs for many visualization tools, both open source and commercial. +For ideas, read [Ways to visualize k6 results](https://k6.io/blog/ways-to-visualize-k6-results). + +{{< /admonition >}} + +### Ramp up until threshold fails + +Finally, run a [breakpoint test](https://grafana.com/docs/k6//testing-guides/test-types/breakpoint-testing), where you probe the system's limits. +In this case, run the test until the availability (error rate) threshold is crossed. + +To do this: + +1. Add the `abortOnFail` property to `http_req_failed`. + +```javascript +export const options = { + // define thresholds + thresholds: { + http_req_failed: [{ threshold: 'rate<0.01', abortOnFail: true }], // http errors should be less than 1%, otherwise abort the test + http_req_duration: ['p(99)<1000'], // 99% of requests should be below 1s + }, + // ... +}; +``` + +1. Update the `scenarios` property to ramp the test up until it fails. + +```javascript +export const options = { + thresholds: { + http_req_failed: [{ threshold: 'rate<0.01', abortOnFail: true }], + http_req_duration: ['p(99)<1000'], + }, + scenarios: { + // define scenarios + breaking: { + executor: 'ramping-vus', + stages: [ + { duration: '10s', target: 20 }, + { duration: '50s', target: 20 }, + { duration: '50s', target: 40 }, + { duration: '50s', target: 60 }, + { duration: '50s', target: 80 }, + { duration: '50s', target: 100 }, + { duration: '50s', target: 120 }, + { duration: '50s', target: 140 }, + //.... + ], + }, + }, +}; +``` + +Here is the full script. + +{{< code >}} + +```javascript +// import necessary modules +import { check } from 'k6'; +import http from 'k6/http'; + +// define configuration +export const options = { + // define thresholds + thresholds: { + http_req_failed: [{ threshold: 'rate<0.01', abortOnFail: true }], // availability threshold for error rate + http_req_duration: ['p(99)<1000'], // Latency threshold for percentile + }, + // define scenarios + scenarios: { + breaking: { + executor: 'ramping-vus', + stages: [ + { duration: '10s', target: 20 }, + { duration: '50s', target: 20 }, + { duration: '50s', target: 40 }, + { duration: '50s', target: 60 }, + { duration: '50s', target: 80 }, + { duration: '50s', target: 100 }, + { duration: '50s', target: 120 }, + { duration: '50s', target: 140 }, + //.... + ], + }, + }, +}; + +export default function () { + // define URL and request body + const url = 'https://test-api.k6.io/auth/basic/login/'; + const payload = JSON.stringify({ + username: 'test_case', + password: '1234', + }); + const params = { + headers: { + 'Content-Type': 'application/json', + }, + }; + + // send a post request and save response as a variable + const res = http.post(url, payload, params); + + // check that response is 200 + check(res, { + 'response code was 200': (res) => res.status == 200, + }); +} +``` + +{{< /code >}} + +Run the test. + +```bash +k6 run api-test.js +``` + +Did the threshold fail? If not, add another stage with a higher target and try again. Repeat until the threshold aborts the test: + +```bash +ERRO[0010] thresholds on metrics 'http_req_duration, http_req_failed' were breached; at least one has abortOnFail enabled, stopping test prematurely +``` + +## Next steps + +In this tutorial, you used [thresholds](https://grafana.com/docs/k6//using-k6/thresholds) to assert performance and [Scenarios](https://grafana.com/docs/k6//using-k6/scenarios) to schedule different load patterns. To learn more about the usual load patterns and their goals, read [Load Test Types](https://grafana.com/docs/k6//testing-guides/test-types/) + +The [next step of this tutorial shows how to interpret test results](https://grafana.com/docs/k6//examples/get-started-with-k6/analyze-results). This involves filtering results and adding custom metrics. diff --git a/docs/sources/k6/v0.55.x/examples/get-timings-for-an-http-metric.md b/docs/sources/k6/v0.55.x/examples/get-timings-for-an-http-metric.md new file mode 100644 index 000000000..03a464583 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/get-timings-for-an-http-metric.md @@ -0,0 +1,47 @@ +--- +title: Get timings for an HTTP metric +description: How to calculate timings for an individual k6 metric +weight: 23 +--- + +# Get timings for an HTTP metric + +To access the timing information from an individual HTTP request, the [Response.timings](https://grafana.com/docs/k6//javascript-api/k6-http/response) object provides the time spent on the various phases in `ms`. +One use case of this is to use the timings in a [Custom metric](https://grafana.com/docs/k6//using-k6/metrics/create-custom-metrics) to make a trend for a specific endpoint. + +The timings are as follows: + +- blocked: equals to `http_req_blocked`. +- connecting: equals to `http_req_connecting`. +- tls_handshaking: equals to `http_req_tls_handshaking`. +- sending: equals to `http_req_sending`. +- waiting: equals to `http_req_waiting`. +- receiving: equals to `http_req_receiving`. +- duration: equals to `http_req_duration`. + +This script gets the request duration timing for a specific GET request and logs it to the console. + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + const res = http.get('https://httpbin.test.k6.io'); + console.log('Response time was ' + String(res.timings.duration) + ' ms'); +} +``` + +{{< /code >}} + +The expected (partial) output looks like this: + +{{< code >}} + +```bash +$ k6 run script.js + + INFO[0001] Response time was 337.962473 ms source=console +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/examples/html-forms.md b/docs/sources/k6/v0.55.x/examples/html-forms.md new file mode 100644 index 000000000..e640c3b04 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/html-forms.md @@ -0,0 +1,39 @@ +--- +title: 'HTML Forms' +description: 'Scripting example on how to handle HTML forms in a k6 test.' +weight: 07 +--- + +# HTML Forms + +Scripting example on how to handle HTML forms. + +In many cases using the [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) API (jQuery API clone) to interact with HTML data is enough, but for some use cases, like with forms, we can make things easier providing a higher-level API like the [Response.submitForm( [params] )](https://grafana.com/docs/k6//javascript-api/k6-http/response/response-submitform) API. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export default function () { + // Request page containing a form + let res = http.get('https://httpbin.test.k6.io/forms/post'); + + // Now, submit form setting/overriding some fields of the form + res = res.submitForm({ + formSelector: 'form', + fields: { custname: 'test', extradata: 'test2' }, + }); + sleep(3); +} +``` + +{{< /code >}} + +**Relevant k6 APIs**: + +- [Response.submitForm([params])](https://grafana.com/docs/k6//javascript-api/k6-http/response/response-submitform) +- [Selection.find(selector)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-find) + (the [jQuery Selector API](http://api.jquery.com/category/selectors/) docs are also a good + resource on what possible selector queries can be made) diff --git a/docs/sources/k6/v0.55.x/examples/http-authentication.md b/docs/sources/k6/v0.55.x/examples/http-authentication.md new file mode 100644 index 000000000..97fb2fc24 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/http-authentication.md @@ -0,0 +1,186 @@ +--- +title: 'HTTP Authentication' +description: 'Scripting examples on how to use different authentication or authorization methods in your load test.' +weight: 02 +--- + +# HTTP Authentication + +Scripting examples on how to use different authentication or authorization methods in your load test. + +## Basic authentication + +{{< code >}} + +```javascript +import encoding from 'k6/encoding'; +import http from 'k6/http'; +import { check } from 'k6'; + +const username = 'user'; +const password = 'passwd'; + +export default function () { + const credentials = `${username}:${password}`; + + // Passing username and password as part of the URL will + // allow us to authenticate using HTTP Basic Auth. + const url = `https://${credentials}@httpbin.test.k6.io/basic-auth/${username}/${password}`; + + let res = http.get(url); + + // Verify response + check(res, { + 'status is 200': (r) => r.status === 200, + 'is authenticated': (r) => r.json().authenticated === true, + 'is correct user': (r) => r.json().user === username, + }); + + // Alternatively you can create the header yourself to authenticate + // using HTTP Basic Auth + const encodedCredentials = encoding.b64encode(credentials); + const options = { + headers: { + Authorization: `Basic ${encodedCredentials}`, + }, + }; + + res = http.get(`https://httpbin.test.k6.io/basic-auth/${username}/${password}`, options); + + // Verify response (checking the echoed data from the httpbin.test.k6.io + // basic auth test API endpoint) + check(res, { + 'status is 200': (r) => r.status === 200, + 'is authenticated': (r) => r.json().authenticated === true, + 'is correct user': (r) => r.json().user === username, + }); +} +``` + +{{< /code >}} + +## Digest authentication + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +const username = 'user'; +const password = 'passwd'; + +export default function () { + // Passing username and password as part of URL plus the auth option will + // authenticate using HTTP Digest authentication. + const credentials = `${username}:${password}`; + const res = http.get( + `https://${credentials}@httpbin.test.k6.io/digest-auth/auth/${username}/${password}`, + { + auth: 'digest', + } + ); + + // Verify response (checking the echoed data from the httpbin.test.k6.io digest auth + // test API endpoint) + check(res, { + 'status is 200': (r) => r.status === 200, + 'is authenticated': (r) => r.json().authenticated === true, + 'is correct user': (r) => r.json().user === username, + }); +} +``` + +{{< /code >}} + +## NTLM authentication + +{{< code >}} + +```javascript +import http from 'k6/http'; + +const username = 'user'; +const password = 'passwd'; + +export default function () { + // Passing username and password as part of URL and then specifying + // "ntlm" as auth type will do the trick! + const credentials = `${username}:${password}`; + const res = http.get(`http://${credentials}@example.com/`, { auth: 'ntlm' }); +} +``` + +{{< /code >}} + +## AWS Signature v4 authentication with the [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws) + +To authenticate requests to AWS APIs using [AWS Signature Version 4](https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html), k6 offers the [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws) JavaScript library, which provides a dedicated `SignatureV4` class. This class can produce authenticated requests to send to AWS APIs using the `http` k6 module. + +Here's an example script to demonstrate how to sign a request to fetch an object from an S3 bucket: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { + AWSConfig, + SignatureV4, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/signature.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + + /** + * Optional session token for temporary credentials. + */ + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +export default function () { + /** + * Create a signer instance with the AWS credentials. + * The signer will be used to sign the request. + */ + const signer = new SignatureV4({ + service: 's3', + region: awsConfig.region, + credentials: { + accessKeyId: awsConfig.accessKeyId, + secretAccessKey: awsConfig.secretAccessKey, + sessionToken: awsConfig.sessionToken, + }, + }); + + /** + * Use the signer to prepare a signed request. + * The signed request can then be used to send the request to the AWS API. + */ + const signedRequest = signer.sign( + { + method: 'GET', + protocol: 'https', + hostname: 'test-jslib-aws.s3.us-east-1.amazonaws.com', + path: '/bonjour.txt', + headers: {}, + uriEscapePath: false, + applyChecksum: false, + }, + { + signingDate: new Date(), + signingService: 's3', + signingRegion: 'us-east-1', + } + ); + + /** + * The `signedRequest` object contains the signed request URL and headers. + * We can use them to send the request to the AWS API. + */ + http.get(signedRequest.url, { headers: signedRequest.headers }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/examples/http2.md b/docs/sources/k6/v0.55.x/examples/http2.md new file mode 100644 index 000000000..cf45edb81 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/http2.md @@ -0,0 +1,28 @@ +--- +title: 'HTTP2' +description: 'Information on how to load test HTTP/2.' +weight: 12 +--- + +# HTTP2 + +If the target system indicates that a connection can be upgraded from HTTP/1.1 to HTTP/2, k6 will do so automatically. + +## Making HTTP/2 requests + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const res = http.get('https://test-api.k6.io/'); + check(res, { + 'status is 200': (r) => r.status === 200, + 'protocol is HTTP/2': (r) => r.proto === 'HTTP/2.0', + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/examples/instant-load-increase.md b/docs/sources/k6/v0.55.x/examples/instant-load-increase.md new file mode 100644 index 000000000..fb86b16ab --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/instant-load-increase.md @@ -0,0 +1,74 @@ +--- +title: 'Instant load increase' +description: 'Scripting example on how to instantly increase the number of VUs or iterations and hold them for a period of time' +draft: 'false' +weight: 22 +--- + +# Instant load increase + +One of the common usages of load testing tools it's the so-called stepped arrival rate. + +In k6, you can do that by using the `options` object, and configuring the number of iterations or VUs in different scenarios. + +Here's an example on how to instantly increase the number of iterations and hold them for a period of time. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + scenarios: { + contacts: { + executor: 'ramping-arrival-rate', + preAllocatedVUs: 50, + timeUnit: '1s', + startRate: 50, + stages: [ + { target: 200, duration: '30s' }, // linearly go from 50 iters/s to 200 iters/s for 30s + { target: 500, duration: '0' }, // instantly jump to 500 iters/s + { target: 500, duration: '10m' }, // continue with 500 iters/s for 10 minutes + ], + }, + }, +}; + +export default function () { + http.get('https://test.k6.io'); + sleep(1); +} +``` + +{{< /code >}} + +Here's an example on how to instantly increase the number of VUs and hold them for a period of time. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + scenarios: { + contacts: { + executor: 'ramping-vus', + startVUs: 3, + stages: [ + { target: 20, duration: '30s' }, // linearly go from 3 VUs to 20 VUs for 30s + { target: 100, duration: '0' }, // instantly jump to 100 VUs + { target: 100, duration: '10m' }, // continue with 100 VUs for 10 minutes + ], + }, + }, +}; + +export default function () { + http.get('https://test.k6.io'); + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/examples/oauth-authentication.md b/docs/sources/k6/v0.55.x/examples/oauth-authentication.md new file mode 100644 index 000000000..adcd58d46 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/oauth-authentication.md @@ -0,0 +1,332 @@ +--- +title: 'OAuth Authentication' +description: 'Scripting examples on how to use OAuth authentication in your load test.' +weight: 03 +--- + +# OAuth Authentication + +Scripting examples on how to use OAuth authentication in your load test. + +## OAuth authentication + +The following examples take a set of arguments, shown in the function documentation, and returns the response body as JSON so that you can extract the token from. + +### Azure Active Directory + +{{< code >}} + +```javascript +import http from 'k6/http'; + +/** + * Authenticate using OAuth against Azure Active Directory + * @function + * @param {string} tenantId - Directory ID in Azure + * @param {string} clientId - Application ID in Azure + * @param {string} clientSecret - Can be obtained from https://docs.microsoft.com/en-us/azure/storage/common/storage-auth-aad-app#create-a-client-secret + * @param {string} scope - Space-separated list of scopes (permissions) that are already given consent to by admin + * @param {string} resource - Either a resource ID (as string) or an object containing username and password + */ +export function authenticateUsingAzure(tenantId, clientId, clientSecret, scope, resource) { + let url; + const requestBody = { + client_id: clientId, + client_secret: clientSecret, + scope: scope, + }; + + if (typeof resource == 'string') { + url = `https://login.microsoftonline.com/${tenantId}/oauth2/token`; + requestBody['grant_type'] = 'client_credentials'; + requestBody['resource'] = resource; + } else if ( + typeof resource == 'object' && + resource.hasOwnProperty('username') && + resource.hasOwnProperty('password') + ) { + url = `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`; + requestBody['grant_type'] = 'password'; + requestBody['username'] = resource.username; + requestBody['password'] = resource.password; + } else { + throw 'resource should be either a string or an object containing username and password'; + } + + const response = http.post(url, requestBody); + + return response.json(); +} +``` + +{{< /code >}} + +### Azure B2C + +The following example shows how you can authenticate with Azure B2C using the [Client Credentials Flow](https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-reference-oauth-code#client-credentials-flow). + +This example is based on a JMeter example found at the [azure-ad-b2c/load-tests](https://github.com/azure-ad-b2c/load-tests) repository. + +To use this script, you need to: + +1. [Set up your own Azure B2C tenant](https://learn.microsoft.com/en-us/azure/active-directory-b2c/tutorial-create-tenant) + - Copy the tenant name, it will be used in your test script. +1. [Register a web application](https://learn.microsoft.com/en-us/azure/active-directory-b2c/tutorial-register-applications?tabs=app-reg-ga) + - Register a single page application with the redirect URL of: https://jwt.ms. That's needed for the flow to receive a token. + - After the creation, you can get the Application (client) ID, and the Directory (tenant) ID. Copy both of them, they'll be used in your test script. +1. [Create a user flow so that you can sign up and create a user](https://docs.microsoft.com/en-us/azure/active-directory-b2c/tutorial-create-user-flows) + - Create a new user, and copy the username and password. They'll be used in the test script. + +You can find the settings in the B2C settings in the Azure portal if you need to refer to them later on. Make sure to fill out all the variables for the `B2CGraphSettings` object, as well as replace `USERNAME` and `PASSWORD` in `export default function`. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import crypto from 'k6/crypto'; +import { randomString } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; + +const B2CGraphSettings = { + B2C: { + client_id: '', // Application ID in Azure + user_flow_name: '', + tenant_id: '', // Directory ID in Azure + tenant_name: '', + scope: 'openid', + redirect_url: 'https://jwt.ms', + }, +}; + +/** + * Authenticate using OAuth against Azure B2C + * @function + * @param {string} username - Username of the user to authenticate + * @param {string} password + * @return {string} id_token + */ +export function GetB2cIdToken(username, password) { + const state = GetState(); + SelfAsserted(state, username, password); + const code = CombinedSigninAndSignup(state); + return GetToken(code, state.codeVerifier); +} + +/** + * @typedef {object} b2cStateProperties + * @property {string} csrfToken + * @property {string} stateProperty + * @property {string} codeVerifier + * + */ + +/** + * Get the id token from Azure B2C + * @function + * @param {string} code + * @returns {string} id_token + */ +const GetToken = (code, codeVerifier) => { + const url = + `https://${B2CGraphSettings.B2C.tenant_name}.b2clogin.com/${B2CGraphSettings.B2C.tenant_id}` + + `/oauth2/v2.0/token` + + `?p=${B2CGraphSettings.B2C.user_flow_name}` + + `&client_id=${B2CGraphSettings.B2C.client_id}` + + `&grant_type=authorization_code` + + `&scope=${B2CGraphSettings.B2C.scope}` + + `&code=${code}` + + `&redirect_uri=${B2CGraphSettings.B2C.redirect_url}` + + `&code_verifier=${codeVerifier}`; + + const response = http.post(url, '', { + tags: { + b2c_login: 'GetToken', + }, + }); + + return JSON.parse(response.body).id_token; +}; + +/** + * Signs in the user using the CombinedSigninAndSignup policy + * extraqct B2C code from response + * @function + * @param {b2cStateProperties} state + * @returns {string} code + */ +const CombinedSigninAndSignup = (state) => { + const url = + `https://${B2CGraphSettings.B2C.tenant_name}.b2clogin.com/${B2CGraphSettings.B2C.tenant_name}.onmicrosoft.com` + + `/${B2CGraphSettings.B2C.user_flow_name}/api/CombinedSigninAndSignup/confirmed` + + `?csrf_token=${state.csrfToken}` + + `&rememberMe=false` + + `&tx=StateProperties=${state.stateProperty}` + + `&p=${B2CGraphSettings.B2C.user_flow_name}`; + + const response = http.get(url, '', { + tags: { + b2c_login: 'CombinedSigninAndSignup', + }, + }); + const codeRegex = '.*code=([^"]*)'; + return response.url.match(codeRegex)[1]; +}; + +/** + * Signs in the user using the SelfAsserted policy + * @function + * @param {b2cStateProperties} state + * @param {string} username + * @param {string} password + */ +const SelfAsserted = (state, username, password) => { + const url = + `https://${B2CGraphSettings.B2C.tenant_name}.b2clogin.com/${B2CGraphSettings.B2C.tenant_id}` + + `/${B2CGraphSettings.B2C.user_flow_name}/SelfAsserted` + + `?tx=StateProperties=${state.stateProperty}` + + `&p=${B2CGraphSettings.B2C.user_flow_name}` + + `&request_type=RESPONSE` + + `&email=${username}` + + `&password=${password}`; + + const params = { + headers: { + 'X-CSRF-TOKEN': `${state.csrfToken}`, + }, + tags: { + b2c_login: 'SelfAsserted', + }, + }; + http.post(url, '', params); +}; + +/** + * Calls the B2C login page to get the state property + * @function + * @returns {b2cStateProperties} b2cState + */ +const GetState = () => { + const nonce = randomString(50); + const challenge = crypto.sha256(nonce.toString(), 'base64rawurl'); + + const url = + `https://${B2CGraphSettings.B2C.tenant_name}.b2clogin.com` + + `/${B2CGraphSettings.B2C.tenant_id}/oauth2/v2.0/authorize?` + + `p=${B2CGraphSettings.B2C.user_flow_name}` + + `&client_id=${B2CGraphSettings.B2C.client_id}` + + `&nonce=${nonce}` + + `&redirect_uri=${B2CGraphSettings.B2C.redirect_url}` + + `&scope=${B2CGraphSettings.B2C.scope}` + + `&response_type=code` + + `&prompt=login` + + `&code_challenge_method=S256` + + `&code_challenge=${challenge}` + + `&response_mode=fragment`; + + const response = http.get(url, '', { + tags: { + b2c_login: 'GetCookyAndState', + }, + }); + + const vuJar = http.cookieJar(); + const responseCookies = vuJar.cookiesForURL(response.url); + + const b2cState = {}; + b2cState.codeVerifier = nonce; + b2cState.csrfToken = responseCookies['x-ms-cpim-csrf'][0]; + b2cState.stateProperty = response.body.match('.*StateProperties=([^"]*)')[1]; + return b2cState; +}; + +/** + * Helper function to get the authorization header for a user + * @param {user} user + * @returns {object} httpsOptions + */ +export const GetAuthorizationHeaderForUser = (user) => { + const token = GetB2cIdToken(user.username, user.password); + + return { + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + token, + }, + }; +}; + +export default function () { + const token = GetB2cIdToken('USERNAME', 'PASSWORD'); + console.log(token); +} +``` + +{{< /code >}} + +### Okta + +{{< code >}} + +```javascript +import http from 'k6/http'; +import encoding from 'k6/encoding'; + +/** + * Authenticate using OAuth against Okta + * @function + * @param {string} oktaDomain - Okta domain to authenticate against (e.g. 'k6.okta.com') + * @param {string} authServerId - Authentication server identifier (default is 'default') + * @param {string} clientId - Generated by Okta automatically + * @param {string} clientSecret - Generated by Okta automatically + * @param {string} scope - Space-separated list of scopes + * @param {string|object} resource - Either a resource ID (as string) or an object containing username and password + */ +export function authenticateUsingOkta( + oktaDomain, + authServerId, + clientId, + clientSecret, + scope, + resource +) { + if (authServerId === 'undefined' || authServerId == '') { + authServerId = 'default'; + } + const url = `https://${oktaDomain}/oauth2/${authServerId}/v1/token`; + const requestBody = { scope: scope }; + let response; + + if (typeof resource == 'string') { + requestBody['grant_type'] = 'client_credentials'; + + const encodedCredentials = encoding.b64encode(`${clientId}:${clientSecret}`); + const params = { + auth: 'basic', + headers: { + Authorization: `Basic ${encodedCredentials}`, + }, + }; + + response = http.post(url, requestBody, params); + } else if ( + typeof resource == 'object' && + resource.hasOwnProperty('username') && + resource.hasOwnProperty('password') + ) { + requestBody['grant_type'] = 'password'; + requestBody['username'] = resource.username; + requestBody['password'] = resource.password; + requestBody['client_id'] = clientId; + requestBody['client_secret'] = clientSecret; + + response = http.post(url, requestBody); + } else { + throw 'resource should be either a string or an object containing username and password'; + } + + return response.json(); +} +``` + +{{< /code >}} + +For a detailed example, please visit this article: [How to Load Test OAuth secured APIs with k6?](https://k6.io/blog/how-to-load-test-oauth-secured-apis-with-k6) diff --git a/docs/sources/k6/v0.55.x/examples/parse-html.md b/docs/sources/k6/v0.55.x/examples/parse-html.md new file mode 100644 index 000000000..17c8960c6 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/parse-html.md @@ -0,0 +1,63 @@ +--- +title: 'Parse HTML' +description: 'Scripting examples parsing HTML content.' +weight: 06 +--- + +# Parse HTML + +Examples parsing HTML content. Use the `k6/html` module for HTML parsing. + +| Name | Type | Description | +| ------------------------------------------------------------------------------------------- | -------- | ----------------------------------------------------- | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | Class | A jQuery-like API for accessing HTML DOM elements. | +| [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) | Class | An HTML DOM element as returned by the Selection API. | +| [parseHTML(src)](https://grafana.com/docs/k6//javascript-api/k6-html/parsehtml) | function | Parse an HTML string and populate a Selection object. | + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import http from 'k6/http'; + +export default function () { + const res = http.get('https://k6.io'); + const doc = parseHTML(res.body); // equivalent to res.html() + const pageTitle = doc.find('head title').text(); + const langAttr = doc.find('html').attr('lang'); +} +``` + +{{< /code >}} + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
Value term 1
+
Value term 2
+
+ `; + const sel = parseHTML(content).find('dl').children(); + + const el1 = sel.get(0); + const el2 = sel.get(1); + + console.log(el1.nodeName()); + console.log(el1.id()); + console.log(el1.textContent()); + + console.log(el2.nodeName()); + console.log(el2.id()); + console.log(el2.textContent()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/examples/single-request.md b/docs/sources/k6/v0.55.x/examples/single-request.md new file mode 100644 index 000000000..eed874701 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/single-request.md @@ -0,0 +1,24 @@ +--- +title: 'Single request' +description: 'Example of one HTTP GET request' +draft: 'false' +weight: 01 +--- + +# Single request + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + iterations: 1, +}; + +export default function () { + const response = http.get('https://test-api.k6.io/public/crocodiles/'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/examples/soap.md b/docs/sources/k6/v0.55.x/examples/soap.md new file mode 100644 index 000000000..4554a2e8b --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/soap.md @@ -0,0 +1,49 @@ +--- +title: 'SOAP' +description: 'Load Testing SOAP API.' +weight: 14 +--- + +# SOAP + +Although k6 doesn't have any built-in APIs for working with SOAP or XML data in general, you +can still easily load test a SOAP-based API by crafting SOAP messages and using the HTTP request APIs. + +## Making SOAP requests + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check, sleep } from 'k6'; + +const soapReqBody = ` + + + + UnitedStates + + +`; + +export default function () { + // When making a SOAP POST request we must not forget to set the content type to text/xml + const res = http.post( + 'http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx', + soapReqBody, + { + headers: { 'Content-Type': 'text/xml' }, + } + ); + + // Make sure the response is correct + check(res, { + 'status is 200': (r) => r.status === 200, + 'black friday is present': (r) => r.body.indexOf('BLACK-FRIDAY') !== -1, + }); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/examples/tls.md b/docs/sources/k6/v0.55.x/examples/tls.md new file mode 100644 index 000000000..4e97b345d --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/tls.md @@ -0,0 +1,34 @@ +--- +title: 'Transport Layer Security (TLS)' +description: | + TLS is the mechanism through which encrypted connections can be established between clients and + servers on the web and through which data can flow with integrity intact. +weight: 15 +--- + +# Transport Layer Security (TLS) + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export const options = { + tlsCipherSuites: ['TLS_RSA_WITH_RC4_128_SHA', 'TLS_RSA_WITH_AES_128_GCM_SHA256'], + tlsVersion: { + min: 'ssl3.0', + max: 'tls1.2', + }, +}; + +export default function () { + const res = http.get('https://sha256.badssl.com'); + check(res, { + 'is TLSv1.2': (r) => r.tls_version === http.TLS_1_2, + 'is sha256 cipher suite': (r) => r.tls_cipher_suite === 'TLS_RSA_WITH_AES_128_GCM_SHA256', + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/examples/tracking-data-per.md b/docs/sources/k6/v0.55.x/examples/tracking-data-per.md new file mode 100644 index 000000000..523a5c141 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/tracking-data-per.md @@ -0,0 +1,72 @@ +--- +title: 'Track transmitted data per URL' +slug: '/track-transmitted-data-per-url' +description: 'This example shows how to track data sent and received for a individual URL.' +weight: 20 +--- + +# Track transmitted data per URL + +By default, k6 collects automatically two [built-in metrics](https://grafana.com/docs/k6//using-k6/metrics#built-in-metrics) related to the transmitted data during the test execution: + +- `data_received`: the amount of received data. +- `data_sent`: the amount of data sent. + +However, the reported values of these metrics don't tag the particular request or URL. Therefore, you cannot know the amount of data transmitted for a specific request or URL. + +This example shows how to track data sent and received for an individual URL. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; +import { Counter } from 'k6/metrics'; + +// Two custom metrics to track data sent and received. We will tag data points added with the corresponding URL +// so we can filter these metrics down to see the data for individual URLs and set threshold across all or per-URL as well. +export const epDataSent = new Counter('endpoint_data_sent'); +export const epDataRecv = new Counter('endpoint_data_recv'); + +export const options = { + duration: '10s', + vus: 10, + thresholds: { + // We can setup thresholds on these custom metrics, "count" means bytes in this case. + 'endpoint_data_sent': ['count < 2048'], + + // The above threshold would look at all data points added to the custom metric. + // If we want to only consider data points for a particular URL/endpoint we can filter by URL. + 'endpoint_data_sent{url:https://test.k6.io/contacts.php}': ['count < 1024'], + 'endpoint_data_recv{url:https://test.k6.io/}': ['count < 2048'], // "count" means bytes in this case + }, +}; + +function sizeOfHeaders(hdrs) { + return Object.keys(hdrs).reduce((sum, key) => sum + key.length + hdrs[key].length, 0); +} + +function trackDataMetricsPerURL(res) { + // Add data points for sent and received data + epDataSent.add(sizeOfHeaders(res.request.headers) + res.request.body.length, { + url: res.url, + }); + epDataRecv.add(sizeOfHeaders(res.headers) + res.body.length, { + url: res.url, + }); +} + +export default function () { + let res; + + res = http.get('https://test.k6.io/'); + trackDataMetricsPerURL(res); + + res = http.get('https://test.k6.io/contacts.php'); + trackDataMetricsPerURL(res); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/examples/tutorials/_index.md b/docs/sources/k6/v0.55.x/examples/tutorials/_index.md new file mode 100644 index 000000000..7501bc840 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/tutorials/_index.md @@ -0,0 +1,18 @@ +--- +title: 'Tutorials' +description: 'k6 Tutorials' +weight: 26 +--- + +# Tutorials + +- [Get started with k6](https://grafana.com/docs/k6//examples/get-started-with-k6) +- [8 basic tasks to learn k6](https://k6.io/blog/learning-js-through-load-testing/) +- [k6 Learn](https://github.com/grafana/k6-learn) +- [Swagger/OpenAPI integration](https://k6.io/blog/load-testing-your-api-with-swagger-openapi-and-k6) +- [Schedule k6 tests with cron](https://k6.io/blog/performance-monitoring-with-cron-and-k6) +- [Load test a GraphQL service](https://k6.io/blog/load-testing-graphql-with-k6) +- [Use TypeScript in k6 scripts](https://grafana.com/docs/k6//using-k6/javascript-typescript-compatibility-mode/) +- [Debug using a Web Proxy](https://k6.io/blog/k6-load-testing-debugging-using-a-web-proxy/) +- [Distributed k6 tests on K8s](https://k6.io/blog/running-distributed-tests-on-k8s/) +- [Create a k6 extension](https://k6.io/blog/extending-k6-with-xk6) diff --git a/docs/sources/k6/v0.55.x/examples/url-query-parameters.md b/docs/sources/k6/v0.55.x/examples/url-query-parameters.md new file mode 100644 index 000000000..479141204 --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/url-query-parameters.md @@ -0,0 +1,91 @@ +--- +title: 'URLs with query parameters' +description: 'Scripting examples using URL and URLSearchParams modules.' +weight: 21 +--- + +# URLs with query parameters + +How to use **URL** and **URLSearchParams** imported from [jslib.k6.io](https://jslib.k6.io/) to construct URLs with/without query parameters. + +## URL + +{{< code >}} + +```javascript +import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js'; +import http from 'k6/http'; + +export default function () { + const url = new URL('https://k6.io'); + + url.searchParams.append('utm_medium', 'organic'); + url.searchParams.append('utm_source', 'test'); + url.searchParams.append('multiple', ['foo', 'bar']); + + const res = http.get(url.toString()); + // https://k6.io?utm_medium=organic&utm_source=test&multiple=foo%2Cbar +} +``` + +{{< /code >}} + + + +| Name | Type | Description | +| --------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| URLSearchParams(init) | Constructor | `init` Optional: One of [USVString, sequence of pairs or record ] | +| toString() | Method | Returns a USVString containing the whole URL | +| hash | Property | A USVString containing a '#' followed by the fragment identifier of the URL. | +| host | Property | A USVString containing the host, that is the `hostname`, and then, if the port of the URL is nonempty, a ':', followed by the `port` of the URL. | +| hostname | Property | A USVString containing the [domain name](https://developer.mozilla.org/en-US/docs/Glossary/Domain_name) of the URL. | +| href | Property | Returns a USVString containing the whole URL. | +| origin | Property | Returns a USVString containing the origin of the URL, that is its scheme, its domain and its port. | +| password | Property | A USVString containing the password specified before the domain name. | +| pathname | Property | Is a USVString containing an initial '/' followed by the path of the URL, not including the query string or fragment. | +| port | Property | A USVString containing the port number of the URL. | +| protocol | Property | A USVString containing the protocol scheme of the URL, including the final ':'. | +| search | Property | A USVString indicating the URL's parameter string; if any parameters are provided, this string includes all of them, beginning with the leading ? character. | +| searchParams | Property | A [URLSearchParams](#urlsearchparams) object which can be used to access the individual query parameters found in search. | +| username | Property | A USVString containing the username specified before the domain name. | + + + +## URLSearchParams + +{{< code >}} + +```javascript +import { URLSearchParams } from 'https://jslib.k6.io/url/1.0.0/index.js'; +import http from 'k6/http'; + +export default function () { + const searchParams = new URLSearchParams([ + ['utm_medium', 'organic'], + ['utm_source', 'test'], + ['multiple', 'foo'], + ['multiple', 'bar'], + ]); + + const res = http.get(`${'https://k6.io'}?${searchParams.toString()}`); + // https://k6.io?utm_medium=organic&utm_source=test&multiple=foo&multiple=bar +} +``` + +{{< /code >}} + +| Name | Type | Description | +| --------------------- | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| URLSearchParams(init) | Constructor | `init` Optional: One of [USVString, sequence of pairs or record ] | +| toString() | Method | Returns a query string (questionmark is omitted). | +| append() | Method | Appends a specified key/value pair as a new search parameter. | +| delete() | Method | Deletes the given search parameter, and its associated value, from the list of all search parameters. | +| entries() | Method | Returns an iterator allowing iteration through all key/value pairs contained in this object. | +| forEach() | Method | Allows iteration through all values contained in this object via a callback function. `callback(value, key)`. | +| get() | Method | Returns the first value associated with the given search parameter. | +| getAll() | Method | Returns all the values associated with a given search parameter. | +| has() | Method | Returns a Boolean that indicates whether a parameter with the specified name exists. | +| keys() | Method | Returns an iterator allowing iteration through all keys of the key/value pairs contained in this object. | +| values() | Method | Returns an iterator allowing iteration through all values of the key/value pairs contained in this object. | +| set() | Method | Sets the value associated with a given search parameter to the given value. If there were several matching values, this method deletes the others. If the search parameter doesn't exist, this method creates it. | +| sort() | Method | Sorts all key/value pairs, if any, by their keys. | diff --git a/docs/sources/k6/v0.55.x/examples/websockets.md b/docs/sources/k6/v0.55.x/examples/websockets.md new file mode 100644 index 000000000..6db2b362d --- /dev/null +++ b/docs/sources/k6/v0.55.x/examples/websockets.md @@ -0,0 +1,84 @@ +--- +title: 'WebSockets' +description: | + Scripting example on how to use WebSocket API in k6. +weight: 13 +--- + +# WebSockets + +Here's a load test for CrocoChat - a WebSocket chat API available on [https://test-api.k6.io/](https://test-api.k6.io/). + +Multiple VUs join a chat room and discuss various things for up to 1 minute, after which they disconnect. + +Each VU receives messages sent by all chat participants. + +{{< code >}} + +```javascript +import { randomString, randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; +import ws from 'k6/ws'; +import { check, sleep } from 'k6'; + +const sessionDuration = randomIntBetween(10000, 60000); // user session between 10s and 1m +const chatRoomName = 'publicRoom'; // choose your chat room name + +export const options = { + vus: 10, + iterations: 10, +}; + +export default function () { + const url = `wss://test-api.k6.io/ws/crocochat/${chatRoomName}/`; + const params = { tags: { my_tag: 'my ws session' } }; + + const res = ws.connect(url, params, function (socket) { + socket.on('open', function open() { + console.log(`VU ${__VU}: connected`); + + socket.send(JSON.stringify({ event: 'SET_NAME', new_name: `Croc ${__VU}` })); + + socket.setInterval(function timeout() { + socket.send(JSON.stringify({ event: 'SAY', message: `I'm saying ${randomString(5)}` })); + }, randomIntBetween(2000, 8000)); // say something every 2-8seconds + }); + + socket.on('ping', function () { + console.log('PING!'); + }); + + socket.on('pong', function () { + console.log('PONG!'); + }); + + socket.on('close', function () { + console.log(`VU ${__VU}: disconnected`); + }); + + socket.on('message', function (message) { + const msg = JSON.parse(message); + if (msg.event === 'CHAT_MSG') { + console.log(`VU ${__VU} received: ${msg.user} says: ${msg.message}`); + } else if (msg.event === 'ERROR') { + console.error(`VU ${__VU} received:: ${msg.message}`); + } else { + console.log(`VU ${__VU} received unhandled message: ${msg.message}`); + } + }); + + socket.setTimeout(function () { + console.log(`VU ${__VU}: ${sessionDuration}ms passed, leaving the chat`); + socket.send(JSON.stringify({ event: 'LEAVE' })); + }, sessionDuration); + + socket.setTimeout(function () { + console.log(`Closing the socket forcefully 3s after graceful LEAVE`); + socket.close(); + }, sessionDuration + 3000); + }); + + check(res, { 'Connected successfully': (r) => r && r.status === 101 }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/extensions/_index.md b/docs/sources/k6/v0.55.x/extensions/_index.md new file mode 100644 index 000000000..afc64157e --- /dev/null +++ b/docs/sources/k6/v0.55.x/extensions/_index.md @@ -0,0 +1,80 @@ +--- +title: Extensions +description: 'The k6 extension ecosystem enables developers and testers to extend k6 to cover use cases not supported natively in the core. Explore the endless possibilities of k6 and xk6.' +weight: 700 +--- + +# Extensions + +Expand the potential use cases for k6. + +## Quickstart + + + +## Custom k6 builds + +With k6 extensions, you can create custom k6 binaries to support your specific reliability-testing needs. + +Currently, k6 supports two ways to extend its native functionality: + +- **JavaScript extensions** extend the JavaScript APIs available to your test scripts. Add support for new network protocols, improve performance compared to equivalent JS libraries, or add features. + +- **Output extensions** send metrics to a custom file format or service. Add custom processing and dispatching. + +## xk6 makes custom binaries + +[xk6](https://github.com/grafana/xk6/) is command-line tool and framework written in Go. With xk6, you build custom k6 binaries that bundle one or more extensions written in Go. You have two options for creating k6 binaries: using [Go and xk6](https://grafana.com/docs/k6//extensions/build-k6-binary-using-go/) or [Docker](https://grafana.com/docs/k6//extensions/build-k6-binary-using-docker/): + +{{< code >}} + +```go-and-xk6 +xk6 build \ + --with github.com/grafana/xk6-sql@v0.0.1 \ + --with github.com/grafana/xk6-output-prometheus-remote +``` + +```docker-in-linux +docker run --rm -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" grafana/xk6 build \ + --with github.com/grafana/xk6-sql@v0.0.1 \ + --with github.com/grafana/xk6-output-prometheus-remote +``` + +{{< /code >}} + +
+ +## Extension use cases + +The extensions ecosystem provides endless possibilities to expand the functionality for your k6 testing. Some reasons you might want to extend k6 include the following: + +- **To add support for a new network protocol** + + For example, [xk6-amqp](https://github.com/grafana/xk6-amqp) gives access to resources using the Advanced Message Queueing Protocol (AMQP). With xk6-amqp, your scripts can create message queues and seed messages for tests that include systems like RabbitMQ or ActiveMQ (among others). + +- **To incorporate a client library for test dependency** + + Everyone wants to run their services in Kubernetes these days. With [xk6-kubernetes](https://github.com/grafana/xk6-kubernetes), your JavaScript tests can interface directly with Kubernetes resources using functionality typically restricted to kubectl. Prepare isolated Namespaces for each test run, or inject environment variables as ConfigMaps. + +- **To format and send metrics to the output of your choice** + + Suppose your company has consolidated its observability metrics into Prometheus. Use [xk6-output-prometheus-remote](https://github.com/grafana/xk6-output-prometheus-remote) to publish your k6 test metrics to Prometheus as well! + +- **To improve script performance and efficiency** + + Perhaps your company uses OpenTelemetry to trace service requests through layers of microservices. Using [xk6-distributed-tracing](https://github.com/grafana/xk6-distributed-tracing), you can update the http client to link your test requests as the origin for your traces—no need to add JavaScript code to supply the required trace headers. + +Next, [Explore the available extensions](https://grafana.com/docs/k6//extensions/explore) to see how you can expand your use of k6 right now. diff --git a/docs/sources/k6/v0.55.x/extensions/build-k6-binary-using-docker.md b/docs/sources/k6/v0.55.x/extensions/build-k6-binary-using-docker.md new file mode 100644 index 000000000..10fd09d0d --- /dev/null +++ b/docs/sources/k6/v0.55.x/extensions/build-k6-binary-using-docker.md @@ -0,0 +1,180 @@ +--- +title: 'Build a k6 binary using Docker' +description: 'Guide to build a k6 binary with extensions using Docker.' +weight: 03 +--- + +# Build a k6 binary using Docker + +Using the [xk6 Docker image](https://hub.docker.com/r/grafana/xk6/) can simplify the process of creating a custom k6 binary. It avoids having to setup a local Go environment, and install xk6 manually. + +{{< admonition type="note" >}} + +This tutorial is about creating a custom k6 binary (using Docker). If you want to create a Docker image with a custom k6 binary, refer instead to [Using modules with Docker](https://grafana.com/docs/k6//using-k6/modules/#using-modules-with-docker). + +{{< /admonition >}} + +## Building your first extension + +For example, to build a custom k6 binary with the latest versions of k6 and the [`xk6-kafka`](https://github.com/mostafa/xk6-kafka) and [`xk6-output-influxdb`](https://github.com/grafana/xk6-output-influxdb) extensions, run one of the commands below, depending on your operating system: + +{{< code >}} + +```linux +docker run --rm -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" grafana/xk6 build \ + --with github.com/mostafa/xk6-kafka \ + --with github.com/grafana/xk6-output-influxdb +``` + +```mac +docker run --rm -e GOOS=darwin -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" \ + grafana/xk6 build \ + --with github.com/mostafa/xk6-kafka \ + --with github.com/grafana/xk6-output-influxdb +``` + +```windows-powershell +docker run --rm -e GOOS=windows -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" ` + grafana/xk6 build --output k6.exe ` + --with github.com/mostafa/xk6-kafka ` + --with github.com/grafana/xk6-output-influxdb +``` + +```windows +docker run --rm -e GOOS=windows -v "%cd%:/xk6" ^ + grafana/xk6 build --output k6.exe ^ + --with github.com/mostafa/xk6-kafka ^ + --with github.com/grafana/xk6-output-influxdb +``` + +{{< /code >}} + +This creates a `k6` (or `k6.exe`) binary in the current working directory. + +To build the binary with concrete versions, see the example below (k6 `v0.45.1`, xk6-kafka `v0.19.1`, and xk6-output-influxdb `v0.4.1`): + +{{< code >}} + +```linux +docker run --rm -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" grafana/xk6 build v0.45.1 \ + --with github.com/mostafa/xk6-kafka@v0.19.1 \ + --with github.com/grafana/xk6-output-influxdb@v0.4.1 +``` + +```mac +docker run --rm -e GOOS=darwin -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" \ + grafana/xk6 build v0.45.1 \ + --with github.com/mostafa/xk6-kafka@v0.19.1 \ + --with github.com/grafana/xk6-output-influxdb@v0.4.1 +``` + +```windows-powershell +docker run --rm -e GOOS=windows -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" ` + grafana/xk6 build v0.45.1 --output k6.exe ` + --with github.com/mostafa/xk6-kafka@v0.19.1 ` + --with github.com/grafana/xk6-output-influxdb@v0.4.1 +``` + +```windows +docker run --rm -e GOOS=windows -v "%cd%:/xk6" ^ + grafana/xk6 build v0.45.1 --output k6.exe ^ + --with github.com/mostafa/xk6-kafka@v0.19.1 ^ + --with github.com/grafana/xk6-output-influxdb@v0.4.1 +``` + +{{< /code >}} + +## Breaking down the command + +The example command line may look a bit intimidating at first, but let's focus on the first part, which pertains strictly to Docker: + +```bash +docker run --rm -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" +``` + +This tells Docker to run a new container from an image. + +- `--rm` means the container will be destroyed once your build is completed. +- `-u` specifies the user and group IDs of the account on the host machine. This is important for the `k6` file to have the same file permissions as the host user. +- `-v` is required to mount the current working directory inside the container, so that the `k6` binary can be written to it. + +For Windows and Mac, we additionally include the target system as an environment variable: + +```bash +-e GOOS= +``` + +The remainder is straight from the [xk6 documentation](https://github.com/grafana/xk6/#command-usage), with the exception that we use the `grafana/xk6` _image_ rather than a local installation of `xk6`: + +{{< code >}} + +```plain +grafana/xk6 build [] + [--output ] + [--with ...] + [--replace ...] + +Flags: + --output specifies the new binary name [default: 'k6'] + --replace enables override of dependencies for k6 and extensions [default: none] + --with the extension module to be included in the binary [default: none] +``` + +{{< /code >}} + +{{< admonition type="caution" >}} + +The use of `--replace` should be considered an advanced feature to be avoided unless required. + +{{< /admonition >}} + +Referring back to our executed command, note that: + +- We specify the version as `v0.43.1`. When you omit the version or specify `latest`, you build using the _latest_ source code for k6. + Consider using a stable [release version](https://github.com/grafana/k6/releases) as a best practice unless you genuinely want the _bleeding edge_. +- We specify a full GitHub URI for the extension repository with each `--with`. + If a version is not specified, the default is again the `latest`. + Check your extension repository for stable release versions, if available, to lock in your version as we've done with `xk6-kafka@v0.17.0` and `xk6-output-influxdb@v0.3.0`. +- For Windows, we used the `--output` option to name our result as `k6.exe`; if not specified, our new binary is `k6` within the current directory. + If you specify a directory, the new binary will be `k6` within _that_ directory. + If you specify a path to a non-existent file, e.g. `/tmp/k6-extended`, this will be the path and filename for the binary. + +Run `./k6 version` (or `k6.exe version`) to check that your build is based on the appropriate `k6` version and contains the desired extensions. For example: + +{{< code >}} + +```bash +$ ./k6 version +k6 v0.43.1 ((devel), go1.20.1, darwin/amd64) +Extensions: + github.com/grafana/xk6-output-influxdb v0.3.0, xk6-influxdb [output] + github.com/mostafa/xk6-kafka v0.17.0, k6/x/kafka [js] +``` + +{{< /code >}} + +## Running your extended binary + +Now that we have our newly built k6 binary, we can run scripts using the functionalities +of the bundled extensions. + +{{< code >}} + +```bash +./k6 run my-script.js +``` + +```batch +k6.exe run my-script.js +``` + +{{< /code >}} + +> Be sure to specify the binary just built in the current directory as `./k6`, or else +> Linux/Mac could execute another k6 binary on your system path. Windows shells will +> first search for the binary in the current directory by default. + +## Encountering issues? + +If you're having issues, search the [k6 Community Forum](https://community.grafana.com/c/grafana-k6/extensions/82). +Someone may have had the same issue in the past. diff --git a/docs/sources/k6/v0.55.x/extensions/build-k6-binary-using-go.md b/docs/sources/k6/v0.55.x/extensions/build-k6-binary-using-go.md new file mode 100644 index 000000000..f444385d5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/extensions/build-k6-binary-using-go.md @@ -0,0 +1,126 @@ +--- +title: 'Build a k6 binary using Go' +description: 'Guide to build a k6 binary that includes one or many extensions using xk6.' +weight: 02 +--- + +# Build a k6 binary using Go + +To use an extension that you found on the [Extension page](https://grafana.com/docs/k6//extensions/explore) or the [xk6 GitHub topic](https://github.com/topics/xk6), +you need to build a binary using Go. + +{{< admonition type="note" >}} + +Not interested in setting up a Go environment? You can [use Docker instead](https://grafana.com/docs/k6//extensions/build-k6-binary-using-docker/). + +{{< /admonition >}} + +## Before you start + +- Set up [Go](https://golang.org/doc/install) and [Git](https://git-scm.com/). +- Make sure that your `$PATH` environment variable is updated so that `go version` returns the correct version. + +## Installing xk6 + +Given the prerequisite Go setup, installing [xk6](https://github.com/grafana/xk6) itself requires only the following command: + +```bash +$ go install go.k6.io/xk6/cmd/xk6@latest +``` + +To confirm your installation, run `which xk6` on Linux and Mac, or `where xk6` on Windows. +Make sure the command returns a valid path. + +If not, check that you've correctly defined the`$GOPATH` environment variable and that `$GOPATH/bin` +is in your `$PATH`. See the [Go documentation](https://golang.org/cmd/go/#hdr-GOPATH_environment_variable) +for details. + +## Building your first extension + +Once installed, building a k6 binary with one or more extensions can be done with the `xk6 build` +command as follows: + +```bash +$ xk6 build latest \ + --with github.com/grafana/xk6-sql@v0.0.1 \ + --with github.com/grafana/xk6-output-prometheus-remote +``` + +Once completed, the **current directory** contains your newly built `k6` binary with +the [`xk6-sql`](https://github.com/grafana/xk6-sql) and [`xk6-output-prometheus-remote`](https://github.com/grafana/xk6-output-prometheus-remote) +extensions. + +```bash +... [INFO] Build environment ready +... [INFO] Building k6 +... [INFO] Build complete: ./k6 + +xk6 has now produced a new k6 binary which may be different than the command on your system path! +Be sure to run './k6 run ' from the '...' directory. +``` + +## Breaking down the xk6 command + +From the [xk6 documentation](https://github.com/grafana/xk6/#command-usage), the command-line usage is as follows: + +```plain +xk6 build [] + [--output ] + [--with ...] + [--replace ...] + +Flags: + --output specifies the new binary name [default: 'k6'] + --replace enables override of dependencies for k6 and extensions [default: none] + --with the extension module to be included in the binary [default: none] +``` + +> The use of `--replace` should be considered an advanced feature to be avoided unless required. + +Referring back to our executed command, note that: + +- We specify the version as `latest`, which is also the default if we hadn't supplied + a version. `latest` means that we'll build using the _latest_ source code for k6. Consider using + a stable [release version](https://github.com/grafana/k6/releases) as a best practice unless + you genuinely want the _bleeding edge_. +- With each `--with`, we specified a full GitHub URI for the extension repository. If not specifying + a version, the default is `latest` once again. Check your extension repository for stable + release versions, if available, to lock in your version as we've done with `xk6-sql@v0.0.1`. +- We did not use the `--output` option; therefore, our new binary is `k6` within the current directory. + Had we used `--output k6-extended`, our binary would be named `k6-extended` within the current + directory. If a directory is specified, then the new binary would be `k6` within + _that_ directory. If a path to a non-existent file, e.g. `/tmp/k6-extended`, this will be the + path and filename for the binary. + +Running `./k6 version` should show your build is based upon the appropriate version. + +## Building from a local repository + +Suppose now you've cloned the `xk6-sql` repository and want the local version included in your +custom binary? From the cloned directory, we would then use: + +```bash +--with github.com/grafana/xk6-sql=. +``` + +Based upon the command usage described in the previous section, this tells xk6 to use +the _current directory_ as the _replacement_ for the _module_. + +## Running your extended binary + +Now that we have our newly built k6 binary, we can run scripts using the functionalities +of the bundled extensions. + +```bash +$ ./k6 run my-script.js +``` + +> Be sure to specify the binary just built in the current directory as `./k6`, or else +> Linux/Mac could execute another k6 binary on your system path. Windows shells will +> first search for the binary in the current directory by default. + + diff --git a/docs/sources/k6/v0.55.x/extensions/create/_index.md b/docs/sources/k6/v0.55.x/extensions/create/_index.md new file mode 100644 index 000000000..912901e0c --- /dev/null +++ b/docs/sources/k6/v0.55.x/extensions/create/_index.md @@ -0,0 +1,43 @@ +--- +title: 'Create a k6 extension' +menuTitle: 'Create an extension' +description: 'Creating k6 extensions does not have to be a daunting task, but there are some prerequisites to succeed.' +weight: 04 +--- + +# Create a k6 extension + +If you find a gap in your testing process that no k6 extension can fix, +consider building your own extension. + +These tutorials show you how to create custom JavaScript and output extensions. + +- [Create a JavaScript extension](https://grafana.com/docs/k6//extensions/create/javascript-extensions) to extend the JavaScript functionality of your script or add support for a new network protocol to test. +- [Create an Output extension](https://grafana.com/docs/k6//extensions/create/output-extensions) to process the metrics emitted by k6 or publish them to unsupported backend stores. + +## Necessary knowledge + +Anyone who can use the command line to edit files and install software should be able to follow along. +But, if you want to create an extension for more than the purposes of demonstration, +there's some background knowledge you should have: + +- You should be familiar with both Go(lang), JavaScript, and their tooling +- You should understand how the [_Go-to-JavaScript_](https://grafana.com/docs/k6//extensions/explanations/go-js-bridge) bridge works within k6 + +{{< admonition type="note" >}} + +If you maintain a public xk6 repository and wish to have your extension listed in our [registry](https://grafana.com/docs/k6//extensions/explore), +be sure to review the [requirements](https://grafana.com/docs/k6//extensions/explanations/extensions-registry#registry-requirements). + +{{< /admonition >}} + +## Avoid unneeded work + +These actions may save you the trouble of building a whole new extension when its not needed. + +- Confirm that a similar extension doesn't already exist for your use case. Take a look at + the [Extensions listing](https://grafana.com/docs/k6//extensions/explore) and the [`xk6` topic on GitHub](https://github.com/topics/xk6). +- Prefer generic solutions. For example, if you can test a system with a generic protocol like _MQTT_, prefer + [xk6-mqtt](https://github.com/pmalhaire/xk6-mqtt) over a new extension for some custom protocol. +- Lean toward writing pure JavaScript libraries over building an extension in Go. + A JavaScript library will be better supported, more straightforward, and reusable than an extension. diff --git a/docs/sources/k6/v0.55.x/extensions/create/javascript-extensions.md b/docs/sources/k6/v0.55.x/extensions/create/javascript-extensions.md new file mode 100644 index 000000000..bff3ba2e7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/extensions/create/javascript-extensions.md @@ -0,0 +1,348 @@ +--- +title: 'JavaScript Extensions' +description: 'Follow these steps to build a JS extension for k6.' +weight: 01 +--- + +# JavaScript Extensions + +Take advantage of Go's speed, power, and efficiency while providing the flexibility of using JavaScript APIs +within your test scripts. + +By implementing k6 interfaces, you can close various gaps in your testing setup: + +- New network protocols +- Improved performance +- Features not supported by k6 core + +## Before you start + +To run this tutorial, you'll need the following applications installed: + +- Go +- Git + +You also need to install xk6: + +```bash +$ go install go.k6.io/xk6/cmd/xk6@latest +``` + +## Write a simple extension + +1. First, set up a directory to work in: + +```bash +$ mkdir xk6-compare; cd xk6-compare; go mod init xk6-compare +``` + +1. In the directory, make a Go file for your JavaScript extension. + +A simple JavaScript extension requires a struct that exposes methods called by the test script. + + + +```go +package compare + +import "fmt" + +// Compare is the type for our custom API. +type Compare struct{ + ComparisonResult string // textual description of the most recent comparison +} + +// IsGreater returns true if a is greater than b, or false otherwise, setting textual result message. +func (c *Compare) IsGreater(a, b int) bool { + if a > b { + c.ComparisonResult = fmt.Sprintf("%d is greater than %d", a, b) + return true + } else { + c.ComparisonResult = fmt.Sprintf("%d is NOT greater than %d", a, b) + return false + } +} +``` + +1. Register the module to use these from k6 test scripts. + +{{< admonition type="note" >}} + +k6 extensions must have the `k6/x/` prefix, +and the short name must be unique among all extensions built in the same k6 binary. + +{{< /admonition >}} + +```go +import "go.k6.io/k6/js/modules" + +// init is called by the Go runtime at application startup. +func init() { + modules.Register("k6/x/compare", new(Compare)) +} +``` + +1. Save the file as something like `compare.go`. The final code looks like this: + +{{< code >}} + +```go +package compare + +import ( + "fmt" + "go.k6.io/k6/js/modules" +) + +// init is called by the Go runtime at application startup. +func init() { + modules.Register("k6/x/compare", new(Compare)) +} + +// Compare is the type for our custom API. +type Compare struct{ + ComparisonResult string // textual description of the most recent comparison +} + +// IsGreater returns true if a is greater than b, or false otherwise, setting textual result message. +func (c *Compare) IsGreater(a, b int) bool { + if a > b { + c.ComparisonResult = fmt.Sprintf("%d is greater than %d", a, b) + return true + } else { + c.ComparisonResult = fmt.Sprintf("%d is NOT greater than %d", a, b) + return false + } +} +``` + +{{< /code >}} + +## Compile your extended k6 + +To build a k6 binary with this extension, run this command: + +```bash +$ xk6 build --with xk6-compare=. +``` + +{{< admonition type="note" >}} + +When building from source code, `xk6-compare` is the Go module name passed to `go mod init`. +Usually, this would be a URL similar to `github.com/grafana/xk6-compare`. + +{{< /admonition >}} + +## Use your extension + +Now, use the extension in a test script! + +1. Make a file with a name like `test.js` then add this code: + +{{< code >}} + +```javascript +import compare from 'k6/x/compare'; + +export default function () { + console.log(`${compare.isGreater(2, 1)}, ${compare.comparison_result}`); + console.log(`${compare.isGreater(1, 3)}, ${compare.comparison_result}`); +} +``` + +{{< /code >}} + +1. Run the test with `./k6 run test.js`. + +It should output the following: + +```shell +INFO[0000] true, 2 is greater than 1 source=console +INFO[0000] false, 1 is NOT greater than 3 source=console +``` + +## Use the advanced module API + +Suppose your extension needs access to internal k6 objects to, for example, inspect the state of the test during execution. +We will need to make slightly more complicated changes to the above example. + +Our main `Compare` struct should implement the [`modules.Instance`](https://pkg.go.dev/go.k6.io/k6/js/modules#Instance) interface +to access the [`modules.VU`](https://pkg.go.dev/go.k6.io/k6/js/modules#VU) to inspect internal k6 objects such as: + +- [`lib.State`](https://pkg.go.dev/go.k6.io/k6/lib#State), the VU state with values like the VU ID and iteration number +- [`sobek.Runtime`](https://pkg.go.dev/github.com/grafana/sobek#Runtime), the JavaScript runtime used by the VU +- a global `context.Context` containing objects like the [`lib.ExecutionState`](https://pkg.go.dev/go.k6.io/k6/lib#ExecutionState) + +Additionally, there should be a root module implementation of the [`modules.Module`](https://pkg.go.dev/go.k6.io/k6/js/modules#Module) +interface to serve as a factory of `Compare` instances for each VU. + +{{< admonition type="caution" >}} + +The significance depends on the size of your module. + +{{< /admonition >}} + +Here's what that would look like: + +{{< code >}} + +```go +package compare + +import ( + "fmt" + "go.k6.io/k6/js/modules" +) + +// init is called by the Go runtime at application startup. +func init() { + modules.Register("k6/x/compare", New()) +} + +type ( + // RootModule is the global module instance that will create module + // instances for each VU. + RootModule struct{} + + // ModuleInstance represents an instance of the JS module. + ModuleInstance struct { + // vu provides methods for accessing internal k6 objects for a VU + vu modules.VU + // comparator is the exported type + comparator *Compare + } +) + +// Ensure the interfaces are implemented correctly. +var ( + _ modules.Instance = &ModuleInstance{} + _ modules.Module = &RootModule{} +) + +// New returns a pointer to a new RootModule instance. +func New() *RootModule { + return &RootModule{} +} + +// NewModuleInstance implements the modules.Module interface returning a new instance for each VU. +func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance { + return &ModuleInstance{ + vu: vu, + comparator: &Compare{vu: vu}, + } +} + +// Compare is the type for our custom API. +type Compare struct{ + vu modules.VU // provides methods for accessing internal k6 objects + ComparisonResult string // textual description of the most recent comparison +} + +// IsGreater returns true if a is greater than b, or false otherwise, setting textual result message. +func (c *Compare) IsGreater(a, b int) bool { + if a > b { + c.ComparisonResult = fmt.Sprintf("%d is greater than %d", a, b) + return true + } else { + c.ComparisonResult = fmt.Sprintf("%d is NOT greater than %d", a, b) + return false + } +} + +// Exports implements the modules.Instance interface and returns the exported types for the JS module. +func (mi *ModuleInstance) Exports() modules.Exports { + return modules.Exports{ + Default: mi.comparator, + } +} +``` + +{{< /code >}} + +{{< admonition type="note" >}} + +Notice that we implemented the Module API and now `modules.Register` the _root module_ rather than our _Compare_ object! + +{{< /admonition >}} + +## Accessing runtime state + +At this time, we've provided access to the [`modules.VU`](https://pkg.go.dev/go.k6.io/k6/js/modules#VU) from the `Compare` +type; however, we aren't taking advantage of the methods provided. Here is a contrived example of how we can utilize the +runtime state: + +{{< code >}} + +```go +// InternalState holds basic metadata from the runtime state. +type InternalState struct { + ActiveVUs int64 `js:"activeVUs"` + Iteration int64 + VUID uint64 `js:"vuID"` + VUIDFromRuntime sobek.Value `js:"vuIDFromRuntime"` +} + +// GetInternalState interrogates the current virtual user for state information. +func (c *Compare) GetInternalState() *InternalState { + state := c.vu.State() + ctx := c.vu.Context() + es := lib.GetExecutionState(ctx) + rt := c.vu.Runtime() + + return &InternalState{ + VUID: state.VUID, + VUIDFromRuntime: rt.Get("__VU"), + Iteration: state.Iteration, + ActiveVUs: es.GetCurrentlyActiveVUsCount(), + } +} +``` + +{{< /code >}} + +Create a test script to utilize the new `getInternalState()` function as in the following: + +{{< code >}} + +```javascript +import compare from 'k6/x/compare'; + +export default function () { + const state = compare.getInternalState(); + console.log( + `Active VUs: ${state.activeVUs}, Iteration: ${state.iteration}, VU ID: ${state.vuID}, VU ID from runtime: ${state.vuIDFromRuntime}` + ); +} +``` + +{{< /code >}} + +Executing the script as `./k6 run test-state.js --vus 2 --iterations 5` will produce output similar to the following: + +```shell +INFO[0000] Active VUs: 2, Iteration: 0, VU ID: 2, VU ID from runtime: 2 source=console +INFO[0000] Active VUs: 2, Iteration: 0, VU ID: 1, VU ID from runtime: 1 source=console +INFO[0000] Active VUs: 2, Iteration: 1, VU ID: 2, VU ID from runtime: 2 source=console +INFO[0000] Active VUs: 2, Iteration: 1, VU ID: 1, VU ID from runtime: 1 source=console +INFO[0000] Active VUs: 2, Iteration: 2, VU ID: 2, VU ID from runtime: 2 source=console +``` + +> For a more extensive usage example of this API, look at the +> [`k6/execution`](https://github.com/grafana/k6/blob/master/js/modules/k6/execution/execution.go) module. + +## Things to keep in mind + +- The code in the `default` function (or another function specified by + [`exec`](https://grafana.com/docs/k6//using-k6/scenarios#options)) will be executed many + times during a test run and possibly in parallel by thousands of VUs. + Any operation of your extension should therefore be performant + and [thread-safe](https://en.wikipedia.org/wiki/Thread_safety). +- Any _heavy_ initialization should be done in the [init context](https://grafana.com/docs/k6//javascript-api/init-context), + if possible, and not as part of the `default` function execution. +- Use the registry's [`NewMetric`](https://pkg.go.dev/go.k6.io/k6/metrics#Registry.NewMetric) method to create + custom metrics; to emit them, use [`metrics.PushIfNotDone()`](https://pkg.go.dev/go.k6.io/k6/metrics#PushIfNotDone). + +> Questions? Feel free to join the discussion on extensions in the [k6 Community Forum](https://community.grafana.com/c/grafana-k6/extensions/82). + +Next, create an [Output extension](https://grafana.com/docs/k6//extensions/create/output-extensions) to publish test metrics +to a destination not already supported by k6. diff --git a/docs/sources/k6/v0.55.x/extensions/create/output-extensions.md b/docs/sources/k6/v0.55.x/extensions/create/output-extensions.md new file mode 100644 index 000000000..a7fbcf0f7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/extensions/create/output-extensions.md @@ -0,0 +1,248 @@ +--- +title: 'Output Extensions' +description: 'Follow these steps to build an output extension for k6.' +weight: 02 +--- + +# Output Extensions + +k6 provides many [metrics](https://grafana.com/docs/k6//using-k6/metrics) and [output formats](https://grafana.com/docs/k6//results-output/), but it cannot directly support all possibilities. +To store or alter metrics captured during an active k6 test, +you can create a custom output extension. + +Output extension binaries can use the `--out` flag to send metrics to a custom place. +Some potential reasons for a custom extension could include: + +- To support a time-series database not already supported +- To add derived metrics data for storage +- To filter metrics to only the data you care about + +Like [JavaScript extensions](https://grafana.com/docs/k6//extensions/create/javascript-extensions), +output extensions rely on the extension author to implement specific APIs. + +## Before you start: + +To run this tutorial, you'll need the following applications installed: + +- Go +- Git + +You also need to install xk6: + +```bash +$ go install go.k6.io/xk6/cmd/xk6@latest +``` + +## Write a simple extension + +1. Set up a directory to work in. + +```bash +$ mkdir xk6-output-logger; cd xk6-output-logger; go mod init xk6-output-logger +``` + +1. The core of an Output extension is a struct that implements the [`output.Output`](https://pkg.go.dev/go.k6.io/k6/output#Output) + interface. + +Create a simple example that outputs each set of metrics to the console as received by the `AddMetricSamples(samples []metrics.SampleContainer)` +method of the output interface. + +```go +package log + +import ( + "fmt" + "strings" + "time" + + "go.k6.io/k6/metrics" + "go.k6.io/k6/output" +) + +// AddMetricSamples receives metric samples from the k6 Engine as they're emitted. +func (l *Logger) AddMetricSamples(samples []metrics.SampleContainer) { + for _, sample := range samples { + all := sample.GetSamples() + fmt.Fprintf(l.out, "%s [%s]\n", all[0].GetTime().Format(time.RFC3339Nano), metricKeyValues(all)) + } +} + +// metricKeyValues returns a string of key-value pairs for all metrics in the sample. +func metricKeyValues(samples []metrics.Sample) string { + names := make([]string, 0, len(samples)) + for _, sample := range samples { + names = append(names, fmt.Sprintf("%s=%v", sample.Metric.Name, sample.Value)) + } + return strings.Join(names, ", ") +} +``` + +1. Register the module to use these from k6 test scripts. + +```go +import "go.k6.io/k6/output" + +// init is called by the Go runtime at application startup. +func init() { + output.RegisterExtension("logger", New) +} +``` + +{{< admonition type="note" >}} + +You must use the registered with the `-o`, or `--out` flag when running k6! + +{{< /admonition >}} + +The final extension code looks like this: + +{{< code >}} + +```go +package log + +import ( + "fmt" + "io" + "strings" + "time" + + "go.k6.io/k6/metrics" + "go.k6.io/k6/output" +) + +// init is called by the Go runtime at application startup. +func init() { + output.RegisterExtension("logger", New) +} + +// Logger writes k6 metric samples to stdout. +type Logger struct { + out io.Writer +} + +// New returns a new instance of Logger. +func New(params output.Params) (output.Output, error) { + return &Logger{params.StdOut}, nil +} + +// Description returns a short human-readable description of the output. +func (*Logger) Description() string { + return "logger" +} + +// Start initializes any state needed for the output, establishes network +// connections, etc. +func (o *Logger) Start() error { + return nil +} + +// AddMetricSamples receives metric samples from the k6 Engine as they're emitted. +func (l *Logger) AddMetricSamples(samples []metrics.SampleContainer) { + for _, sample := range samples { + all := sample.GetSamples() + fmt.Fprintf(l.out, "%s [%s]\n", all[0].GetTime().Format(time.RFC3339Nano), metricKeyValues(all)) + } +} + +// metricKeyValues returns a string of key-value pairs for all metrics in the sample. +func metricKeyValues(samples []metrics.Sample) string { + names := make([]string, 0, len(samples)) + for _, sample := range samples { + names = append(names, fmt.Sprintf("%s=%v", sample.Metric.Name, sample.Value)) + } + return strings.Join(names, ", ") +} + +// Stop finalizes any tasks in progress, closes network connections, etc. +func (*Logger) Stop() error { + return nil +} +``` + +{{< /code >}} + +Notice a couple of things: + +- The module initializer `New()` receives an instance of + [`output.Params`](https://pkg.go.dev/go.k6.io/k6/output#Params). + With this object, the extension can access the output-specific configuration, + interfaces to the filesystem, synchronized stdout and stderr, and more. + +- `AddMetricSamples` in this example writes to stdout. This output might have + to be buffered and flushed periodically in a real-world scenario to avoid memory + leaks. Below we'll discuss some helpers you can use for that. + +## Compile your extended k6 + +To build a k6 binary with this extension, run: + +```bash +$ xk6 build --with xk6-output-logger=. +``` + +{{< admonition type="note" >}} + +`xk6-output-logger` is the Go module name passed to `go mod init` + +Usually, this would be a URL similar to `github.com/grafana/xk6-output-logger`. + +{{< /admonition >}} + +## Use your extension + +Now we can use the extension with a test script. + +1. In new JavaScript file, make some simple test logic. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export default function () { + http.get('https://test-api.k6.io'); + sleep(0.5); +} +``` + +{{< /code >}} + +1. Now, run the test. + +```bash +$ ./k6 run test.js --out logger --quiet --no-summary --iterations 2 +``` + +{{< admonition type="note" >}} + +The `--out logger` argument tells k6 to use your custom output. The flag +`--quiet --no-summary` configures k6 to show only custom output. + +{{< /admonition >}} + +Your output should look something like this: + +```shell +2022-07-01T08:55:09.59272-05:00 [http_reqs=1, http_req_duration=117.003, http_req_blocked=558.983, http_req_connecting=54.135, http_req_tls_handshaking=477.198, http_req_sending=0.102, http_req_waiting=116.544, http_req_receiving=0.357, http_req_failed=0] +2022-07-01T08:55:09.917036-05:00 [vus=1, vus_max=1] +2022-07-01T08:55:10.094196-05:00 [data_sent=446, data_received=21364, iteration_duration=1177.505083, iterations=1] +2022-07-01T08:55:10.213926-05:00 [http_reqs=1, http_req_duration=119.122, http_req_blocked=0.015, http_req_connecting=0, http_req_tls_handshaking=0, http_req_sending=0.103, http_req_waiting=118.726, http_req_receiving=0.293, http_req_failed=0] +2022-07-01T08:55:10.715323-05:00 [data_sent=102, data_received=15904, iteration_duration=620.862459, iterations=1] +``` + +## Things to keep in mind + +- Output structs can optionally implement additional interfaces that allow them to + receive [thresholds](https://pkg.go.dev/go.k6.io/k6/output#WithThresholds) or + [run-status updates](https://pkg.go.dev/go.k6.io/k6/output#WithRunStatusUpdates) + and even [interrupt a test](https://pkg.go.dev/go.k6.io/k6/output#WithTestRunStop). +- Consider using [`output.SampleBuffer`](https://pkg.go.dev/go.k6.io/k6/output#SampleBuffer) + and [`output.PeriodicFlusher`](https://pkg.go.dev/go.k6.io/k6/output#PeriodicFlusher) + to improve performance given the large amounts of data produced by k6. Refer to + [`statsd` output](https://github.com/grafana/k6/blob/master/output/statsd/output.go) for an example. +- Use the [project template](https://github.com/grafana/xk6-output-template) as a starting point + for your output extension. + +> Questions? Feel free to join the discussion on extensions in the [k6 Community Forum](https://community.grafana.com/c/grafana-k6/extensions/82). diff --git a/docs/sources/k6/v0.55.x/extensions/explanations/_index.md b/docs/sources/k6/v0.55.x/extensions/explanations/_index.md new file mode 100644 index 000000000..b8ac55466 --- /dev/null +++ b/docs/sources/k6/v0.55.x/extensions/explanations/_index.md @@ -0,0 +1,10 @@ +--- +title: Explanations +weight: 05 +--- + +# Explanations + + + +{{< section >}} diff --git a/docs/sources/k6/v0.55.x/extensions/explanations/extension-graduation.md b/docs/sources/k6/v0.55.x/extensions/explanations/extension-graduation.md new file mode 100644 index 000000000..1a8e4063e --- /dev/null +++ b/docs/sources/k6/v0.55.x/extensions/explanations/extension-graduation.md @@ -0,0 +1,56 @@ +--- +title: Extension Graduation +description: Some extensions are created with the intent to become a part of core of k6. +weight: 03 +--- + +# Extension Graduation + +Some _Go_ extensions may one day be available within the k6 binary. +These select extensions pass through different phases to become core functionality. + +This graduation process benefits both users and developers. +k6 users can access new features and provide feedback to influence its developments. +k6 developers meanwhile can iterate quickly and respond to feedback without worrying about breaking changes. + +A _core-bound_ extension passes through the following phases: +![Extension graduation](/media/docs/k6-oss/extension-graduation.png) + +### Extension + +Most extensions in the k6 ecosystem remain _extensions_ requiring [xk6](https://github.com/grafana/xk6) to incorporate the custom functionality. +These extensions might be provided by Grafana or by the community, and _may_ be included in the [Extensions Registry](https://grafana.com/docs/k6//extensions/explore). + +{{< admonition type="note" >}} + +Only Grafana-controlled extensions may progress beyond the _extension_ phase to become _experimental_ or _core modules_. + +{{< /admonition >}} + +### Experimental Module + +This phase is the first exposure to core k6. +The extension is still maintained outside the core of k6 but imported as a Go module, no longer requiring xk6. + +Once an extension is promoted as an _experimental module_, the extension will be removed from the [extension listing](https://grafana.com/docs/k6//extensions/explore). +At this time, documentation for the functionality will be provided in [k6 API](https://grafana.com/docs/k6//javascript-api/k6-experimental) and [output](https://grafana.com/docs/k6//results-output/real-time) for _JavaScript_ and _Output_ extensions, respectively. + +There should be a reasonably high degree of quality and stability at this point. +This phase makes the feature accessible to more users, which in turn gives k6 developers more chances to receive feedback. +The key will be to achieve a balance between usability and stability. + +{{< admonition type="caution" >}} + +Not all experimental modules will progress to become a core module! +The k6 team reserves the right to discontinue and remove any experimental module if is no longer deemed desirable. + +{{< /admonition >}} + +### Core Module + +The stabilized feature is now part of the standard k6 product as a built-in module. +An extension may be in the _experimental module_ phase for an extended time before progressing as a core module. + +The module code is in the main k6 repository, with the old extension repository archived. +Options from the _experimental module_ phase are deprecated and removed after two k6 releases, +providing time for users to upgrade scripts for the new module. diff --git a/docs/sources/k6/v0.55.x/extensions/explanations/extensions-registry.md b/docs/sources/k6/v0.55.x/extensions/explanations/extensions-registry.md new file mode 100644 index 000000000..db9cd2cd5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/extensions/explanations/extensions-registry.md @@ -0,0 +1,103 @@ +--- +title: About the Extensions Registry +description: Reasons for the registry and what is required to be included. +weight: 01 +--- + +# About the Extensions Registry + +Did you create an extension and want to share it with your fellow k6 users? +We'd love to spread word of this new feature adding to our [registry](https://grafana.com/docs/k6//extensions/explore) of available extensions. +However, before an extension is added to the registry, we must ensure that it is maintained to the registry standard. + +Our desire is to provide the best developer experience when using k6. +This extends to the extensions ecosystem as well. +The adaptability provided by k6 extensions opens a wide array of potential use cases. + +To ensure quality, we need a well-maintained, curated listing of extensions. +Our pledge to the community is to make our best attempt to ensure the listed projects meet certain standards. +While we cannot guarantee the quality of community-provided extensions, we _can_ aid the evaluation by requiring certain consistencies. + +## Registry Requirements + +At minimum, each source code repository must have the following: + +- **a README file** + + The `README` must contain documentation such as the project description, build and usage instructions, as well as k6 version compatibility. + The goal is to provide enough information to quickly and easily evaluate the extension. + +- **the `xk6` topic set** + + GitHub allows setting _topics_ for a repository. + This supports querying all public repositories based upon a keyword for better discoverability, i.e. ["xk6"](https://github.com/topics/xk6). + See the [GitHub documentation](https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/classifying-your-repository-with-topics) to add topics. + +- **a non-restrictive license** + + Any open source software (OSS) license will suffice, but [Apache2](https://www.apache.org/licenses/LICENSE-2.0) is preferred. + +- **an `examples` folder with examples** + + Provide at least one script to show proper usage of your API. + If a [Docker Compose](https://docs.docker.com/compose/compose-file/) specification is provided, these could be used as integration tests to validate the extension works as intended. + +- **at least one versioned release** + + As features or fixes are ready to be consumed, create a [release](https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository). + This promotes stability by allowing a user to utilize a particular version. + Use [semantic versioning](https://semver.org/) to communicate changes as the extension evolves. + +- **builds with a recent k6 version** + + Ideally, the extension should build with the [latest release](https://github.com/grafana/k6/releases/latest). + But, it must build with a version of k6 that is no more than three releases old. + For example, if latest version of k6 is `v0.100`, the extension must build with at least version `v0.98`. + Be sure to also match the version of Go as determined by the version of k6. + +## Naming Conventions + +Some extensions may be very specific, where others are more general. +Multiple extensions may even be created for the same product with different levels of support based upon version. +By adhering to typical naming conventions, your extension name can remove some doubts as to what is supported. + +For any extension, we recommend the `xk6-` prefix as well as an optional `output-` for [Output extensions](https://grafana.com/docs/k6//extensions/create/output-extensions). +Next, provide the product or protocol name; don't be cryptic. +Ensure the usage is explicit by adopting only well-known acronyms or abbreviations if necessary. +If your extension supports only a specific version of a product, incorporate the version into the name, for example `v2`. + +As an example, suppose an extension that outputs test metrics to the _AwesomeLog_ application, and it uses only the v2 API. +In this case, say the latest v3 API is not backward-compatible. +Applying our conventions, we'd recommend naming this repository as `xk6-output-awesomelog-v2`. + +{{< admonition type="note" >}} + +Our goal is to quickly understand the intent of the extension. + +{{< /admonition >}} + +## Extension Tiers + +Extensions come from multiple sources. +To help distinguish extensions, we're now categorizing each extension into a _tier_. +Each tier definition is as follows: + +- **Official Extensions** + + _Official extensions_ are those owned and maintained by Grafana Labs. + They will have official documentation and have support provided by members of the Grafana organization. + +- **Community Extensions** + + _Community extensions_ are created and maintained by an individual or group from the community at large. + These have no implied warranty or level of support. + The Grafana team will make best-effort assistance to keep popular projects in compliance. + +## Potential for De-listing + +Given our desire to provide the best developer experience when using k6, we reserve the right to de-list any extension we deem is no longer maintaining standards. +Before any action takes place, the extension maintainers will be contacted to be given a chance to rectify the project and thus avoid de-listing. +Such contact may be in the form of GitHub issues or merge requests. + +Should any extension be de-listed, this does not constitute a permanent removal. +Any extension that has been de-listed may be reinstated once the reasons for the initial removal have been remediated. diff --git a/docs/sources/k6/v0.55.x/extensions/explanations/go-js-bridge.md b/docs/sources/k6/v0.55.x/extensions/explanations/go-js-bridge.md new file mode 100644 index 000000000..5fc85940b --- /dev/null +++ b/docs/sources/k6/v0.55.x/extensions/explanations/go-js-bridge.md @@ -0,0 +1,41 @@ +--- +title: About the Go-to-JS bridge +description: Technical details about how JavaScript works in the sobek engine. +weight: 02 +--- + +# About the Go-to-JS bridge + +All k6 and xk6 binaries have an embedded JavaScript engine, [sobek](https://github.com/grafana/sobek), +which your test scripts run on. + +You will deepen your conceptual knowledge of how your k6 extension works if you understand the _bridge_ between Go internals and the JavaScript runtime. + +## Go-to-JavaScript bridge features + +The bridge has a few features we should highlight: + +- Go method names are converted from _Pascal_ to _Camel_ case when + accessed in JS. For example, `IsGreater` becomes `isGreater`. + +- Go field names convert from _Pascal_ to _Snake_ case. For example, the struct field `ComparisonResult string` + becomes `comparison_result` in JS. + +- Field names may be explicit using `js` struct tags. For example, declaring the field as ComparisonResult string `js:"result"` + or hiding from JS using `js:"-"`. + +## Type conversion and native constructors + +The JavaScript runtime transparently converts Go types like `int64` to their JS equivalent. +For complex types where this is impossible, your script might fail with a `TypeError`, requiring you to explicitly convert +your object to a [`sobek.Object`](https://pkg.go.dev/github.com/grafana/sobek#Object) or [`sobek.Value`](https://pkg.go.dev/github.com/grafana/sobek#Value). + +```go +func (*Compare) XComparator(call sobek.ConstructorCall, rt *sobek.Runtime) *sobek.Object { + return rt.ToValue(&Compare{}).ToObject(rt) +} +``` + +The preceding snippet also demonstrates the _native constructors_ feature from sobek, where methods can become JS constructors. +Methods with this signature can create `Comparator` instances in JS with `new compare.Comparator()`. +While this is more idiomatic to JS, it still has the benefit of receiving the `sobek.Runtime`. diff --git a/docs/sources/k6/v0.55.x/extensions/explore.md b/docs/sources/k6/v0.55.x/extensions/explore.md new file mode 100644 index 000000000..7540db65b --- /dev/null +++ b/docs/sources/k6/v0.55.x/extensions/explore.md @@ -0,0 +1,296 @@ +--- +title: 'Explore k6 extensions' +menuTitle: 'Explore extensions' +weight: 01 +--- + +# Explore k6 extensions + +With over 50 available extensions, the k6 extension ecosystem has many options to meet your requirements and help you incorporate new protocol access, embed a particular client, or improve your test performance. Extensions are developed both by the k6 developers and by the open-source developer community. + +Extensions are composable; you can combine any extensions, or mix and match different test cases. You can use [Go and xk6](https://grafana.com/docs/k6//extensions/build-k6-binary-using-go/) or [Docker](https://grafana.com/docs/k6//extensions/build-k6-binary-using-docker/) to build your custom k6 binary: + +{{< code >}} + +```go-and-xk6 +xk6 build \ + --with github.com/grafana/xk6-sql@v0.0.1 \ + --with github.com/grafana/xk6-output-prometheus-remote +``` + +```docker-in-linux +docker run --rm -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" grafana/xk6 build \ + --with github.com/grafana/xk6-sql@v0.0.1 \ + --with github.com/grafana/xk6-output-prometheus-remote +``` + +{{< /code >}} + +
+ +Use the table to explore the many extensions. Questions? Feel free to join the discussion about extensions in the [k6 Community Forum](https://community.grafana.com/c/grafana-k6/extensions/82). + + + +Don't see what you need? Learn how you can [create a custom extension](https://grafana.com/docs/k6//extensions/create/). diff --git a/docs/sources/k6/v0.55.x/get-started/_index.md b/docs/sources/k6/v0.55.x/get-started/_index.md new file mode 100644 index 000000000..1c0579636 --- /dev/null +++ b/docs/sources/k6/v0.55.x/get-started/_index.md @@ -0,0 +1,10 @@ +--- +weight: 175 +title: Get started +--- + +# Get started + + + +{{< section >}} diff --git a/docs/sources/k6/v0.55.x/get-started/resources.md b/docs/sources/k6/v0.55.x/get-started/resources.md new file mode 100644 index 000000000..01650f8d4 --- /dev/null +++ b/docs/sources/k6/v0.55.x/get-started/resources.md @@ -0,0 +1,46 @@ +--- +title: k6 resources +description: 'An overview of the k6 resources beyond the k6 docs: videos, repositories, test servers, courses, and more' +weight: 05 +--- + +# k6 resources + +The docs aim to cover everything necessary to use the core k6 products in your daily operational work. +But scripting and testing are skills that take time to learn. +What's more, k6 is an extensible tool, already incorporated with many other functionalities and protocols. + +These resources help you write and run k6 tests in a safe environment and explore how to use k6 with other applications. + +## Learning + +- [Get started with k6 tutorial](https://grafana.com/docs/k6//examples/get-started-with-k6). The getting started tutorial provides some procedures for common real-life uses of k6 and does not require prior knowledge of k6 or JavaScript. +- [k6 Learn](https://github.com/grafana/k6-learn). A repository with a course and a ton of learning resources. +- [k6 OSS workshop](https://github.com/grafana/k6-oss-workshop). A 2-3 hour k6 workshop with practical k6 examples using the QuickPizza demo app. +- [k6 YouTube channel](https://www.youtube.com/playlist?list=PLDGkOdUX1UjrZM3lIHvFcKJxVgl2n4J65). Office hours, specific playlists, and other interesting videos from the community. +- [Awesome k6](https://github.com/grafana/awesome-k6). A list of awesome stuff about k6. +- [Examples](https://github.com/grafana/k6/tree/master/examples). A directory full of example k6 scripts for different use cases. + +## Community + +- [The k6 community forum](https://community.grafana.com/). Get support from the k6 team and community. +- [Get in touch](https://k6.io/community/#join-the-conversation). Slack, Meetup, Twitter, Stack Overflow, LinkedIn, and more. + +## Test servers + +If you need a place to learn k6 and test your scripts, you can use these playground/demo applications: + +- [test-api.k6.io](https://test-api.k6.io/). A simple REST and WebSocket web application. [grafana/test-api.k6.io](https://github.com/grafana/test-api.k6.io) +- [grpcbin.test.k6.io](https://grpcbin.test.k6.io/). A simple gRPC Request & Response Service. [grafana/k6-grpcbin](https://github.com/grafana/k6-grpcbin) +- [grafana/quickpizza](https://github.com/grafana/quickpizza). A simple demo web application. +- [grafana/httpbin](https://github.com/grafana/httpbin). A simple HTTP Request & Response Service. + +Note that these are shared testing environments - please avoid high-load tests. Alternatively, you can deploy and host them on your infrastructure and run the examples in the repository. + +## k6 + your favorite tool + +- [Kubernetes Operator](https://k6.io/blog/running-distributed-tests-on-k8s/). Distribute test execution across a Kubernetes cluster. +- [xk6 extensions](https://grafana.com/docs/k6//extensions). Custom k6 binaries to support the tool you need. +- [The browser recorder](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings/using-the-browser-recorder). Make test scripts from browser sessions. +- [Use TypeScript in k6 scripts](https://grafana.com/docs/k6//using-k6/javascript-typescript-compatibility-mode/) +- [Integrations](https://grafana.com/docs/k6//misc/integrations) diff --git a/docs/sources/k6/v0.55.x/get-started/results-output.md b/docs/sources/k6/v0.55.x/get-started/results-output.md new file mode 100644 index 000000000..c3239c49e --- /dev/null +++ b/docs/sources/k6/v0.55.x/get-started/results-output.md @@ -0,0 +1,105 @@ +--- +title: 'Results output' +description: 'For basic tests, the top-level summary that k6 provides might be enough. For detailed analysis, you can stream all data your test outputs to an external source.' +weight: 04 +--- + +# Results output + +As k6 generates load for your test, it also makes _metrics_ that measure the performance of the system. +Broadly, you can analyze metrics in two ways: + +- As summary statistics, in an _end-of-test_ summary report. +- In granular detail, with measurements for every data point across test (and timestamps) + +You can customize almost every aspect of result output: + +- Create custom metrics +- Configure new summary statistics and print them to any text format. +- Stream the results to one or multiple services of your choice (for example, InfluxDB or Prometheus). + +## Metrics + +**Documentation**: [Using metrics](https://grafana.com/docs/k6//using-k6/metrics) + +k6 comes with built-in metrics about the test load and the system response. +Key metrics include: + +- `http_req_duration`, the end-to-end time of all requests (that is, the total latency) +- `http_req_failed`, the total number of failed requests +- `iterations`, the total number of iterations + +## End-of-test summary + +**Documentation**: [End-of-test summary](https://grafana.com/docs/k6//results-output/end-of-test) + +By default, k6 prints summarized results to `stdout`. + +When you run a test, k6 outputs a plain-text logo, your test progress, and some test details. +After the test finishes, k6 prints the full details and summary statistics of the test metrics. + +![k6 results - console/stdout output](/media/docs/k6-oss/k6-results-stdout.png) + +The end-of-test summary shows aggregated statistical values for your result metrics, including: + +- Median and average values +- Minimum and maximum values +- p90, p95, and p99 values + +You can configure the statistics to report with the [`--summary-trend-stats`](https://grafana.com/docs/k6//using-k6/k6-options/reference#summary-trend-stats) option. +For example, this command displays only median, p95, and p99.9 values. + +```sh +k6 run --iterations=100 --vus=10 \ +--summary-trend-stats="med,p(95),p(99.9)" script.js +``` + +### Custom reports with `handleSummary()` + +For completely customized end-of-summary reports, k6 provides the `handleSummary()` function. + +At the end of the test, k6 automatically creates an object with all aggregated statistics. +The `handleSummary()` function can process this object into a custom report in any text format: JSON, HTML, XML, and whatever else. + +## Time series and external outputs + +**Documentation**: [Real-time metrics](https://grafana.com/docs/k6//results-output/real-time) + +The condensed end-of-test summary provides a top-level view of the test. +For deeper analysis, you need to look at granular time-series data, +which has metrics and timestamps for every point of the test. + +You can access time-series metrics in two ways: + +- Write them to a JSON or CSV file. +- Stream them to an external service. + +In both cases, you can use the `--out` flag and declare your export format as the flag argument. +If you want to send the metrics to multiple sources, you can use multiple flags with multiple arguments: + +```sh +k6 run \ +--out json=test.json \ +--out influxdb=http://localhost:8086/k6 +``` + +The available built-in outputs include: + + + +- [Amazon CloudWatch](https://grafana.com/docs/k6//results-output/real-time/amazon-cloudwatch) +- [Cloud](https://grafana.com/docs/k6//results-output/real-time/cloud) +- [CSV](https://grafana.com/docs/k6//results-output/real-time/csv) +- [Datadog](https://grafana.com/docs/k6//results-output/real-time/datadog) +- [Dynatrace](https://grafana.com/docs/k6//results-output/real-time/dynatrace) +- [Elasticsearch](https://grafana.com/docs/k6//results-output/real-time/elasticsearch) +- [Grafana Cloud Prometheus](https://grafana.com/docs/k6//results-output/real-time/grafana-cloud-prometheus) +- [InfluxDB](https://grafana.com/docs/k6//results-output/real-time/influxdb) +- [JSON](https://grafana.com/docs/k6//results-output/real-time/json) +- [Netdata](https://grafana.com/docs/k6//results-output/real-time/netdata) +- [New Relic](https://grafana.com/docs/k6//results-output/real-time/new-relic) +- [Prometheus](https://grafana.com/docs/k6//results-output/real-time/prometheus-remote-write) +- [TimescaleDB](https://grafana.com/docs/k6//results-output/real-time/timescaledb) +- [StatsD](https://grafana.com/docs/k6//results-output/real-time/statsd) + + diff --git a/docs/sources/k6/v0.55.x/get-started/running-k6.md b/docs/sources/k6/v0.55.x/get-started/running-k6.md new file mode 100644 index 000000000..719372a79 --- /dev/null +++ b/docs/sources/k6/v0.55.x/get-started/running-k6.md @@ -0,0 +1,241 @@ +--- +title: 'Running k6' +description: 'Follow along to learn how to run a test, add virtual users, increase the test duration, and ramp the number of requests up and down as the test runs.' +weight: 03 +--- + +# Running k6 + +Follow along to learn how to: + +1. Run a test. +2. Add virtual users. +3. Increase the test duration. +4. Ramp the number of requests up and down as the test runs. + +With these example snippets, you'll run the test with your machine's resources. +But, if you have a k6 Cloud account, you can also use the `k6 cloud run` command to outsource the test to k6 servers. + +## Before you begin + +- [Install k6](https://grafana.com/docs/k6//set-up/install-k6/) on your machine. + +## Run local tests + +To run a simple local script: + +1. Create and initialize a new script by running the following command: + + {{< code >}} + + ```linux + $ k6 new + ``` + + ```docker + $ docker run --rm -i -v $PWD:/app -w /app grafana/k6 new + ``` + + ```windows + PS C:\> docker run --rm -i -v ${PWD}:/app -w /app grafana/k6 init + ``` + + {{< /code >}} + + This command creates a new script file named `script.js` in the current directory. + You can also specify a different file name as an argument to the `k6 new` command, for example `k6 new my-test.js`. + +1. Run k6 with the following command: + + {{< code >}} + + ```linux + $ k6 run script.js + ``` + + ```docker + # When using the `k6` docker image, you can't just give the script name since + # the script file will not be available to the container as it runs. Instead + # you must tell k6 to read `stdin` by passing the file name as `-`. Then you + # pipe the actual file into the container with `<` or equivalent. This will + # cause the file to be redirected into the container and be read by k6. + + $ docker run --rm -i grafana/k6 run - cat script.js | docker run --rm -i grafana/k6 run - + ``` + + {{< /code >}} + +## Add VUs + +Now run a load test with more than one virtual user and a longer duration: + +{{< code >}} + +```linux +$ k6 run --vus 10 --duration 30s script.js +``` + +```docker +$ docker run --rm -i grafana/k6 run --vus 10 --duration 30s - cat script.js | docker run --rm -i grafana/k6 run --vus 10 --duration 30s - +``` + +{{< /code >}} + +_Running a 30-second, 10-VU load test_ + +{{< admonition type="note" >}} + +k6 runs multiple iterations in parallel with _virtual users_ (VUs). +In general terms, more virtual users means more simulated traffic. + +VUs are essentially parallel `while(true)` loops. +Scripts are written in JavaScript, as ES6 modules, +so you can break larger tests into smaller pieces or make reusable pieces as you like. + +{{< /admonition >}} + +### The init context and the default function + +For a test to run, you need to have _init code_, which prepares the test, and _VU code,_ which makes requests. + +Code in the init context defines functions and configures the test options (like `duration`). + +Every test also has a `default` function, +which defines the VU logic. + +```javascript +// init + +export default function () { + // vu code: do things here... +} +``` + +Init code runs first and is called only once per VU. +The `default` code runs as many times or as long as is configured in the test options. + +To learn more about how k6 executes, read about the [Test lifecycle](https://grafana.com/docs/k6//using-k6/test-lifecycle). + +## Set options + +Instead of typing `--vus 10` and `--duration 30s` each time you run the script, +you can set the options in your JavaScript file: + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; +export const options = { + vus: 10, + duration: '30s', +}; +export default function () { + http.get('http://test.k6.io'); + sleep(1); +} +``` + +If you run the script without flags, k6 uses the options defined in the script: + +{{< code >}} + +```linux +$ k6 run script.js +``` + +```docker +$ docker run --rm -i grafana/k6 run - cat script.js | docker run --rm -i grafana/k6 run - +``` + +{{< /code >}} + +## Ramp VUs up and down in stages + +You can ramp the number of VUs up and down during the test. +To configure ramping, use the `options.stages` property. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check, sleep } from 'k6'; + +export const options = { + stages: [ + { duration: '30s', target: 20 }, + { duration: '1m30s', target: 10 }, + { duration: '20s', target: 0 }, + ], +}; + +export default function () { + const res = http.get('https://httpbin.test.k6.io/'); + check(res, { 'status was 200': (r) => r.status == 200 }); + sleep(1); +} +``` + +{{< /code >}} + +For more granular ramp configuration, you can use [scenarios](https://grafana.com/docs/k6//using-k6/scenarios) and the `ramping-vus` executor. + +## Execution modes + +{{< admonition type="note" >}} + +Portability is a major design goal of k6. + +You can run the same test in different modes with minimal changes. + +{{< /admonition >}} + +k6 supports three execution modes to run a k6 test: local, distributed, and cloud. + +- **Local**: the test execution happens entirely on a single machine, container, or CI server. + + ```bash + k6 run script.js + ``` + +- **Distributed**: the test execution is [distributed across a Kubernetes cluster](https://k6.io/blog/running-distributed-tests-on-k8s/). + + Save the following YAML as `k6-resource.yaml`: + + ```yaml + --- + apiVersion: k6.io/v1alpha1 + kind: K6 + metadata: + name: k6-sample + spec: + parallelism: 4 + script: + configMap: + name: 'k6-test' + file: 'script.js' + ``` + + Apply the resource with the following command: + + ```bash + kubectl apply -f /path/to/k6-resource.yaml + ``` + +- **Cloud**: the test runs on [Grafana Cloud k6](https://grafana.com/docs/grafana-cloud/testing/k6/get-started/run-cloud-tests-from-the-cli/). + + ```bash + k6 cloud run script.js + ``` + + Additionally, cloud-based solutions can run cloud tests on your [own cloud infrastructure](https://grafana.com/docs/grafana-cloud/testing/k6/author-run/private-load-zone-v2/), and accept the test results from a [local](https://grafana.com/docs/k6//results-output/real-time/cloud) or [distributed test](https://github.com/grafana/k6-operator#k6-cloud-output). diff --git a/docs/sources/k6/v0.55.x/grafana-cloud-k6/_index.md b/docs/sources/k6/v0.55.x/grafana-cloud-k6/_index.md new file mode 100644 index 000000000..5c0965fe6 --- /dev/null +++ b/docs/sources/k6/v0.55.x/grafana-cloud-k6/_index.md @@ -0,0 +1,10 @@ +--- +weight: 1000 +title: Grafana Cloud k6 +--- + +# Grafana Cloud k6 + +Grafana Cloud k6 is a performance-testing application that's part of Grafana Cloud. It lets you leverage all of the k6 capabilities, while Grafana handles the infrastructure work of scaling servers, handling distributed load zones, and storing and aggregating metrics from your tests. + +Visit the [Grafana Cloud k6 documentation](https://grafana.com/docs/grafana-cloud/testing/k6/) for more information. diff --git a/docs/sources/k6/v0.55.x/javascript-api/_index.md b/docs/sources/k6/v0.55.x/javascript-api/_index.md new file mode 100644 index 000000000..796c0e31e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/_index.md @@ -0,0 +1,86 @@ +--- +title: JavaScript API +weight: 600 +--- + +# JavaScript API + +The list of k6 modules natively supported in your k6 scripts. + +## Init context + +{{< docs/shared source="k6" lookup="javascript-api/init-context.md" version="" >}} + +## import.meta + +{{< docs/shared source="k6" lookup="javascript-api/import.meta.md" version="" >}} + +## k6 + +{{< docs/shared source="k6" lookup="javascript-api/k6.md" version="" >}} + +## k6/browser + +The [`k6/browser` module](https://grafana.com/docs/k6//javascript-api/k6-experimental) provides browser-level APIs to interact with browsers and collect frontend performance metrics as part of your k6 tests. + +{{< docs/shared source="k6" lookup="javascript-api/k6-browser-module.md" version="" >}} + +{{< docs/shared source="k6" lookup="javascript-api/k6-browser-api.md" version="" >}} + +## k6/crypto + +{{< docs/shared source="k6" lookup="javascript-api/k6-crypto.md" version="" >}} + +## k6/data + +{{< docs/shared source="k6" lookup="javascript-api/k6-data.md" version="" >}} + +## k6/encoding + +{{< docs/shared source="k6" lookup="javascript-api/k6-encoding.md" version="" >}} + +## k6/execution + +{{< docs/shared source="k6" lookup="javascript-api/k6-execution-intro.md" version="" >}} + +## k6/experimental + +[`k6/experimental` modules](https://grafana.com/docs/k6//javascript-api/k6-experimental) are stable modules that may introduce breaking changes. Once they become fully stable, they may graduate to become k6 core modules. + +{{< docs/shared source="k6" lookup="javascript-api/k6-experimental.md" version="" >}} + +## k6/html + +{{< docs/shared source="k6" lookup="javascript-api/k6-html.md" version="" >}} + +## k6/http + +{{< docs/shared source="k6" lookup="javascript-api/k6-http.md" version="" >}} + +## k6/metrics + +{{< docs/shared source="k6" lookup="javascript-api/k6-metrics.md" version="" >}} + +## k6/net/grpc + +{{< docs/shared source="k6" lookup="javascript-api/k6-net-grpc.md" version="" >}} + +## k6/timers + +{{< docs/shared source="k6" lookup="javascript-api/k6-timers.md" version="" >}} + +## k6/ws + +{{< docs/shared source="k6" lookup="javascript-api/k6-ws.md" version="" >}} + +## Error codes + +The following specific error codes are currently defined: + +{{< docs/shared source="k6" lookup="javascript-api/k6-errors-list.md" version="" >}} + +Read more about [Error codes](https://grafana.com/docs/k6//javascript-api/error-codes). + +## jslib + +{{< docs/shared source="k6" lookup="javascript-api/jslib.md" version="" >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/error-codes.md b/docs/sources/k6/v0.55.x/javascript-api/error-codes.md new file mode 100644 index 000000000..25695cd3d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/error-codes.md @@ -0,0 +1,25 @@ +--- +title: 'Error Codes' +description: 'Error codes are unique numbers that can be used to identify and handle different application and network errors more easily.' +weight: 13 +--- + +# Error Codes + +Error codes are unique numbers that can be used to identify and handle different application and network errors more easily. For the moment, these error codes are applicable only for errors that happen during HTTP requests, but they will be reused and extended to support other protocols in future k6 releases. + +When an error occurs, its code is determined and returned as both the `error_code` field of the [`http.Response`](https://grafana.com/docs/k6//javascript-api/k6-http/response) object, and also attached as the `error_code` [tag](https://grafana.com/docs/k6//using-k6/tags-and-groups) to any [metrics](https://grafana.com/docs/k6//using-k6/metrics) associated with that request. Additionally, for more details, the `error` metric tag and `http.Response` field will still contain the actual string error message. + +Error codes for different errors are as distinct as possible, but for easier handling and grouping, codes in different error categories are also grouped in broad ranges. The current error code ranges are: + +- 1000-1099 - General errors +- 1100-1199 - DNS errors +- 1200-1299 - TCP errors +- 1300-1399 - TLS errors +- 1400-1499 - HTTP 4xx errors +- 1500-1599 - HTTP 5xx errors +- 1600-1699 - HTTP/2 specific errors + +The following specific error codes are currently defined: + +{{< docs/shared source="k6" lookup="javascript-api/k6-errors-list.md" version="" >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/import.meta/_index.md b/docs/sources/k6/v0.55.x/javascript-api/import.meta/_index.md new file mode 100644 index 000000000..b14819913 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/import.meta/_index.md @@ -0,0 +1,9 @@ +--- +title: 'import.meta' +description: 'Details about the ECMAScript import meta property.' +weight: 20 +--- + +## import.meta + +{{< docs/shared source="k6" lookup="javascript-api/import.meta.md" version="" >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/import.meta/resolve.md b/docs/sources/k6/v0.55.x/javascript-api/import.meta/resolve.md new file mode 100644 index 000000000..e7e8f324a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/import.meta/resolve.md @@ -0,0 +1,32 @@ +--- +title: 'resolve( path )' +description: 'Resolve the path to a URL string in the same way an import statement does.' +--- + +# resolve( path ) + +Resolve a path to a URL string in the same way an import statement does.' + +This is useful if you want to provide a path relative to the current module to a function that uses `open` or another function that takes URLs. + +This is particularly useful as some functions within k6 are not relative to the same origin, which might lead to seemingly similar paths having different files being loaded from the filesystem. + +For more details, refer to this [issue](https://github.com/grafana/k6/issues/3857). + +| Parameter | Type | Description | +| --------- | ------ | -------------------------- | +| path | string | A file path. | + +### Example + +{{< code >}} + +```javascript +const pdfFile = open(import.meta.resolve('./path/to/file.pdf'), 'b'); + +export default function () { + // this is here to avoid an exception if run using k6 +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/init-context/_index.md b/docs/sources/k6/v0.55.x/javascript-api/init-context/_index.md new file mode 100644 index 000000000..acf3f0fae --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/init-context/_index.md @@ -0,0 +1,9 @@ +--- +title: 'Init context' +description: 'The init context (aka "init code") is code in the global context that has access to a few functions not accessible during main script execution.' +weight: 01 +--- + +# Init context + +{{< docs/shared source="k6" lookup="javascript-api/init-context.md" version="" >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/init-context/open.md b/docs/sources/k6/v0.55.x/javascript-api/init-context/open.md new file mode 100644 index 000000000..5cea3cf4c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/init-context/open.md @@ -0,0 +1,118 @@ +--- +head_title: 'JavaScript API: open' +title: 'open( filePath, [mode] )' +description: 'Opens a file and reads all the contents into memory.' +description: 'Opens a file and reads all the contents into memory.' +--- + +# open( filePath, [mode] ) + +Opens a file, reading all its contents into memory for use in the script. + +{{< admonition type="caution" >}} + +`open()` can only be called from the [init context](https://grafana.com/docs/k6//using-k6/test-lifecycle#the-init-stage). This restriction is necessary to determine the local files needed to bundle when distributing the test across multiple nodes. + +{{< /admonition >}} + +{{< admonition type="note" >}} + +`open()` often consumes a large amount of memory because every VU keeps a separate copy of the file in memory. + +To reduce the memory consumption, you can: + +- Use `open()` within a [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray), which shares the allocated file memory between VUs. +- Use [open() from the `k6/experimental/fs`](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/) module. It provides a memory-efficient way to handle file interactions in your test script and can read files in small chunks. + +{{< /admonition >}} + +| Parameter | Type | Description | +| --------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------- | +| filePath | string | The path to the file, absolute or relative, that will be read into memory. The file will only be loaded once, even when running with several VUs. | +| mode | string | By default, the contents of the file are read as text, but if you specify `b`, the file will be read as binary data instead. | + +### Returns + +| Type | Description | +| -------------------- | ----------------------------------------------------------------------------------------------- | +| string / ArrayBuffer | The contents of the file, returned as string or ArrayBuffer (if `b` was specified as the mode). | + +### Examples + +{{< code >}} + +```json +[ + { + "username": "user1", + "password": "password1" + }, + { + "username": "user2", + "password": "password2" + }, + { + "username": "user3", + "password": "password3" + } +] +``` + +{{< /code >}} + +{{< code >}} + +```javascript +import { SharedArray } from 'k6/data'; +import { sleep } from 'k6'; + +const data = new SharedArray('users', function () { + // here you can open files, and then do additional processing or generate the array with data dynamically + const f = JSON.parse(open('./users.json')); + return f; // f must be an array[] +}); + +export default () => { + const randomUser = data[Math.floor(Math.random() * data.length)]; + console.log(`${randomUser.username}, ${randomUser.password}`); + sleep(3); +}; +``` + +{{< /code >}} + +{{< code >}} + +```javascript +import { sleep } from 'k6'; + +const users = JSON.parse(open('./users.json')); // consider using SharedArray for large files + +export default function () { + const user = users[__VU - 1]; + console.log(`${user.username}, ${user.password}`); + sleep(3); +} +``` + +{{< /code >}} + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +const binFile = open('/path/to/file.bin', 'b'); + +export default function () { + const data = { + field: 'this is a standard form field', + file: http.file(binFile, 'test.bin'), + }; + const res = http.post('https://example.com/upload', data); + sleep(3); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/_index.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/_index.md new file mode 100644 index 000000000..6a7fbe5d2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/_index.md @@ -0,0 +1,9 @@ +--- +title: 'jslib' +description: 'External JavaScript libraries for k6' +weight: 15 +--- + +# jslib + +{{< docs/shared source="k6" lookup="javascript-api/jslib.md" version="" >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/EventBridgeClient/_index.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/EventBridgeClient/_index.md new file mode 100644 index 000000000..1c573b5d2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/EventBridgeClient/_index.md @@ -0,0 +1,71 @@ +--- +title: 'EventBridgeClient' +head_title: 'EventBridgeClient' +description: 'EventBridgeClient allows interacting with AWS EventBridge service' +description: 'EventBridgeClient class allows sending custom events to Amazon EventBridge so that they can be matched to rules.' +weight: 00 +--- + +# EventBridgeClient + +`EventBridgeClient` interacts with the AWS EventBridge service. + +With it, you can send custom events to Amazon EventBridge. These events can then be matched to rules defined in EventBridge. For a full list of supported operations, see [Methods](#methods). + +Both the dedicated `event-bridge.js` jslib bundle and the all-encompassing `aws.js` bundle include the `EventBridgeClient`. + +### Methods + +| Function | Description | +| :---------------------------------------------------------------------------------------------------------------- | :---------------------------------------- | +| [putEvents(input)](https://grafana.com/docs/k6//javascript-api/jslib/aws/eventbridgeclient/putevents) | Send custom events to Amazon EventBridge. | + +### Throws + +EventBridgeClient methods will throw errors in case of failure. + +| Error | Condition | +| :---------------------- | :-------------------------------------------------------------------------- | +| InvalidSignatureError | when invalid credentials were provided or the request signature is invalid. | +| EventBridgeServiceError | when AWS replied to the requested operation with an error. | + +### Examples + +{{< code >}} + +```javascript +import { + AWSConfig, + EventBridgeClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/event-bridge.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const eventBridge = new EventBridgeClient(awsConfig); + +export default async function () { + const eventDetails = { + Source: 'my.custom.source', + Detail: { key1: 'value1', key2: 'value2' }, + DetailType: 'MyDetailType', + Resources: ['arn:aws:resource1'], + }; + + const input = { + Entries: [eventDetails], + }; + + try { + await eventBridge.putEvents(input); + } catch (error) { + console.error(`Failed to put events: ${error.message}`); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/EventBridgeClient/putEvents.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/EventBridgeClient/putEvents.md new file mode 100644 index 000000000..ee5ec7d15 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/EventBridgeClient/putEvents.md @@ -0,0 +1,76 @@ +--- +title: 'putEvents' +head_title: 'EventBridgeClient.putEvents' +description: 'EventBridgeClient.putEvents sends custom events to Amazon EventBridge' +description: 'EventBridgeClient.putEvents sends custom events to Amazon EventBridge' +weight: 10 +--- + +# putEvents + +`EventBridgeClient.putEvents` sends custom events to Amazon EventBridge so that they can be matched to rules. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :-------------------------------- | :------------------------------------------------------- | +| input | [PutEventsInput](#puteventsinput) | An array of objects representing events to be submitted. | + +#### PutEventsInput + +| Parameter | Type | Description | +| :--------- | :-------------------------------------- | :------------------------------------------------------- | +| Entries | [EventBridgeEntry](#eventbridgeentry)[] | An array of objects representing events to be submitted. | +| EndpointId | string (optional) | The ID of the target to receive the event. | + +#### EventBridgeEntry + +| Parameter | Type | Description | +| :----------- | :------------------ | :------------------------------------------------------------------------------------------------------------------------------------------------ | +| Source | string | The source of the event. | +| Detail | object | A JSON object containing event data. | +| DetailType | string | Free-form string used to decide what fields to expect in the event detail. | +| Resources | string[] (optional) | AWS resources, identified by Amazon Resource Name (ARN), which the event primarily concerns. | +| EventBusName | string (optional) | The event bus that will receive the event. If you omit this, the default event bus is used. Only the AWS account that owns a bus can write to it. | + +### Returns + +| Type | Description | +| :-------------- | :---------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the events have been sent to Amazon EventBridge. | + +### Example + +{{< code >}} + +```javascript +import { + AWSConfig, + EventBridgeClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/event-bridge.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const eventBridge = new EventBridgeClient(awsConfig); +const eventEntry = { + Source: 'my.source', + Detail: { + key: 'value', + }, + DetailType: 'MyDetailType', + Resources: ['resource-arn'], +}; + +export default async function () { + await eventBridge.putEvents({ + Entries: [eventEntry], + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/KMSDataKey.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/KMSDataKey.md new file mode 100644 index 000000000..520492726 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/KMSDataKey.md @@ -0,0 +1,59 @@ +--- +title: 'KMSDataKey' +slug: 'kmsdatakey' +head_title: 'KMSDataKey' +description: 'KMSDataKey is returned by the KMSClient.*DataKey methods that query KMS data keys' +weight: 20 +--- + +# KMSDataKey + +`KMSClient.*DataKey` methods, querying Key Management Service data keys, return some KMSDataKey instances. +The KMSDataKey object describes an Amazon Key Management Service data key. +For instance, the [`generateDataKey`](https://grafana.com/docs/k6//javascript-api/jslib/aws/kmsclient/generatedatakey/) returns the generated KMSDataKey object. + +| Name | Type | Description | +| :-------------------------- | :----- | :-------------------------------------------------------------------------------------------------------------------------------------------------- | +| `KMSDataKey.id` | string | The identifier of the Key Management Service key that encrypted the data key. | +| `KMSDataKey.ciphertextBlob` | string | The base64-encoded encrypted copy of the data key. | +| `KMSDataKey.plaintext` | string | The plain text data key. Use this data key to encrypt your data outside of Key Management Service. Then, remove it from memory as soon as possible. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + KMSClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/kms.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const kms = new KMSClient(awsConfig); +const testKeyId = 'e67f95-4c047567-4-a0b7-62f7ce8ec8f48'; + +export default async function () { + // List the KMS keys the AWS authentication configuration + // gives us access to. + const keys = await kms.listKeys(); + + // If our test key does not exist, abort the execution. + if (keys.filter((b) => b.keyId === testKeyId).length == 0) { + exec.test.abort(); + } + + // Generate a data key from the KMS key. + const key = await kms.generateDataKey(testKeyId, 32); +} +``` + +_A k6 script that generating a data key from an AWS Key Management Service key_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/KMSKey.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/KMSKey.md new file mode 100644 index 000000000..5bea77d69 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/KMSKey.md @@ -0,0 +1,53 @@ +--- +title: 'KMSKey' +head_title: 'KMSKey' +description: 'KMSKey is returned by the KMSClient.* methods that query KMS keys' +description: 'KMSKey is returned by the KMSClient.* methods that query KMS keys' +weight: 20 +--- + +# KMSKey + +`KMSClient.*` methods querying Key Management Service keys return some `KMSKey` instances. Namely, `listKeys()` returns an array of `KMSKey` objects. The `KMSKey` object describes an Amazon Key Management Service key. + +| Name | Type | Description | +| :-------------- | :----- | :--------------------------- | +| `KMSKey.keyId` | string | Unique identifier of the key | +| `KMSKey.keyArn` | string | ARN of the key | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + KMSClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/kms.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const kms = new KMSClient(awsConfig); +const testKeyId = 'e67f95-4c047567-4-a0b7-62f7ce8ec8f48'; + +export default async function () { + // List the KMS keys the AWS authentication configuration + // gives us access to. + const keys = await kms.listKeys(); + + // If our test key does not exist, abort the execution. + if (keys.filter((b) => b.keyId === testKeyId).length == 0) { + exec.test.abort(); + } +} +``` + +_A k6 script querying the user's Key Management Service keys and verifying their test key exists_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/_index.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/_index.md new file mode 100644 index 000000000..0fc528e54 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/_index.md @@ -0,0 +1,75 @@ +--- +title: 'KMSClient' +head_title: 'KMSClient' +description: 'KMSClient allows interacting with the AWS Key Management Service' +description: 'KMSClient allows interacting with the AWS Key Management Service' +weight: 00 +--- + +# KMSClient + +{{< docs/shared source="k6" lookup="blocking-aws-blockquote.md" version="" >}} + +`KMSClient` interacts with the AWS Key Management Service. + +With it, the user can list all the Key Management Service keys in the caller's AWS account and region. They can also generate symmetric data keys to use outside of AWS Key Management Service. + +Both the dedicated `kms.js` jslib bundle and the all-encompassing `aws.js` bundle include the `KMSClient`. + +### Methods + +| Function | Description | +| :------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------- | +| [listKeys](https://grafana.com/docs/k6//javascript-api/jslib/aws/kmsclient/listkeys) | List the all the Key Management Service keys in the caller's AWS account and region. | +| [generateDataKey](https://grafana.com/docs/k6//javascript-api/jslib/aws/kmsclient/generatedatakey) | Generate a symmetric data key for use outside of the AWS Key Management Service. | + +### Throws + +`KMSClient` methods throw errors in case of failure. + +| Error | Condition | +| :---------------------- | :-------------------------------------------------------- | +| `InvalidSignatureError` | when using invalid credentials | +| `KMSServiceError` | when AWS replied to the requested operation with an error | + +### Example + +{{< code >}} + +```javascript +import { textSummary } from 'https://jslib.k6.io/k6-summary/0.0.2/index.js'; + +import { + AWSConfig, + KMSClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/kms.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const kms = new KMSClient(awsConfig); +const keyAlias = 'alias/k6-key'; + +export async function setup() { + // Create a symmetric data key + return { + dataKey: await kms.generateDataKey(keyAlias, 32), + }; +} + +export default async function (data) { + // Use the data key to encrypt data +} + +export function handleSummary(data) { + return { + 'stdout': textSummary(data, { indent: ' ', enableColors: true }), + './test-run.key': data.dataKey, + }; +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/generateDataKey.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/generateDataKey.md new file mode 100644 index 000000000..0bfa53897 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/generateDataKey.md @@ -0,0 +1,64 @@ +--- +aliases: + - ./kmsclient-generatedatakey/ # /docs/k6//javascript-api/jslib/aws/kmsclient/kmsclient-generatedatakey/ +title: 'generateDataKey' +description: 'KMSClient.generateDataKey generates a symmetric data key for use outside of the AWS Key Management Service' +weight: 10 +--- + +# generateDataKey + +`KMSClient.generateDataKey` generates a symmetric data key for use outside of the AWS Key Management Service. + +### Parameters + +| Name | Type | Description | +| :----- | :----- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `id` | string | The identifier of the key. This can be either the key ID or the Amazon Resource Name (ARN) of the key. | +| `size` | number | The length of the data key. For example, use the value 64 to generate a 512-bit data key (64 bytes is 512 bits). For 256-bit (32-byte) data keys, use the value 32, instead. | + +### Returns + +| Type | Description | +| :------------------------------------------------------------------------------------------------------------ | :-------------------------------------------------------------------------------------------------------------------------------------- | +| Promise<[KMSDataKey](https://grafana.com/docs/k6//javascript-api/jslib/aws/kmsclient/kmsdatakey)> | A Promise that fulfills with a [KMSDataKey](https://grafana.com/docs/k6//javascript-api/jslib/aws/kmsclient/kmskey) object. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + KMSClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/kms.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const kms = new KMSClient(awsConfig); +const testKeyId = 'e67f95-4c047567-4-a0b7-62f7ce8ec8f48'; + +export default async function () { + // List the KMS keys the AWS authentication configuration + // gives us access to. + const keys = await kms.listKeys(); + + // If our test key does not exist, abort the execution. + if (keys.filter((b) => b.keyId === testKeyId).length == 0) { + exec.test.abort(); + } + + // Generate a data key from the KMS key. + const key = await kms.generateDataKey(testKeyId, 32); +} +``` + +_A k6 script that generating a data key from an AWS Key Management Service key_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/listKeys.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/listKeys.md new file mode 100644 index 000000000..8675ac603 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/KMSClient/listKeys.md @@ -0,0 +1,54 @@ +--- +title: 'listKeys' +head_title: 'KMSClient.listKeys()' +description: "KMSClient.listKeys lists all the KMS keys in the caller's AWS account and region" +description: "KMSClient.listKeys lists all the KMS keys in the caller's AWS account and region" +weight: 10 +--- + +# listKeys + +`KMSClient.listKeys()` lists all the Key Management Service keys in the caller's AWS account and region. + +### Returns + +| Type | Description | +| :------------------------------------------------------------------------------------------------------ | :----------------------------------------------------------------------------------------------------------------------------------------------- | +| Promise<[KMSKey[]](https://grafana.com/docs/k6//javascript-api/jslib/aws/kmsclient/kmskey)> | A Promise that fulfills with an array of [`KMSKey`](https://grafana.com/docs/k6//javascript-api/jslib/aws/kmsclient/kmskey) objects. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + KMSClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/kms.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const kms = new KMSClient(awsConfig); +const testKeyId = 'e67f95-4c047567-4-a0b7-62f7ce8ec8f48'; + +export default async function () { + // List the KMS keys the AWS authentication configuration + // gives us access to. + const keys = await kms.listKeys(); + + // If our test key does not exist, abort the execution. + if (keys.filter((b) => b.keyId === testKeyId).length == 0) { + exec.test.abort(); + } +} +``` + +_A k6 script querying the user's Key Management Service keys and verifying their test key exists_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/LambdaClient/_index.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/LambdaClient/_index.md new file mode 100644 index 000000000..3e2e34c72 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/LambdaClient/_index.md @@ -0,0 +1,61 @@ +--- +title: 'LambdaClient' +head_title: 'LambdaClient' +description: 'LambdaClient allows interacting with the AWS Lambda service' +weight: 00 +--- + +# LambdaClient + +{{< docs/shared source="k6" lookup="blocking-aws-blockquote.md" version="" >}} + +`LambdaClient` interacts with [AWS Lambda](https://aws.amazon.com/lambda/). With it, you can invoke a Lambda function. + +Both the dedicated `lambda.js` jslib bundle and the all-encompassing `aws.js` bundle include the `LambdaClient`. + +### Methods + +| Function | Description | +| :------------------------------------------------------------------------------------------------------------------------ | :------------------------------ | +| [invoke(name, payload, [options])](https://grafana.com/docs/k6//javascript-api/jslib/aws/lambdaclient/invoke) | Invokes an AWS Lambda function. | + +### Throws + +`LambdaClient` methods will throw errors in case of failure. + +| Error | Condition | +| :-------------------- | :---------------------------------------------------------- | +| InvalidSignatureError | When invalid credentials were provided. | +| LambdaInvocationError | When AWS replied to the requested invocation with an error. | + +### Examples + +{{< code >}} + +```javascript +import { + AWSConfig, + LambdaClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/lambda.js'; +import { check } from 'k6'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const lambdaClient = new LambdaClient(awsConfig); + +export default async function () { + const response = await lambdaClient.invoke('add-numbers', JSON.stringify({ x: 1, y: 2 })); + + check(response, { + 'status is 200': (r) => r.statusCode === 200, + 'payload is 3': (r) => r.payload === 3, + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/LambdaClient/invoke.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/LambdaClient/invoke.md new file mode 100644 index 000000000..6dde961fd --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/LambdaClient/invoke.md @@ -0,0 +1,79 @@ +--- +title: 'invoke' +head_title: 'LambdaClient.invoke(name, payload, [options])' +description: 'LambdaClient.invoke invokes an AWS Lamba function' +weight: 10 +--- + +# LambdaClient.invoke(name, payload, [options]) + +`LambdaClient.invoke` invokes an AWS Lambda function. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :-------------------------------------- | :---------------------------------------------- | +| name | string | Name of the function to invoke. | +| payload | string | Payload to send to the function being invoked. | +| options | [InvocationOptions](#invocationoptions) | Additional options to customize the invocation. | + +#### InvocationOptions + +InvocationOptions is an object used to customize the invocation of a Lambda function. +It has the following properties: + +| Property | Type | Description | +| :------------- | :---------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| invocationType | string (optional) | Specifies the invocation type (synchronous or asynchronous). Possible values are: `RequestResponse`, which invokes the function synchronously, `Event`, which invokes the function asynchronously, and `DryRun` which validates the parameter values and verifies that the user or role has permission to invoke the function. Default is `RequestResponse`. | +| logType | string (optional) | Specifies the type of log to include in the response. Set to `Tail` to include the execution log in the response. Applies to synchronously invoked functions only. Default is `None`. | +| clientContext | string (optional) | Client context to pass to the function. Can be made up of up to 3,583 bytes of base64-encoded data. Default is `null`. | +| qualifier | string (optional) | Version or alias of the function to invoke. | + +### Returns + +| Type | Description | +| :------------------------------------------------- | :---------------------------------------------------------------- | +| Promise<[InvocationResponse](#invocationresponse)> | A promise that fulfills when the object has been deleted from S3. | + +#### InvocationResponse + +InvocationResponse is an object that represents the response of an invocation. It has the following properties: + +| Property | Type | Description | +| :-------------- | :---------------- | :----------------------------------------- | +| statusCode | number | Status code of the invocation. | +| executedVersion | string (optional) | The version of the function that executed. | +| logResult | string (optional) | Log result of the invocation. | +| payload | string (optional) | Payload returned by the function. | + +### Example + +{{< code >}} + +```javascript +import { + AWSConfig, + LambdaClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/lambda.js'; +import { check } from 'k6'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const lambdaClient = new LambdaClient(awsConfig); + +export default async function () { + const response = await lambdaClient.invoke('add-numbers', JSON.stringify({ x: 1, y: 2 })); + + check(response, { + 'status is 200': (r) => r.statusCode === 200, + 'payload is 3': (r) => r.payload === 3, + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/Bucket.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/Bucket.md new file mode 100755 index 000000000..51460775b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/Bucket.md @@ -0,0 +1,45 @@ +--- +title: 'Bucket' +head_title: 'Bucket' +description: 'Bucket is returned by the S3Client.* methods who query S3 buckets.' +weight: 20 +--- + +# Bucket + +Bucket is returned by the S3Client.\* methods that query S3 buckets. Namely, `listBuckets()` returns an array of Bucket objects. The Bucket object describes an Amazon S3 bucket. + +| Name | Type | Description | +| :-------------------- | :----- | :--------------------------- | +| `Bucket.name` | string | The S3 bucket's name | +| `Bucket.creationDate` | Date | The S3 bucket's creationDate | + +### Example + +{{< code >}} + +```javascript +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const s3 = new S3Client(awsConfig); + +export default async function () { + // List the buckets the AWS authentication configuration + // gives us access to. + const buckets = await s3.listBuckets(); + console.log(JSON.stringify(buckets)); +} +``` + +_A k6 script that will query the user's S3 buckets and print all of their metadata_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/Object.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/Object.md new file mode 100755 index 000000000..c03f51af5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/Object.md @@ -0,0 +1,61 @@ +--- +title: 'Object' +head_title: 'Object' +description: "Object is returned by the S3Client.* methods who query S3 buckets' objects." +weight: 20 +--- + +# Object + +Object is returned by the S3Client.\* methods that query S3 buckets' objects. Namely, [`listObjects`](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/listobjects), [`getObject`](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/getobject), [`putObject`](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/putobject), +and [`deleteObject`](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/deleteobject). The Object construct describes an Amazon S3 object. + +| Name | Type | Description | +| :-------------------- | :---------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `Object.key` | string | The S3 object's name. | +| `Object.lastModified` | number | The S3 object's last modification date. | +| `Object.etag` | string | The S3 object's `etag` is a hash of the object. The ETag reflects changes only to the contents of an object, not its metadata. The ETag may or may not be an MD5 digest of the object data. | +| `Object.size` | size | The S3 object's size in bytes. | +| `Object.storageClass` | `STANDARD` \| `REDUCED_REDUNDANCY` \| `GLACIER` \| `STANDARD_IA` \| `INTELLIGENT_TIERING` \| `DEEP_ARCHIVE` \| `OUTPOSTS` \| `GLACIER_IR` | The S3 object's class of storage used to store it. | +| `Object.data` | `string` or `bytes` or `null` | The S3 object's content. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + // listBuckets, + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const s3 = new S3Client(awsConfig); +const testBucketName = 'test-jslib-aws'; +const testFileKey = 'bonjour.txt'; + +export default async function () { + const objects = await s3.listObjects(testBucketName); + + // If our test object does not exist, abort the execution. + if (objects.filter((o) => o.key === testFileKey).length == 0) { + exec.test.abort(); + } + + // Let's download our test object and print its content + const object = await s3.getObject(testBucketName, testFileKey); + console.log(JSON.stringify(object)); +} +``` + +_A k6 script that will query a S3 bucket's objects and print its content and metadata_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/S3MultipartUpload.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/S3MultipartUpload.md new file mode 100644 index 000000000..fa94d9130 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/S3MultipartUpload.md @@ -0,0 +1,50 @@ +--- +title: 'S3MultipartUpload' +description: 'S3MultipartUpload is returned by the S3Client.createMultipartUpload method when creating a multipart upload.' +weight: 20 +--- + +# S3MultipartUpload + +S3MultipartUpload is returned by the [`createMultipartUpload(bucketName, objectKey)`](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/createmultipartupload) method when creating a [multipart upload](https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateMultipartUpload.html). + +| Name | Type | Description | +| :--------------------------- | :----- | :---------------------------- | +| `S3MultipartUpload.key` | string | The S3 Multipart object's key | +| `S3MultipartUpload.uploadId` | Date | The S3 Multipart upload Id | + +### Example + +{{< code >}} + +```javascript +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const s3 = new S3Client(awsConfig); + +const testBucketName = 'test-jslib-aws'; +const testFileKey = 'multipart.txt'; + +export default async function () { + // Initialize a multipart upload + const multipartUpload = await s3.createMultipartUpload(testBucketName, testFileKey); + console.log(multipartUpload.uploadId); + + // Abort multipart upload + await s3.abortMultipartUpload(testBucketName, testFileKey, multipartUpload.uploadId); +} +``` + +_A k6 script that will create a multipart upload and log the multipart `uploadId` and abort the multipart upload_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/S3Part.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/S3Part.md new file mode 100755 index 000000000..90d0b4b5a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/S3Part.md @@ -0,0 +1,86 @@ +--- +title: 'S3Part' +head_title: 'S3Part' +slug: 's3part' +description: 'S3Part is returned by the S3Client.uploadPart method when uploading a part to a multipart upload.' +weight: 20 +--- + +# S3Part + +S3Part is returned by the [`uploadPart(bucketName, objectKey, uploadId, partNumber, data)`](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/uploadpart) method when uploading a part to a multipart upload. The S3Part object describes an Amazon S3 Part. + +| Name | Type | Description | +| :-------------------- | :----- | :----------------- | +| `S3Part.partNumber` | number | The S3 Part'number | +| `S3Part.eTag ` | String | The S3 Part's etag | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; +import exec from 'k6/execution'; + +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const s3 = new S3Client(awsConfig); + +const testBucketName = 'test-jslib-aws'; +const testFileKey = 'multipart.txt'; + +export default async function () { + // Produce random bytes to upload of size ~12MB, that + // we will upload in two 6MB parts. This is done as the + // minimum part size supported by S3 is 5MB. + const bigFile = crypto.randomBytes(12 * 1024 * 1024); + + // Initialize a multipart upload + const multipartUpload = await s3.createMultipartUpload(testBucketName, testFileKey); + + // Upload the first part + const firstPartData = bigFile.slice(0, 6 * 1024 * 1024); + const firstPart = await s3.uploadPart( + testBucketName, + testFileKey, + multipartUpload.uploadId, + 1, + firstPartData + ); + + // Upload the second part + const secondPartData = bigFile.slice(6 * 1024 * 1024, 12 * 1024 * 1024); + const secondPart = await s3.uploadPart( + testBucketName, + testFileKey, + multipartUpload.uploadId, + 2, + secondPartData + ); + + // Complete the multipart upload + await s3.completeMultipartUpload(testBucketName, testFileKey, multipartUpload.uploadId, [ + firstPart, + secondPart, + ]); + + // Let's redownload it verify it's correct, and delete it + const obj = await s3.getObject(testBucketName, testFileKey); + await s3.deleteObject(testBucketName, testFileKey); +} +``` + +_A k6 script that will upload a part in a multipart upload to an S3 bucket_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/_index.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/_index.md new file mode 100644 index 000000000..49826faf3 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/_index.md @@ -0,0 +1,184 @@ +--- +title: 'S3Client' +head_title: 'S3Client' +description: 'S3Client allows interacting with AWS S3 buckets and objects' +description: 'S3Client class allows interacting with AWS S3 buckets and objects' +weight: 00 +--- + +# S3Client + +{{< docs/shared source="k6" lookup="blocking-aws-blockquote.md" version="" >}} + +`S3Client` interacts with the AWS Simple Storage Service (S3). + +With it, you can do several operations such as list buckets, list objects in a bucket, or download objects from a bucket. For a full list of supported operations, see [Methods](#methods). + +Both the dedicated `s3.js` jslib bundle and the all-encompassing `aws.js` bundle include the `S3Client`. + +### Methods + +| Function | Description | +| :-------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------- | +| [listBuckets()](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/listbuckets) | List the buckets the authenticated user has access to | +| [listObjects(bucketName, [prefix])](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/listobjects) | List the objects contained in a bucket | +| [getObject(bucketName, objectKey)](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/getobject) | Download an object from a bucket | +| [putObject(bucketName, objectKey, data)](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/putobject) | Upload an object to a bucket | +| [deleteObject(bucketName, objectKey)](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/deleteobject) | Delete an object from a bucket | +| [copyObject(sourceBucket, sourceKey, destinationBucket, destinationKey)](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/copyobject) | Copy an object from one bucket to another | +| [createMultipartUpload(bucketName, objectKey)](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/createmultipartupload) | Create a multipart upload for a given objectKey to a bucket | +| [uploadPart(bucketName, objectKey, uploadId, partNumber, data)](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/uploadpart) | Upload a part in a multipart upload | +| [completeMultipartUpload(bucketName, objectKey, uploadId, parts)](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/completemultipartupload) | Complete a previously assembled multipart upload | +| [abortMultipartUpload(bucketName, objectKey, uploadId)](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/abortmultipartupload) | Abort a multipart upload | + +### Throws + +S3 Client methods will throw errors in case of failure. + +| Error | Condition | +| :-------------------- | :--------------------------------------------------------- | +| InvalidSignatureError | when invalid credentials were provided. | +| S3ServiceError | when AWS replied to the requested operation with an error. | + +### Examples + +{{< code >}} + +```javascript +import { check } from 'k6'; +import exec from 'k6/execution'; +import http from 'k6/http'; + +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const s3 = new S3Client(awsConfig); +const testBucketName = 'test-jslib-aws'; +const testInputFileKey = 'productIDs.json'; +const testOutputFileKey = `results-${Date.now()}.json`; + +export async function setup() { + // If our test bucket does not exist, abort the execution. + const buckets = await s3.listBuckets(); + if (buckets.filter((b) => b.name === testBucketName).length == 0) { + exec.test.abort(); + } + + // If our test object does not exist, abort the execution. + const objects = await s3.listObjects(testBucketName); + if (objects.filter((o) => o.key === testInputFileKey).length == 0) { + exec.test.abort(); + } + + // Download the S3 object containing our test data + const inputObject = await s3.getObject(testBucketName, testInputFileKey); + + // Let's return the downloaded S3 object's data from the + // setup function to allow the default function to use it. + return { + productIDs: JSON.parse(inputObject.data), + }; +} + +export default async function (data) { + // Pick a random product ID from our test data + const randomProductID = data.productIDs[Math.floor(Math.random() * data.productIDs.length)]; + + // Query our ecommerce website's product page using the ID + const res = await http.asyncRequest('GET', `http://your.website.com/product/${randomProductID}/`); + check(res, { 'is status 200': res.status === 200 }); +} + +export async function handleSummary(data) { + // Once the load test is over, let's upload the results to our + // S3 bucket. This is executed after teardown. + await s3.putObject(testBucketName, testOutputFileKey, JSON.stringify(data)); +} +``` + +{{< /code >}} + +#### Multipart uploads + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; +import exec from 'k6/execution'; + +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const s3 = new S3Client(awsConfig); + +const testBucketName = 'test-jslib-aws'; +const testFileKey = 'multipart.txt'; + +export default async function () { + // List the buckets the AWS authentication configuration + // gives us access to. + const buckets = await s3.listBuckets(); + + // If our test bucket does not exist, abort the execution. + if (buckets.filter((b) => b.name === testBucketName).length == 0) { + exec.test.abort(); + } + + // Produce random bytes to upload of size ~12MB, that + // we will upload in two 6MB parts. This is done as the + // minimum part size supported by S3 is 5MB. + const bigFile = crypto.randomBytes(12 * 1024 * 1024); + + // Initialize a multipart upload + const multipartUpload = await s3.createMultipartUpload(testBucketName, testFileKey); + + // Upload the first part + const firstPartData = bigFile.slice(0, 6 * 1024 * 1024); + const firstPart = await s3.uploadPart( + testBucketName, + testFileKey, + multipartUpload.uploadId, + 1, + firstPartData + ); + + // Upload the second part + const secondPartData = bigFile.slice(6 * 1024 * 1024, 12 * 1024 * 1024); + const secondPart = await s3.uploadPart( + testBucketName, + testFileKey, + multipartUpload.uploadId, + 2, + secondPartData + ); + + // Complete the multipart upload + await s3.completeMultipartUpload(testBucketName, testFileKey, multipartUpload.uploadId, [ + firstPart, + secondPart, + ]); + + // Let's redownload it verify it's correct, and delete it + const obj = await s3.getObject(testBucketName, testFileKey); + await s3.deleteObject(testBucketName, testFileKey); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/abortMultipartUpload.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/abortMultipartUpload.md new file mode 100755 index 000000000..1452bf9bf --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/abortMultipartUpload.md @@ -0,0 +1,59 @@ +--- +title: 'abortMultipartUpload' +head_title: 'S3Client.abortMultipartUpload(bucketName, objectKey, uploadId)' +description: 'S3Client.abortMultipartUpload aborts a multipart upload to a bucket' +weight: 10 +--- + +# abortMultipartUpload + +`S3Client.abortMultipartUpload` aborts a multipart upload to an S3 bucket. + +### Parameters + +| Parameter | Type | Description | +| :--------- | :----- | :------------------------------------------------------ | +| bucketName | string | Name of the bucket to delete the multipart object from. | +| objectKey | string | Name of the multipart object to delete. | +| uploadId | number | UploadId of the multipart upload to abort. | + +### Returns + +| Type | Description | +| :-------------- | :------------------------------------------------------------------ | +| `Promise` | A promise that fulfills when the multipart upload has been aborted. | + +### Example + +{{< code >}} + +```javascript +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const s3 = new S3Client(awsConfig); + +const testBucketName = 'test-jslib-aws'; +const testFileKey = 'multipart.txt'; + +export default async function () { + // Initialize a multipart upload + const multipartUpload = await s3.createMultipartUpload(testBucketName, testFileKey); + + // Abort multipart upload + await s3.abortMultipartUpload(testBucketName, testFileKey, multipartUpload.uploadId); +} +``` + +_A k6 script that will create a multipart upload and abort the multipart_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/completeMultipartUpload.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/completeMultipartUpload.md new file mode 100755 index 000000000..60c432772 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/completeMultipartUpload.md @@ -0,0 +1,104 @@ +--- +title: 'completeMultipartUpload' +head_title: 'S3Client.completeMultipartUpload(bucketName, objectKey, uploadId, parts)' +description: 'S3Client.completeMultipartUpload uploads a multipar object to a bucket' +weight: 10 +--- + +# completeMultipartUpload + +`S3Client.completeMultipartUpload` uploads a multipart object to an S3 bucket. + +### Parameters + +| Parameter | Type | Description | +| :--------- | :------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------ | +| bucketName | string | Name of the bucket to delete the object to. | +| objectKey | string | Name of the uploaded object. | +| uploadId | number | UploadId of the multipart upload to complete. | +| parts | Array<[S3Part](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/s3part)> | The [S3Part](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/s3part)s to assemble. | + +### Returns + +| Type | Description | +| :-------------- | :-------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the multipart upload has been completed. | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; +import exec from 'k6/execution'; + +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const s3 = new S3Client(awsConfig); + +const testBucketName = 'test-jslib-aws'; +const testFileKey = 'multipart.txt'; + +export default async function () { + // List the buckets the AWS authentication configuration + // gives us access to. + const buckets = await s3.listBuckets(); + + // If our test bucket does not exist, abort the execution. + if (buckets.filter((b) => b.name === testBucketName).length == 0) { + exec.test.abort(); + } + + // Produce random bytes to upload of size ~12MB, that + // we will upload in two 6MB parts. This is done as the + // minimum part size supported by S3 is 5MB. + const bigFile = crypto.randomBytes(12 * 1024 * 1024); + + // Initialize a multipart upload + const multipartUpload = await s3.createMultipartUpload(testBucketName, testFileKey); + + // Upload the first part + const firstPartData = bigFile.slice(0, 6 * 1024 * 1024); + const firstPart = await s3.uploadPart( + testBucketName, + testFileKey, + multipartUpload.uploadId, + 1, + firstPartData + ); + + // Upload the second part + const secondPartData = bigFile.slice(6 * 1024 * 1024, 12 * 1024 * 1024); + const secondPart = await s3.uploadPart( + testBucketName, + testFileKey, + multipartUpload.uploadId, + 2, + secondPartData + ); + + // Complete the multipart upload + await s3.completeMultipartUpload(testBucketName, testFileKey, multipartUpload.uploadId, [ + firstPart, + secondPart, + ]); + + // Let's redownload it verify it's correct, and delete it + const obj = await s3.getObject(testBucketName, testFileKey); + await s3.deleteObject(testBucketName, testFileKey); +} +``` + +_A k6 script that will upload a multipart upload to an S3 bucket_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/copyObject.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/copyObject.md new file mode 100644 index 000000000..1516d2677 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/copyObject.md @@ -0,0 +1,62 @@ +--- +title: 'copyObject' +head_title: 'S3Client.copyObject' +description: 'S3Client.copyObject copies an object from a bucket to another' +weight: 10 +--- + +# copyObject + +`S3Client.copyObject` copies an object from one bucket to another. + +### Parameters + +| Parameter | Type | Description | +| :---------------- | :----- | :------------------------------------------------- | +| sourceBucket | string | Name of the bucket to copy the object from. | +| sourceKey | string | Name of the object to copy from the source bucket. | +| destinationBucket | string | Name of the bucket to copy the object to. | +| destinationKey | string | Name of the destination object. | + +### Returns + +| Type | Description | +| :-------------- | :---------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the object has been copied from one bucket to another. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const testFile = open('./bonjour.txt', 'r'); +const s3 = new S3Client(awsConfig); +const testBucketName = 'test-jslib-aws'; +const testFileKey = 'bonjour.txt'; +const testDestinationBucketName = 'test-jslib-aws-destination'; + +export default async function () { + // Let's upload our test file to the bucket + await s3.putObject(testBucketName, testFileKey, testFile); + + // Let's create our destination bucket + await s3.copyObject(testBucketName, testFileKey, testDestinationBucketName, testFileKey); +} +``` + +_A k6 script that will copy an object from a source S3 bucket to a destination bucket_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/createMultipartUpload.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/createMultipartUpload.md new file mode 100755 index 000000000..345d83496 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/createMultipartUpload.md @@ -0,0 +1,56 @@ +--- +title: 'createMultipartUpload' +head_title: 'S3Client.createMultipartUpload(bucketName, objectKey)' +description: 'S3Client.createMultipartUpload creates a multipart upload for an object key to a bucket' +weight: 10 +--- + +# createMultipartUpload + +`S3Client.createMultipartUpload` creates a new multipart upload for a given an object key in a bucket. + +| Parameter | Type | Description | +| :--------- | :----- | :------------------------------------------ | +| bucketName | string | Name of the bucket to upload the object to. | +| objectKey | string | Name of the uploaded object. | + +### Returns + +| Type | Description | +| :------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Promise<[S3MultipartUpload](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/s3multipartupload)> | A Promise that fulfills with a [S3MultipartUpload](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/s3multipartupload) representing a S3 Multipart Upload. | + +### Example + +{{< code >}} + +```javascript +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const s3 = new S3Client(awsConfig); + +const testBucketName = 'test-jslib-aws'; +const testFileKey = 'multipart.txt'; + +export default async function () { + // Initialize a multipart upload + const multipartUpload = await s3.createMultipartUpload(testBucketName, testFileKey); + + // Abort multipart upload + await s3.abortMultipartUpload(testBucketName, testFileKey, multipartUpload.uploadId); +} +``` + +_A k6 script that will create a multipart upload to an S3 bucket_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/deleteObject.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/deleteObject.md new file mode 100755 index 000000000..395eae73f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/deleteObject.md @@ -0,0 +1,61 @@ +--- +title: 'deleteObject' +head_title: 'S3Client.deleteObject(bucketName, objectKey)' +description: 'S3Client.deleteObject deletes an object from a bucket' +weight: 10 +--- + +# deleteObject + +`S3Client.deleteObject` deletes an object from a bucket. + +### Parameters + +| Parameter | Type | Description | +| :--------- | :----- | :-------------------------------------------- | +| bucketName | string | Name of the bucket to delete the object from. | +| objectKey | string | Name of the object to delete. | + +### Returns + +| Type | Description | +| :-------------- | :---------------------------------------------------------------- | +| `Promise` | A promise that fulfills when the object has been deleted from S3. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const s3 = new S3Client(awsConfig); +const testBucketName = 'test-jslib-aws'; +const testFileKey = 'bonjour.txt'; + +export default async function () { + // Let's delete our test object + await s3.deleteObject(testBucketName, testFileKey); + + // And make sure it was indeed deleted + const objects = await s3.listObjects(); + if (objects.filter((o) => o.name === testBucketName).length != 0) { + exec.test.abort(); + } +} +``` + +_A k6 script that will delete an object from a S3 bucket_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/getObject.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/getObject.md new file mode 100755 index 000000000..81a7450de --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/getObject.md @@ -0,0 +1,108 @@ +--- +title: 'getObject' +head_title: 'S3Client.getObject(bucketName, objectKey)' +description: 'S3Client.getObject downloads an object from a bucket' +weight: 10 +--- + +# getObject + +`S3Client.getObject` downloads an object from a bucket. + +As a default, the object's data will be treated as a string. In order to treat the object's data as binary content, and +receive the data as an `ArrayBuffer`, you can pass [`additionalHeaders`](#parameters) to `getObject`, with the `Accept` header set to +`application/octet-stream`. + +### Parameters + +| Parameter | Type | Description | +| :---------------- | :----- | :------------------------------------------- | +| bucketName | string | Name of the bucket to fetch the object from. | +| objectKey | string | Name of the object to download. | +| additionalHeaders | Object | Additional headers to send with the request. | + +### Returns + +| Type | Description | +| :--------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Promise<[Object](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/object)> | A Promise that fulfills with an [Object](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/object) describing and holding the downloaded content. | + +### Example + +#### Downloading a text file from AWS S3 + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const s3 = new S3Client(awsConfig); +const testBucketName = 'test-jslib-aws'; +const testFileKey = 'bonjour.txt'; + +export default async function () { + const objects = await s3.listObjects(testBucketName); + + // If our test object does not exist, abort the execution. + if (objects.filter((o) => o.key === testFileKey).length == 0) { + exec.test.abort(); + } + + // Let's download our test object and print its content + const object = await s3.getObject(testBucketName, testFileKey); + console.log(JSON.stringify(object)); +} +``` + +_A k6 script that will download an object from a bucket_ + +{{< /code >}} + +#### Downloading a binary file from AWS S3 + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const s3 = new S3Client(awsConfig); +const testBucketName = 'test-jslib-aws'; +const testFileKey = 'quick.pdf'; + +export default async function () { + // Download an object from S3, and require from the `getObject` operation + // to treat it as a binary content's file, and return an Object who's data + // parameter is an ArrayBuffer. + const s3Object = await s3.getObject(testBucketName, testFileKey, { + Accept: 'application/octet-stream', + }); + + console.log(`Successfully downloaded a binary file of size ${s3Object.data.byteLength} bytes`); +} +``` + +_A k6 script that will download a binary object from a bucket_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/listBuckets.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/listBuckets.md new file mode 100644 index 000000000..d0eaff8c2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/listBuckets.md @@ -0,0 +1,53 @@ +--- +title: 'listBuckets' +head_title: 'S3Client.listBuckets()' +description: 'S3Client.listBuckets lists the buckets the authenticated user has access to' +weight: 10 +--- + +# listBuckets + +`S3Client.listBuckets()` lists the buckets the authenticated user has access to in the region set by the `S3Client` instance's configuration. + +### Returns + +| Type | Description | +| :---------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------- | +| Promise/javascript-api/jslib/aws/s3client/bucket)>> | A Promise that fulfills with an array of [Bucket](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/bucket) objects. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const s3 = new S3Client(awsConfig); +const testBucketName = 'test-jslib-aws'; + +export default async function () { + // List the buckets the AWS authentication configuration + // gives us access to. + const buckets = await s3.listBuckets(); + + // If our test bucket does not exist, abort the execution. + if (buckets.filter((b) => b.name === testBucketName).length == 0) { + exec.test.abort(); + } + + // ... work with the bucket's content +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/listObjects.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/listObjects.md new file mode 100755 index 000000000..5123f7613 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/listObjects.md @@ -0,0 +1,61 @@ +--- +title: 'listObjects' +head_title: 'S3Client.listObjects(bucketName, [prefix])' +description: 'S3Client.listObjects lists the objects contained in a bucket' +weight: 10 +--- + +# listObjects + +`S3Client.listObjects()` lists the objects contained in a bucket. + +### Parameters + +| Parameter | Type | Description | +| :---------------- | :----- | :---------------------------------------------------------------- | +| bucketName | string | Name of the bucket to fetch the object from. | +| prefix (optional) | string | Limits the response to keys that begin with the specified prefix. | + +### Returns + +| Type | Description | +| :---------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------- | +| Promise/javascript-api/jslib/aws/s3client/object)>> | A Promise that fulfills with an array of [Object](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/object) objects. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const s3 = new S3Client(awsConfig); +const testBucketName = 'test-jslib-aws'; +const testFileKey = 'bonjour.txt'; + +export default async function () { + // List our bucket's objects + const objects = await s3.listObjects(testBucketName); + + // If our test object does not exist, abort the execution. + if (objects.filter((o) => o.key === testFileKey).length == 0) { + exec.test.abort(); + } + + // ... work with the bucket's objects + console.log(JSON.stringify(objects)); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/putObject.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/putObject.md new file mode 100755 index 000000000..51c44459e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/putObject.md @@ -0,0 +1,73 @@ +--- +title: 'putObject' +head_title: 'S3Client.putObject(bucketName, objectKey, data)' +description: 'S3Client.putObject uploads an object to a bucket' +weight: 10 +--- + +# putObject + +`S3Client.putObject` uploads an object to a bucket. + +### Parameters + +| Parameter | Type | Description | +| :----------- | :--------------------------------------------- | :------------------------------------------ | +| `bucketName` | string | Name of the bucket to upload the object to. | +| `objectKey` | string | Name of the uploaded object. | +| `data` | string \| ArrayBuffer | Content of the object to upload. | +| `params` | [PutObjectParams](#putobjectparams) (optional) | Options for the request. | + +#### PutObjectParams + +| Name | Type | Description | +| :------------------- | :---------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `contentDisposition` | string (optional) | Specifies presentational information for the object. For more information, see [RFC 6266](https://tools.ietf.org/html/rfc6266). | +| `contentEncoding` | string (optional) | Specifies what content encodings have been applied to the object and thus what decoding mechanisms must be applied to obtain the media-type referenced by the Content-Type header field. For more information, see [RFC 2616](https://tools.ietf.org/html/rfc2616). | +| `contentLength` | number (optional) | Size of the body in bytes. This parameter is useful when the size of the body cannot be determined automatically. | +| `contentMD5` | string (optional) | The base64-encoded 128-bit MD5 digest of the message (without the headers) according to RFC 1864. This header can be used as a message integrity check to verify that the received message is identical to the message that was sent. | +| `contentType` | string (optional) | A standard MIME type describing the format of the object data. For more information, see [RFC 2616](https://tools.ietf.org/html/rfc2616). | + +### Returns + +| Type | Description | +| :-------------- | :-------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the object has been uploaded to the S3 bucket. | + +### Example + +{{< code >}} + +```javascript +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const s3 = new S3Client(awsConfig); +const testBucketName = 'test-jslib-aws'; +const testFileKey = 'bonjour.txt'; +const testFile = open('./bonjour.txt', 'r'); + +export default async function () { + // Let's upload our test file to the bucket + await s3.putObject(testBucketName, testFileKey, testFile, { + contentType: 'text/plain', + contentLength: testFile.length, + }); + + // And let's redownload it to verify it's correct + const obj = await s3.getObject(testBucketName, testFileKey); + console.log(JSON.stringify(obj)); +} +``` + +_A k6 script that will upload an object to a S3 bucket_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/uploadPart.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/uploadPart.md new file mode 100755 index 000000000..230290642 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/S3Client/uploadPart.md @@ -0,0 +1,94 @@ +--- +title: 'uploadPart' +head_title: 'S3Client.uploadPart(bucketName, objectKey, uploadId,partNumber, data)' +description: 'S3Client.uploadPart a part in a multipart upload to a bucket' +weight: 10 +--- + +# uploadPart + +`S3Client.uploadPart` uploads a part to multipart upload in a bucket. + +| Parameter | Type | Description | +| :--------- | :-------------------- | :------------------------------------------ | +| bucketName | string | Name of the bucket to upload the object to. | +| objectKey | string | Name of the object to upload. | +| uploadId | string | UploadId of the multipart upload. | +| partNumber | number | The Part number of the Part to upload. | +| data | string \| ArrayBuffer | Content of the part to upload. | + +### Returns + +| Type | Description | +| :--------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Promise<[S3Part](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/s3part)> | A Promise that fulfills with a [S3Part](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client/s3part) representing a S3 Part Upload. | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; +import exec from 'k6/execution'; + +import { + AWSConfig, + S3Client, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/s3.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const s3 = new S3Client(awsConfig); + +const testBucketName = 'test-jslib-aws'; +const testFileKey = 'multipart.txt'; + +export default async function () { + // Produce random bytes to upload of size ~12MB, that + // we will upload in two 6MB parts. This is done as the + // minimum part size supported by S3 is 5MB. + const bigFile = crypto.randomBytes(12 * 1024 * 1024); + + // Initialize a multipart upload + const multipartUpload = await s3.createMultipartUpload(testBucketName, testFileKey); + + // Upload the first part + const firstPartData = bigFile.slice(0, 6 * 1024 * 1024); + const firstPart = await s3.uploadPart( + testBucketName, + testFileKey, + multipartUpload.uploadId, + 1, + firstPartData + ); + + // Upload the second part + const secondPartData = bigFile.slice(6 * 1024 * 1024, 12 * 1024 * 1024); + const secondPart = await s3.uploadPart( + testBucketName, + testFileKey, + multipartUpload.uploadId, + 2, + secondPartData + ); + + // Complete the multipart upload + await s3.completeMultipartUpload(testBucketName, testFileKey, multipartUpload.uploadId, [ + firstPart, + secondPart, + ]); + + // Let's redownload it verify it's correct, and delete it + const obj = await s3.getObject(testBucketName, testFileKey); + await s3.deleteObject(testBucketName, testFileKey); +} +``` + +_A k6 script that will upload a part in a multipart upload to an S3 bucket_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/_index.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/_index.md new file mode 100644 index 000000000..71d803ac2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/_index.md @@ -0,0 +1,67 @@ +--- +title: 'SQSClient' +head_title: 'SQSClient' +description: 'SQSClient enables interaction with the AWS Simple Queue Service (SQS)' +description: 'SQSClient allows interacting with the AWS Simple Queue Service (SQS)' +weight: 00 +--- + +# SQSClient + +`SQSClient` interacts with the AWS Simple Queue Service (SQS). + +With it, the user can send messages to specified queues and list available queues in the current region. + +`SQSClient` is included in both the dedicated `sqs.js` jslib bundle and the all-encompassing `aws.js` one, containing all the services clients. + +### Methods + +| Function | Description | +| :------------------------------------------------------------------------------------------------------- | :--------------------------------------------------- | +| [`sendMessage`](https://grafana.com/docs/k6//javascript-api/jslib/aws/sqsclient/sendmessage) | Delivers a message to the specified queue. | +| [`listQueues`](https://grafana.com/docs/k6//javascript-api/jslib/aws/sqsclient/listqueues) | Returns a list of your queues in the current region. | + +### Throws + +`SQSClient` methods throw errors in case of failure. + +| Error | Condition | +| :---------------------- | :-------------------------------------------------------- | +| `InvalidSignatureError` | when using invalid credentials | +| `SQSServiceError` | when AWS replied to the requested operation with an error | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + SQSClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/sqs.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const sqs = new SQSClient(awsConfig); +const testQueue = 'https://sqs.us-east-1.amazonaws.com/000000000/test-queue'; + +export default async function () { + // If our test queue does not exist, abort the execution. + const queuesResponse = await sqs.listQueues(); + if (queuesResponse.urls.filter((q) => q === testQueue).length == 0) { + exec.test.abort(); + } + + // Send message to test queue + await sqs.sendMessage(testQueue, JSON.stringify({ value: '123' })); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/listQueues.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/listQueues.md new file mode 100644 index 000000000..53c02d08e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/listQueues.md @@ -0,0 +1,61 @@ +--- +aliases: + - ./sqsclient-listqueues/ # /docs/k6//javascript-api/jslib/aws/sqsclient/sqsclient-listqueues/ +title: 'listQueues' +description: 'SQSClient.listQueues retrieves a list of available Amazon SQS queues' +weight: 10 +--- + +# listQueues + +`SQSClient.listQueues(options)` retrieves a list of available Amazon Simple Queue Service (SQS) queues. + +### Parameters + +| Name | Type | Description | +| :-------- | :---------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `options` | object (optional) | Options for the request. Accepted properties are: `queueNamePrefix` (optional string) setting the prefix filter for the returned queue list, `maxResults` (optional number) setting the maximum number of results to include in the response (1 <= `maxResults` <= 1000>), and `nextToken` (optional string) setting the pagination token to request the next set of results. | + +### Returns + +| Type | Description | +| :---------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with an object with an `urls` property containing an array of queue URLs, and an optional `nextToken` containing a pagination token to include in the next request when relevant. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + SQSClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/sqs.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const sqs = new SQSClient(awsConfig); +const testQueue = 'https://sqs.us-east-1.amazonaws.com/000000000/test-queue'; + +export default async function () { + // List all queues in the AWS account + const queuesResponse = await sqs.listQueues(); + + // If our test queue does not exist, abort the execution. + if (queuesResponse.urls.filter((q) => q === testQueue).length == 0) { + exec.test.abort(); + } + + // Send message to test queue + await sqs.sendMessage(testQueue, JSON.stringify({ value: '123' })); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/sendMessage.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/sendMessage.md new file mode 100644 index 000000000..bc62a180f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/sendMessage.md @@ -0,0 +1,85 @@ +--- +aliases: + - ./sqsclient-sendmessage/ # /docs/k6//javascript-api/jslib/aws/sqsclient/sqsclient-sendmessage/ +title: 'sendMessage' +description: 'SQSClient.sendMessage sends a message to the specified Amazon SQS queue' +weight: 10 +--- + +# sendMessage + +`SQSClient.sendMessage(queueUrl, messageBody, options)` sends a message to the specified Amazon Simple Queue Service (SQS) queue. + +### Parameters + +| Name | Type | Description | +| :------------ | :--------------------------------------------------- | :--------------------------------------------------------------------------------------------------- | +| `queueUrl` | string | The URL of the Amazon SQS queue to which a message is sent. Queue URLs and names are case-sensitive. | +| `messageBody` | string | The message to send. The minimum size is one character. The maximum size is 256 KB. | +| `options` | [SendMessageOptions](#sendmessageoptions) (optional) | Options for the request. | + +#### SendMessageOptions + +| Name | Type | Description | +| :----------------------- | :---------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `messageDeduplicationId` | string (optional) | The token used for deduplication of sent messages. This parameter applies only to FIFO (first-in-first-out) queues. If a message with a particular MessageDeduplicationId is sent successfully, any messages with the same MessageDeduplicationId are accepted but not delivered during the 5-minute deduplication interval. | +| `messageGroupId` | string (optional) | The tag that specifies that a message belongs to a specific message group. Messages that belong to the same message group are processed in a FIFO manner. Messages in different message groups might be processed out of order. | +| `messageAttributes` | object (optional) | Each message attribute consists of a `Name`, `Type`, and `Value`. For more information, see [Amazon SQS Message Attributes](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-message-attributes.html). | +| `delaySeconds` | number (optional) | The length of time, in seconds, for which to delay a specific message. Valid values: 0 to 900. Maximum: 15 minutes. Messages with a positive `delaySeconds` value become available for processing after the delay period is finished. If you don't specify a value, the default value for the queue applies. | + +### Returns + +| Type | Description | +| :---------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the message that was sent, as an object containing an `id` string property holding the unique identifier for the message, and a `bodyMD5` string property holding the MD5 digest of the non-URL-encoded message body string. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + SQSClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/sqs.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const sqs = new SQSClient(awsConfig); +const testQueue = 'https://sqs.us-east-1.amazonaws.com/000000000/test-queue'; + +export default async function () { + // If our test queue does not exist, abort the execution. + const queuesResponse = await sqs.listQueues(); + if (queuesResponse.urls.filter((q) => q === testQueue).length == 0) { + exec.test.abort(); + } + + // Send message to test queue + await sqs.sendMessage(testQueue, 'test', { + messageAttributes: { + 'test-string': { + type: 'String', + value: 'test', + }, + 'test-number': { + type: 'Number', + value: '23', + }, + 'test-binary': { + type: 'Binary', + value: 'dGVzdA==', + }, + }, + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/sendMessageBatch.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/sendMessageBatch.md new file mode 100644 index 000000000..22738dcbd --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SQSClient/sendMessageBatch.md @@ -0,0 +1,81 @@ +--- +aliases: +title: 'sendMessageBatch' +description: 'SQSClient.sendMessageBatch Delivers up to ten messages to the specified queue.' +weight: 10 +--- + +# sendMessage + +`SQSClient.sendMessageBatch(queueUrl, entries)` delivers up to ten messages to the specified Amazon Simple Queue +Service (SQS) queue. + +### Parameters + +| Name | Type | Description | +| :--------- | :---------------------------------------------- | :--------------------------------------------------------------------------------------------------- | +| `queueUrl` | string | The URL of the Amazon SQS queue to which a message is sent. Queue URLs and names are case-sensitive. | +| `entries` | [SendMessageBatchEntry](#sendmessagebatchentry) | A list of up to ten messages to send. | + +#### SendMessageBatchEntry + +| Name | Type | Description | +| :---------------- | :--------------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------- | +| `messageId` | string | The identifier of the batch entry message. | +| `messageBody` | string | The message to send. The minimum size is one character. The maximum size is 256 KB. | +| `messageOptions?` | [SendMessageOptions](https://grafana.com/docs/k6//javascript-api/jslib/sqs/sendmessageoption)>) (optional) | Options for the request. | + +### Returns + +| Type | Description | +| :------------------------------------------------------- | :-------------------------------------------------------------- | +| `Promise<[MessageBatchResponse](#messagebatchresponse)>` | A Promise that fulfills with a batch message creation response. | + +#### MessageBatchResponse + +| Name | Type | Description | +| :----------- | :---------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `successful` | object[] | A list of succesful messages as objects containing an `id` string property holding the unique identifier for the message, and a `bodyMD5` string property holding the MD5 digest of the non-URL-encoded message body string.. | +| `failed` | SQSServiceError[] | A list of error responses. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + SQSClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/sqs.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const sqs = new SQSClient(awsConfig); +const testQueue = 'https://sqs.us-east-1.amazonaws.com/000000000/test-queue'; + +export default async function () { + // If our test queue does not exist, abort the execution. + const queuesResponse = await sqs.listQueues(); + if (queuesResponse.queueUrls.filter((q) => q === testQueue).length == 0) { + exec.test.abort(); + } + + // Prepare a bunch of batch messages to add to the queue + const messageBatch = [ + { messageId: '0', messageBody: 'test0' }, + { messageId: '1', messageBody: 'test1' }, + ]; + + // Send the batch of messages to the queue + await sqs.sendMessageBatch(testQueue, messageBatch); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/Secret.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/Secret.md new file mode 100644 index 000000000..215fca407 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/Secret.md @@ -0,0 +1,63 @@ +--- +title: 'Secret' +head_title: 'Secret' +description: 'Secret is returned by the SecretsManagerClient.* methods who query secrets from AWS secrets manager.' +weight: 20 +--- + +# Secret + +Secret is returned by the SecretsManagerClient.\* methods that query secrets. Namely, [listSecrets](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/listsecrets/), +[getSecret](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/getsecret), +[createSecret](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/createsecret), and +[putSecretValue](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/putsecretvalue) returns either an instance or array of Secret objects. The Secret object describes an Amazon Secrets Manager secret. + +| Name | Type | Description | +| :----------------------- | :---------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------- | +| `Secret.name` | string | The friendly name of the secret. You can use forward slashes in the name to represent a path hierarchy. | +| `Secret.arn` | string | The Amazon Resource Name (ARN) of the secret. | +| `Secret.createdAt` | number | The date and time (timestamp) when a secret was created. | +| `Secret.lastAccessDate` | number | The last date that this secret was accessed. This value is truncated to midnight of the date and therefore shows only the date, not the time. | +| `Secret.lastChangedDate` | number | The last date and time that this secret was modified in any way. | +| `Secret.tags` | Array<{"key": "value"}> | The list of user-defined tags associated with the secret. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + SecretsManagerClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/secrets-manager.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const secretsManager = new SecretsManagerClient(awsConfig); +const testSecretName = 'jslib-test-secret'; + +export default async function () { + // List the secrets the AWS authentication configuration + // gives us access to. + const secrets = await secretsManager.listSecrets(); + + // If our test secret does not exist, abort the execution. + if (secrets.filter((s) => s.name === testSecretName).length == 0) { + exec.test.abort('test secret not found'); + } + + // Let's get it and print its content + const downloadedSecret = await secretsManager.getSecret(testSecretName); + console.log(downloadedSecret.secret); +} +``` + +_A k6 script that will query the user's secrets and print a test secret's value_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/_index.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/_index.md new file mode 100644 index 000000000..a122ee0c3 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/_index.md @@ -0,0 +1,93 @@ +--- +title: 'SecretsManagerClient' +head_title: 'SecretsManagerClient' +description: 'SecretsManagerClient allows interacting with AWS secrets stored in Secrets Manager' +weight: 00 +--- + +# SecretsManagerClient + +`SecretsManagerClient` interacts with the AWS Secrets Manager. + +With it, you can perform several operations such as listing, creating and downloading secrets owned by the authenticated user. For a full list of supported operations, see [Methods](#methods). + +`SecretsManagerClient` is included in both the dedicated jslib `secrets-manager.js` bundle, and the `aws.js` one, containing all the services clients. + +### Methods + +| Function | Description | +| :------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------- | +| [listSecrets()](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/listsecrets) | List secrets owned by the authenticated user | +| [getSecret(secretID)](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/getsecret) | Download a secret | +| [createSecret(name, secretString, description, [versionID], [tags])](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/createsecret) | Create a new secret | +| [putSecretValue(secretID, secretString, [versionID])](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/putsecretvalue) | Update a secret | +| [deleteSecret(secretID, { recoveryWindow: 30, noRecovery: false}})](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/deletesecret) | Delete a secret | + +### Throws + +S3 Client methods will throw errors in case of failure. + +| Error | Condition | +| :------------------------- | :--------------------------------------------------------- | +| InvalidSignatureError | when invalid credentials were provided. | +| SecretsManagerServiceError | when AWS replied to the requested operation with an error. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + SecretsManagerClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/secrets-manager.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const secretsManager = new SecretsManagerClient(awsConfig); +const testSecretName = 'jslib-test-secret'; +const testSecretValue = 'jslib-test-value'; + +export async function setup() { + // Let's make sure our test secret is created + const testSecret = await secretsManager.createSecret( + testSecretName, + testSecretValue, + 'this is a test secret, delete me.' + ); + + // List the secrets the AWS authentication configuration + // gives us access to, and verify the creation was successful. + const secrets = await secretsManager.listSecrets(); + if (!secrets.filter((s) => s.name === testSecret.name).length == 0) { + exec.test.abort('test secret not found'); + } +} + +export default async function () { + // Knnowing that we know the secret exist, let's update its value + const newTestSecretValue = 'new-test-value'; + await secretsManager.putSecretValue(testSecretName, newTestSecretValue); + + // Let's get its value and verify it was indeed updated + const updatedSecret = await secretsManager.getSecret(testSecretName); + if (updatedSecret.secret !== newTestSecretValue) { + exec.test.abort('unable to update test secret'); + } + + // Let's now use our secret in the context of our load test... +} + +export async function teardown() { + // Finally, let's clean after ourselves and delete our test secret + await secretsManager.deleteSecret(testSecretName, { noRecovery: true }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/createSecret.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/createSecret.md new file mode 100644 index 000000000..4847976ed --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/createSecret.md @@ -0,0 +1,64 @@ +--- +title: 'createSecret' +head_title: 'SecretsManagerClient.createSecret(name, secretString, description, [versionID], [tags])' +description: 'SecretsManagerClient.createSecret creates a new secret' +weight: 10 +--- + +# createSecret + +`SecretsManagerClient.createSecret` creates a secret in AWS' secrets manager. + +### Parameters + +| Parameter | Type | Description | +| :------------------- | :----------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------- | +| name | string | The friendly name of the secret. You can use forward slashes in the name to represent a path hierarchy. | +| secretString | string | The text data to encrypt and store in this new version of the secret. We recommend you use a JSON structure of key/value pairs for your secret value. | +| description | string | The description of the secret. | +| versionID (optional) | string | Optional unique version identifier for the created secret. If no versionID is provided, an auto-generated UUID will be used instead. | +| tags (optional) | Array<{"key": "value"},> | A list of tags to attach to the secret. Each tag is a key and value pair of strings in a JSON text string | + +### Returns + +| Type | Description | +| :--------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Promise<[Secret](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/secret)> | A Promise that fulfills with a [Secret](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/secret) object that contains the details of the created secret. | + +### Example + +{{< code >}} + +```javascript +import { + AWSConfig, + SecretsManagerClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/secrets-manager.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const secretsManager = new SecretsManagerClient(awsConfig); +const testSecretName = 'jslib-test-secret'; +const testSecretValue = 'jslib-test-value'; + +export default async function () { + // Let's create our test secret. + const testSecret = await secretsManager.createSecret( + testSecretName, + testSecretValue, + 'this is a test secret, delete me.' + ); + + // Let's get its value and verify it was indeed created. + const createdSecret = await secretsManager.getSecret(testSecretName); + console.log(JSON.stringify(createdSecret)); +} +``` + +_A k6 script that will create a secret in AWS secrets manager_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/deleteSecret.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/deleteSecret.md new file mode 100644 index 000000000..97026b609 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/deleteSecret.md @@ -0,0 +1,60 @@ +--- +title: 'deleteSecret' +head_title: 'SecretsManagerClient.deleteSecret(secretID, { recoveryWindow: 30, noRecovery: false}})' +description: 'SecretsManagerClient.deleteSecret deletes a secret' +weight: 10 +--- + +# deleteSecret + +`SecretsManagerClient.deleteSecret` deletes a secret from AWS' secrets manager. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :---------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| secretID | string | The ARN or name of the secret to update. | +| options | { recoveryWindow: 30, noRecovery: false } | Use options to control the deletion behavior. recoveryWindow defines how long a secret will remain “soft-deleted”, in days, before being hard-deleted. noRecovery set to true would hard-delete the secret immediately. Note that both options are exclusive. | + +### Returns + +| Type | Description | +| :-------------- | :--------------------------------------------------------- | +| `Promise` | A promise that will be resolved when the secret is deleted | + +### Example + +{{< code >}} + +```javascript +import { + AWSConfig, + SecretsManagerClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/secrets-manager.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const secretsManager = new SecretsManagerClient(awsConfig); +const testSecretName = 'jslib-test-secret'; +const testSecretValue = 'jslib-test-value'; + +export default async function () { + // Let's make sure our test secret is created + const testSecret = await secretsManager.createSecret( + testSecretName, + testSecretValue, + 'this is a test secret, delete me.' + ); + + // Let's hard delete our test secret and verify it worked + await secretsManager.deleteSecret(testSecretName, { noRecovery: true }); +} +``` + +_A k6 script that will delete a secret in AWS secrets manager_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/getSecret.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/getSecret.md new file mode 100644 index 000000000..3f0a5a6e6 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/getSecret.md @@ -0,0 +1,59 @@ +--- +title: 'getSecret' +head_title: 'SecretsManagerClient.getSecret(secretID)' +description: 'SecretsManagerClient.getSecret(secretID) downloads a secret from AWS secrets manager' +weight: 10 +--- + +# getSecret + +`SecretsManagerClient.getSecret` downloads a secret from AWS secrets manager. + +| Parameter | Type | Description | +| :-------- | :----- | :----------------------------------------- | +| secretID | string | The ARN or name of the secret to retrieve. | + +### Returns + +| Type | Description | +| :--------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Promise<[Secret](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/secret)> | A Promise that fulfills with a [Secret](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/secret) describing and holding the downloaded secret. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + SecretsManagerClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/secrets-manager.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const secretsManager = new SecretsManagerClient(awsConfig); +const testSecretName = 'jslib-test-secret'; + +export default async function () { + // List the secrets the AWS authentication configuration + // gives us access to. + const secrets = await secretsManager.listSecrets(); + if (secrets.filter((s) => s.name === testSecretName).length == 0) { + exec.test.abort('test secret not found'); + } + + // Let's get our test secret's value and print it. + const secret = await secretsManager.getSecret(testSecretName); + console.log(JSON.stringify(secret)); +} +``` + +_A k6 script that will download a user's secret from AWS secrets manager_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/listSecrets.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/listSecrets.md new file mode 100644 index 000000000..5491753ae --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/listSecrets.md @@ -0,0 +1,53 @@ +--- +title: 'listSecrets' +head_title: 'SecretsManagerClient.listSecrets()' +description: 'SecretsManagerClient.listSecrets lists the secrets the authenticated user has access to' +weight: 10 +--- + +# listSecrets + +`S3Client.listSecrets` lists the secrets the authenticated user has access to in the region set by the `SecretsManagerClient` instance's configuration. + +### Returns + +| Type | Description | +| :---------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Promise/javascript-api/jslib/aws/secretsmanagerclient/secret)>> | A Promise that fulfills with an array of [Secret](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/secret) objects. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + SecretsManagerClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/secrets-manager.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const secretsManager = new SecretsManagerClient(awsConfig); +const testSecretName = 'jslib-test-secret'; + +export default async function () { + // List the secrets the AWS authentication configuration + // gives us access to, and verify the test secret exists. + const secrets = await secretsManager.listSecrets(); + if (secrets.filter((s) => s.name === testSecretName).length == 0) { + exec.test.abort('test secret not found'); + } + + console.log(JSON.stringify(secrets)); +} +``` + +_A k6 script that will list a user's secrets from AWS secrets manager_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/putSecretValue.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/putSecretValue.md new file mode 100644 index 000000000..81ff3593b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SecretsManagerClient/putSecretValue.md @@ -0,0 +1,69 @@ +--- +title: 'putSecretValue' +head_title: 'SecretsManagerClient.putSecretValue(secretID, secretString, [versionID], [tags])' +description: "SecretsManagerClient.putSecretValue updates an existing secret's value" +weight: 10 +--- + +# putSecretValue + +`SecretsManagerClient.putSecretValue` updates a secret's value in AWS' secrets manager. + +| Parameter | Type | Description | +| :------------------- | :----------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------- | +| secretID | string | The ARN or name of the secret to update. | +| secretString | string | The text data to encrypt and store in this new version of the secret. We recommend you use a JSON structure of key/value pairs for your secret value. | +| versionID (optional) | string | Optional unique version identifier for the updated version of the secret. If no versionID is provided, an auto-generated UUID will be used instead. | +| tags (optional) | Array<{"key": "value"},> | A list of tags to attach to the secret. Each tag is a key and value pair of strings in a JSON text string | + +### Returns + +| Type | Description | +| :--------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | +| Promise<[Secret](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/secret)> | A Promise that fulfills with the updated [Secret](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient/secret). | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + SecretsManagerClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/secrets-manager.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const secretsManager = new SecretsManagerClient(awsConfig); +const testSecretName = 'jslib-test-secret'; +const testSecretValue = 'jslib-test-value'; + +export default async function () { + // Let's make sure our test secret is created + const testSecret = await secretsManager.createSecret( + testSecretName, + testSecretValue, + 'this is a test secret, delete me.' + ); + + // Now that we know the secret exist, let's update its value + const newTestSecretValue = 'new-test-value'; + const u = await secretsManager.putSecretValue(testSecretName, newTestSecretValue); + + // Let's get its value back and verify it was indeed updated + const updatedSecret = await secretsManager.getSecret(testSecretName); + if (updatedSecret.secret !== newTestSecretValue) { + exec.test.abort('unable to update test secret'); + } +} +``` + +_A k6 script that will update a secret's value in AWS secrets manager_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SignatureV4/_index.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SignatureV4/_index.md new file mode 100644 index 000000000..a6bf2817e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SignatureV4/_index.md @@ -0,0 +1,171 @@ +--- +title: 'SignatureV4' +head_title: 'SignatureV4' +description: 'SignatureV4 is used to sign or pre-sign requests to AWS services using the Signature V4 algorithm' +description: 'SignatureV4 is used to sign and pre-sign requests to AWS services using the Signature V4 algorithm' +weight: 00 +--- + +# SignatureV4 + +{{< docs/shared source="k6" lookup="blocking-aws-blockquote.md" version="" >}} + +With SignatureV4, you can produce authenticated HTTP requests to AWS services. Namely, it lets you sign and pre-sign requests to AWS services using the [Signature V4](https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html) algorithm. The `sign` operation produces a signed request with authorization information stored in its headers. +The `presign` operation produces a pre-signed request with authorization information stored in its query string parameters. + +SignatureV4 is included in both the dedicated jslib `signature.js` bundle and the `aws.js` one, containing all the service's clients. + +Instantiating a new `SignatureV4` requires a single options object argument with the following properties: + +| Property | Type | Description | +| :------------ | :-------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| service | string | the AWS region to sign or pre-sign requests for. As described by [Amazon AWS docs](https://docs.aws.amazon.com/general/latest/gr/rande.html) | +| region | string | the AWS service to sign or pre-sign requests for. As described by [Amazon AWS docs](https://aws.amazon.com/about-aws/global-infrastructure/regions_az/)] | +| credentials | an object with `accessKeyId`, `secretAccessKey`, and optional `sessionToken` properties | the AWS credentials to sign or pre-sign requests with. | +| uriEscapePath | boolean | Whether to uri-escape the request URI path as part of computing the canonical request string. As of late 2017, this is **required for every AWS service** except Amazon S3. | +| applyChecksum | boolean | Whether to calculate a checksum of the request body and include it as either a request header (when signing) or as a query string parameter (when pre-signing). This is **required for AWS Glacier and Amazon S3** and optional for every other AWS service as of late 2017. | + +## Methods + + + +| Method | Description | +| :------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------- | +| [sign()](https://grafana.com/docs/k6//javascript-api/jslib/aws/signaturev4/sign) | Signs an authenticated HTTP request using the AWS signature v4 algorithm | +| [presign()](https://grafana.com/docs/k6//javascript-api/jslib/aws/signaturev4/presign) | Produces an authenticated pre-signed URL using the AWS signature v4 algorithm | + + + +### Throws + +SignatureV4 methods throw errors on failure. + +| Error | Condition | +| :-------------------- | :-------------------------------------- | +| InvalidSignatureError | when invalid credentials were provided. | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +import { + AWSConfig, + Endpoint, + SignatureV4, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/signature.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +export default function () { + /** + * In order to be able to sign an HTTP request's, + * we need to instantiate a SignatureV4 object. + */ + const signer = new SignatureV4({ + service: 's3', + region: awsConfig.region, + credentials: { + accessKeyId: awsConfig.accessKeyId, + secretAccessKey: awsConfig.secretAccessKey, + sessionToken: awsConfig.sessionToken, + }, + + /** + * Whether the URI should be escaped or not. + */ + uriEscapePath: false, + + /** + * Whether or not the body's hash should be calculated and included + * in the request. + */ + applyChecksum: true, + }); + + /** + * The sign operation will return a new HTTP request with the + * AWS signature v4 protocol headers added. It returns an Object + * implementing the SignedHTTPRequest interface, holding a `url` and a `headers` + * properties, ready to use in the context of k6's http call. + */ + const signedRequest = signer.sign( + /** + * HTTP request description + */ + { + /** + * The HTTP method we will use in the request. + */ + method: 'GET', + + /** + * The endpoint of the service we will be making the request to. + * + * The endpoint is instantiated from a URL string, of the format: `{scheme}://{hostname}[:{port}]` + */ + endpoint: new Endpoint('https://s3.us-east-1.amazonaws.com'), + + /** + * The path of the request. + */ + path: '/my-bucket/bonjour.txt', + + /** + * The query parameters to include in the request. + */ + query: { + 'abc': '123', + 'easy as': ['do', 're', 'mi'], + }, + + /** + * The headers we will be sending in the request. + */ + headers: {}, + + /** + * The body of the request. + */ + body: null, + }, + + /** + * (optional) Signature operation options allows to override the + * SignatureV4's options in the context of this specific request. + */ + { + /** + * The date and time to be used as signature metadata. This value should be + * a Date object, a unix (epoch) timestamp, or a string that can be + * understood by the JavaScript `Date` constructor.If not supplied, the + * value returned by `new Date()` will be used. + */ + signingDate: new Date(), + + /** + * The service signing name. It will override the service name of the signer + * in current invocation + */ + signingService: 's3', + + /** + * The region name to sign the request. It will override the signing region of the + * signer in current invocation + */ + signingRegion: 'us-east-1', + } + ); + + http.get(signedRequest.url, { headers: signedRequest.headers }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SignatureV4/presign.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SignatureV4/presign.md new file mode 100644 index 000000000..d4f56a959 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SignatureV4/presign.md @@ -0,0 +1,208 @@ +--- +title: 'presign' +head_title: 'presign' +slug: 'presign' +description: 'Signaturev4.presign pre-signs a URL with the AWS Signature V4 algorithm' +description: 'SignatureV4.sign pre-signs a URL with the AWS Signature V4 algorithm' +--- + +# presign + +`SignatureV4.presign()` pre-signs a URL with the AWS Signature V4 algorithm. Given an HTTP request description, it returns a new HTTP request with the AWS signature v4 authorization added. It returns an Object holding a `url` containing the authorization information encoded in its query string, ready to use in the context of a k6 HTTP call. + +### Parameters + +The first parameter of the `presign` method consists of an Object with the following properties. + +| Property | Type | Description | +| :------- | :------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| method | string | The HTTP method of the request | +| endpoint | Endpoint | The endpoint of the request. The Endpoint constructor can be imported from both the `aws.js` bundle, as well as the `signature.js` file, and takes a string of the form `{scheme}://{hostname}[:{port}]` as input, allowing to define the target of the request (See provided example below). | +| path | string | The path of the request | +| headers | Object | The headers of the HTTP request | + +You can provide further options and override SignatureV4 options in the context of this specific request. +To do this, pass a second parameter to the `presign` method, which is an Object with the following parameters. + +| Property | Type | Description | +| :---------------- | :------------ | :------------------------------------------------------------------------- | +| expiresIn | number | The number of seconds before the pre-signed URL expires | +| signingDate | Date | overrides the Date used in the signing operation | +| signingService | string | overrides the signer's AWS service in the context of the `sign` operation. | +| signingRegion | string | overrides the signer's AWS region in the context of the `sign` operation | +| unsignableHeaders | `Set` | excludes headers from the signing process | +| signableHeaders | `Set` | mark headers as signed | + +### Returns + +The `presign` operation returns an Object with the following properties. + +| Property | Type | Description | +| :------- | :----- | :------------------------------------------------------------------------ | +| headers | Object | The pre-signed request headers to use in the context of a k6 HTTP request | +| url | string | The pre-signed url to use in the context of a k6 HTTP request | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +import { + AWSConfig, + Endpoint, + SignatureV4, + AMZ_CONTENT_SHA256_HEADER, + UNSIGNED_PAYLOAD, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/signature.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +export default function () { + // In order to be able to produce pre-signed URLs, + // we need to instantiate a SignatureV4 object. + const signer = new SignatureV4({ + service: 's3', + region: awsConfig.region, + credentials: { + accessKeyId: awsConfig.accessKeyId, + secretAccessKey: awsConfig.secretAccessKey, + sessionToken: awsConfig.sessionToken, + }, + + /** + * Whether the URI should be escaped or not. + */ + uriEscapePath: false, + + /** + * Whether or not the body's hash should be calculated and included + * in the request. + */ + applyChecksum: true, + }); + + // We can now use the signer to produce a pre-signed URL. + const presignedRequest = signer.presign( + /** + * HTTP request description + */ + { + /** + * The HTTP method we will use in the request. + */ + method: 'GET', + + /** + * The endpoint of the service we will be making the request to. + * + * The endpoint is instantiated from a URL string, of the format: `{scheme}://{hostname}[:{port}]` + */ + endpoint: new Endpoint('https://s3.us-east-1.amazonaws.com'), + + /** + * The path of the request. + */ + path: '/my-bucket/bonjour.txt', + + /** + * The headers we will be sending in the request. + * + * Note that in the specific case of this example, requesting + * an object from S3, we want to set the `x-amz-content-sha256` + * header to `UNSIGNED_PAYLOAD`. That way, we bypass the payload + * hash calculation, and communicate that value instead, as specified. + */ + headers: { [AMZ_CONTENT_SHA256_HEADER]: 'UNSIGNED-PAYLOAD' }, + }, + + /** + * (optional) pre-sign operation options. + */ + { + /** + * The number of seconds before the pre-signed URL expires + */ + expiresIn: 86400, + + /** + * A set of strings whose representing headers that should not be hoisted + * to pre-signed request's query string. If not supplied, the pre-signer + * moves all the AWS-specific headers (starting with `x-amz-`) to the request + * query string. If supplied, these headers remain in the pre-signed request's + * header. + * All headers in the provided request will have their names converted to + * lower case and then checked for existence in the unhoistableHeaders set. + * + * In the case of pre-signing S3 URLs, the body needs to be empty. + * however, the AMZ_CONTENT_SHA256_HEADER needs to be set to + * UNSIGNED_PAYLOAD. To do this, we need to set the header, + * but declare it as unhoistable, and unsignable. + */ + unhoistableHeaders: new Set([AMZ_CONTENT_SHA256_HEADER]), + + /** + * A set of strings whose members represents headers that cannot be signed. + * All headers in the provided request will have their names converted to + * lower case and then checked for existence in the unsignableHeaders set. + * + * In the case of pre-signing S3 URLs, the body needs to be empty. + * however, the AMZ_CONTENT_SHA256_HEADER needs to be set to + * UNSIGNED_PAYLOAD. To do this, we need to set the header, + * but declare it as unhoistable, and unsignable. + */ + unsignableHeaders: new Set([AMZ_CONTENT_SHA256_HEADER]), + + /** + * A set of strings whose members represents headers that should be signed. + * Any values passed here will override those provided via unsignableHeaders, + * allowing them to be signed. + * + * All headers in the provided request will have their names converted to + * lower case before signing. + */ + signableHeaders: new Set(), + + /** + * The date and time to be used as signature metadata. This value should be + * a Date object, a unix (epoch) timestamp, or a string that can be + * understood by the JavaScript `Date` constructor.If not supplied, the + * value returned by `new Date()` will be used. + */ + signingDate: new Date(), + + /** + * The service signing name. It will override the service name of the signer + * in current invocation + */ + signingService: 's3', + + /** + * The signingRegion and signingService options let the user + * specify a different region or service to pre-sign the request for. + */ + signingRegion: 'us-east-1', + } + ); + + console.log(`presigned URL: ${presignedRequest.url}`); + + /** + * Our URL is now ready to be used. + */ + const res = http.get(presignedRequest.url, { + headers: presignedRequest.headers, + }); + + check(res, { 'status is 200': (r) => r.status === 200 }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SignatureV4/sign.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SignatureV4/sign.md new file mode 100644 index 000000000..7568b2b91 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SignatureV4/sign.md @@ -0,0 +1,167 @@ +--- +title: 'sign' +head_title: 'sign' +slug: 'sign' +description: 'Signaturev4.sign signs an HTTP request with the AWS Signature V4 algorithm' +description: 'SignatureV4.sign signs an HTTP request with the AWS Signature V4 algorithm' +--- + +# sign + +`SignatureV4.sign()` signs an HTTP request with the AWS Signature V4 algorithm. Given an HTTP request description, it returns a new HTTP request with the AWS signature v4 protocol headers added. It returns an Object holding a `url` and a `headers` properties, ready to use in the context of k6's HTTP call. + +### Parameters + +The first parameter of the `sign` method consists of an Object with the following properties. + +| Property | Type | Description | +| :--------------- | :------------------------------------------ | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| method | string | The HTTP method of the request | +| endpoint | Endpoint | The endpoint of the request. The Endpoint constructor can be imported from both the `aws.js` bundle, as well as the `signature.js` file, and takes a string of the form `{scheme}://{hostname}[:{port}]` as input, allowing to define the target of the request (See provided example below). | +| path | string | The path of the request | +| headers | Object | The headers of the HTTP request | +| body (optional) | string or ArrayBuffer | The optional body of the HTTP request | +| query (optional) | `Object.>` | The optional query parameters of the HTTP request | + +You can override SignatureV4 options in the context of this specific request. To do this, pass a second parameter to the `sign` method, which is an Object with the following parameters. + +| Property | Type | Description | +| :---------------- | :------------ | :------------------------------------------------------------------------- | +| signingDate | Date | overrides the Date used in the signing operation | +| signingService | string | overrides the signer's AWS service in the context of the `sign` operation. | +| signingRegion | string | overrides the signer's AWS region in the context of the `sign` operation | +| unsignableHeaders | `Set` | excludes headers from the signing process | +| signableHeaders | `Set` | mark headers as signed | + +### Returns + +| Property | Type | Description | +| :------- | :----- | :---------------------------------------------------------------------- | +| headers | Object | The signed request's headers to use in the context of a k6 HTTP request | +| url | string | The signed url to use in the context of a k6 HTTP request | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +import { + AWSConfig, + Endpoint, + SignatureV4, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/signature.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +export default function () { + /** + * In order to be able to sign an HTTP request's, + * we need to instantiate a SignatureV4 object. + */ + const signer = new SignatureV4({ + service: 's3', + region: awsConfig.region, + credentials: { + accessKeyId: awsConfig.accessKeyId, + secretAccessKey: awsConfig.secretAccessKey, + sessionToken: awsConfig.sessionToken, + }, + + /** + * Whether the URI should be escaped or not. + */ + uriEscapePath: false, + + /** + * Whether or not the body's hash should be calculated and included + * in the request. + */ + applyChecksum: true, + }); + + /** + * The sign operation will return a new HTTP request with the + * AWS signature v4 protocol headers added. It returns an Object + * implementing the SignedHTTPRequest interface, holding a `url` and a `headers` + * properties, ready to use in the context of k6's http call. + */ + const signedRequest = signer.sign( + /** + * HTTP request description + */ + { + /** + * The HTTP method we will use in the request. + */ + method: 'GET', + + /** + * The endpoint of the service we will be making the request to. + * + * The endpoint is instantiated from a URL string, of the format: `{scheme}://{hostname}[:{port}]` + */ + endpoint: new Endpoint('https://s3.us-east-1.amazonaws.com'), + + /** + * The path of the request. + */ + path: '/my-bucket/bonjour.txt', + + /** + * The query parameters to include in the request. + */ + query: { + 'abc': '123', + 'easy as': ['do', 're', 'mi'], + }, + + /** + * The headers we will be sending in the request. + */ + headers: {}, + + /** + * The body of the request. + */ + body: null, + }, + + /** + * (optional) Signature operation options allows to override the + * SignatureV4's options in the context of this specific request. + */ + { + /** + * The date and time to be used as signature metadata. This value should be + * a Date object, a unix (epoch) timestamp, or a string that can be + * understood by the JavaScript `Date` constructor.If not supplied, the + * value returned by `new Date()` will be used. + */ + signingDate: new Date(), + + /** + * The service signing name. It will override the service name of the signer + * in current invocation + */ + signingService: 's3', + + /** + * The region name to sign the request. It will override the signing region of the + * signer in current invocation + */ + signingRegion: 'us-east-1', + } + ); + + http.get(signedRequest.url, { headers: signedRequest.headers }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SystemsManagerClient/SystemsManagerParameter.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SystemsManagerClient/SystemsManagerParameter.md new file mode 100644 index 000000000..a51a3d4d7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SystemsManagerClient/SystemsManagerParameter.md @@ -0,0 +1,75 @@ +--- +title: 'SystemsManagerParameter' +head_title: 'SystemsManagerParameter' +slug: 'systemsmanagerparameter' +description: 'SystemsManagerParameter is returned by the SystemsManagerClient.* methods that query parameters' +weight: 20 +--- + +# SystemsManagerParameter + +`SystemsManagerParameter.*` methods querying the Systems Manager Service parameters return some `SystemsManagerParameter` instances. Namely, `getParameter` returns an array of `SystemsManagerParameter` objects. The `SystemsManagerParameter` object describes an Amazon Systems Manager Service parameter. + +| Name | Type | Description | +| :----------------------------------------- | :----- | :------------------------------------------------------------------------------------ | +| `SystemsManagerParameter.arn` | string | The Amazon Resource Name (ARN) of the parameter | +| `SystemsManagerParameter.dataType` | string | The data type of the parameter, such as text or aws:ec2:image. The default is text. | +| `SystemsManagerParameter.lastModifiedDate` | number | Date the parameter was last changed or updated and the parameter version was created. | +| `SystemsManagerParameter.name` | string | The friendly name of the parameter. | +| `SystemsManagerParameter.selector` | string | Either the version number or the label used to retrieve the parameter value | +| `SystemsManagerParameter.sourceResult` | string | The raw result or response from the source. | +| `SystemsManagerParameter.type` | string | The type of parameter | +| `SystemsManagerParameter.value` | string | The parameter value | +| `SystemsManagerParameter.version` | string | The parameter version | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + SystemsManagerClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/ssm.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const systemsManager = new SystemsManagerClient(awsConfig); +const testParameterName = 'jslib-test-parameter'; +const testParameterValue = 'jslib-test-value'; +const testParameterSecretName = 'jslib-test-parameter-secret'; +// this value was created with --type SecureString +const testParameterSecretValue = 'jslib-test-secret-value'; + +export default async function () { + // Currently the parameter needs to be created before hand + + // Let's get its value + // getParameter returns a parameter object: e.g. {name: string, value: string...} + const parameter = await systemsManager.getParameter(testParameterName); + if (parameter.value !== testParameterValue) { + exec.test.abort('test parameter not found'); + } + + // Let's get the secret value with decryption + // destructure the parameter object to get to the values you want + const { value: encryptedParameterValue } = await systemsManager.getParameter( + testParameterSecretName, + true + ); + if (encryptedParameterValue !== testParameterSecretValue) { + exec.test.abort('encrypted test parameter not found'); + } +} +``` + +_A k6 script querying a user Systems Manager Service parameter_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SystemsManagerClient/_index.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SystemsManagerClient/_index.md new file mode 100644 index 000000000..7ed54661e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SystemsManagerClient/_index.md @@ -0,0 +1,81 @@ +--- +title: 'SystemsManagerClient' +head_title: 'SystemsManagerClient' +description: 'SystemsManagerClient allows interacting with the AWS Systems Manager Service' +weight: 00 +--- + +# SystemsManagerClient + +`SystemsManagerClient` interacts with the AWS Systems Manager Service. + +With it, the user can get parameters from the Systems Manager Service in the caller's AWS account and region. + +Both the dedicated `ssm.js` jslib bundle and the all-encompassing `aws.js` bundle include the `SystemsManagerClient`. + +### Methods + +| Function | Description | +| :------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------- | +| [getParameter](https://grafana.com/docs/k6//javascript-api/jslib/aws/systemsmanagerclient/getparameter) | Retrieves a parameter from Amazon Systems Manager. | + +### Throws + +`SystemsManagerClient` methods throw errors in case of failure. + +| Error | Condition | +| :--------------------------- | :-------------------------------------------------------- | +| `InvalidSignatureError` | when using invalid credentials | +| `SystemsManagerServiceError` | when AWS replied to the requested operation with an error | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + SystemsManagerClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/ssm.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const systemsManager = new SystemsManagerClient(awsConfig); +const testParameterName = 'jslib-test-parameter'; +const testParameterValue = 'jslib-test-value'; +const testParameterSecretName = 'jslib-test-parameter-secret'; +// this value was created with --type SecureString +const testParameterSecretValue = 'jslib-test-secret-value'; + +export default async function () { + // Currently the parameter needs to be created before hand + + // Let's get its value + // getParameter returns a parameter object: e.g. {name: string, value: string...} + const parameter = await systemsManager.getParameter(testParameterName); + if (parameter.value !== testParameterValue) { + exec.test.abort('test parameter not found'); + } + + // Let's get the secret value with decryption + // destructure the parameter object to get to the values you want + const { value: encryptedParameterValue } = await systemsManager.getParameter( + testParameterSecretName, + true + ); + if (encryptedParameterValue !== testParameterSecretValue) { + exec.test.abort('encrypted test parameter not found'); + } +} +``` + +_A k6 script querying a user's Systems Manager Service parameter_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SystemsManagerClient/getParameter.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SystemsManagerClient/getParameter.md new file mode 100644 index 000000000..ae2ccce09 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/SystemsManagerClient/getParameter.md @@ -0,0 +1,68 @@ +--- +title: 'getParameter' +head_title: 'SystemsManagerClient.getParameter()' +description: "SystemsManagerClient.getParameter gets a Systems Manager parameter in the caller's AWS account and region" +weight: 10 +--- + +# getParameter + +`SystemsManagerClient.getParameter` gets a Systems Manager parameter in the caller's AWS account and region. + +### Returns + +| Type | Description | +| :------------------------------------------------------------------------------------------------------------------------------------------------------ | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [`Promise`](https://grafana.com/docs/k6//javascript-api/jslib/aws/systemsmanagerclient/systemsmanagerparameter/) | A Promise that fulfills with an array of [`SystemsManagerParameter`](https://grafana.com/docs/k6//javascript-api/jslib/aws/systemsmanagerclient/systemsmanagerparameter/) objects. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +import { + AWSConfig, + SystemsManagerClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/ssm.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, + sessionToken: __ENV.AWS_SESSION_TOKEN, +}); + +const systemsManager = new SystemsManagerClient(awsConfig); +const testParameterName = 'jslib-test-parameter'; +const testParameterValue = 'jslib-test-value'; +const testParameterSecretName = 'jslib-test-parameter-secret'; +// this value was created with --type SecureString +const testParameterSecretValue = 'jslib-test-secret-value'; + +export default async function () { + // Currently the parameter needs to be created before hand + + // Let's get its value + // getParameter returns a parameter object: e.g. {name: string, value: string...} + const parameter = await systemsManager.getParameter(testParameterName); + if (parameter.value !== testParameterValue) { + exec.test.abort('test parameter not found'); + } + + // Let's get the secret value with decryption + // destructure the parameter object to get to the values you want + const { value: encryptedParameterValue } = await systemsManager.getParameter( + testParameterSecretName, + true + ); + if (encryptedParameterValue !== testParameterSecretValue) { + exec.test.abort('encrypted test parameter not found'); + } +} +``` + +_A k6 script querying a user's Systems Manager Service parameter_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/_index.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/_index.md new file mode 100644 index 000000000..9e1ef78eb --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/_index.md @@ -0,0 +1,18 @@ +--- +weight: 01 +title: aws +--- + +# aws + + + +{{< admonition type="note" >}} + +The source code for this library can be found in the [grafana/k6-jslib-aws](https://github.com/grafana/k6-jslib-aws) GitHub repository. + +{{< /admonition >}} + +The `aws` module is an external JavaScript library that helps interact with a subset of AWS services in the context of k6 test scripts. + +{{< section >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/awsconfig.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/awsconfig.md new file mode 100644 index 000000000..9b2691160 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/aws/awsconfig.md @@ -0,0 +1,67 @@ +--- +title: 'AWSConfig' +head_title: 'AWSConfig' +description: 'AWSConfig is used to configure an AWS service client instances' +description: 'AWSConfig is used to configure an AWS service client instances' +weight: 00 +--- + +# AWSConfig + +AWSConfig is used to configure an AWS service client instance, such as [S3Client](https://grafana.com/docs/k6//javascript-api/jslib/aws/s3client) or [SecretsManagerClient](https://grafana.com/docs/k6//javascript-api/jslib/aws/secretsmanagerclient). It effectively allows the user to select a [region](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html) they wish to interact with, and the AWS credentials they wish to use to authenticate. + +AWSConfig is included in the `aws.js` bundle, which includes all the content of the library. It is also included in the various services clients dedicated bundles such as `s3.js` and `secrets-manager.js`. + +It takes an options object as its single parameter, with the following properties: + +| Property | Type | Description | +| :---------------------- | :----- | :------------------------------------------------------------------------------------------------------------------------ | +| region | string | the AWS region to connect to. As described by [Amazon AWS docs](https://docs.aws.amazon.com/general/latest/gr/rande.html) | +| accessKeyID | string | The AWS access key ID credential to use for authentication. | +| secretAccessKey | string | The AWS secret access credential to use for authentication. | +| sessionToken (optional) | string | The AWS secret access token to use for authentication. | + +### Methods + +| Name | Description | +| :------------------ | :----------------------------------------------------------------------------------------------------------------------------------------- | +| `fromEnvironment()` | Creates an `AWSConfig` using the `AWS_REGION`, `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_SESSION_TOKEN` environment variables. | + +### Throws + +S3 Client methods will throw errors in case of failure. + +| Error | Condition | +| :----------------------- | :---------------------------------------------------- | +| InvalidArgumentException | when an invalid region name or credentials were used. | + +### Example + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +// Note that you AWSConfig is also included in the dedicated service +// client bundles such as `s3.js` and `secrets-manager.js` +import { + AWSConfig, + SecretsManagerClient, +} from 'https://jslib.k6.io/aws/{{< param "JSLIB_AWS_VERSION" >}}/aws.js'; + +const awsConfig = new AWSConfig({ + region: __ENV.AWS_REGION, + accessKeyId: __ENV.AWS_ACCESS_KEY_ID, + secretAccessKey: __ENV.AWS_SECRET_ACCESS_KEY, +}); + +const secretsManager = new SecretsManagerClient(awsConfig); + +export default function () { + // ... +} +``` + +_k6 will instantiate an `AWSConfig` object and use it to configure a `SecretsManagerClient` instance_ + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-pyroscope/_index.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-pyroscope/_index.md new file mode 100644 index 000000000..db5572065 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-pyroscope/_index.md @@ -0,0 +1,62 @@ +--- +title: HTTP instrumentation for Pyroscope +menuTitle: http-instrumentation-pyroscope +description: 'k6 Pyroscope instrumentation API' +weight: 04 +--- + +# HTTP instrumentation for Pyroscope + +{{< admonition type="note" >}} + +The source code for this library can be found in the [grafana/jslib.k6.io](https://github.com/grafana/jslib.k6.io/tree/main/lib/http-instrumentation-pyroscope) GitHub repository. + +{{< /admonition >}} + +The `http-instrumentation-pyroscope` module allows you to _instrument_ HTTP requests in a way that lets you tag Grafana Cloud Profiles with relevant information generated from k6 tests. + +## About baggage header + +The _baggage header_ is a standardized HTTP header used to propagate distributed context. The [W3C specification](https://www.w3.org/TR/baggage/) goes into more detail on the specifics, but like many other headers, the baggage header is a list of key-value pairs. + +This module, by default, adds three key-value pairs: + +1. Scenario name +2. Name of the request (URL if not set) +3. Value of `__ENV.K6_CLOUDRUN_TEST_RUN_ID`, which is set automatically in Grafana Cloud k6. + +## API + +| Class/Function | Description | +| :---------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------- | +| [instrumentHTTP](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-pyroscope/instrumenthttp) | Instruments the k6 http module with baggage header. | +| [Client](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-pyroscope/client) | Configurable Client that exposes instrumented HTTP operations. | + +## Example + +This example demonstrates how to use the this library to instrument every HTTP request made in a script with the `baggage` header. + +{{< code >}} + +```javascript +import { check } from 'k6'; +import pyroscope from 'https://jslib.k6.io/http-instrumentation-pyroscope/1.0.1/index.js'; +import http from 'k6/http'; + +// instrumentHTTP will ensure that all requests made by the http module +// from this point forward will have a baggage context attached. +pyroscope.instrumentHTTP(); + +export default () => { + // the instrumentHTTP call in the init context replaced + // the http module with a version that will automatically + // attach a baggage header to every request. + const res = http.get('http://httpbin.org/get', { + headers: { + 'X-Example-Header': 'instrumented/get', + }, + }); +}; +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-pyroscope/client.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-pyroscope/client.md new file mode 100644 index 000000000..6542b4195 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-pyroscope/client.md @@ -0,0 +1,75 @@ +--- +title: 'Client' +description: 'Client is a HTTP client attaching baggage headers to its requests.' +weight: 02 +--- + +# Client + +`Client` is an HTTP client constructor that attaches baggage headers to its requests. +Use the `Client` class to include a context in HTTP requests so that [Grafana pyroscope](https://grafana.com/oss/pyroscope/) can incorporate their results. + +The `Client` class acts as a drop-in replacement for the standard `http` module and attaches a [baggage header](https://www.w3.org/TR/baggage/) to the request. For details about propagation, refer to [About baggage header](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-pyroscope#about-baggage-header). + +The `Client` constructor accepts a function that takes the method, the body, and the params of each request and returns a map of headers to be added to the request. By default, it appends the baggage header with the info necessary for Grafana Cloud Profiling to integrate with Grafana Cloud k6. + +For details about propagation, refer to [About baggage header](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-pyroscope#about-baggage-header). + +## Example + +This example demonstrates how to instantiate a client and use it to instrument HTTP calls. The example also illustrates how you can use the client alongside the standard `http` module to perform non-instrumented HTTP calls. + +{{< code >}} + +```javascript +import { check } from 'k6'; +import pyroscope from 'https://jslib.k6.io/http-instrumentation-pyroscope/1.0.1/index.js'; +import http from 'k6/http'; + +// Explicitly instantiating a Pyroscope client allows to distinguish +// instrumented from non-instrumented HTTP calls, by keeping APIs separate. +const instrumentedHTTP = new pyroscope.Client(); + +const testData = { name: 'Bert' }; + +export default () => { + // Using the pyroscope client instance, HTTP calls will have + // baggage header added. + let res = instrumentedHTTP.request('GET', 'http://httpbin.org/get', null, { + headers: { + 'X-Example-Header': 'instrumented/request', + }, + }); + + // The client offers more flexibility over + // the `instrumentHTTP` function, as it leaves the + // imported standard `http` module untouched. Thus, + // one can still perform non-instrumented HTTP calls + // using it. + res = http.post('http://httpbin.org/post', JSON.stringify(testData), { + headers: { 'X-Example-Header': 'noninstrumented/post' }, + }); + + res = instrumentedHTTP.del('http://httpbin.org/delete', null, { + headers: { 'X-Example-Header': 'instrumented/delete' }, + }); +}; +``` + +{{< /code >}} + +## HTTP module functions equivalents + +The `Client` class exposes the same API as the standard `http` module except for the `batch` method, which is absent from `Client`. The following table lists the `Client` methods which have an equivalent in the standard `http` module: + +| Method | HTTP equivalent | Description | +| :--------------------------------------------------- | :------------------------------------------------------------------------------------------------ | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Client.del(url, [body], [params])` | [http.del](https://grafana.com/docs/k6//javascript-api/k6-http/del) | Performs an instrumented HTTP DELETE request. The method has the same prototype as the `http.del` function and should transparently act as a drop-in replacement for it. | +| `Client.get(url, [params])` | [http.get](https://grafana.com/docs/k6//javascript-api/k6-http/get) | Performs an instrumented HTTP GET request. The method has the same prototype as the `http.get` function and should transparently act as a drop-in replacement for it. | +| `Client.head(url, [params])` | [http.head](https://grafana.com/docs/k6//javascript-api/k6-http/head) | Performs an instrumented HTTP HEAD request. The method has the same prototype as the `http.head` function and should transparently act as a drop-in replacement for it. | +| `Client.options(url, [body], [params])` | [http.options](https://grafana.com/docs/k6//javascript-api/k6-http/options) | Performs an instrumented HTTP OPTIONS request. The method has the same prototype as the `http.options` function and should transparently act as a drop-in replacement for it. | +| `Client.patch(url, [body], [params])` | [http.patch](https://grafana.com/docs/k6//javascript-api/k6-http/patch) | Performs an instrumented HTTP PATCH request. The method has the same prototype as the `http.patch` function and should transparently act as a drop-in replacement for it. | +| `Client.post(url, [body], [params])` | [http.post](https://grafana.com/docs/k6//javascript-api/k6-http/post) | Performs an instrumented HTTP POST request. The method has the same prototype as the `http.post` function and should transparently act as a drop-in replacement for it. | +| `Client.put(url, [body], [params])` | [http.put](https://grafana.com/docs/k6//javascript-api/k6-http/head) | Performs an instrumented HTTP HEAD request. The method has the same prototype as the `http.put` function and should transparently act as a drop-in replacement for it. | +| `Client.request(method, url, [body], [params])` | [http.request](https://grafana.com/docs/k6//javascript-api/k6-http/request) | Performs an instrumented HTTP request. The method has the same prototype as the `http.request` function and should transparently act as a drop-in replacement for it. | +| `Client.asyncRequest(method, url, [body], [params])` | [http.asyncRequest](https://grafana.com/docs/k6//javascript-api/k6-http/asyncrequest) | Performs an instrumented HTTP asynchronous request. The method has the same prototype as the `http.asyncRequest` function and should transparently act as a drop-in replacement for it. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-pyroscope/instrumenthttp.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-pyroscope/instrumenthttp.md new file mode 100644 index 000000000..a52bea98b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-pyroscope/instrumenthttp.md @@ -0,0 +1,50 @@ +--- +title: 'instrumentHTTP' +description: 'instrumentHTTP instruments the k6 HTTP module with baggage header.' +weight: 01 +--- + +# instrumentHTTP + +The `instrumentHTTP` function instruments the k6 HTTP module with additional headers. It transparently replaces each of the k6 http module functions with versions that automatically attach a [baggage header](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-pyroscope#about-baggage-header)(by default) to every request. + +Instrumented functions include [del](https://grafana.com/docs/k6//javascript-api/k6-http/del),[get](https://grafana.com/docs/k6//javascript-api/k6-http/get),[head](https://grafana.com/docs/k6//javascript-api/k6-http/head),[options](https://grafana.com/docs/k6//javascript-api/k6-http/options),[patch](https://grafana.com/docs/k6//javascript-api/k6-http/patch),[post](https://grafana.com/docs/k6//javascript-api/k6-http/post),[put](https://grafana.com/docs/k6//javascript-api/k6-http/head), and [request](https://grafana.com/docs/k6//javascript-api/k6-http/request). + +This means that, to instrument the HTTP requests, you don't need to rewrite any code. +Instead, call the module once in the init context. +For details about propagation, refer to [About baggage header](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-pyroscope#about-baggage-header). + +## Parameters + +| Name | Type | Description | +| :-------- | :--------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `generateHeaders` | `function(method, body, params) headersMap` | A function taking the request method, body and params and returning a map of headers that will be attached. By default adding baggage header as explained in the [about baggage header]((https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-pyroscope#about-baggage-header)) | + +## Example + +This example demonstrates how to use the this library to instrument every HTTP request made in a script with baggage header. + +{{< code >}} + +```javascript +import { check } from 'k6'; +import pyroscope from 'https://jslib.k6.io/http-instrumentation-pyroscope/1.0.1/index.js'; +import http from 'k6/http'; + +// instrumentHTTP will ensure that all requests made by the `http` module +// from this point forward will have a baggage context attached. +pyroscope.instrumentHTTP(); + +export default () => { + // the instrumentHTTP call in the init context replaced + // the `http` module with a version that will automatically + // attach a baggage header to every request. + const res = http.get('http://httpbin.org/get', { + headers: { + 'X-Example-Header': 'instrumented/get', + }, + }); +}; +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/_index.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/_index.md new file mode 100644 index 000000000..456061b8d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/_index.md @@ -0,0 +1,74 @@ +--- +aliases: + - ../k6-experimental/tracing/ # docs/k6//javascript-api/k6-experimental/tracing/ +title: 'HTTP instrumentation for Tempo' +menuTitle: http-instrumentation-tempo +description: 'k6 Tempo instrumentation API' +weight: 04 +--- + +# HTTP instrumentation for Tempo + +{{< admonition type="note" >}} + +The source code for this library can be found in the [grafana/jslib.k6.io](https://github.com/grafana/jslib.k6.io/tree/main/lib/http-instrumentation-tempo/1.0.0) GitHub repository. + +{{< /admonition >}} + +The `http-instrumentation-tempo` module allows you to _instrument_ HTTP requests so that they emit traces as the test runs. Use it to include a tracing context in HTTP requests, which can then be used by a tracing backend such as [Grafana Tempo](https://grafana.com/docs/grafana-cloud/testing/k6/analyze-results/integration-with-grafana-cloud-traces/). + +## Migration from `k6/experimental/tracing` + +This jslib is a drop in replacement, so all you need to migrate to it is to replace `'k6/experimental/tracing'` import with `'https://jslib.k6.io/http-instrumentation-tempo/1.0.0/index.js'` + +## About trace contexts + +A _trace context_ is a set of standardized HTTP headers added to a request that lets a tracing system correlate it with other requests as they navigate through a system. The trace context specifications, such as the supported [W3C Trace Context](https://www.w3.org/TR/trace-context/) and [Jaeger Trace Context](https://www.jaegertracing.io/docs/1.21/client-libraries/#propagation-format), define specific header names and an encoding format for the header values. + +A trace context generally consists of, at least, a `trace_id`, a `span_id`, and a `sampled` flag. The `trace_id` is a unique identifier for the trace, the `span_id` is a unique identifier for the request, and the `sampled` flag is a boolean that indicates whether the request should be traced. For instance, the [W3C Trace Context](https://www.w3.org/TR/trace-context/) defines the `Traceparent` header, whose value contains a `trace_id`, a `span_id` and a `sampled` flag, encoded as a dash (`-`) separated list of hexadecimal values. When a trace context header is attached to an HTTP request, we refer to it as being _propagated_ to the downstream service. + +## API + +| Class/Function | Description | +| :------------------------------------------------------------------------------------------------------------------------ | :-------------------------------------------------------------------------------------------------------------------------- | +| [instrumentHTTP](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-tempo/instrumenthttp) | Instruments the k6 http module with tracing capabilities. | +| [Client](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-tempo/client) | A configurable client that exposes instrumented HTTP operations and allows selectively instrumenting requests with tracing. | + +## Example + +This example demonstrates how to use the tracing API to instrument every HTTP request made in a script with tracing information. + +{{< code >}} + +```javascript +import { check } from 'k6'; +import tempo from 'https://jslib.k6.io/http-instrumentation-tempo/1.0.0/index.js'; +import http from 'k6/http'; + +// instrumentHTTP will ensure that all requests made by the http module +// from this point forward will have a trace context attached. +// +// The first argument is a configuration object that +// can be used to configure the tracer. +tempo.instrumentHTTP({ + // possible values: "w3c", "jaeger" + propagator: 'w3c', +}); + +export default () => { + // the instrumentHTTP call in the init context replaced + // the http module with a version that will automatically + // attach a trace context to every request. + // + // Because the instrumentHTTP call was configured with the + // w3c trace propagator, this request will as a result have + // a `traceparent` header attached. + const res = http.get('http://httpbin.org/get', { + headers: { + 'X-Example-Header': 'instrumented/get', + }, + }); +}; +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/client.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/client.md new file mode 100644 index 000000000..abd89005a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/client.md @@ -0,0 +1,89 @@ +--- +aliases: + - ../../k6-experimental/tracing/client/ # docs/k6//javascript-api/k6-experimental/tracing/client/ +title: 'Client' +description: 'Client is a HTTP client attaching tracing information to its requests.' +weight: 02 +--- + +# Client + +`Client` is an HTTP client constructor that attaches tracing information to its requests. Use it to include a tracing context in HTTP requests so that tracing backends (such as [Grafana Tempo](https://grafana.com/oss/tempo/)) can incorporate their results. + +The `Client` class acts as a drop-in replacement for the standard `http` module and attaches a [trace context](https://www.w3.org/TR/trace-context/) to the requests headers, and add a `trace_id` to HTTP-related k6 output's data points metadata. It currently supports the [W3C Trace Context](https://www.w3.org/TR/trace-context/) and [Jaeger](https://www.jaegertracing.io/docs/1.21/client-libraries/#propagation-format) trace context propagation formats. For details about propagation, refer to [About trace contexts](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-tempo#about-trace-contexts). + +The `Client` constructor accepts an [`Options`](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-tempo/options) object as its only parameter. + +## Example + +This example demonstrates how to instantiate a tracing client and use it to instrument HTTP calls with trace context headers. It also illustrates how you can use it alongside the standard `http` module to perform non-instrumented HTTP calls. + +{{< code >}} + +```javascript +import { check } from 'k6'; +import tempo from 'https://jslib.k6.io/http-instrumentation-tempo/1.0.0/index.js'; +import http from 'k6/http'; + +// Explicitly instantiating a tempo client allows to distinguish +// instrumented from non-instrumented HTTP calls, by keeping APIs separate. +// It also allows for finer-grained configuration control, by letting +// users changing the tracing configuration on the fly during their +// script's execution. +const instrumentedHTTP = new tempo.Client({ + propagator: 'w3c', +}); + +const testData = { name: 'Bert' }; + +export default () => { + // Using the tracing client instance, HTTP calls will have + // their trace context headers set. + let res = instrumentedHTTP.request('GET', 'http://httpbin.org/get', null, { + headers: { + 'X-Example-Header': 'instrumented/request', + }, + }); + + // The tracing client offers more flexibility over + // the `instrumentHTTP` function, as it leaves the + // imported standard http module untouched. Thus, + // one can still perform non-instrumented HTTP calls + // using it. + res = http.post('http://httpbin.org/post', JSON.stringify(testData), { + headers: { 'X-Example-Header': 'noninstrumented/post' }, + }); + + res = instrumentedHTTP.del('http://httpbin.org/delete', null, { + headers: { 'X-Example-Header': 'instrumented/delete' }, + }); +}; +``` + +{{< /code >}} + +## HTTP module functions equivalents + +The `Client` class exposes the same API as the standard `http` module, except for the `batch` method. + +The following table lists the `Client` methods which have an equivalent in the standard `http` module: + +| Method | HTTP equivalent | Description | +| :--------------------------------------------------- | :------------------------------------------------------------------------------------------------ | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Client.del(url, [body], [params])` | [http.del](https://grafana.com/docs/k6//javascript-api/k6-http/del) | Performs an instrumented HTTP DELETE request. The method has the same prototype as the `http.del` function and should transparently act as a drop-in replacement for it. | +| `Client.get(url, [params])` | [http.get](https://grafana.com/docs/k6//javascript-api/k6-http/get) | Performs an instrumented HTTP GET request. The method has the same prototype as the `http.get` function and should transparently act as a drop-in replacement for it. | +| `Client.head(url, [params])` | [http.head](https://grafana.com/docs/k6//javascript-api/k6-http/head) | Performs an instrumented HTTP HEAD request. The method has the same prototype as the `http.head` function and should transparently act as a drop-in replacement for it. | +| `Client.options(url, [body], [params])` | [http.options](https://grafana.com/docs/k6//javascript-api/k6-http/options) | Performs an instrumented HTTP OPTIONS request. The method has the same prototype as the `http.options` function and should transparently act as a drop-in replacement for it. | +| `Client.patch(url, [body], [params])` | [http.patch](https://grafana.com/docs/k6//javascript-api/k6-http/patch) | Performs an instrumented HTTP PATCH request. The method has the same prototype as the `http.patch` function and should transparently act as a drop-in replacement for it. | +| `Client.post(url, [body], [params])` | [http.post](https://grafana.com/docs/k6//javascript-api/k6-http/post) | Performs an instrumented HTTP POST request. The method has the same prototype as the `http.post` function and should transparently act as a drop-in replacement for it. | +| `Client.put(url, [body], [params])` | [http.put](https://grafana.com/docs/k6//javascript-api/k6-http/head) | Performs an instrumented HTTP HEAD request. The method has the same prototype as the `http.put` function and should transparently act as a drop-in replacement for it. | +| `Client.request(method, url, [body], [params])` | [http.request](https://grafana.com/docs/k6//javascript-api/k6-http/request) | Performs an instrumented HTTP request. The method has the same prototype as the `http.request` function and should transparently act as a drop-in replacement for it. | +| `Client.asyncRequest(method, url, [body], [params])` | [http.asyncRequest](https://grafana.com/docs/k6//javascript-api/k6-http/asyncrequest) | Performs an instrumented HTTP asynchronous request. The method has the same prototype as the `http.asyncRequest` function and should transparently act as a drop-in replacement for it. | + +## Configuration + +`Client` instances support being reconfigured using the following API: + +| Method | Description | +| :-------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Client.configure(options)` | Reconfigures the tracing client instance with the provided [`Options`](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-tempo/options) | diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/instrumenthttp.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/instrumenthttp.md new file mode 100644 index 000000000..3622e47b4 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/instrumenthttp.md @@ -0,0 +1,57 @@ +--- +aliases: + - ../../k6-experimental/tracing/instrumenthttp/ # docs/k6//javascript-api/k6-experimental/tracing/instrumenthttp/ +title: 'instrumentHTTP' +description: 'instrumentHTTP instruments the k6 http module with tracing capabilities.' +weight: 01 +--- + +# instrumentHTTP + +The `instrumentHTTP` function instruments the k6 http module with tracing capabilities. It transparently replaces each of the k6 http module functions with versions that automatically attach a trace context to every request. Instrumented functions include [del](https://grafana.com/docs/k6//javascript-api/k6-http/del),[get](https://grafana.com/docs/k6//javascript-api/k6-http/get),[head](https://grafana.com/docs/k6//javascript-api/k6-http/head),[options](https://grafana.com/docs/k6//javascript-api/k6-http/options),[patch](https://grafana.com/docs/k6//javascript-api/k6-http/patch),[post](https://grafana.com/docs/k6//javascript-api/k6-http/post),[put](https://grafana.com/docs/k6//javascript-api/k6-http/head), and [request](https://grafana.com/docs/k6//javascript-api/k6-http/request). + +The `instrumentHTTP` automatically adds tracing information to HTTP requests performed using the `k6/http` module functions (mentioned above). +This means that, to instrument the HTTP requests, you don't need to rewrite any code. +Instead, call it once in the init context. +From that point forward, all requests made by the HTTP module from that point forward will have a trace context header added to them, and the metadata for the data-point output will have the used `trace_id`. For details about propagation, refer to [About trace contexts](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-tempo#about-trace-contexts). + +## Parameters + +| Name | Type | Description | +| :-------- | :------------------------------------------------------------------------------------------------------------ | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `options` | [`Options`](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-tempo/options) | Configures the tracing behavior with the provided [`Options`](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-tempo/options) object. | + +## Example + +This example demonstrates how to call the `instrumentHTTP` function in the script's init context. From that point forward, all the requests made by the HTTP module have a trace context header attached. + +{{< code >}} + +```javascript +import { check } from 'k6'; +import tempo from 'https://jslib.k6.io/http-instrumentation-tempo/1.0.0/index.js'; +import http from 'k6/http'; + +// instrumentHTTP will ensure that all requests made by the http module +// will be traced. The first argument is a configuration object that +// can be used to configure the tracer. +// +// Currently supported HTTP methods are: get, post, put, patch, head, +// del, options, and request. +tempo.instrumentHTTP({ + // propagator defines the trace context propagation format. + // Currently supported: w3c and jaeger. + // Default: w3c + propagator: 'w3c', +}); + +export default () => { + const res = http.get('http://httpbin.org/get', { + headers: { + 'X-Example-Header': 'instrumented/get', + }, + }); +}; +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/options.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/options.md new file mode 100644 index 000000000..2f08f625d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/http-instrumentation-tempo/options.md @@ -0,0 +1,18 @@ +--- +aliases: + - ../../k6-experimental/tracing/options/ # docs/k6//javascript-api/k6-experimental/tracing/options/ +title: 'Options' +description: 'Options allows to configure the tracing instrumentation behavior.' +weight: 03 +--- + +# Options + +Use the `Options` object to configure the tracing instrumentation behavior. It is used during the instantiation of a [`Client`](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-tempo/client) instance and also as a parameter to the [`instrumentHTTP`](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-tempo/instrumenthttp) function. It controls the general behavior of the tracing instrumentation and is unspecific to any particular tracing client instance. + +## Options + +| Option name | Type | Default | Description | +| :----------- | :------- | :------ | :------------------------------------------------------------------------------------------------ | +| `propagator` | `string` | `w3c` | The trace context propagation format. Currently supported: `w3c` and `jaeger`. | +| `sampling` | `number` | `1` | A number between 0 and 1 defining the sampling rate. 1 means sample always, 0 means never sample. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/_index.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/_index.md new file mode 100644 index 000000000..23bc5a629 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/_index.md @@ -0,0 +1,114 @@ +--- +title: 'httpx' +description: 'httpx is a wrapper library around the native k6 http module' +weight: 02 +--- + +# httpx + +{{< admonition type="note" >}} + +The source code for this library can be found in the [grafana/k6-jslib-httpx](https://github.com/k6io/k6-jslib-httpx) GitHub repository. + +{{< /admonition >}} + +The `httpx` module is an external JavaScript library that wraps around the native [k6/http](https://grafana.com/docs/k6//javascript-api/k6-http) module. +It's an HTTP client with features that are not yet available in the native module. + +- Ability to set HTTP options globally (such as timeout). +- Ability to set default tags and headers that will be used for all requests. +- More user-friendly arguments to request functions (get, post, put take the same arguments). + +The `httpx` module integrates well with the expect library. + +{{< admonition type="caution" >}} + +This library is in active development. It's stable enough to be useful, but you can watch the [GitHub repository](https://github.com/k6io/k6-jslib-httpx) to be notified when a new version is released. + +{{< /admonition >}} + +### Methods + +| Function | Description | +| ------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------ | +| [asyncRequest(method, url, [body], [params])](https://grafana.com/docs/k6//javascript-api/jslib/httpx/asyncrequest) | Generic method for making arbitrary, asynchronous HTTP requests. | +| [request(method, url, [body], [params])](https://grafana.com/docs/k6//javascript-api/jslib/httpx/request) | Generic method for making arbitrary HTTP requests. | +| [get(url, [body], [params])](https://grafana.com/docs/k6//javascript-api/jslib/httpx/get) | Makes GET request | +| [post(url, [body], [params])](https://grafana.com/docs/k6//javascript-api/jslib/httpx/post) | Makes POST request | +| [put(url, [body], [params])](https://grafana.com/docs/k6//javascript-api/jslib/httpx/put) | Makes PUT request | +| [patch(url, [body], [params])](https://grafana.com/docs/k6//javascript-api/jslib/httpx/patch) | Makes PATCH request | +| [delete(url, [body], [params])](https://grafana.com/docs/k6//javascript-api/jslib/httpx/delete) | Makes DELETE request | +| [batch(requests)](https://grafana.com/docs/k6//javascript-api/jslib/httpx/batch) | Batches multiple HTTP requests together to issue them in parallel. | +| [setBaseUrl(url)](https://grafana.com/docs/k6//javascript-api/jslib/httpx/setbaseurl) | Sets the base URL for the session | +| [addHeader(key, value)](https://grafana.com/docs/k6//javascript-api/jslib/httpx/addheader) | Adds a header to the session | +| [addHeaders(object)](https://grafana.com/docs/k6//javascript-api/jslib/httpx/addheaders) | Adds multiple headers to the session | +| [clearHeader(name)](https://grafana.com/docs/k6//javascript-api/jslib/httpx/clearheader) | Removes header from the session | +| [addTag(key, value)](https://grafana.com/docs/k6//javascript-api/jslib/httpx/addtag) | Adds a tag to the session | +| [addTags(object)](https://grafana.com/docs/k6//javascript-api/jslib/httpx/addtags) | Adds multiple tags to the session | +| [clearTag(name)](https://grafana.com/docs/k6//javascript-api/jslib/httpx/cleartag) | Removes tag from the session | + +### Example + +{{< code >}} + +```javascript +import { fail } from 'k6'; +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; +import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; + +const USERNAME = `user${randomIntBetween(1, 100000)}@example.com`; // random email address +const PASSWORD = 'superCroc2021'; + +const session = new Httpx({ + baseURL: 'https://test-api.k6.io', + headers: { + 'User-Agent': 'My custom user agent', + 'Content-Type': 'application/x-www-form-urlencoded', + }, + timeout: 20000, // 20s timeout. +}); + +export default function testSuite() { + const registrationResp = session.post(`/user/register/`, { + first_name: 'Crocodile', + last_name: 'Owner', + username: USERNAME, + password: PASSWORD, + }); + + if (registrationResp.status !== 201) { + fail('registration failed'); + } + + const loginResp = session.post(`/auth/token/login/`, { + username: USERNAME, + password: PASSWORD, + }); + + if (loginResp.status !== 200) { + fail('Authentication failed'); + } + + const authToken = loginResp.json('access'); + + // set the authorization header on the session for the subsequent requests. + session.addHeader('Authorization', `Bearer ${authToken}`); + + const payload = { + name: `Croc Name`, + sex: 'M', + date_of_birth: '2019-01-01', + }; + + // this request uses the Authorization header set above. + const respCreateCrocodile = session.post(`/my/crocodiles/`, payload); + + if (respCreateCrocodile.status !== 201) { + fail('Crocodile creation failed'); + } else { + console.log('New crocodile created'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addheader.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addheader.md new file mode 100644 index 000000000..e662c8c4e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addheader.md @@ -0,0 +1,31 @@ +--- +title: 'addHeader( key, value )' +description: 'adds a header to the session' +description: 'adds a header to the session' +weight: 21 +--- + +# addHeader( key, value ) + +| Parameter | Type | Description | +| --------- | ------ | ------------ | +| name | string | Header name | +| value | string | Header value | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx({ baseURL: 'https://test-api.k6.io' }); + +session.addHeader('Authorization', 'token1'); + +export default function () { + session.get('/public/crocodiles/1/'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addheaders.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addheaders.md new file mode 100644 index 000000000..0c3994262 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addheaders.md @@ -0,0 +1,34 @@ +--- +title: 'addHeaders( object )' +description: 'adds multiple headers to the session' +description: 'adds multiple headers to the session' +weight: 22 +--- + +# addHeaders( object ) + +| Parameter | Type | Description | +| --------- | ------ | ----------- | +| headers | object | Object | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx(); + +session.addHeaders({ + 'Authorization': 'token1', + 'User-Agent': 'My custom user agent', + 'Content-Type': 'application/x-www-form-urlencoded', +}); + +export default function () { + session.get('https://test-api.k6.io/public/crocodiles/1/'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addtag.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addtag.md new file mode 100644 index 000000000..cea24539b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addtag.md @@ -0,0 +1,32 @@ +--- +title: 'addTag( key, value )' +description: 'adds a tag to the session' +description: 'adds a tag to the session' +weight: 24 +--- + +# addTag( key, value ) + +| Parameter | Type | Description | +| --------- | ------ | ----------- | +| name | string | Tag name | +| value | string | Tag value | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx({ baseURL: 'https://test-api.k6.io' }); + +session.addTag('tagName', 'tagValue'); +session.addTag('AnotherTagName', 'tagValue2'); + +export default function () { + session.get('/public/crocodiles/1/'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addtags.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addtags.md new file mode 100644 index 000000000..ed87dbdb6 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/addtags.md @@ -0,0 +1,34 @@ +--- +title: 'addTags( object )' +description: 'adds multiple tags to the session' +description: 'adds multiple tags to the session' +weight: 25 +--- + +# addTags( object ) + +| Parameter | Type | Description | +| --------- | ------ | ----------- | +| headers | object | Object | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx(); + +session.addTags({ + Tag1: 'value1', + Tag2: 'value2', + Tag3: 'value3', +}); + +export default function () { + session.get('https://test-api.k6.io/public/crocodiles/1/'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/asyncrequest.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/asyncrequest.md new file mode 100644 index 000000000..1dfd7aa0d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/asyncrequest.md @@ -0,0 +1,55 @@ +--- +title: 'asyncRequest(method, url, [body], [params])' +head_title: 'httpx.asyncRequest()' +description: 'Generic method for making asynchronous HTTP requests' +description: 'Generic method for making asynchronous HTTP requests' +weight: 08 +--- + +# asyncRequest(method, url, [body], [params]) + +Generic method for making arbitrary asynchronous HTTP requests. +Note, this method returns a Promise. You must use the `await` keyword to resolve it. + +| Parameter | Type | Description | +| ----------------- | --------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| method | string | HTTP method. Must be uppercase (GET, POST, PUT, PATCH, OPTIONS, HEAD, etc) | +| url | string | HTTP URL. If baseURL is set, provide only path. | +| body (optional) | null / string / object / ArrayBuffer / [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) | Request body; objects will be `x-www-form-urlencoded`. Set to `null` to omit the body. | +| params (optional) | null or object {} | Additional [parameters](https://grafana.com/docs/k6//javascript-api/k6-http/params) for this specific request. | + +### Returns + +| Type | Description | +| --------------------- | ------------------------------------------------------------------------------------------------- | +| Promise with Response | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx({ + baseURL: 'https://httpbin.test.k6.io', + timeout: 20000, // 20s timeout. +}); + +export default async function testSuite() { + const resp_get = await session.asyncRequest('GET', `/status/200`); + const resp_post = await session.asyncRequest('POST', `/status/200`, { key: 'value' }); + const resp_put = await session.asyncRequest('PUT', `/status/200`, { key: 'value' }); + const resp_patch = await session.asyncRequest('PATCH', `/status/200`, { key: 'value' }); + const resp_delete = await session.asyncRequest('DELETE', `/status/200`); + + // specific methods are also available. + const respGet = await session.asyncGet(`/status/200`); + const respPost = await session.asyncPost(`/status/200`, { key: 'value' }); + const respPut = await session.asyncPut(`/status/200`, { key: 'value' }); + const respPatch = await session.asyncPatch(`/status/200`, { key: 'value' }); + const respDelete = await session.asyncDelete(`/status/200`); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/batch.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/batch.md new file mode 100644 index 000000000..34ea828f0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/batch.md @@ -0,0 +1,62 @@ +--- +title: 'batch( requests )' +head_title: 'httpx.batch(requests)' +description: 'Issue multiple HTTP requests in parallel (like e.g. browsers tend to do).' +description: 'Issue multiple HTTP requests in parallel (like e.g. browsers tend to do).' +weight: 19 +--- + +# batch( requests ) + +Batch multiple HTTP requests together, to issue them in parallel over multiple TCP connections. + +| Parameter | Type | Description | +| ----------------- | ------ | ------------------------------------------------------ | +| requests | array | An array containing requests, in string or object form | +| params (optional) | object | Additional parameters for all requests in the batch | + +### Returns + +| Type | Description | +| ----- | ----------------------------------------------------------------------------------------------------------------- | +| array | An array containing [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) objects. | + +### Example + +{{< code >}} + +```javascript +import { Httpx, Get } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; +import { describe } from 'https://jslib.k6.io/expect/0.0.4/index.js'; + +const session = new Httpx({ baseURL: 'https://test-api.k6.io' }); + +export default function () { + describe('01. Fetch public crocodiles all at once', (t) => { + const responses = session.batch( + [ + new Get('/public/crocodiles/1/'), + new Get('/public/crocodiles/2/'), + new Get('/public/crocodiles/3/'), + new Get('/public/crocodiles/4/'), + ], + { + tags: { name: 'PublicCrocs' }, + } + ); + + responses.forEach((response) => { + t.expect(response.status) + .as('response status') + .toEqual(200) + .and(response) + .toHaveValidJson() + .and(response.json('age')) + .as('croc age') + .toBeGreaterThan(7); + }); + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/clearheader.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/clearheader.md new file mode 100644 index 000000000..721dfaaaa --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/clearheader.md @@ -0,0 +1,30 @@ +--- +title: 'clearHeader( name )' +description: 'removes header from the session' +description: 'removes header from the session' +weight: 23 +--- + +# clearHeader( name ) + +| Parameter | Type | Description | +| --------- | ------ | ------------------------- | +| name | string | Header name to be removed | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx({ headers: { Authorization: 'token1' } }); + +session.clearHeader('Authorization'); // removes header set in the constructor + +export default function () { + session.get('https://test-api.k6.io/public/crocodiles/1/'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/cleartag.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/cleartag.md new file mode 100644 index 000000000..5328b36ba --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/cleartag.md @@ -0,0 +1,30 @@ +--- +title: 'clearTag( name )' +description: 'removes tag from the session' +description: 'removes tag from the session' +weight: 26 +--- + +# clearTag( name ) + +| Parameter | Type | Description | +| --------- | ------ | ---------------------- | +| name | string | Tag name to be removed | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx({ tags: { tagName: 'tagValue' } }); + +session.clearTag('tagName'); // removes tag set in the constructor + +export default function () { + session.get('https://test-api.k6.io/public/crocodiles/1/'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/delete.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/delete.md new file mode 100644 index 000000000..7b1a1499d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/delete.md @@ -0,0 +1,41 @@ +--- +title: 'delete(url, [body], [params])' +description: 'httpx.delete makes DELETE requests' +description: 'httpx.delete makes DELETE requests' +weight: 14 +--- + +# delete(url, [body], [params]) + +`session.delete(url, body, params)` makes a DELETE request. Only the first parameter is required. Body is discouraged. + +| Parameter | Type | Description | +| ----------------- | --------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| url | string | HTTP URL. If baseURL is set, provide only path. | +| body (optional) | null / string / object / ArrayBuffer / [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) | Request body; objects will be `x-www-form-urlencoded`. Set to `null` to omit the body. | +| params (optional) | null or object {} | Additional [parameters](https://grafana.com/docs/k6//javascript-api/k6-http/params) for this specific request. | + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx({ + baseURL: 'https://httpbin.test.k6.io', + timeout: 20000, // 20s timeout. +}); + +export default function testSuite() { + const resp = session.delete(`/delete`); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/get.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/get.md new file mode 100644 index 000000000..3af87819b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/get.md @@ -0,0 +1,41 @@ +--- +title: 'get(url, [body], [params])' +description: 'httpx.get makes GET requests' +description: 'httpx.get makes GET requests' +weight: 10 +--- + +# get(url, [body], [params]) + +`session.get(url, body, params)` makes a GET request. Only the URL parameter is required + +| Parameter | Type | Description | +| ----------------- | --------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| url | string | HTTP URL. If baseURL is set, provide only path. | +| body (optional) | null / string / object / ArrayBuffer / [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) | Set to `null` to omit the body. | +| params (optional) | null or object {} | Additional [parameters](https://grafana.com/docs/k6//javascript-api/k6-http/params) for this specific request. | + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx({ + baseURL: 'https://test-api.k6.io', + timeout: 20000, // 20s timeout. +}); + +export default function testSuite() { + const resp = session.get(`/public/crocodiles/`); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/head.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/head.md new file mode 100644 index 000000000..7cf066111 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/head.md @@ -0,0 +1,42 @@ +--- +title: 'head(url, [body], [params])' +description: 'httpx.head makes HEAD requests' +description: 'httpx.head makes HEAD requests' +weight: 16 +--- + +# head(url, [body], [params]) + +`session.head(url, body, params)` makes a HEAD request. Only the first parameter is required + +| Parameter | Type | Description | +| ----------------- | --------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| url | string | HTTP URL. If baseURL is set, provide only path. | +| body (optional) | null / string / object / ArrayBuffer / [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) | Request body; objects will be `x-www-form-urlencoded`. Set to `null` to omit the body. | +| params (optional) | null or object {} | Additional [parameters](https://grafana.com/docs/k6//javascript-api/k6-http/params) for this specific request. | + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx({ + baseURL: 'https://httpbin.test.k6.io', + timeout: 20000, // 20s timeout. +}); + +export default function testSuite() { + const resp = session.head(`/head`); + console.log(resp.status); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/options.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/options.md new file mode 100644 index 000000000..d7310722a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/options.md @@ -0,0 +1,42 @@ +--- +title: 'options(url, [body], [params])' +description: 'httpx.options makes OPTIONS requests' +description: 'httpx.options makes OPTIONS requests' +weight: 15 +--- + +# options(url, [body], [params]) + +`session.options(url, body, params)` makes an OPTIONS request. Only the first parameter is required + +| Parameter | Type | Description | +| ----------------- | --------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| url | string | HTTP URL. If baseURL is set, provide only path. | +| body (optional) | null / string / object / ArrayBuffer / [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) | Request body; objects are `x-www-form-urlencoded`. To omit the body, set to `null`. | +| params (optional) | null or object {} | Additional [parameters](https://grafana.com/docs/k6//javascript-api/k6-http/params) for this specific request. | + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx({ + baseURL: 'https://httpbin.test.k6.io', + timeout: 20000, // 20s timeout. +}); + +export default function testSuite() { + const resp = session.options(`/options`); + console.log(resp.status); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/patch.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/patch.md new file mode 100644 index 000000000..c21488464 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/patch.md @@ -0,0 +1,47 @@ +--- +title: 'patch(url, [body], [params])' +head_title: 'httpx.patch' +description: 'httpx.patch makes PATCH requests' +description: 'httpx.patch makes PATCH requests' +weight: 13 +--- + +# patch(url, [body], [params]) + +`session.patch(url, body, params)` makes a PATCH request. Only the first parameter is required + +| Parameter | Type | Description | +| ----------------- | --------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| url | string | HTTP URL. If baseURL is set, provide only path. | +| body (optional) | null / string / object / ArrayBuffer / [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) | Request body; objects are `x-www-form-urlencoded`. To omit body, set to `null` . | +| params (optional) | null or object {} | Additional [parameters](https://grafana.com/docs/k6//javascript-api/k6-http/params) for this specific request. | + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx({ + baseURL: 'https://httpbin.test.k6.io', + timeout: 20000, // 20s timeout. +}); + +export default function testSuite() { + const resp = session.patch(`/patch`, { + first_name: 'Mr', + last_name: 'Croco', + username: 'my user', + password: 'my password', + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/post.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/post.md new file mode 100644 index 000000000..877284a77 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/post.md @@ -0,0 +1,45 @@ +--- +title: 'post(url, [body], [params])' +head_title: 'httpx.post' +description: 'httpx.post makes POST requests' +description: 'httpx.post makes POST requests' +weight: 11 +--- + +# post(url, [body], [params]) + +| Parameter | Type | Description | +| ----------------- | --------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| url | string | HTTP URL. If baseURL is set, provide only path. | +| body (optional) | null / string / object / ArrayBuffer / [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) | Request body; objects are `x-www-form-urlencoded`. To omit body, set to `null` . | +| params (optional) | null or object {} | Additional [parameters](https://grafana.com/docs/k6//javascript-api/k6-http/params) for this specific request. | + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx({ + baseURL: 'https://test-api.k6.io', + timeout: 20000, // 20s timeout. +}); + +export default function testSuite() { + const resp = session.post(`/user/register/`, { + first_name: 'Mr', + last_name: 'Croco', + username: 'my user', + password: 'my password', + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/put.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/put.md new file mode 100644 index 000000000..b865d5522 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/put.md @@ -0,0 +1,48 @@ +--- +title: 'put(url, [body], [params])' +head_title: 'httpx.put' +description: 'httpx.put makes PUT requests' +description: 'httpx.put makes PUT requests' +weight: 12 +--- + +# put(url, [body], [params]) + +`session.put(url, body, params)` makes a PUT request. Only the first parameter is required + +| Parameter | Type | Description | +| ----------------- | --------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| url | string | HTTP URL. If baseURL is set, provide only path. | +| body (optional) | null / string / object / ArrayBuffer / [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) | Request body; objects are `x-www-form-urlencoded`. To omit body, set to `null`. | +| params (optional) | null or object {} | Additional [parameters](https://grafana.com/docs/k6//javascript-api/k6-http/params) for this specific request. | + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx({ + baseURL: 'https://httpbin.test.k6.io', + timeout: 20000, // 20s timeout. +}); + +export default function testSuite() { + const resp = session.put(`/put`, { + first_name: 'Mr', + last_name: 'Croco', + username: 'my user', + password: 'my password', + }); + console.log(resp.status); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/request.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/request.md new file mode 100644 index 000000000..774fc143b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/request.md @@ -0,0 +1,49 @@ +--- +title: 'request(method, url, [body], [params])' +head_title: 'httpx.request()' +description: 'Generic method for making arbitrary HTTP requests' +description: 'Generic method for making arbitrary HTTP requests' +weight: 09 +--- + +# request(method, url, [body], [params]) + +Generic method for making arbitrary HTTP requests. + +Consider using specific methods for making common requests [get](https://grafana.com/docs/k6//javascript-api/jslib/httpx/get), [post](https://grafana.com/docs/k6//javascript-api/jslib/httpx/post), [put](https://grafana.com/docs/k6//javascript-api/jslib/httpx/put), [patch](https://grafana.com/docs/k6//javascript-api/jslib/httpx/patch). + +| Parameter | Type | Description | +| ----------------- | --------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| method | string | HTTP method. Must be uppercase (GET, POST, PUT, PATCH, OPTIONS, HEAD, etc) | +| url | string | HTTP URL. If baseURL is set, provide only path. | +| body (optional) | null / string / object / ArrayBuffer / [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) | Request body; objects are `x-www-form-urlencoded`. To omit body, set to `null`. | +| params (optional) | null or object {} | Additional [parameters](https://grafana.com/docs/k6//javascript-api/k6-http/params) for this specific request. | + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx({ + baseURL: 'https://httpbin.test.k6.io', + timeout: 20000, // 20s timeout. +}); + +export default function testSuite() { + const resp_get = session.request('GET', `/status/200`); + const resp_post = session.request('POST', `/status/200`, { key: 'value' }); + const resp_put = session.request('PUT', `/status/200`, { key: 'value' }); + const resp_patch = session.request('PATCH', `/status/200`, { key: 'value' }); + const resp_delete = session.request('DELETE', `/status/200`); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/setbaseurl.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/setbaseurl.md new file mode 100644 index 000000000..5c0e06130 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/setbaseurl.md @@ -0,0 +1,30 @@ +--- +title: 'setBaseUrl( url )' +description: 'sets the base URL for the session' +description: 'sets the base URL for the session' +weight: 20 +--- + +# setBaseUrl( url ) + +| Parameter | Type | Description | +| --------- | ------ | ---------------------------------------------------------- | +| baseURL | string | Base URL to be used for all requests issued in the session | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx(); + +session.setBaseUrl('https://test-api.k6.io'); + +export default function () { + session.get('/public/crocodiles/1/'); // baseUrl doesn't need to be repeated on every request +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/trace.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/trace.md new file mode 100644 index 000000000..6057a953f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/httpx/trace.md @@ -0,0 +1,42 @@ +--- +title: 'trace(url, [body], [params])' +description: 'httpx.trace makes TRACE requests' +description: 'httpx.trace makes TRACE requests' +weight: 17 +--- + +# trace(url, [body], [params]) + +`session.trace(url, body, params)` makes a TRACE request. Only the first parameter is required + +| Parameter | Type | Description | +| ----------------- | --------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| url | string | HTTP URL. If baseURL is set, provide only path. | +| body (optional) | null / string / object / ArrayBuffer / [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) | Request body; objects will be `x-www-form-urlencoded`. Set to `null` to omit the body. | +| params (optional) | null or object {} | Additional [parameters](https://grafana.com/docs/k6//javascript-api/k6-http/params) for this specific request. | + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Example + +{{< code >}} + +```javascript +import { Httpx } from 'https://jslib.k6.io/httpx/0.1.0/index.js'; + +const session = new Httpx({ + baseURL: 'https://httpbin.test.k6.io', + timeout: 20000, // 20s timeout. +}); + +export default function testSuite() { + const resp = session.trace(`/trace`); + console.log(resp.status); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/_index.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/_index.md new file mode 100644 index 000000000..4cc30d878 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/_index.md @@ -0,0 +1,83 @@ +--- +title: 'k6chaijs' +description: 'Assertion library for k6' +weight: 03 +--- + +# k6chaijs + +{{< admonition type="note" >}} + +The source code for this library can be found in the [grafana/k6-jslib-k6chaijs](https://github.com/grafana/k6-jslib-k6chaijs) GitHub repository. + +{{< /admonition >}} + +`k6chaijs` is a library to provide BDD assertions in k6 based on [ChaiJS](https://www.chaijs.com/). You can use `k6chaijs` as an alternative to [check](https://grafana.com/docs/k6//javascript-api/k6/check) and [group](https://grafana.com/docs/k6//javascript-api/k6/group). + +With this library, you get the following: + +- BDD style of assertions for more expressive language +- Chainable assertions +- More powerful assertions functions such as: `deep`, `nested`, `ordered`, etc. +- Automatic assertion messages +- [Exception handling](https://grafana.com/docs/k6//javascript-api/jslib/k6chaijs/error-handling) for better test stability + +## Installation + +This library is hosted on [jslib](https://jslib.k6.io/) and can be imported in directly in your k6 script. + +```javascript +import { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js'; +``` + +Alternatively, you can use a copy of this file stored locally. The source code is available on [GitHub](https://github.com/grafana/k6-jslib-k6chaijs). + +## Example + +The following example tests a hypothetical HTTP API that returns a JSON array of objects. Copy the following code, and save it as `script.js`: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js'; + +export default function testSuite() { + describe('Fetch a list of public crocodiles', () => { + const response = http.get('https://test-api.k6.io/public/crocodiles'); + + expect(response.status, 'response status').to.equal(200); + expect(response).to.have.validJsonBody(); + expect(response.json().length, 'number of crocs').to.be.above(4); + }); +} +``` + +{{< /code >}} + +When you run this test with `k6 run script.js`, the output at the end of the test shows: + +```bash +█ Fetch a list of public crocodiles + ✓ expected response status to equal 200 + ✓ has valid json body + ✓ expected number of crocs to be above 4 +``` + +If you are familiar with k6, the result is the same as using [check](https://grafana.com/docs/k6//javascript-api/k6/check) and [group](https://grafana.com/docs/k6//javascript-api/k6/group). Note that [expect](https://grafana.com/docs/k6//javascript-api/jslib/k6chaijs/expect) might add or extend the assertion message. + +## API + +| API | Description | +| ------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [config](https://grafana.com/docs/k6//javascript-api/jslib/k6chaijs/config) | Options to change `k6chaijs` behaviour. | +| [describe](https://grafana.com/docs/k6//javascript-api/jslib/k6chaijs/describe) | A wrapper of [group](https://grafana.com/docs/k6//javascript-api/k6/group) that catches exceptions to allow continuing the test execution. It returns a boolean to indicate the success of all its `k6chaijs` assertions. | +| [expect](https://grafana.com/docs/k6//javascript-api/jslib/k6chaijs/expect) | A wrapper of [check](https://grafana.com/docs/k6//javascript-api/k6/check) that provides BDD style of assertions. | + +## Plugins + +It's possible to extend the default functionality with [Chai plugins](https://www.chaijs.com/plugins/). To use a plugin or build a Chai version with plugins, follow the instructions in this [example](https://community.grafana.com/t/how-to-build-plugins-for-chaijs/97010/3). + +## Read more + +- [Using chai with k6](https://k6.io/blog/k6-chai-js/) diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/config.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/config.md new file mode 100644 index 000000000..514f01483 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/config.md @@ -0,0 +1,53 @@ +--- +title: 'config' +description: 'Global configuration options for k6Chaijs' +weight: 31 +--- + +# config + +Chai exposes a few options to change the library configuration. + +| Config option | Default | Description | +| ------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| truncateMsgThreshold | 300 | Check message length is truncated to this value. | +| truncateVariableThreshold | 100 | Variables interpolated into check message are truncated to this length. It prevents mistakes when the check name is very large, especially when `aggregateChecks` is `false`. | +| aggregateChecks | true | The actual values are not interpolated into the check message. Disable for tests with 1 iteration. | +| logFailures | false | When the check fails, debug messages are printed. | + +{{< code >}} + +```javascript +import http from 'k6/http'; +import chai, { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js'; + +// individual variables should be up to 20 chars after rendering. +chai.config.truncateVariableThreshold = 20; + +// whole check() message must be below 300 chars. +chai.config.truncateMsgThreshold = 300; + +// the variable values (resp.status) are inserted into the check message - useful for debugging or tests with 1 iteration +chai.config.aggregateChecks = false; + +// when the check fails, WARN messages with variables are printed. Useful for debugging. +chai.config.logFailures = true; + +export default function testSuite() { + describe('Testing bad assertion.', () => { + const response = http.get('https://test-api.k6.io/'); + + expect(response.body).to.have.lengthOf.at.least(500); + }); +} +``` + +{{< /code >}} + +The resulting + +```bash +█ Testing bad assertion. + ✓ expected '\n\n\n\n/javascript-api/k6/group) that adds the ability to: + +- [Catch exceptions](https://grafana.com/docs/k6//javascript-api/jslib/k6chaijs/error-handling) to allow continuing the execution outside of the `describe` function. +- Returns a boolean to indicate the success of all its `k6chaijs` assertions. + +{{< code >}} + +```javascript +import { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js'; + +export default function testSuite() { + const success1 = describe('Basic test', () => { + expect(1, 'number one').to.equal(1); + }); + console.log(success1); // true + + const success2 = describe('Another test', () => { + throw 'Something entirely unexpected happened'; + }); + console.log(success2); // false + + const success3 = describe('Yet another test', () => { + expect(false, 'my vaule').to.be.true(); + }); + console.log(success3); // false +} +``` + +{{< /code >}} + +{{< code >}} + +```bash +default ✓ [======================================] 1 VUs 00m00.0s/10m0s 1/1 iters, 1 per VU + + █ Basic test + ✓ expected number one to equal 1 + + █ Another test + ✗ Exception raised "Something entirely unexpected happened" + ↳ 0% — ✓ 0 / ✗ 1 + + █ Yet another test + ✗ expected my vaule to be true + ↳ 0% — ✓ 0 / ✗ 1 +``` + +{{< /code >}} + +## API + +| Parameter | Type | Description | +| --------- | -------- | ---------------------------------------------------------------------------------------------- | +| name | string | Test case name. The test case name should be unique. Otherwise, the test case will be grouped. | +| function | function | The test case function to be executed | + +### Returns + +| Type | Description | +| ---- | ------------------------------------------------------------------------------------------------------------------------------------------------- | +| bool | Returns true when all `expect` conditions within the `describe()` body were successful, and no unhandled exceptions were raised, otherwise false. | + +## Chaining describe() blocks + +If you want to skip the execution of the following `describe` blocks, consider chaining them using `&&` as shown below. + + + +```javascript +import { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js'; + +export default function testSuite() { + describe('Basic test', () => { + expect(1, 'number one').to.equal(1); + }) && + describe('Another test', () => { + throw 'Something entirely unexpected happened'; + }) && + describe('Yet another test', () => { + // the will not be executed because the prior block returned `false` + expect(false, 'my vaule').to.be.true(); + }); +} +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/error-handling.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/error-handling.md new file mode 100644 index 000000000..c45ed6c98 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/error-handling.md @@ -0,0 +1,96 @@ +--- +title: 'Error handling' +description: 'How to handle errors in k6chaijs.' +weight: 35 +--- + +# Error handling + +When you execute a load test, your System Under Test (SUT) may often become over saturated and start responding with errors. In this case, you need to consider what the iteration execution should do: + +1. to embrace the system error and continue the execution of the scenario +2. or to exit + +It's not uncommon for performance testers to forget about these cases. We often write fragile test code that assumes our system's response will always succeed and contain the expected data. + +{{< code >}} + +```javascript +import { check, group } from 'k6'; +import http from 'k6/http'; + +export default function () { + group('Fetch a list of public crocodiles', () => { + const res = http.get('https://test-api.k6.io/public/crocodiles'); + check(res, { + 'is status 200': (r) => r.status === 200, + 'got more than 5 crocs': (r) => r.json().length > 5, + }); + // ... continue test within the group... + }); + + group('other group', () => { + //... + }); +} +``` + +{{< /code >}} + +This code will work fine when the SUT returns correct responses. But, when the SUT starts to fail, `r.json().length` will throw an exception: + +```bash +ERRO[0001] cannot parse json due to an error at line 1, character 2 , error: invalid character '<' looking for beginning of value +running at reflect.methodValueCall (native) +``` + +In this case, k6 throws a JavaScript exception and **exits the execution of the current iteration** but continues starting new iterations. + +This might not be ideal because: + +1. system errors are propagated as exceptions that are not reported on the test results +2. you might want to embrace these errors - as normal - and continue the execution + +It's possible to rewrite this test to be less fragile, but it can make our test code longer and less readable. + +## Handling exceptions + +Sometimes it's hard to predict how a SUT might fail. For those cases, [describe](https://grafana.com/docs/k6//javascript-api/jslib/k6chaijs/describe) catches any internal exceptions and: + +1. records them as failed assertions +2. continues the execution (outside of its `describe()` function) + +{{< code >}} + +```javascript +import { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js'; + +export default function testSuite() { + describe('Test case against a Shaky SUT', (t) => { + throw 'Something entirely unexpected happened'; + }); + + // this test case will be executed because + // the previous `describe` catched the exception + describe('Another test case', (t) => { + expect(2).to.equal(2); + }); +} +``` + +{{< /code >}} + +{{< code >}} + +```bash +█ Test case against a Shaky SUT + + ✗ Exception raised "Something entirely unexpected happened" + ↳ 0% — ✓ 0 / ✗ 1 + +█ Another test case + + ✓ expected ${this} to equal 2 +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/expect.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/expect.md new file mode 100644 index 000000000..abfd2ada9 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/k6chaijs/expect.md @@ -0,0 +1,168 @@ +--- +title: 'expect()' +description: 'BDD style to construct k6 assertions.' +weight: 34 +--- + +# expect() + +`expect` is a wrapper of [check](https://grafana.com/docs/k6//javascript-api/k6/check) to provide BDD style of assertions in k6. It implements the [Chai Expect API](https://www.chaijs.com/api/bdd/): + + + +- [not](https://www.chaijs.com/api/bdd/#method_not) +- [deep](https://www.chaijs.com/api/bdd/#method_deep) +- [nested](https://www.chaijs.com/api/bdd/#method_nested) +- [own](https://www.chaijs.com/api/bdd/#method_own) +- [ordered](https://www.chaijs.com/api/bdd/#method_ordered) +- [any](https://www.chaijs.com/api/bdd/#method_any) +- [all](https://www.chaijs.com/api/bdd/#method_all) +- [a](https://www.chaijs.com/api/bdd/#method_a) +- [include](https://www.chaijs.com/api/bdd/#method_include) +- [ok](https://www.chaijs.com/api/bdd/#method_ok) +- [true](https://www.chaijs.com/api/bdd/#method_true) +- [false](https://www.chaijs.com/api/bdd/#method_false) +- [null](https://www.chaijs.com/api/bdd/#method_null) +- [undefined](https://www.chaijs.com/api/bdd/#method_undefined) +- [NaN](https://www.chaijs.com/api/bdd/#method_nan) +- [exist](https://www.chaijs.com/api/bdd/#method_exist) +- [empty](https://www.chaijs.com/api/bdd/#method_empty) +- [arguments](https://www.chaijs.com/api/bdd/#method_arguments) +- [equal](https://www.chaijs.com/api/bdd/#method_equal) +- [eql](https://www.chaijs.com/api/bdd/#method_eql) +- [above](https://www.chaijs.com/api/bdd/#method_above) +- [least](https://www.chaijs.com/api/bdd/#method_least) +- [below](https://www.chaijs.com/api/bdd/#method_below) +- [most](https://www.chaijs.com/api/bdd/#method_most) +- [within](https://www.chaijs.com/api/bdd/#method_within) +- [instanceof](https://www.chaijs.com/api/bdd/#method_instanceof) +- [property](https://www.chaijs.com/api/bdd/#method_property) +- [ownPropertyDescriptor](https://www.chaijs.com/api/bdd/#method_ownpropertydescriptor) +- [lengthOf](https://www.chaijs.com/api/bdd/#method_lengthOf) +- [match](https://www.chaijs.com/api/bdd/#method_match) +- [string](https://www.chaijs.com/api/bdd/#method_string) +- [keys](https://www.chaijs.com/api/bdd/#method_keys) +- [throw](https://www.chaijs.com/api/bdd/#method_throw) +- [respondTo](https://www.chaijs.com/api/bdd/#method_respondto) +- [itself](https://www.chaijs.com/api/bdd/#method_itself) +- [satisfy](https://www.chaijs.com/api/bdd/#method_satisfy) +- [closeTo](https://www.chaijs.com/api/bdd/#method_closeto) +- [members](https://www.chaijs.com/api/bdd/#method_members) +- [oneOf](https://www.chaijs.com/api/bdd/#method_oneOf) +- [change](https://www.chaijs.com/api/bdd/#method_change) +- [increase](https://www.chaijs.com/api/bdd/#method_increase) +- [decrease](https://www.chaijs.com/api/bdd/#method_decrease) +- [by](https://www.chaijs.com/api/bdd/#method_by) +- [extensible](https://www.chaijs.com/api/bdd/#method_extensible) +- [sealed](https://www.chaijs.com/api/bdd/#method_sealed) +- [frozen](https://www.chaijs.com/api/bdd/#method_frozen) +- [finite](https://www.chaijs.com/api/bdd/#method_finite) + + + +{{< code >}} + + + +```javascript +import http from 'k6/http'; +import { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js'; + +export default function () { + describe('Basic test', () => { + expect({ a: 1 }).to.not.have.property('b'); + expect(2).to.equal(2); + expect({ a: 1 }).to.deep.equal({ a: 1 }); + expect({ a: { b: ['x', 'y'] } }).to.have.nested.property('a.b[1]'); + expect({ a: 1 }).to.have.own.property('a'); + expect([1, 2]).to.have.ordered.members([1, 2]).but.not.have.ordered.members([2, 1]); + expect({ a: 1, b: 2 }).to.not.have.any.keys('c', 'd'); + expect({ a: 1, b: 2 }).to.have.all.keys('a', 'b'); + expect('foo').to.be.a('string'); + expect([1, 2, 3]).to.be.an('array').that.includes(2); + expect('foobar').to.include('foo'); + expect(1).to.equal(1); + expect(true).to.be.true; + expect(false).to.be.false; + expect(null).to.be.null; + expect(undefined).to.be.undefined; + expect(NaN).to.be.NaN; + expect(1).to.equal(1); + expect([]).to.be.empty; + expect({}).not.to.be.arguments; + expect(1).to.equal(1); + expect({ a: 1 }).to.eql({ a: 1 }).but.not.equal({ a: 1 }); + expect('foo').to.have.lengthOf(3); + expect(2).to.equal(2); + expect(1).to.equal(1); + expect(2).to.be.at.most(3); + expect(2).to.equal(2); + expect({ a: 1 }).to.not.be.an.instanceof(Array); + expect({ a: 1 }).to.have.property('a'); + expect([1, 2, 3]).to.have.lengthOf(3); + expect('foobar').to.match(/^foo/); + expect('foobar').to.have.string('bar'); + expect({ a: 1, b: 2 }).to.have.all.keys('a', 'b'); + const badFn = function () { + throw new TypeError('Illegal salmon!'); + }; + expect(badFn).to.throw(); + expect(1).to.satisfy(function (num) { + return num > 0; + }); + expect(1.5).to.equal(1.5); + expect([1, 2, 3]).to.have.members([2, 1, 3]); + expect('Today is sunny').to.contain.oneOf(['sunny', 'cloudy']); + expect({ a: 1 }).to.be.extensible; + expect(1).to.be.finite; + }); +} +``` + +{{< /code >}} + +When you run this test with `k6 run script.js`, the output at the end of the test shows: + +```bash +default ✓ [======================================] 1 VUs 00m00.0s/10m0s 1/1 iters, 1 per VU + + █ Basic test + + ✓ expected ${this} to not have property 'b' + ✓ expected ${this} to equal 2 + ✓ expected ${this} to deeply equal { a: 1 } + ✓ expected ${this} to have nested property 'a.b[1]' + ✓ expected ${this} to have own property 'a' + ✓ expected ${this} to be an array + ✓ expected ${this} to have the same ordered members as [ 1, 2 ] + ✓ expected ${this} to not have the same ordered members as [ 2, 1 ] + ✓ expected ${this} to not have keys 'c', or 'd' + ✓ expected ${this} to have keys 'a', and 'b' + ✓ expected ${this} to be a string + ✓ expected ${this} to include 2 + ✓ expected ${this} to include 'foo' + ✓ expected ${this} to equal 1 + ✓ expected ${this} to be true + ✓ expected ${this} to be false + ✓ expected ${this} to be null + ✓ expected ${this} to be undefined + ✓ expected ${this} to be NaN + ✓ expected ${this} to be empty + ✓ expected ${this} to not be arguments + ✓ expected ${this} to not equal { a: 1 } + ✓ expected ${this} to have property 'length' + ✓ expected ${this} to have a length of 3 got ${actual} + ✓ expected ${this} to be at most 3 + ✓ expected ${this} to not be an instance of Array + ✓ expected ${this} to have property 'a' + ✓ expected ${this} to match /^foo/ + ✓ expected ${this} to contain 'bar' + ✓ expected ${this} to be a function + ✓ expected ${this} to throw an error + ✓ expected ${this} to satisfy [Function] + ✓ expected ${this} to equal 1.5 + ✓ expected ${this} to have the same members as [ 2, 1, 3 ] + ✓ expected ${this} to contain one of [ 'sunny', 'cloudy' ] + ✓ expected ${this} to be extensible + ✓ expected ${this} to be a finite number +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/_index.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/_index.md new file mode 100644 index 000000000..f832cd686 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/_index.md @@ -0,0 +1,62 @@ +--- +title: 'utils' +description: 'A collection of small utility functions useful during load testing with k6. ' +weight: 04 +--- + +# utils + +{{< admonition type="note" >}} + +The source code for this library can be found in the [grafana/k6-jslib-utils](https://github.com/k6io/k6-jslib-utils) GitHub repository. + +{{< /admonition >}} + +The `utils` module contains number of small utility functions useful in every day load testing. + +| Function | Description | +| ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [check(val, sets, [tags])](https://grafana.com/docs/k6//javascript-api/jslib/utils/check) | Drop-in replacement for [`check`](https://grafana.com/docs/k6//javascript-api/k6/check/) with support for async values. | +| [randomIntBetween(min, max)](https://grafana.com/docs/k6//javascript-api/jslib/utils/randomintbetween) | Random integer in a given range | +| [randomItem(array)](https://grafana.com/docs/k6//javascript-api/jslib/utils/randomitem) | Random item from a given array | +| [randomString(length, [charset])](https://grafana.com/docs/k6//javascript-api/jslib/utils/randomstring) | Random string of a given length, optionally selected from a custom character set | +| [uuidv4()](https://grafana.com/docs/k6//javascript-api/jslib/utils/uuidv4) | Random UUID v4 in a string representation | +| [findBetween(content, left, right, [repeat])](https://grafana.com/docs/k6//javascript-api/jslib/utils/findbetween) | Extract a string between two surrounding strings | +| [normalDistributionStages(maxVUs, durationSeconds, [numberOfStages])](https://grafana.com/docs/k6//javascript-api/jslib/utils/normaldistributionstages) | Creates [stages](https://grafana.com/docs/k6//using-k6/k6-options#stages) which will produce a normal distribution (bell-curve) of VUs for a test | +| getCurrentStageIndex | Get the index of the running stage as defined in the `stages` array options. It can be used only with the executors that support the `stages` option as [ramping-vus](https://grafana.com/docs/k6//using-k6/scenarios/executors/ramping-vus) or [ramping-arrival-rate](https://grafana.com/docs/k6//using-k6/scenarios/executors/ramping-arrival-rate). | +| tagWithCurrentStageIndex | Tag all the generated metrics in the iteration with the index of the current running stage. | +| tagWithCurrentStageProfile | Tag all the generated metrics in the iteration with the computed profile for the current running stage. | + +## Simple example + +{{< code >}} + +```javascript +import { sleep } from 'k6'; +import http from 'k6/http'; + +import { + randomIntBetween, + randomString, + randomItem, + uuidv4, + findBetween, +} from 'https://jslib.k6.io/k6-utils/1.4.0/index.js'; + +export default function () { + const res = http.post(`https://test-api.k6.io/user/register/`, { + first_name: randomItem(['Joe', 'Jane']), // random name + last_name: `Jon${randomString(1, 'aeiou')}s`, //random character from given list + username: `user_${randomString(10)}@example.com`, // random email address, + password: uuidv4(), // random password in form of uuid + }); + + // find a string between two strings to grab the username: + const username = findBetween(res.body, '"username":"', '"'); + console.log('username from response: ' + username); + + sleep(randomIntBetween(1, 5)); // sleep between 1 and 5 seconds. +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/check.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/check.md new file mode 100644 index 000000000..3ddb2fbc8 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/check.md @@ -0,0 +1,61 @@ +--- +title: 'check( val, sets, [tags] )' +description: 'The jslib check function runs one or more checks on a value and generates a pass/fail result, but does not throw errors or interrupt test execution upon failure. It supports async values.' +--- + +# check( val, sets, [tags] ) + +The `check` function is a drop-in replacement for the built-in [check](https://grafana.com/docs/k6//javascript-api/k6/check/), with added support for async values. Any `Promise`s will be awaited, and the result is reported once the operation has been completed. + +| Parameter | Type | Description | +| --------------- | ---------------------- | ----------------------------------------------------------------------------------------- | +| val | any | Value to test. | +| sets | Record | Tests (checks) to run on the value. Functions will be called. `Promise`s will be awaited. | +| tags (optional) | Record | Extra tags to attach to metrics emitted. | + +### Returns + +| Type | Description | +| ---------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------- | +| Promise | boolean | `true` if all checks have succeeded, `false` otherwise. If any of the checked values was a `Promise`, the result is wrapped in a `Promise`. | + +### Examples + +Using `check()` with async values. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { fail } from 'k6'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; + +function waitFor(delay) { + return new Promise((resolve) => { + setTimeout(() => { + resolve(true); + }, delay); + }); +} + +export default async function () { + const res = http.get('https://httpbin.test.k6.io'); + + const success = await check(res, { + 'passing promise as a value': waitFor(1000), + 'async function': async (res) => { + await waitFor(500); + + return res.status === 200; + }, + }); + + if (!success) { + fail('check did not succeed'); + } +} +``` + +{{< /code >}} + +For general usage of checks, refer to [check(val, set, [tags])](https://grafana.com/docs/k6//javascript-api/k6/check/#examples). diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/findbetween.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/findbetween.md new file mode 100644 index 000000000..d19d37b92 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/findbetween.md @@ -0,0 +1,43 @@ +--- +title: 'findBetween(content, left, right, [repeat])' +description: 'findBetween function' +weight: 45 +--- + +# findBetween(content, left, right, [repeat]) + +Function that returns a string from between two other strings. + +| Parameter | Type | Description | +| ----------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------- | +| content | string | The string to search through (e.g. [Response.body](https://grafana.com/docs/k6//javascript-api/k6-http/response)) | +| left | string | The string immediately before the value to be extracted | +| right | string | The string immediately after the value to be extracted | +| repeat (optional) | boolean | If `true`, the result will be a string array containing all occurrences | + +### Returns + +| Type | Description | +| ------ | ------------------------------------------------------------------------------------------------------------------------------------ | +| string | The extracted string, or an empty string if no match was found. If `repeat=true`, this will be an array of strings or an empty array | + +### Example + +{{< code >}} + +```javascript +import { findBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; + +export default function () { + const response = '
Message 1
Message 2
'; + + const message = findBetween(response, '
', '
'); + + console.log(message); // Message 1 + + const allMessages = findBetween(response, '
', '
', true); + console.log(allMessages.length); // 2 +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/normaldistributionstages.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/normaldistributionstages.md new file mode 100644 index 000000000..036fa9b0a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/normaldistributionstages.md @@ -0,0 +1,44 @@ +--- +title: 'normalDistributionStages(maxVus, durationSeconds, [numberOfStages])' +description: 'normalDistributionStages function' +weight: 46 +--- + +# normalDistributionStages(maxVus, durationSeconds, [numberOfStages]) + +Function to create [stages](https://grafana.com/docs/k6//using-k6/k6-options#stages) producing a _normal distribution (bell-curve)_ of VUs for a test. + +| Parameter | Type | Description | +| ------------------------- | ---- | ------------------------------------------------ | +| maxVus | int | Maximum virtual users at the height of the curve | +| durationSeconds | int | Overall duration for all stages combined | +| numberOfStages (optional) | int | Number of stages to create; default is `10` | + +### Returns + +| Type | Description | +| -------------- | ---------------------------------------------------------------------------------- | +| array\[object] | An array of `{"duration": "XXXs", "target": XXX}` JSON objects representing stages | + +### Example + +{{< code >}} + +```javascript +import { sleep } from 'k6'; +import exec from 'k6/execution'; +import { normalDistributionStages } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; + +export const options = { + // Alters the number of VUs from 1 to 10 over a period + // of 20 seconds comprised of 5 stages. + stages: normalDistributionStages(10, 20, 5), +}; + +export default function () { + console.log(exec.instance.vusActive); + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/randomintbetween.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/randomintbetween.md new file mode 100644 index 000000000..c27a07671 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/randomintbetween.md @@ -0,0 +1,37 @@ +--- +title: 'randomIntBetween(min, max)' +description: 'Random integer' +weight: 41 +--- + +# randomIntBetween(min, max) + +Function returns a random number between the specified range. The returned value is no lower than (and may possibly equal) min, and is no bigger than (and may possibly equal) max. + +| Parameter | Type | Description | +| --------- | ---- | -------------------------- | +| min | int | Lower-end bound. Inclusive | +| max | int | Upper-end bound. Inclusive | + +### Returns + +| Type | Description | +| ---- | -------------- | +| int | Random integer | + +### Example + +{{< code >}} + +```javascript +import { sleep } from 'k6'; +import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; + +export default function () { + // code ... + + sleep(randomIntBetween(1, 5)); // sleep between 1 and 5 seconds. +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/randomitem.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/randomitem.md new file mode 100644 index 000000000..382c9efd5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/randomitem.md @@ -0,0 +1,36 @@ +--- +title: 'randomItem(array)' +description: 'Random item from an array' +weight: 42 +--- + +# randomItem(array) + +Function returns a random item from an array. + +| Parameter | Type | Description | +| ------------ | ----- | ----------------- | +| arrayOfItems | array | Array [] of items | + +### Returns + +| Type | Description | +| ---- | -------------------------- | +| any | Random item from the array | + +### Example + +{{< code >}} + +```javascript +import { randomItem } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; + +const names = ['John', 'Jane', 'Bert', 'Ed']; + +export default function () { + const randomName = randomItem(names); + console.log(`Hello, my name is ${randomName}`); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/randomstring.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/randomstring.md new file mode 100644 index 000000000..77a4b6420 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/randomstring.md @@ -0,0 +1,41 @@ +--- +title: 'randomString(length, [charset])' +description: 'Random string' +weight: 43 +--- + +# randomString(length, [charset]) + +Function returns a random string of a given length, optionally selected from a custom character set. + +| Parameter | Type | Description | +| ------------------ | ------ | ------------------------------- | +| length | int | Length of the random string | +| charset (optional) | string | A customized list of characters | + +### Returns + +| Type | Description | +| ---- | ------------------------------------------- | +| any | Random item(s) from the array of characters | + +### Example + +{{< code >}} + +```javascript +import { randomString } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; + +export default function () { + const randomFirstName = randomString(8); + console.log(`Hello, my first name is ${randomFirstName}`); + + const randomLastName = randomString(10, `aeioubcdfghijpqrstuv`); + console.log(`Hello, my last name is ${randomLastName}`); + + const randomCharacterWeighted = randomString(1, `AAAABBBCCD`); + console.log(`Chose a random character ${randomCharacterWeighted}`); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/uuidv4.md b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/uuidv4.md new file mode 100644 index 000000000..bb9e9f272 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/jslib/utils/uuidv4.md @@ -0,0 +1,36 @@ +--- +title: 'uuidv4()' +description: 'uuid v4 function' +weight: 44 +--- + +# uuidv4() + +Function returns a random uuid v4 in a string form. + +### Parameters + +| Parameter | Type | Description | +| :---------------- | :------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| secure (optional) | boolean | By default, `uuidv4()` uses a standard random number generator. If the `secure` option is set to `true`, `uuidv4` uses a cryptographically secure random number generator instead. While this adds security, the `secure` option also makes the function an order of magnitude slower. | + +### Returns + +| Type | Description | +| :----- | :-------------------- | +| string | Random UUID v4 string | + +### Example + +{{< code >}} + +```javascript +import { uuidv4 } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js'; + +export default function () { + const randomUUID = uuidv4(); + console.log(randomUUID); // 35acae14-f7cb-468a-9866-1fc45713149a +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/_index.md new file mode 100644 index 000000000..9effdc626 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/_index.md @@ -0,0 +1,155 @@ +--- +title: 'k6/browser' +description: 'An overview of the browser-level APIs from browser module.' +weight: 02 +--- + +# browser + +The browser module APIs aim for rough compatibility with the [Playwright API for NodeJS](https://playwright.dev/docs/api/class-playwright). + +Note that because k6 does not run in NodeJS, the browser module APIs will slightly differ from their Playwright counterparts. + +You can find examples of using [the browser module API](#browser-module-api) in our [getting started guide](https://grafana.com/docs/k6//using-k6-browser). + +{{< admonition type="note" >}} + +To work with the browser module, make sure you are using the latest [k6 version](https://github.com/grafana/k6/releases). + +{{< /admonition >}} + +## Properties + +The table below lists the properties you can import from the browser module (`'k6/browser'`). + +| Property | Description | +| -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| browser | The browser module API is the entry point for all your tests. See the [example](#example) and the [API](#browser-module-api) below. | +| devices | Returns predefined emulation settings for many end-user devices that can be used to simulate browser behavior on a mobile device. See the [devices example](#devices-example) below. | + +## Browser Module API + +The browser module is the entry point for all your tests, and it is what interacts with the actual web browser via [Chrome DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/) (CDP). It manages: + +- [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext) which is where you can set a variety of attributes to control the behavior of pages; +- and [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page) which is where your rendered site is displayed. + +{{< docs/shared source="k6" lookup="javascript-api/k6-browser-module.md" version="" >}} + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, + thresholds: { + checks: ['rate==1.0'], + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/'); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} + +Then, you can run the test with this command. Also, see the [browser module options](https://grafana.com/docs/k6//using-k6-browser/options) for customizing the browser module's behavior using environment variables. + +{{< code >}} + +```bash +$ k6 run script.js +``` + +```docker +# WARNING! +# The grafana/k6:master-with-browser image launches a Chrome browser by setting the +# 'no-sandbox' argument. Only use it with trustworthy websites. +# +# As an alternative, you can use a Docker SECCOMP profile instead, and overwrite the +# Chrome arguments to not use 'no-sandbox' such as: +# docker container run --rm -i -e K6_BROWSER_ARGS='' --security-opt seccomp=$(pwd)/chrome.json grafana/k6:master-with-browser run - k6 run script.js +``` + +```windows-powershell +PS C:\k6> k6 run script.js +``` + +{{< /code >}} + +### Devices example + +To emulate the browser behaviour on a mobile device and approximately measure the browser performance, you can import `devices` from `k6/browser`. + +{{< code >}} + +```javascript +import { browser, devices } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, + thresholds: { + checks: ['rate==1.0'], + }, +}; + +export default async function () { + const iphoneX = devices['iPhone X']; + const context = await browser.newContext(iphoneX); + const page = await context.newPage(); + + try { + await page.goto('https://test.k6.io/'); + } finally { + page.close(); + } +} +``` + +{{< /code >}} + +## Browser-level APIs + +{{< docs/shared source="k6" lookup="javascript-api/k6-browser-api.md" version="" >}} + +## Browser module options + +You can customize the behavior of the browser module by providing browser options as environment variables. + +{{< docs/shared source="k6" lookup="browser/env-var-options.md" version="" >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/_index.md new file mode 100644 index 000000000..0a22dfe06 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/_index.md @@ -0,0 +1,31 @@ +--- +title: "BrowserContext" +description: "Browser module: BrowserContext Class" +weight: 02 +weight: 02 +--- + +# BrowserContext + +`BrowserContext`s provide a way to operate multiple independent sessions, with separate pages, cache, and cookies. A default `BrowserContext` is created when a browser is launched. + +The [browser module API](https://grafana.com/docs/k6//javascript-api/k6-browser#browser-module-api) is used to create a new `BrowserContext`. + +If a [page](https://grafana.com/docs/k6//javascript-api/k6-browser/page) opens another page, e.g. with a `window.open` call, the popup will belong to the parent page's `BrowserContext`. + +| Method | Description | +| ----------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [addCookies()](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/addcookies) | Adds [cookies](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/cookie) into the `BrowserContext`. | +| [addInitScript()](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/addinitscript) | Adds a script that will be evaluated on [page](https://grafana.com/docs/k6//javascript-api/k6-browser/page) creation, [frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame) attached or a navigation occurs. | +| [clearCookies()](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/clearcookies) | Clear the `BrowserContext`'s [cookies](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/cookie). | +| [clearPermissions()](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/clearpermissions) | Clears all permission overrides for the `BrowserContext`. | +| [cookies()](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/cookies) | Returns a list of [cookies](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/cookie) from the `BrowserContext`. | +| [close()](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/close) | Close the `BrowserContext` and all its [page](https://grafana.com/docs/k6//javascript-api/k6-browser/page)s. | +| [grantPermissions(permissions[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/grantpermissions) | Grants specified permissions to the `BrowserContext`. | +| [newPage()](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/newpage) | Uses the `BrowserContext` to create a new [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page) and returns it. | +| [pages()](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/pages) {{< docs/bwipt id="444" >}} | Returns a list of [page](https://grafana.com/docs/k6//javascript-api/k6-browser/page)s that belongs to the `BrowserContext`. | +| [setDefaultNavigationTimeout(timeout)](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/setdefaultnavigationtimeout) | Sets the default navigation timeout in milliseconds. | +| [setDefaultTimeout(timeout)](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/setdefaulttimeout) | Sets the default maximum timeout for all methods accepting a timeout option in milliseconds. | +| [setGeolocation(geolocation)](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/setgeolocation) {{< docs/bwipt id="435" >}} | Sets the `BrowserContext`'s geolocation. | +| [setOffline(offline)](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/setoffline) | Toggles the `BrowserContext`'s connectivity on/off. | +| [waitForEvent(event[, optionsOrPredicate])](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/waitforevent) | Waits for the event to fire and passes its value into the predicate function. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/addcookies.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/addcookies.md new file mode 100644 index 000000000..cc9a0447c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/addcookies.md @@ -0,0 +1,95 @@ +--- +title: 'addCookies()' +description: 'Clears context cookies.' +--- + +# addCookies() + +Adds a list of [cookies](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/cookie) into the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/). All pages within this [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) will have these [cookies](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/cookie) set. + +{{< admonition type="note" >}} + +If a [cookie](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/cookie)'s `url` property is not provided, both `domain` and `path` properties must be specified. + +{{< /admonition >}} + +### Returns + +| Type | Description | +| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the [cookies](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/cookie) have been added to the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/). | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext(); + const page = await context.newPage(); + + try { + const unixTimeSinceEpoch = Math.round(new Date() / 1000); + const day = 60 * 60 * 24; + const dayAfter = unixTimeSinceEpoch + day; + const dayBefore = unixTimeSinceEpoch - day; + + await context.addCookies([ + // this cookie expires at the end of the session + { + name: 'testcookie', + value: '1', + sameSite: 'Strict', + domain: 'httpbin.org', + path: '/', + httpOnly: true, + secure: true, + }, + // this cookie expires in a day + { + name: 'testcookie2', + value: '2', + sameSite: 'Lax', + domain: 'httpbin.org', + path: '/', + expires: dayAfter, + }, + // this cookie expires in the past, so it will be removed. + { + name: 'testcookie3', + value: '3', + sameSite: 'Lax', + domain: 'httpbin.org', + path: '/', + expires: dayBefore, + }, + ]); + + const response = await page.goto('https://httpbin.org/cookies', { + waitUntil: 'networkidle', + }); + console.log(response.json()); + // prints: + // {"cookies":{"testcookie":"1","testcookie2":"2"}} + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/addinitscript.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/addinitscript.md new file mode 100644 index 000000000..c7f5aa0e3 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/addinitscript.md @@ -0,0 +1,74 @@ +--- +title: 'addInitScript()' +description: 'Adds an init script.' +--- + +# addInitScript() + +Adds a script which will be evaluated in one of the following scenarios: + +- Whenever a [page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/) is created in the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext) or is navigated. +- Whenever a child [frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/) is attached or navigated in any [page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/) in the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext). In this case, the script is evaluated in the context of the newly attached [frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/). + +The script is evaluated after the document is created but before any of its scripts are run. This is useful to amend the JavaScript environment, for example, to override `Math.random`. + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the script has been added to the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext). | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const browserContext = await browser.newContext(); + + // This will execute and override the existing implementation of Math.random. + await browserContext.addInitScript('Math.random = function(){return 0}'); + + const page = await browserContext.newPage(); + + // In this example we are going to set the content manually, so we first + // navigate to about:blank which will execute the init script, before setting + // the content on the page. + await page.goto('about:blank'); + + await page.setContent(` + + + +
waiting...
+ + + `); + + await check(page.locator('#random'), { + 'is zero': async random => await random.textContent() == '0' + }); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/clearcookies.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/clearcookies.md new file mode 100644 index 000000000..374f2ba72 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/clearcookies.md @@ -0,0 +1,50 @@ +--- +title: 'clearCookies()' +description: 'Clears context cookies.' +--- + +# clearCookies() + +Clears the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext)'s cookies. + +### Returns + +| Type | Description | +| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the cookies have been cleared from the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext). | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext(); + const page = await context.newPage(); + + await page.goto('https://httpbin.org/cookies/set?testcookie=testcookievalue'); + let cookies = await context.cookies(); + console.log(cookies.length); // prints: 1 + + await context.clearCookies(); + cookies = await context.cookies(); + console.log(cookies.length); // prints: 0 +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/clearpermissions.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/clearpermissions.md new file mode 100644 index 000000000..b64ef64be --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/clearpermissions.md @@ -0,0 +1,44 @@ +--- +title: 'clearPermissions()' +description: 'Clears all permission overrides for the BrowserContext.' +--- + +# clearPermissions() + +Clears all permission overrides for the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext). + +### Returns + +| Type | Description | +| --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the permissions have been cleared from the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext). | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext(); + await context.grantPermissions(['clipboard-read']); + // do stuff ... + await context.clearPermissions(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/close.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/close.md new file mode 100644 index 000000000..158512c80 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/close.md @@ -0,0 +1,44 @@ +--- +title: 'close()' +description: 'Close the BrowserContext and all its pages.' +--- + +# close() + +Close the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext) and all its [pages](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). The [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext) is unusable after this call and a new one must be created. This is typically called to cleanup before ending the test. + +### Returns + +| Type | Description | +| --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext) and all its [page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/)s have been closed. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext(); + await context.newPage(); + + await context.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/cookie.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/cookie.md new file mode 100644 index 000000000..737d61876 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/cookie.md @@ -0,0 +1,22 @@ +--- +title: 'Cookie' +description: 'Browser module: Cookie Class' +--- + +# Cookie + +Cookie class represents a cookie in the [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext). + +See the [HTTP Cookies documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies) on the Mozilla website for more details about cookies. + +| Property | Type | Default | Description | +| -------- | ------ | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| name | string | `""` | The cookie's name. Required. | +| value | string | `""` | The cookie's value. Required. | +| domain | string | `""` | The cookie's domain. | +| path | string | `'/'` | The cookie's path. | +| url | string | `""` | The cookie's URL. | +| expires | number | `-1` | The cookie's expiration date as the number of seconds since the UNIX epoch. `-1` means a session cookie. | +| httpOnly | bool | `false` | A cookie is inaccessible to the JavaScript [document.cookie](https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie) API when this property is `true`. | +| secure | bool | `false` | The cookie's secure flag. | +| sameSite | string | `'Lax'` | The cookie's same site flag. It can be one of `'Strict'`, `'Lax'`, and `'None'`. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/cookies.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/cookies.md new file mode 100644 index 000000000..64f557075 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/cookies.md @@ -0,0 +1,82 @@ +--- +title: 'cookies([urls])' +description: 'Retrieves context cookies.' +--- + +# cookies([urls]) + +Returns a list of [cookies](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/cookie) from the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext) filtered by the provided `urls`. If no `urls` are provided, all cookies are returned. + +| Parameter | Type | Description | +| --------- | ----- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| urls | array | A string array of URLs to filter the [cookies](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/cookie) in the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext). | + +### Returns + +| Type | Description | +| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise>` | A Promise that fulfills with an array of [cookies](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/cookie). | + +{{< admonition type="note" >}} + +[Cookies](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/cookie) can be added with [BrowserContext.addCookies](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/addcookies/). + +{{< /admonition >}} + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext(); + const page = await context.newPage(); + + try { + // get cookies from the browser context + let cookies = await context.cookies(); + console.log('initial cookies length:', cookies.length); // prints 0 + + // let's add more cookies to filter by urls + await context.addCookies([ + { name: 'foo', value: 'foovalue', sameSite: 'Strict', url: 'http://foo.com' }, + { name: 'bar', value: 'barvalue', sameSite: 'Lax', url: 'https://bar.com' }, + { name: 'baz', value: 'bazvalue', sameSite: 'Lax', url: 'https://baz.com' }, + ]); + + // get all cookies + cookies = await context.cookies(); + console.log('filtered cookies length:', cookies.length); // prints 3 + + // get cookies filtered by urls + cookies = await context.cookies('http://foo.com', 'https://baz.com'); + console.log('filtered cookies length:', cookies.length); // prints 2 + + // the first filtered cookie + console.log("1st cookie's name :", cookies[0].name); // prints foo + console.log("1st cookie's value:", cookies[0].value); // prints foovalue + // the first filtered cookie + console.log("2nd cookie's name :", cookies[1].name); // prints baz + console.log("2nd cookie's value:", cookies[1].value); // prints bazvalue + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/grantpermissions.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/grantpermissions.md new file mode 100644 index 000000000..eff5e170f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/grantpermissions.md @@ -0,0 +1,54 @@ +--- +title: 'grantPermissions(permissions[, options])' +description: 'Grants specified permissions to the BrowserContext.' +--- + +# grantPermissions(permissions[, options]) + +Grants specified permissions to the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext). Only grants corresponding permissions to the given origin if specified. + + + +| Parameter | Type | Description | +| -------------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| permissions | array | A string array of permissions to grant. A permission can be one of the following values: `'geolocation'`, `'midi'`, `'midi-sysex'` (system-exclusive midi), `'notifications'`, `'camera'`, `'microphone'`, `'background-sync'`, `'ambient-light-sensor'`, `'accelerometer'`, `'gyroscope'`, `'magnetometer'`, `'accessibility-events'`, `'clipboard-read'`, `'clipboard-write'`, `'payment-handler'`. | +| options | object | Optional. | +| options.origin | string | The [origin](https://developer.mozilla.org/en-US/docs/Glossary/Origin) to grant permissions to, e.g. `'https://example.com'`. | + + + +### Returns + +| Type | Description | +| --------------- | --------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the permissions have been granted. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext(); + await context.grantPermissions(['clipboard-read', 'clipboard-write'], { + origin: 'https://example.com/', + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/newpage.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/newpage.md new file mode 100644 index 000000000..30c2080ff --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/newpage.md @@ -0,0 +1,35 @@ +--- +title: 'newPage()' +description: 'Creates a new page inside this BrowserContext.' +--- + +# newPage() + +Uses the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext) to create and return a new [page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with a new [page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/) object. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export default async function () { + const context = await browser.newContext(); + const page = await context.newPage(); + + try { + await page.goto('https://test.k6.io/browser.php'); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/pages.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/pages.md new file mode 100644 index 000000000..0d2d89b56 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/pages.md @@ -0,0 +1,52 @@ +--- +title: 'pages()' +description: 'Returns a list of pages inside this BrowserContext.' +--- + +# pages() + +{{< admonition type="caution" >}} + +This feature has **known issues**. For details, refer to +[#444](https://github.com/grafana/xk6-browser/issues/444). + +{{< /admonition >}} + +Returns all open [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/)s in the `BrowserContext`. + +### Returns + +| Type | Description | +| ------------- | ----------------------------------------------------------------------------------------------------- | +| `Array` | An array of [page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/) objects. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext(); + await context.newPage(); + const pages = context.pages(); + console.log(pages.length); // 1 + await context.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setdefaultnavigationtimeout.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setdefaultnavigationtimeout.md new file mode 100644 index 000000000..ed1d41d63 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setdefaultnavigationtimeout.md @@ -0,0 +1,47 @@ +--- +title: 'setDefaultNavigationTimeout(timeout)' +description: 'Sets the default navigation timeout in milliseconds.' +--- + +# setDefaultNavigationTimeout(timeout) + +Sets the default maximum navigation timeout for [Page.goto()](https://grafana.com/docs/k6//javascript-api/k6-browser/page/goto). + +| Parameter | Type | Default | Description | +| --------- | ------ | ------------------------ | ---------------------------- | +| timeout | number | Dependent on the action. | The timeout in milliseconds. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext(); + const page = await context.newPage(); + context.setDefaultNavigationTimeout(1000); // 1s + + try { + await page.goto('https://httpbin.test.k6.io/delay/5'); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setdefaulttimeout.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setdefaulttimeout.md new file mode 100644 index 000000000..1c32592f1 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setdefaulttimeout.md @@ -0,0 +1,42 @@ +--- +title: 'setDefaultTimeout(timeout)' +description: 'Sets the default timeout in milliseconds.' +--- + +# setDefaultTimeout(timeout) + +Sets the default maximum timeout for all methods accepting a `timeout` option in milliseconds. + +| Parameter | Type | Default | Description | +| --------- | ------ | ------------------------ | ---------------------------- | +| timeout | number | Dependent on the action. | The timeout in milliseconds. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext(); + context.setDefaultTimeout(1000); // 1s + const page = await context.newPage(); + await page.click('h2'); // times out +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setgeolocation.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setgeolocation.md new file mode 100644 index 000000000..3bb279287 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setgeolocation.md @@ -0,0 +1,60 @@ +--- +title: 'setGeolocation(geolocation)' +description: "Sets the BrowserContext's geolocation." +--- + +# setGeolocation(geolocation) + +{{< admonition type="caution" >}} + +This feature has **known issues**. For details, refer to +[#435](https://github.com/grafana/xk6-browser/issues/435). + +{{< /admonition >}} + +Sets the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext)'s geolocation. + + + +| Parameter | Type | Default | Description | +| --------------------- | ------ | ------- | ------------------------------------- | +| geolocation | object | `null` | | +| geolocation.latitude | number | `0` | Latitude between -90 and 90. | +| geolocation.longitude | number | `0` | Latitude between -180 and 180. | +| geolocation.accuracy | number | `0` | Optional non-negative accuracy value. | + + + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the geolocation has been set. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext(); + await context.setGeolocation({ latitude: 59.95, longitude: 30.31667 }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setoffline.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setoffline.md new file mode 100644 index 000000000..7b81b20a1 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/setoffline.md @@ -0,0 +1,56 @@ +--- +title: 'setOffline(offline)' +description: "Toggles the BrowserContext's connectivity on/off." +--- + +# setOffline(offline) + +Toggles the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext)'s connectivity on/off. + +| Parameter | Type | Default | Description | +| --------- | ------- | ------- | ------------------------------------------------------------------------------------------- | +| offline | boolean | `false` | Whether to emulate the [browser context](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext) being disconnected (`true`) or connected (`false`). | + +### Returns + +| Type | Description | +| --------------- | ----------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the connectivity has been set. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext(); + + await context.setOffline(true); + + const page = await context.newPage(); + + try { + // Will not be able to load the page + await page.goto('https://test.k6.io/browser.php'); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/waitforevent.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/waitforevent.md new file mode 100644 index 000000000..c32896e0e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/browsercontext/waitforevent.md @@ -0,0 +1,124 @@ +--- +title: 'waitForEvent(event[, optionsOrPredicate])' +description: 'Waits for event to fire and returns its value.' +--- + +Waits for the event to fire and returns its value. If a predicate function has been set it will pass the value to the predicate function, which must return `true` for the promise to resolve. + + + +| Parameter | Type | Default | Description | +| ---------------------------- | ---------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | +| event | string | `null` | Name of event to wait for. Currently the `'page'` event is the only one that is supported. | +| optionsOrPredicate | function\|object | `null` | Optional. If it's a function, the `'page'` event data will be passed to it and it must return `true` to continue. | +| optionsOrPredicate.predicate | function | `null` | Optional. Function that will be called when the `'page'` event is emitted. The event data will be passed to it and it must return `true` to continue. | +| optionsOrPredicate.timeout | number | `30000` | Optional. Maximum time to wait in milliseconds. | + + + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with a [page](https://grafana.com/docs/k6//javascript-api/k6-browser/page) object when the `page` event has been emitted. | + +### Example + +This API is useful when you want to wait for the new page to open after clicking on a link that opens in a new tab. + + + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/'); + + await page.keyboard.down('ControlOrMeta'); + + // Open the link in a new tab with the help of the meta key. + // Wait for the new page to be created. + const browserContext = browser.context(); + const [newTab] = await Promise.all([ + browserContext.waitForEvent('page'), + await page.locator('a[href="/my_messages.php"]').click(), + ]); + + await page.keyboard.up('ControlOrMeta'); + + // Wait for the new page (tab) to load. + await newTab.waitForLoadState('load'); + + // Take screenshots of each page. + await page.screenshot({ path: `screenshot-page.png` }); + await newTab.screenshot({ path: `screenshot-newTab.png` }); + + await newTab.close(); + await page.close(); +} +``` + + + +Here's an example working with the predicate: + + + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext(); + + // Call waitForEvent with a predicate which will return true once at least + // one page has been created. + let counter = 0; + const promise = await context.waitForEvent('page', { + predicate: (page) => { + if (++counter >= 1) { + return true; + } + return false; + }, + }); + + // Now we create a page. + const page = await context.newPage(); + + // Wait for the predicate to pass. + await promise; + console.log('predicate passed'); + + await page.close(); +} +``` + + diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/closecontext.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/closecontext.md new file mode 100644 index 000000000..b983f5f1f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/closecontext.md @@ -0,0 +1,53 @@ +--- +title: 'closeContext()' +description: 'Browser module: close context method' +--- + +# closeContext() + +Closes the current [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext). If there is no active browser context, because none has been created yet or because it has been previously closed, this method throws an error. + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the [`BrowserContext`](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext) is closed. | + +### Example + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page1 = await browser.newPage({ + isMobile: true, + }); // implicitly creates a new context + + await page1.goto('https:/test.k6.io/'); + page1.close(); + await browser.closeContext(); // closes the context created on newPage + + const page2 = await browser.newPage({ + isMobile: false, + }); // creates a new context with different settings + + await page2.goto('https://test.k6.io/'); + page2.close(); + await browser.closeContext(); + + await browser.closeContext(); // throws an error as browser has no active context +} +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/console-message.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/console-message.md new file mode 100644 index 000000000..df1ead3ef --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/console-message.md @@ -0,0 +1,19 @@ +--- +title: 'ConsoleMessage' +slug: 'consolemessage' +description: 'Browser module: ConsoleMessage Class' +weight: 03 +--- + +# ConsoleMessage + +{{< docs/shared source="k6" lookup="browser-module-wip.md" version="" >}} + +## Supported APIs + +| Method | Playwright Relevant Distinctions | +| --------------------------------------------------------------------------------------------------------------- | -------------------------------- | +| args() | - | +| page() | - | +| text() | - | +| type() | - | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/context.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/context.md new file mode 100644 index 000000000..88e6fee6a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/context.md @@ -0,0 +1,51 @@ +--- +title: 'context()' +description: 'Browser module: context method' +--- + +# context() + +Returns the current [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/). + +{{< admonition type="note" >}} + +A 1-to-1 mapping between [Browser](https://grafana.com/docs/k6//javascript-api/k6-browser) and `BrowserContext` means you cannot run `BrowserContexts` concurrently. If you wish to create a new `BrowserContext` while one already exists, you will need to [close](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/close) the current one, and create a new one with either [newContext](https://grafana.com/docs/k6//javascript-api/k6-browser/newcontext/) or [newPage](https://grafana.com/docs/k6//javascript-api/k6-browser/newpage). All resources associated to the closed `BrowserContext` will also be closed and cleaned up (such as [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/)s). + +{{< /admonition >}} + +### Returns + +| Type | Description | +| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | +| object \| null | The current [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) if one has been created, otherwise `null`. | + +### Example + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + console.log(browser.context()); // null + + const page1 = await browser.newPage(); // implicitly creates a new browserContext + const context = browser.context(); // underlying live browserContext associated with browser + const page2 = await context.newPage(); // shares the browserContext with page1 + + page1.close(); + page2.close(); + await context.close(); +} +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/_index.md new file mode 100644 index 000000000..3c3f04247 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/_index.md @@ -0,0 +1,169 @@ +--- +title: 'ElementHandle' +slug: 'elementhandle' +description: 'Browser module: ElementHandle Class' +weight: 04 +--- + +# ElementHandle + +{{< docs/shared source="k6" lookup="browser-module-wip.md" version="" >}} + +| Method | Description | +| --------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [$(selector)](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/query) | Queries the element for the given selector. | +| [$$(selector)](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/queryall) | Queries the elements for the given selector. | +| [boundingBox()](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/boundingbox) | Returns the bounding box of the element. | +| [check([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/check) | Checks the element if it is a `checkbox` or `radio` button. | +| [click([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/click) | Clicks on the element. | +| [contentFrame()](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/contentframe) | Returns the [Frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame) of the element. | +| [dblclick([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/dblclick) | Double clicks on the element. | +| [dispatchEvent(type[, eventInit])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/dispatchevent) | Dispatches an event to the element. | +| [fill(value[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/fill) | Fills the specified value into the element. | +| [focus()](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/focus) | Focuses on the element. | +| [getAttribute(name)](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/getattribute) | Returns the specified attribute of the element. | +| [hover([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/hover) | Hovers over the element. | +| [innerHTML()](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/innerhtml) | Returns the inner HTML of the element. | +| [innerText()](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/innertext) | Returns the inner text of the element. | +| [inputValue([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/inputvalue) | Returns the value of the input element. | +| [isChecked()](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/ischecked) | Checks if the `checkbox` input type is selected. | +| [isDisabled()](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/isdisabled) | Checks if the element is `disabled`. | +| [isEditable()](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/iseditable) | Checks if the element is `editable`. | +| [isEnabled()](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/isenabled) | Checks if the element is `enabled`. | +| [isHidden()](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/ishidden/) | Checks if the element is `hidden`. | +| [isVisible()](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/isvisible/) | Checks if the element is `visible`. | +| [ownerFrame()](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/ownerframe) | Returns the [Frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame) of the element. | +| [press(key[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/press) | Focuses on the element and presses a single key or a combination of keys using the virtual [keyboard](https://grafana.com/docs/k6//javascript-api/k6-browser/keyboard). | +| [screenshot([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/screenshot) | Takes a screenshot of the element. | +| [scrollIntoViewIfNeeded([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/scrollintoviewifneeded) | Scrolls the element into view if needed. | +| [selectOption(values[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/selectoption) | Selects the `select` element's one or more options which match the values. | +| [selectText([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/selecttext) | Selects the text of the element. | +| [setChecked(checked[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/setchecked) | Sets the `checkbox` or `radio` input element's value to the specified checked or unchecked state. | +| [setInputFiles(file[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/setinputfiles) | Sets the file input element's value to the specified files. | +| [tap(options)](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/tap) | Taps the element. | +| [textContent()](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/textcontent) | Returns the text content of the element. | +| [type(text[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/type) | Focuses on the element and types the specified text into the element using the virtual [keyboard](https://grafana.com/docs/k6//javascript-api/k6-browser/keyboard). | +| [uncheck([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/uncheck) | Unchecks the element if it is a `checkbox` or `radio` button. | +| [waitForElementState(state[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/waitforelementstate) | Waits for the element to reach the specified state. | +| [waitForSelector(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/waitforselector) | Waits for the element to be present in the DOM and to be visible. | + +## Examples + +```javascript +import { browser } from 'k6/browser'; +import { check } from "https://jslib.k6.io/k6-utils/1.5.0/index.js"; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + // Goto front page, find login link and click it + try { + await page.goto('https://test.k6.io/'); + const messagesLink = await page.$('a[href="/my_messages.php"]'); + + await Promise.all([page.waitForNavigation(), messagesLink.click()]); + // Enter login credentials and login + const login = await page.$('input[name="login"]'); + await login.type('admin'); + const password = await page.$('input[name="password"]'); + await password.type('123'); + + const submitButton = await page.$('input[type="submit"]'); + + await Promise.all([page.waitForNavigation(), submitButton.click()]); + + await check(page, { + 'header': async p => { + const h2 = await p.$('h2'); + return await h2.textContent() == 'Welcome, admin!'; + }, + }); + } finally { + await page.close(); + } +} +``` + + + +```javascript +import { browser } from 'k6/browser'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + // Inject page content + await page.setContent(` +
Hello world
+ +
Edit me
+ + + + + `); + + // Check state + await check(page, { + 'is visible': async p => { + const e = await p.$('.visible'); + return e.isVisible(); + }, + 'is hidden': async p => { + const e = await p.$('.hidden'); + return e.isHidden(); + }, + 'is editable': async p => { + const e = await p.$('.editable'); + return e.isEditable(); + }, + 'is enabled': async p => { + const e = await p.$('.enabled'); + return e.isEnabled(); + }, + 'is disabled': async p => { + const e = await p.$('.disabled'); + return e.isDisabled(); + }, + 'is checked': async p => { + const e = await p.$('.checked'); + return e.isChecked(); + }, + 'is unchecked': async p => { + const e = await p.$('.unchecked'); + return await e.isChecked() === false; + }, + }); + } finally { + await page.close(); + } +} +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/boundingbox.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/boundingbox.md new file mode 100644 index 000000000..867eec091 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/boundingbox.md @@ -0,0 +1,60 @@ +--- +title: 'boundingBox()' +description: 'Browser module: elementHandle.boundingBox method' +--- + +# boundingBox() + +Returns the bounding box of the element. The bounding box is calculated with respect to the position of the [Frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame) of the current [ElementHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle), which is usually the [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page)'s main frame. + +### Returns + +| Type | Description | +| ----------------------- | ---------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the bounding box of the element as a [Rect](#rect). If the element is not visible, the Promise resolves to `null`. | + + +### Rect + +The `Rect` object represents the bounding box of an element. + +| Property | Type | Description | +| -------- | -------- | ------------------------------------------ | +| x | `number` | The x-coordinate of the element in pixels. | +| y | `number` | The y-coordinate of the element in pixels. | +| width | `number` | The width of the element in pixels. | +| height | `number` | The height of the element in pixels. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const element = await page.$('#text1'); + const boundingBox = await element.boundingBox(); + console.log(`x: ${boundingBox.x}, y: ${boundingBox.y}, width: ${boundingBox.width}, height: ${boundingBox.height}`); + + await page.close(); +} +``` + +{{< /code >}} \ No newline at end of file diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/check.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/check.md new file mode 100644 index 000000000..2015c2b94 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/check.md @@ -0,0 +1,68 @@ +--- +title: 'check([options])' +description: 'Browser module: elementHandle.check method' +--- + +# check([options]) + +{{< admonition type="warning" >}} + +Use [`locator.check([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/check/) instead. + +{{< /admonition >}} + +Use this method to select an `input` checkbox. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | Optional parameters. | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the check action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const checkbox = await page.$('#checkbox1'); + await checkbox.check(); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/click.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/click.md new file mode 100644 index 000000000..95f8e8aa3 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/click.md @@ -0,0 +1,110 @@ +--- +title: 'click([options])' +description: 'Browser module: elementHandle.click method' +--- + +# click([options]) + +{{< admonition type="warning" >}} + +Use [`locator.click([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/click/) instead. + +{{< /admonition >}} + +Mouse click on the chosen element. + + + +| Parameter | Type | Default | Description | +| ------------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | Optional parameters. | +| options.button | string | `left` | The mouse button (`left`, `middle` or `right`) to use during the action. | +| options.clickCount | number | `1` | The number of times the action is performed. | +| options.delay | number | `0` | Milliseconds to wait between `mousedown` and `mouseup`. | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.modifiers | string[] | `null` | `Alt`, `Control`, `Meta` or `Shift` modifiers keys pressed during the action. If not specified, currently pressed modifiers are used. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the click action is finished. | + +### Examples + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const button = await page.$('#counter-button'); + await button.click(); + + await page.close(); +} +``` + +{{< /code >}} + +When a click action results in a page navigation, remember to work with `Promise.all()` and `page.waitForNavigation()` to properly handle the asynchronous operation. + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/'); + + const messagesLink = await page.$('a[href="/my_messages.php"]'); + + await Promise.all([ + page.waitForNavigation(), + messagesLink.click() + ]); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/contentframe.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/contentframe.md new file mode 100644 index 000000000..6b4ed0393 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/contentframe.md @@ -0,0 +1,48 @@ +--- +title: 'contentFrame()' +description: 'Browser module: elementHandle.contentFrame method' +--- + +# contentFrame() + +Returns the [Frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/) that this element is contained in. + +### Returns + +| Type | Description | +| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that resolves to the [Frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/) that this element is contained in | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const element = await page.$('#text1'); + const frame = await element.contentFrame(); + console.log(frame.url()); + + await page.close(); +} +``` + +{{< /code >}} \ No newline at end of file diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/dblclick.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/dblclick.md new file mode 100644 index 000000000..c563286d4 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/dblclick.md @@ -0,0 +1,65 @@ +--- +title: 'dblclick([options])' +description: 'Browser module: elementHandle.dblclick method' +--- + +# dblclick([options]) + +{{< admonition type="warning" >}} + +Use [`locator.dblclick([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/dblclick/) instead. + +{{< /admonition >}} + +Mouse double clicks on the element. + + + +| Parameter | Type | Default | Description | +| ------------------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| options | object | `null` | Optional parameters. | +| options.button | string | `left` | The mouse button (`left`, `middle` or `right`) to use during the action. | +| options.delay | number | `0` | Milliseconds to wait between `mousedown` and `mouseup`. | +| options.force | boolean | `false` | Bypasses the actionability checks (`visible`, `stable`, `enabled`) if set to `true`. | +| options.modifiers | string[] | `null` | `Alt`, `Control`, `Meta` or `Shift` modifiers keys pressed during the action. If not specified, currently pressed modifiers are used. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it doesn't wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Passing `0` disables the timeout. The `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/) can override the default timeout. | +| options.trial | boolean | `false` | Performs the actionability checks without performing the double click action if set to `true`. | + + + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const elementHandle = await page.$('#counter-button'); + await elementHandle.dblclick(); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/dispatchevent.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/dispatchevent.md new file mode 100644 index 000000000..77858c068 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/dispatchevent.md @@ -0,0 +1,76 @@ +--- +title: 'dispatchEvent(type, eventInit)' +description: 'Browser module: elementHandle.dispatchEvent method' +--- + +# dispatchEvent(type, eventInit) + +{{< admonition type="warning" >}} + +Use [`locator.dispatchEvent(type, eventInit[, options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/dispatchevent/) instead. + +{{< /admonition >}} + +Dispatches HTML DOM event types e.g. `'click'`. + + + +| Parameter | Type | Defaults | Description | +| --------------- | ------ | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| type | string | `''` | DOM event type e.g. `'click'`. | +| eventInit | object | `null` | Optional event specific properties. See [eventInit](#eventinit) for more details. | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### eventInit + +Since `eventInit` is event-specific, please refer to the events documentation for the lists of initial properties: + +- [DragEvent](https://developer.mozilla.org/en-US/docs/Web/API/DragEvent/DragEvent) +- [FocusEvent](https://developer.mozilla.org/en-US/docs/Web/API/FocusEvent/FocusEvent) +- [KeyboardEvent](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent) +- [MouseEvent](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent) +- [PointerEvent](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/PointerEvent) +- [TouchEvent](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/TouchEvent) +- [Event](https://developer.mozilla.org/en-US/docs/Web/API/Event/Event) + +### Returns + +| Type | Description | +| --------------- | ----------------------------------------------------- | +| `Promise` | A Promise that fulfills once the event is dispatched. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const button = await page.$('#counter-button'); + await button.dispatchEvent('click'); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/fill.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/fill.md new file mode 100644 index 000000000..c64795429 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/fill.md @@ -0,0 +1,65 @@ +--- +title: 'fill(value, [options])' +description: 'Browser module: elementHandle.fill method' +--- + +# fill(value, [options]) + +{{< admonition type="warning" >}} + +Use [`locator.fill(value, [options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/fill/) instead. + +{{< /admonition >}} + +Fill an `input`, `textarea` or `contenteditable` element with the provided value. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| value | string | `''` | Value to set for the `input`, `textarea` or `contenteditable` element. | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | --------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the fill action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const textbox = await page.$('#text1'); + await textbox.fill('hello world!'); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/focus.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/focus.md new file mode 100644 index 000000000..4f1cec19c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/focus.md @@ -0,0 +1,53 @@ +--- +title: 'focus()' +description: 'Browser module: elementHandle.focus method' +--- + +# focus() + +{{< admonition type="warning" >}} + +Use [`locator.focus([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/focus/) instead. + +{{< /admonition >}} + +Calls [focus](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) on the element, if it can be focused on. + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the focus action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const textbox = await page.$('#text1'); + await textbox.focus(); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/getattribute.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/getattribute.md new file mode 100644 index 000000000..b20ecf381 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/getattribute.md @@ -0,0 +1,62 @@ +--- +title: 'getAttribute(name)' +description: 'Browser module: elementHandle.getAttribute method' +--- + +# getAttribute(name) + +{{< admonition type="warning" >}} + +Use [`locator.getAttribute(name[, options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/getattribute/) instead. + +{{< /admonition >}} + +Returns the element attribute value for the given attribute name. + + + +| Parameter | Type | Default | Description | +| --------------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| name | string | `''` | Attribute name to get the value for. | + + + +### Returns + +| Type | Description | +| ------------------------- | --------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the string value of the attribute or `null` if the attribute is not present. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const textbox = await page.$('#text1'); + const attribute = await textbox.getAttribute('onfocus'); + console.log(attribute); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/hover.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/hover.md new file mode 100644 index 000000000..a91bb482c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/hover.md @@ -0,0 +1,69 @@ +--- +title: 'hover([options])' +description: 'Browser module: elementHandle.hover method' +--- + +# hover([options]) + +{{< admonition type="warning" >}} + +Use [`locator.hover([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/hover/) instead. + +{{< /admonition >}} + +Hovers over the element. + + + +| Parameter | Type | Default | Description | +| ------------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.modifiers | string[] | `null` | `Alt`, `Control`, `Meta` or `Shift` modifiers keys pressed during the action. If not specified, currently pressed modifiers are used. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the hover action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const offScreenElement = await page.$('#off-screen'); + await offScreenElement.hover(); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/innerhtml.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/innerhtml.md new file mode 100644 index 000000000..0c4734fd3 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/innerhtml.md @@ -0,0 +1,53 @@ +--- +title: 'innerHTML()' +description: 'Browser module: elementHandle.innerHTML method' +--- + +# innerHTML() + +{{< admonition type="warning" >}} + +Use [`locator.innerHTML([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/innerhtml/) instead. + +{{< /admonition >}} + +Returns the `element.innerHTML`. + +### Returns + +| Type | Description | +| ----------------- | ----------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the inner HTML of the element. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const offScreen = await page.$('#off-screen'); + console.log(await offScreen.innerHTML()); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/innertext.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/innertext.md new file mode 100644 index 000000000..339bac309 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/innertext.md @@ -0,0 +1,53 @@ +--- +title: 'innerText()' +description: 'Browser module: elementHandle.innerText method' +--- + +# innerText() + +{{< admonition type="warning" >}} + +Use [`locator.innerText([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/innertext/) instead. + +{{< /admonition >}} + +Returns the element's inner text. + +### Returns + +| Type | Description | +| ----------------- | ----------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the inner text of the element. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const offScreen = await page.$('#off-screen'); + console.log(await offScreen.innerText()); // Off page div + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/inputvalue.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/inputvalue.md new file mode 100644 index 000000000..e38b6b80b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/inputvalue.md @@ -0,0 +1,57 @@ +--- +title: 'inputValue([options])' +description: 'Browser module: elementHandle.inputValue method' +--- + +# inputValue([options]) + +Returns `input.value` for the selected `input`, `textarea` or `select` element. + + + +| Parameter | Type | Default | Description | +| --------------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| ----------------- | ------------------------------------------------------------ | +| `Promise` | A Promise that fulfills with the input value of the element. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const textInput = await page.$('#text1'); + await textInput.fill('Hello world!'); + console.log(await textInput.inputValue()); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/ischecked.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/ischecked.md new file mode 100644 index 000000000..75e4ccaa3 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/ischecked.md @@ -0,0 +1,56 @@ +--- +title: 'isChecked()' +description: 'Browser module: elementHandle.isChecked method' +--- + +# isChecked() + +{{< admonition type="warning" >}} + +Use [`locator.isChecked([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/ischecked/) instead. + +{{< /admonition >}} + +Checks to see if the `checkbox` `input` type is selected or not. + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with `true` if the element is checked, else `false`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const checkbox = await page.$('#checkbox1'); + const isChecked = await checkbox.isChecked(); + if (!isChecked) { + await checkbox.check(); + } + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/isdisabled.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/isdisabled.md new file mode 100644 index 000000000..257a0608f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/isdisabled.md @@ -0,0 +1,56 @@ +--- +title: 'isDisabled()' +description: 'Browser module: elementHandle.isDisabled method' +--- + +# isDisabled() + +{{< admonition type="warning" >}} + +Use [`locator.isDisabled([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/isdisabled/) instead. + +{{< /admonition >}} + +Checks if the element is disabled. + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with `true` if the element is disabled, else `false`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const text = await page.$('#input-text-disabled'); + const isDisabled = await text.isDisabled(); + if (isDisabled) { + console.log('element is disabled'); + } + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/iseditable.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/iseditable.md new file mode 100644 index 000000000..bf8030abb --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/iseditable.md @@ -0,0 +1,56 @@ +--- +title: 'isEditable()' +description: 'Browser module: elementHandle.isEditable method' +--- + +# isEditable() + +{{< admonition type="warning" >}} + +Use [`locator.isEditable([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/iseditable/) instead. + +{{< /admonition >}} + +Checks if the element is editable. + +### Returns + +| Type | Description | +| --------------- | ----------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with `true` if the element is editable, else `false`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const text = await page.$('#text1'); + const isEditable = await text.isEditable(); + if (isEditable) { + text.fill('hello world!'); + } + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/isenabled.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/isenabled.md new file mode 100644 index 000000000..4f1c0ac6d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/isenabled.md @@ -0,0 +1,56 @@ +--- +title: 'isEnabled()' +description: 'Browser module: elementHandle.isEnabled method' +--- + +# isEnabled() + +{{< admonition type="warning" >}} + +Use [`locator.isEnabled([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/isenabled/) instead. + +{{< /admonition >}} + +Checks if the element is enabled. + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with `true` if the element is enabled, else `false`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const text = await page.$('#text1'); + const isEnabled = await text.isEnabled(); + if (isEnabled) { + console.log('element is enabled'); + } + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/ishidden.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/ishidden.md new file mode 100644 index 000000000..6d7d1e8dd --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/ishidden.md @@ -0,0 +1,56 @@ +--- +title: 'isHidden()' +description: 'Browser module: elementHandle.isHidden method' +--- + +# isHidden() + +{{< admonition type="warning" >}} + +Use [`locator.isHidden()`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/ishidden/) instead. + +{{< /admonition >}} + +Checks if the element is hidden. + +### Returns + +| Type | Description | +| --------------- | --------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with `true` if the element is hidden, else `false`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const text = await page.$('#input-text-hidden'); + const isHidden = await text.isHidden(); + if (isHidden) { + console.log('element is hidden'); + } + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/isvisible.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/isvisible.md new file mode 100644 index 000000000..7c663cee7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/isvisible.md @@ -0,0 +1,56 @@ +--- +title: 'isVisible()' +description: 'Browser module: elementHandle.isVisible method' +--- + +# isVisible() + +{{< admonition type="warning" >}} + +Use [`locator.isVisible()`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/isvisible/) instead. + +{{< /admonition >}} + +Checks if the element is visible. + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with `true` if the element is visible, else `false`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const text = await page.$('#text1'); + const isVisible = await text.isVisible(); + if (isVisible) { + console.log('element is visible'); + } + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/ownerframe.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/ownerframe.md new file mode 100644 index 000000000..3784fdaa0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/ownerframe.md @@ -0,0 +1,48 @@ +--- +title: 'ownerFrame' +description: 'Browser module: elementHandle.ownerFrame method' +--- + +# ownerFrame + +Returns the [Frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame) of the element. + +### Returns + +| Type | Description | +| ------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the [Frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/) of the element, or `null` if the element is not attached to a frame. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const element = await page.$('#text1'); + const frame = await element.ownerFrame(); + console.log(frame.url()); + + await page.close(); +} +``` + +{{< /code >}} \ No newline at end of file diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/press.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/press.md new file mode 100644 index 000000000..d92596645 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/press.md @@ -0,0 +1,67 @@ +--- +title: 'press(key, [options])' +description: 'Browser module: elementHandle.press method' +--- + +# press(key, [options]) + +{{< admonition type="warning" >}} + +Use [`locator.press(key[, options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/press/) instead. + +{{< /admonition >}} + +Press a single key on the keyboard or a combination of keys. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| key | string | `''` | Name of the key to press or a character to generate, such as `ArrowLeft` or `a`. A superset of the key values can be found [here](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values). | +| options | object | `null` | | +| options.delay | number | `0` | Milliseconds to wait between `keydown` and `keyup`. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------ | +| `Promise` | A Promise that fulfills when the key is pressed. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const text = await page.$('#text1'); + await text.press('i'); + await text.press('ArrowLeft'); + await text.press('h'); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/query.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/query.md new file mode 100644 index 000000000..dfe18d3ee --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/query.md @@ -0,0 +1,60 @@ +--- +title: '$(selector)' +description: 'Browser module: elementHandle.$ method' +--- + +# $(selector) + +{{< admonition type="warning" >}} + +Use [`page.locator(selector[, options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/) instead. + +{{< /admonition >}} + +Queries the element for the given selector in the [ElementHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle)'s subtree. + + + +| Parameter | Type | Default | Description | +| --------- | -------- | ------- | ------------------------------------- | +| selector | `string` | | A selector to query the elements for. | + + + +### Returns + +| Type | Description | +| -------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the [ElementHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle) for the first element found. If no element is found, the Promise resolves to `null`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + const textbox = await page.$('#text1'); + // ... + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/queryall.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/queryall.md new file mode 100644 index 000000000..cc4c3ddb7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/queryall.md @@ -0,0 +1,57 @@ +--- +title: '$$(selector)' +description: 'Browser module: elementHandle.$$ method' +--- + +# $$(selector) + +Queries the elements for the given selector in the [ElementHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle)'s subtree. + + + +| Parameter | Type | Default | Description | +| --------- | -------- | ------- | ------------------------------------- | +| selector | `string` | | A selector to query the elements for. | + + + +### Returns + +| Type | Description | +| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `Promise` | A Promise that fulfills with an [ElementHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle) array for the elements found. If no element is found, the Promise resolves to `null`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + const elements = await page.$$('#text1'); + for (const element of elements) { + // ... + } + + await page.close(); +} +``` + +{{< /code >}} \ No newline at end of file diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/screenshot.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/screenshot.md new file mode 100644 index 000000000..fe38e0929 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/screenshot.md @@ -0,0 +1,62 @@ +--- +title: 'screenshot([options])' +description: 'Browser module: elementHandle.screenshot method' +--- + +# screenshot([options]) + +Takes a screenshot of the element. + + + +| Parameter | Type | Default | Description | +| ---------------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.path | string | `''` | The file path to save the image to. The screenshot type will be inferred from file extension. If `path` is a relative path, then it is resolved relative to the current working directory. If no path is provided, the image won't be saved to the disk. | +| options.format | string | `png` | Specify screenshot type. Acceptable values are `jpeg` and `png`. | +| options.omitBackground | boolean | `false` | Hides default white background and allows capturing screenshots with transparency. Not applicable to `jpeg` images. | +| options.quality | number | `100` | The quality of the image, between 0-100; Only applicable to `jpeg` only. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| ------------------------------ | ------------------------------------------------------------------------------------------------------- | +| `Promise` | The [ArrayBuffer](https://nodejs.org/api/buffer.html#buffer_class_buffer) with the captured screenshot. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const element = await page.$(".header"); + const screenshot = await element.screenshot({ + path: 'header.png' + }); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/scrollintoviewifneeded.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/scrollintoviewifneeded.md new file mode 100644 index 000000000..f03cf8acf --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/scrollintoviewifneeded.md @@ -0,0 +1,56 @@ +--- +title: 'scrollIntoViewIfNeeded([options])' +description: 'Browser module: elementHandle.scrollIntoViewIfNeeded method' +--- + +# scrollIntoViewIfNeeded([options]) + +Scrolls the element into view if needed. + + + +| Parameter | Type | Default | Description | +| ---------------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | --------------------------------------------------------------- | +| `Promise` | A promise that resolves when the element is scrolled into view. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io'); + + const element = await page.$(".header"); + await element.scrollIntoViewIfNeeded(); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/selectoption.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/selectoption.md new file mode 100644 index 000000000..b4fe42747 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/selectoption.md @@ -0,0 +1,65 @@ +--- +title: 'selectOption(values, [options])' +description: 'Browser module: elementHandle.selectOption method' +--- + +# selectOption(values, [options]) + +{{< admonition type="warning" >}} + +Use [`locator.selectOption(values[, options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/selectoption/) instead. + +{{< /admonition >}} + +Select one or more options which match the values. + + + +| Parameter | Type | Default | Description | +| ------------------- | ---------------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| values | string or string[] or object | `''` | If the `select` has the multiple attribute, all matching options are selected, otherwise only the first option matching one of the passed options is selected. Object can be made up of keys with `value`, `label` or `index`. | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| ------------------- | ------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the selected options as an array of strings. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const options = await page.$('#numbers-options'); + await options.selectOption('three'); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/selecttext.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/selecttext.md new file mode 100644 index 000000000..492ac4a24 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/selecttext.md @@ -0,0 +1,57 @@ +--- +title: 'selectText(values, [options])' +description: 'Browser module: elementHandle.selectText method' +--- + +# selectText(values, [options]) + +Selects the text of the element. + + + +| Parameter | Type | Default | Description | +| ------------------- | ---------------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | -------------------------------------------------- | +| `Promise` | A Promise that fulfills when the text is selected. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io'); + + const element = await page.$('h1'); + await element.selectText(); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/setchecked.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/setchecked.md new file mode 100644 index 000000000..288027215 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/setchecked.md @@ -0,0 +1,69 @@ +--- +title: 'setChecked(checked[, options])' +description: 'Browser module: elementHandle.setChecked method' +--- + +# setChecked(checked[, options]) + +{{< admonition type="warning" >}} + +Use [`locator.setChecked(checked [, options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/setchecked/) instead. + +{{< /admonition >}} + +Use this method to select or unselect an `input` or `radio` checkbox. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | Optional parameters. | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, the script won't wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the check action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const checkbox = await page.$('#checkbox1'); + await checkbox.setChecked(true); + await checkbox.setChecked(false); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/setinputfiles.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/setinputfiles.md new file mode 100644 index 000000000..8637b2d31 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/setinputfiles.md @@ -0,0 +1,160 @@ +--- +title: 'setInputFiles(file[, options])' +description: 'Sets the file input element' +--- + +# setInputFiles(file[, options]) + +Sets the file input element's value to the specified files. + +To work with local files on the file system, use the [experimental fs module](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/fs/) to load and read the file contents. + +| Parameter | Type | Default | Description | +| ------------------- | ----------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| file | object | `null` | This is a required parameter. | +| file.name | string | `''` | The name of the file. For example, `file.txt`. | +| file.mimeType | string | `''` | The type of the file content. For example, `text/plain`. | +| file.buffer | ArrayBuffer | `[]` | Base64 encoded content of the file. | +| options | object | `null` | This is an optional parameter. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + +### Returns + +| Type | Description | +| --------------- | --------------------------------------------- | +| `Promise` | A Promise that fulfills when the file is set. | + +### Inline File Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import encoding from 'k6/encoding'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + // In this example we create a simple web page with an upload input field. + // Usually, you would use page.goto to navigate to a page with a file input field. + await page.setContent(` + + + + +
+ + +
+ + `); + + const eh = await page.$('input[id="upload"]'); + + // The file is set to the input element with the id "upload". + await eh.setInputFiles({ + name: 'file.txt', + mimetype: 'text/plain', + buffer: encoding.b64encode('hello world'), + }); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} + +### Local File Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import encoding from 'k6/encoding'; +import { open } from 'k6/experimental/fs'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +// Declare the location of the file on the local filesystem. +let file; +(async function () { + file = await open('/abs/path/to/file.txt'); +})(); + +export default async function () { + const page = await browser.newPage(); + + try { + // In this example we create a simple web page with an upload input field. + // Usually, you would use page.goto to navigate to a page with a file input field. + await page.setContent(` + + + + +
+ + +
+ + `); + + const eh = await page.$('input[id="upload"]'); + + // Read the whole file content into a buffer. + const buffer = await readAll(file); + + // The file is set to the input element with the id "upload". + await eh.setInputFiles({ + name: 'file.txt', + mimetype: 'text/plain', + buffer: encoding.b64encode(buffer), + }); + } finally { + await page.close(); + } +} + +// readAll will read the whole of the file from the local filesystem into a +// buffer. +async function readAll(file) { + const fileInfo = await file.stat(); + const buffer = new Uint8Array(fileInfo.size); + + const bytesRead = await file.read(buffer); + if (bytesRead !== fileInfo.size) { + throw new Error('unexpected number of bytes read'); + } + + return buffer; +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/tap.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/tap.md new file mode 100644 index 000000000..9f4205af3 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/tap.md @@ -0,0 +1,70 @@ +--- +title: 'tap(options)' +description: 'Browser module: elementhandle.tap method' +--- + +# tap(options) + +{{< admonition type="warning" >}} + +Use [`locator.tap([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/tap/) instead. + +{{< /admonition >}} + +Taps the element. + + + +| Parameter | Type | Default | Description | +| ------------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.modifiers | string[] | `null` | `Alt`, `Control`, `Meta` or `Shift` modifiers keys pressed during the action. If not specified, currently pressed modifiers are used. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.strict | boolean | `false` | When `true`, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | -------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the tap action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage({ + hasTouch: true, + }); + + await page.goto('https://test.k6.io/browser.php'); + const el = await page.$("#numbers-options"); + await el.tap(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/textcontent.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/textcontent.md new file mode 100644 index 000000000..23837a06f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/textcontent.md @@ -0,0 +1,53 @@ +--- +title: 'textContent()' +description: 'Browser module: elementHandle.textContent method' +--- + +# textContent() + +{{< admonition type="warning" >}} + +Use [`locator.textContent([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/textcontent/) instead. + +{{< /admonition >}} + +Returns the element's text content. + +### Returns + +| Type | Description | +| ------------------------- | ------------------------------------------------------------------------ | +| `Promise` | A Promise that fulfills with the text content of the selector or `null`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const options = await page.$('#checkbox1'); + console.log(await options.textContent()); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/type.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/type.md new file mode 100644 index 000000000..d383977b0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/type.md @@ -0,0 +1,65 @@ +--- +title: 'type(text, [options])' +description: 'Browser module: elementHandle.type method' +--- + +# type(text, [options]) + +{{< admonition type="warning" >}} + +Use [`locator.type([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/type/) instead. + +{{< /admonition >}} + +Type in the text into the input field. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| text | string | `''` | A text to type into a focused element. | +| options | object | `null` | | +| options.delay | number | `0` | Milliseconds to wait between key presses. Defaults to `0`. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | --------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the type action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const text = await page.$('#text1'); + await text.type('hello world!'); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/uncheck.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/uncheck.md new file mode 100644 index 000000000..74cfe648c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/uncheck.md @@ -0,0 +1,69 @@ +--- +title: 'uncheck([options])' +description: 'Browser module: elementHandle.uncheck method' +--- + +# uncheck([options]) + +{{< admonition type="warning" >}} + +Use [`locator.uncheck([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/uncheck/) instead. + +{{< /admonition >}} + +Unselect the `input` checkbox. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------------ | +| `Promise` | A Promise that fulfills when the uncheck action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const checkbox = await page.$('#checkbox1'); + await checkbox.check(); + await checkbox.uncheck(); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/waitforelementstate.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/waitforelementstate.md new file mode 100644 index 000000000..c6879d1c0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/waitforelementstate.md @@ -0,0 +1,56 @@ +--- +title: 'waitForElementState(state[, options])' +description: 'Browser module: elementHandle.waitForElementState method' +--- + +# waitForElementState(state[, options]) + +Waits for the element to reach the specified state. + + + +| Parameter | Type | Default | Description | +| --------- | ------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| state | string | | The state to wait for. This can be one of `visible`, `hidden`, `stable`, `enabled`, `disabled`, or `editable`. | +| timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | ----------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the element reaches the specified state or the timeout is reached. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const element = await page.$('#text1'); + await element.waitForElementState('visible'); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/waitforselector.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/waitforselector.md new file mode 100644 index 000000000..7506feabb --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/elementhandle/waitforselector.md @@ -0,0 +1,66 @@ +--- +title: 'waitForSelector(selector[, options])' +description: 'Browser module: elementHandle.waitForSelector method' +--- + +# waitForSelector(selector[, options]) + +{{< admonition type="warning" >}} + +Use [`locator.waitFor([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/waitfor/) instead. + +{{< /admonition >}} + +Waits for the element to be present in the DOM and to be visible. + + + +| Parameter | Type | Default | Description | +| --------------- | ------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | | A selector to query the element. | +| options | object | `null` | Optional settings. | +| options.state | string | `visible` | The state to wait for. This can be one of `visible`, `hidden`, `stable`, `enabled`, `disabled`, or `editable`. | +| options.strict | boolean | `false` | If set to `true`, the method will throw an error if the element is not found. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| -------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the [ElementHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/) when the element is found. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto("https://test.k6.io"); + + const element = await page.$(".header"); + const el = await element.waitForSelector(".title"); + // ... do something with the element + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/_index.md new file mode 100644 index 000000000..5fcf65e8a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/_index.md @@ -0,0 +1,58 @@ +--- +title: 'Frame' +description: 'Browser module: Frame Class' +weight: 05 +--- + +# Frame + +Frame represents a single frame in a browser window. It can be a top-level frame or an iframe. Frames are used to organize content in a browser window. Frames can be nested, and each frame has its own document and window objects. + +| Method | Description | +| ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------- | +| [frame$(selector)](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/frame-dollar) | Finds an element matching the specified `selector` within the frame. | +| [frame$$(selector)](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/frame-doubledollar) | Finds all elements matching the specified `selector` within the frame. | +| [check(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/check/) | Select the input checkbox. | +| [childFrames()](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/childframes) | Returns an array of child frames. | +| [click(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/click/) | Clicks on an element matching a `selector`. | +| [content()](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/content) | Gets the HTML contents of the frame. | +| [dblclick(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/dblclick) | Double click on an element matching the provided `selector`. | +| [dispatchEvent(selector, type, eventInit[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/dispatchevent/) | Dispatches HTML DOM event types e.g. `'click'` | +| [evaluate(pageFunction[, arg])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/evaluate/) | Returns the value of the `pageFunction` invocation. | +| [evaluateHandle(pageFunction[, arg])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/evaluatehandle/) | Returns the value of the `pageFunction` invocation as a [JSHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/jshandle). | +| [fill(selector, value[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/fill/) | Fill an `input`, `textarea` or `contenteditable` element with the provided value. | +| [focus(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/focus/) | Fetches an element with `selector` and focuses on it. | +| [frameElement()](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/frameelement) | Returns the `frame`'s `element`. | +| [getAttribute(selector, name[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/getattribute/) | Returns the element attribute value for the given attribute name. | +| [goto(url[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/goto/) | Navigates to the specified `url`. | +| [hover(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/hover/) | Hovers over an element matching `selector`. | +| [innerHTML(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/innerhtml/) | Returns the `element.innerHTML`. | +| [innerText(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/innertext/) | Returns the `element.innerText`. | +| [inputValue(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/inputvalue/) | Returns `input.value` for the selected `input`, `textarea` or `select` element. | +| [isChecked(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/ischecked/) | Checks to see if the `checkbox` `input` type is selected or not. | +| [isDetached()](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/isdetached/) | Returns `true` if the frame is detached from the DOM. | +| [isDisabled(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/isdisabled/) | Checks if the element is `disabled`. | +| [isEditable(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/iseditable/) | Checks if the element is `editable`. | +| [isEnabled(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/isenabled/) | Checks if the element is `enabled`. | +| [isHidden(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/ishidden/) | Checks if the matched element is `hidden`. | +| [isVisible(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/isvisible/) | Checks if the matched element is `visible`. | +| [locator(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/locator) | Returns a [Locator](https://grafana.com/docs/k6//javascript-api/k6-browser/locator) for the given `selector`. | +| [name()](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/name) | Returns the name of the frame. | +| [page()](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/page) | Returns the page containing the frame. | +| [parentFrame()](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/parentframe) | Returns the parent frame, if any. | +| [press(selector, key[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/press/) | Focuses the element, and then presses the given `key` on the [Keyboard](https://grafana.com/docs/k6//javascript-api/k6-browser/keyboard). | +| [selectOption(selector, values[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/selectoption/) | Selects one or more options which match the values from a `` element. + + + +| Parameter | Type | Default | Description | +| ------------------- | ---------------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | `''` | A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. | +| values | string or string[] or object | `''` | If the `select` has the multiple attribute, all matching options are selected, otherwise only the first option matching one of the passed options is selected. Object can be made up of keys with `value`, `label` or `index`. | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.strict | boolean | `false` | When `true`, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| ------------------- | -------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the list of the selected options. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/setchecked.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/setchecked.md new file mode 100644 index 000000000..da5d7a904 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/setchecked.md @@ -0,0 +1,70 @@ +--- +title: 'setChecked(selector, checked[, options])' +description: 'Browser module: frame.setChecked method' +--- + +# setChecked(selector, checked[, options]) + +{{< admonition type="warning" >}} + +Use [`locator.setChecked(checked [, options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/setchecked/) instead. + +{{< /admonition >}} + +Use this method to select on unselect an `input` or `radio` checkbox. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | Optional parameters. | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | +| options.strict | boolean | `false` | When `true`, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception. | + + + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the check action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const frame = page.mainFrame(); + await frame.setChecked('#checkbox1', true); + await frame.setChecked('#checkbox1', false); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/setcontent.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/setcontent.md new file mode 100644 index 000000000..5edad7d5a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/setcontent.md @@ -0,0 +1,25 @@ +--- +title: 'setContent(html[, options])' +description: 'Browser module: frame.setContent(html[, options]) method' +--- + +# setContent(html[, options]) + +Sets the supplied HTML string to the current frame. + + + +| Parameter | Type | Default | Description | +| ----------------- | ------ | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| html | string | `''` | HTML markup to assign to the page. | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum operation time in milliseconds. Pass `0` to disable the timeout. The default value can be changed via the [browserContext.setDefaultNavigationTimeout(timeout)](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/setdefaultnavigationtimeout/), [browserContext.setDefaultTimeout(timeout)](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/setdefaulttimeout/), [page.setDefaultNavigationTimeout(timeout)](https://grafana.com/docs/k6//javascript-api/k6-browser/page/setdefaultnavigationtimeout/) or [page.setDefaultTimeout(timeout)](https://grafana.com/docs/k6//javascript-api/k6-browser/page/setdefaulttimeout/) methods. Setting the value to `0` will disable the timeout. | +| options.waitUntil | string | `load` | When to consider operation to have succeeded. See Events for more details. | + + + +### Returns + +| Type | Description | +| --------------- | -------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the page has been set with the supplied HTML. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/setinputfiles.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/setinputfiles.md new file mode 100644 index 000000000..cbd21b20f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/setinputfiles.md @@ -0,0 +1,155 @@ +--- +title: 'setInputFiles(selector, file[, options])' +description: 'Sets the file input element' +--- + +# setInputFiles(selector, file[, options]) + +Sets the file input element's value to the specified files. + +To work with local files on the file system, use the [experimental fs module](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/fs/) to load and read the file contents. + +| Parameter | Type | Default | Description | +| ------------------- | ----------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | `''` | A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. | +| file | object | `null` | This is a required parameter. | +| file.name | string | `''` | The name of the file. For example, `file.txt`. | +| file.mimeType | string | `''` | The type of the file content. For example, `text/plain`. | +| file.buffer | ArrayBuffer | `[]` | Base64 encoded content of the file. | +| options | object | `null` | This is an optional parameter. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + +### Inline File Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import encoding from 'k6/encoding'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + // In this example we create a simple web page with an upload input field. + // Usually, you would use page.goto to navigate to a page with a file input field. + await page.setContent(` + + + + +
+ + +
+ + `); + + const frame = page.mainFrame(); + + // The file is set to the input element with the id "upload". + frame.setInputFiles('input[id="upload"]', { + name: 'file.txt', + mimetype: 'text/plain', + buffer: encoding.b64encode('hello world'), + }); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} + +### Local File Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import encoding from 'k6/encoding'; +import { open } from 'k6/experimental/fs'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +// Declare the location of the file on the local filesystem. +let file; +(async function () { + file = await open('/abs/path/to/file.txt'); +})(); + +export default async function () { + const page = await browser.newPage(); + + try { + // In this example we create a simple web page with an upload input field. + // Usually, you would use page.goto to navigate to a page with a file input field. + await page.setContent(` + + + + +
+ + +
+ + `); + + const frame = page.mainFrame(); + + // Read the whole file content into a buffer. + const buffer = await readAll(file); + + // The file is set to the input element with the id "upload". + frame.setInputFiles({ + name: 'file.txt', + mimetype: 'text/plain', + buffer: encoding.b64encode(buffer), + }); + } finally { + await page.close(); + } +} + +// readAll will read the whole of the file from the local filesystem into a +// buffer. +async function readAll(file) { + const fileInfo = await file.stat(); + const buffer = new Uint8Array(fileInfo.size); + + const bytesRead = await file.read(buffer); + if (bytesRead !== fileInfo.size) { + throw new Error('unexpected number of bytes read'); + } + + return buffer; +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/tap.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/tap.md new file mode 100644 index 000000000..f92e03bb5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/tap.md @@ -0,0 +1,65 @@ +--- +title: 'tap(selector[, options])' +description: 'Browser module: frame.tap(selector[, options]) method' +--- + +# tap(selector[, options]) + +Tap the first element that matches the selector. + + + +| Parameter | Type | Default | Description | +| ------------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | `''` | A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.modifiers | string[] | `null` | `Alt`, `Control`, `Meta` or `Shift` modifiers keys pressed during the action. If not specified, currently pressed modifiers are used. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.strict | boolean | `false` | When `true`, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | -------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the tap action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage({ + hasTouch: true, + }); + + await page.goto('https://test.k6.io/browser.php'); + const frame = page.mainFrame(); + await frame.tap("#numbers-options"); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/textcontent.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/textcontent.md new file mode 100644 index 000000000..12eb52e6a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/textcontent.md @@ -0,0 +1,31 @@ +--- +title: 'textContent(selector[, options])' +description: 'Browser module: locator.textContent(selector[, options]) method' +--- + +# textContent(selector[, options]) + +{{< admonition type="warning" >}} + +Use locator-based [`locator.textContent([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/textcontent/) instead. + +{{< /admonition >}} + +Returns the `element.textContent`. + + + +| Parameter | Type | Default | Description | +| --------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | `''` | A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. | +| options | object | `null` | | +| options.strict | boolean | `false` | When `true`, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| ------------------------- | ------------------------------------------------------------------------ | +| `Promise` | A Promise that fulfills with the text content of the selector or `null`. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/title.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/title.md new file mode 100644 index 000000000..b56022c75 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/title.md @@ -0,0 +1,14 @@ +--- +title: 'title()' +description: 'Browser module: frame.title method' +--- + +# title() + +Returns the frame's title. + +### Returns + +| Type | Description | +| ----------------- | ----------------------------------------------- | +| `Promise` | A Promise that fulfills with the frame's title. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/type.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/type.md new file mode 100644 index 000000000..27023e4e9 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/type.md @@ -0,0 +1,34 @@ +--- +title: 'type(selector, text[, options])' +description: 'Browser module: frame.type(selector, text[, options]) method' +--- + +# type(selector, text[, options]) + +{{< admonition type="warning" >}} + +Use locator-based [`locator.type()`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/type/) instead. + +{{< /admonition >}} + +Type the `text` in the first element found that matches the selector. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | `''` | A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. | +| text | string | `''` | A text to type into a focused element. | +| options | object | `null` | | +| options.delay | number | `0` | Milliseconds to wait between key presses. Defaults to `0`. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.strict | boolean | `false` | When `true`, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | ----------------------------------------------------- | +| `Promise` | A Promise that fulfills when the text has been typed. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/uncheck.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/uncheck.md new file mode 100644 index 000000000..86ba65cfb --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/uncheck.md @@ -0,0 +1,37 @@ +--- +title: 'uncheck(selector[, options])' +description: 'Browser module: frame.uncheck(selector[, options]) method' +--- + +# uncheck(selector[, options]) + +{{< admonition type="warning" >}} + +Use locator-based [`locator.uncheck([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/uncheck/) instead. + +{{< /admonition >}} + +This method is used to unselect an input checkbox. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | `''` | A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.strict | boolean | `false` | When `true`, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the checkbox is unchecked. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/url.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/url.md new file mode 100644 index 000000000..e7f71c8a4 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/url.md @@ -0,0 +1,14 @@ +--- +title: 'url()' +description: 'Browser module: frame.url method' +--- + +# url() + +Returns the frame's URL. + +### Returns + +| Type | Description | +| ------ | ---------------- | +| string | The frame's URL. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitforfunction.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitforfunction.md new file mode 100644 index 000000000..4191a9700 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitforfunction.md @@ -0,0 +1,26 @@ +--- +title: 'waitForFunction(pageFunction, arg[, options])' +description: 'Browser module: frame.waitForFunction(pageFunction, arg[, options]) method' +--- + +# waitForFunction(pageFunction, arg[, options]) + +Returns when the `pageFunction` returns a truthy value. + + + +| Parameter | Type | Default | Description | +| --------------- | --------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| pageFunction | function | | Function to be evaluated in the page context. | +| arg | string | `''` | Optional argument to pass to `pageFunction` | +| options | object | `null` | | +| options.polling | number or `raf` | `raf` | If `polling` is `'raf'`, then `pageFunction` is constantly executed in `requestAnimationFrame` callback. If `polling` is a number, then it is treated as an interval in milliseconds at which the function would be executed. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------- | +| Promise<[JSHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/jshandle/)> | The `JSHandle` instance associated with the page. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitforloadstate.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitforloadstate.md new file mode 100644 index 000000000..18244fd25 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitforloadstate.md @@ -0,0 +1,44 @@ +--- +title: 'waitForLoadState(state[, options])' +description: 'Browser module: frame.waitForLoadState(state[, options]) method' +--- + +# waitForLoadState(state[, options]) + +{{< admonition type="caution" >}} + +This method has **known issues**. For details, refer to [#880](https://github.com/grafana/xk6-browser/issues/880). + +{{< /admonition >}} + +This waits for the given load state to be reached. It will immediately unblock if that lifecycle event has already been received. + + + +| Parameter | Type | Default | Description | +| --------------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| state | string | `load` | Optional load state to wait for. See [Events](#events) for more details. | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Events + +{{< admonition type="caution" >}} + +`networkidle` is DISCOURAGED. Don't use this method for testing especially with chatty websites where the event may never fire, rely on web assertions to assess readiness instead. + +{{< /admonition >}} + +Events can be either: + +- `'domcontentloaded'` - consider operation to be finished when the `DOMContentLoaded` event is fired. +- `'load'` - consider operation to be finished when the `load` event is fired. +- `'networkidle'` - Consider operation to be finished when there are no network connections for at least `500` ms. + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the load state is reached. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitfornavigation.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitfornavigation.md new file mode 100644 index 000000000..5fda14139 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitfornavigation.md @@ -0,0 +1,38 @@ +--- +title: 'waitForNavigation([options])' +description: 'Browser module: frame.waitForNavigation([options]) method' +--- + +# waitForNavigation([options]) + +Waits for the given navigation lifecycle event to occur and returns the main resource response. + + + +| Parameter | Type | Default | Description | +| ----------------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.waitUntil | string | `load` | When to consider operation to have succeeded. See [Events](#events) for more details. | + + + +### Events + +{{< admonition type="caution" >}} + +`networkidle` is DISCOURAGED. Don't use this method for testing especially with chatty websites where the event may never fire, rely on web assertions to assess readiness instead. + +{{< /admonition >}} + +Events can be either: + +- `'domcontentloaded'` - consider operation to be finished when the `DOMContentLoaded` event is fired. +- `'load'` - consider operation to be finished when the `load` event is fired. +- `'networkidle'` - Consider operation to be finished when there are no network connections for at least `500` ms. + +### Returns + +| Type | Description | +| --------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- | +| Promise/javascript-api/k6-browser/response/)> | The `Response` instance associated with the frame. Else, it returns `null` | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitforselector.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitforselector.md new file mode 100644 index 000000000..19f6b0f37 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitforselector.md @@ -0,0 +1,41 @@ +--- +title: 'waitForSelector(selector[, options])' +description: 'Browser module: frame.waitForSelector(selector[, options]) method' +--- + +# waitForSelector(selector[, options]) + +{{< admonition type="note" >}} + +Use web assertions that assert visibility or a locator-based [`locator.waitFor([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/waitfor/) instead. + +{{< /admonition >}} + +Returns when element specified by selector satisfies `state` option. + + + +| Parameter | Type | Default | Description | +| --------------- | ------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | `''` | A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. | +| options | object | `null` | | +| options.state | string | `visible` | Can be either `attached`, `detached`, `visible`, `hidden` See [Element states](#element-states) for more details. | +| options.strict | boolean | `false` | When `true`, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Element states + +Element states can be either: + +- `'attached'` - wait for element to be present in DOM. +- `'detached'` - wait for element to not be present in DOM. +- `'visible'` - wait for element to have non-empty bounding box and no `visibility:hidden`. +- `'hidden'` - wait for element to be either detached from DOM, or have an empty bounding box or `visibility:hidden`. + +### Returns + +| Type | Description | +| -------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the [ElementHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/keyboard/) when a matching element is found, or `null` if the element is not found. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitfortimeout.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitfortimeout.md new file mode 100644 index 000000000..8f19183da --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/frame/waitfortimeout.md @@ -0,0 +1,20 @@ +--- +title: 'waitForTimeout(timeout)' +description: 'Browser module: waitForTimeout(timeout) method' +--- + +# waitForTimeout(timeout) + +{{< admonition type="note" >}} + +Never wait for timeout in production, use this only for debugging. Tests that wait for time are inherently flaky. Use [`Locator`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/) actions and web assertions that wait automatically. + +{{< /admonition >}} + +Waits for the given `timeout` in milliseconds. + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------- | +| `Promise` | A Promise that fulfills when the timeout is reached. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/isconnected.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/isconnected.md new file mode 100644 index 000000000..0f6314a48 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/isconnected.md @@ -0,0 +1,49 @@ +--- +title: 'isConnected()' +description: 'Browser module: isConnected method' +--- + +# isConnected() + +{{< admonition type="caution" >}} + +This feature has **known issues**. +For details, refer to [#453](https://github.com/grafana/xk6-browser/issues/453). + +{{< /admonition >}} + +Indicates whether the [CDP](https://chromedevtools.github.io/devtools-protocol/) connection to the browser process is active or not. + +### Returns + +| Type | Description | +| ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| boolean | Returns `true` if the [browser module](https://grafana.com/docs/k6//javascript-api/k6-browser) is connected to the browser application. Otherwise, returns `false`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default function () { + const isConnected = browser.isConnected(); + console.log(isConnected); // true +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/_index.md new file mode 100644 index 000000000..0fa2307d4 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/_index.md @@ -0,0 +1,60 @@ +--- +title: 'JSHandle' +slug: 'jshandle' +description: 'Browser module: JSHandle Class' +weight: 06 +--- + +# JSHandle + +Represents a reference to a JavaScript object within the context of a webpage. This allows you to interact with JavaScript objects directly from your script. + +{{< docs/shared source="k6" lookup="browser-module-wip.md" version="" >}} + +## Supported APIs + +| Method | Description | +| --------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [asElement()](https://grafana.com/docs/k6//javascript-api/k6-browser/jshandle/aselement) | Returns either `null` or the object handle itself, if the object handle is an instance of [ElementHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/). | +| [dispose()](https://grafana.com/docs/k6//javascript-api/k6-browser/jshandle/dispose) | Stops referencing the element handle. | +| [evaluate(pageFunction[, arg])](https://grafana.com/docs/k6//javascript-api/k6-browser/jshandle/evaluate) | Evaluates the `pageFunction` and returns its return value. | +| [evaluateHandle(pageFunction[, arg])](https://grafana.com/docs/k6//javascript-api/k6-browser/jshandle/evaluatehandle) | Evaluates the `pageFunction` and returns a `JSHandle`. | +| [getProperties()](https://grafana.com/docs/k6//javascript-api/k6-browser/jshandle/getproperties) | Fetches a map with own property names of of the `JSHandle` with their values as `JSHandle` instances. | +| [jsonValue()](https://grafana.com/docs/k6//javascript-api/k6-browser/jshandle/jsonvalue) | Fetches a JSON representation of the object. | + +### Example + +{{< code >}} + + + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/'); + const jsHandle = await page.evaluateHandle(() => document.head); + // ... + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/aselement.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/aselement.md new file mode 100644 index 000000000..2549c5067 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/aselement.md @@ -0,0 +1,53 @@ +--- +title: 'asElement()' +description: 'Browser module: JSHandle.asElement method' +--- + +# asElement() + +Returns either `null` or the object handle itself, provided the object handle is an instance of [ElementHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle/). + +### Returns + +| Type | Description | +| --------------------- | ------------------------------- | +| ElementHandle \| null | The ElementHandle if available. | + +### Example + +{{< code >}} + + + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/'); + const jsHandle = await page.evaluateHandle(() => document.head); + + const element = jsHandle.asElement(); + console.log(await element.innerHTML()); //
... + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/dispose.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/dispose.md new file mode 100644 index 000000000..ca01bc40c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/dispose.md @@ -0,0 +1,54 @@ +--- +title: 'dispose()' +description: 'Browser module: JSHandle.dispose method' +--- + +# dispose() + +Releases the remote object this references. + +### Returns + +| Type | Description | +| ------------- | ------------------------------- | +| Promise | Resolves to null if successful. | + +### Example + +{{< code >}} + + + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/'); + const jsHandle = await page.evaluateHandle(() => document.head); + + await jsHandle.dispose(); + const element = jsHandle.asElement(); + await element.innerHTML(); // Error! JS handle is disposed + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/evaluate.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/evaluate.md new file mode 100644 index 000000000..c058c9282 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/evaluate.md @@ -0,0 +1,62 @@ +--- +title: 'evaluate(pageFunction[, arg])' +description: 'Browser module: JSHandle.evaluate(pageFunction[, arg]) method' +--- + +# evaluate(pageFunction[, arg]) + +Returns the value of the `pageFunction` invocation. It passes this handle as the first argument to the `pageFunction`. + + + +| Parameter | Type | Defaults | Description | +| ------------ | ------------------ | -------- | -------------------------------------------- | +| pageFunction | function or string | | Function to be evaluated. | +| arg | string | `''` | Optional argument to pass to `pageFunction`. | + + + +### Returns + +| Type | Description | +| ------------ | ----------------------------------- | +| Promise | The return value of `pageFunction`. | + +### Example + +{{< code >}} + + + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/browser.php'); + const jsHandle = await page.evaluateHandle(() => document.body); + + const innerHTML = await jsHandle.evaluate((node) => node.innerHTML); + console.log(innerHTML); //

< Back

... + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/evaluatehandle.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/evaluatehandle.md new file mode 100644 index 000000000..8d782ef1d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/evaluatehandle.md @@ -0,0 +1,66 @@ +--- +title: 'evaluateHandle(pageFunction[, arg])' +description: 'Browser module: JSHandle.evaluateHandle(pageFunction[, arg]) method' +--- + +# evaluateHandle(pageFunction[, arg]) + +Returns the value of the `pageFunction` invocation as a `JSHandle`. It passes this handle as the first argument to the `pageFunction`. + +The only difference between [evaluate](https://grafana.com/docs/k6//javascript-api/k6-browser/jshandle/evaluate/) and `evaluateHandle` is that `evaluateHandle` returns [JSHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/jshandle/). + + + +| Parameter | Type | Defaults | Description | +| ------------ | ------------------ | -------- | -------------------------------------------- | +| pageFunction | function or string | | Function to be evaluated. | +| arg | string | `''` | Optional argument to pass to `pageFunction`. | + + + +### Returns + +| Type | Description | +| ----------------- | --------------------------------------------------- | +| Promise | A `JSHandle` of the return value of `pageFunction`. | + +### Example + +{{< code >}} + + + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/browser.php'); + const jsHandle = await page.evaluateHandle(() => document.body); + + const obj = await jsHandle.evaluateHandle((handle) => { + return { innerText: handle.innerText }; + }); + console.log(await obj.jsonValue()); // {"innerText":"< Back... + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/getproperties.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/getproperties.md new file mode 100644 index 000000000..c2bb03994 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/getproperties.md @@ -0,0 +1,55 @@ +--- +title: 'getProperties()' +description: 'Browser module: JSHandle.getProperties() method' +--- + +# getProperties() + +This allows you to inspect and interact with the properties of the object within the page context. + +### Returns + +| Type | Description | +| ------------------------------ | ----------------------------------------------------------------------------------- | +| Promise> | A map with property names as keys and `JSHandle` instances for the property values. | + +### Example + +{{< code >}} + + + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/browser.php'); + const jsHandle = await page.evaluateHandle(() => { + return { window, document }; + }); + + const properties = await jsHandle.getProperties(); + console.log(properties); // {"window":{},"document":{}} + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/jsonvalue.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/jsonvalue.md new file mode 100644 index 000000000..67d6b78cc --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/jshandle/jsonvalue.md @@ -0,0 +1,55 @@ +--- +title: 'jsonValue()' +description: 'Browser module: JSHandle.jsonValue() method' +--- + +# jsonValue() + +Retrieves the JSON representation of the JavaScript object that the JSHandle refers to, making it easy to work with the data outside the browser context. + +### Returns + +| Type | Description | +| ------------ | ------------------------------------ | +| Promise | A JSON representation of the object. | + +### Example + +{{< code >}} + + + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/browser.php'); + const jsHandle = await page.evaluateHandle(() => { + return { document }; + }); + + const j = await jsHandle.jsonValue(); + console.log(j); // {"document":{"location":{"ancestorOrigins":{}... + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/_index.md new file mode 100644 index 000000000..374e0f9d7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/_index.md @@ -0,0 +1,60 @@ +--- +title: 'Keyboard' +description: 'Browser module: Keyboard Class' +weight: 07 +--- + +# Keyboard + +{{< docs/shared source="k6" lookup="browser-module-wip.md" version="" >}} + +`Keyboard` provide a way to interact with a virtual keyboard. + +| Method | Description | +| ---------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ | +| [down(key)](https://grafana.com/docs/k6//javascript-api/k6-browser/keyboard/down) | Dispatches a `keydown` event. | +| [up(key)](https://grafana.com/docs/k6//javascript-api/k6-browser/keyboard/up) | Dispatches a `keyup` event. | +| [press(key[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/keyboard/press) | Dispatches a `keydown` event followed by an `keyup` event. | +| [type(text[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/keyboard/type) | Dispatches a `keydown`, `keypress` or`input`, and `keyup` events for each character in the text. | +| [insertText(text)](https://grafana.com/docs/k6//javascript-api/k6-browser/keyboard/inserttext) | Dispatches only an `input` event. | + +### Example + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + } +} + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/my_messages.php', { + waitUntil: 'networkidle' + }); + + const login = page.locator('input[name="login"]'); + await login.click(); + await page.keyboard.type("admin"); + + const password = page.locator('input[name="password"]'); + await password.click(); + await page.keyboard.type("123"); + + await Promise.all([ + page.keyboard.press('Enter'), // submit + page.waitForNavigation(), + ]); + + await page.close(); +} +``` \ No newline at end of file diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/down.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/down.md new file mode 100644 index 000000000..c47eec1de --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/down.md @@ -0,0 +1,18 @@ +--- +title: 'down(key)' +description: 'Dispatches a `keydown` event. This is useful for simulating holding down a key.' +--- + +# down(key) + +Dispatches a `keydown` event. This is useful for simulating holding down a key. + +| Parameter | Type | Description | +| --------- | ------ | ------------------------------------------ | +| key | string | Name of the key to press, such as `Enter`. | + +### Returns + +| Type | Description | +| --------------- | --------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the `keydown` event has been dispatched. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/inserttext.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/inserttext.md new file mode 100644 index 000000000..883a12aa1 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/inserttext.md @@ -0,0 +1,18 @@ +--- +title: 'insertText(text)' +description: 'Dispatches only an `input` event.' +--- + +# insertText(text) + +Dispatches only an `input` event. This is useful for simulating typing text into an input field. + +| Parameter | Type | Description | +| --------- | ------ | ---------------------------------------- | +| text | string | The text to insert into the input field. | + +### Returns + +| Type | Description | +| --------------- | ----------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the text has been inserted into the input field. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/press.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/press.md new file mode 100644 index 000000000..e43208131 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/press.md @@ -0,0 +1,20 @@ +--- +title: 'press(key[, options])' +description: 'Dispatches a `keydown` event followed by a `keyup` event. This is useful for simulating pressing and releasing a key.' +--- + +# press(key[, options]) + +Dispatches a `keydown` event followed by a `keyup` event. This is useful for simulating pressing a key. + +| Parameter | Type | Default | Description | +| ------------- | ------ | ------- | ----------------------------------------------------------------------- | +| key | string | `''` | Name of the key to press, such as `Enter`. | +| options | object | `null` | Optional settings. | +| options.delay | number | `0` | Optional. Time to wait before running the press action in milliseconds. | + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------ | +| `Promise` | A Promise that fulfills when the key has been pressed. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/type.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/type.md new file mode 100644 index 000000000..6d3ddd5f5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/type.md @@ -0,0 +1,20 @@ +--- +title: 'type(text[, options])' +description: 'Dispatches a `keydown`, `keypress` or`input`, and `keyup` events for each character in the text.' +--- + +# type(text[, options]) + +Dispatches a `keydown`, `keypress` or`input`, and `keyup` events for each character in the text. This is useful for simulating typing text into an input field. + +| Parameter | Type | Default | Description | +| ------------- | ------ | ------- | -------------------------------------------------------------------- | +| text | string | `''` | The text to type into the input field. | +| options | object | `null` | Optional settings. | +| options.delay | number | `0` | Optional. Time to wait before typing each character in milliseconds. | + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------------------------ | +| `Promise` | A Promise that fulfills when the text has been typed in the input field. | \ No newline at end of file diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/up.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/up.md new file mode 100644 index 000000000..d6fd8ab88 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/keyboard/up.md @@ -0,0 +1,18 @@ +--- +title: 'up(key)' +description: 'Dispatches a `keyup` event. This is useful for simulating releasing a key.' +--- + +# up(key) + +Dispatches a `keyup` event. This is useful for simulating releasing a key. + +| Parameter | Type | Description | +| --------- | ------ | -------------------------------------------- | +| key | string | Name of the key to release, such as `Enter`. | + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the `keyup` event has been dispatched. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/_index.md new file mode 100644 index 000000000..b795c02d3 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/_index.md @@ -0,0 +1,114 @@ +--- +title: "Locator" +description: "Browser module: Locator Class" +weight: 08 +weight: 08 +--- + +# Locator + +The Locator API makes it easier to work with dynamically changing elements. Some of the benefits of using it over existing ways to locate an element (e.g. `Page.$()`) include: + +- Helps with writing robust tests by finding an element even if the underlying frame navigates. +- Makes it easier to work with dynamic web pages and SPAs built with Svelte, React, Vue, etc. +- Enables the use of test abstractions like the Page Object Model (POM) pattern to simplify and organize tests. +- `strict` mode is enabled for all `locator` methods, which means that if more than one element matches the given selector it will throw an error. + +Locator can be created with the [page.locator(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/locator) method. + +| Method | Description | +| ------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------- | +| [check([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/check) {{< docs/bwipt id="471" >}} | Select the input checkbox. | +| [clear([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/clear) | Clears text boxes and input fields of any existing values. | +| [click([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/click) {{< docs/bwipt id="471" >}} | Mouse click on the chosen element. | +| [dblclick([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/dblclick) {{< docs/bwipt id="471" >}} | Mouse double click on the chosen element. | +| [dispatchEvent(type, eventInit, [options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/dispatchevent) | Dispatches HTML DOM event types e.g. `'click'`. | +| [fill(value, [options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/fill) | Fill an `input`, `textarea` or `contenteditable` element with the provided value. | +| [focus([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/focus) | Calls [focus](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) on the element, if it can be focused. | +| [getAttribute(name, [options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/getattribute) | Returns the element attribute value for the given attribute name. | +| [hover([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/hover) {{< docs/bwipt id="471" >}} | Hovers over the element. | +| [innerHTML([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/innerhtml) | Returns the `element.innerHTML`. | +| [innerText([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/innertext) | Returns the `element.innerText`. | +| [inputValue([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/inputvalue) | Returns `input.value` for the selected `input`, `textarea` or `select` element. | +| [isChecked([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/ischecked) | Checks if the `checkbox` `input` type is selected. | +| [isDisabled([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/isdisabled) | Checks if the element is `disabled`. | +| [isEditable([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/iseditable) | Checks if the element is `editable`. | +| [isEnabled([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/isenabled) | Checks if the element is `enabled`. | +| [isHidden()](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/ishidden) | Checks if the element is `hidden`. | +| [isVisible()](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/isvisible) | Checks if the element is `visible`. | +| [press(key, [options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/press) | Press a single key on the keyboard or a combination of keys. | +| [selectOption(values, [options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/selectoption) {{< docs/bwipt id="470" >}} | Select one or more options which match the values. | +| [setChecked(checked[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/setchecked) | Sets the `checkbox` or `radio` input element's value to the specified checked or unchecked state. | +| [tap([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/tap) {{< docs/bwipt id="471" >}} | Tap on the chosen element. | +| [textContent([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/textcontent) | Returns the `element.textContent`. | +| [type(text, [options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/type) | Type in the text into the input field. | +| [uncheck([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/uncheck) {{< docs/bwipt id="471" >}} | Unselect the `input` checkbox. | +| [waitFor([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/waitfor) {{< docs/bwipt id="472" >}} | Wait for the element to be in a particular state e.g. `visible`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/flip_coin.php'); + + /* + In this example, we will use two locators, matching a + different betting button on the page. If you were to query + the buttons once and save them as below, you would see an + error after the initial navigation. Try it! + + const heads = await page.$("input[value='Bet on heads!']"); + const tails = await page.$("input[value='Bet on tails!']"); + + The Locator API allows you to get a fresh element handle each + time you use one of the locator methods. And, you can carry a + locator across frame navigations. Let's create two locators; + each locates a button on the page. + */ + const heads = page.locator("input[value='Bet on heads!']"); + const tails = page.locator("input[value='Bet on tails!']"); + + const currentBet = page.locator("//p[starts-with(text(),'Your bet: ')]"); + + // In the following Promise.all the tails locator clicks + // on the tails button by using the locator's selector. + // Since clicking on each button causes page navigation, + // waitForNavigation is needed -- this is because the page + // won't be ready until the navigation completes. + // Setting up the waitForNavigation first before the click + // is important to avoid race conditions. + await Promise.all([page.waitForNavigation(), tails.click()]); + console.log(await currentBet.innerText()); + // the heads locator clicks on the heads button + // by using the locator's selector. + await Promise.all([page.waitForNavigation(), heads.click()]); + console.log(await currentBet.innerText()); + await Promise.all([page.waitForNavigation(), tails.click()]); + console.log(await currentBet.innerText()); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/check.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/check.md new file mode 100644 index 000000000..81489995e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/check.md @@ -0,0 +1,66 @@ +--- +title: 'check([options])' +description: 'Browser module: locator.check method' +--- + +# check([options]) + +{{< admonition type="caution" >}} + +This feature has known issues. For details, refer to +[#471](https://github.com/grafana/xk6-browser/issues/471) and [#475](https://github.com/grafana/xk6-browser/issues/475). + +{{< /admonition >}} + +Use this method to select an `input` checkbox. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the check action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const checkbox = page.locator('#checkbox1'); + await checkbox.check(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/clear.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/clear.md new file mode 100644 index 000000000..d5807497a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/clear.md @@ -0,0 +1,77 @@ +--- +title: 'clear([options])' +description: 'Browser module: locator.clear method' +--- + +# clear([options]) + +Clears text boxes and input fields (`input`, `textarea` or `contenteditable` elements) of any existing values. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the clear action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import { check } from "https://jslib.k6.io/k6-utils/1.5.0/index.js"; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext(); + const page = await context.newPage(); + + await page.goto("https://test.k6.io/my_messages.php", { + waitUntil: 'networkidle', + }); + + // Fill an input element with some text that we will later clear + const login = page.locator('input[name="login"]'); + await login.type('admin'); + + // Check that the element has been filled with text + await check(login, { + 'not empty': async lo => await lo.inputValue() != '', + }); + + // Now clear the text from the element + await login.clear(); + + // Check that the element is now empty + await check(login, { + empty: async lo => await lo.inputValue() == '', + }); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/click.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/click.md new file mode 100644 index 000000000..3751cca72 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/click.md @@ -0,0 +1,102 @@ +--- +title: 'click([options])' +description: 'Browser module: locator.click method' +--- + +# click([options]) + +{{< admonition type="caution" >}} + +This method has **known issues**. For details, refer to [#471](https://github.com/grafana/xk6-browser/issues/471) and [#474](https://github.com/grafana/xk6-browser/issues/474). + +{{< /admonition >}} + +Mouse click on the chosen element. + + + +| Parameter | Type | Default | Description | +| ------------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.button | string | `left` | The mouse button (`left`, `middle` or `right`) to use during the action. | +| options.clickCount | number | `1` | The number of times the action is performed. | +| options.delay | number | `0` | Milliseconds to wait between `mousedown` and `mouseup`. | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.modifiers | string[] | `null` | `Alt`, `Control`, `Meta` or `Shift` modifiers keys pressed during the action. If not specified, currently pressed modifiers are used. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the click action is finished. | + +### Examples + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + const button = page.locator('#counter-button'); + await button.click(); +} +``` + +{{< /code >}} + +When a click action results in a page navigation, remember to work with `Promise.all()` and `page.waitForNavigation()` to properly handle the asynchronous operation. + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/'); + const messagesLink = page.locator('a[href="/my_messages.php"]'); + + await Promise.all([page.waitForNavigation(), messagesLink.click()]); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/dblclick.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/dblclick.md new file mode 100644 index 000000000..ca3c70c27 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/dblclick.md @@ -0,0 +1,68 @@ +--- +title: 'dblclick([options])' +description: 'Browser module: locator.dblclick method' +--- + +# dblclick([options]) + +{{< admonition type="caution" >}} + +This feature has known issues. For details, refer to [#471](https://github.com/grafana/xk6-browser/issues/471). + +{{< /admonition >}} + +Mouse double click on the chosen element. + + + +| Parameter | Type | Default | Description | +| ------------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.button | string | `left` | The mouse button (`left`, `middle` or `right`) to use during the action. | +| options.delay | number | `0` | Milliseconds to wait between `mousedown` and `mouseup`. | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.modifiers | string[] | `null` | `Alt`, `Control`, `Meta` or `Shift` modifiers keys pressed during the action. If not specified, currently pressed modifiers are used. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | ----------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the double click action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const button = page.locator('#counter-button'); + await button.dblclick(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/dispatchevent.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/dispatchevent.md new file mode 100644 index 000000000..3a4c1aa05 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/dispatchevent.md @@ -0,0 +1,57 @@ +--- +title: 'dispatchEvent(type, eventInit, [options])' +description: 'Browser module: locator.dispatchEvent method' +--- + +# dispatchEvent(type, eventInit, [options]) + +Dispatches HTML DOM event types e.g. `'click'`. + + + +| Parameter | Type | Defaults | Description | +| --------------- | ------ | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| type | string | `''` | DOM event type e.g. `'click'`. | +| eventInit | object | `null` | Optional event specific properties. See [eventInit](#eventinit) for more details. | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### eventInit + +Since eventInit is event-specific, please refer to the events documentation for the lists of initial properties: + +- [DragEvent](https://developer.mozilla.org/en-US/docs/Web/API/DragEvent/DragEvent) +- [FocusEvent](https://developer.mozilla.org/en-US/docs/Web/API/FocusEvent/FocusEvent) +- [KeyboardEvent](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent) +- [MouseEvent](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent) +- [PointerEvent](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/PointerEvent) +- [TouchEvent](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/TouchEvent) +- [Event](https://developer.mozilla.org/en-US/docs/Web/API/Event/Event) + +### Returns + +| Type | Description | +| --------------- | ----------------------------------------------------- | +| `Promise` | A Promise that fulfills once the event is dispatched. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + const button = page.locator('#counter-button'); + await button.dispatchEvent('click'); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/fill.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/fill.md new file mode 100644 index 000000000..11f7cb162 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/fill.md @@ -0,0 +1,56 @@ +--- +title: 'fill(value, [options])' +description: 'Browser module: locator.fill method' +--- + +# fill(value, [options]) + +Fill an `input`, `textarea` or `contenteditable` element with the provided value. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| value | string | `''` | Value to set for the `input`, `textarea` or `contenteditable` element. | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | --------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the fill action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const textbox = page.locator('#text1'); + await textbox.fill('hello world!'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/focus.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/focus.md new file mode 100644 index 000000000..47ebcd366 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/focus.md @@ -0,0 +1,53 @@ +--- +title: 'focus([options])' +description: 'Browser module: locator.focus method' +--- + +# focus([options]) + +Calls [focus](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) on the element, if it can be focused on. + + + +| Parameter | Type | Default | Description | +| --------------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the focus action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const textbox = page.locator('#text1'); + await textbox.focus(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/getattribute.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/getattribute.md new file mode 100644 index 000000000..7b802900d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/getattribute.md @@ -0,0 +1,55 @@ +--- +title: 'getAttribute(name, [options])' +description: 'Browser module: locator.getAttribute method' +--- + +# getAttribute(name, [options]) + +Returns the element attribute value for the given attribute name. + + + +| Parameter | Type | Default | Description | +| --------------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| name | string | `''` | Attribute name to get the value for. | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| ----------------- | --------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the string value of the attribute or `null` if the attribute is not present. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const textbox = page.locator('#text1'); + const attribute = await textbox.getAttribute('onfocus'); + console.log(attribute); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/hover.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/hover.md new file mode 100644 index 000000000..b20ad2fc5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/hover.md @@ -0,0 +1,67 @@ +--- +title: 'hover([options])' +description: 'Browser module: locator.hover method' +--- + +# hover([options]) + +{{< admonition type="caution" >}} + +This feature has known issues. For details, refer to +[#471](https://github.com/grafana/xk6-browser/issues/471). + +{{< /admonition >}} + +Hovers over the element. + + + +| Parameter | Type | Default | Description | +| ------------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.modifiers | string[] | `null` | `Alt`, `Control`, `Meta` or `Shift` modifiers keys pressed during the action. If not specified, currently pressed modifiers are used. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the hover action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const offScreenElement = page.locator('#off-screen'); + await offScreenElement.hover(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/innerhtml.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/innerhtml.md new file mode 100644 index 000000000..800bc9c74 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/innerhtml.md @@ -0,0 +1,53 @@ +--- +title: 'innerHTML([options])' +description: 'Browser module: locator.innerHTML method' +--- + +# innerHTML([options]) + +Returns the `element.innerHTML`. + + + +| Parameter | Type | Default | Description | +| --------------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| ----------------- | ----------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the inner HTML of the element. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const offScreen = page.locator('#off-screen'); + console.log(await offScreen.innerHTML()); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/innertext.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/innertext.md new file mode 100644 index 000000000..0d347f5c2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/innertext.md @@ -0,0 +1,53 @@ +--- +title: 'innerText([options])' +description: 'Browser module: locator.innerText method' +--- + +# innerText([options]) + +Returns the `element.innerText`. + + + +| Parameter | Type | Default | Description | +| --------------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| ----------------- | ----------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the inner text of the element. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const offScreen = page.locator('#off-screen'); + console.log(await offScreen.innerText()); // Off page div +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/inputvalue.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/inputvalue.md new file mode 100644 index 000000000..9f08b68e1 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/inputvalue.md @@ -0,0 +1,54 @@ +--- +title: 'inputValue([options])' +description: 'Browser module: locator.inputValue method' +--- + +# inputValue([options]) + +Returns `input.value` for the selected `input`, `textarea` or `select` element. + + + +| Parameter | Type | Default | Description | +| --------------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| ----------------- | ------------------------------------------------------------ | +| `Promise` | A Promise that fulfills with the input value of the element. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const textInput = page.locator('#text1'); + await textInput.fill('Hello world!'); + console.log(await textInput.inputValue()); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/ischecked.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/ischecked.md new file mode 100644 index 000000000..401c834c8 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/ischecked.md @@ -0,0 +1,56 @@ +--- +title: 'isChecked([options])' +description: 'Browser module: locator.isChecked method' +--- + +# isChecked([options]) + +Checks to see if the `checkbox` `input` type is selected or not. + + + +| Parameter | Type | Default | Description | +| --------------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with `true` if the element is checked, else `false`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const checkbox = page.locator('#checkbox1'); + const isChecked = await checkbox.isChecked(); + if (!isChecked) { + await checkbox.check(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/isdisabled.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/isdisabled.md new file mode 100644 index 000000000..36b24ffb2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/isdisabled.md @@ -0,0 +1,56 @@ +--- +title: 'isDisabled([options])' +description: 'Browser module: locator.isDisabled method' +--- + +# isDisabled([options]) + +Checks if the element is disabled. + + + +| Parameter | Type | Default | Description | +| --------------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with `true` if the element is disabled, else `false`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const text = page.locator('#input-text-disabled'); + const isDisabled = await text.isDisabled(); + if (isDisabled) { + console.log('element is disabled'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/iseditable.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/iseditable.md new file mode 100644 index 000000000..d2caedaf4 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/iseditable.md @@ -0,0 +1,56 @@ +--- +title: 'isEditable([options])' +description: 'Browser module: locator.isEditable method' +--- + +# isEditable([options]) + +Checks if the element is editable. + + + +| Parameter | Type | Default | Description | +| --------------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | ----------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with `true` if the element is editable, else `false`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const text = page.locator('#text1'); + const isEditable = await text.isEditable(); + if (isEditable) { + text.fill('hello world!'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/isenabled.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/isenabled.md new file mode 100644 index 000000000..dbd1d6bab --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/isenabled.md @@ -0,0 +1,57 @@ +--- +title: 'isEnabled([options])' +description: 'Browser module: locator.isEnabled method' +--- + +# isEnabled([options]) + +Checks if the element is enabled. + + + +| Parameter | Type | Default | Description | +| --------------- | ------ | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with `true` if the element is enabled, else `false`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + const text = page.locator('#text1'); + const isEnabled = await text.isEnabled(); + if (isEnabled) { + console.log('element is enabled'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/ishidden.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/ishidden.md new file mode 100644 index 000000000..cf231904c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/ishidden.md @@ -0,0 +1,47 @@ +--- +title: 'isHidden()' +description: 'Browser module: locator.isHidden method' +--- + +# isHidden() + +Checks if the element is hidden. + +### Returns + +| Type | Description | +| --------------- | --------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with `true` if the element is hidden, else `false`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const text = page.locator('#input-text-hidden'); + const isHidden = await text.isHidden(); + if (isHidden) { + console.log('element is hidden'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/isvisible.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/isvisible.md new file mode 100644 index 000000000..e417b8343 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/isvisible.md @@ -0,0 +1,47 @@ +--- +title: 'isVisible()' +description: 'Browser module: locator.isVisible method' +--- + +# isVisible() + +Checks if the element is visible. + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with `true` if the element is visible, else `false`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const text = page.locator('#text1'); + const isVisible = await text.isVisible(); + if (isVisible) { + console.log('element is visible'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/press.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/press.md new file mode 100644 index 000000000..0696b9c50 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/press.md @@ -0,0 +1,58 @@ +--- +title: 'press(key, [options])' +description: 'Browser module: locator.press method' +--- + +# press(key, [options]) + +Press a single key on the keyboard or a combination of keys. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| key | string | `''` | Name of the key to press or a character to generate, such as `ArrowLeft` or `a`. A superset of the key values can be found [here](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values). | +| options | object | `null` | | +| options.delay | number | `0` | Milliseconds to wait between `keydown` and `keyup`. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------ | +| `Promise` | A Promise that fulfills when the key is pressed. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const text = page.locator('#text1'); + await text.press('i'); + await text.press('ArrowLeft'); + await text.press('h'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/selectoption.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/selectoption.md new file mode 100644 index 000000000..7019f8a69 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/selectoption.md @@ -0,0 +1,63 @@ +--- +title: 'selectOption(values, [options])' +description: 'Browser module: locator.selectOption method' +--- + +# selectOption(values, [options]) + +{{< admonition type="caution" >}} + +This feature has **known issues**. For details, refer to +[#470](https://github.com/grafana/xk6-browser/issues/470) and [#471](https://github.com/grafana/xk6-browser/issues/471). + +{{< /admonition >}} + +Select one or more options which match the values. + + + +| Parameter | Type | Default | Description | +| ------------------- | ---------------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| values | string or string[] or object | `''` | If the `select` has the multiple attribute, all matching options are selected, otherwise only the first option matching one of the passed options is selected. Object can be made up of keys with `value`, `label` or `index`. | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| ------------------------ | ------------------------------------------------------------------------- | +| `Promise>` | A Promise that fulfills with the selected options as an array of strings. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const options = page.locator('#numbers-options'); + await options.selectOption('three'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/setchecked.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/setchecked.md new file mode 100644 index 000000000..1f53ddcdd --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/setchecked.md @@ -0,0 +1,63 @@ +--- +title: 'setChecked(checked[, options])' +description: 'Browser module: locator.setChecked method' +--- + +# setChecked(checked[, options]) + +Use this method to select or unselect an `input` or `radio` checkbox. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | Optional parameters. | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the check action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + const checkbox = await page.locator('#checkbox1'); + await checkbox.setChecked(true); + await checkbox.setChecked(false); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/tap.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/tap.md new file mode 100644 index 000000000..3b63fedd5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/tap.md @@ -0,0 +1,57 @@ +--- +title: 'tap([options])' +description: 'Browser module: locator.tap method' +--- + +# tap([options]) + +{{< admonition type="caution" >}} + +This feature has **known issues**. For details, refer to +[#436](https://github.com/grafana/xk6-browser/issues/436) and [#471](https://github.com/grafana/xk6-browser/issues/471). + +{{< /admonition >}} + +Tap on the chosen element. + + + +| Parameter | Type | Default | Description | +| ------------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.modifiers | string[] | `null` | `Alt`, `Control`, `Meta` or `Shift` modifiers keys pressed during the action. If not specified, currently pressed modifiers are used. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | -------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the tap action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export default async function () { + const page = await browser.newPage({ + hasTouch: true, + }); + + await page.goto('https://test.k6.io/browser.php'); + const options = page.locator('#numbers-options'); + await options.tap(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/textcontent.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/textcontent.md new file mode 100644 index 000000000..8c3bcd1f7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/textcontent.md @@ -0,0 +1,53 @@ +--- +title: 'textContent([options])' +description: 'Browser module: locator.textContent method' +--- + +# textContent([options]) + +Returns the `element.textContent`. + + + +| Parameter | Type | Default | Description | +| --------------- | ------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| ------------------------- | ------------------------------------------------------------------------ | +| `Promise` | A Promise that fulfills with the text content of the selector or `null`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const options = page.locator('#checkbox1'); + console.log(await options.textContent()); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/type.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/type.md new file mode 100644 index 000000000..d1a4d8aae --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/type.md @@ -0,0 +1,56 @@ +--- +title: 'type(text, [options])' +description: 'Browser module: locator.type method' +--- + +# type(text, [options]) + +Type in the text into the input field. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| text | string | `''` | A text to type into a focused element. | +| options | object | `null` | | +| options.delay | number | `0` | Milliseconds to wait between key presses. Defaults to `0`. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | --------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the type action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const text = page.locator('#text1'); + await text.type('hello world!'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/uncheck.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/uncheck.md new file mode 100644 index 000000000..e3bf951e0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/uncheck.md @@ -0,0 +1,67 @@ +--- +title: 'uncheck([options])' +description: 'Browser module: locator.uncheck method' +--- + +# uncheck([options]) + +{{< admonition type="caution" >}} + +This feature has **known issues**. +For details, refer to [#471](https://github.com/grafana/xk6-browser/issues/471). + +{{< /admonition >}} + +Unselect the `input` checkbox. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------------ | +| `Promise` | A Promise that fulfills when the uncheck action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + const checkbox = page.locator('#checkbox1'); + await checkbox.check(); + await checkbox.uncheck(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/waitfor.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/waitfor.md new file mode 100644 index 000000000..54ba2afcf --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/locator/waitfor.md @@ -0,0 +1,64 @@ +--- +title: 'waitFor([options])' +description: 'Browser module: locator.waitFor method' +--- + +# waitFor([options]) + +{{< admonition type="caution" >}} + +This feature has **known issues**. For details, +refer to [#472](https://github.com/grafana/xk6-browser/issues/472). + +{{< /admonition >}} + +Wait for the element to be in a particular state e.g. `visible`. + + + +| Parameter | Type | Default | Description | +| --------------- | ------ | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.state | string | `visible` | Can be `attached`, `detached`, `visible` or `hidden`. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | ----------------------------------------------------------------- | +| `Promise` | A Promise that fulfills once the element is in the desired state. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + const text = page.locator('#input-text-hidden'); + await text.waitFor({ + state: 'hidden', + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/metricmessage.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/metricmessage.md new file mode 100644 index 000000000..71d5416de --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/metricmessage.md @@ -0,0 +1,59 @@ +--- +title: 'MetricMessage' +description: 'Browser module: MetricMessage Object' +weight: 03 +--- + +# MetricMessage + +A `MetricMessage` object allows tagging of metrics that are measured and emitted for the page. + +`MetricMessage` objects are dispatched by the page when a handler is registered with [page.on('metric')](https://grafana.com/docs/k6//javascript-api/k6-browser/page/on). For each metric that is measured and emitted for the page, the k6 browser module delivers a `MetricMessage` object to the registered handlers, allowing them to pattern match and tag metrics. + +## tag + +The `tag` method matches the given `matches` with the current metric's url and name tags. When a match is found, it will use `name` to replace the existing URL and name tag values. + +Doing this helps group metrics with different URL and name tags that, in fact, reference the same resource, allowing for correlation over time and reducing the cardinality of the metrics. + + + +| Parameter | Type | Description | +| ----------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| tagMatch | object | The tagMatch object and its properties that are used for matching metrics. Required. | +| tagMatch.name | string | The name value that replaces the current metric's URL and name tag values, if a match is found. Required, and must not be an empty string. | +| tagMatch.matches | object[] | An array of objects containing the matchers which will be used to match against the current metric's URL and name tags. Required. | +| tagMatch.matches.url | RegExp | The regular expression used to find matches in the current metric's URL and name tags. Required. | +| tagMatch.matches.method | string? | Used to match the metric's method tag. Valid values are `'GET'`, `'POST'`, `'PUT'`, `'DELETE'`, `'PATCH'`, `'OPTIONS'`, `'HEAD'`, `'TRACE'` and `'CONNECT'`. It's optional, and when it's not set it will group all metrics regardless of the method tag. | + + + +### Example Usage + +{{< code >}} + + + +```javascript +// First we need to register a handler with `page.on('metric')`. +page.on('metric', (metric) => { + // It will find a match between the current metric url and name tags against + // the supplied regular expression in `url`. + metric.tag({ + // This is the new name value that will replace the existing value in the + // url and name tags when a match is found. + name: 'test', + // You can provide multiple matches here. + matches: [ + { + url: /^https:\/\/test\.k6\.io\/\?q=[0-9a-z]+$/, + // When a method is defined it will also need to match on that too. If a + // method is not provided it will match on all method types. + method: 'GET', + }, + ], + }); +}); +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/_index.md new file mode 100644 index 000000000..d5dd32913 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/_index.md @@ -0,0 +1,55 @@ +--- +title: 'Mouse' +description: 'Browser module: Mouse Class' +weight: 09 +--- + +# Mouse + +`Mouse` provides a way to interact with a virtual mouse. + +| Method | Description | +| -------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------- | +| [click(x, y[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/mouse/click) | Mouse clicks on the `x` and `y` coordinates. | +| [dblclick(x, y[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/mouse/dblclick) | Mouse double clicks on the `x` and `y` coordinates. | +| [down([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/mouse/down) | Dispatches a `mousedown` event on the mouse's current position. | +| [up([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/mouse/up) | Dispatches a `mouseup` event on the mouse's current position. | +| [move(x, y[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/mouse/move) | Dispatches a `mousemove` event on the mouse's current position. | + +### Example + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + } +} + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/', { + waitUntil: 'networkidle' +}); + + // Obtain ElementHandle for news link and navigate to it + // by clicking in the 'a' element's bounding box + const newsLinkBox = await page.$('a[href="/news.php"]'); + const boundingBox = await newsLinkBox.boundingBox(); + const x = newsLinkBox.x + newsLinkBox.width / 2; // center of the box + const y = newsLinkBox.y; + + await page.mouse.click(x, y); + + await page.close(); +} +``` \ No newline at end of file diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/click.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/click.md new file mode 100644 index 000000000..5fb3ede66 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/click.md @@ -0,0 +1,23 @@ +--- +title: 'click(x, y[, options])' +description: 'Mouse clicks on the `x` and `y` coordinates.' +--- + +# click(x, y[, options]) + +Mouse clicks on the `x` and `y` coordinates. It's a shorthand for calling [`mouse.move(x, y)`](https://grafana.com/docs/k6//javascript-api/k6-browser/mouse/move) followed by [`mouse.down()`](https://grafana.com/docs/k6//javascript-api/k6-browser/mouse/down) and [`mouse.up()`](https://grafana.com/docs/k6//javascript-api/k6-browser/mouse/up). This is useful for simulating a single mouse click. + +| Parameter | Type | Description | +| ------------------ | ------ | -------------------------------------------------------------------------------------------------------- | +| x | number | The x-coordinate to click on. | +| y | number | The y-coordinate to click on. | +| options | object | Optional. | +| options.button | string | The mouse button to click. Possible values are `'left'`, `'right'`, and `'middle'`. Default is `'left'`. | +| options.clickCount | number | The number of times to click. Default is `1`. | +| options.delay | number | The delay in milliseconds between `mousedown` and `mouseup` events. Default is `0`. | + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the mouse click action is complete. | \ No newline at end of file diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/dblclick.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/dblclick.md new file mode 100644 index 000000000..2e1292a41 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/dblclick.md @@ -0,0 +1,22 @@ +--- +title: 'dblclick(x, y[, options])' +description: 'Mouse double clicks on the `x` and `y` coordinates.' +--- + +# dblclick(x, y[, options]) + +Mouse double clicks on the `x` and `y` coordinates. Similar to [`mouse.click()`](https://grafana.com/docs/k6//javascript-api/k6-browser/mouse/click), but it simulates a double click. + +| Parameter | Type | Description | +| -------------- | ------ | --------------------------------------------------------------------------------------------------------------- | +| x | number | The x-coordinate to double click on. | +| y | number | The y-coordinate to double click on. | +| options | object | Optional. | +| options.button | string | The mouse button to double click. Possible values are `'left'`, `'right'`, and `'middle'`. Default is `'left'`. | +| options.delay | number | The delay in milliseconds between `mousedown` and `mouseup` events. Default is `0`. | + +### Returns + +| Type | Description | +| --------------- | ----------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the mouse double click action is complete. | \ No newline at end of file diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/down.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/down.md new file mode 100644 index 000000000..a90f2d76c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/down.md @@ -0,0 +1,20 @@ +--- +title: 'down([options])' +description: 'Mouse dispatches a `mousedown` event on the current position of the mouse.' +--- + +# down([options]) + +Dispatches a `mousedown` event on the mouse's current position. Mouse's current position can be changed using [`mouse.move()`](https://grafana.com/docs/k6//javascript-api/k6-browser/mouse/move). + +| Parameter | Type | Description | +| ------------------ | ------ | -------------------------------------------------------------------------------------------------------- | +| options | object | Optional. | +| options.button | string | The mouse button to click. Possible values are `'left'`, `'right'`, and `'middle'`. Default is `'left'`. | +| options.clickCount | number | The number of times to click. Default is `1`. | + +### Returns + +| Type | Description | +| --------------- | --------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the mouse down action is complete. | \ No newline at end of file diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/move.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/move.md new file mode 100644 index 000000000..dc2c2b1c5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/move.md @@ -0,0 +1,21 @@ +--- +title: 'move(x, y[, options])' +description: 'Mouse moves to the `x` and `y` coordinates.' +--- + +# move(x, y[, options]) + +Mouse moves to the `x` and `y` coordinates. This is useful for simulating mouse movement. The `step` option is the number of steps the mouse will take to move from the current position to the target position. The higher the step count, the smoother the movement will be. + +| Parameter | Type | Description | +| ------------- | ------ | -------------------------------------------------------------- | +| x | number | The x-coordinate to move to. | +| y | number | The y-coordinate to move to. | +| options | object | Optional. | +| options.steps | number | The number of steps to take to move the mouse. Default is `1`. | + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the mouse up action is complete. | \ No newline at end of file diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/up.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/up.md new file mode 100644 index 000000000..2ccb8c8a8 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/mouse/up.md @@ -0,0 +1,20 @@ +--- +title: 'up([options])' +description: 'Mouse dispatches a `mouseup` event on the current position of the mouse.' +--- + +# up([options]) + +Dispatches a `mouseup` event on the mouse's current position. Mouse's current position can be changed using [`mouse.move()`](https://grafana.com/docs/k6//javascript-api/k6-browser/mouse/move). + +| Parameter | Type | Description | +| ------------------ | ------ | -------------------------------------------------------------------------------------------------------- | +| options | object | Optional. | +| options.button | string | The mouse button to click. Possible values are `'left'`, `'right'`, and `'middle'`. Default is `'left'`. | +| options.clickCount | number | The number of times to click. Default is `1`. | + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the mouse up action is complete. | \ No newline at end of file diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/newcontext.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/newcontext.md new file mode 100644 index 000000000..1a298aba5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/newcontext.md @@ -0,0 +1,95 @@ +--- +title: 'newContext([options])' +description: 'Browser module: newContext method' +--- + +# newContext([options]) + +Creates and returns a new [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/), if one hasn't already been initialized for the [Browser](https://grafana.com/docs/k6//javascript-api/k6-browser). If one has already been initialized an error is thrown. + +{{< admonition type="note" >}} + +A 1-to-1 mapping between [Browser](https://grafana.com/docs/k6//javascript-api/k6-browser) and `BrowserContext` means you cannot run `BrowserContexts` concurrently. Due to this restriction, if one already exists, it must be [close](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/close)d first before creating a new one. + +{{< /admonition >}} + + + +| Parameter | Type | Default | Description | +| ----------------------------------------------------- | ------- | -------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.bypassCSP | boolean | `false` | Whether to bypass a page's Content-Security-Policy. | +| options.colorScheme | string | `'light'` | Whether to display a page in dark or light mode by emulating the 'prefers-colors-scheme' media feature. It can be one of `'light'`, `'dark'`, `'no-preference'`. | +| options.deviceScaleFactor {{< docs/bwipt id="433" >}} | number | `1` | Sets the resolution ratio in physical pixels to the resolution in CSS pixels i.e. if set higher than `1`, then images will look sharper on high pixel density screens. See an [example](#devicescalefactor-example) below. | +| options.extraHTTPHeaders | object | `null` | Contains additional HTTP headers to be sent with every request, where the keys are HTTP headers and values are HTTP header values. | +| options.geolocation {{< docs/bwipt id="435" >}} | object | `null` | Sets the user's geographical location. | +| options.geolocation.latitude | number | `0` | Latitude should be between `-90` and `90`. | +| options.geolocation.longitude | number | `0` | Longitude should be between `-180` and `180`. | +| options.geolocation.accuracy | number | `0` | Accuracy should only be a non-negative number. Defaults to `0`. | +| options.hasTouch {{< docs/bwipt id="436" >}} | boolean | `false` | Whether to simulate a device with touch events. | +| options.httpCredentials | object | `null` | Sets the credentials for HTTP authentication using Basic Auth. | +| options.httpCredentials.username | string | `''` | Username to pass to the web browser for Basic HTTP Authentication. | +| options.httpCredentials.password | string | `''` | Password to pass to the web browser for Basic HTTP Authentication. | +| options.ignoreHTTPSErrors | boolean | `false` | Whether to ignore HTTPS errors that may be caused by invalid certificates. | +| options.isMobile | boolean | `false` | Whether to simulate a mobile device. | +| options.javaScriptEnabled | boolean | `true` | Whether to activate JavaScript support for the context. | +| options.locale | string | system | Specifies the user's locale, such as `'en-US'`, `'tr-TR'`, etc. | +| options.offline | boolean | `false` | Whether to emulate an offline network. | +| options.permissions | Array | `null` | Permissions to grant for the context's pages. See [browserContext.grantPermissions()](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/grantpermissions) for the options. | +| options.reducedMotion | string | `'no-preference'` | Minimizes the amount of motion by emulating the 'prefers-reduced-motion' media feature. It can be one of `'reduce'` and `'no-preference'`. See [page.emulateMedia()](https://grafana.com/docs/k6//javascript-api/k6-browser/page/emulatemedia) for the options. | +| options.screen | object | `{'width': 1280, 'height': 720}` | Sets a window screen size for all pages in the context. It can only be used when the viewport is set. | +| options.screen.width | number | `1280` | Page width in pixels. | +| options.screen.height | number | `720` | Page height in pixels. | +| options.timezoneID | string | system | Changes the context's timezone. See [ICU's metaZones.txt](https://cs.chromium.org/chromium/src/third_party/icu/source/data/misc/metaZones.txt?rcl=faee8bc70570192d82d2978a71e2a615788597d1) for a list of supported timezone IDs. | +| options.userAgent | string | browser | Specifies the user agent to use in the context. | +| options.viewport | object | `{'width': 1280, 'height': 720}` | Sets a viewport size for all pages in the context. `null` disables the default viewport. | +| options.viewport.width | number | `1280` | Page width in pixels. | +| options.viewport.height | number | `720` | Page height in pixels. | + + + +### Returns + +| Type | Description | +| ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with a [`BrowserContext`](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext) object. | + +### deviceScaleFactor example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext({ + viewport: { + width: 375, + height: 812, + }, + deviceScaleFactor: 3, + }); + const page = await context.newPage(); + + try { + await page.goto('https://test.k6.io/'); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/newpage.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/newpage.md new file mode 100644 index 000000000..ac6927760 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/newpage.md @@ -0,0 +1,100 @@ +--- +title: 'newPage([options])' +description: 'Browser module: newPage method' +--- + +# newPage([options]) + +Creates and returns a new [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/) in a new [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) if a `BrowserContext` hasn't already been initialized for the [Browser](https://grafana.com/docs/k6//javascript-api/k6-browser). If a `BrowserContext` has already been initialized an error is thrown. + +{{< admonition type="note" >}} + +A 1-to-1 mapping between [Browser](https://grafana.com/docs/k6//javascript-api/k6-browser) and `BrowserContext` means you cannot run `BrowserContexts` concurrently. Due to this restriction, if one already exists, it must be [retrieved](https://grafana.com/docs/k6//javascript-api/k6-browser/context) and [close](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/close)d first before creating a new one. + +{{< /admonition >}} + +{{< admonition type="caution" >}} + +Pages that have been opened ought to be closed using [`Page.close`](https://grafana.com/docs/k6//javascript-api/k6-browser/page/close/). Pages left open could potentially distort the results of Web Vital metrics. + +{{< /admonition >}} + + + +| Parameter | Type | Default | Description | +| ----------------------------------------------------- | ------- | -------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.bypassCSP | boolean | `false` | Whether to bypass a page's Content-Security-Policy. | +| options.colorScheme | string | `'light'` | Whether to display a page in dark or light mode by emulating the 'prefers-colors-scheme' media feature. It can be one of `'light'`, `'dark'`, `'no-preference'`. | +| options.deviceScaleFactor {{< docs/bwipt id="433" >}} | number | `1` | Sets the resolution ratio in physical pixels to the resolution in CSS pixels i.e. if set higher than `1`, then images will look sharper on high pixel density screens. See an [example](#devicescalefactor-example) below. | +| options.extraHTTPHeaders | object | `null` | Contains additional HTTP headers to be sent with every request, where the keys are HTTP headers and values are HTTP header values. | +| options.geolocation {{< docs/bwipt id="435" >}} | object | `null` | Sets the user's geographical location. | +| options.geolocation.latitude | number | `0` | Latitude should be between `-90` and `90`. | +| options.geolocation.longitude | number | `0` | Longitude should be between `-180` and `180`. | +| options.geolocation.accuracy | number | `0` | Accuracy should only be a non-negative number. Defaults to `0`. | +| options.hasTouch {{< docs/bwipt id="436" >}} | boolean | `false` | Whether to simulate a device with touch events. | +| options.httpCredentials | object | `null` | Sets the credentials for HTTP authentication using Basic Auth. | +| options.httpCredentials.username | string | `''` | Username to pass to the web browser for Basic HTTP Authentication. | +| options.httpCredentials.password | string | `''` | Password to pass to the web browser for Basic HTTP Authentication. | +| options.ignoreHTTPSErrors | boolean | `false` | Whether to ignore HTTPS errors that may be caused by invalid certificates. | +| options.isMobile | boolean | `false` | Whether to simulate a mobile device. | +| options.javaScriptEnabled | boolean | `true` | Whether to activate JavaScript support for the context. | +| options.locale | string | system | Specifies the user's locale, such as `'en-US'`, `'tr-TR'`, etc. | +| options.offline | boolean | `false` | Whether to emulate an offline network. | +| options.permissions | Array | `null` | Permissions to grant for the context's pages. See [browserContext.grantPermissions()](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/grantpermissions) for the options. | +| options.reducedMotion | string | `'no-preference'` | Minimizes the amount of motion by emulating the 'prefers-reduced-motion' media feature. It can be one of `'reduce'` and `'no-preference'`. See [page.emulateMedia()](https://grafana.com/docs/k6//javascript-api/k6-browser/page/emulatemedia) for the options. | +| options.screen | object | `{'width': 1280, 'height': 720}` | Sets a window screen size for all pages in the context. It can only be used when the viewport is set. | +| options.screen.width | number | `1280` | Page width in pixels. | +| options.screen.height | number | `720` | Page height in pixels. | +| options.timezoneID | string | system | Changes the context's timezone. See [ICU's metaZones.txt](https://cs.chromium.org/chromium/src/third_party/icu/source/data/misc/metaZones.txt?rcl=faee8bc70570192d82d2978a71e2a615788597d1) for a list of supported timezone IDs. | +| options.userAgent | string | browser | Specifies the user agent to use in the context. | +| options.viewport | object | `{'width': 1280, 'height': 720}` | Sets a viewport size for all pages in the context. `null` disables the default viewport. | +| options.viewport.width | number | `1280` | Page width in pixels. | +| options.viewport.height | number | `720` | Page height in pixels. | + + + +### Returns + +| Type | Description | +| --------------- | -------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with a [`Page`](https://grafana.com/docs/k6//javascript-api/k6-browser/page/) object. | + +### deviceScaleFactor example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage({ + viewport: { + width: 375, + height: 812, + }, + deviceScaleFactor: 3, + }); + + try { + await page.goto('https://test.k6.io/'); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/_index.md new file mode 100644 index 000000000..3b54fddda --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/_index.md @@ -0,0 +1,75 @@ +--- +title: 'Page' +description: 'Browser module: Page Class' +weight: 10 +--- + +# Page + +Page provides methods to interact with a single tab in a running web browser. A single [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext) can have many `pages`. + +| Method | Description | +| ----------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [bringToFront()](https://grafana.com/docs/k6//javascript-api/k6-browser/page/bringtofront) | Activates a browser tab. | +| [check(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/check/) | Select the input checkbox. | +| [click(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/click/) | Clicks on an element matching a `selector`. | +| [close()](https://grafana.com/docs/k6//javascript-api/k6-browser/page/close) | Closes a tab that the `page` is associated with. | +| [content()](https://grafana.com/docs/k6//javascript-api/k6-browser/page/content) | Gets the HTML contents of the page. | +| [context()](https://grafana.com/docs/k6//javascript-api/k6-browser/page/context) | Gets the [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext) that the page belongs to. | +| [dblclick(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/dblclick/) | With the [Mouse](https://grafana.com/docs/k6//javascript-api/k6-browser/mouse), double click on an element matching the provided `selector`. | +| [dispatchEvent(selector, type, eventInit[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/dispatchevent/) | Dispatches HTML DOM event types e.g. `'click'` | +| [page$(selector)](https://grafana.com/docs/k6//javascript-api/k6-browser/page/page-dollar) | Finds an element matching the specified `selector` within the page. | +| [page$$(selector)](https://grafana.com/docs/k6//javascript-api/k6-browser/page/page-doubledollar) | Finds all elements matching the specified `selector` within the page. | +| [emulateMedia([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/emulatemedia/) | Changes the CSS media type and the color scheme feature. | +| [emulateVisionDeficiency(type)](https://grafana.com/docs/k6//javascript-api/k6-browser/page/emulatevisiondeficiency) | Emulates your website with the specified vision deficiency `type`. | +| [evaluate(pageFunction[, arg])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/evaluate/) | Returns the value of the `pageFunction` invocation. | +| [evaluateHandle(pageFunction[, arg])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/evaluatehandle/) | Returns the value of the `pageFunction` invocation as a [JSHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/jshandle). | +| [fill(selector, value[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/fill/) | Fill an `input`, `textarea` or `contenteditable` element with the provided value. | +| [focus(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/focus/) | Fetches an element with `selector` and focuses on it. | +| [frames()](https://grafana.com/docs/k6//javascript-api/k6-browser/page/frames) | Returns an array of frames on the page. | +| [getAttribute(selector, name[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/getattribute/) | Returns the element attribute value for the given attribute name. | +| [goto(url[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/goto/) | Navigates to the specified `url`. | +| [hover(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/hover/) | Hovers over an element matching `selector`. | +| [innerHTML(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/innerhtml/) | Returns the `element.innerHTML`. | +| [innerText(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/innertext/) | Returns the `element.innerText`. | +| [inputValue(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/inputvalue/) | Returns `input.value` for the selected `input`, `textarea` or `select` element. | +| [isChecked(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/ischecked/) | Checks to see if the `checkbox` `input` type is selected or not. | +| [isClosed()](https://grafana.com/docs/k6//javascript-api/k6-browser/page/isclosed) {{< docs/bwipt id="878" >}} | Checks if the page has been closed. | +| [isDisabled(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/isdisabled/) | Checks if the element is `disabled`. | +| [isEditable(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/iseditable/) | Checks if the element is `editable`. | +| [isEnabled(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/isenabled/) | Checks if the element is `enabled`. | +| [isHidden(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/ishidden/) | Checks if the element is `hidden`. | +| [isVisible(selector[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/isvisible/) | Checks if the element is `visible`. | +| [keyboard](https://grafana.com/docs/k6//javascript-api/k6-browser/page/keyboard) | Returns the [Keyboard](https://grafana.com/docs/k6//javascript-api/k6-browser/keyboard) instance to interact with a virtual keyboard on the page. | +| [locator(selector)](https://grafana.com/docs/k6//javascript-api/k6-browser/page/locator) | Returns a [Locator](https://grafana.com/docs/k6//javascript-api/k6-browser/locator) for the given `selector`. | +| [mainFrame()](https://grafana.com/docs/k6//javascript-api/k6-browser/page/mainframe) | Returns the page's main [Frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame). | +| [mouse](https://grafana.com/docs/k6//javascript-api/k6-browser/page/mouse) | Returns the [Mouse](https://grafana.com/docs/k6//javascript-api/k6-browser/mouse) instance to interact with a virtual mouse on the page. | +| [on(event, handler)](https://grafana.com/docs/k6//javascript-api/k6-browser/page/on) | Registers a handler to be called whenever the specified event occurs. | +| [opener()](https://grafana.com/docs/k6//javascript-api/k6-browser/page/opener) | Returns the `page` that opened the current `page`. | +| [press(selector, key[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/press/) | Focuses the element, and then presses the given `key` on the [Keyboard](https://grafana.com/docs/k6//javascript-api/k6-browser/keyboard). | +| [reload([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/reload/) | Reloads the current page. | +| [screenshot([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/screenshot/) | Returns a buffer with the captured screenshot from the web browser. | +| [selectOption(selector, values[, options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/selectoption/) | Selects one or more options which match the values from a `` element. + + + +| Parameter | Type | Default | Description | +| ------------------- | ---------------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | `''` | A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. | +| values | string or string[] or object | `''` | If the `select` has the multiple attribute, all matching options are selected, otherwise only the first option matching one of the passed options is selected. Object can be made up of keys with `value`, `label` or `index`. | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.strict | boolean | `false` | When `true`, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| ------------------- | -------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the list of the selected options. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + await page.selectOption('#numbers-options', 'three'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setchecked.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setchecked.md new file mode 100644 index 000000000..490ec0a71 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setchecked.md @@ -0,0 +1,69 @@ +--- +title: 'setChecked(selector, checked[, options])' +description: 'Browser module: page.setChecked method' +--- + +# setChecked(selector, checked[, options]) + +{{< admonition type="warning" >}} + +Use [`locator.setChecked(checked [, options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/setchecked/) instead. + +{{< /admonition >}} + +Use this method to select or unselect an `input` or `radio` checkbox. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | Optional parameters. | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | +| options.strict | boolean | `false` | When `true`, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception. | + + + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the set check action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + + await page.setChecked('#checkbox1', true); + await page.setChecked('#checkbox1', false); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setcontent.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setcontent.md new file mode 100644 index 000000000..e8d08129a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setcontent.md @@ -0,0 +1,64 @@ +--- +title: 'setContent(html[, options])' +description: 'Browser module: page.setContent(html[, options]) method' +--- + +# setContent(html[, options]) + + + +Sets the supplied HTML string to the current page. + + + +| Parameter | Type | Default | Description | +| ----------------- | ------ | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| html | string | `''` | HTML markup to assign to the page. | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum operation time in milliseconds. Pass `0` to disable the timeout. The default value can be changed via the [browserContext.setDefaultNavigationTimeout(timeout)](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/setdefaultnavigationtimeout/), [browserContext.setDefaultTimeout(timeout)](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/setdefaulttimeout/), [page.setDefaultNavigationTimeout(timeout)](https://grafana.com/docs/k6//javascript-api/k6-browser/page/setdefaultnavigationtimeout/) or [page.setDefaultTimeout(timeout)](https://grafana.com/docs/k6//javascript-api/k6-browser/page/setdefaulttimeout/) methods. Setting the value to `0` will disable the timeout. | +| options.waitUntil | string | `load` | When to consider operation to have succeeded. See Events for more details. | + + + +### Returns + +| Type | Description | +| --------------- | -------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the page has been set with the supplied HTML. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + const htmlContent = ` + + + Test + Test + + `; + + await page.setContent(htmlContent); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setdefaultnavigationtimeout.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setdefaultnavigationtimeout.md new file mode 100644 index 000000000..b2a4d8ea2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setdefaultnavigationtimeout.md @@ -0,0 +1,47 @@ +--- +title: 'setDefaultNavigationTimeout(timeout)' +description: 'Browser module: page.setDefaultNavigationTimeout(timeout) method' +--- + +# setDefaultNavigationTimeout(timeout) + +This setting will change the navigation timeout for the following methods: + +- [page.goto(url, [options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/goto/) +- [page.reload([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/reload/) +- [page.setContent(html, [options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/setcontent/) +- [page.waitForNavigation([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/page/waitfornavigation/) + +| Parameter | Type | Default | Description | +| --------- | ------ | ------- | ------------------------ | +| timeout | number | | Timeout in milliseconds. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + page.setDefaultNavigationTimeout(60000); + await page.goto('https://test.k6.io/browser.php'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setdefaulttimeout.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setdefaulttimeout.md new file mode 100644 index 000000000..9b1be598c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setdefaulttimeout.md @@ -0,0 +1,42 @@ +--- +title: 'setDefaultTimeout(timeout)' +description: 'Browser module: page.setDefaultTimeout(timeout) method' +--- + +# setDefaultTimeout(timeout) + +This setting will change the timeout for all the methods accepting a `timeout` option. + +| Parameter | Type | Default | Description | +| --------- | ------ | ------- | ------------------------ | +| timeout | number | | Timeout in milliseconds. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + page.setDefaultTimeout(60000); + await page.goto('https://test.k6.io/browser.php'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setextrahttpheaders.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setextrahttpheaders.md new file mode 100644 index 000000000..0adc09a28 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setextrahttpheaders.md @@ -0,0 +1,50 @@ +--- +title: 'setExtraHTTPHeaders(headers)' +description: 'Browser module: page.setExtraHTTPHeaders(headers) method' +--- + +# setExtraHTTPHeaders(headers) + +This sets extra HTTP headers which will be sent with subsequent HTTP requests. + +| Parameter | Type | Default | Description | +| --------- | ---------------------- | ------- | ------------------------------------------------------------------------------------ | +| headers | Object | | An object containing the additional HTTP headers. All header values must be strings. | + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the headers have been set. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.setExtraHTTPHeaders({ foo: 'bar' }); + const url = await page.goto('https://test.k6.io/browser.php'); + + console.log(url.request().headers().foo); // prints bar +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setinputfiles.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setinputfiles.md new file mode 100644 index 000000000..543f724ca --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setinputfiles.md @@ -0,0 +1,157 @@ +--- +title: 'setInputFiles(selector, file[, options])' +description: 'Sets the file input element' +--- + +# setInputFiles(selector, file[, options]) + +Sets the file input element's value to the specified files. + +To work with local files on the file system, use the [experimental fs module](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/fs/) to load and read the file contents. + +| Parameter | Type | Default | Description | +| ------------------- | ----------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | `''` | A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. | +| file | object | `null` | This is a required parameter. | +| file.name | string | `''` | The name of the file. For example, `file.txt`. | +| file.mimeType | string | `''` | The type of the file content. For example, `text/plain`. | +| file.buffer | ArrayBuffer | `[]` | Base64 encoded content of the file. | +| options | object | `null` | This is an optional parameter. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + +### Returns + +| Type | Description | +| --------------- | --------------------------------------------------- | +| `Promise` | A Promise that fulfills when the file has been set. | + +### Inline File Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import encoding from 'k6/encoding'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + // In this example we create a simple web page with an upload input field. + // Usually, you would use page.goto to navigate to a page with a file input field. + await page.setContent(` + + + + +
+ + +
+ + `); + + // The file is set to the input element with the id "upload". + await page.setInputFiles('input[id="upload"]', { + name: 'file.txt', + mimetype: 'text/plain', + buffer: encoding.b64encode('hello world'), + }); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} + +### Local File Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import encoding from 'k6/encoding'; +import { open } from 'k6/experimental/fs'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +// Declare the location of the file on the local filesystem. +let file; +(async function () { + file = await open('/abs/path/to/file.txt'); +})(); + +export default async function () { + const page = await browser.newPage(); + + try { + // In this example we create a simple web page with an upload input field. + // Usually, you would use page.goto to navigate to a page with a file input field. + page.setContent(` + + + + +
+ + +
+ + `); + + // Read the whole file content into a buffer. + const buffer = await readAll(file); + + // The file is set to the input element with the id "upload". + await page.setInputFiles({ + name: 'file.txt', + mimetype: 'text/plain', + buffer: encoding.b64encode(buffer), + }); + } finally { + await page.close(); + } +} + +// readAll will read the whole of the file from the local filesystem into a +// buffer. +async function readAll(file) { + const fileInfo = await file.stat(); + const buffer = new Uint8Array(fileInfo.size); + + const bytesRead = await file.read(buffer); + if (bytesRead !== fileInfo.size) { + throw new Error('unexpected number of bytes read'); + } + + return buffer; +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setviewportsize.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setviewportsize.md new file mode 100644 index 000000000..2c8b0861c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/setviewportsize.md @@ -0,0 +1,57 @@ +--- +title: 'setViewportSize(viewportSize)' +description: 'Browser module: page.setViewportSize(viewportSize) method' +--- + +# setViewportSize(viewportSize) + +This will update the page's width and height. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------ | ------- | ------------------------------------------------------------------------------------ | +| viewportSize | Object | | An object containing the additional HTTP headers. All header values must be strings. | +| viewportSize.width | number | | Page width in pixels. | +| viewportSize.height | number | | Page height in pixels. | + + + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------ | +| `Promise` | A Promise that fulfills when the viewport size is set. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.setViewportSize({ + width: 640, + height: 480, + }); + await page.goto('https://test.k6.io/browser.php'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/tap.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/tap.md new file mode 100644 index 000000000..124021a31 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/tap.md @@ -0,0 +1,68 @@ +--- +title: 'tap(selector[, options])' +description: 'Browser module: locator.tap(selector[, options]) method' +--- + +# tap(selector[, options]) + +{{< admonition type="warning" >}} + +Use locator-based [`locator.tap([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/tap/) instead. + +{{< /admonition >}} + +Tap the first element that matches the selector. + + + +| Parameter | Type | Default | Description | +| ------------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | `''` | A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.modifiers | string[] | `null` | `Alt`, `Control`, `Meta` or `Shift` modifiers keys pressed during the action. If not specified, currently pressed modifiers are used. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.strict | boolean | `false` | When `true`, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | -------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the tap action is finished. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + await page.tap('#numbers-options'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/textcontent.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/textcontent.md new file mode 100644 index 000000000..f2bc72bff --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/textcontent.md @@ -0,0 +1,61 @@ +--- +title: 'textContent(selector[, options])' +description: 'Browser module: locator.textContent(selector[, options]) method' +--- + +# textContent(selector[, options]) + +{{< admonition type="warning" >}} + +Use locator-based [`locator.textContent([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/textcontent/) instead. + +{{< /admonition >}} + +Returns the `element.textContent`. + + + +| Parameter | Type | Default | Description | +| --------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | `''` | A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. | +| options | object | `null` | | +| options.strict | boolean | `false` | When `true`, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| ------------------------- | ------------------------------------------------------------------------ | +| `Promise` | A Promise that fulfills with the text content of the selector or `null`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + console.log(await page.textContent('#checkbox1')); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/throttlecpu.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/throttlecpu.md new file mode 100644 index 000000000..fde95f9c9 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/throttlecpu.md @@ -0,0 +1,55 @@ +--- +title: 'throttleCPU(cpuProfile)' +description: 'Browser module: page.throttleCPU(cpuProfile) method' +--- + +# throttleCPU(cpuProfile) + +Throttles the CPU in Chrome/Chromium to slow it down by the specified `rate` in `cpuProfile`. + +| Parameter | Type | Default | Description | +| --------------- | ---------- | ------- | -------------------------------------------------------------------- | +| cpuProfile | CPUProfile | `null` | This is a mandatory parameter. | +| cpuProfile.rate | number | `1` | rate as a slowdown factor (1 is no throttle, 2 is 2x slowdown, etc). | + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------------------------------ | +| `Promise` | A Promise that fulfills when the CPU has been throttled to the specified rate. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext(); + const page = await context.newPage(); + + try { + await page.throttleCPU({ rate: 4 }); + + await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' }); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/throttlenetwork.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/throttlenetwork.md new file mode 100644 index 000000000..7ee2304bc --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/throttlenetwork.md @@ -0,0 +1,65 @@ +--- +title: 'throttleNetwork(networkProfile)' +description: 'Browser module: page.throttleNetwork(networkProfile) method' +--- + +# throttleNetwork(networkProfile) + +Throttles the network in Chrome/Chromium to slow it down by the specified fields in the `networkProfile` object. + +| Parameter | Type | Default | Description | +| ----------------------- | -------------- | ------- | -------------------------------------------------------------------------------------- | +| networkProfile | NetworkProfile | `null` | This is a mandatory parameter. | +| networkProfile.latency | number | `0` | Minimum latency from request sent to response headers received (ms). | +| networkProfile.download | number | `-1` | Maximal aggregated download throughput (bytes/sec). `-1` disables download throttling. | +| networkProfile.upload | number | `-1` | Maximal aggregated upload throughput (bytes/sec). `-1` disables upload throttling. | + +To work with the most commonly tested network profiles, import `networkProfiles` from the browser module. There are three profiles available: + +| Name | Notes | +| ----------------- | ------------------------------------------------------------------------------------------------------------------------------ | +| `'No Throttling'` | No throttling, which is the default before applying any network throttling. This can be used to remove the network throttling. | +| `'Fast 3G'` | Emulates a typical fast 3G connection | +| `'Slow 3G'` | Emulates a typical slow 3G connection | + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the network has been throttled to the specified rate. | + +### Example + +{{< code >}} + +```javascript +import { browser, networkProfiles } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = await browser.newContext(); + const page = await context.newPage(); + + try { + await page.throttleNetwork(networkProfiles['Slow 3G']); + + await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' }); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/title.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/title.md new file mode 100644 index 000000000..7e9278255 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/title.md @@ -0,0 +1,44 @@ +--- +title: 'title()' +description: 'Browser module: page.title method' +--- + +# title() + +Returns the page's title. + +### Returns + +| Type | Description | +| ----------------- | ---------------------------------------------- | +| `Promise` | A Promise that fulfills with the page's title. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + console.log(await page.title()); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/touchscreen.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/touchscreen.md new file mode 100644 index 000000000..719227725 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/touchscreen.md @@ -0,0 +1,14 @@ +--- +title: 'touchscreen' +description: 'Browser module: page.touchscreen method' +--- + +# touchscreen + +Returns the [Touchscreen](https://grafana.com/docs/k6//javascript-api/k6-browser/touchscreen/) instance to interact with a virtual touchscreen on the page. + +### Returns + +| Type | Description | +| ----------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- | +| [Touchscreen](https://grafana.com/docs/k6//javascript-api/k6-browser/touchscreen/) | The `Touchscreen` instance associated with the page. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/type.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/type.md new file mode 100644 index 000000000..e9a03e5a4 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/type.md @@ -0,0 +1,64 @@ +--- +title: 'type(selector, text[, options])' +description: 'Browser module: page.type(selector, text[, options]) method' +--- + +# type(selector, text[, options]) + +{{< admonition type="warning" >}} + +Use locator-based [`locator.type()`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/type/) instead. + +{{< /admonition >}} + +Type the `text` in the first element found that matches the selector. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | `''` | A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. | +| text | string | `''` | A text to type into a focused element. | +| options | object | `null` | | +| options.delay | number | `0` | Milliseconds to wait between key presses. Defaults to `0`. | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.strict | boolean | `false` | When `true`, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| --------------- | ----------------------------------------------------- | +| `Promise` | A Promise that fulfills when the text has been typed. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + await page.type('#text1', 'hello world!'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/uncheck.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/uncheck.md new file mode 100644 index 000000000..04582410a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/uncheck.md @@ -0,0 +1,68 @@ +--- +title: 'uncheck(selector[, options])' +description: 'Browser module: page.uncheck(selector[, options]) method' +--- + +# uncheck(selector[, options]) + +{{< admonition type="warning" >}} + +Use locator-based [`locator.uncheck([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/uncheck/) instead. + +{{< /admonition >}} + +This method is used to unselect an input checkbox. + + + +| Parameter | Type | Default | Description | +| ------------------- | ------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | `''` | A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. | +| options | object | `null` | | +| options.force | boolean | `false` | Setting this to `true` will bypass the actionability checks (`visible`, `stable`, `enabled`). | +| options.noWaitAfter | boolean | `false` | If set to `true` and a navigation occurs from performing this action, it will not wait for it to complete. | +| options.position | object | `null` | A point to use relative to the top left corner of the element. If not supplied, a visible point of the element is used. | +| options.position.x | number | `0` | The x coordinate. | +| options.position.y | number | `0` | The y coordinate. | +| options.strict | boolean | `false` | When `true`, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.trial | boolean | `false` | Setting this to `true` will perform the actionability checks without performing the action. | + + + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the checkbox is unchecked. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + await page.check('#checkbox1'); + await page.uncheck('#checkbox1'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/url.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/url.md new file mode 100644 index 000000000..79b7eb450 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/url.md @@ -0,0 +1,44 @@ +--- +title: 'url()' +description: 'Browser module: page.url method' +--- + +# url() + +Returns the page's URL. + +### Returns + +| Type | Description | +| ------ | --------------- | +| string | The page's URL. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + console.log(page.url()); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/viewportsize.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/viewportsize.md new file mode 100644 index 000000000..88badbf07 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/viewportsize.md @@ -0,0 +1,49 @@ +--- +title: 'viewportSize()' +description: 'Browser module: page.viewportSize method' +--- + +# viewportSize() + +Returns the page's size (width and height). + +### Returns + +| Type | Description | +| ------ | ------------------------------------------------- | +| Object | An object containing the page's width and height. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.setViewportSize({ + width: 640, + height: 480, + }); + await page.goto('https://test.k6.io/browser.php'); + + console.log(page.viewportSize()); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitforfunction.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitforfunction.md new file mode 100644 index 000000000..9f35f6c1c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitforfunction.md @@ -0,0 +1,77 @@ +--- +title: 'waitForFunction(pageFunction, arg[, options])' +description: 'Browser module: page.waitForFunction(pageFunction, arg[, options]) method' +--- + +# waitForFunction(pageFunction, arg[, options]) + +Returns when the `pageFunction` returns a truthy value. + + + +| Parameter | Type | Default | Description | +| --------------- | --------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| pageFunction | function | | Function to be evaluated in the page context. | +| arg | string | `''` | Optional argument to pass to `pageFunction` | +| options | object | `null` | | +| options.polling | number or `raf` | `raf` | If `polling` is `'raf'`, then `pageFunction` is constantly executed in `requestAnimationFrame` callback. If `polling` is a number, then it is treated as an interval in milliseconds at which the function would be executed. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------- | +| Promise<[JSHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/jshandle/)> | The `JSHandle` instance associated with the page. | + +### Example + +{{< code >}} + + + +```javascript +import { browser } from 'k6/browser'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.evaluate(() => { + setTimeout(() => { + const el = document.createElement('h1'); + el.innerHTML = 'Hello'; + document.body.appendChild(el); + }, 1000); + }); + + const ok = await page.waitForFunction("document.querySelector('h1')", { + polling: 'mutation', + timeout: 2000, + }); + await check(ok, { + 'waitForFunction successfully resolved': async (ok) => + await ok.innerHTML() == 'Hello' + }); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitforloadstate.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitforloadstate.md new file mode 100644 index 000000000..1236843b2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitforloadstate.md @@ -0,0 +1,90 @@ +--- +title: 'waitForLoadState(state[, options])' +description: 'Browser module: page.waitForLoadState(state[, options]) method' +--- + +# waitForLoadState(state[, options]) + +{{< admonition type="caution" >}} + +This method has **known issues**. For details, refer to [#880](https://github.com/grafana/xk6-browser/issues/880). + +{{< /admonition >}} + +This waits for the given load state to be reached. It will immediately unblock if that lifecycle event has already been received. + + + +| Parameter | Type | Default | Description | +| --------------- | ------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| state | string | `load` | Optional load state to wait for. See [Events](#events) for more details. | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Events + +{{< admonition type="caution" >}} + +`networkidle` is DISCOURAGED. Don't use this method for testing especially with chatty websites where the event may never fire, rely on web assertions to assess readiness instead. + +{{< /admonition >}} + +Events can be either: + +- `'domcontentloaded'` - consider operation to be finished when the `DOMContentLoaded` event is fired. +- `'load'` - consider operation to be finished when the `load` event is fired. +- `'networkidle'` - Consider operation to be finished when there are no network connections for at least `500` ms. + +### Returns + +| Type | Description | +| --------------- | ------------------------------------------------------- | +| `Promise` | A Promise that fulfills when the load state is reached. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + let page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/my_messages.php'); + + await page.locator('input[name="login"]').type('admin'); + await page.locator('input[name="password"]').type('123'); + + const submitButton = page.locator('input[type="submit"]'); + await submitButton.click(); + + await page.waitForLoadState('networkidle'); // waits until the `networkidle` event + + await check(page.locator('h2'), { + 'header': async h2 => await h2.textContent() == 'Welcome, admin!' + }); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitfornavigation.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitfornavigation.md new file mode 100644 index 000000000..d1702915e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitfornavigation.md @@ -0,0 +1,86 @@ +--- +title: 'waitForNavigation([options])' +description: 'Browser module: page.waitForNavigation([options]) method' +--- + +# waitForNavigation([options]) + +Waits for the given navigation lifecycle event to occur and returns the main resource response. + + + +| Parameter | Type | Default | Description | +| ----------------- | ------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| options | object | `null` | | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | +| options.waitUntil | string | `load` | When to consider operation to have succeeded. See [Events](#events) for more details. | + + + +### Events + +{{< admonition type="caution" >}} + +`networkidle` is DISCOURAGED. Don't use this method for testing especially with chatty websites where the event may never fire, rely on web assertions to assess readiness instead. + +{{< /admonition >}} + +Events can be either: + +- `'domcontentloaded'` - consider operation to be finished when the `DOMContentLoaded` event is fired. +- `'load'` - consider operation to be finished when the `load` event is fired. +- `'networkidle'` - Consider operation to be finished when there are no network connections for at least `500` ms. + +### Returns + +| Type | Description | +| --------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------- | +| Promise/javascript-api/k6-browser/response/)> | The `Response` instance associated with the page. Else, it returns `null` | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/my_messages.php'); + + await page.locator('input[name="login"]').type('admin'); + await page.locator('input[name="password"]').type('123'); + + const submitButton = page.locator('input[type="submit"]'); + + await Promise.all([ + submitButton.click(), + page.waitForNavigation(), + ]); + + await check(page.locator('h2'), { + header: async h2 => await h2.textContent() == 'Welcome, admin!' + }); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitforselector.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitforselector.md new file mode 100644 index 000000000..1cc57d7cf --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitforselector.md @@ -0,0 +1,70 @@ +--- +title: 'waitForSelector(selector[, options])' +description: 'Browser module: page.waitForSelector(selector[, options]) method' +--- + +# waitForSelector(selector[, options]) + +{{< admonition type="note" >}} + +Use web assertions that assert visibility or a locator-based [`locator.waitFor([options])`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/waitfor/) instead. + +{{< /admonition >}} + +Returns when element specified by selector satisfies `state` option. + + + +| Parameter | Type | Default | Description | +| --------------- | ------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | `''` | A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. | +| options | object | `null` | | +| options.state | string | `visible` | Can be either `attached`, `detached`, `visible`, `hidden` See [Element states](#element-states) for more details. | +| options.strict | boolean | `false` | When `true`, the call requires selector to resolve to a single element. If given selector resolves to more than one element, the call throws an exception. | +| options.timeout | number | `30000` | Maximum time in milliseconds. Pass `0` to disable the timeout. Default is overridden by the `setDefaultTimeout` option on [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/) or [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page/). | + + + +### Element states + +Element states can be either: + +- `'attached'` - wait for element to be present in DOM. +- `'detached'` - wait for element to not be present in DOM. +- `'visible'` - wait for element to have non-empty bounding box and no `visibility:hidden`. +- `'hidden'` - wait for element to be either detached from DOM, or have an empty bounding box or `visibility:hidden`. + +### Returns + +| Type | Description | +| -------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | A Promise that fulfills with the [ElementHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/keyboard/) when a matching element is found, or `null` if the element is not found. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + await page.goto('https://test.k6.io/browser.php'); + await page.waitForSelector('#text1'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitfortimeout.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitfortimeout.md new file mode 100644 index 000000000..f3d579125 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/waitfortimeout.md @@ -0,0 +1,50 @@ +--- +title: 'waitForTimeout(timeout)' +description: 'Browser module: waitForTimeout(timeout) method' +--- + +# waitForTimeout(timeout) + +{{< admonition type="note" >}} + +Never wait for timeout in production, use this only for debugging. Tests that wait for time are inherently flaky. Use [`Locator`](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/) actions and web assertions that wait automatically. + +{{< /admonition >}} + +Waits for the given `timeout` in milliseconds. + +### Returns + +| Type | Description | +| --------------- | ---------------------------------------------------- | +| `Promise` | A Promise that fulfills when the timeout is reached. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + await page.waitForTimeout(5000); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/workers.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/workers.md new file mode 100644 index 000000000..2a9e2cfb1 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/page/workers.md @@ -0,0 +1,44 @@ +--- +title: 'workers()' +description: 'Browser module: page.workers method' +--- + +# workers() + +This method returns an array of the dedicated [WebWorkers](https://grafana.com/docs/k6//javascript-api/k6-browser/worker/) associated with the page. + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------------------------- | ----------------------------------------------- | +| [WebWorkers](https://grafana.com/docs/k6//javascript-api/k6-browser/worker/)[] | Array of `WebWorkers` associated with the page. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + console.log(page.workers()); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/_index.md new file mode 100644 index 000000000..abb640c36 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/_index.md @@ -0,0 +1,64 @@ +--- +title: 'Request' +description: 'Browser module: Request Class' +weight: 11 +--- + +# Request + +The request that the browser performs can be retrieved from the [Response](https://grafana.com/docs/k6//javascript-api/k6-browser/response) when a navigation occurs. + +{{< docs/shared source="k6" lookup="browser-module-wip.md" version="" >}} + +## Supported APIs + +| Method | Playwright Relevant Distinctions | +| --------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- | +| [allHeaders()](https://grafana.com/docs/k6//javascript-api/k6-browser/request/allheaders) {{< docs/bwipt id="965" >}} | Returns an object of headers associated to the request including headers added by the browser. | +| [frame()](https://grafana.com/docs/k6//javascript-api/k6-browser/request/frame) | The [Frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/) that initiated the request. | +| [headers()](https://grafana.com/docs/k6//javascript-api/k6-browser/request/headers) | Returns an object of headers associated to the request. | +| [headersArray()](https://grafana.com/docs/k6//javascript-api/k6-browser/request/headersarray) | An array with all the request HTTP headers. | +| [headerValue(name)](https://grafana.com/docs/k6//javascript-api/k6-browser/request/headervalue) | Returns the value of the header matching the name. The name is case insensitive. | +| [isNavigationRequest()](https://grafana.com/docs/k6//javascript-api/k6-browser/request/isnavigationrequest) | Returns a boolean stating whether the request is for a navigation. | +| [method()](https://grafana.com/docs/k6//javascript-api/k6-browser/request/method) | Request's method (GET, POST, etc.). | +| [postData()](https://grafana.com/docs/k6//javascript-api/k6-browser/request/postdata) | Contains the request's post body, if any. | +| [postDataBuffer()](https://grafana.com/docs/k6//javascript-api/k6-browser/request/postdatabuffer) | Request's post body in a binary form, if any. | +| [resourceType()](https://grafana.com/docs/k6//javascript-api/k6-browser/request/resourcetype) | Contains the request's resource type as it was perceived by the rendering engine. | +| [response()](https://grafana.com/docs/k6//javascript-api/k6-browser/request/response) | Returns the matching [Response](https://grafana.com/docs/k6//javascript-api/k6-browser/response) object. | +| [size()](https://grafana.com/docs/k6//javascript-api/k6-browser/request/size) | Unlike Playwright, this method returns an object containing the sizes of request headers and body. | +| [timing()](https://grafana.com/docs/k6//javascript-api/k6-browser/request/timing) | Returns resource timing information for given request. | +| [url()](https://grafana.com/docs/k6//javascript-api/k6-browser/request/url) | URL of the request. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + const req = res.request(); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/allheaders.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/allheaders.md new file mode 100644 index 000000000..c11cc18dc --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/allheaders.md @@ -0,0 +1,57 @@ +--- +title: 'allHeaders()' +description: 'Browser module: Request.allHeaders method' +--- + +# allHeaders() + +{{< admonition type="caution" >}} + +This method has a **known issue**. For details, refer to [#965](https://github.com/grafana/xk6-browser/issues/965). + +{{< /admonition >}} + +An object of key value pairs made up of HTTP headers associated with the request and the ones that the browser adds (such as cookies). All header names are lower-case. + +### Returns + +| Type | Description | +| --------------------------------- | ------------------------------------------------------------------------ | +| `Promise>` | A promise that resolves to an object of key value pairs for each header. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + const req = res.request(); + + const allHeaders = await req.allHeaders(); + console.log(`allHeaders: ${JSON.stringify(allHeaders)}`); // allHeaders: {"user-agent":"Mozilla/5.0...} + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/frame.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/frame.md new file mode 100644 index 000000000..d06d246d7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/frame.md @@ -0,0 +1,51 @@ +--- +title: 'frame()' +description: 'Browser module: Request.frame method' +--- + +# frame() + +The [Frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/) that initiated the request. + +### Returns + +| Type | Description | +| ----------------------------------------------------------------------------------------------- | --------------------------------------- | +| [Frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/) | The `Frame` that initiated the request. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + const req = res.request(); + + const frame = req.frame(); + // ... + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/headers.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/headers.md new file mode 100644 index 000000000..734df8270 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/headers.md @@ -0,0 +1,51 @@ +--- +title: 'headers()' +description: 'Browser module: Request.headers method' +--- + +# headers() + +An object of key value pairs made up of HTTP headers associated with the request. + +### Returns + +| Type | Description | +| ------------------------ | -------------------------------- | +| `Record` | Key value pairs for each header. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + const req = res.request(); + + const headers = req.headers(); + console.log(`headers: ${JSON.stringify(headers)}`); // headers: {"user-agent":"Mozilla/5.0...} + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/headersarray.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/headersarray.md new file mode 100644 index 000000000..2f7c22f57 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/headersarray.md @@ -0,0 +1,51 @@ +--- +title: 'headersArray()' +description: 'Browser module: Request.headersArray method' +--- + +# headersArray() + +An array with all the request HTTP headers. Unlike [request.allHeaders()](https://grafana.com/docs/k6//javascript-api/k6-browser/request/allheaders), header names are not lower-cased. Headers with multiple entries, such as `Set-Cookie`, appear in the array multiple times. + +### Returns + +| Type | Description | +| ------------------------------------------------ | -------------------------------------------------------------------- | +| `Promise` | A promise that resolves to an array of all the request HTTP headers. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + const req = res.request(); + + const headersArray = await req.headersArray(); + console.log(`headersArray: ${JSON.stringify(headersArray)}`); // headersArray: [{"name":"Accept-Language","value"...}] + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/headervalue.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/headervalue.md new file mode 100644 index 000000000..e1a58b4f5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/headervalue.md @@ -0,0 +1,51 @@ +--- +title: 'headerValue(name)' +description: 'Browser module: Request.headerValue method' +--- + +# headerValue(name) + +Returns the value of the header matching the name. The name is case insensitive. + +### Returns + +| Type | Description | +| ------------------------- | --------------------------------------------------------------------------------------- | +| `Promise` | A promise that resolves to the value of the header matching the name, otherwise `null`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + const req = res.request(); + + const headerValue = await req.headerValue('accept-language'); + console.log(`headerValue: ${headerValue}`); // headerValue: en-US + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/isnavigationrequest.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/isnavigationrequest.md new file mode 100644 index 000000000..945aa5b45 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/isnavigationrequest.md @@ -0,0 +1,51 @@ +--- +title: 'isNavigationRequest()' +description: 'Browser module: Request.isNavigationRequest method' +--- + +# isNavigationRequest() + +Returns a boolean stating whether the request is for a navigation. + +### Returns + +| Type | Description | +| ------- | -------------------------------------------------------- | +| boolean | Boolean stating whether the request is for a navigation. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + const req = res.request(); + + const isNavReq = req.isNavigationRequest(); + console.log(`isNavReq: ${isNavReq}`); // isNavReq: true + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/method.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/method.md new file mode 100644 index 000000000..409061aa0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/method.md @@ -0,0 +1,51 @@ +--- +title: 'method()' +description: 'Browser module: Request.method method' +--- + +# method() + +Request's method (GET, POST, etc.). + +### Returns + +| Type | Description | +| ------ | ---------------------- | +| string | Request's method name. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + const req = res.request(); + + const method = req.method(); + console.log(`method: ${method}`); // method: GET + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/postdata.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/postdata.md new file mode 100644 index 000000000..c9e4b8d44 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/postdata.md @@ -0,0 +1,51 @@ +--- +title: 'postData()' +description: 'Browser module: Request.postData method' +--- + +# postData() + +Contains the request's post body, if any. + +### Returns + +| Type | Description | +| -------------- | ----------------------------------------------------- | +| string \| null | Request's post body, otherwise `null` if none exists. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + const req = res.request(); + + const postData = req.postData(); + console.log(`postData: ${postData}`); // postData: null + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/postdatabuffer.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/postdatabuffer.md new file mode 100644 index 000000000..60c4d38d5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/postdatabuffer.md @@ -0,0 +1,51 @@ +--- +title: 'postDataBuffer()' +description: 'Browser module: Request.postDataBuffer method' +--- + +# postDataBuffer() + +Request's post body in a binary form, if any. + +### Returns + +| Type | Description | +| ------------------- | ---------------------------------------------------------------------------- | +| ArrayBuffer \| null | Returns an `ArrayBuffer` with request's post data or `null` if no post data. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + const req = res.request(); + + const postDataBuffer = req.postDataBuffer(); + console.log(`postDataBuffer: ${postDataBuffer}`); // postDataBuffer: null + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/resourcetype.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/resourcetype.md new file mode 100644 index 000000000..5bb7d782e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/resourcetype.md @@ -0,0 +1,51 @@ +--- +title: 'resourceType()' +description: 'Browser module: Request.resourceType method' +--- + +# resourceType() + +Contains the request's resource type as it was perceived by the rendering engine. It will be one of the following: `document`, `stylesheet`, `image`, `media`, `font`, `script`, `texttrack`, `xhr`, `fetch`, `eventsource`, `websocket`, `manifest`, `other`. + +### Returns + +| Type | Description | +| ------ | ------------------- | +| string | Resource type name. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + const req = res.request(); + + const resourceType = req.resourceType(); + console.log(`resourceType: ${resourceType}`); // resourceType: Document + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/response.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/response.md new file mode 100644 index 000000000..6ae9a0e64 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/response.md @@ -0,0 +1,51 @@ +--- +title: 'response()' +description: 'Browser module: Request.response method' +--- + +# response() + +Returns the matching [Response](https://grafana.com/docs/k6//javascript-api/k6-browser/response) object, or `null` if the response was not received due to error. + +### Returns + +| Type | Description | +| --------------------------- | ------------------------------------------------------------------------------- | +| `Promise` | The `Response` object, or `null` if the response was not received due to error. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + const req = res.request(); + + const response = await req.response(); + // ... + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/size.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/size.md new file mode 100644 index 000000000..dd002cebc --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/size.md @@ -0,0 +1,58 @@ +--- +title: 'size()' +description: 'Browser module: Request.size method' +--- + +# size() + +This method returns the size (in bytes) of body and header sections of the [Request](https://grafana.com/docs/k6//javascript-api/k6-browser/request). + +### Returns + +| Type | Description | +| ------------- | ---------------------- | +| Promise | Returns [Size](#size). | + +### Size + +| Property | Type | Description | +| -------- | ------ | ---------------------------------- | +| body | number | Size in bytes of the request body. | +| headers | number | Size in bytes of the headers body. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + const req = res.request(); + + const size = req.size(); + console.log(`size: ${JSON.stringify(size)}`); // size: {"headers":344,"body":0} + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/timing.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/timing.md new file mode 100644 index 000000000..d3b7e2c7c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/timing.md @@ -0,0 +1,65 @@ +--- +title: 'timing()' +description: 'Browser module: Request.timing method' +--- + +# timing() + +Returns resource timing information for the given request. Most of the timing values become available upon the response, `responseEnd` becomes available when request finishes. + +### Returns + +| Type | Description | +| -------------- | ------------------------------------------ | +| ResourceTiming | Returns [ResourceTiming](#resourcetiming). | + +### ResourceTiming + +| Property | Type | Description | +| --------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| statTime | number | Request start time in milliseconds elapsed since January 1, 1970 00:00:00 UTC. | +| domainLookupStart | number | Time immediately before the browser starts the domain name lookup for the resource. The value is given in milliseconds relative to `startTime`, -1 if not available. | +| domainLookupEnd | number | Time immediately after the browser ends the domain name lookup for the resource. The value is given in milliseconds relative to `startTime`, -1 if not available. | +| connectStart | number | Time immediately before the user agent starts establishing the connection to the server to retrieve the resource. The value is given in milliseconds relative to `startTime`, -1 if not available. | +| secureConnectionStart | number | Time immediately before the browser starts the handshake process to secure the current connection. The value is given in milliseconds relative to `startTime`, -1 if not available. | +| connectEnd | number | Time immediately after the user agent establishes the connection to the server to retrieve the resource. The value is given in milliseconds relative to `startTime`, -1 if not available. | +| requestStart | number | Time immediately before the browser starts requesting the resource from the server, cache, or local resource. The value is given in milliseconds relative to `startTime`, -1 if not available. | +| responseStart | number | Time immediately after the browser receives the first byte of the response from the server, cache, or local resource. The value is given in milliseconds relative to `startTime`, -1 if not available. | +| responseEnd | number | Time immediately after the browser receives the last byte of the resource or immediately before the transport connection is closed, whichever comes first. The value is given in milliseconds relative to `startTime`, -1 if not available. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + const req = res.request(); + + const timing = await req.timing(); + console.log(`timing: ${JSON.stringify(timing)}`); // timing: {"startTime":534898988.85297775,...} + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/url.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/url.md new file mode 100644 index 000000000..a6c286909 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/request/url.md @@ -0,0 +1,51 @@ +--- +title: 'url()' +description: 'Browser module: Request.url method' +--- + +# url() + +URL of the request. + +### Returns + +| Type | Description | +| ------ | ------------------- | +| string | URL of the request. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + const req = res.request(); + + const url = await req.url(); + console.log(`url: ${url}`); // url: https://test.k6.io/ + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/_index.md new file mode 100644 index 000000000..9c7bdeca0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/_index.md @@ -0,0 +1,67 @@ +--- +title: 'Response' +description: 'Browser module: Response Class' +weight: 12 +--- + +# Response + +Response represents a response received by the page. + +{{< docs/shared source="k6" lookup="browser-module-wip.md" version="" >}} + +## Supported APIs + +| Method | Description | +| ---------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [allHeaders()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/allheaders) {{< docs/bwipt id="965" >}} | Returns an object of headers associated to the response including headers added by the browser. | +| [body()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/body) | Returns the response body. | +| [frame()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/frame) | The [Frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/) that initiated the request which this response is associated to. | +| [headers()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/headers) | Returns an object of headers associated to the response. | +| [headersArray()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/headersarray) | An array with all the response HTTP headers. | +| [headerValue(name)](https://grafana.com/docs/k6//javascript-api/k6-browser/response/headervalue) | Returns the value of the header matching the name. The name is case insensitive. | +| [headerValues(name)](https://grafana.com/docs/k6//javascript-api/k6-browser/response/headervalues) | Returns all values of the headers matching the name, for example `set-cookie`. The name is case insensitive. | +| [json()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/json) | Returns the JSON representation of response body. | +| [ok()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/ok) | Returns a `boolean` stating whether the response was successful or not. | +| [request()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/request) | Returns the matching [Request](https://grafana.com/docs/k6//javascript-api/k6-browser/request) object. | +| [securityDetails()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/securitydetails) | Returns SSL and other security information. | +| [serverAddr()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/serveraddr) | Returns the IP address and port of the server for this response. | +| [status()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/status) | Contains the status code of the response (e.g., 200 for a success). | +| [statusText()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/statustext) | Contains the status text of the response (e.g. usually an "OK" for a success). | +| [size()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/size) | The size of the response body and the headers. | +| [text()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/text) | Returns the response body as a string. | +| [url()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/url) | URL of the response. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + // Response returned once goto resolves. + const res = await page.goto('https://test.k6.io/'); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/allheaders.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/allheaders.md new file mode 100644 index 000000000..ba5d0283f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/allheaders.md @@ -0,0 +1,56 @@ +--- +title: 'allHeaders()' +description: 'Browser module: Response.allHeaders method' +--- + +# allHeaders() + +{{< admonition type="caution" >}} + +This method has a **known issue**. For details, refer to [#965](https://github.com/grafana/xk6-browser/issues/965). + +{{< /admonition >}} + +An object of key value pairs made up of HTTP headers associated with the response and the ones that the browser adds (such as cookies). All header names are lower-case. + +### Returns + +| Type | Description | +| --------------------------------- | ------------------------------------------------------------------------ | +| `Promise>` | A promise that resolves to an object of key value pairs for each header. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + const allHeaders = await res.allHeaders(); + console.log(`allHeaders: ${JSON.stringify(allHeaders)}`); // allHeaders: {"transfer-encoding":"chunked"...} + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/body.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/body.md new file mode 100644 index 000000000..0842c2363 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/body.md @@ -0,0 +1,50 @@ +--- +title: 'body()' +description: 'Browser module: Response.body method' +--- + +# allHeaders() + +Returns the response body. + +### Returns + +| Type | Description | +| ---------------------- | ------------------------------------------------------------- | +| `Promise` | A promise that resolves to the buffer with the response body. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + const body = await res.body(); + // Refer to https://github.com/grafana/k6/issues/2440 on decoding the body to a string. + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/frame.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/frame.md new file mode 100644 index 000000000..85fe7d0e1 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/frame.md @@ -0,0 +1,50 @@ +--- +title: 'frame()' +description: 'Browser module: Response.frame method' +--- + +# frame() + +The [Frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/) that initiated the request which this response is associated to. + +### Returns + +| Type | Description | +| ----------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | +| [Frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame/) | The `Frame` that initiated the request which this response is associated to. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + const frame = res.frame(); + // ... + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headers.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headers.md new file mode 100644 index 000000000..b1dff2911 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headers.md @@ -0,0 +1,50 @@ +--- +title: 'headers()' +description: 'Browser module: Response.headers method' +--- + +# headers() + +An object of key value pairs made up of HTTP headers associated with the response. + +### Returns + +| Type | Description | +| ------------------------ | -------------------------------- | +| `Record` | Key value pairs for each header. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + const headers = res.headers(); + console.log(`headers: ${JSON.stringify(headers)}`); // headers: {"Content-Type":"text/html;... + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headersarray.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headersarray.md new file mode 100644 index 000000000..7bcd47e60 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headersarray.md @@ -0,0 +1,50 @@ +--- +title: 'headersArray()' +description: 'Browser module: Response.headersArray method' +--- + +# headersArray() + +An array with all the response HTTP headers. Unlike [response.allHeaders()](https://grafana.com/docs/k6//javascript-api/k6-browser/response/allheaders), header names are not lower-cased. Headers with multiple entries, such as `Set-Cookie`, appear in the array multiple times. + +### Returns + +| Type | Description | +| ------------------------------------------------ | --------------------------------------------------------------------- | +| `Promise` | A promise that resolves to an array of all the response HTTP headers. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + const headersArray = await res.headersArray(); + console.log(`headersArray: ${JSON.stringify(headersArray)}`); // headersArray: [{"name":"Transfer-Encoding"...}] + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headervalue.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headervalue.md new file mode 100644 index 000000000..82a63a446 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headervalue.md @@ -0,0 +1,58 @@ +--- +title: 'headerValue(name)' +description: 'Browser module: Response.headerValue method' +--- + +# headerValue(name) + +Returns the value of the header matching the name. The name is case insensitive. + + + +| Parameter | Type | Description | +| --------- | ------ | ----------------------------------- | +| name | string | Header name to retrieve values for. | + + + +### Returns + +| Type | Description | +| ------------------------- | --------------------------------------------------------------------------------------- | +| `Promise` | A promise that resolves to the value of the header matching the name, otherwise `null`. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + const headerValue = await res.headerValue('connection'); + console.log(`headerValue: ${headerValue}`); // headerValue: keep-alive + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headervalues.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headervalues.md new file mode 100644 index 000000000..dc9605d55 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/headervalues.md @@ -0,0 +1,58 @@ +--- +title: 'headerValues(name)' +description: 'Browser module: Response.headerValues method' +--- + +# headerValues(name) + +Returns all values of the headers matching the name, for example `set-cookie`. The name is case insensitive. + + + +| Parameter | Type | Description | +| --------- | ------ | ----------------------------------- | +| name | string | Header name to retrieve values for. | + + + +### Returns + +| Type | Description | +| ------------------------- | ------------------------------------------------------------------------ | +| `Promise` | A promise that resolves to an array of header values for the given name. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + const headerValues = await res.headerValues('connection'); + console.log(`headerValues: ${headerValues}`); // headerValues: keep-alive + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/json.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/json.md new file mode 100644 index 000000000..1c1aaa66c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/json.md @@ -0,0 +1,50 @@ +--- +title: 'json()' +description: 'Browser module: Response.json method' +--- + +# json() + +Returns the JSON representation of response body. Throws if response body is not parsable via `JSON.parse`. + +### Returns + +| Type | Description | +| -------------- | ------------------------------------- | +| `Promise` | JSON representation of response body. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://httpbin.test.k6.io/json'); + + const json = await res.json(); + console.log(`json: ${JSON.stringify(json)}`); // json: {"slideshow": + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/ok.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/ok.md new file mode 100644 index 000000000..4c5fc949a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/ok.md @@ -0,0 +1,49 @@ +--- +title: 'ok()' +description: 'Browser module: Response.ok method' +--- + +# ok() + +Returns a `boolean` stating whether the response was successful (status in the range 200-299) or not. + +### Returns + +| Type | Description | +| --------- | -------------------------------------------------------------- | +| `boolean` | Returns a boolean stating whether the response was successful. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + console.log(`ok: ${res.ok()}`); // ok: true + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/request.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/request.md new file mode 100644 index 000000000..95880d948 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/request.md @@ -0,0 +1,50 @@ +--- +title: 'request()' +description: 'Browser module: Response.request method' +--- + +# request() + +Returns the matching [Request](https://grafana.com/docs/k6//javascript-api/k6-browser/request) object. + +### Returns + +| Type | Description | +| --------- | --------------------- | +| `Request` | The `Request` object. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + const req = res.request(); + // ... + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/securitydetails.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/securitydetails.md new file mode 100644 index 000000000..a5010c354 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/securitydetails.md @@ -0,0 +1,61 @@ +--- +title: 'securityDetails()' +description: 'Browser module: Response.securityDetails method' +--- + +# securityDetails() + +Returns SSL and other security information. + +### Returns + +| Type | Description | +| -------------------------------- | -------------------------------------------- | +| Promise | Returns [SecurityDetails](#securitydetails). | + +### SecurityDetails + +| Property | Type | Description | +| ----------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------- | +| subjectName | string | Common Name component of the Subject field. The value is extracted from the certificate. This should only be used for informational purposes. | +| issuer | string | Common Name component of the Issuer field. The value is extracted from the certificate. This should only be used for informational purposes. | +| validFrom | number | Unix timestamp (in seconds) specifying the exact date/time when this cert becomes valid. | +| validTo | number | Unix timestamp (in seconds) specifying the exact date/time when this cert becomes invalid. | +| protocol | string | The specific TLS protocol used. For example `TLS 1.3`. | +| sanList | string[] | String with hex encoded SHA256 fingerprint of the certificate. The value is extracted from the certificate. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + const sd = await res.securityDetails(); + console.log(`securityDetails: ${JSON.stringify(sd)}`); // securityDetails: {"subject_name":"*.k6.io"...} + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/serveraddr.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/serveraddr.md new file mode 100644 index 000000000..ad1ba2c67 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/serveraddr.md @@ -0,0 +1,57 @@ +--- +title: 'serverAddr()' +description: 'Browser module: Response.serverAddr method' +--- + +# serverAddr() + +Returns the IP address and port of the server for this response. + +### Returns + +| Type | Description | +| --------------------------- | ---------------------------------- | +| Promise | Returns [ServerAddr](#serveraddr). | + +### ServerAddr + +| Property | Type | Description | +| --------- | ------ | ------------------ | +| ipAddress | string | Remote IP address. | +| port | number | Remote port. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + const sa = await res.serverAddr(); + console.log(`serverAddr: ${JSON.stringify(sa)}`); // serverAddr: {"ip_address":"18.208.91.74","port":443} + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/size.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/size.md new file mode 100644 index 000000000..4a0f5a6c7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/size.md @@ -0,0 +1,57 @@ +--- +title: 'size()' +description: 'Browser module: Response.size method' +--- + +# size() + +The size of the response body and the headers. + +### Returns + +| Type | Description | +| ------------- | ---------------------- | +| Promise | Returns [Size](#size). | + +### Size + +| Property | Type | Description | +| -------- | ------ | ----------------------------------- | +| body | number | Size in bytes of the response body. | +| headers | number | Size in bytes of the headers body. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + const size = await res.size(); + console.log(`size: ${JSON.stringify(size)}`); // size: {"headers":174,"body":11279} + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/status.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/status.md new file mode 100644 index 000000000..e158622fe --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/status.md @@ -0,0 +1,49 @@ +--- +title: 'status()' +description: 'Browser module: Response.status method' +--- + +# status() + +Contains the status code of the response (e.g., 200 for a success). + +### Returns + +| Type | Description | +| ------ | -------------------------------- | +| number | The status code of the response. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + console.log(`status: ${res.status()}`); // status: 200 + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/statusText.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/statusText.md new file mode 100644 index 000000000..ae1add2b6 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/statusText.md @@ -0,0 +1,49 @@ +--- +title: 'statusText()' +description: 'Browser module: Response.statusText method' +--- + +# statusText() + +Contains the status text of the response (e.g. usually an "OK" for a success). + +### Returns + +| Type | Description | +| ------ | -------------------------------- | +| string | The status text of the response. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + console.log(`statusText: ${res.statusText()}`); // statusText: OK + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/text.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/text.md new file mode 100644 index 000000000..da624838f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/text.md @@ -0,0 +1,50 @@ +--- +title: 'text()' +description: 'Browser module: Response.text method' +--- + +# text() + +Returns the response body as a string. + +### Returns + +| Type | Description | +| ----------------- | --------------------------------------------------------- | +| `Promise` | A promise that resolves to the response body as a string. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + const text = await res.text(); + console.log(`text: ${text}`); // text: ... + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/url.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/url.md new file mode 100644 index 000000000..f0fee12c6 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/response/url.md @@ -0,0 +1,50 @@ +--- +title: 'url()' +description: 'Browser module: Response.url method' +--- + +# url() + +URL of the response. + +### Returns + +| Type | Description | +| ------ | -------------------- | +| string | URL of the response. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + const res = await page.goto('https://test.k6.io/'); + + const url = res.url(); + console.log(`url: ${url}`); // url: https://test.k6.io/ + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/touchscreen/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/touchscreen/_index.md new file mode 100644 index 000000000..1c176c33f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/touchscreen/_index.md @@ -0,0 +1,13 @@ +--- +title: 'Touchscreen' +description: 'Browser module: Touchscreen Class' +weight: 13 +--- + +# Touchscreen + +The `Touchscreen` class allows to interact with a virtual touchscreen. A `Touchscreen` instance can be obtained by calling the [`page.touchscreen`](https://grafana.com/docs/k6//javascript-api/k6-browser/page/touchscreen/) method. + +| Method | Description | +| ----------------------------------------------------------------------------------------------------------------------- | ---------------- | +| [tap()](https://grafana.com/docs/k6//javascript-api/k6-browser/touchscreen/tap) {{< docs/bwipt id="436" >}} | Simulates a tap. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/touchscreen/tap.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/touchscreen/tap.md new file mode 100644 index 000000000..17ccfcd49 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/touchscreen/tap.md @@ -0,0 +1,49 @@ +--- +title: 'tap()' +description: 'Browser module: Touchscreen.tap method' +--- + +# tap() + +Simulates a tap (dispatches a `touchstart` and `touchend` events) at the specified coordinates. The `x` and `y` arguments are the coordinates of the tap relative to the top-left corner of the main frame. + + + +| Parameter | Type | Default | Description | +| --------- | ------ | ------- | ----------------- | +| x | number | `0` | The x coordinate. | +| y | number | `0` | The y coordinate. | + + + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage({ + hasTouch: true, + }); + + const touchscreen = page.touchscreen; + await touchscreen.tap(50, 50); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/version.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/version.md new file mode 100644 index 000000000..373c9f58d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/version.md @@ -0,0 +1,42 @@ +--- +title: 'version()' +description: 'Browser module: version method' +--- + +# version() + +Returns the browser application's version. + +### Returns + +| Type | Description | +| ------ | ---------------------------------- | +| string | The browser application's version. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default function () { + const version = browser.version(); + console.log(version); // 105.0.5195.52 +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/worker/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/worker/_index.md new file mode 100644 index 000000000..694fbec0f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/worker/_index.md @@ -0,0 +1,49 @@ +--- +title: 'Worker' +description: 'Browser module: Worker Class' +weight: 14 +--- + +# Worker + +{{< docs/shared source="k6" lookup="browser-module-wip.md" version="" >}} + +Represents a Web Worker or a Service Worker within the browser context. + +## Supported APIs + +| k6 Class | Description | +| -------------------------------------------------------------------------------------- | ---------------------------------- | +| [url()](https://grafana.com/docs/k6//javascript-api/k6-browser/worker/url) | Returns the URL of the web worker. | + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/browser.php'); + console.log(page.workers()); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-browser/worker/url.md b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/worker/url.md new file mode 100644 index 000000000..f4e3e7f31 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-browser/worker/url.md @@ -0,0 +1,52 @@ +--- +title: 'url()' +description: 'Browser module: worker.url method' +--- + +# url() + +Returns the URL of the web worker. + +### Returns + +| Type | Description | +| ------ | -------------------------- | +| string | The URL of the web worker. | + +### Example + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/browser.php'); + const workers = page.workers(); + + for (const worker of workers) { + console.log('Worker URL:', worker.url()); + } + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/_index.md new file mode 100644 index 000000000..d3f2320e2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/_index.md @@ -0,0 +1,11 @@ +--- +title: 'k6/crypto' +description: 'The k6/crypto module provides common hashing functionality available in the GoLang crypto.' +weight: 03 +--- + +# k6/crypto + +{{< docs/shared source="k6" lookup="crypto-module.md" version="" >}} + +{{< docs/shared source="k6" lookup="javascript-api/k6-crypto.md" version="" >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/createhash.md b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/createhash.md new file mode 100644 index 000000000..e591f2241 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/createhash.md @@ -0,0 +1,47 @@ +--- +title: 'createHash( algorithm )' +description: 'Create a Hasher object, allowing the user to add data to hash multiple times, and extract hash digests along the way.' +description: 'Create a Hasher object, allowing the user to add data to hash multiple times, and extract hash digests along the way.' +weight: 01 +--- + +# createHash( algorithm ) + +{{< docs/shared source="k6" lookup="crypto-module.md" version="" >}} + +Creates a hashing object that can then be fed with data repeatedly, and from which you can extract a hash digest whenever you want. + +| Parameter | Type | Description | +| --------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| algorithm | string | The name of the hashing algorithm you want to use. Can be any one of "md4", "md5", "sha1", "sha256", "sha384", "sha512", "sha512_224", "sha512_256", "ripemd160". | + +### Returns + +| Type | Description | +| ------ | -------------------------------------------------------------------------------------------- | +| object | A [Hasher](https://grafana.com/docs/k6//javascript-api/k6-crypto/hasher) object. | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; + +export default function () { + console.log(crypto.sha256('hello world!', 'hex')); + const hasher = crypto.createHash('sha256'); + hasher.update('hello '); + hasher.update('world!'); + console.log(hasher.digest('hex')); +} +``` + +{{< /code >}} + +The above script should result in the following being printed during execution: + +```bash +INFO[0000] 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 +INFO[0000] 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/createhmac.md b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/createhmac.md new file mode 100644 index 000000000..9bea04585 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/createhmac.md @@ -0,0 +1,48 @@ +--- +title: 'createHMAC( algorithm, secret )' +description: 'Create an HMAC hashing object, allowing the user to add data to hash multiple times, and extract hash digests along the way.' +description: 'Create an HMAC hashing object, allowing the user to add data to hash multiple times, and extract hash digests along the way.' +weight: 02 +--- + +# createHMAC( algorithm, secret ) + +{{< docs/shared source="k6" lookup="crypto-module.md" version="" >}} + +Creates a HMAC hashing object that can then be fed with data repeatedly, and from which you can extract a signed hash digest whenever you want. + +| Parameter | Type | Description | +| --------- | :------------------: | :---------------------------------------------------------------------------------------------------------------------------------- | +| algorithm | string | The hashing algorithm to use. One of `md4`, `md5`, `sha1`, `sha256`, `sha384`, `sha512`, `sha512_224`, `sha512_256` or `ripemd160`. | +| secret | string / ArrayBuffer | A shared secret used to sign the data. | + +### Returns + +| Type | Description | +| ------ | :------------------------------------------------------------------------------------------- | +| object | A [Hasher](https://grafana.com/docs/k6//javascript-api/k6-crypto/hasher) object. | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; + +export default function () { + console.log(crypto.hmac('sha256', 'a secret', 'my data', 'hex')); + const hasher = crypto.createHMAC('sha256', 'a secret'); + hasher.update('my '); + hasher.update('data'); + console.log(hasher.digest('hex')); +} +``` + +{{< /code >}} + +The above script should result in the following being printed during execution: + +```bash +INFO[0000] 82f669c8fde13aef6d6977257588dc4953dfac505428f8fd6b52e19cd96d7ea5 +INFO[0000] 82f669c8fde13aef6d6977257588dc4953dfac505428f8fd6b52e19cd96d7ea5 +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/hasher.md b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/hasher.md new file mode 100644 index 000000000..e8a129caf --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/hasher.md @@ -0,0 +1,53 @@ +--- +title: 'Hasher' +description: 'Object returned by crypto.createHash(). It allows adding more data to be hashed and to extract digests along the way.' +description: 'Object returned by crypto.createHash(). It allows adding more data to be hashed and to extract digests along the way.' +weight: 80 +--- + +# Hasher + +{{< docs/shared source="k6" lookup="crypto-module.md" version="" >}} + +This object is returned by [crypto.createHash()](https://grafana.com/docs/k6//javascript-api/k6-crypto/createhash) +and allows the user to successively add more string data to be hashed, and to extract digests along the way. + +| Name | Type | Description | +| --------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Hasher.update(string) | function | Add more data to the string we want to create a hash of. Takes one string argument, which is the new data we want to add. | +| Hasher.digest(string) | function | Return a digest from the data added (using `update()`) to the Hasher object so far. Takes one string argument, which is the encoding format to return. This can be either "base64", "base64url", "base64rawurl", "hex" or "binary". See the examples below for details. | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; + +export default function () { + console.log(crypto.sha256('hello world!', 'hex')); + const hasher = crypto.createHash('sha256'); + hasher.update('hello '); + hasher.update('world!'); + console.log(hasher.digest('hex')); + + // Other encodings + console.log('base64:', hasher.digest('base64')); + console.log('base64url:', hasher.digest('base64url')); + console.log('base64rawurl:', hasher.digest('base64rawurl')); + console.log('binary:', new Uint8Array(hasher.digest('binary'))); +} +``` + +{{< /code >}} + +The above code sample should produce this in its output: + +```bash +INFO[0000] 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 +INFO[0000] 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 +INFO[0000] base64: dQnlvaDHYtK6x/kNdYtbImP6Acy8VCq1498WO+CObKk= +INFO[0000] base64url: dQnlvaDHYtK6x_kNdYtbImP6Acy8VCq1498WO-CObKk= +INFO[0000] base64rawurl: dQnlvaDHYtK6x_kNdYtbImP6Acy8VCq1498WO-CObKk +INFO[0000] binary: 117,9,229,189,160,199,98,210,186,199,249,13,117,139,91,34,99,250,1,204,188,84,42,181,227,223,22,59,224,142,108,169 +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/hmac.md b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/hmac.md new file mode 100644 index 000000000..7edc68512 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/hmac.md @@ -0,0 +1,54 @@ +--- +title: 'hmac( algorithm, secret, data, outputEncoding )' +description: 'Use HMAC to sign input data.' +description: 'Use HMAC to sign input data.' +weight: 03 +--- + +# hmac( algorithm, secret, data, outputEncoding ) + +{{< docs/shared source="k6" lookup="crypto-module.md" version="" >}} + +Use [HMAC](https://en.wikipedia.org/wiki/Hash-based_message_authentication_code) to sign a piece of data using a shared secret. + +| Parameter | Type | Description | +| -------------- | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | +| algorithm | string | The hashing algorithm to use. One of `md4`, `md5`, `sha1`, `sha256`, `sha384`, `sha512`, `sha512_224`, `sha512_256` or `ripemd160`. | +| secret | string / ArrayBuffer | A shared secret used to sign the data. | +| data | string / ArrayBuffer | The data to sign. | +| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". | + +### Returns + +| Type | Description | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; + +export default function () { + let hash = crypto.hmac('sha256', 'mysecret', 'hello world!', 'hex'); + console.log(hash); + const binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]; + hash = crypto.hmac('sha256', 'mysecret', new Uint8Array(binArray).buffer, 'hex'); + console.log(hash); +} +``` + +{{< /code >}} + +The above script should result in the following being printed during execution: + +{{< code >}} + +```bash +INFO[0000] 893a72d8cab129e5ba85aea4599fd53f59bfe652cff4098a3780313228d8c20f +INFO[0000] 893a72d8cab129e5ba85aea4599fd53f59bfe652cff4098a3780313228d8c20f +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/md4.md b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/md4.md new file mode 100644 index 000000000..60e661363 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/md4.md @@ -0,0 +1,48 @@ +--- +title: 'md4( input, outputEncoding )' +description: 'Use MD4 to hash input data.' +description: 'Use MD4 to hash input data.' +weight: 04 +--- + +# md4( input, outputEncoding ) + +{{< docs/shared source="k6" lookup="crypto-module.md" version="" >}} + +Use [md4](https://pkg.go.dev/golang.org/x/crypto/md4) to hash input data. + +| Parameter | Type | Description | +| -------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. | +| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". | + +### Returns + +| Type | Description | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; + +export default function () { + let hash = crypto.md4('hello world!', 'hex'); + console.log(hash); + const binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]; + hash = crypto.md4(new Uint8Array(binArray).buffer, 'hex'); + console.log(hash); +} +``` + +{{< /code >}} + +The above script should result in the following being printed during execution: + +```bash +INFO[0000] 3363b72840acd5f49f922fef598ee85d +INFO[0000] 3363b72840acd5f49f922fef598ee85d +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/md5.md b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/md5.md new file mode 100644 index 000000000..a42306a55 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/md5.md @@ -0,0 +1,48 @@ +--- +title: 'md5( input, outputEncoding )' +description: 'Use MD5 to hash input data.' +description: 'Use MD5 to hash input data.' +weight: 05 +--- + +# md5( input, outputEncoding ) + +{{< docs/shared source="k6" lookup="crypto-module.md" version="" >}} + +Use [md5](https://golang.org/pkg/crypto/md5/) to hash input data. + +| Parameter | Type | Description | +| -------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. | +| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". | + +### Returns + +| Type | Description | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; + +export default function () { + let hash = crypto.md5('hello world!', 'hex'); + console.log(hash); + const binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]; + hash = crypto.md5(new Uint8Array(binArray).buffer, 'hex'); + console.log(hash); +} +``` + +{{< /code >}} + +The above script should result in the following being printed during execution: + +```bash +INFO[0000] fc3ff98e8c6a0d3087d515c0473f8677 +INFO[0000] fc3ff98e8c6a0d3087d515c0473f8677 +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/randombytes.md b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/randombytes.md new file mode 100644 index 000000000..873ca636a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/randombytes.md @@ -0,0 +1,38 @@ +--- +title: 'randomBytes( int )' +description: 'randomBytes.' +description: 'randomBytes.' +weight: 06 +--- + +# randomBytes( int ) + +{{< docs/shared source="k6" lookup="crypto-module.md" version="" >}} + +Return an ArrayBuffer object with a number of cryptographically random bytes. It will either return exactly the amount of bytes requested or will throw an exception if something went wrong. + +| Parameter | Type | Description | +| --------- | ------- | --------------------------------------- | +| int | integer | The length of the returned ArrayBuffer. | + +### Returns + +| Type | Description | +| ----------- | --------------------------------------------------- | +| ArrayBuffer | An ArrayBuffer with cryptographically random bytes. | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; + +export default function () { + const bytes = crypto.randomBytes(42); + const view = new Uint8Array(bytes); + console.log(view); // 156,71,245,191,56,... +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/ripemd160.md b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/ripemd160.md new file mode 100644 index 000000000..fa7109d99 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/ripemd160.md @@ -0,0 +1,48 @@ +--- +title: 'ripemd160( input, outputEncoding )' +description: 'Use RIPEMD-160 to hash input data.' +description: 'Use RIPEMD-160 to hash input data.' +weight: 07 +--- + +# ripemd160( input, outputEncoding ) + +{{< docs/shared source="k6" lookup="crypto-module.md" version="" >}} + +Use [ripemd160](https://pkg.go.dev/golang.org/x/crypto/ripemd160) to hash input data. + +| Parameter | Type | Description | +| -------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. | +| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". | + +### Returns + +| Type | Description | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; + +export default function () { + let hash = crypto.ripemd160('hello world!', 'hex'); + console.log(hash); + const binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]; + hash = crypto.ripemd160(new Uint8Array(binArray).buffer, 'hex'); + console.log(hash); +} +``` + +{{< /code >}} + +The above script should result in the following being printed during execution: + +```bash +INFO[0000] dffd03137b3a333d5754813399a5f437acd694e5 +INFO[0000] dffd03137b3a333d5754813399a5f437acd694e5 +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha1.md b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha1.md new file mode 100644 index 000000000..26c57b54b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha1.md @@ -0,0 +1,48 @@ +--- +title: 'sha1( input, outputEncoding )' +description: 'Use SHA-1 to hash input data.' +description: 'Use SHA-1 to hash input data.' +weight: 08 +--- + +# sha1( input, outputEncoding ) + +{{< docs/shared source="k6" lookup="crypto-module.md" version="" >}} + +Use [sha1](https://golang.org/pkg/crypto/sha1/) to hash input data. + +| Parameter | Type | Description | +| -------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. | +| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". | + +### Returns + +| Type | Description | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; + +export default function () { + let hash = crypto.sha1('hello world!', 'hex'); + console.log(hash); + const binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]; + hash = crypto.sha1(new Uint8Array(binArray).buffer, 'hex'); + console.log(hash); +} +``` + +{{< /code >}} + +The above script should result in the following being printed during execution: + +```bash +INFO[0000] 430ce34d020724ed75a196dfc2ad67c77772d169 +INFO[0000] 430ce34d020724ed75a196dfc2ad67c77772d169 +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha256.md b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha256.md new file mode 100644 index 000000000..0dd4f1d63 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha256.md @@ -0,0 +1,48 @@ +--- +title: 'sha256( input, outputEncoding )' +description: 'Use SHA-256 to hash input data.' +description: 'Use SHA-256 to hash input data.' +weight: 09 +--- + +# sha256( input, outputEncoding ) + +{{< docs/shared source="k6" lookup="crypto-module.md" version="" >}} + +Use [sha256](https://golang.org/pkg/crypto/sha256/) to hash input data. + +| Parameter | Type | Description | +| -------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. | +| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". | + +### Returns + +| Type | Description | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; + +export default function () { + let hash = crypto.sha256('hello world!', 'hex'); + console.log(hash); + const binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]; + hash = crypto.sha256(new Uint8Array(binArray).buffer, 'hex'); + console.log(hash); +} +``` + +{{< /code >}} + +The above script should result in the following being printed during execution: + +```bash +INFO[0000] 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 +INFO[0000] 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha384.md b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha384.md new file mode 100644 index 000000000..60d3d0463 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha384.md @@ -0,0 +1,48 @@ +--- +title: 'sha384( input, outputEncoding )' +description: 'Use SHA-384 to hash input data.' +description: 'Use SHA-384 to hash input data.' +weight: 10 +--- + +# sha384( input, outputEncoding ) + +{{< docs/shared source="k6" lookup="crypto-module.md" version="" >}} + +Use [sha384](https://golang.org/pkg/crypto/sha512/) to hash input data. + +| Parameter | Type | Description | +| -------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. | +| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". | + +### Returns + +| Type | Description | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; + +export default function () { + let hash = crypto.sha384('hello world!', 'hex'); + console.log(hash); + const binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]; + hash = crypto.sha384(new Uint8Array(binArray).buffer, 'hex'); + console.log(hash); +} +``` + +{{< /code >}} + +The above script should result in the following being printed during execution: + +```bash +INFO[0000] d33d40f7010ce34aa86efd353630309ed5c3d7ffac66d988825cf699f4803ccdf3f033230612f0945332fb580d8af805 +INFO[0000] d33d40f7010ce34aa86efd353630309ed5c3d7ffac66d988825cf699f4803ccdf3f033230612f0945332fb580d8af805 +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha512.md b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha512.md new file mode 100644 index 000000000..acb7141d3 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha512.md @@ -0,0 +1,48 @@ +--- +title: 'sha512( input, outputEncoding )' +description: 'Use SHA-512 to hash input data.' +description: 'Use SHA-512 to hash input data.' +weight: 11 +--- + +# sha512( input, outputEncoding ) + +{{< docs/shared source="k6" lookup="crypto-module.md" version="" >}} + +Use [sha512](https://golang.org/pkg/crypto/sha512/) to hash input data. + +| Parameter | Type | Description | +| -------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. | +| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". | + +### Returns + +| Type | Description | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; + +export default function () { + let hash = crypto.sha512('hello world!', 'hex'); + console.log(hash); + const binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]; + hash = crypto.sha512(new Uint8Array(binArray).buffer, 'hex'); + console.log(hash); +} +``` + +{{< /code >}} + +The above script should result in the following being printed during execution: + +```bash +INFO[0000] db9b1cd3262dee37756a09b9064973589847caa8e53d31a9d142ea2701b1b28abd97838bb9a27068ba305dc8d04a45a1fcf079de54d607666996b3cc54f6b67c +INFO[0000] db9b1cd3262dee37756a09b9064973589847caa8e53d31a9d142ea2701b1b28abd97838bb9a27068ba305dc8d04a45a1fcf079de54d607666996b3cc54f6b67c +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha512_224.md b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha512_224.md new file mode 100644 index 000000000..ac31edd39 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha512_224.md @@ -0,0 +1,48 @@ +--- +title: 'sha512_224( input, outputEncoding )' +description: 'Use SHA-512/224 to hash input data.' +description: 'Use SHA-512/224 to hash input data.' +weight: 13 +--- + +# sha512_224( input, outputEncoding ) + +{{< docs/shared source="k6" lookup="crypto-module.md" version="" >}} + +Use [sha512_224](https://golang.org/pkg/crypto/sha512/) to hash input data. + +| Parameter | Type | Description | +| -------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. | +| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". | + +### Returns + +| Type | Description | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; + +export default function () { + let hash = crypto.sha512_224('hello world!', 'hex'); + console.log(hash); + const binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]; + hash = crypto.sha512_224(new Uint8Array(binArray).buffer, 'hex'); + console.log(hash); +} +``` + +{{< /code >}} + +The above script should result in the following being printed during execution: + +```bash +INFO[0000] bc4ed196f7ba1c20f6fb6be1f91edf8293a35b065d6e7d6fd368c890 +INFO[0000] bc4ed196f7ba1c20f6fb6be1f91edf8293a35b065d6e7d6fd368c890 +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha512_256.md b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha512_256.md new file mode 100644 index 000000000..9e442247b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-crypto/sha512_256.md @@ -0,0 +1,48 @@ +--- +title: 'sha512_256( input, outputEncoding )' +description: 'Use SHA-512/256 to hash input data.' +description: 'Use SHA-512/256 to hash input data.' +weight: 12 +--- + +# sha512_256( input, outputEncoding ) + +{{< docs/shared source="k6" lookup="crypto-module.md" version="" >}} + +Use [sha512_256](https://golang.org/pkg/crypto/sha512/) to hash input data. + +| Parameter | Type | Description | +| -------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------- | +| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to hash. | +| outputEncoding | string | Describes the type of encoding to use for the hash value. Can be "base64", "base64url", "base64rawurl", "hex" or "binary". | + +### Returns + +| Type | Description | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| string / Array | The hash digest as string (for "base64", "base64url", "base64rawurl", "hex" `outputEncoding`) or raw array of integers (for "binary" `outputEncoding`). | + +### Example + +{{< code >}} + +```javascript +import crypto from 'k6/crypto'; + +export default function () { + let hash = crypto.sha512_256('hello world!', 'hex'); + console.log(hash); + const binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]; + hash = crypto.sha512_256(new Uint8Array(binArray).buffer, 'hex'); + console.log(hash); +} +``` + +{{< /code >}} + +The above script should result in the following being printed during execution: + +```bash +INFO[0000] 595b5926068b4828fb1c27db21281e31118b8475cb6c3ceeb09be7b685414d5f +INFO[0000] 595b5926068b4828fb1c27db21281e31118b8475cb6c3ceeb09be7b685414d5f +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-data/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-data/_index.md new file mode 100644 index 000000000..afda6ed3c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-data/_index.md @@ -0,0 +1,9 @@ +--- +title: 'k6/data' +description: 'k6 data API' +weight: 04 +--- + +# k6/data + +{{< docs/shared source="k6" lookup="javascript-api/k6-data.md" version="" >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-data/sharedarray.md b/docs/sources/k6/v0.55.x/javascript-api/k6-data/sharedarray.md new file mode 100644 index 000000000..5750ac52f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-data/sharedarray.md @@ -0,0 +1,138 @@ +--- +title: SharedArray +description: 'SharedArray is an array-like object that shares the underlying memory between VUs.' +weight: 1 +--- + +# SharedArray + +`SharedArray` is an array-like object that shares the underlying memory between VUs. +The function executes only once, and its result is saved in memory once. +When a script requests an element, k6 gives a _copy_ of that element. + +You must construct a `SharedArray` in the [`init` context](https://grafana.com/docs/k6//using-k6/test-lifecycle). +Its constructor takes a name for the `SharedArray` and a function that needs to return an array object itself: + +```javascript +import { SharedArray } from 'k6/data'; + +const data = new SharedArray('some name', function () { + const dataArray = []; + // more operations + return dataArray; // must be an array +}); +``` + +The name argument is required. +VUs are completely separate JS VMs, and k6 needs some way to identify the `SharedArray` that it needs to return. +You can have multiple `SharedArrays` and even load only some of them for given VUs, +though this is unlikely to have any performance benefit. + +Supported operations on a `SharedArray` include: + +- Getting the number of elements with `length` +- Getting an element by its index using the normal syntax `array[index]` +- Using `for-of` loops + +In most cases, you should be able to reduce the memory usage of an array data structure by wrapping it in a `SharedArray`. +Once constructed, a `SharedArray` is read-only, so **you can't use a SharedArray to communicate data between VUs**. + +{{< admonition type="caution" >}} + +Attempting to instantiate a `SharedArray` outside of the [init context](https://grafana.com/docs/k6//using-k6/test-lifecycle) results in the exception `new SharedArray must be called in the init context`. + +This limitation will eventually be removed, but for now, the implication is that you can use `SharedArray` to populate test data only at the very beginning of your test and not as a result of receiving data from a response (for example). + +{{< /admonition >}} + +## Example + +{{< code >}} + +```javascript +import { SharedArray } from 'k6/data'; + +const data = new SharedArray('some name', function () { + // All heavy work (opening and processing big files for example) should be done inside here. + // This way it will happen only once and the result will be shared between all VUs, saving time and memory. + const f = JSON.parse(open('./somefile.json')); + return f; // f must be an array +}); + +export default function () { + const element = data[Math.floor(Math.random() * data.length)]; + // do something with element +} +``` + +{{< /code >}} + +## Performance characteristics + +Internally, the current implementation of `SharedArray` keeps the data marshaled as JSON and unmarshals elements only when they are requested. + +In general, this operation should be unnoticeable (relative to whatever else you do with the data). +But, for small data sets, `SharedArray` might perform worse. +However, this is highly dependent on use case. + +To test this, we ran the following script on version v0.31.0 with 100 VUs. + +{{< code >}} + +```javascript +import { check } from 'k6'; +import http from 'k6/http'; +import { SharedArray } from 'k6/data'; + +const n = parseInt(__ENV.N); +function generateArray() { + const arr = new Array(n); + for (let i = 0; i < n; i++) { + arr[i] = { something: 'something else' + i, password: '12314561' }; + } + return arr; +} + +let data; +if (__ENV.SHARED === 'true') { + data = new SharedArray('my data', generateArray); +} else { + data = generateArray(); +} + +export default function () { + const iterationData = data[Math.floor(Math.random() * data.length)]; + const res = http.post('https://httpbin.test.k6.io/anything', JSON.stringify(iterationData), { + headers: { 'Content-type': 'application/json' }, + }); + check(res, { 'status 200': (r) => r.status === 200 }); +} +``` + +{{< /code >}} + +As the table shows, performance didn't differ much at lower numbers of data lines: +up until around 1000 data lines, `SharedArray` shows little benefit in memory usage +and had a higher upper bound of CPU usage (though not substantially higher). + +At 10k lines and above, the memory savings started to heavily carry over to CPU savings as well. + +| data lines | shared | wall time | CPU % | MEM usage | http requests | +| ---------- | ------ | --------- | -------- | ----------- | ------------- | +| 100 | true | 2:01:70 | 70-79% | 213-217MB | 92191-98837 | +| 100 | false | 2:01:80 | 74-75% | 224-232MB | 96851-98643 | +| 1000 | true | 2:01:60 | 74-79% | 209-216MB | 98251-98806 | +| 1000 | false | 2:01:90 | 75-77% | 333-339MB | 98069-98951 | +| 10000 | true | 2:01:70 | 78-79% | 213-217MB | 97953-98735 | +| 10000 | false | 2:03:00 | 80-83% | 1364-1400MB | 96816-98852 | +| 100000 | true | 2:02:20 | 78-79% | 238-275MB | 98103-98540 | +| 100000 | false | 2:14:00 | 120-124% | 8.3-9.1GB | 96003-97802 | + +In v0.30.0, the difference in CPU usage at lower numbers was around 10-15%, but it also started to even out at around 10k data lines and was a clear winner at 100k. + +The CPU/memory data came from using `/usr/bin/time`. Refer to the [gist with the raw data](https://gist.github.com/MStoykov/1181cfa6f00bc56b90915155f885e2bb). + +These numbers are purely illustrative: the performance can be affected by any additional processing of the element retrieved from the `SharedArray`, or if an output is in use, or it gets multiple elements, etc. +While `SharedArray` has some CPU usage, +it might turn out to be negligible in a given situation with just 10 elements, or more problematic than the memory usage for a 100k elements. +So, if in doubt, you should probably run some benchmarks and decide which tradeoffs are more important for your use case. diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-encoding/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-encoding/_index.md new file mode 100644 index 000000000..e2e16eae7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-encoding/_index.md @@ -0,0 +1,9 @@ +--- +title: 'k6/encoding' +description: 'The encoding module provides base64 encoding/decoding as defined by RFC4648.' +weight: 05 +--- + +# k6/encoding + +{{< docs/shared source="k6" lookup="javascript-api/k6-encoding.md" version="" >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-encoding/b64decode--input---encoding---format.md b/docs/sources/k6/v0.55.x/javascript-api/k6-encoding/b64decode--input---encoding---format.md new file mode 100644 index 000000000..543f6e863 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-encoding/b64decode--input---encoding---format.md @@ -0,0 +1,50 @@ +--- +title: 'b64decode( input, [encoding], [format] )' +description: 'Base64 decode a string.' +description: 'Base64 decode a string.' +slug: 'b64decode' +--- + +# b64decode( input, [encoding], [format] ) + +Decode the passed base64 encoded `input` string into the unencoded original input in either binary or string formats. + +| Parameter | Type | Description | +| ------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| input | string | The string to base64 decode. | +| encoding (optional) | string | The base64 encoding to use.
Available options are:
- **"std"**: the standard encoding with `=` padding chars and `+` and `/` characters in encoding alphabet. This is the default.
- **"rawstd"**: like `std` but without `=` padding characters.
- **"url"**: URL safe version of `std`, encoding alphabet doesn't contain `+` and `/` characters, but rather `-` and `_` characters.
- **"rawurl"**: like `url` but without `=` padding characters. | +| format (optional) | string | If `"s"` return the data as a string, otherwise if unspecified an ArrayBuffer object is returned. | + +### Returns + +| Type | Description | +| -------------------- | ----------------------------------------------------------------------------------------------------------------------------- | +| ArrayBuffer / string | The base64 decoded version of the `input` string in either string or ArrayBuffer format, depending on the `format` parameter. | + +### Example + +{{< code >}} + +```javascript +import { check } from 'k6'; +import encoding from 'k6/encoding'; + +export default function () { + const str = 'hello world'; + const enc = 'aGVsbG8gd29ybGQ='; + const expBin = new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]); + check(null, { + 'is decoding to string correct': () => encoding.b64decode(enc, 'std', 's') === str, + 'is decoding to ArrayBuffer correct': () => { + const decBin = new Uint8Array(encoding.b64decode(enc)); + if (decBin.length != expBin.length) return false; + for (let i = 0; i < decBin.length; i++) { + if (decBin[i] !== expBin[i]) return false; + } + return true; + }, + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-encoding/b64encode--input---encoding.md b/docs/sources/k6/v0.55.x/javascript-api/k6-encoding/b64encode--input---encoding.md new file mode 100644 index 000000000..affde5436 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-encoding/b64encode--input---encoding.md @@ -0,0 +1,40 @@ +--- +title: 'b64encode( input, [encoding] )' +description: 'Encode data in base64.' +description: 'Encode data in base64.' +slug: 'b64encode' +--- + +# b64encode( input, [encoding] ) + +| Parameter | Type | Description | +| ------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| input | string / ArrayBuffer | The input string or `ArrayBuffer` object to base64 encode. | +| encoding (optional) | string | The base64 encoding to use.
Available options are:
- **"std"**: the standard encoding with `=` padding chars and `+` and `/` characters in encoding alphabet. This is the default.
- **"rawstd"**: like `std` but without `=` padding characters.
- **"url"**: URL safe version of `std`, encoding alphabet doesn't contain `+` and `/` characters, but rather `-` and `_` characters.
- **"rawurl"**: like `url` but without `=` padding characters. | + +### Returns + +| Type | Description | +| ------ | ---------------------------------------- | +| string | The base64 encoding of the `input` data. | + +### Example + +{{< code >}} + +```javascript +import { check } from 'k6'; +import encoding from 'k6/encoding'; + +export default function () { + const str = 'hello world'; + const enc = 'aGVsbG8gd29ybGQ='; + const buf = new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]).buffer; + check(null, { + 'is encoding string correct': () => encoding.b64encode(str) === enc, + 'is encoding ArrayBuffer correct': () => encoding.b64encode(buf) === enc, + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-execution.md b/docs/sources/k6/v0.55.x/javascript-api/k6-execution.md new file mode 100644 index 000000000..e057b8377 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-execution.md @@ -0,0 +1,255 @@ +--- +title: 'k6/execution' +description: "Get information about the current test's execution state." +weight: 06 +--- + +# k6/execution + +{{< docs/shared source="k6" lookup="javascript-api/k6-execution-intro.md" version="" >}} + +
+ +```javascript +import exec from 'k6/execution'; + +export const options = { + scenarios: { + myscenario: { + // this will be the returned name + executor: 'shared-iterations', + maxDuration: '30m', + }, + }, +}; + +export default function () { + console.log(exec.scenario.name); // myscenario +} +``` + +
+ +> ℹ️ **Identifiers** +> +> All unique identifiers are sequentially generated starting from a base of zero (iterations) or one (VU IDs). In distributed/cloud test runs, the test-wide iteration numbers and VU identifiers are still going to be unique across instances, though there might be gaps in the sequences when, for example, some instances execute faster iterations than others or allocate more VUs mid-test. + +### instance + +The instance object provides information associated with the load generator instance. You can think of it as the current running k6 process, which will likely be a single process if you are running k6 on your local machine. When running a cloud/distributed test with multiple load generator instances, the values of the following properties can differ across instances. + +| Property | Type | Description | +| ---------------------- | ------- | ------------------------------------------------------------------------- | +| iterationsInterrupted | integer | The number of prematurely interrupted iterations in the current instance. | +| iterationsCompleted | integer | The number of completed iterations in the current instance. | +| vusActive | integer | The number of active VUs. | +| vusInitialized | integer | The number of currently initialized VUs. | +| currentTestRunDuration | float | The time passed from the start of the current test run in milliseconds. | + +### scenario + +Meta information and execution details about the current running [scenario](https://grafana.com/docs/k6//using-k6/scenarios). + +| Property | Type | Description | +| ------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| name | string | The assigned name of the running scenario. | +| executor | string | The name of the running [Executor](https://grafana.com/docs/k6//using-k6/scenarios#executors) type. | +| startTime | integer | The Unix timestamp in milliseconds when the scenario started. | +| progress | float | Percentage in a 0 to 1 interval of the scenario progress. | +| iterationInInstance | integer | The unique and zero-based sequential number of the current iteration in the scenario, across the current instance. | +| iterationInTest | integer | The unique and zero-based sequential number of the current iteration in the scenario. It is unique in all k6 execution modes - in local, cloud and distributed/segmented test runs. However, while every instance will get non-overlapping index values in cloud/distributed tests, they might iterate over them at different speeds, so the values won't be sequential across them. | + +### test + +Control the test execution. + +| Property | Type | Description | +| --------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| abort([String]) | function | It aborts the test run with the exit code `108`, and an optional string parameter can provide an error message. Aborting the test will not prevent the `teardown()` execution. | +| options | Object | It returns an object with all the test options as properties. The options' values are consolidated following the [order of precedence](https://grafana.com/docs/k6//using-k6/k6-options/how-to#order-of-precedence) and derived if shortcuts have been used. It returns `null` for properties where the relative option hasn't been defined. | + +### vu + +Meta information and execution details about the current vu. + +| Property | Type | Description | +| ------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| iterationInInstance | integer | The identifier of the iteration in the current instance for this VU. This is only unique for current VU and this instance (if multiple instances). This keeps being aggregated if a given VU is reused between multiple scenarios. | +| iterationInScenario | integer | The identifier of the iteration in the current scenario for this VU. This is only unique for current VU and scenario it is currently executing. | +| idInInstance | integer | The identifier of the VU across the instance. Not unique across multiple instances. | +| idInTest | integer | The globally unique (across the whole test run) identifier of the VU. | +| metrics.tags | object | The map that gives control over [VU's Tags](https://grafana.com/docs/k6//using-k6/tags-and-groups#tags). The Tags will be included in every metric emitted by the VU and the Tags' state is maintained across different iterations of the same Scenario while the VU exists. Check how to use it in the [example](#tags) below. | +| metrics.metadata | object | The map that gives control over VU's Metadata. The Metadata will be included in every metric emitted by the VU and the Metadata's state is maintained across different iterations of the same Scenario while the VU exists. Check how to use it in the [example](#metadata) below. | + +{{< collapse title="Setting vu.metrics.tags" >}} + +Setting a Tag with the same key as a [system tag](https://grafana.com/docs/k6//using-k6/k6-options/reference#system-tags) is allowed, but it requires attention to avoid unexpected results. Overwriting system tags will not throw an error, but in most cases will not actually change the value of the emitted metrics as expected. For example, trying to set the `url` tag value will not result in a changed tag value when `http.get()` is called, since the tag value is determined by the HTTP request itself. However, it will add the tag `url` to the metric samples emitted by a `check()` or `metric.add()`, which is probably not the desired behavior. On the other hand, setting the `name` tag will work as expected, since that was already supported for `http.*` methods, for the purposes of the [URL Grouping](https://grafana.com/docs/k6//using-k6/http-requests#url-grouping) feature. + +Not all the types are accepted as a tag value: k6 supports strings, numbers and boolean types. Under the hood, the `tags` object handles a Tag as a `String` key-value pair, so all the types will be implicitly converted into a string. If one of the denied types is used (e.g. Object or Array) and the [`throw` option](https://grafana.com/docs/k6//using-k6/k6-options/reference#throw) is set, an exception will be thrown. Otherwise, only a warning is printed and the tag value will be discarded. + +{{< /collapse >}} + +## Examples and use cases + +### Getting unique data once + +This is a common use case for data parameterization, you can read the [examples](https://grafana.com/docs/k6//examples/data-parameterization#retrieving-unique-data) using `scenario.iterationInTest` and `vu.idInTest`. + +### Timing operations + +The `startTime` property from the `scenario` object can be used to time operations. + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +export default function () { + // do some long operations + // ... + console.log(`step1: scenario ran for ${new Date() - new Date(exec.scenario.startTime)}ms`); + + // some more long operations + //... + console.log(`step2: scenario ran for ${new Date() - new Date(exec.scenario.startTime)}ms`); +} +``` + +{{< /code >}} + +### Script naming + +The `name` property can be used for executing the logic based on which script is currently running. + +> **Tip**: +> If you need to run [multiple scenarios](https://grafana.com/docs/k6//using-k6/scenarios/advanced-examples#using-multiple-scenarios) in your script you can use `exec` option achieve a similar goal + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +export const options = { + scenarios: { + 'the-first': { + // ... + }, + 'the-second': { + // ... + }, + }, +}; + +export default function () { + if (exec.scenario.name === 'the-first') { + // do some logic during this scenario + } else { + // do some other logic in the others + } +} +``` + +{{< /code >}} + +### Test Abort + +Aborting is possible during initialization: + +{{< code >}} + +```javascript +import exec from 'k6/execution'; +exec.test.abort(); +``` + +{{< /code >}} + +As well as inside the `default` function: + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +export default function () { + // Note that you can abort with a specific message too + exec.test.abort('this is the reason'); +} + +export function teardown() { + console.log('teardown will still be called after test.abort()'); +} +``` + +{{< /code >}} + +### Get test options + +Get the consolidated and derived options' values + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +export const options = { + stages: [ + { duration: '5s', target: 100 }, + { duration: '5s', target: 50 }, + ], +}; + +export default function () { + console.log(exec.test.options.paused); // null + console.log(exec.test.options.scenarios.default.stages[0].target); // 100 +} +``` + +{{< /code >}} + +### Tags + +The `vu.metrics.tags` property can be used for getting or setting [VU's tags](https://grafana.com/docs/k6//using-k6/tags-and-groups#tags). + +{{< code >}} + +```javascript +import http from 'k6/http'; +import exec from 'k6/execution'; + +export default function () { + exec.vu.metrics.tags['mytag'] = 'value1'; + exec.vu.metrics.tags['mytag2'] = 2; + + // the metrics these HTTP requests emit will get tagged with `mytag` and `mytag2`: + http.batch(['https://test.k6.io', 'https://test-api.k6.io']); +} +``` + +{{< /code >}} + +`vu.tags` (without `metrics`) can also be used, but is deprecated for the more context-specific variant. + +### Metadata + +The `vu.metrics.metadata` property can be used for getting or setting VU's metadata. It is similar to `tags`, but can be used for high cardinality data. It also can not be used in thresholds and will likely be handled differently by each output. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import exec from 'k6/execution'; + +export default function () { + exec.vu.metrics.metadata['trace_id'] = 'somecoolide'; + + // the metrics these HTTP requests emit will get the metadata `trace_id`: + http.batch(['https://test.k6.io', 'https://test-api.k6.io']); + + delete exec.vu.metrics.metadata['trace_id']; // this will unset it + // which will make the metrics these requests to not have the metadata `trace_id` set on them. + http.batch(['https://test.k6.io', 'https://test-api.k6.io']); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/_index.md new file mode 100644 index 000000000..84e758208 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/_index.md @@ -0,0 +1,11 @@ +--- +title: 'k6/experimental' +description: 'k6 experimental APIs' +weight: 07 +--- + +# k6/experimental + +{{< docs/shared source="k6" lookup="experimental-module.md" version="" >}} + +{{< docs/shared source="k6" lookup="javascript-api/k6-experimental.md" version="" >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/Options.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/Options.md new file mode 100644 index 000000000..23879fb4a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/Options.md @@ -0,0 +1,53 @@ +--- +title: 'Options' +description: 'Options represents the configuration for CSV parsing.' +weight: 40 +--- + +# Options + +The `Options` object describes the configuration available for the operation of parsing CSV files using the [`csv.parse`](https://grafana.com/docs/k6//javascript-api/k6-experimental/csv/parse) function and the [`csv.Parser`](https://grafana.com/docs/k6//javascript-api/k6-experimental/csv/parser) class. + +## Properties + +| Property | Type | Description | +| :------------ | :---------------- | :-------------------------------------------------------------------------------------------------------- | +| delimiter | string | The character used to separate fields in the CSV file. Default is `','`. | +| skipFirstLine | boolean | Whether to skip the first line of the CSV file. Default is `false`. | +| fromLine | (optional) number | The line number from which to start reading the CSV file. Default is `0`. | +| toLine | (optional) number | The line number at which to stop reading the CSV file. If the option is not set, then read until the end. | + +## Example + +{{< code >}} + +```javascript +import { open } from 'k6/experimental/fs'; +import csv from 'k6/experimental/csv'; + +const file = await open('data.csv'); +const parser = new csv.Parser(file, { + delimiter: ',', + skipFirstLine: true, + fromLine: 2, + toLine: 8, +}); + +export default async function () { + // The `next` method attempts to read the next row from the CSV file. + // + // It returns an iterator-like object with a `done` property that indicates whether + // there are more rows to read, and a `value` property that contains the row fields + // as an array. + const { done, value } = await parser.next(); + if (done) { + throw new Error('No more rows to read'); + } + + // We expect the `value` property to be an array of strings, where each string is a field + // from the CSV record. + console.log(done, value); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/Parser.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/Parser.md new file mode 100644 index 000000000..d2e1135f1 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/Parser.md @@ -0,0 +1,92 @@ +--- +title: 'Parser' +description: 'A CSV parser for streaming CSV parsing, allowing line-by-line reading with minimal memory consumption.' +weight: 30 +--- + +# Parser + +The `csv.Parser` class provides a streaming parser that reads CSV files line-by-line, offering fine-grained control over the parsing process and minimizing memory consumption. +It's well-suited for scenarios where memory efficiency is crucial or when you need to process large CSV files without loading the entire file into memory. + +## Asynchronous nature + +The `csv.Parser` class methods are asynchronous and return Promises. +Due to k6's current limitation with the [init context](https://grafana.com/docs/k6//using-k6/test-lifecycle/#the-init-stage) (which doesn't support asynchronous functions directly), you need to use an asynchronous wrapper such as: + +{{< code >}} + +```javascript +import { open } from 'k6/experimental/fs'; +import csv from 'k6/experimental/csv'; + +let file; +let parser; +(async function () { + file = await open('data.csv'); + parser = new csv.Parser(file); +})(); +``` + +{{< /code >}} + +## Constructor + +| Parameter | Type | Description | +| :-------- | :-------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------- | +| file | [fs.File](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/file) | A file instance opened using the fs.open function. | +| options | [Options](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/options) | An optional parameter object to customize the parsing behavior. Options can include a delimiter (string). | + +### Methods + +| Name | Description | +| :------- | :---------------------------------------------------------------------------------------------------- | +| `next()` | Reads the next line from the CSV file and returns a promise that resolves to an iterator-like object. | + +### Returns + +A promise resolving to an object with the following properties: + +| Property | Type | Description | +| :------- | :------- | :---------------------------------------------------------------------------------------------------- | +| done | boolean | Indicates whether there are more rows to read (false) or the end of the file has been reached (true). | +| value | string[] | Contains the fields of the CSV record as an array of strings. If done is true, value is undefined. | + +## Example + +{{< code >}} + +```javascript +import { open } from 'k6/experimental/fs'; +import csv from 'k6/experimental/csv'; + +export const options = { + iterations: 10, +}; + +const file = await open('data.csv'); +const parser = new csv.Parser(file, { skipFirstLine: true }); + +export default async function () { + // The `next` method attempts to read the next row from the CSV file. + // + // It returns an iterator-like object with a `done` property that indicates whether + // there are more rows to read, and a `value` property that contains the row fields + // as an array. + const { done, value } = await parser.next(); + if (done) { + throw new Error('No more rows to read'); + } + + // We expect the `value` property to be an array of strings, where each string is a field + // from the CSV record. + console.log(done, value); +} +``` + +{{< /code >}} + +## Notes on usage + +- **Memory efficiency**: Since `csv.Parser` reads the file line-by-line, it keeps memory usage low and avoids loading the entire set of records into memory. This is particularly useful for large CSV files. +- **Streaming control**: The streaming approach provides more control over how records are processed, which can be beneficial for complex data handling requirements. diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/_index.md new file mode 100644 index 000000000..b5628b667 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/_index.md @@ -0,0 +1,107 @@ +--- +title: 'csv' +description: 'k6 csv experimental API' +weight: 10 +--- + +# csv + +{{< docs/shared source="k6" lookup="experimental-module.md" version="" >}} + +The `k6-experimental/csv` module provides efficient ways to handle CSV files in k6, offering faster parsing and lower memory +usage compared to traditional JavaScript-based libraries. + +This module includes functionalities for both full-file parsing and streaming, allowing users to choose between +performance and memory optimization. + +## Key features + +- The [`csv.parse()`](https://grafana.com/docs/k6//javascript-api/k6-experimental/csv/parse) function parses a complete CSV file into a SharedArray, leveraging Go-based processing for better performance and reduced memory footprint compared to JavaScript alternatives. +- The [`csv.Parser`](https://grafana.com/docs/k6//javascript-api/k6-experimental/csv/parser) class is a streaming parser that reads CSV files line-by-line, optimizing memory usage and giving more control over the parsing process through a stream-like API. + +### Benefits + +- **Faster parsing**: The [`csv.parse()`](https://grafana.com/docs/k6//javascript-api/k6-experimental/csv/parse) function bypasses the JavaScript runtime, significantly speeding up parsing for large CSV files. +- **Lower memory usage**: Both [`csv.parse()`](https://grafana.com/docs/k6//javascript-api/k6-experimental/csv/parse) and [`csv.Parser`](https://grafana.com/docs/k6//javascript-api/k6-experimental/csv/parser) support shared memory across virtual users (VUs) when using the [`fs.open()`](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/open) function. +- **Flexibility**: Users can choose between full-file parsing with [`csv.parse`](https://grafana.com/docs/k6//javascript-api/k6-experimental/csv/parse) for speed or line-by-line streaming with [`csv.Parser`](https://grafana.com/docs/k6//javascript-api/k6-experimental/csv/parser) for memory efficiency. + +### Trade-offs + +- The [`csv.parse()`](https://grafana.com/docs/k6//javascript-api/k6-experimental/csv/parse) function parses the entire file during the initialization phase, which might increase startup time and memory usage for large files. Best for scenarios where performance is more important than memory consumption. +- The [`csv.Parser`](https://grafana.com/docs/k6//javascript-api/k6-experimental/csv/parser) class processes the file line-by-line, making it more memory-efficient but potentially slower due to the overhead of reading each line. Suitable for scenarios where memory usage is critical or more granular control over parsing is needed. + +## API + +| Function/Object | Description | +| ------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------- | +| [csv.parse()](https://grafana.com/docs/k6//javascript-api/k6-experimental/csv/parse) | Parses an entire CSV file into a SharedArray for high-performance scenarios. | +| [csv.Parser](https://grafana.com/docs/k6//javascript-api/k6-experimental/csv/parser) | A class for streaming CSV parsing, allowing line-by-line reading with minimal memory consumption. | + +## Example + +### Parsing a full CSV File into a SharedArray + +{{< code >}} + +```javascript +import { open } from 'k6/experimental/fs'; +import csv from 'k6/experimental/csv'; +import { scenario } from 'k6/execution'; + +export const options = { + iterations: 10, +}; + +const file = await open('data.csv'); + +// The `csv.parse` function consumes the entire file at once and returns +// the parsed records as a `SharedArray` object. +const csvRecords = await csv.parse(file, { delimiter: ',' }); + +export default async function () { + // `csvRecords` is a `SharedArray`. Each element is a record from the CSV file, represented as an array + // where each element is a field from the CSV record. + // + // Thus, `csvRecords[scenario.iterationInTest]` will give us the record for the current iteration. + console.log(csvRecords[scenario.iterationInTest]); +} +``` + +{{< /code >}} + +### Streaming a CSV file line-by-line + +{{< code >}} + +```javascript +import { open } from 'k6/experimental/fs'; +import csv from 'k6/experimental/csv'; + +export const options = { + iterations: 10, +}; + +const file = await open('data.csv'); + +// The `csv.parse` function consumes the entire file at once and returns +// the parsed records as a `SharedArray` object. +const parser = new csv.Parser(file); + +export default async function () { + // The parser `next` method attempts to read the next row from the CSV file. + // + // It returns an iterator-like object with a `done` property that indicates whether + // there are more rows to read, and a `value` property that contains the row fields + // as an array. + const { done, value } = await parser.next(); + if (done) { + throw new Error('No more rows to read'); + } + + // We expect the `value` property to be an array of strings, where each string is a field + // from the CSV record. + console.log(done, value); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/parse.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/parse.md new file mode 100644 index 000000000..42fbbc185 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/csv/parse.md @@ -0,0 +1,78 @@ +--- +title: 'parse( file, [options] )' +description: 'parse a CSV file into a SharedArray' +weight: 20 +--- + +# parse( file, [options] ) + +The `csv.parse` function parses an entire CSV file at once and returns a promise that resolves to a [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) instance. +This function uses Go-based processing, which results in faster parsing and lower memory usage compared to JavaScript alternatives. +It's ideal for scenarios where performance is a priority, and the entire CSV file can be loaded into memory. + +## Asynchronous Nature + +`csv.parse` is an asynchronous function that returns a Promise. Due to k6's current limitation with the [init context](https://grafana.com/docs/k6//using-k6/test-lifecycle/) (which +doesn't support asynchronous functions directly), you need to use an asynchronous wrapper like this: + +{{< code >}} + +```javascript +import { open } from 'k6/experimental/fs'; +import csv from 'k6/experimental/csv'; + +let file; +let csvRecords; +(async function () { + file = await open('data.csv'); + csvRecords = await csv.parse(file, { delimiter: ',' }); +})(); +``` + +{{< /code >}} + +## Parameters + +| Parameter | Type | Description | +| :-------- | :-------------------------------------------------------------------------------------------- | :-------------------------------------------------------------- | +| file | [fs.File](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/file) | A file instance opened using the `fs.open` function. | +| options | [Options](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/options) | An optional parameter object to customize the parsing behavior. | + +## Returns + +A promise resolving to a [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) instance, where each element is an array representing a CSV record, and each sub-element is a field from that record. + +## Example + +{{< code >}} + +```javascript +import { open } from 'k6/experimental/fs'; +import csv from 'k6/experimental/csv'; +import { scenario } from 'k6/execution'; + +export const options = { + iterations: 10, +}; + +const file = await open('data.csv'); + +// The `csv.parse` function consumes the entire file at once and returns +// the parsed records as a `SharedArray` object. +const csvRecords = await csv.parse(file, { skipFirstLine: true }); + +export default async function () { + // `csvRecords` is a `SharedArray`. Each element is a record from the CSV file, represented as an array + // where each element is a field from the CSV record. + // + // `csvRecords[scenario.iterationInTest]` gives the record for the current iteration. + console.log(csvRecords[scenario.iterationInTest]); +} +``` + +{{< /code >}} + +## Notes on Usage + +- **Memory considerations**: `csv.parse` loads the entire CSV file into memory at once, which may lead to increased memory usage and startup time for very large files. +- **Shared memory usage**: The [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) returned by `csv.parse` is shared among all Virtual Users (VUs), reducing memory overhead when multiple VUs access the same data. diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/FileInfo.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/FileInfo.md new file mode 100644 index 000000000..4305e968a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/FileInfo.md @@ -0,0 +1,36 @@ +--- +title: 'FileInfo' +description: 'FileInfo represents information about a file.' +weight: 30 +--- + +# FileInfo + +The `FileInfo` class represents information about a [file](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/file). + +## Properties + +| Property | Type | Description | +| :------- | :----- | :----------------------------- | +| name | string | The name of the file. | +| size | number | The size of the file in bytes. | + +## Example + +{{< code >}} + +```javascript +import { open, SeekMode } from 'k6/experimental/fs'; + +const file = await open('bonjour.txt'); + +export default async function () { + // Retrieve information about the file + const fileinfo = await file.stat(); + if (fileinfo.name != 'bonjour.txt') { + throw new Error('Unexpected file name'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/SeekMode.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/SeekMode.md new file mode 100644 index 000000000..7a3506b85 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/SeekMode.md @@ -0,0 +1,40 @@ +--- +title: 'SeekMode' +description: 'SeekMode is used to specify the position from which to seek in a file.' +weight: 40 +--- + +# SeekMode + +The `SeekMode` enum specifies the position from which to [seek](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/file/seek) in a file. + +## Members + +| Member | Value | Description | +| :------ | :---- | :------------------------------------------ | +| Start | 0 | Seek from the start of the file. | +| Current | 1 | Seek from the current position in the file. | +| End | 2 | Seek from the end of the file. | + +## Example + +{{< code >}} + +```javascript +import { open, SeekMode } from 'k6/experimental/fs'; + +const file = await open('bonjour.txt'); + +export default async function () { + // Seek 6 bytes from the start of the file + await file.seek(6, SeekMode.Start); + + // Seek 2 more bytes from the current position + await file.seek(2, SeekMode.Current); + + // Seek backwards 2 bytes from the end of the file + await file.seek(-2, SeekMode.End); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/_index.md new file mode 100644 index 000000000..413eb8709 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/_index.md @@ -0,0 +1,77 @@ +--- +title: 'fs' +description: 'k6 fs experimental API' +weight: 10 +--- + +# fs + +{{< docs/shared source="k6" lookup="experimental-module.md" version="" >}} + +The k6 filesystem experimental module provides a memory-efficient way to handle file interactions within your test scripts. It currently offers support for opening files, reading their content, seeking through their content, and retrieving metadata about them. + +### Memory efficiency + +One of the key advantages of the filesystem module is its memory efficiency. Unlike the traditional [open](https://grafana.com/docs/k6//javascript-api/init-context/open/) function, which loads a file multiple times into memory, the filesystem module reduces memory usage by loading the file as little as possible and sharing the same memory space between all VUs. This approach reduces the risk of encountering out-of-memory errors, especially in load tests involving large files. + +### Notes on usage + +An important consideration when using the filesystem module is its handling of external file modifications. Once k6 loads a file, it behaves like a "view" over its contents. If you modify the underlying file during a test, k6 will not reflect those changes in the loaded [File](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/file/) instance. + +## API Overview + +The module exports functions and objects to interact with the file system: + +| Function/Object | Description | +| ----------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | +| [open](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/open) | Opens a file and returns a promise resolving to a `File` instance. | +| [File](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/file) | Represents a file with methods for reading, seeking, and obtaining file stats. | +| [SeekMode](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/seekmode) | Enum for specifying the reference point for seek operations. Includes `Start`, `Current`, and `End`. | + +## Example + +{{< code >}} + +```javascript +import { open, SeekMode } from 'k6/experimental/fs'; + +const file = await open('bonjour.txt'); + +export default async function () { + // Seek to the beginning of the file + await file.seek(0, SeekMode.Start); + + // About information about the file + const fileinfo = await file.stat(); + if (fileinfo.name != 'bonjour.txt') { + throw new Error('Unexpected file name'); + } + + const buffer = new Uint8Array(4); + + let totalBytesRead = 0; + while (true) { + // Read into the buffer + const bytesRead = await file.read(buffer); + if (bytesRead == null) { + // EOF + break; + } + + // Do something useful with the content of the buffer + totalBytesRead += bytesRead; + + // If bytesRead is less than the buffer size, we've read the whole file + if (bytesRead < buffer.byteLength) { + break; + } + } + + // Check that we read the expected number of bytes + if (totalBytesRead != fileinfo.size) { + throw new Error('Unexpected number of bytes read'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/_index.md new file mode 100644 index 000000000..4ab72b9e7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/_index.md @@ -0,0 +1,71 @@ +--- +title: 'File' +description: 'File represents a file with methods for reading, seeking, and obtaining file stats.' +weight: 10 +--- + +# File + +The `File` class represents a file with methods for reading, seeking, and obtaining file stats. It's returned by the [open](https://grafana.com/docs/k6//javascript-api/init-context/open/) function. + +## Properties + +| Property | Type | Description | +| :------- | :----- | :----------------------------- | +| path | string | The absolute path to the file. | + +## Methods + +| Method | Description | +| :------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| [read](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/file/read) | Reads up to `buffer.byteLength` bytes from the file into the passed `buffer`. Returns a promise resolving to the number of bytes read. | +| [seek](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/file/seek) | Sets the file position indicator for the file to the passed `offset` bytes. Returns a promise resolving to the new offset. | +| [stat](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/file/stat) | Returns a promise resolving to a [FileInfo](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/fileinfo/) object with information about the file. | + +## Example + +{{< code >}} + +```javascript +import { open, SeekMode } from 'k6/experimental/fs'; + +const file = await open('bonjour.txt'); + +export default async function () { + // Seek to the beginning of the file + await file.seek(0, SeekMode.Start); + + // About information about the file + const fileinfo = await file.stat(); + if (fileinfo.name != 'bonjour.txt') { + throw new Error('Unexpected file name'); + } + + const buffer = new Uint8Array(4); + + let totalBytesRead = 0; + while (true) { + // Read into the buffer + const bytesRead = await file.read(buffer); + if (bytesRead == null) { + // EOF + break; + } + + // Do something useful with the content of the buffer + totalBytesRead += bytesRead; + + // If bytesRead is less than the buffer size, we've read the whole file + if (bytesRead < buffer.byteLength) { + break; + } + } + + // Check that we read the expected number of bytes + if (totalBytesRead != fileinfo.size) { + throw new Error('Unexpected number of bytes read'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/read.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/read.md new file mode 100644 index 000000000..bb2f9762a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/read.md @@ -0,0 +1,106 @@ +--- +title: 'read' +description: 'the read method is used to read a chunk of the file.' +weight: 20 +--- + +# read + +The `read` method is used to read a chunk of the file into an [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) buffer. + +It resolves to either the number of bytes read during the operation, or `null` if there was nothing more to read. + +## Parameters + +| Parameter | Type | Description | +| :-------- | :-------------------------------------------------------------------------------------------------------- | :-------------------------------- | +| buffer | [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) | The buffer to read the data into. | + +## Returns + +A [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving to the number of bytes read or `null` if the end of the file has been reached. + +## Example + +### Reading a file + +In the following example, we open a file and read it in chunks of 128 bytes until we reach the end of the file. + +{{< code >}} + +```javascript +import { open, SeekMode } from 'k6/experimental/fs'; + +const file = await open('bonjour.txt'); + +export default async function () { + // Seek to the beginning of the file + await file.seek(0, SeekMode.Start); + + const buffer = new Uint8Array(128); + + let totalBytesRead = 0; + while (true) { + // Read into the buffer + const bytesRead = await file.read(buffer); + if (bytesRead == null) { + // EOF + break; + } + + // Do something useful with the content of the buffer + totalBytesRead += bytesRead; + + // If bytesRead is less than the buffer size, we've read the whole file + if (bytesRead < buffer.byteLength) { + break; + } + } +} +``` + +{{< /code >}} + +### `readAll` helper function + +The following helper function can be used to read the entire contents of a file into a [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) buffer. + +{{< code >}} + +```javascript +import { open, SeekMode } from 'k6/experimental/fs'; + +let file; +(async function () { + file = await open('bonjour.txt'); +})(); + +async function readAll(file) { + // Seek to the beginning of the file + await file.seek(0, SeekMode.Start); + + const fileInfo = await file.stat(); + const buffer = new Uint8Array(fileInfo.size); + + const bytesRead = await file.read(buffer); + if (bytesRead !== fileInfo.size) { + throw new Error( + 'unexpected number of bytes read; expected ' + + fileInfo.size + + ' but got ' + + bytesRead + + ' bytes' + ); + } + + return buffer; +} + +export default async function () { + // Read the whole file + const fileContent = await readAll(file); + console.log(JSON.stringify(fileContent)); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/seek.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/seek.md new file mode 100644 index 000000000..4a16ecc9f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/seek.md @@ -0,0 +1,49 @@ +--- +title: 'seek' +description: 'seek sets the file position indicator for the file to the passed offset bytes.' +weight: 30 +--- + +# seek + +The `seek` method sets the file position indicator for the file to the passed `offset` bytes, under the mode given by `whence`. The call resolves to the new position within the resource (bytes from the start). + +Based on the [SeekMode](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/seekmode) passed, the offset is interpreted as follows: + +- when using `SeekMode.Start`, the offset must be greater than or equal to zero. +- when using `SeekMode.Current`, the offset can be positive or negative. +- when using `SeekMode.End`, the offset must be less than or equal to zero. + +## Parameters + +| Parameter | Type | Description | +| :-------- | :---------------------------------------------------------------------------------------------- | :----------------------------------------------------------- | +| offset | number | The offset in bytes from the position specified by `whence`. | +| whence | [SeekMode](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/seekmode) | The position from which the offset is applied. | + +## Returns + +A [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving to the new offset within the file. + +## Example + +{{< code >}} + +```javascript +import { open, SeekMode } from 'k6/experimental/fs'; + +const file = await open('bonjour.txt'); + +export default async function () { + // Seek 6 bytes from the start of the file + await file.seek(6, SeekMode.Start); + + // Seek 2 more bytes from the current position + await file.seek(2, SeekMode.Current); + + // Seek backwards 2 bytes from the end of the file + await file.seek(-2, SeekMode.End); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/stat.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/stat.md new file mode 100644 index 000000000..9da5bf9be --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/file/stat.md @@ -0,0 +1,35 @@ +--- +title: 'stat' +description: 'stat returns a promise resolving to a FileInfo object with information about the file.' +weight: 40 +--- + +# stat + +The `stat` method returns a promise resolving to a [FileInfo](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/fileinfo) object with information about the file. + +## Returns + +A [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) resolving to a [FileInfo](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/fileinfo) object with information about the file. + +## Examples + +{{< code >}} + +```javascript +import { open, SeekMode } from 'k6/experimental/fs'; + +const file = await open('bonjour.txt'); + +export default async function () { + // About information about the file + const fileinfo = await file.stat(); + if (fileinfo.name != 'bonjour.txt') { + throw new Error('Unexpected file name'); + } + + console.log(JSON.stringify(fileinfo)); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/open.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/open.md new file mode 100644 index 000000000..4638a8dfc --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/fs/open.md @@ -0,0 +1,45 @@ +--- +title: 'open' +description: 'open opens a file and returns a promise resolving to a File instance.' +weight: 20 +--- + +# open + +The `open` function opens a file and returns a promise that resolves to a [File](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/file) instance. Unlike the traditional [open](https://grafana.com/docs/k6//javascript-api/init-context/open/) function, which loads a file multiple times into memory, the filesystem module reduces memory usage by loading the file as little possible, and sharing the same memory space between all VUs. This approach reduces the risk of encountering out-of-memory errors, especially in load tests involving large files. + +## Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :----------------------------------------------------------------------------------- | +| path | string | The path to the file to open. Relative paths are resolved relative to the k6 script. | + +## Returns + +A promise resolving to a [File](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs/file) instance. + +## Example + +{{< code >}} + +```javascript +import { open } from 'k6/experimental/fs'; + +// k6 doesn't support async in the init context. We use a top-level async function for `await`. +// +// Each Virtual User gets its own `file` copy. +// So, operations like `seek` or `read` won't impact other VUs. +const file = await open('bonjour.txt'); + +export default async function () { + // About information about the file + const fileinfo = await file.stat(); + if (fileinfo.name != 'bonjour.txt') { + throw new Error('Unexpected file name'); + } + + console.log(JSON.stringify(fileinfo)); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/_index.md new file mode 100644 index 000000000..6145d3fd3 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/_index.md @@ -0,0 +1,35 @@ +--- +title: "redis" +description: "k6 Redis experimental API" +weight: 02 +weight: 02 +--- + +# redis + +{{< docs/shared source="k6" lookup="experimental-module.md" version="" >}} + +The [Redis](https://redis.io/) module provides a client library that makes it possible to interact with Redis directly from a k6 script. +With this module, you can: + +- Load test Redis +- Use Redis as a data store for test-script logic. + +Though the API intends to be thorough and extensive, it does not expose the whole Redis API. +Instead, the intent is to expose Redis for use cases most appropriate to k6. + +| Class | Description | +| :----------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------- | +| [Client](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client) | Client that exposes allowed interactions with Redis. | +| [Options](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/redis-options) | Options used to configure the behavior of the [Redis Client](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client). | + +### Notes on usage + +The `Client` exposes a [promise](https://javascript.info/promise-basics)-based API. +Unlike most other current k6 modules and extensions, +which operate in a synchronous manner, +the Redis `Client` operates in an asynchronous manner. +In practice, this means that using the Redis `Client`'s methods won't block test execution, +and that the test will continue to run even if the Redis `Client` isn't ready to respond to the request. +The `async` and `await` keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains +(for details, refer to the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)). diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/_index.md new file mode 100644 index 000000000..4d0bf1d4f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/_index.md @@ -0,0 +1,352 @@ +--- +title: 'Client' +description: 'Client is a Redis client to interact with a Redis server, cluster, or sentinel.' +weight: 10 +weight: 10 +--- + +# Client + +`Client` is a [Redis](https://redis.io) client to interact with a Redis server, sentinel, or cluster. It exposes a promise-based API, which users can interact with in an asynchronous manner. + +Though the API intends to be thorough and extensive, it does not expose the whole Redis API. Instead, the intent is to expose Redis for use cases most appropriate to k6. + +## Usage + +### Single-node server + +You can create a new `Client` instance that connects to a single Redis server by passing a URL string. +It must be in the format: + +``` +redis[s]://[[username][:password]@][host][:port][/db-number] +``` + +Here's an example of a URL string that connects to a Redis server running on localhost, on the default port (6379), and using the default database (0): + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +const client = new redis.Client('redis://localhost:6379'); +``` + +{{< /code >}} + +A client can also be instantiated using an [options](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/redis-options) object to support more complex use cases, and for more flexibility: + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +const client = new redis.Client({ + socket: { + host: 'localhost', + port: 6379, + }, + username: 'someusername', + password: 'somepassword', +}); +``` + +{{< /code >}} + +### TLS + +You can configure a TLS connection in a couple of ways. + +If the server has a certificate signed by a public Certificate Authority, you can use the `rediss` URL scheme: + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +const client = new redis.Client('rediss://example.com'); +``` + +{{< /code >}} + +Otherwise, you can supply your own self-signed certificate in PEM format using the [socket.tls](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/redis-options#tls-configuration-options-tlsoptions) object: + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +const client = new redis.Client({ + socket: { + host: 'localhost', + port: 6379, + tls: { + ca: [open('ca.crt')], + }, + }, +}); +``` + +{{< /code >}} + +Note that for self-signed certificates, k6's [insecureSkipTLSVerify](https://grafana.com/docs/k6//using-k6/k6-options/reference/#insecure-skip-tls-verify) option must be enabled (set to `true`). + +#### TLS client authentication (mTLS) + +You can also enable mTLS by setting two additional properties in the [socket.tls](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/redis-options#tls-configuration-options-tlsoptions) object: + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +const client = new redis.Client({ + socket: { + host: 'localhost', + port: 6379, + tls: { + ca: [open('ca.crt')], + cert: open('client.crt'), // client certificate + key: open('client.key'), // client private key + }, + }, +}); +``` + +{{< /code >}} + +### Cluster client + +You can connect to a cluster of Redis servers by using the [cluster](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/redis-options#redis-cluster-options-clusteroptions) configuration property, and passing 2 or more node URLs: + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +const client = new redis.Client({ + cluster: { + // Cluster options + maxRedirects: 3, + readOnly: true, + routeByLatency: true, + routeRandomly: true, + nodes: ['redis://host1:6379', 'redis://host2:6379'], + }, +}); +``` + +{{< /code >}} + +Or the same as above, but passing [socket](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/redis-options#socket-connection-options-socketoptions) objects to the nodes array instead of URLs: + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +const client = new redis.Client({ + cluster: { + nodes: [ + { + socket: { + host: 'host1', + port: 6379, + }, + }, + { + socket: { + host: 'host2', + port: 6379, + }, + }, + ], + }, +}); +``` + +{{< /code >}} + +### Sentinel client + +A [Redis Sentinel](https://redis.io/docs/management/sentinel/) provides high availability features, as an alternative to a Redis cluster. + +You can connect to a sentinel instance by setting additional [options](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/redis-options) in the object passed to the `Client` constructor: + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +const client = new redis.Client({ + username: 'someusername', + password: 'somepassword', + socket: { + host: 'localhost', + port: 6379, + }, + // Sentinel options + masterName: 'masterhost', + sentinelUsername: 'sentineluser', + sentinelPassword: 'sentinelpass', +}); +``` + +{{< /code >}} + +## Real world example + +{{< code >}} + +```javascript +import { check } from 'k6'; +import http from 'k6/http'; +import redis from 'k6/experimental/redis'; +import exec from 'k6/execution'; +import { textSummary } from 'https://jslib.k6.io/k6-summary/0.0.2/index.js'; + +export const options = { + scenarios: { + redisPerformance: { + executor: 'shared-iterations', + vus: 10, + iterations: 200, + exec: 'measureRedisPerformance', + }, + usingRedisData: { + executor: 'shared-iterations', + vus: 10, + iterations: 200, + exec: 'measureUsingRedisData', + }, + }, +}; + +// Instantiate a new redis client +const redisClient = new redis.Client(`redis://localhost:6379`); + +// Prepare an array of crocodile ids for later use +// in the context of the measureUsingRedisData function. +const crocodileIDs = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); + +export async function measureRedisPerformance() { + // VUs are executed in a parallel fashion, + // thus, to ensure that parallel VUs are not + // modifying the same key at the same time, + // we use keys indexed by the VU id. + const key = `foo-${exec.vu.idInTest}`; + + await redisClient.set(key, 1); + await redisClient.incrBy(key, 10); + const value = await redisClient.get(key); + if (value !== '11') { + throw new Error('foo should have been incremented to 11'); + } + + await redisClient.del(key); + if ((await redisClient.exists(key)) !== 0) { + throw new Error('foo should have been deleted'); + } +} + +export async function setup() { + await redisClient.sadd('crocodile_ids', ...crocodileIDs); +} + +export async function measureUsingRedisData() { + // Pick a random crocodile id from the dedicated redis set, + // we have filled in setup(). + const randomID = await redisClient.srandmember('crocodile_ids'); + const url = `https://test-api.k6.io/public/crocodiles/${randomID}`; + const res = await http.asyncRequest('GET', url); + + check(res, { 'status is 200': (r) => r.status === 200 }); + + await redisClient.hincrby('k6_crocodile_fetched', url, 1); +} + +export async function teardown() { + await redisClient.del('crocodile_ids'); +} + +export function handleSummary(data) { + redisClient + .hgetall('k6_crocodile_fetched') + .then((fetched) => Object.assign(data, { k6_crocodile_fetched: fetched })) + .then((data) => redisClient.set(`k6_report_${Date.now()}`, JSON.stringify(data))) + .then(() => redisClient.del('k6_crocodile_fetched')); + + return { + stdout: textSummary(data, { indent: ' ', enableColors: true }), + }; +} +``` + +{{< /code >}} + +## API + +### Key value methods + +| Method | Redis command | Description | +| :-------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------- | :-------------------------------------------------------------------------- | +| [`Client.set(key, value, expiration)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-set) | **[SET](https://redis.io/commands/set)** | Set `key` to hold `value`, with a time to live equal to `expiration`. | +| [`Client.get(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-get) | **[GET](https://redis.io/commands/get)** | Get the value of `key`. | +| [`Client.getSet(key, value)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-getset) | **[GETSET](https://redis.io/commands/getset)** | Atomically sets `key` to `value` and returns the old value stored at `key`. | +| [`Client.del(keys)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-del) | **[DEL](https://redis.io/commands/del)** | Removes the specified keys. | +| [`Client.getDel(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-getdel) | **[GETDEL](https://redis.io/commands/getdel)** | Get the value of `key` and delete the key. | +| [`Client.exists(keys)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-exists) | **[EXISTS](https://redis.io/commands/exists)** | Returns the number of `key` arguments that exist. | +| [`Client.incr(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-incr) | **[INCR](https://redis.io/commands/incr)** | Increments the number stored at `key` by one. | +| [`Client.incrBy(key, increment)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-incrby) | **[INCRBY](https://redis.io/commands/incrby)** | Increments the number stored at `key` by `increment`. | +| [`Client.decr(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-decr) | **[DECR](https://redis.io/commands/decr)** | Decrements the number stored at `key` by one. | +| [`Client.decrBy(key, decrement)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-decrby) | **[DECRBY](https://redis.io/commands/decrby)** | Decrements the number stored at `key` by `decrement`. | +| [`Client.randomKey()`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-randomkey) | **[RANDOMKEY](https://redis.io/commands/randomkey)** | Returns a random key's value. | +| [`Client.mget(keys)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-mget) | **[MGET](https://redis.io/commands/mget)** | Returns the values of all specified keys. | +| [`Client.expire(key, seconds)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-expire) | **[EXPIRE](https://redis.io/commands/expire)** | Sets a timeout on key, after which the key will automatically be deleted. | +| [`Client.ttl(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-ttl) | **[TTL](https://redis.io/commands/ttl)** | Returns the remaining time to live of a key that has a timeout. | +| [`Client.persist(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-persist) | **[PERSIST](https://redis.io/commands/persist)** | Removes the existing timeout on key. | + +### List methods + +| Method | Redis command | Description | +| :-------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------- | :------------------------------------------------------------------------------ | +| [`Client.lpush(key, values)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-lpush) | **[LPSUH](https://redis.io/commands/lpush)** | Inserts all the specified values at the head of the list stored at `key`. | +| [`Client.rpush(key, values)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-rpush) | **[RPUSH](https://redis.io/commands/rpush)** | Inserts all the specified values at the tail of the list stored at `key`. | +| [`Client.lpop(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-lpop) | **[LPOP](https://redis.io/commands/lpop)** | Removes and returns the first element of the list stored at `key`. | +| [`Client.rpop(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-rpop) | **[RPOP](https://redis.io/commands/rpop)** | Removes and returns the last element of the list stored at `key`. | +| [`Client.lrange(key, start, stop)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-lrange) | **[LRANGE](https://redis.io/commands/lrange)** | Returns the specified elements of the list stored at `key`. | +| [`Client.lindex(key, start, stop)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-lindex) | **[LINDEX](https://redis.io/commands/lindex)** | Returns the specified element of the list stored at `key`. | +| [`Client.lset(key, index, element)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-lset) | **[LSET](https://redis.io/commands/lset)** | Sets the list element at `index` to `element`. | +| [`Client.lrem(key, count, value)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-lrem) | **[LREM](https://redis.io/commands/lrem)** | Removes the first `count` occurrences of `value` from the list stored at `key`. | +| [`Client.llen(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-llen) | **[LLEN](https://redis.io/commands/llen)** | Returns the length of the list stored at `key`. | + +### Hash methods + +| Method | Redis command | Description | +| :--------------------------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------- | :--------------------------------------------------------------------------------------------------- | +| [`Client.hset(key, field, value)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-hset) | **[HSET](https://redis.io/commands/hset)** | Sets the specified field in the hash stored at `key` to `value`. | +| [`Client.hsetnx(key, field, value)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-hsetnx) | **[HSETNX](https://redis.io/commands/hsetnx)** | Sets the specified field in the hash stored at `key` to `value`, only if `field` does not yet exist. | +| [`Client.hget(key, field)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-hget) | **[HGET](https://redis.io/commands/hget)** | Returns the value associated with `field` in the hash stored at `key`. | +| [`Client.hdel(key, fields)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-hdel) | **[HDEL](https://redis.io/commands/hdel)** | Deletes the specified fields from the hash stored at `key`. | +| [`Client.hgetall(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-hgetall) | **[HGETALL](https://redis.io/commands/hgetall)** | Returns all fields and values of the hash stored at `key`. | +| [`Client.hkeys(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-hkeys) | **[HKEYS](https://redis.io/commands/hkeys)** | Returns all fields of the hash stored at `key`. | +| [`Client.hvals(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-hvals) | **[HVALS](https://redis.io/commands/hvals)** | Returns all values of the hash stored at `key`. | +| [`Client.hlen(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-hlen) | **[HLEN](https://redis.io/commands/hlen)** | Returns the number of fields in the hash stored at `key`. | +| [`Client.hincrby(key, field, increment)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-hincrby) | **[HINCRBY](https://redis.io/commands/hincrby)** | Increments the integer value of `field` in the hash stored at `key` by `increment`. | + +### Set methods + +| Method | Redis command | Description | +| :--------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------- | :----------------------------------------------------------------------- | +| [`Client.sadd(key, members)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-sadd) | **[SADD](https://redis.io/commands/sadd)** | Adds the specified members to the set stored at `key`. | +| [`Client.srem(key, members)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-srem) | **[SREM](https://redis.io/commands/srem)** | Removes the specified members from the set stored at `key`. | +| [`Client.sismember(key, member)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-sismember) | **[SISMEMBER](https://redis.io/commands/sismember)** | Returns if member is a member of the set stored at `key`. | +| [`Client.smembers(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-smembers) | **[SMEMBERS](https://redis.io/commands/smembers)** | Returns all the members of the set values stored at `keys`. | +| [`Client.srandmember(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-srandmember) | **[SRANDMEMBER](https://redis.io/commands/srandmember)** | Returns a random element from the set value stored at `key`. | +| [`Client.spop(key)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-spop) | **[SPOP](https://redis.io/commands/spop)** | Removes and returns a random element from the set value stored at `key`. | + +### Miscellaneous + +| Method | Description | +| :--------------------------------------------------------------------------------------------------------------------------------------------- | :---------------------------------- | +| [`Client.sendCommand(command, args)`](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client/client-sendcommand) | Send a command to the Redis server. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-decr.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-decr.md new file mode 100644 index 000000000..685dea8f2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-decr.md @@ -0,0 +1,47 @@ +--- +title: 'Client.decr(key)' +description: 'Decrements the number stored at `key` by one.' +--- + +# Client.decr(key) + +Decrements the number stored at `key` by one. If the key does not exist, it is set to zero before performing the operation. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :-------------------- | +| `key` | string | the key to decrement. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :---------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the value of `key` after the decrement. | If the key contains a value of the wrong type, or contains a string that cannot be represented as an integer, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.set('mykey', 10, 0); + + let value; + value = await redisClient.incr('mykey'); + value = await redisClient.incrBy('mykey', value); + value = await redisClient.decrBy('mykey', value); + value = await redisClient.decr('mykey'); + + if (value !== -1) { + throw new Error('mykey should have been -1'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-decrby.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-decrby.md new file mode 100644 index 000000000..65b20088e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-decrby.md @@ -0,0 +1,43 @@ +--- +title: 'Client.decrBy(key, decrement)' +description: 'Decrements the number stored at `key` by `decrement`.' +--- + +# Client.decrBy(key, decrement) + +Decrements the number stored at `key` by `decrement`. If the key does not exist, it is set to zero before performing the operation. + +### Parameters + +| Parameter | Type | Description | +| :---------- | :----- | :------------------------- | +| `key` | string | the key to decrement. | +| `decrement` | number | the amount to decrement by | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :---------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the value of `key` after the decrement. | If the key contains a value of the wrong type, or contains a string that cannot be represented as an integer, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.set('mykey', 10, 0); + + const value = await redisClient.decrBy('mykey', 2); + if (value !== 8) { + throw new Error('mykey should have been 8'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-del.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-del.md new file mode 100644 index 000000000..92d2e61b0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-del.md @@ -0,0 +1,47 @@ +--- +title: 'Client.del(keys)' +description: '' +--- + +# Client.del(keys) + +Removes the specified keys. A key is ignored if it does not exist. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :------- | :------------------ | +| `keys` | string[] | the keys to delete. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :---------------------------------------------------------------------------- | :------------ | +| `Promise` | On success, the promise resolves with the `number` of keys that were removed. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.set('mykey', 'myvalue', 0); + + const exists = await redisClient.exists('mykey'); + if (exists === false) { + throw new Error('mykey should exist'); + } + + const value = await redisClient.get('mykey'); + console.log(`set key 'mykey' to value: ${value}`); + + await redisClient.del('mykey'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-exists.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-exists.md new file mode 100644 index 000000000..f568447de --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-exists.md @@ -0,0 +1,49 @@ +--- +title: 'Client.exists(keys)' +description: 'Returns the number of `key` arguments that exist.' +--- + +# Client.exists(keys) + +Returns the number of `key` arguments that exist. Note that if the same existing key is mentioned in the argument multiple times, it will be counted multiple times. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :------- | :---------------------------------- | +| `keys` | string[] | the keys to check the existence of. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :------------------------------------------------------------------------------------------------------- | :------------ | +| `Promise` | On success, the promise resolves with the `number` of keys that exist from those specified as arguments. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + let exists = await redisClient.exists('mykey'); + if (exists === true) { + throw new Error('mykey should not exist'); + } + + await redisClient.set('mykey', 'myvalue', 0); + + exists = await redisClient.exists('mykey'); + if (exists === false) { + throw new Error('mykey should exist'); + } + + await redisClient.del('mykey'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-expire.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-expire.md new file mode 100644 index 000000000..dff437a70 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-expire.md @@ -0,0 +1,44 @@ +--- +title: 'Client.expire(key, seconds)' +description: 'Sets an expiration date (a timeout) on the key `key`.' +--- + +# Client.expire(key, seconds) + +Sets a timeout on key, after which the key will automatically be deleted. Note that calling Expire with a non-positive timeout will result in the key being deleted rather than expired. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :--------------------------------------------- | +| `key` | string | the key to set the expiration of. | +| `seconds` | number | the value in seconds to set the expiration to. | + +### Returns + +| Type | Resolves with | Rejected when | +| :----------------- | :---------------------------------------------------------------------------------------------------------- | :------------ | +| `Promise` | On success, the promise resolves with `true` if the timeout was set, and `false` if the timeout wasn't set. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.set('mykey', 'myvalue', 10); + await redisClient.expire('mykey', 100); + + const ttl = await redisClient.ttl('mykey'); + if (ttl <= 10 || ttl >= 100) { + throw new Error('mykey should have a ttl of 10 <= x < 100'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-get.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-get.md new file mode 100644 index 000000000..ce1420be9 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-get.md @@ -0,0 +1,47 @@ +--- +title: 'Client.get(key)' +description: 'Get the value of `key`.' +--- + +# Client.get(key) + +Get the key's value. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :------------------------- | +| `key` | string | the name of the key to get | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :------------------------------------------------------------ | :---------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the value of the `key`. | If the key does not exist, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.set('mykey', 'myvalue', 0); + + const exists = await redisClient.exists('mykey'); + if (exists === false) { + throw new Error('mykey should exist'); + } + + const value = await redisClient.get('mykey'); + console.log(`set key 'mykey' to value: ${value}`); + + await redisClient.del('mykey'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-getdel.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-getdel.md new file mode 100644 index 000000000..9326ab9ad --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-getdel.md @@ -0,0 +1,43 @@ +--- +title: 'Client.getDel(key)' +description: 'Get the value of `key` and delete the key.' +--- + +# Client.getDel(key) + +Get the value of `key` and delete the key. This functionality is similar to `get`, except for the fact that it also deletes the key on success. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :------------------------ | +| `key` | string | the key to get and delete | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :-------------------------------------------------------- | :---------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the value of `key`. | If the key does not exist, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.set('mykey', 'oldvalue', 0); + let value = await redisClient.getSet('mykey', 'newvalue'); + + value = await redisClient.getDel('mykey'); + if (value !== 'newvalue') { + throw new Error('mykey should have been newvalue'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-getset.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-getset.md new file mode 100644 index 000000000..07cbc08e0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-getset.md @@ -0,0 +1,40 @@ +--- +title: 'Client.getSet(key, value)' +description: 'Atomically sets `key` to `value` and returns the old value stored at `key`.' +--- + +# Client.getSet(key, value) + +Atomically sets `key` to `value` and returns the value previously stored at `key`. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :------------------------- | :--------------------- | +| `key` | string | the key to get and set | +| `value` | string, number, or boolean | the value to set | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the old value stored at `key`. | If `key` does not exist, or does not hold a string value, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.set('mykey', 'oldvalue', 0); + await redisClient.getSet('mykey', 'newvalue'); + await redisClient.getDel('mykey'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hdel.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hdel.md new file mode 100644 index 000000000..781877bbb --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hdel.md @@ -0,0 +1,40 @@ +--- +title: 'Client.hdel(key, fields)' +description: 'Deletes fields from the hash stored at `key`.' +--- + +# Client.hdel(key, fields) + +Deletes the specified fields from the hash stored at `key`. The number of fields that were removed from the hash is returned on resolution (non including non existing fields). + +### Parameters + +| Parameter | Type | Description | +| :-------- | :------- | :-------------------------------------------- | +| `key` | string | key holding the hash to delete the fields of. | +| `fields` | string[] | fields to delete from the hash. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :--------------------------------------------------------------------------------------------------------------------------------------------- | :------------ | +| `Promise` | On success, the promise resolves with the number of fields that were removed from the hash, not including specified, but non existing, fields. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.hset('myhash', 'myfield', 'myvalue'); + await redisClient.hget('myhash', 'myfield'); + await redisClient.hdel('myhash', 'myfield'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hget.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hget.md new file mode 100644 index 000000000..0839f1e9b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hget.md @@ -0,0 +1,40 @@ +--- +title: 'Client.hget(key, field)' +description: 'Returns the value of field in the hash stored at `key`.' +--- + +# Client.hget(key, field) + +Returns the value associated with `field` in the hash stored at `key`. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :---------------------------------------- | +| `key` | string | key holding the hash to get the field of. | +| `field` | string | field to get from the hash. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :----------------------------------------------------------------------- | :----------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the value associated with `field`. | If the hash does not exist, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.hset('myhash', 'myfield', 'myvalue'); + await redisClient.hget('myhash', 'myfield'); + await redisClient.hdel('myhash', 'myfield'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hgetall.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hgetall.md new file mode 100644 index 000000000..e9a3aedcc --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hgetall.md @@ -0,0 +1,40 @@ +--- +title: 'Client.hgetall(key)' +description: 'Returns all fields and values of the hash stored at `key`.' +--- + +# Client.hgetall(key) + +Returns all fields and values of the hash stored at `key`. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :----------------------------------------- | +| `key` | string | key holding the hash to get the fields of. | + +### Returns + +| Type | Resolves with | Rejected when | +| :----------------------------- | :-------------------------------------------------------------------------------------------- | :------------ | +| `Promise<[key: string]string>` | On success, the promise resolves with the list of fields and their values stored in the hash. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.hset('myhash', 'myfield', 'myvalue'); + await redisClient.hset('myhash', 'myotherfield', 'myothervalue'); + const object = await redisClient.hgetall('myhash'); + console.log(`myhash has key:value pairs ${JSON.stringify(object)}`); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hincrby.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hincrby.md new file mode 100644 index 000000000..54a95bafa --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hincrby.md @@ -0,0 +1,40 @@ +--- +title: 'Client.hincrby(key, field, increment)' +description: 'Increments the value of a hash field by the given number.' +--- + +# Client.hincrby(key, field, increment) + +Increments the integer value of `field` in the hash stored at `key` by `increment`. If `key` does not exist, a new key holding a hash is created. If `field` does not exist the value is set to 0 before the operation is set to 0 before the operation is performed. + +### Parameters + +| Parameter | Type | Description | +| :---------- | :----- | :---------------------------------------------- | +| `key` | string | key holding the hash to increment the field of. | +| `field` | string | field to increment in the hash. | +| `increment` | number | amount to increment the field by. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :---------------------------------------------------------------------------------------- | :------------ | +| `Promise` | On success, the promise resolves with the value at `field` after the increment operation. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.hset('myhash', 'myfield', 10); + await redisClient.hincrby('myhash', 'myfield', 20); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hkeys.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hkeys.md new file mode 100644 index 000000000..2dbe8ec1b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hkeys.md @@ -0,0 +1,45 @@ +--- +title: 'Client.hkeys(key)' +description: 'Returns all fields of the hash stored at `key`.' +--- + +# Client.hkeys(key) + +Returns all fields of the hash stored at `key`. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :----------------------------------------- | +| `key` | string | key holding the hash to get the fields of. | + +### Returns + +| Type | Resolves with | Rejected when | +| :------------------ | :-------------------------------------------------------------------- | :----------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the list of fields in the hash. | If the hash does not exist, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.hset('myhash', 'myfield', 'myvalue'); + await redisClient.hset('myhash', 'myotherfield', 'myothervalue'); + + const keys = await redisClient.hkeys('myhash'); + if (keys.length !== 2) { + throw new Error('myhash should have 2 keys'); + } + + console.log(`myhash has keys ${keys}`); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hlen.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hlen.md new file mode 100644 index 000000000..b6d8e9b00 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hlen.md @@ -0,0 +1,39 @@ +--- +title: 'Client.hlen(key)' +description: 'Returns the number of fields in the hash stored at `key`.' +--- + +# Client.hlen(key) + +Returns the number of fields in the hash stored at `key`. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :----------------------------------------- | +| `key` | string | key holding the hash to get the fields of. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :---------------------------------------------------------------------- | :----------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the number of fields in the hash. | If the hash does not exist, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.hset('myhash', 'myfield', 10); + await redisClient.hset('myhash', 'myotherfield', 20); + await redisClient.hlen('myhash'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hset.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hset.md new file mode 100644 index 000000000..491e199e5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hset.md @@ -0,0 +1,41 @@ +--- +title: 'Client.hset(key, field, value)' +description: 'Sets the value of field in the hash stored at `key` to `value`.' +--- + +# Client.hset(key, field, value) + +Sets the specified field in the hash stored at `key` to `value`. If the `key` does not exist, a new key holding a hash is created. If `field` already exists in the hash, it is overwritten. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :---------------------------------------- | +| `key` | string | key holding the hash to set the field of. | +| `field` | string | field to set in the hash. | +| `value` | string | value to set the field to. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :-------------------------------------------------------------------------- | :----------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the number of fields that were added. | If the hash does not exist, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.hset('myhash', 'myfield', 'myvalue'); + await redisClient.hget('myhash', 'myfield'); + await redisClient.hdel('myhash', 'myfield'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hsetnx.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hsetnx.md new file mode 100644 index 000000000..1bdc8784a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hsetnx.md @@ -0,0 +1,45 @@ +--- +title: 'Client.hsetnx(key, field, value)' +description: 'Sets the value of field in the hash stored at `key` to `value` only if field does not exist in the hash.' +--- + +# Client.hsetnx(key, field, value) + +Sets the specified field in the hash stored at `key` to `value`, only if `field` does not yet exist. If `key` does not exist, a new key holding a hash is created. If `field` already exists, this operation has no effect. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :---------------------------------------- | +| `key` | string | key holding the hash to set the field of. | +| `field` | string | field to set in the hash. | +| `value` | string | value to set the field to. | + +### Returns + +| Type | Resolves with | Rejected when | +| :----------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :------------ | +| `Promise` | On success, the promise resolves with `1` if `field` is a new field in the hash and value was set, and with `0` if `field` already exists in the hash and no operation was performed. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.hsetnx('myhash', 'myfield', 'myvalue'); + await redisClient.hsetnx('myhash', 'myotherfield', 'myothervalue'); + + const set = await redisClient.hsetnx('myhash', 'myfield', 'mynewvalue'); + if (set === true) { + throw new Error('hsetnx should have failed on existing field'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hvals.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hvals.md new file mode 100644 index 000000000..7fd8dc368 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-hvals.md @@ -0,0 +1,45 @@ +--- +title: 'Client.hvals(key)' +description: 'Returns all values of the hash stored at `key`.' +--- + +# Client.hvals(key) + +Returns all values of the hash stored at `key`. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :----------------------------------------- | +| `key` | string | key holding the hash to get the fields of. | + +### Returns + +| Type | Resolves with | Rejected when | +| :------------------ | :-------------------------------------------------------------------- | :----------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the list of values in the hash. | If the hash does not exist, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.hset('myhash', 'myfield', 'myvalue'); + await redisClient.hset('myhash', 'myotherfield', 'myothervalue'); + + const values = await redisClient.hvals('myhash'); + if (values.length !== 2) { + throw new Error('myhash should have 2 values'); + } + + console.log(`myhash has values ${values}`); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-incr.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-incr.md new file mode 100644 index 000000000..e8f304cb9 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-incr.md @@ -0,0 +1,45 @@ +--- +title: 'Client.incr(key)' +description: 'Increments the number stored at `key` by one.' +--- + +# Client.incr(key) + +Increments the number stored at `key` by one. If the key does not exist, it is set to zero before performing the operation. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :------------------- | +| `key` | string | the key to increment | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :---------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the value of `key` after the increment. | If the key contains a value of the wrong type, or contains a string that cannot be represented as an integer, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.set('mykey', 10, 0); + + let value = await redisClient.incr('mykey'); + value = await redisClient.incrBy('mykey', value); + value = await redisClient.decrBy('mykey', value); + value = await redisClient.decr('mykey'); + if (value !== -1) { + throw new Error('mykey should have been -1'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-incrby.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-incrby.md new file mode 100644 index 000000000..f94fb6a04 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-incrby.md @@ -0,0 +1,46 @@ +--- +title: 'Client.incrBy(key, increment)' +description: 'Increments the number stored at `key` by `increment`.' +--- + +# Client.incrBy(key, increment) + +Increments the number stored at `key` by `increment`. If the key does not exist, it is set to zero before performing the operation. + +### Parameters + +| Parameter | Type | Description | +| :---------- | :----- | :------------------------- | +| `key` | string | the key to increment | +| `increment` | number | the amount to increment by | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :---------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the value of `key` after the increment. | If the key contains a value of the wrong type, or contains a string that cannot be represented as an integer, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.set('mykey', 10, 0); + + let value = await redisClient.incr('mykey'); + value = await redisClient.incrBy('mykey', value); + value = await redisClient.decrBy('mykey', value); + value = await redisClient.decr('mykey'); + if (value !== -1) { + throw new Error('mykey should have been -1'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lindex.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lindex.md new file mode 100644 index 000000000..232a6153d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lindex.md @@ -0,0 +1,46 @@ +--- +title: 'Client.lindex(key)' +description: 'Returns the element at index `index` of the list stored at `key`.' +--- + +# Client.lindex(key) + +Returns the specified element of the list stored at `key`. The index is zero-based. Negative indices can be used to designate elements starting at the tail of the list. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :------------------------------------------ | +| `key` | string | key holding the list to get the element of. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :----------------------------------------------------------- | :------------------------------------------------------------------------------------------------ | +| `Promise` | On success, the promise resolves with the requested element. | If the list does not exist, or the index is out of bounds, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.rpush('mylist', 'first'); + await redisClient.rpush('mylist', 'second'); + await redisClient.rpush('mylist', 'third'); + + const item = await redisClient.lindex('mylist', 0); + if (item !== 'first') { + throw new Error('lindex operation should have returned first'); + } + + await redisClient.lrange('mylist', 1, 2); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-llen.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-llen.md new file mode 100644 index 000000000..e17856323 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-llen.md @@ -0,0 +1,46 @@ +--- +title: 'Client.llen(key)' +description: 'Returns the length of the list stored at `key`.' +--- + +# Client.llen(key) + +Returns the length of the list stored at `key`. If `key` does not exist, it is interpreted as an empty list and 0 is returned. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :----------------------------------------- | +| `key` | string | key holding the list to get the length of. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :--------------------------------------------------------------------- | :----------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the length of the list at `key`. | If the list does not exist, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default function () { + redisClient + .rpush('mylist', 'first') + .then((_) => redisClient.rpush('mylist', 'second')) + .then((_) => redisClient.rpush('mylist', 'third')) + .then((_) => redisClient.llen('mylist')) + .then((length) => { + if (length !== 3) { + throw new Error('llen operations should have returned 3'); + } + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lpop.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lpop.md new file mode 100644 index 000000000..cb1889518 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lpop.md @@ -0,0 +1,42 @@ +--- +title: 'Client.lpop(key)' +description: 'Removes and returns the first element of the list stored at `key`.' +--- + +# Client.lpop(key) + +Removes and returns the first element of the list stored at `key`. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :------------------------------------- | +| `key` | string | key holding the list to left pop from. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :-------------------------------------------------------------------- | :----------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the value of the first element. | If the list does not exist, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.lpush('mylist', 'first'); + await redisClient.rpush('mylist', 'second'); + + const item = await redisClient.lpop('mylist'); + await redisClient.rpush('mylist', item); + await redisClient.rpop('mylist'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lpush.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lpush.md new file mode 100644 index 000000000..b7532b05a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lpush.md @@ -0,0 +1,43 @@ +--- +title: 'Client.lpush(key, values)' +description: 'Adds the string `value` to the left of the list stored at `key`.' +--- + +# Client.lpush(key, values) + +Inserts all the specified values at the head of the list stored at `key`. If `key` does not exist, it is created as empty list before performing the push operations. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :------------------------------------------------ | :------------------------------------ | +| `key` | string | key holding the list to left push to. | +| `values` | a variadic array of strings, numbers, or booleans | values to push to the list. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :--------------------------------------------------------------------------- | :---------------------------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the length of the list after the push. | When `key` holds a value that is not a list, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.lpush('mylist', 'first'); + await redisClient.rpush('mylist', 'second'); + + let item = await redisClient.lpop('mylist'); + item = redisClient.rpush('mylist', item); + item = redisClient.rpop('mylist'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lrange.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lrange.md new file mode 100644 index 000000000..983b7c04c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lrange.md @@ -0,0 +1,48 @@ +--- +title: 'Client.lrange(key, start, stop)' +description: 'Returns the specified elements of the list stored at `key`.' +--- + +# Client.lrange(key, start, stop) + +Returns the specified elements of the list stored at `key`. The offsets start and stop are zero-based indexes. These offsets can be negative numbers, where they indicate offsets starting at the end of the list. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :---------------------------------------- | +| `key` | string | key holding the list to get the range of. | +| `start` | number | index of the first element to return. | +| `stop` | number | index of the last element to return. | + +### Returns + +| Type | Resolves with | Rejected when | +| :------------------ | :--------------------------------------------------------------------------------- | :------------ | +| `Promise` | On success, the promise resolves with the list of elements in the specified range. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.rpush('mylist', 'first'); + await redisClient.rpush('mylist', 'second'); + await redisClient.rpush('mylist', 'third'); + + const item = redisClient.lindex('mylist', 0); + if (item !== 'first') { + throw new Error('lindex operation should have returned first'); + } + + await redisClient.lrange('mylist', 1, 2); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lrem.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lrem.md new file mode 100644 index 000000000..ff878f210 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lrem.md @@ -0,0 +1,50 @@ +--- +title: 'Client.lrem(key, count, value)' +description: 'Removes the first count occurrences of elements equal to value from the list stored at `key`.' +--- + +# Client.lrem(key, count, value) + +Removes the first `count` occurrences of `value` from the list stored at `key`. If `count` is positive, elements are removed from the beginning of the list. If `count` is negative, elements are removed from the end of the list. If `count` is zero, all elements matching `value` are removed. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :---------------------------------------------- | +| `key` | string | key holding the list to remove the elements of. | +| `count` | number | number of elements to remove. | +| `value` | string | value to remove from the list. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :-------------------------------------------------------------------- | :----------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the number of removed elements. | If the list does not exist, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.rpush('mylist', 'first'); + await redisClient.rpush('mylist', 'second'); + await redisClient.rpush('mylist', 'first'); + await redisClient.rpush('mylist', 'second'); + await redisClient.lrem('mylist', 0, 'second'); + + const length = await redisClient.lrem('mylist', 1, 'first'); + if (length !== 1) { + throw new Error('lrem operations should have left 1 item behind'); + } + + await redisClient.lpop('mylist'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lset.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lset.md new file mode 100644 index 000000000..78d4bd02d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-lset.md @@ -0,0 +1,43 @@ +--- +title: 'Client.lset(key, index, element)' +description: 'Sets the list element at index `index` of the list stored at `key` to `value`.' +--- + +# Client.lset(key, index, element) + +Sets the list element at `index` to `element`. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :------------------------------------------ | +| `key` | string | key holding the list to set the element of. | +| `index` | number | index of the element to set. | +| `element` | string | value to set the element to. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :------------------------------------------ | :------------------------------------------------------------------------------------------------ | +| `Promise` | On success, the promise resolves with `OK`. | If the list does not exist, or the index is out of bounds, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.rpush('mylist', 'first'); + await redisClient.rpush('mylist', 'second'); + await redisClient.rpush('mylist', 'third'); + await redisClient.lset('mylist', 0, 1); + await redisClient.lset('mylist', 1, 2); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-mget.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-mget.md new file mode 100644 index 000000000..122d07978 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-mget.md @@ -0,0 +1,42 @@ +--- +title: 'Client.mget(keys)' +description: 'Returns the values of all specified keys.' +--- + +# Client.mget(keys) + +Returns the values of all specified keys. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :------- | :----------------------------- | +| `keys` | string[] | the keys to get the values of. | + +### Returns + +| Type | Resolves with | Rejected when | +| :--------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------ | +| `Promise` | On success, the promise resolves with the list of values at the specified keys. For every key that does not hold a string value, or does not exist, the value `null` will be returned. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.set('first', 1, 0); + await redisClient.set('second', 2, 0); + await redisClient.set('third', 3, 0); + + const values = await redisClient.mget('first', 'second', 'third'); + console.log(`set values are: ${values}`); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-persist.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-persist.md new file mode 100644 index 000000000..a49ede209 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-persist.md @@ -0,0 +1,45 @@ +--- +title: 'Client.persist(key)' +description: 'Remove the expiration from a key.' +--- + +# Client.persist(key) + +Removes the existing timeout on `key`. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :-------------------------------- | +| `key` | string | the key to remove the timeout of. | + +### Returns + +| Type | Resolves with | Rejected when | +| :----------------- | :------------------------------------------------------------------------------------------ | :------------ | +| `Promise` | On success, the promise resolves with `true` if the timeout was removed, `false` otherwise. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.set('mykey', 'myvalue', 10); + await redisClient.expire('mykey', 100); + + const ttl = await redisClient.ttl('mykey'); + if (ttl <= 10) { + throw new Error('mykey should have a ttl of 10 <= x < 100'); + } + + await redisClient.persist('mykey', 100); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-randomkey.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-randomkey.md new file mode 100644 index 000000000..a72f7613b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-randomkey.md @@ -0,0 +1,36 @@ +--- +title: 'Client.randomKey()' +description: 'Returns a random key from the keyspace.' +--- + +# Client.randomKey() + +Returns a random key. + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :--------------------------------------------------------- | :--------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the random key name. | If the database is empty, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.set('first', 1, 0); + await redisClient.set('second', 2, 0); + await redisClient.set('third', 3, 0); + + const key = await redisClient.randomKey(); + console.log(`picked random key is: ${key}`); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-rpop.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-rpop.md new file mode 100644 index 000000000..4e72fc98d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-rpop.md @@ -0,0 +1,42 @@ +--- +title: 'Client.rpop(key)' +description: 'Removes and returns the last element of the list stored at `key`.' +--- + +# Client.rpop(key) + +Removes and returns the last element of the list stored at `key`. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :-------------------------------------- | +| `key` | string | key holding the list to right pop from. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :-------------------------------------------------------------------- | :----------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the value of the first element. | If the list does not exist, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.lpush('mylist', 'first'); + await redisClient.rpush('mylist', 'second'); + + const item = await redisClient.lpop('mylist'); + await redisClient.rpush('mylist', item); + await redisClient.rpop('mylist'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-rpush.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-rpush.md new file mode 100644 index 000000000..0876a14da --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-rpush.md @@ -0,0 +1,43 @@ +--- +title: 'Client.rpush(key, values)' +description: 'Adds the string `value` to the right of the list stored at `key`.' +--- + +# Client.rpush(key, values) + +Inserts all the specified values at the tail of the list stored at `key`. If `key` does not exist, it is created as empty list before performing the push operation. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :------------------------------------------------ | :------------------------------------- | +| `key` | string | key holding the list to right push to. | +| `values` | a variadic array of strings, numbers, or booleans | values to push to the list. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the length of the list after the push operation. | When `key` holds a value that is not a list, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.lpush('mylist', 'first'); + await redisClient.rpush('mylist', 'second'); + + const item = await redisClient.lpop('mylist'); + await redisClient.rpush('mylist', item); + await redisClient.rpop('mylist'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-sadd.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-sadd.md new file mode 100644 index 000000000..90bb2a0e7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-sadd.md @@ -0,0 +1,44 @@ +--- +title: 'Client.sadd(key, members)' +description: 'Adds the specified members to the set stored at `key`.' +--- + +# Client.sadd(key, members) + +Adds the specified members to the set stored at `key`. Specified members that are already a member of this set are ignored. If key does not exist, a new set is created before adding the specified members. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :------------------------------------------------ | :----------------------------------------- | +| `key` | string | key holding the set to add the members to. | +| `members` | a variadic array of strings, numbers, or booleans | members to add to the set. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :------------------------------------------------------------------------------------------------------------------------------------------ | :------------ | +| `Promise` | On success, the promise resolves with the number of elements that were added to the set, not including elements already present in the set. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.sadd('myset', 'foo'); + await redisClient.sadd('myset', 'bar'); + + const isit = await redisClient.sismember('myset', 'foo'); + if (isit === false) { + throw new Error('sismember should have returned true'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-sendcommand.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-sendcommand.md new file mode 100644 index 000000000..04f02bcd2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-sendcommand.md @@ -0,0 +1,41 @@ +--- +title: 'Client.sendCommand(command, args)' +description: 'Issue a command to the Redis server.' +--- + +# Client.sendCommand(command, args) + +In the event a Redis command you wish to use is not implemented yet, the `sendCommand` method can be used to send a custom commands to the server. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :------------------------------------------------ | :------------------------------------------------------------------------------------------------------------- | +| `command` | string | command name to issue to the Redis server, as described in [Redis' documentation](https://redis.io/commands/). | +| `args` | a variadic array of strings, numbers, or booleans | command arguments to pass to the Redis server. | + +### Returns + +| Type | Resolves with | Rejected when | +| :------------- | :------------------------------------------------------------------------------------------------------------------ | :------------ | +| `Promise` | On success, the promise resolves with string, number, or boolean result the server would reply to the command sent. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + const result = await redisClient.sendCommand('ECHO', 'Hello world'); + if (result !== 'Hello world') { + throw new Error('ECHO should have returned "Hello world"'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-set.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-set.md new file mode 100644 index 000000000..2e243c188 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-set.md @@ -0,0 +1,49 @@ +--- +title: 'Client.set(key, value, expiration)' +description: 'Set `key` to hold `value`, with a time to live equal to `expiration`.' +--- + +# Client.set(key, value, expiration) + +Set the value of a key, with a time to live equal to the expiration time parameter (in seconds). If the key already holds a value, it is overwritten. + +### Parameters + +| Parameter | Type | Description | +| :----------- | :------------------------- | :------------------------------------------------------------------ | +| `key` | string | the key to set | +| `value` | string, number, or boolean | the value to set | +| `expiration` | integer | the time to live in seconds. the `0` value indicates no expiration. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :------------------------------------------ | :----------------------------------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with 'OK'. | If the provided `value` is not of a supported type, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.set('mykey', 'myvalue', 0); + + const exists = await redisClient.exists('mykey'); + if (exists === false) { + throw new Error('mykey should exist'); + } + + const value = await redisClient.get('mykey'); + console.log(`set key 'mykey' to value: ${value}`); + + await redisClient.del('mykey'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-sismember.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-sismember.md new file mode 100644 index 000000000..ad45e1dc2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-sismember.md @@ -0,0 +1,44 @@ +--- +title: 'Client.sismember(key, member)' +description: 'Determines if a given value is a member of the set stored at `key`.' +--- + +# Client.sismember(key, member) + +Returns if member is a member of the set stored at `key`. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :------------------------- | :--------------------------------------------------------- | +| `key` | string | key holding the set to check if the member is a member of. | +| `member` | string, number, or boolean | member to check if is a member of the set. | + +### Returns + +| Type | Resolves with | Rejected when | +| :----------------- | :----------------------------------------------------------------------------------------------------- | :------------ | +| `Promise` | On success, the promise resolves with `true` if the element is a member of the set, `false` otherwise. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.sadd('myset', 'foo'); + await redisClient.sadd('myset', 'bar'); + + const isit = await redisClient.sismember('myset', 'foo'); + if (isit === false) { + throw new Error('sismember should have returned true'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-smembers.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-smembers.md new file mode 100644 index 000000000..e85b83f81 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-smembers.md @@ -0,0 +1,44 @@ +--- +title: 'Client.smembers(key)' +description: 'Returns all the members of the set stored at `key`.' +--- + +# Client.smembers(key) + +Returns all the members of the set values stored at `keys`. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :----------------------------------------- | +| `key` | string | key holding the set to get the members of. | + +### Returns + +| Type | Resolves with | Rejected when | +| :------------------ | :--------------------------------------------------------------------------------------- | :------------ | +| `Promise` | On success, the promise resolves with an array containing the values present in the set. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.sadd('myset', 'foo'); + await redisClient.sadd('myset', 'bar'); + await redisClient.sadd('myset', 'foo'); + + const members = await redisClient.smembers('myset'); + if (members.length !== 2) { + throw new Error('sismember should have length 2'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-spop.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-spop.md new file mode 100644 index 000000000..e5b933b7a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-spop.md @@ -0,0 +1,44 @@ +--- +title: 'Client.spop(key)' +description: 'Removes and returns a random member of the set stored at `key`.' +--- + +# Client.spop(key) + +Removes and returns a random element from the set value stored at `key`. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :------------------------------------------------------- | +| `key` | string | The key value holding the set to get a random member of. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :----------------------------------------------------------- | :--------------------------------------------------------------- | +| `Promise` | On success, the promise resolves to the returned set member. | If the set doesn't exist, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.sadd('myset', 'foo'); + await redisClient.sadd('myset', 'bar'); + await redisClient.spop('myset'); + + const members = await redisClient.smembers('myset'); + if (members.length !== 1) { + throw new Error('sismember should have length 1'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-srandmember.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-srandmember.md new file mode 100644 index 000000000..608191130 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-srandmember.md @@ -0,0 +1,43 @@ +--- +title: 'Client.srandmember(key)' +description: 'Returns a random member of the set stored at `key`.' +--- + +# Client.srandmember(key) + +Returns a random element from the set value stored at `key`. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :------------------------------------------------------- | +| `key` | string | The key value holding the set to get a random member of. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :---------------------------------------------------------------- | :--------------------------------------------------------------- | +| `Promise` | On success, the promise resolves with the selected random member. | If the set doesn't exist, the promise is rejected with an error. | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.sadd('myset', 'foo'); + await redisClient.sadd('myset', 'bar'); + + const randomMember = await redisClient.srandmember('myset'); + if (randomMember !== 'foo' && randomMember !== 'bar') { + throw new Error('randomMember should be equal to "foo" or "bar"'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-srem.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-srem.md new file mode 100644 index 000000000..b77a21390 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-srem.md @@ -0,0 +1,45 @@ +--- +title: 'Client.srem(key, members)' +description: 'Removes the specified members from the set stored at `key`.' +--- + +# Client.srem(key, members) + +Removes the specified members from the set stored at `key`. Specified members that are not a member of this set are ignored. If key does not exist, it is treated as an empty set and this command returns 0. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :------------------------------------------------ | :---------------------------------------------- | +| `key` | string | key holding the set to remove the members from. | +| `members` | a variadic array of strings, numbers, or booleans | members to remove from the set. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :----------------------------------------------------------------------------------------------------------------------------- | :------------ | +| `Promise` | On success, the promise resolves with the number of members that were remove from the set, not including non existing members. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.sadd('myset', 'foo'); + await redisClient.sadd('myset', 'bar'); + await redisClient.srem('myset', 'foo'); + + const members = await redisClient.smembers('myset'); + if (members.length !== 1) { + throw new Error('sismember should have length 1'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-ttl.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-ttl.md new file mode 100644 index 000000000..6fd438df5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/client/client-ttl.md @@ -0,0 +1,45 @@ +--- +title: 'Client.ttl(key)' +description: 'Returns the remaining time to live of a key.' +--- + +# Client.ttl(key) + +Returns the remaining time to live of a key that has a timeout. + +### Parameters + +| Parameter | Type | Description | +| :-------- | :----- | :------------------------- | +| key | string | the key to get the TTL of. | + +### Returns + +| Type | Resolves with | Rejected when | +| :---------------- | :-------------------------------------------------------------- | :------------ | +| `Promise` | On success, the promise resolves with the TTL value in seconds. | | + +### Example + +{{< code >}} + +```javascript +import redis from 'k6/experimental/redis'; + +// Instantiate a new redis client +const redisClient = new redis.Client('redis://localhost:6379'); + +export default async function () { + await redisClient.set('mykey', 'myvalue', 10); + await redisClient.expire('mykey', 100); + + const ttl = await redisClient.ttl('mykey'); + if (ttl <= 10) { + throw new Error('mykey should have a ttl of 10 <= x < 100'); + } + + await redisClient.persist('mykey', 100); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/redis-options.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/redis-options.md new file mode 100644 index 000000000..7b92c94a0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/redis/redis-options.md @@ -0,0 +1,66 @@ +--- +title: 'Options' +description: 'Options allow you to fine-tune how a Redis client behaves and interacts with a Redis server or cluster.' +weight: 20 +--- + +# Redis options + +You can configure the [Redis Client](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client) by using a Redis connection URL as demonstrated in the [client documentation](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/client#usage), or by using an [Options](#options) object to access more advanced configuration. + +## Options + +The configuration for the overall Redis client, including authentication and connection settings. + +| Option Name | Type | Description | +| ---------------- | --------------------------------------------------- | ------------------------------------------------------------------------------- | +| socket | [SocketOptions](#socket-connection-options) | The configuration of the connection socket used to connect to the Redis server. | +| username | String (optional) | Username for client authentication. | +| password | String (optional) | Password for client authentication. | +| clientName | String (optional) | Name for the client connection. | +| database | Number (optional) | Database ID to select after connecting. | +| masterName | String (optional) | Master instance name for Sentinel. | +| sentinelUsername | String (optional) | Username for Sentinel authentication. | +| sentinelPassword | String (optional) | Password for Sentinel authentication. | +| cluster | [ClusterOptions](#redis-cluster-options) (optional) | The configuration for Redis Cluster connections. | + +### Socket Connection Options + +Socket-level settings for connecting to a Redis server. + +| Option Name | Type | Description | +| ------------------ | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------- | +| host | String | IP address or hostname of the Redis server. | +| port | Number (optional) | Port number of the Redis server. | +| tls | [TLSOptions](#tls-configuration-options) (optional) | The configuration for TLS/SSL. | +| dialTimeout | Number (optional, default is _5_ (seconds)) | Timeout for establishing a connection, in seconds. | +| readTimeout | Number (optional, default is _3_ (seconds)) | Timeout for socket reads, in seconds. A value of `-1` disables the timeout. | +| writeTimeout | Number (optional, default is `readTimeout`) | Timeout for socket writes, in seconds. A value of `-1` disables the timeout. | +| poolSize | Number (optional, default is _10_ (per CPU)) | Number of socket connections in the pool per CPU. | +| minIdleConns | Number (optional, default is _0_ (idle connections are not closed by default)) | Minimum number of idle connections in the pool. | +| maxConnAge | Number (optional, default is _0_ (no maximum idle time)) | Maximum time before closing a connection. | +| poolTimeout | Number (optional, `readTimeout + 1`) | Timeout for acquiring a connection from the pool. | +| idleTimeout | Number (optional, `readTimeout + 1`) | Timeout for idle connections in the pool. | +| idleCheckFrequency | Number (optional, default is _1_ (minute)) | Frequency of idle connection checks, in minutes. A value of `-1` disables the checks. | + +#### TLS Configuration Options + +Options for establishing a secure TLS connection. + +| Option Name | Type | Description | +| ----------- | ---------------------- | --------------------------------------------------- | +| ca | ArrayBuffer[] | Array of CA certificates. | +| cert | ArrayBuffer (optional) | Client certificate for mutual TLS. | +| key | ArrayBuffer (optional) | Private key associated with the client certificate. | + +### Redis Cluster Options + +Options for behavior in a Redis Cluster setup. + +| Option Name | Type | Description | +| -------------- | --------------------------------------------------------- | ----------------------------------------------------------------------------- | +| maxRedirects | Number (optional, default is _3_ retries) | Maximum number of command redirects. | +| readOnly | Boolean (optional) | Enables read-only mode for replicas. | +| routeByLatency | Boolean (optional) | Route read commands by latency. | +| routeRandomly | Boolean (optional) | Random routing for read commands. | +| nodes | String[] or [SocketOptions](#socket-connection-options)[] | List of cluster nodes as URLs or [SocketOptions](#socket-connection-options). | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/_index.md new file mode 100644 index 000000000..9452ddb8c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/_index.md @@ -0,0 +1,128 @@ +--- +title: 'streams' +description: 'k6 streams experimental API' +weight: 10 +--- + +# streams + +{{< docs/shared source="k6" lookup="experimental-module.md" version="" >}} + +The k6 streams experimental module provides an implementation of the Streams API specification, providing a way to define and consume streams of data within your test scripts. It currently implements a subset of the full specification, and offers support for defining and consuming readable streams. + +## Concepts and usage + +Streaming involves breaking a resource that one wants to process or consume into smaller chunks, which can be processed or consumed incrementally. This is particularly useful when dealing with large files or data sources, as it allows you to work with the data in smaller, more manageable pieces. + +With the Streams API support in k6, you can start processing raw data with Javascript bit by bit, as soon as it's available, without needing to generate a full in-memory representation of the data. This helps to reduce memory usage and improve performance, especially when working with large data sets. + +## API Overview + +| Class | Description | +| ---------------------------------------------------------------------------------------------------------------- | ------------------------------------- | +| [ReadableStream](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestream) | Represents a readable stream of data. | + +## Example + +{{< code >}} + +```javascript +import { open } from 'k6/experimental/fs'; +import { ReadableStream } from 'k6/experimental/streams'; + +// Open a csv file containing the data to be read +const file = await open('./data.csv'); + +export default async function () { + let lineReaderState; + + // Define a ReadableStream that reads lines from the file + // and parses them into objects with name and color properties. + const fileLinesStream = new ReadableStream({ + // The start function is called when the readable stream is + // created. In here, you can connect to the data source + // and perform administrative tasks. + async start(controller) { + lineReaderState = { + buffer: new Uint8Array(1024), + remaining: '', + }; + }, + + // The pull function is called repeatedly to get data, while the + // internal high water mark is not reached. + async pull(controller) { + const line = await getNextLine(file, lineReaderState); + if (line === null) { + controller.close(); + return; + } + + const [name, color] = line.split(','); + controller.enqueue({ name, color }); + }, + }); + + // Obtain and lock a reader to the stream + const reader = fileLinesStream.getReader(); + + try { + // Read and process each item from the stream + while (true) { + const { done, value } = await reader.read(); + if (done) { + break; + } + + console.log(value); + } + } catch (error) { + console.error('Stream reading failed: ', error); + } +} + +// getNextLine reads the next line from the file and returns it. +// +// It reads the file in chunks and buffers the remaining data +// to handle partial lines. It returns null when there are no +// more lines to read. +async function getNextLine(file, state) { + while (true) { + if (state.remaining.includes('\n')) { + const lineEndIndex = state.remaining.indexOf('\n'); + const line = state.remaining.substring(0, lineEndIndex).trim(); + + state.remaining = state.remaining.substring(lineEndIndex + 1); + + if (line) { + return line; + } + } else { + const bytesRead = await file.read(state.buffer); + if (bytesRead === null) { + // EOF + + if (state.remaining) { + const finalLine = state.remaining.trim(); + + // Clear remaining to signal the end + state.remaining = ''; + + // Return the last non-empty line + return finalLine; + } + + // Indicate that there are no more lines to read + return null; + } + + state.remaining += String.fromCharCode.apply( + null, + new Uint8Array(state.buffer.slice(0, bytesRead)) + ); + } + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/CountQueuingStrategy.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/CountQueuingStrategy.md new file mode 100644 index 000000000..af07ff5f8 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/CountQueuingStrategy.md @@ -0,0 +1,65 @@ +--- +title: 'CountQueuingStrategy' +description: 'The CountQueuingStrategy interface of the Streams API represents a built-in queuing strategy that counts the number of chunks in the queue.' +weight: 300 +--- + +# CountQueuingStrategy + +The `CountQueuingStrategy` interface of the Streams API represents a built-in queuing strategy that counts the number of chunks in the queue. + +## Properties + +| Name | Type | Description | +| ------------- | ------ | -------------------------------------------------------- | +| highWaterMark | number | The maximum number of chunks that the queue can contain. | + +## Example + +{{< code >}} + +```javascript +import { CountQueuingStrategy, ReadableStream } from 'k6/experimental/streams'; +import { setTimeout } from 'k6/timers'; + +export default async function () { + // Define a number stream that emits numbers from 1 to 10 every second + const stream = numbersStream(); + + // We use the getReader method to create a reader and lock the stream to it + const reader = stream.getReader(); + + while (true) { + const { done, value } = await reader.read(); + if (done) break; + console.log(`received number ${value} from stream`); + } +} + +function numbersStream() { + let currentNumber = 0; + + // Using the CountQueuingStrategy to limit the number of chunks in the queue to 5 + const queuingStrategy = new CountQueuingStrategy({ highWaterMark: 5 }); + + return new ReadableStream( + { + start(controller) { + const fn = () => { + if (currentNumber < 10) { + controller.enqueue(++currentNumber); + setTimeout(fn, 1000); + return; + } + + controller.close(); + }; + setTimeout(fn, 1000); + }, + }, + queuingStrategy + ); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/_index.md new file mode 100644 index 000000000..382594095 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/_index.md @@ -0,0 +1,226 @@ +--- +title: 'ReadableStream' +description: 'ReadableStream represents a readable stream of data.' +weight: 10 +--- + +# ReadableStream + +The `ReadableStream` type represents a readable stream of data. + +## Constructing a ReadableStream + +The constructor creates a new `ReadableStream` object. + +It takes two _optional_ arguments: + +- `underlyingsource`: defines the underlying source of data. +- `queuingStrategy`: the queuing strategy to adopt. + +```javascript +import { ReadableStream } from 'k6/experimental/streams'; + +new ReadableStream( + { + start(controller) { + // Perform any setup tasks + }, + + pull(controller) { + // Fetch and queue data into the stream + }, + + cancel(reason) { + // Perform any cleanup tasks + }, + + type: 'default', + }, + { + highWaterMark: 1, + size(chunk) { + return 1; + }, + } +); +``` + +### Constructor arguments + +#### underlyingSource (optional) + +The `underlyingSource` argument is an object that defines the source of data for the stream. It can be an object with the following properties: + +- `start(controller)`: An **optional** function that is called when the stream is created. It can be used to perform any setup tasks. The content of this method is to be defined by the user. The `controller` parameter passed to this method is a [ReadableStreamDefaultController](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultcontroller) object. +- `pull(controller)`: An **optional** function that is called repeatedly to fetch and queue data into the stream, until it reaches its high water mark. If `pull()` returns a promise, it won't be called again until the promise is resolved. The `controller` parameter passed to this method is a [ReadableStreamDefaultController](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultcontroller) object. +- `cancel(reason)`: An **optional** function, defined by the user, that is called when the stream is canceled. It can be used to release access to the stream source and perform any cleanup tasks. The `reason` parameter passed to this method is an optional human-readable value that represents the reason for canceling the stream. +- `type`: An **optional** string that specifies the type of the underlying source. It can currently only receive the value `'default'` which is its default value. + +#### queuingStrategy argument (optional) + +The `queuingStrategy` argument is an object that defines the queuing strategy to adopt for the stream. It can be an object with the following properties: + +- `highWaterMark`: An **optional** number that represents the maximum number of chunks that the stream can hold in its internal queue. The default value is `1`. +- `size(chunk)`: An **optional** function that returns the size of the chunk passed as an argument. The default value is a function that returns `1`. + +Although you can define your own custom queueing strategy, the default behavior and recommended way to use the `ReadableStream` is to use a [CountQueuingStrategy](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestream/countqueuingstrategy) object. + +## Methods + +| Name | Description | +| ----------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------- | +| [cancel(reason)](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestream/cancel) | Closes the stream and signals a reason for the closure. | +| [getReader()](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestream/getreader) | Returns a `ReadableStreamDefaultReader` object. | + +## Examples + +The simplest illustrative example of using a `ReadableStream` is to create a stream of numbers. + +{{< code >}} + +```javascript +import { ReadableStream } from 'k6/experimental/streams'; +import { setTimeout } from 'k6/timers'; + +function numbersStream() { + let currentNumber = 0; + + return new ReadableStream({ + start(controller) { + const fn = () => { + if (currentNumber < 10) { + controller.enqueue(++currentNumber); + setTimeout(fn, 1000); + return; + } + + controller.close(); + }; + setTimeout(fn, 1000); + }, + }); +} + +export default async function () { + const stream = numbersStream(); + const reader = stream.getReader(); + + while (true) { + const { done, value } = await reader.read(); + if (done) break; + console.log(`received number ${value} from stream`); + } + + console.log('we are done'); +} +``` + +{{< /code >}} + +A much more useful illustration of defining a `ReadableStream` is to read lines from a file. + +{{< code >}} + +```javascript +import { open } from 'k6/experimental/fs'; +import { ReadableStream } from 'k6/experimental/streams'; + +// Open a csv file containing the data to be read +let file; +(async function () { + file = await open('./data.csv'); +})(); + +export default async function () { + let lineReaderState; + + // Define a ReadableStream that reads lines from the file + // and parses them into objects with name and color properties. + const fileLinesStream = new ReadableStream({ + // The start function is called when the readable stream is + // created. In here, you can connect to the data source + // and perform administrative tasks. + async start(controller) { + lineReaderState = { + buffer: new Uint8Array(1024), + remaining: '', + }; + }, + + // The pull function is called repeatedly to get data, while the + // internal high water mark is not reached. + async pull(controller) { + const line = await getNextLine(file, lineReaderState); + if (line === null) { + controller.close(); + return; + } + + const [name, color] = line.split(','); + controller.enqueue({ name, color }); + }, + }); + + // Obtain and lock a reader to the stream + const reader = fileLinesStream.getReader(); + + try { + // Read and process each item from the stream + while (true) { + const { done, value } = await reader.read(); + if (done) { + break; + } + + console.log(value); + } + } catch (error) { + console.error('Stream reading failed: ', error); + } +} + +// getNextLine reads the next line from the file and returns it. +// +// It reads the file in chunks and buffers the remaining data +// to handle partial lines. It returns null when there are no +// more lines to read. +async function getNextLine(file, state) { + while (true) { + if (state.remaining.includes('\n')) { + const lineEndIndex = state.remaining.indexOf('\n'); + const line = state.remaining.substring(0, lineEndIndex).trim(); + + state.remaining = state.remaining.substring(lineEndIndex + 1); + + if (line) { + return line; + } + } else { + const bytesRead = await file.read(state.buffer); + if (bytesRead === null) { + // EOF + + if (state.remaining) { + const finalLine = state.remaining.trim(); + + // Clear remaining to signal the end + state.remaining = ''; + + // Return the last non-empty line + return finalLine; + } + + // Indicate that there are no more lines to read + return null; + } + + state.remaining += String.fromCharCode.apply( + null, + new Uint8Array(state.buffer.slice(0, bytesRead)) + ); + } + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/cancel.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/cancel.md new file mode 100644 index 000000000..39aba3ec5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/cancel.md @@ -0,0 +1,74 @@ +--- +title: 'cancel(reason)' +description: 'Returns a Promise that resolves when the stream is canceled.' +weight: 100 +--- + +# cancel(reason) + +The `cancel()` method of the [ReadableStream](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestream) interface returns a `Promise` that resolves when the stream is canceled. + +Cancel is used when you've completely finished with the stream and don't need any more data from it, even if chunks are enqueued waiting to be read. That data is lost after `cancel` is called, and the stream is not readable any more. To close the stream without getting rid of enqueued chunks, use [ReadableStreamDefaultController.close()](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/close). + +## Arguments + +| Name | Type | Description | +| ------ | ---- | ------------------------------------------------------------------------------------- | +| reason | any | An optional human-readable value that represents the reason for canceling the stream. | + +## Return value + +A promise that resolves with the value `undefined` when the stream is canceled. + +## Exceptions + +| Exception | Description | +| --------- | ---------------------------------------- | +| TypeError | Thrown the stream is locked to a reader. | + +## Example + +{{< code >}} + +```javascript +import { ReadableStream } from 'k6/experimental/streams'; +import { setTimeout } from 'k6/timers'; + +export default async function () { + // Define a number stream that emits numbers from 1 to 10 every second + const stream = numbersStream(); + const reader = stream.getReader(); + + while (true) { + const { done, value } = await reader.read(); + if (done) break; + + if (value === 8) { + // Cancel the stream when the number is 8, any enqueued chunks are lost + await reader.cancel('cancelling the stream'); + break; + } + } +} + +function numbersStream() { + let currentNumber = 0; + + return new ReadableStream({ + start(controller) { + const fn = () => { + if (currentNumber < 10) { + controller.enqueue(++currentNumber); + setTimeout(fn, 1000); + return; + } + + controller.close(); + }; + setTimeout(fn, 1000); + }, + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/getreader.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/getreader.md new file mode 100644 index 000000000..c7d137ea2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestream/getreader.md @@ -0,0 +1,65 @@ +--- +title: 'getReader()' +description: 'Creates a reader and locks the stream to it.' +weight: 200 +--- + +# getReader() + +Creates a reader and locks the [stream](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestream) to it. While the stream is locked, no other reader can be acquired until this one is released. + +## Arguments + +The `getReader` method takes a single optional `options` argument, which is an object with the following properties: + +| Name | Type | Default value | Description | +| ---- | ------ | ------------- | --------------------------------------------------------------------------------------------------------- | +| mode | string | `undefined` | A property that specifies the type of reader to create. It currently only supports the `undefined` value. | + +## Return value + +A [ReadableStreamDefaultReader](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultreader/) object instance. + +## Example + +{{< code >}} + +```javascript +import { ReadableStream } from 'k6/experimental/streams'; +import { setTimeout } from 'k6/timers'; + +export default async function () { + // Define a number stream that emits numbers from 1 to 10 every second + const stream = numbersStream(); + + // We use the getReader method to create a reader and lock the stream to it + const reader = stream.getReader(); + + while (true) { + const { done, value } = await reader.read(); + if (done) break; + console.log(`received number ${value} from stream`); + } +} + +function numbersStream() { + let currentNumber = 0; + + return new ReadableStream({ + start(controller) { + const fn = () => { + if (currentNumber < 10) { + controller.enqueue(++currentNumber); + setTimeout(fn, 1000); + return; + } + + controller.close(); + }; + setTimeout(fn, 1000); + }, + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/_index.md new file mode 100644 index 000000000..63d921233 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/_index.md @@ -0,0 +1,17 @@ +--- +title: 'ReadableStreamDefaultController' +description: "ReadableStreamDefaultController allows to control a ReadableStream's state and internal queue." +weight: 30 +--- + +# ReadableStreamDefaultController + +The `ReadableStreamDefaultController` type allows you to control a `ReadableStream`'s state and internal queue. + +## Methods + +| Name | Description | +| ----------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- | +| [close()](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/close) | Closes the associated stream. | +| [enqueue(chunk)](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/enqueue) | Enqueues a chunk of data into the associated stream. | +| [error(reason)](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/error) | Causes the stream to become errored. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/close.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/close.md new file mode 100644 index 000000000..6c975cce9 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/close.md @@ -0,0 +1,61 @@ +--- +title: 'close()' +description: 'The close method closes the associated stream.' +weight: 20 +--- + +# close() + +The `close()` method of the [ReadableStreamDefaultController](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultcontroller) interface closes the associated stream. + +Readers can still read any previously enqueued chunks from the stream. Once those chunks are read, the stream closes, and no more data is available. + +## Exceptions + +| Exception | Description | +| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| TypeError | Thrown when the source object is not a [ReadableStreamDefaultController](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultcontroller). | + +## Example + +{{< code >}} + +```javascript +import { ReadableStream } from 'k6/experimental/streams'; +import { setTimeout } from 'k6/timers'; + +export default async function () { + let currentNumber = 0; + + const stream = new ReadableStream({ + start(controller) { + const fn = () => { + if (currentNumber % 8 == 0) { + // Close the stream when the number is divisible by 8 + controller.close(); + return; + } + + if (currentNumber < 10) { + controller.enqueue(++currentNumber); + setTimeout(fn, 1000); + return; + } + + controller.close(); + }; + setTimeout(fn, 1000); + }, + }); + + const reader = stream.getReader(); + + while (true) { + const { done, value } = await reader.read(); + if (done) break; + console.log(`received number ${value} from stream`); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/enqueue.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/enqueue.md new file mode 100644 index 000000000..3a4ca9c94 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/enqueue.md @@ -0,0 +1,126 @@ +--- +title: 'enqueue(chunk)' +description: 'The enqueue method of the ReadableStreamDefaultController interface enqueues a chunk of data into the associated stream.' +weight: 30 +--- + +# enqueue(chunk) + +The `enqueue()` method of the [ReadableStreamDefaultController](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultcontroller) interface enqueues a chunk of data into the associated stream. + +## Parameters + +| Name | Type | Description | +| ----- | ---- | --------------------------------------------- | +| chunk | any | The chunk of data to enqueue into the stream. | + +## Exceptions + +| Exception | Description | +| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| TypeError | Thrown when the source object is not a [ReadableStreamDefaultController](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultcontroller). | + +## Example + +{{< code >}} + +```javascript +import { open } from 'k6/experimental/fs'; +import { ReadableStream } from 'k6/experimental/streams'; + +// Open a csv file containing the data to be read +const file = await open('./data.csv'); + +export default async function () { + let lineReaderState; + + // Define a ReadableStream that reads lines from the file + // and parses them into objects with name and color properties. + const fileLinesStream = new ReadableStream({ + // The start function is called when the readable stream is + // created. In here, you can connect to the data source + // and perform administrative tasks. + async start(controller) { + lineReaderState = { + buffer: new Uint8Array(1024), + remaining: '', + }; + }, + + // The pull function is called repeatedly to get data, while the + // internal high water mark is not reached. + async pull(controller) { + const line = await getNextLine(file, lineReaderState); + if (line === null) { + controller.close(); + return; + } + + const [name, color] = line.split(','); + controller.enqueue({ name, color }); + }, + }); + + // Obtain and lock a reader to the stream + const reader = fileLinesStream.getReader(); + + try { + // Read and process each item from the stream + while (true) { + const { done, value } = await reader.read(); + if (done) { + break; + } + + console.log(value); + } + } catch (error) { + console.error('Stream reading failed: ', error); + } +} + +// getNextLine reads the next line from the file and returns it. +// +// It reads the file in chunks and buffers the remaining data +// to handle partial lines. It returns null when there are no +// more lines to read. +async function getNextLine(file, state) { + while (true) { + if (state.remaining.includes('\n')) { + const lineEndIndex = state.remaining.indexOf('\n'); + const line = state.remaining.substring(0, lineEndIndex).trim(); + + state.remaining = state.remaining.substring(lineEndIndex + 1); + + if (line) { + return line; + } + } else { + const bytesRead = await file.read(state.buffer); + if (bytesRead === null) { + // EOF + + if (state.remaining) { + const finalLine = state.remaining.trim(); + + // Clear remaining to signal the end + state.remaining = ''; + + // Return the last non-empty line + return finalLine; + } + + // Indicate that there are no more lines to read + return null; + } + + state.remaining += String.fromCharCode.apply( + null, + new Uint8Array(state.buffer.slice(0, bytesRead)) + ); + } + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/error.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/error.md new file mode 100644 index 000000000..8311a2135 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/error.md @@ -0,0 +1,65 @@ +--- +title: 'error(reason)' +description: 'The error method of the ReadableStreamDefaultController makes any future interactions with the associated stream to error.' +weight: 40 +--- + +# error(reason) + +The `error()` method of the [ReadableStreamDefaultController](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultcontroller) makes any future interactions with the associated stream to error. + +## Parameters + +| Name | Type | Description | +| ---- | ---- | --------------------------------------------------------------- | +| e | any | The error to return to any future interactions with the stream. | + +## Exceptions + +| Exception | Description | +| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| TypeError | Thrown when the source object is not a [ReadableStreamDefaultController](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultcontroller). | + +## Example + +{{< code >}} + +```javascript +import { ReadableStream } from 'k6/experimental/streams'; +import { setTimeout } from 'k6/timers'; + +export default async function () { + let currentNumber = 0; + + const stream = new ReadableStream({ + start(controller) { + const fn = () => { + if (currentNumber % 8 == 0) { + // Error the stream when the number is divisible by 8 + controller.error('erroring the stream because we received a multiple of 8'); + return; + } + + if (currentNumber < 10) { + controller.enqueue(++currentNumber); + setTimeout(fn, 1000); + return; + } + + controller.close(); + }; + setTimeout(fn, 1000); + }, + }); + + const reader = stream.getReader(); + + while (true) { + const { done, value } = await reader.read(); + if (done) break; + console.log(`received number ${value} from stream`); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/_index.md new file mode 100644 index 000000000..224825ffe --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/_index.md @@ -0,0 +1,17 @@ +--- +title: 'ReadableStreamDefaultReader' +description: 'ReadableStreamDefaultReader represents a default reader that can be used to read stream data.' +weight: 20 +--- + +# ReadableStreamDefaultReader + +The `ReadableStreamDefaultReader` type represents a default reader that can be used to read stream data. It can be used to read from a [ReadableStream](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestream) object that has an [underlying source](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestream/#underlyingsource-optional) of any type. + +## Methods + +| Name | Description | +| ---------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [cancel(reason)](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultreader/cancel) | Returns a `Promise` that resolves when the stream is canceled. | +| [read()](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultreader/read) | Returns a `Promise` that resolves with an object containing the `done` and `value` properties, providing access to the next chunk in the stream's internal queue. | +| [releaseLock()](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultreader/releaselock) | Releases the lock on the stream. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/cancel.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/cancel.md new file mode 100644 index 000000000..99d8e2b6c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/cancel.md @@ -0,0 +1,77 @@ +--- +title: 'cancel(reason)' +description: 'Returns a Promise that resolves when the stream is canceled.' +weight: 20 +--- + +# cancel(reason) + +The `cancel()` method of the [ReadableStreamDefaultReader](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultreader) interface returns a `Promise` that resolves when the stream is canceled. + +Cancel is used when you've completely finished with the stream and don't need any more data from it, even if chunks are enqueued waiting to be read. That data is lost after `cancel` is called, and the stream is not readable any more. To close the stream without getting rid of enqueued chunks, use [ReadableStreamDefaultController.close()](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultcontroller/close). + +## Arguments + +| Name | Type | Description | +| ------ | ---- | ------------------------------------------------------------------------------------- | +| reason | any | An optional human-readable value that represents the reason for canceling the stream. | + +## Returns + +A promise that resolves with the provided `reason` when the stream is canceled. + +## Exceptions + +| Exception | Description | +| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| TypeError | Thrown when the source object is not a [ReadableStreamDefaultReader](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultreader) or the stream has no owner. | + +## Example + +{{< code >}} + +```javascript +import { ReadableStream } from 'k6/experimental/streams'; +import { setTimeout } from 'k6/timers'; + +export default async function () { + // Define a number stream that emits numbers from 1 to 10 every second + const stream = numbersStream(); + + // We use the getReader method to create a reader and lock the stream to it + const reader = stream.getReader(); + + while (true) { + const { done, value } = await reader.read(); + if (done) break; + if (value === 8) { + // Cancel the stream when the number is 8 + await reader.cancel('cancelling the stream'); + break; + } + + console.log(`received number ${value} from stream`); + } +} + +function numbersStream() { + let currentNumber = 0; + + return new ReadableStream({ + start(controller) { + const fn = () => { + if (currentNumber < 10) { + controller.enqueue(++currentNumber); + setTimeout(fn, 1000); + return; + } + + controller.close(); + }; + setTimeout(fn, 1000); + }, + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/read.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/read.md new file mode 100644 index 000000000..340e47b9c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/read.md @@ -0,0 +1,68 @@ +--- +title: 'read()' +description: "The read() method of the ReadableStreamDefaultReader interface returns a Promise providing access to the next chunk in the stream's internal queue." +weight: 30 +--- + +# read() + +The `read()` method of the [ReadableStreamDefaultReader](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultreader) interface returns a promise providing access to the next chunk in the stream's internal queue. + +## Returns + +A promise which fullfils or rejects with a value depending on the state of the stream: + +- If a chunk is available, the promise resolves with an object of the form: `{ done: false, value: chunkValue }`. +- If the stream is closed and no more data is available, the promise resolves with an object of the form: `{ done: true, value: undefined }`. +- If the stream is errored, the promise rejects with the error that caused the stream to error. + +## Exceptions + +| Exception | Description | +| --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| TypeError | Thrown when the source object is not a [ReadableStreamDefaultReader](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultreader), the stream has no owner, or [ReadableStreamDefaultReader.releaseLock()](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultreader/releaselock) is called. | + +## Example + +{{< code >}} + +```javascript +import { ReadableStream } from 'k6/experimental/streams'; +import { setTimeout } from 'k6/timers'; + +export default async function () { + // Define a number stream that emits numbers from 1 to 10 every second + const stream = numbersStream(); + + // We use the getReader method to create a reader and lock the stream to it + const reader = stream.getReader(); + + while (true) { + const { done, value } = await reader.read(); + if (done) break; + + console.log(`received number ${value} from stream`); + } +} + +function numbersStream() { + let currentNumber = 0; + + return new ReadableStream({ + start(controller) { + const fn = () => { + if (currentNumber < 10) { + controller.enqueue(++currentNumber); + setTimeout(fn, 1000); + return; + } + + controller.close(); + }; + setTimeout(fn, 1000); + }, + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/releaseLock.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/releaseLock.md new file mode 100644 index 000000000..4e2d99dde --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/streams/readablestreamdefaultreader/releaseLock.md @@ -0,0 +1,66 @@ +--- +title: 'releaseLock()' +description: "The releaseLock() method of the ReadableStreamDefaultReader interface releases the reader's lock on the stream." +weight: 40 +--- + +# releaseLock() + +The `releaseLock()` method of the [ReadableStreamDefaultReader](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultreader) interface releases the reader's lock on the stream. + +If the associated stream is errored as the lock is released, the reader will be errored as well. This method is useful when you're done with the stream and want to release the lock on it. + +If the reader's lock is released as pending read operations are still in progress, the reader's [ReadableStreamDefaultReader.read()](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultreader/read) calls are immediately rejected with a `TypeError`. + +## Exceptions + +| Exception | Description | +| --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| TypeError | Thrown when the source object is not a [ReadableStreamDefaultReader](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestreamdefaultreader) | + +## Example + +{{< code >}} + +```javascript +import { ReadableStream } from 'k6/experimental/streams'; +import { setTimeout } from 'k6/timers'; + +export default async function () { + // Define a number stream that emits numbers from 1 to 10 every second + const stream = numbersStream(); + + // We use the getReader method to create a reader and lock the stream to it + const reader = stream.getReader(); + + while (true) { + const { done, value } = await reader.read(); + if (done) break; + } + + // Release the lock on the stream, so we're free to obtain another reader + // and read from the stream again + reader.releaseLock(); +} + +function numbersStream() { + let currentNumber = 0; + + return new ReadableStream({ + start(controller) { + const fn = () => { + if (currentNumber < 10) { + controller.enqueue(++currentNumber); + setTimeout(fn, 1000); + return; + } + + controller.close(); + }; + setTimeout(fn, 1000); + }, + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/_index.md new file mode 100644 index 000000000..8f06b918c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/_index.md @@ -0,0 +1,95 @@ +--- +title: 'webcrypto' +description: "k6 webcrypto experimental API" +weight: 06 +weight: 06 +--- + +# webcrypto + +{{< docs/shared source="k6" lookup="experimental-module.md" version="" >}} + +With this experimental module, you can use the [WebCrypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) in your k6 scripts. However, note that this API is not yet fully implemented and some algorithms and features might still be missing. + +The Web Crypto API is a JavaScript API for performing cryptographic operations such as encryption, decryption, digital signature generation and verification, and key generation and management. It provides a standard interface to access cryptographic functionality, which can help ensure that cryptographic operations are performed correctly and securely. + +## API + +The module exports a top-level `crypto` object with the following properties and methods: + +| Interface/Function | Description | +| :-------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| [getRandomValues](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/crypto/getrandomvalues) | Fills the passed `TypedArray` with cryptographically sound random values. | +| [randomUUID](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/crypto/randomuuid) | Returns a randomly generated, 36 character long v4 UUID. | +| [subtle](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto) | The [SubtleCrypto](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto) interface provides access to common cryptographic primitives, such as hashing, signing, encryption, or decryption. | + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const plaintext = stringToArrayBuffer('Hello, World!'); + + /** + * Generate a symmetric key using the AES-CBC algorithm. + */ + const key = await crypto.subtle.generateKey( + { + name: 'AES-CBC', + length: 256, + }, + true, + ['encrypt', 'decrypt'] + ); + + /** + * Encrypt the plaintext using the AES-CBC key with + * have generated. + */ + const iv = crypto.getRandomValues(new Uint8Array(16)); + const ciphertext = await crypto.subtle.encrypt( + { + name: 'AES-CBC', + iv: iv, + }, + key, + plaintext + ); + + /** + * Decrypt the ciphertext using the same key to verify + * that the resulting plaintext is the same as the original. + */ + const deciphered = await crypto.subtle.decrypt( + { + name: 'AES-CBC', + iv: iv, + }, + key, + ciphertext + ); + + console.log( + 'deciphered text == original plaintext: ', + arrayBufferToHex(deciphered) === arrayBufferToHex(plaintext) + ); +} + +function arrayBufferToHex(buffer) { + return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join(''); +} + +function stringToArrayBuffer(str) { + const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char + const bufView = new Uint16Array(buf); + for (let i = 0, strLen = str.length; i < strLen; i++) { + bufView[i] = str.charCodeAt(i); + } + return buf; +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aescbcparams.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aescbcparams.md new file mode 100644 index 000000000..eeace4d95 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aescbcparams.md @@ -0,0 +1,66 @@ +--- +title: 'AesCbcParams' +description: 'AesCbcParams represents the object that should be passed as the algorithm parameter into the encrypt and decrypt operation when using the AES-CBC algorithm.' +weight: 04 +--- + +# AesCbcParams + +The `AesCbcParams` object represents the object that should be passed as the algorithm parameter into the [encrypt](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/encrypt) and [decrypt](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/decrypt) operation when using the AES-CBC algorithm. + +For more details, head to the [MDN Web Crypto API documentation on AES-CBC](https://developer.mozilla.org/en-US/docs/Web/API/AesCbcParams). + +## Properties + +| Property | Type | Description | +| :------- | :----------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| name | `string` | Should be set to `AES-CBC`. | +| iv | `ArrayBuffer`, `TypedArray`, or `DataView` | The initialization vector. Must be 16 bytes, unpredictable and cryptographically random. Yet, it doesn't need to be secret and can be transmitted along with the ciphertext. | + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const plaintext = stringToArrayBuffer('Hello, World!'); + + /** + * Generate a symmetric key using the AES-CBC algorithm. + */ + const key = await crypto.subtle.generateKey( + { + name: 'AES-CBC', + length: 256, + }, + true, + ['encrypt', 'decrypt'] + ); + + /** + * Encrypt the plaintext using the AES-CBC key with + * have generated. + */ + const ciphertext = await crypto.subtle.encrypt( + { + name: 'AES-CBC', + iv: crypto.getRandomValues(new Uint8Array(16)), + }, + key, + plaintext + ); +} + +function stringToArrayBuffer(str) { + const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char + const bufView = new Uint16Array(buf); + for (let i = 0, strLen = str.length; i < strLen; i++) { + bufView[i] = str.charCodeAt(i); + } + return buf; +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aesctrparams.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aesctrparams.md new file mode 100644 index 000000000..c0fc53150 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aesctrparams.md @@ -0,0 +1,68 @@ +--- +title: 'AesCtrParams' +description: 'AesCtrParams represents the object that should be passed as the algorithm parameter into the encrypt and decrypt operation when using the AES-CTR algorithm.' +weight: 05 +--- + +# AesCtrParams + +The `AesCtrParams` object represents the object that should be passed as the algorithm parameter into the [encrypt](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/encrypt) and [decrypt](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/decrypt) operation when using the AES-CTR algorithm. + +For more details, head to the MDN Web Crypto API documentation on [AES-CTR](https://developer.mozilla.org/en-US/docs/Web/API/AesCtrParams). + +## Properties + +| Property | Type | Description | +| :------- | :----------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------- | +| name | `string` | Should be set to `AES-CTR`. | +| counter | `ArrayBuffer`, `TypedArray`, or `DataView` | The initial value of the counter block. This must be 16-bytes long, like the AES block size. | +| length | `number` | The number of bits in the counter block that are used for the actual counter. It is recommended to use 64 (half of the counter block). | + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const plaintext = stringToArrayBuffer('Hello, World!'); + + /** + * Generate a symmetric key using the AES-CTR algorithm. + */ + const key = await crypto.subtle.generateKey( + { + name: 'AES-CTR', + length: 256, + }, + true, + ['encrypt', 'decrypt'] + ); + + /** + * Encrypt the plaintext using the AES-CTR key with + * have generated. + */ + const ciphertext = await crypto.subtle.encrypt( + { + name: 'AES-CTR', + counter: crypto.getRandomValues(new Uint8Array(16)), + length: 128, + }, + key, + plaintext + ); +} + +function stringToArrayBuffer(str) { + const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char + const bufView = new Uint16Array(buf); + for (let i = 0, strLen = str.length; i < strLen; i++) { + bufView[i] = str.charCodeAt(i); + } + return buf; +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aesgcmparams.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aesgcmparams.md new file mode 100644 index 000000000..868b189d9 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aesgcmparams.md @@ -0,0 +1,68 @@ +--- +title: 'AesGcmParams' +description: 'AesGcmParams represents the object that should be passed as the algorithm parameter into the encrypt and decrypt operation when using the AES-GCM algorithm.' +weight: 06 +--- + +# AesGcmParams + +The `AesGcmParams` object represents the object that should be passed as the algorithm parameter into the [encrypt](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/encrypt) and [decrypt](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/decrypt) operation when using the AES-GCM algorithm. + +For more details, head to the [MDN Web Crypto API documentation on AES-GCM](https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams). + +## Properties + +| Property | Type | Description | +| :------------------------ | :----------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| name | `string` | Should be set to `AES-GCM`. | +| iv | `ArrayBuffer`, `TypedArray`, or `DataView` | The initialization vector. It must be 12 bytes long, unpredictable and cryptographically random. It must be unique for every encryption operation carried out with a given key. Never reuse an iv with the same key. Yet, it doesn't need to be secret and can be transmitted along with the ciphertext. | +| additionalData (optional) | `ArrayBuffer`, `TypedArray` or `DataView` | Additional data that should be authenticated, but not encrypted. It must be included in the calculation of the authentication tag, but not encrypted itself. | +| tagLength (optional) | `number` | The length of the authentication tag in bits. Should be set, and will default to 96. | + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const plaintext = stringToArrayBuffer('Hello, World!'); + + /** + * Generate a symmetric key using the AES-CBC algorithm. + */ + const key = await crypto.subtle.generateKey( + { + name: 'AES-GCM', + length: 256, + }, + true, + ['encrypt', 'decrypt'] + ); + + /** + * Encrypt the plaintext using the AES-CBC key with + * have generated. + */ + const ciphertext = await crypto.subtle.encrypt( + { + name: 'AES-GCM', + iv: crypto.getRandomValues(new Uint8Array(12)), + }, + key, + plaintext + ); +} + +function stringToArrayBuffer(str) { + const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char + const bufView = new Uint16Array(buf); + for (let i = 0, strLen = str.length; i < strLen; i++) { + bufView[i] = str.charCodeAt(i); + } + return buf; +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aeskeygenparams.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aeskeygenparams.md new file mode 100644 index 000000000..eb83b2972 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/aeskeygenparams.md @@ -0,0 +1,37 @@ +--- +title: 'AesKeyGenParams' +description: 'AesKeyGenParams represents the object that should be passed as the algorithm parameter into the generateKey operation, when generating an AES key.' +weight: 07 +--- + +# AesKeyGenParams + +The `AesKeyGenParams` object represents the object that should be passed as the algorithm parameter into the [generateKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/generatekey) operation when generating an AES key. + +## Properties + +| Property | Type | Description | +| :------- | :------- | :---------------------------------------------------------------------------------- | +| name | `string` | The name of the algorithm. Possible values are `AES-CBC`, `AES-CTR`, and `AES-GCM`. | +| length | `number` | The length of the key in bits. Possible values are 128, 192 or 256. | + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const key = await crypto.subtle.generateKey( + { + name: 'AES-CBC', + length: 256, + }, + true, + ['encrypt', 'decrypt'] + ); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/crypto/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/crypto/_index.md new file mode 100644 index 000000000..5eaa39e53 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/crypto/_index.md @@ -0,0 +1,22 @@ +--- +title: 'Crypto' +description: 'Crypto offers basic cryptography features.' +weight: 01 +--- + +# Crypto + +`Crypto` allows access to a cryptographically strong random number generator and to cryptographic primitives. + +## Properties + +| Name | Description | +| :------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| [subtle](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto) | The [SubtleCrypto](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto) interface provides access to common cryptographic primitives, such as hashing, signing, encryption, or decryption. | + +## Methods + +| Name | Description | +| :----------------------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------- | +| [getRandomValues()](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/crypto/getrandomvalues/) | Returns an array of cryptographically secure random values. | +| [randomUUID()](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/crypto/randomuuid/) | Returns a randomly generated, 36 character long v4 UUID. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/crypto/getrandomvalues.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/crypto/getrandomvalues.md new file mode 100644 index 000000000..12299df15 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/crypto/getrandomvalues.md @@ -0,0 +1,50 @@ +--- +title: 'getRandomValues' +description: 'getRandomValues fills the passed TypedArray with cryptographically sound random values.' +weight: 01 +--- + +# getRandomValues + +The `getRandomValues()` method fills the passed `TypedArray` with cryptographically sound random values. + +## Usage + +``` +getRandomValues(typedArray) +``` + +## Parameters + +| Name | Type | Description | +| :----------- | :----------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `typedArray` | `TypedArray` | An integer-based `TypedArray` to fill with random values. Accepted `TypedArray` specific types are: `Int8Array`, `Uint8Array`, `Uint8ClampedArray`, `Int16Array`, `Uint16Array`, `Int32Array`, or `Uint32Array`. | + +## Return Value + +The same array is passed as the `typedArray` parameter with its contents replaced with the newly generated random numbers. The `typedArray` parameter is modified in place, and no copy is made. + +## Throws + +| Type | Description | +| :------------------- | :------------------------------------------------------------------------ | +| `QuotaExceededError` | Thrown when `typedArray` is too large and its `byteLength` exceeds 65536. | + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default function () { + const array = new Uint32Array(10); + crypto.getRandomValues(array); + + for (const num of array) { + console.log(num); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/crypto/randomuuid.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/crypto/randomuuid.md new file mode 100644 index 000000000..0a3a0f693 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/crypto/randomuuid.md @@ -0,0 +1,35 @@ +--- +title: 'randomUUID' +description: 'randomUUID produces a 36-characters long string containing a cryptographically random UUID v4.' +weight: 02 +--- + +# randomUUID + +The `randomUUID` method produces a 36-characters long string that contains a cryptographically random UUID v4. + +## Usage + +``` +randomUUID() +``` + +## Return Value + +A string containing a 36-character cryptographically random UUID v4. + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default function () { + const myUUID = crypto.randomUUID(); + + console.log(myUUID); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/cryptokey.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/cryptokey.md new file mode 100644 index 000000000..4e6a77eb2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/cryptokey.md @@ -0,0 +1,18 @@ +--- +title: 'CryptoKey' +description: 'CryptoKey represents a cryptographic key used for encryption, decryption, signing, or verification.' +weight: 02 +--- + +# CryptoKey + +The `CryptoKey` object represents a cryptographic key used for [encryption](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/encrypt),[decryption](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/decrypt),[signing](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/sign), or [verification](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/verify) within the webcrypto module. The `CryptoKey` object is created using the SubtleCrypto.generateKey() or SubtleCrypto.importKey() methods. + +## Properties + +| Property | Type | Description | +| :---------- | :--------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| type | `string` | Indicates the type of the key material. Possible values are `public`, `private`, `secret`, `unspecified`, or `unknown`. | +| extractable | `boolean` | Indicates whether the raw key material can be exported. | +| algorithm | `object` | An object containing the algorithm used to generate or import the key. | +| usages | `string[]` | An array of strings indicating the cryptographic operations that the key can be used for. Possible values are `encrypt`, `decrypt`, `sign`, `verify`, `deriveKey`, `deriveBits`, `wrapKey`, and `unwrapKey`. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/cryptokeypair.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/cryptokeypair.md new file mode 100644 index 000000000..f5eda8b4a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/cryptokeypair.md @@ -0,0 +1,18 @@ +--- +title: 'CryptoKeyPair' +description: 'CryptoKeyPair represents an asymmetric key pair with public and private keys.' +weight: 08 +--- + +# CryptoKeyPair + +The `CryptoKeyPair` object represents an asymmetric key pair with public and private keys. + +The [`generateKey`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/generatekey/) method can be used to create `CryptoKeyPair` object for asymmetric algorithms such as `ECDH` or `ECDSA`. + +## Properties + +| Property | Type | Description | +| :--------- | :--------------------------------------------------------------------------------------------------------- | :------------- | +| publicKey | [`CryptoKey`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) | A public key. | +| privateKey | [`CryptoKey`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) | A private key. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/ecdhkeyderiveparams.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/ecdhkeyderiveparams.md new file mode 100644 index 000000000..d6049a468 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/ecdhkeyderiveparams.md @@ -0,0 +1,18 @@ +--- +title: 'EcdhKeyDeriveParams' +description: 'EcdhKeyDeriveParams is a parameter used for derive bits operation.' +weight: 10 +--- + +# EcdhKeyDeriveParams + +The `EcdhKeyDeriveParams` represents the object that should be passed as the algorithm parameter into [`deriveBits`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/derivebits/), when using the ECDH algorithm. + +ECDH is a secure communication method. Parties exchange public keys and use them with their private keys to generate a unique shared secret key. + +## Properties + +| Property | Type | Description | +| :-------- | :--------------------------------------------------------------------------------------------------------- | :----------------------------------- | +| name | `string` | An algorithm name. Should be `ECDH`. | +| publicKey | [`CryptoKey`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) | Another party's public key. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/ecdsaparams.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/ecdsaparams.md new file mode 100644 index 000000000..bb5a0ef43 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/ecdsaparams.md @@ -0,0 +1,16 @@ +--- +title: 'EcdsaParams' +description: 'EcdsaParams is a parameter used for sign or verify operations.' +weight: 11 +--- + +# EcdsaParams + +The `EcdsaParams` represents the object that should be passed as the algorithm parameter into [`sign`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/sign/) or [`verify`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/verify/) when using the ECDSA algorithm. + +## Properties + +| Property | Type | Description | +| :------- | :------- | :---------------------------------------------------------------------------------------------------- | +| name | `string` | An algorithm name. Should be `ECDSA`. | +| hash | `string` | An identifier for the digest algorithm to use. Possible values are `SHA-256`, `SHA-384` or `SHA-512`. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/eckeygenparams.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/eckeygenparams.md new file mode 100644 index 000000000..99cd22d12 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/eckeygenparams.md @@ -0,0 +1,39 @@ +--- +title: 'EcKeyGenParams' +description: 'EcKeyGenParams represents the object that should be passed as the algorithm parameter into the generateKey operation, when generating ECDH or ECDSA key pairs.' +weight: 09 +--- + +# EcKeyGenParams + +The `EcKeyGenParams` object represents the object that should be passed as the algorithm parameter into the [generateKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/generatekey) operation when generating key pairs for ECDH or ECDSA algorithms. + +## Properties + +| Property | Type | Description | +| :--------- | :------- | :------------------------------------------------------------------------------------------------------- | +| name | `string` | An algorithm name. Possible values are `ECDH` or `ECDSA`. | +| namedCurve | `string` | A elliptic curve's name to use for key pair generation. Possible values are `P-256`, `P-384` or `P-521`. | + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const keyPair = await crypto.subtle.generateKey( + { + name: 'ECDSA', + namedCurve: 'P-256', + }, + true, + ['sign', 'verify'] + ); + + console.log(JSON.stringify(keyPair)); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/hmackeygenparams.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/hmackeygenparams.md new file mode 100644 index 000000000..61bafce45 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/hmackeygenparams.md @@ -0,0 +1,39 @@ +--- +title: 'HmacKeyGenParams' +description: 'HmacKeyGenParams represents the object that should be passed as the algorithm parameter into the generateKey operation, when generating an HMAC key.' +weight: 12 +--- + +# HmacKeyGenParams + +The `HmacKeyGenParams` object represents the object that should be passed as the algorithm parameter into the [generateKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/generatekey) operation when generating an HMAC key. + +## Properties + +| Property | Type | Description | +| :---------------- | :------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| name | `string` | This should be set to `HMAC`. | +| hash | `string` | The name of the digest function to use. Possible values are `SHA-1`, `SHA-256`, `SHA-384` and `SHA-512`. | +| length (optional) | `number` | The length of the key in bits. If this is omitted, the length of the key is equal to the block size of the hash function you have chosen. We recommend to leave this parameter empty, unless you have a good reason to use something different. | + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const key = await crypto.subtle.generateKey( + { + name: 'HMAC', + hash: { name: 'SHA-512' }, + length: 256, + }, + true, + ['sign', 'verify'] + ); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/jsonwebkey.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/jsonwebkey.md new file mode 100644 index 000000000..9f9dad333 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/jsonwebkey.md @@ -0,0 +1,59 @@ +--- +title: 'JsonWebKey' +description: 'JsonWebKey represents object/dictionary generated by exporting a CryptoKey or used as an input parameter for key import.' +weight: 13 +--- + +# JsonWebKey + +The `JsonWebKey` object represents object/dictionary generated by exporting a [`CryptoKey`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) or used as an input parameter for key import. + +The properties of the `JsonWebKey` could vary depending on the algorithm and key type. See specification [JsonWebKey](https://www.w3.org/TR/WebCryptoAPI/#JsonWebKey-dictionary) for details. + +## Properties + +| Property | Type | Description | +| :------- | :------------- | :---------------------- | +| kty | `string` | The key type. | +| k | `string` | The key value. | +| alg | `string` | The algorithm. | +| ext | `bool` | The key is extractable. | +| key_ops | `string` array | The key operations. | +| crv | `string` | The curve name. | +| x | `string` | The x coordinate. | +| y | `string` | The y coordinate. | +| d | `string` | The private key. | + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const jwk = { + alg: 'HS256', + ext: true, + k: 'H6gLp3lw7w27NrPUn00WpcKU-IJojJdNzhL_8F6se2k', + key_ops: ['sign', 'verify'], + kty: 'oct', + }; + + const importedKey = await crypto.subtle.importKey( + 'jwk', + jwk, + { name: 'HMAC', hash: { name: 'SHA-256' } }, + true, + ['sign', 'verify'] + ); + + const exportedAgain = await crypto.subtle.exportKey('jwk', importedKey); + + console.log('exported again: ' + JSON.stringify(exportedAgain)); + // should print + // INFO[0000] exported again: {"k":"H6gLp3lw7w27NrPUn00WpcKU-IJojJdNzhL_8F6se2k","kty":"oct","ext":true,"key_ops":["sign","verify"],"alg":"HS256"} source=console +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsahashedimportparams.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsahashedimportparams.md new file mode 100644 index 000000000..bccde9b8a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsahashedimportparams.md @@ -0,0 +1,16 @@ +--- +title: 'RsaHashedImportParams' +description: 'RsaHashedImportParams represents the object that should be passed as the algorithm parameter into the importKey operation, when using the RSA algorithm.' +weight: 12 +--- + +# RsaHashedImportParams + +The `RsaHashedImportParams` represents the object that should be passed as the algorithm parameter into [`importKey`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/importkey/) when using the RSA algorithm. + +## Properties + +| Property | Type | Description | +| :------- | :------- | :------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------- | +| name | `string` | This should be set to `RSASSA-PKCS1-v1_5`, `RSA-PSS` or `RSA-OAEP`. | +| hash | `string` | `object` | The name or an object with a `name` property of the digest function to use. Possible values are `SHA-1`, `SHA-256`, `SHA-384` and `SHA-512`. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsahashedkeygenparams.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsahashedkeygenparams.md new file mode 100644 index 000000000..e7e5a816c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsahashedkeygenparams.md @@ -0,0 +1,41 @@ +--- +title: 'RSAHashedKeyGenParams' +description: 'RSAHashedKeyGenParams represents the object that should be passed as the algorithm parameter into the generateKey operation, when generating an RSA key pair.' +weight: 12 +--- + +# RSAHashedKeyGenParams + +The `RSAHashedKeyGenParams` object represents the object that should be passed as the algorithm parameter into the [generateKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/generatekey) operation when generating an RSA key pair. + +## Properties + +| Property | Type | Description | +| :------------- | :----------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- | +| name | `string` | This should be set to `RSASSA-PKCS1-v1_5`, `RSA-PSS` or `RSA-OAEP`. | +| modulusLength | `number` | The length in bits of the RSA modulus. This should be at least 2048. Some organizations are now recommending that it should be 4096. | +| publicExponent | `Uint8Array` | The public exponent. Unless you have a good reason to use something else, specify `65537` here, which represented as a `Uint8Array` is `new Uint8Array([1, 0, 1])` | +| hash | `string` | `object` | The name or an object with a `name` property of the digest function to use. Possible values are `SHA-1`, `SHA-256`, `SHA-384` and `SHA-512`. | + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const keyPair = await crypto.subtle.generateKey( + { + name: 'RSA-PSS', + modulusLength: 2048, + publicExponent: new Uint8Array([1, 0, 1]), + hash: { name: 'SHA-256' }, + }, + true, + ['sign', 'verify'] + ); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsaoaepparams.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsaoaepparams.md new file mode 100644 index 000000000..1eef8f19b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsaoaepparams.md @@ -0,0 +1,19 @@ +--- +title: 'RsaOaepParams' +description: 'RsaOaepParams represents the object that should be passed as the algorithm parameter into the encrypt and decrypt operation when using the RSA-OAEP algorithm.' +weight: 06 +--- + +# RsaOaepParams + +The `RsaOaepParams` object represents the object that should be passed as the algorithm parameter into the [encrypt](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/encrypt) and [decrypt](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/decrypt) operation when using the RSA-OAEP algorithm. + +For more details, head to the [MDN Web Crypto API documentation on RSA-OAEP](https://developer.mozilla.org/en-US/docs/Web/API/RsaOaepParams). + +## Properties + +| Property | Type | Description | +| :--------------- | :----------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| name | `string` | Should be set to `RSA-OAEP`. | +| label (optional) | `ArrayBuffer`, `TypedArray`, or `DataView` | It's an array of bytes that does not itself need to be encrypted but which should be bound to the ciphertext. A digest of the label is part of the input to the encryption operation. Unless your application calls for a label, you can just omit this argument, and it will not affect the security of the encryption operation. | +| | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsapssparams.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsapssparams.md new file mode 100644 index 000000000..a7fe8035c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/rsapssparams.md @@ -0,0 +1,22 @@ +--- +title: 'RsaPssParams' +description: 'RsaPssParams is a parameter used for sign or verify operations.' +weight: 11 +--- + +# RsaPssParams + +The `RsaPssParams` represents the object that should be passed as the algorithm parameter into [`sign`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/sign/) or [`verify`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/verify/) when using the RSA-PSS algorithm. + +## Properties + +| Property | Type | Description | +| :--------- | :------- | :-------------------------------------------------------------------------- | +| name | `string` | An algorithm name. Should be `RSA-PSS`. | +| saltLength | `number` | A long integer representing the length of the random salt to use, in bytes. | + +{{< admonition type="caution" >}} + +Since under the hood we use Golang's SDK the salt length 0 is not supported. In that case the maximum possible salt length will be used. + +{{< /admonition >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/_index.md new file mode 100644 index 000000000..32dbb0f94 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/_index.md @@ -0,0 +1,94 @@ +--- +title: 'SubtleCrypto' +description: 'SubtleCrypto offers low-level cryptographic functions.' +weight: 03 +--- + +# SubtleCrypto + +The `SubtleCrypto` interface provides a set of low-level cryptographic primitives such as encryption, decryption, digital signature generation and verification, and key generation and management. It is useful for using secure and efficient cryptographic operations within k6 scripts. + +## Methods + +| Method | Description | +| :------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------------------------------------------------------------------- | +| [encrypt](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/encrypt) | Encrypts the given plaintext data using the specified algorithm and key. | +| [decrypt](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/decrypt) | Decrypts the given ciphertext data using the specified algorithm and key. | +| [sign](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/sign) | Signs the given data using the specified algorithm and key. | +| [verify](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/verify) | Verifies the signature of the given data using the specified algorithm and key. | +| [digest](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/digest) | Computes the digest (hash) of the given data using the specified algorithm. | +| [generateKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/generatekey) | Generates a new cryptographic key for use with the specified algorithm. | +| [importKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/importkey) | Imports a raw key material into the Web Crypto API, generating a new key object to use with the specified algorithm. | +| [exportKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/exportkey) | Exports the raw key material of the given key object. | +| [deriveBits](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/derivebits) | Derives bits using provided input. | + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const plaintext = stringToArrayBuffer('Hello, World!'); + + /** + * Generate a symmetric key using the AES-CBC algorithm. + */ + const key = await crypto.subtle.generateKey( + { + name: 'AES-CBC', + length: 256, + }, + true, + ['encrypt', 'decrypt'] + ); + + /** + * Encrypt the plaintext using the AES-CBC key with + * have generated. + */ + const iv = crypto.getRandomValues(new Uint8Array(16)); + const ciphertext = await crypto.subtle.encrypt( + { + name: 'AES-CBC', + iv: iv, + }, + key, + plaintext + ); + + /** + * Decrypt the ciphertext using the same key to verify + * that the resulting plaintext is the same as the original. + */ + const deciphered = await crypto.subtle.decrypt( + { + name: 'AES-CBC', + iv: iv, + }, + key, + ciphertext + ); + + console.log( + 'deciphered text == original plaintext: ', + arrayBufferToHex(deciphered) === arrayBufferToHex(plaintext) + ); +} + +function arrayBufferToHex(buffer) { + return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join(''); +} + +function stringToArrayBuffer(str) { + const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char + const bufView = new Uint16Array(buf); + for (let i = 0, strLen = str.length; i < strLen; i++) { + bufView[i] = str.charCodeAt(i); + } + return buf; +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/decrypt.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/decrypt.md new file mode 100644 index 000000000..a3c541e91 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/decrypt.md @@ -0,0 +1,109 @@ +--- +title: 'decrypt' +description: 'decrypt decrypts some encrypted data' +weight: 01 +--- + +# decrypt + +The `decrypt()` method decrypts some encrypted data. + +## Usage + +``` +decrypt(algorithm, key, data) +``` + +## Parameters + +| Name | Type | Description | +| :---------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `algorithm` | [AesCbcParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/aescbcparams), [AesCtrParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/aesctrparams), or [AesGcmParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/aesgcmparams) object | Defines the algorithm to use and any extra-parameters. The values given for the extra parameters must match those used in the corresponding [encrypt] call. | +| `key` | [CryptoKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) | The [key](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) to use for decryption. | +| `data` | `ArrayBuffer`, `TypedArray`, or `DataView` | The encrypted data to be decrypted (also known as _ciphertext_). | + +### Supported algorithms + +{{< docs/shared source="k6" lookup="webcrypto/supported-encrypt-decrypt.md" version="" >}} + +## Return Value + +A `Promise` that resolves to a new `ArrayBuffer` containing the decrypted data. + +## Throws + +| Type | Description | +| :------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `InvalidAccessError` | Raised when the requested operation is not valid with the provided key. For instance when an invalid encryption algorithm is used, or a key not matching the selected algorithm is provided. | +| `OperationError` | Raised when the operation failed for an operation-specific reason. For instance, if the algorithm size is invalid, or errors occurred during the process of decrypting the ciphertext. | + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const plaintext = stringToArrayBuffer('Hello, World!'); + + /** + * Generate a symmetric key using the AES-CBC algorithm. + */ + const key = await crypto.subtle.generateKey( + { + name: 'AES-CBC', + length: 256, + }, + true, + ['encrypt', 'decrypt'] + ); + + /** + * Encrypt the plaintext using the AES-CBC key with + * have generated. + */ + const iv = crypto.getRandomValues(new Uint8Array(16)); + const ciphertext = await crypto.subtle.encrypt( + { + name: 'AES-CBC', + iv: iv, + }, + key, + plaintext + ); + + /** + * Decrypt the ciphertext using the same key to verify + * that the resulting plaintext is the same as the original. + */ + const deciphered = await crypto.subtle.decrypt( + { + name: 'AES-CBC', + iv: iv, + }, + key, + ciphertext + ); + + console.log( + 'deciphered text == original plaintext: ', + arrayBufferToHex(deciphered) === arrayBufferToHex(plaintext) + ); +} + +function arrayBufferToHex(buffer) { + return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join(''); +} + +function stringToArrayBuffer(str) { + const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char + const bufView = new Uint16Array(buf); + for (let i = 0, strLen = str.length; i < strLen; i++) { + bufView[i] = str.charCodeAt(i); + } + return buf; +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/derivebits.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/derivebits.md new file mode 100644 index 000000000..d3df0a780 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/derivebits.md @@ -0,0 +1,90 @@ +--- +title: 'deriveBits' +description: 'deriveBits derives an array of bits' +weight: 02 +--- + +# deriveBits + +It takes as its arguments the base key, the derivation algorithm to use, and the length of the bits to derive. It returns a Promise which will be fulfilled with an `ArrayBuffer` containing the derived bits. This array of bits can be used as a key for encryption or decryption as a shared secret. + +## Usage + +``` +deriveBits(algorithm, baseKey, length) +``` + +## Parameters + +| Name | Type | Description | +| :---------- | :---------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------- | +| `algorithm` | [EcdhKeyDeriveParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/ecdhkeyderiveparams/) | An object defining a derivation algorithm to use. | +| `baseKey` | [CryptoKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) | Represent an input to derivation algorithm. Currently it could be only a private ECDH key. | +| `length` | `number` | Optional. A length of the bits to derive. Currently, only multiplies of 8 are supported. | + +### Supported algorithms + +| ECDH | HKDF | PBKDF2 | +| :------------------------------------------------------------------------------------------------------------------------------- | :--- | :----- | +| ✅ [EcdhKeyDeriveParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/ecdhkeyderiveparams/) | ❌ | ❌ | + +## Return Value + +A `Promise` that resolves to a new `ArrayBuffer` containing the derived bits. + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + // Generate a key pair for Alice + const aliceKeyPair = await crypto.subtle.generateKey( + { + name: 'ECDH', + namedCurve: 'P-256', + }, + true, + ['deriveKey', 'deriveBits'] + ); + + // Generate a key pair for Bob + const bobKeyPair = await crypto.subtle.generateKey( + { + name: 'ECDH', + namedCurve: 'P-256', + }, + true, + ['deriveKey', 'deriveBits'] + ); + + // Derive shared secret for Alice + const aliceSharedSecret = await deriveSharedSecret(aliceKeyPair.privateKey, bobKeyPair.publicKey); + + // Derive shared secret for Bob + const bobSharedSecret = await deriveSharedSecret(bobKeyPair.privateKey, aliceKeyPair.publicKey); + + console.log('alice shared secret: ' + printArrayBuffer(aliceSharedSecret)); + console.log('bob shared secret: ' + printArrayBuffer(bobSharedSecret)); +} + +async function deriveSharedSecret(privateKey, publicKey) { + return crypto.subtle.deriveBits( + { + name: 'ECDH', + public: publicKey, + }, + privateKey, + 256 + ); +} + +const printArrayBuffer = (buffer) => { + const view = new Uint8Array(buffer); + return Array.from(view); +}; +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/digest.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/digest.md new file mode 100644 index 000000000..cb60f4c25 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/digest.md @@ -0,0 +1,56 @@ +--- +title: 'digest' +description: 'digest decrypts some encrypted data' +weight: 02 +--- + +# digest + +The `digest()` method generates a cryptographically secure [digest](https://developer.mozilla.org/en-US/docs/Glossary/Digest) of the given data. A digest is a short fixed-length value derived from some input data. The `digest()` method is commonly used to compute a checksum of data or to verify the integrity of data. + +## Usage + +``` +digest(algorithm, data) +``` + +## Parameters + +| Name | Type | Description | +| :---------- | :-------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `algorithm` | a `string` or object with a single `name` string property | Names the hash function to use. Supported values are `"SHA-1"`, `"SHA-256"`, `"SHA-384"` and `"SHA-512"`. Note that the SHA-1 hash function is not considered safe for cryptographic use. | +| `data` | `ArrayBuffer`, `TypedArray`, or `DataView` | The data to be digested. | + +### Supported algorithms + +| SHA-1 | SHA-256 | SHA-384 | SHA-512 | +| :---- | :------ | :------ | :------ | +| ✅ | ✅ | ✅ | ✅ | + +## Return Value + +A `Promise` that resolves to a new `ArrayBuffer` containing the digest. + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const digest = await crypto.subtle.digest('SHA-256', stringToArrayBuffer('Hello, world!')); + + console.log(arrayBufferToHex(digest)); +} + +function arrayBufferToHex(buffer) { + return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join(''); +} + +function stringToArrayBuffer(s) { + return Uint8Array.from(new String(s), (x) => x.charCodeAt(0)); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/encrypt.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/encrypt.md new file mode 100644 index 000000000..c23b59821 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/encrypt.md @@ -0,0 +1,109 @@ +--- +title: 'encrypt' +description: 'encrypt decrypts some encrypted data' +weight: 03 +--- + +# encrypt + +The `encrypt()` method encrypts some data. + +## Usage + +``` +encrypt(algorithm, key, data) +``` + +## Parameters + +| Name | Type | Description | +| :---------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------- | +| `algorithm` | [AesCbcParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/aescbcparams), [AesCtrParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/aesctrparams), or [AesGcmParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/aesgcmparams) object | Defines the algorithm to use and any extra-parameters. | +| `key` | [CryptoKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) | The [key](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) to use for encryption. | +| `data` | `ArrayBuffer`, `TypedArray`, or `DataView` | The data to be encrypted (also known as "plaintext"). | + +### Supported algorithms + +{{< docs/shared source="k6" lookup="webcrypto/supported-encrypt-decrypt.md" version="" >}} + +## Return Value + +A `Promise` that resolves to a new `ArrayBuffer` containing the encrypted data. + +## Throws + +| Type | Description | +| :------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `InvalidAccessError` | Raised when the requested operation is not valid with the provided key. For instance when an invalid encryption algorithm is used, or a key not matching the selected algorithm is provided. | +| `OperationError` | Raised when the operation failed for an operation-specific reason. For instance, if the algorithm size is invalid, or errors occurred during the process of decrypting the ciphertext. | + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const plaintext = stringToArrayBuffer('Hello, World!'); + + /** + * Generate a symmetric key using the AES-CBC algorithm. + */ + const key = await crypto.subtle.generateKey( + { + name: 'AES-CBC', + length: 256, + }, + true, + ['encrypt', 'decrypt'] + ); + + /** + * Encrypt the plaintext using the AES-CBC key with + * have generated. + */ + const iv = crypto.getRandomValues(new Uint8Array(16)); + const ciphertext = await crypto.subtle.encrypt( + { + name: 'AES-CBC', + iv: iv, + }, + key, + plaintext + ); + + /** + * Decrypt the ciphertext using the same key to verify + * that the resulting plaintext is the same as the original. + */ + const deciphered = await crypto.subtle.decrypt( + { + name: 'AES-CBC', + iv: iv, + }, + key, + ciphertext + ); + + console.log( + 'deciphered text == original plaintext: ', + arrayBufferToHex(deciphered) === arrayBufferToHex(plaintext) + ); +} + +function arrayBufferToHex(buffer) { + return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join(''); +} + +function stringToArrayBuffer(str) { + const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char + const bufView = new Uint16Array(buf); + for (let i = 0, strLen = str.length; i < strLen; i++) { + bufView[i] = str.charCodeAt(i); + } + return buf; +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/exportkey.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/exportkey.md new file mode 100644 index 000000000..4f1a69a9b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/exportkey.md @@ -0,0 +1,83 @@ +--- +title: 'exportKey' +description: 'exportKey exports a key in an external, portable format.' +weight: 04 +--- + +# exportKey + +The `exportKey()` method takes a [CryptoKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) object as input and exports it in an external, portable format. + +Note that for a key to be exportable, it must have been created with the `extractable` flag set to `true`. + +## Usage + +``` +exportKey(format, key) +``` + +## Parameters + +| Name | Type | Description | +| :------- | :------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `format` | `string` | Defines the data format in which the key should be exported. Depending on the algorithm and key type, the data format could vary. Currently supported formats are `raw`, `jwk`, `spki`, and `pkcs8`. | +| `key` | [CryptoKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) | The [key](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) to export. | + +### Supported algorithms + +{{< docs/shared source="k6" lookup="webcrypto/supported-key-methods.md" version="" >}} + +### Supported formats + +{{< docs/shared source="k6" lookup="webcrypto/supported-key-methods-formats.md" version="" >}} + +## Return Value + +A `Promise` that resolves to a new `ArrayBuffer` or an [JsonWebKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/jsonwebkey) object/dictionary containing the key. + +## Throws + +| Type | Description | +| :------------------- | :-------------------------------------------------- | +| `InvalidAccessError` | Raised when trying to export a non-extractable key. | +| `NotSupportedError` | Raised when trying to export in an unknown format. | +| `TypeError` | Raised when trying to use an invalid format. | + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + /** + * Generate a symmetric key using the AES-CBC algorithm. + */ + const generatedKey = await crypto.subtle.generateKey( + { + name: 'AES-CBC', + length: '256', + }, + true, + ['encrypt', 'decrypt'] + ); + + /** + * Export the key in raw format. + */ + const exportedKey = await crypto.subtle.exportKey('raw', generatedKey); + + /** + * Reimport the key in raw format to verify its integrity. + */ + const importedKey = await crypto.subtle.importKey('raw', exportedKey, 'AES-CBC', true, [ + 'encrypt', + 'decrypt', + ]); + + console.log(JSON.stringify(importedKey)); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/generatekey.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/generatekey.md new file mode 100644 index 000000000..fdae3524d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/generatekey.md @@ -0,0 +1,67 @@ +--- +title: 'generateKey' +description: 'generateKey generates a new key.' +weight: 05 +--- + +# generateKey + +The `generateKey()` generates a new cryptographic key and returns it as a [CryptoKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) object or a [CryptoKeyPair](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokeypair) object that can be used with the Web Crypto API. + +## Usage + +``` +generateKey(algorithm, extractable, keyUsages) +``` + +## Parameters + +| Name | Type | Description | +| :------------ | :--------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `algorithm` | a `string` or algorithm object with a single `name` string | The type of key to generate. It can be either a string with any of the currently supported algorithms as a value or any of the generation key parameter objects. | +| `extractable` | `boolean` | Whether the key can be exported using [exportKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/exportkey). | +| `keyUsages` | `Array` | An array of strings describing what operations can be performed with the key. Key usages could vary depending on the algorithm. | + +### Supported algorithms + +{{< docs/shared source="k6" lookup="webcrypto/supported-key-methods.md" version="" >}} + +## Return Value + +A `Promise` that resolves with the generated key as a [CryptoKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) object or a [CryptoKeyPair](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokeypair) object. + +### Algorithm specific input + +| | HMAC | AES | ECDH | ECDSA | RSA-OAEP | RSASSA-PKCS1-v1_5 | RSA-PSS | +| :--------------------- | :----------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------- |----- |----- |----- | +| Parameters type to use | [`HmacKeyGenParams`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/hmackeygenparams) | [`AesKeyGenParams`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/aeskeygenparams) | [`EcKeyGenParams`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/eckeygenparams) | [`EcKeyGenParams`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/eckeygenparams) | [`RSAHashedKeyGenParams`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/rsahashedkeygenparams) | [`RSAHashedKeyGenParams`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/rsahashedkeygenparams) | [`RSAHashedKeyGenParams`](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/rsahashedkeygenparams) | +| Possible key usages | `sign`, `verify` | `encrypt`, `decrypt` | `deriveKey`, `deriveBits` | `sign`, `verify` | `encrypt`, `decrypt` | `sign`, `verify` | `sign`, `verify` | + +## Throws + +| Type | Description | +| :------------ | :------------------------------------------------------------------------------------------- | +| `SyntaxError` | Raised when the `keyUsages` parameter is empty, but the key is of type `secret` or `private`. | + +## Example + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const key = await crypto.subtle.generateKey( + { + name: 'AES-CBC', + length: 256, + }, + true, + ['encrypt', 'decrypt'] + ); + + console.log(JSON.stringify(key)); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/importkey.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/importkey.md new file mode 100644 index 000000000..3b59c4b9c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/importkey.md @@ -0,0 +1,230 @@ +--- +title: 'importKey' +description: 'importKey imports a key from an external, portable format and gives you a CryptoKey object.' +weight: 06 +--- + +# importKey + +The `importKey()` imports a key from an external, portable format, and gives you a [CryptoKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) object that can be used with the Web Crypto API. + +## Usage + +``` +importKey(format, keyData, algorithm, extractable, keyUsages) +``` + +## Parameters + +| Name | Type | Description | +| :------------ | :---------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `format` | `string` | Defines the data format of the key to import. Depending on the algorithm and key type, the data format could vary. Currently supported formats are `raw`, `jwk`, `spki`, and `pkcs8`. | +| `keyData` | `ArrayBuffer`, `TypedArray`, `DataView` or [JsonWebKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/jsonwebkey) | the data to import the key from. | +| `algorithm` | a `string` or object with a single `name` string property | The algorithm to use to import the key. | +| `extractable` | `boolean` | Indicates whether it will be possible to export the key using [exportKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/subtlecrypto/exportkey). | +| `keyUsages` | `Array` | An array of strings describing what operations can be performed with the key. Currently supported usages include `encrypt`, `decrypt`, `sign`, and `verify`. | + +### Supported algorithms + +{{< docs/shared source="k6" lookup="webcrypto/supported-key-methods.md" version="" >}} + +### Supported formats + +{{< docs/shared source="k6" lookup="webcrypto/supported-key-methods-formats.md" version="" >}} + +## Return Value + +A `Promise` that resolves with the imported key as a [CryptoKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) object. + +## Throws + +| Type | Description | +| :------------ | :---------------------------------------------------------------------------------------------- | +| `SyntaxError` | Raised when the `keyUsages` parameter is empty but the key is of type `secret` or `private`. | +| `TypeError` | Raised when trying to use an invalid format, or if the `keyData` is not suited for that format. | + +## Examples + +### Round-trip key export/import + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + /** + * Generate a symmetric key using the AES-CBC algorithm. + */ + const generatedKey = await crypto.subtle.generateKey( + { + name: 'AES-CBC', + length: '256', + }, + true, + ['encrypt', 'decrypt'] + ); + + /** + * Export the key in raw format. + */ + const exportedKey = await crypto.subtle.exportKey('raw', generatedKey); + + /** + * Reimport the key in raw format to verify its integrity. + */ + const importedKey = await crypto.subtle.importKey('raw', exportedKey, 'AES-CBC', true, [ + 'encrypt', + 'decrypt', + ]); + + console.log(JSON.stringify(importedKey)); +} +``` + +{{< /code >}} + +### Import a static raw key and decrypt transmitted data + +This example demonstrates how to import a static `raw` key and decrypt some transmitted data in `base64`. The transmitted data in this example represents an initialization vector and encoded data, and in a real-world scenario, it can be a response body or other data received from a request. + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; +import { b64decode } from 'k6/encoding'; + +export default async function () { + const transmittedData = base64Decode( + 'whzEN310mrlWIH/icf0dMquRZ2ENyfOzkvPuu92WR/9F8dbeFM8EGUVNIhaS' + ); + + // keyData is the key used to decrypt the data, which is usually stored in a secure location + // for this example, we are using a static key + const keyData = new Uint8Array([ + 109, 151, 76, 33, 232, 253, 176, 90, 94, 40, 146, 227, 139, 208, 245, 139, 69, 215, 55, 197, 43, + 122, 160, 178, 228, 104, 4, 115, 138, 159, 119, 49, + ]); + + try { + const result = await decrypt(keyData, transmittedData); + + // should output decrypted message + // INFO[0000] result: 'my secret message' source=console + console.log("result: '" + result + "'"); + } catch (e) { + console.log('Error: ' + JSON.stringify(e)); + } +} + +const decrypt = async (keyData, transmittedData) => { + const initializeVectorLength = 12; + + // the first 12 bytes are the initialization vector + const iv = new Uint8Array(transmittedData.subarray(0, initializeVectorLength)); + + // the rest of the transmitted data is the encrypted data + const encryptedData = new Uint8Array(transmittedData.subarray(initializeVectorLength)); + + const importedKey = await crypto.subtle.importKey( + 'raw', + keyData, + { name: 'AES-GCM', length: '256' }, + true, + ['decrypt'] + ); + + const plain = await crypto.subtle.decrypt( + { name: 'AES-GCM', iv: iv }, + importedKey, + encryptedData + ); + + return arrayBufferToString(plain); +}; + +const arrayBufferToString = (buffer) => { + return String.fromCharCode.apply(null, new Uint8Array(buffer)); +}; + +const base64Decode = (base64String) => { + return new Uint8Array(b64decode(base64String)); +}; +``` + +{{< /code >}} + +### Import a static JWK key and decrypt transmitted data + +This example is similar to the previous one. It demonstrates how to import a static `jwk` key and decrypt some transmitted data (which contains the initialization vector and encoded data) in `base64`. + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; +import { b64decode } from 'k6/encoding'; + +export default async function () { + // transmitted data is the base64 of the initialization vector + encrypted data + // that unusually transmitted over the network + const transmittedData = base64Decode( + 'drCfxl4O+5FcrHe8Bs0CvKlw3gZpv+S5if3zn7c4BJzHJ35QDFV4sJB0pbDT' + ); + + // keyData is the key used to decrypt the data, which is usually stored in a secure location + // for this example, we are using a static key + const jwkKeyData = { + kty: 'oct', + ext: true, + key_ops: ['decrypt', 'encrypt'], + alg: 'A256GCM', + k: '9Id_8iG6FkGOWmc1S203vGVnTExtpDGxdQN7v7OV9Uc', + }; + + try { + const result = await decrypt(jwkKeyData, transmittedData); + + // should output decrypted message + // INFO[0000] result: 'my secret message' source=console + console.log("result: '" + result + "'"); + } catch (e) { + console.log('Error: ' + JSON.stringify(e)); + } +} + +const decrypt = async (keyData, transmittedData) => { + const initializeVectorLength = 12; + + // the first 12 bytes are the initialization vector + const iv = new Uint8Array(transmittedData.subarray(0, initializeVectorLength)); + + // the rest of the transmitted data is the encrypted data + const encryptedData = new Uint8Array(transmittedData.subarray(initializeVectorLength)); + + const importedKey = await crypto.subtle.importKey( + 'jwk', + keyData, + { name: 'AES-GCM', length: 256 }, + true, + ['encrypt', 'decrypt'] + ); + + const plain = await crypto.subtle.decrypt( + { name: 'AES-GCM', iv: iv }, + importedKey, + encryptedData + ); + + return arrayBufferToString(plain); +}; + +const arrayBufferToString = (buffer) => { + return String.fromCharCode.apply(null, new Uint8Array(buffer)); +}; + +const base64Decode = (base64String) => { + return new Uint8Array(b64decode(base64String)); +}; +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/sign.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/sign.md new file mode 100644 index 000000000..86cabbd4c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/sign.md @@ -0,0 +1,133 @@ +--- +title: 'sign' +description: 'sign generates a digital signature.' +weight: 07 +--- + +# sign + +The `sign()` operation generates a digital signature of the provided `data`, using the given [CryptoKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) object. + +## Usage + +``` +sign(algorithm, key, data) +``` + +## Parameters + +| Name | Type | Description | +| :---------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------- | +| `algorithm` | `string` or object with a single `name` string property (`{name: "RSASSA-PKCS1-v1_5"}`) or an [EcdsaParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/ecdsaparams/), [HmacKeyGenParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/hmackeygenparams/), or [RsaPssParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/rsapssparams/) object. | The signature algorithm to use. | +| `key` | [CryptoKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) | The key to use for signing. | +| `data` | `ArrayBuffer`, `TypedArray`, or `DataView` | The data to be signed. | + +### Supported algorithms + +{{< docs/shared source="k6" lookup="webcrypto/supported-sign-verify.md" version="" >}} + +## Return Value + +A `Promise` that resolves with the signature as an `ArrayBuffer`. + +## Throws + +| Type | Description | +| :------------------- | :--------------------------------------------------------------------------------------------------------------------- | +| `InvalidAccessError` | Raised when the signing key either does not support signing operation, or is incompatible with the selected algorithm. | + +## Examples + +### Signing data with HMAC key + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const generatedKey = await crypto.subtle.generateKey( + { + name: 'HMAC', + hash: { name: 'SHA-1' }, + }, + true, + ['sign', 'verify'] + ); + + const data = string2ArrayBuffer('Hello World'); + + /** + * Signes the encoded data with the provided key using the HMAC algorithm + * the returned signature can be verified using the verify method. + */ + const signature = await crypto.subtle.sign('HMAC', generatedKey, data); + + /** + * Verifies the signature of the encoded data with the provided key using the HMAC algorithm. + */ + const verified = await crypto.subtle.verify('HMAC', generatedKey, signature, data); + + console.log('verified: ', verified); +} + +function string2ArrayBuffer(str) { + const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char + const bufView = new Uint16Array(buf); + for (let i = 0, strLen = str.length; i < strLen; i++) { + bufView[i] = str.charCodeAt(i); + } + return buf; +} +``` + +{{< /code >}} + +### Signing and verifying data with ECDSA + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const keyPair = await crypto.subtle.generateKey( + { + name: 'ECDSA', + namedCurve: 'P-256', + }, + true, + ['sign', 'verify'] + ); + + const data = string2ArrayBuffer('Hello World'); + + const alg = { name: 'ECDSA', hash: { name: 'SHA-256' } }; + + // makes a signature of the encoded data with the provided key + const signature = await crypto.subtle.sign(alg, keyPair.privateKey, data); + + console.log('signature: ', printArrayBuffer(signature)); + + //Verifies the signature of the encoded data with the provided key + const verified = await crypto.subtle.verify(alg, keyPair.publicKey, signature, data); + + console.log('verified: ', verified); +} + +const string2ArrayBuffer = (str) => { + const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char + const bufView = new Uint16Array(buf); + for (let i = 0, strLen = str.length; i < strLen; i++) { + bufView[i] = str.charCodeAt(i); + } + return buf; +}; + +const printArrayBuffer = (buffer) => { + const view = new Uint8Array(buffer); + return Array.from(view); +}; +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/verify.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/verify.md new file mode 100644 index 000000000..ad58aff13 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/verify.md @@ -0,0 +1,146 @@ +--- +title: 'verify' +description: 'verify verifies a digital signature.' +weight: 08 +--- + +# verify + +The `verify()` operation verifies a digital signature. It ensures that some data was signed by a known key and that the data has not been tampered with since it was signed. + +## Usage + +``` +verify(algorithm, key, signature, data) +``` + +## Parameters + +| Name | Type | Description | +| :---------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------- | +| `algorithm` | `string` or object with a single `name` string property (`{name: "RSASSA-PKCS1-v1_5"}`) or an [EcdsaParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/ecdsaparams/), [HmacKeyGenParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/hmackeygenparams/), or [RsaPssParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/rsapssparams/) object. | The signature algorithm to use. | +| `key` | [CryptoKey](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/cryptokey) | The key that will be used to verify the signature. | +| `signature` | `ArrayBuffer` | The signature to verify. | +| `data` | `ArrayBuffer` | The data whose signature is to be verified. | + +### Supported algorithms + +{{< docs/shared source="k6" lookup="webcrypto/supported-sign-verify.md" version="" >}} + +## Return Value + +A `Promise` that resolves to a `boolean` value indicating if the signature is valid. + +## Throws + +| Type | Description | +| :------------------- | :------------------------------------------------------------------------------------------------------------------ | +| `InvalidAccessError` | Raised when the key either does not support the `verify` operation, or is incompatible with the selected algorithm. | + +## Examples + +### Verifying an HMAC signature + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const generatedKey = await crypto.subtle.generateKey( + { + name: 'HMAC', + hash: { name: 'SHA-1' }, + }, + true, + ['sign', 'verify'] + ); + + const data = string2ArrayBuffer('Hello World'); + + /** + * Signes the encoded data with the provided key using the HMAC algorithm + * the returned signature can be verified using the verify method. + */ + const signature = await crypto.subtle.sign('HMAC', generatedKey, data); + + /** + * Verifies the signature of the encoded data with the provided key using the HMAC algorithm. + */ + const verified = await crypto.subtle.verify('HMAC', generatedKey, signature, data); + + console.log('verified: ', verified); +} + +function string2ArrayBuffer(str) { + const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char + const bufView = new Uint16Array(buf); + for (let i = 0, strLen = str.length; i < strLen; i++) { + bufView[i] = str.charCodeAt(i); + } + return buf; +} +``` + +{{< /code >}} + +### Verifying an ECDSA signature + +{{< code >}} + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + const publicKey = await crypto.subtle.importKey( + 'spki', + spkiPublicKeyData, + { name: 'ECDSA', namedCurve: 'P-256' }, + true, + ['verify'] + ); + + // Verifies the signature of the encoded data with the provided key + const verified = await crypto.subtle.verify( + { + name: 'ECDSA', + hash: 'SHA-256', + }, + publicKey, + signature, + plaintText + ); + + console.log('verified: ', verified); +} + +const spkiPublicKeyData = new Uint8Array([ + 48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, + 4, 10, 5, 30, 56, 111, 103, 196, 166, 225, 229, 203, 238, 125, 55, 116, 91, 88, 142, 190, 114, 15, + 117, 89, 22, 40, 111, 150, 41, 105, 122, 57, 23, 17, 216, 106, 234, 201, 103, 8, 210, 58, 38, 35, + 216, 198, 237, 187, 84, 217, 164, 63, 100, 6, 105, 49, 128, 15, 53, 29, 158, 117, 235, 238, 30, +]); + +const plaintText = new Uint8Array([ + 95, 77, 186, 79, 50, 12, 12, 232, 118, 114, 90, 252, 229, 251, 210, 91, 248, 62, 90, 113, 37, 160, + 140, 175, 231, 60, 62, 186, 196, 33, 119, 157, 249, 213, 93, 24, 12, 58, 233, 148, 38, 69, 225, + 216, 47, 238, 140, 157, 41, 75, 60, 177, 160, 138, 153, 49, 32, 27, 60, 14, 129, 252, 71, 202, + 207, 131, 21, 162, 175, 102, 50, 65, 19, 195, 182, 98, 48, 195, 70, 8, 196, 244, 89, 54, 52, 206, + 2, 178, 103, 54, 34, 119, 240, 168, 64, 202, 116, 188, 61, 26, 98, 54, 149, 44, 94, 215, 170, 248, + 168, 254, 203, 221, 250, 117, 132, 230, 151, 140, 234, 93, 42, 91, 159, 183, 241, 180, 140, 139, + 11, 229, 138, 48, 82, 2, 117, 77, 131, 118, 16, 115, 116, 121, 60, 240, 38, 170, 238, 83, 0, 114, + 125, 131, 108, 215, 30, 113, 179, 69, 221, 178, 228, 68, 70, 255, 197, 185, 1, 99, 84, 19, 137, + 13, 145, 14, 163, 128, 152, 74, 144, 25, 16, 49, 50, 63, 22, 219, 204, 157, 107, 225, 104, 184, + 72, 133, 56, 76, 160, 62, 18, 96, 10, 193, 194, 72, 2, 138, 243, 114, 108, 201, 52, 99, 136, 46, + 168, 192, 42, 171, +]); + +const signature = new Uint8Array([ + 83, 223, 63, 226, 42, 29, 106, 105, 225, 145, 197, 180, 118, 154, 109, 110, 66, 67, 47, 251, 53, + 190, 203, 65, 207, 36, 19, 57, 49, 122, 124, 118, 59, 74, 222, 134, 42, 235, 180, 229, 134, 24, + 205, 81, 171, 156, 100, 218, 127, 242, 126, 53, 27, 77, 249, 101, 157, 132, 244, 30, 67, 30, 64, + 12, +]); +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/_index.md new file mode 100644 index 000000000..7a521873e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/_index.md @@ -0,0 +1,123 @@ +--- +title: 'websockets' +description: 'k6 websockets experimental API' +weight: 05 +--- + +# websockets + +{{< docs/shared source="k6" lookup="experimental-module.md" version="" >}} + +This experimental API implements the browser [WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) with additional k6-specific functionalities (cookies, tags, headers and so on). + +The main difference between this module and [`k6/ws`](https://grafana.com/docs/k6//javascript-api/k6-ws) is that this module uses a global event loop instead of a local one. +A global event loop lets a single VU have multiple concurrent connections, which improves performance. + +The `WebSocket API` is not fully implemented, and we're working on it, but we believe it's usable for most users. So whether you're writing a new WebSocket test, or currently using the `k6/ws` module, we invite you to give it a try, and report any issues in the project's [issue tracker](https://github.com/grafana/xk6-websockets/). Our midterm goal is to make this module part of k6 core, and long-term to replace the [`k6/ws`](https://grafana.com/docs/k6//javascript-api/k6-ws) module. + +| Class/Method | Description | +| --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [Params](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/params) | Used for setting various WebSocket connection parameters such as headers, cookie jar, compression, etc. | +| [WebSocket(url, protocols, params)](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket) | Constructs a new WebSocket connection. | +| [WebSocket.close()](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-close) | Close the WebSocket connection. | +| [WebSocket.ping()](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-ping) | Send a ping. | +| [WebSocket.send(data)](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-send) | Send data. | +| [WebSocket.addEventListener(event, handler)](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-addeventlistener) | Add an event listener on the connection for specific event. | +| [Blob](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/blob) | Interface that represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a [ReadableStream](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestream). | + +A WebSocket instance also has the following properties: + + + +| Class/Property | Description | +| --------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| WebSocket.readyState | The current state of the connection. Could be one of [the four states](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState). | +| WebSocket.url | The URL of the connection as resolved by the constructor. | +| WebSocket.bufferedAmount | The number of bytes of data that have been queued using calls to `send()` but not yet transmitted to the network. | +| WebSocket.binaryType | The [`binaryType`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/binaryType) controls the type of binary data being received over the WebSocket connection. Use `"blob"` to receive it as `Blob`, or `"arraybuffer"` to receive it as `ArrayBuffer`. The default value is `"blob"`. | +| [WebSocket.onmessage](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-onmessage) | A handler for `message` events. | +| [WebSocket.onerror](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-onerror) | A handler for `error` events. | +| [WebSocket.onopen](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-onopen) | A handler for `open` events. | +| [WebSocket.onclose](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-onclose) | A handler for `close` events. | +| [WebSocket.onping](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-onping) | A handler for `ping` events. | +| [WebSocket.onpong](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-onpong) | A handler for `pong` events. | + + + +## Websocket metrics + +k6 takes specific measurements for Websockets. +For the complete list, refer to the [Metrics reference](https://grafana.com/docs/k6//using-k6/metrics/reference#websockets). + +## Example + +This example shows: + +- How a single VU can run multiple WebSockets connections asynchronously. +- How to use the timeout and interval functions to stop the connections after some period. + +{{< code >}} + +```javascript +import { randomString, randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.1.0/index.js'; +import { WebSocket } from 'k6/experimental/websockets'; + +const chatRoomName = 'publicRoom'; // choose any chat room name +const sessionDuration = randomIntBetween(5000, 60000); // user session between 5s and 1m + +export default function () { + for (let i = 0; i < 4; i++) { + startWSWorker(i); + } +} + +function startWSWorker(id) { + // create a new websocket connection + const ws = new WebSocket(`wss://test-api.k6.io/ws/crocochat/${chatRoomName}/`); + ws.binaryType = 'arraybuffer'; + + ws.addEventListener('open', () => { + // change the user name + ws.send(JSON.stringify({ event: 'SET_NAME', new_name: `Croc ${__VU}:${id}` })); + + // listen for messages/errors and log them into console + ws.addEventListener('message', (e) => { + const msg = JSON.parse(e.data); + if (msg.event === 'CHAT_MSG') { + console.log(`VU ${__VU}:${id} received: ${msg.user} says: ${msg.message}`); + } else if (msg.event === 'ERROR') { + console.error(`VU ${__VU}:${id} received:: ${msg.message}`); + } else { + console.log(`VU ${__VU}:${id} received unhandled message: ${msg.message}`); + } + }); + + // send a message every 2-8 seconds + const intervalId = setInterval(() => { + ws.send(JSON.stringify({ event: 'SAY', message: `I'm saying ${randomString(5)}` })); + }, randomIntBetween(2000, 8000)); // say something every 2-8 seconds + + // after a sessionDuration stop sending messages and leave the room + const timeout1id = setTimeout(function () { + clearInterval(intervalId); + console.log(`VU ${__VU}:${id}: ${sessionDuration}ms passed, leaving the chat`); + ws.send(JSON.stringify({ event: 'LEAVE' })); + }, sessionDuration); + + // after a sessionDuration + 3s close the connection + const timeout2id = setTimeout(function () { + console.log(`Closing the socket forcefully 3s after graceful LEAVE`); + ws.close(); + }, sessionDuration + 3000); + + // when connection is closing, clean up the previously created timers + ws.addEventListener('close', () => { + clearTimeout(timeout1id); + clearTimeout(timeout2id); + console.log(`VU ${__VU}:${id}: disconnected`); + }); + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/blob/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/blob/_index.md new file mode 100644 index 000000000..3291fd90d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/blob/_index.md @@ -0,0 +1,27 @@ +--- +title: 'Blob' +description: 'Interface that represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream.' +weight: 10 +--- + +# Blob + +`Blob` is an interface that represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a [ReadableStream](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestream). + +It's the type of the data received on [WebSocket.onmessage](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-onmessage) when `WebSocket.binaryType` is set to `"blob"`. + +A `Blob` instance has the following methods/properties: + + + +| Class/Property | Description | +|--------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| Blob.size | The number of bytes of data contained within the `Blob`. | +| Blob.type | A string containing the MIME type, or an empty string if the type could not be determined. | +| Blob.arrayBuffer() | Returns a `Promise` that resolves with the contents of the blob as binary data contained in an `ArrayBuffer`. | +| Blob.bytes() | Returns a `Promise` that resolves with a `Uint8Array` containing the contents of the blob as an array of bytes. | +| Blob.slice() | Returns a new `Blob` object which contains data from a subset of the blob on which it's called. | +| Blob.stream() | Returns a [ReadableStream](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams/readablestream) which upon reading returns the data contained within the `Blob`. | +| Blob.text() | Returns a `Promise` that resolves with a string containing the contents of the blob, interpreted as `UTF-8`. | + + diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/params.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/params.md new file mode 100644 index 000000000..8ed8bf888 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/params.md @@ -0,0 +1,53 @@ +--- +title: 'Params' +description: 'Used for setting various WebSocket request-specific parameters such as headers, tags, etc.' +weight: 20 +--- + +# Params + +`Params` is an object used by the WebSocket constructor. The `Params` object contains request-specific options, such as headers that should be inserted into the connection initialization request. + +| Name | Type | Description | +| -------------------- | ------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Params.compression` | string | Compression algorithm to be used by the WebSocket connection. The only supported algorithm currently is `deflate`. | +| `Params.jar` | [http.CookieJar](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar) | The cookie jar that will be used when making the initial HTTP request to establish the WebSocket connection. If empty, the [default VU cookie jar](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar) will be used. | +| `Params.headers` | object | Custom HTTP headers in key-value pairs that will be added to the initial HTTP request to establish the WebSocket connection. Keys are header names and values are header values. | +| `Params.tags` | object | [Custom metric tags](https://grafana.com/docs/k6//using-k6/tags-and-groups#user-defined-tags) in key-value pairs where the keys are names of tags and the values are tag values. The WebSocket connection will [generate metrics samples](https://grafana.com/docs/k6//javascript-api/k6-ws/socket#websocket-built-in-metrics) with these tags attached, allowing users to filter the results data or set [thresholds on sub-metrics](https://grafana.com/docs/k6//using-k6/thresholds#thresholds-on-tags). | + +### Example of custom metadata headers and tags + +_A k6 script that makes a WebSocket request with a custom header and tags results data with a specific tag_ + +{{< code >}} + +```javascript +import { WebSocket } from 'k6/experimental/websockets'; + +export default function () { + const url = 'ws://localhost:10000'; + const params = { + headers: { 'X-MyHeader': 'k6test' }, + tags: { k6test: 'yes' }, + }; + + const ws = new WebSocket(url, null, params); + + ws.onopen = () => { + console.log('WebSocket connection established!'); + ws.close(); + }; +} +``` + +{{< /code >}} + +The preceding example uses a WebSocket echo server, which you can run with the following command: + +{{< code >}} + +```bash +$ docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/_index.md new file mode 100644 index 000000000..068cdaa45 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/_index.md @@ -0,0 +1,58 @@ +--- +title: 'WebSocket' +description: 'Create a WebSocket connection, and provides a WebSocket instance to interact with the service.' +weight: 10 +--- + +# WebSocket + +Creates a WebSocket instance for connection to a remote host. + +The following events can close the connection: + +- remote host close event. +- [WebSocket.close()](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-close). +- k6 VU interruption based on test configuration or CLI commands. + +| Parameter | Type | Description | +| --------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- | +| url | string | The URL to which to connect (e.g. "ws://localhost:10000"). | +| protocols | array | Not yet implemented, reserved for the future use. | +| params | object | [Params](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/params) object containing additional request parameters. | + +### Returns + +| Type | Description | +| --------- | -------------------------------- | +| WebSocket | An instance of WebSocket object. | + +### Example + +_A k6 script that initiates a WebSocket connection._ + +{{< code >}} + +```javascript +import { WebSocket } from 'k6/experimental/websockets'; + +export default function () { + const ws = new WebSocket('ws://localhost:10000'); + + ws.onopen = () => { + console.log('WebSocket connection established!'); + ws.close(); + }; +} +``` + +{{< /code >}} + +The preceding example uses a WebSocket echo server, which you can run with the following command: + +{{< code >}} + +```bash +$ docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-addeventlistener.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-addeventlistener.md new file mode 100644 index 000000000..21e6d3136 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-addeventlistener.md @@ -0,0 +1,66 @@ +--- +title: 'WebSocket.addEventListener(event, handler)' +description: 'Set up handler functions for various events on the WebSocket connection.' +weight: 10 +--- + +# WebSocket.addEventListener(event, handler) + +Set up handler functions for various events on the WebSocket connection. You can define multiple handlers for the same event. + +| Parameter | Type | Description | +| --------- | -------- | -------------------------------------------- | +| event | string | The event name to define a handler for. | +| handler | function | The function to call when the event happens. | + +| Event name | Description | +| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| open | Emitted when the connection is established. | +| message | Emitted when a message is received from the server. | +| ping | Emitted when a ping is received from the server. The client will automatically send back a `pong`. | +| pong | Emitted when a pong is received from the server. | +| close | Emitted when the connection is closed by the client [WebSocket.close()](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-close) or when the server sends the `close` event with code status 1000 (normal closure). | +| error | Emitted when an error occurs. | + +### Example + +_A k6 script that demonstrates how to add multiple event listeners for the WebSocket `message` connection event._ + +{{< code >}} + +```javascript +import { WebSocket } from 'k6/experimental/websockets'; + +export default function () { + const ws = new WebSocket('ws://localhost:10000'); + ws.binaryType = "arraybuffer"; + + ws.onopen = () => { + console.log('connected'); + ws.send(Date.now().toString()); + }; + + ws.onmessage = () => { + console.log('onmessage event handler!'); + }; + + // Multiple event handlers on the same event + ws.addEventListener('message', () => { + console.log('addEventListener event handler!'); + + ws.close(); + }); +} +``` + +{{< /code >}} + +The preceding example uses a WebSocket echo server, which you can run with the following command: + +{{< code >}} + +```bash +$ docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-close.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-close.md new file mode 100644 index 000000000..e9d1f2604 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-close.md @@ -0,0 +1,44 @@ +--- +title: 'WebSocket.close([code])' +description: 'Close the WebSocket connection.' +weight: 15 +--- + +# WebSocket.close([code]) + +Close the WebSocket connection. + +| Parameter | Type | Description | +| --------------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------- | +| code (optional) | number | WebSocket status code. (default: 1000) . See [the list of supported](https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code) close codes. | + +### Example + +_A k6 script that initiates a WebSocket connection and closes it using the `onopen` handler._ + +{{< code >}} + +```javascript +import { WebSocket } from 'k6/experimental/websockets'; + +export default function () { + const ws = new WebSocket('ws://localhost:10000'); + + ws.onopen = () => { + ws.close(); + console.log('connection closed'); + }; +} +``` + +{{< /code >}} + +The preceding example uses a WebSocket echo server, which you can run with the following command: + +{{< code >}} + +```bash +$ docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onclose.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onclose.md new file mode 100644 index 000000000..001527a4e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onclose.md @@ -0,0 +1,46 @@ +--- +title: 'WebSocket.onclose' +description: 'A handler function for WebSocket connection close event.' +weight: 30 +--- + +# WebSocket.onclose + +A handler for a WebSocket connection `close` event. +For multiple, simultaneous event handlers, use [`WebSocket.addEventListener()`](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-addeventlistener). + +### Example + +_A k6 script that initiates a WebSocket connection, sends a ping, and closes it using `onopen` handler. +The console should also log `WebSocket connection closed` from the `onclose` handler._ + +{{< code >}} + +```javascript +import { WebSocket } from 'k6/experimental/websockets'; + +export default function () { + const ws = new WebSocket('ws://localhost:10000'); + + ws.onopen = () => { + console.log('WebSocket connection established!'); + ws.close(); + }; + + ws.onclose = () => { + console.log('WebSocket connection closed!'); + }; +} +``` + +{{< /code >}} + +The preceding example uses a WebSocket echo server, which you can run with the following command: + +{{< code >}} + +```bash +$ docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onerror.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onerror.md new file mode 100644 index 000000000..b44c79199 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onerror.md @@ -0,0 +1,41 @@ +--- +title: 'WebSocket.onerror' +description: 'A handler function for WebSocket connection error event.' +weight: 30 +--- + +# WebSocket.onerror + +A handler for a WebSocket connection `error` event. +For multiple, simultaneous event handlers, use [`WebSocket.addEventListener()`](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-addeventlistener). + +### Example + +_A k6 script that initiates a WebSocket connection and sets up a handler for the `error` event._ + +{{< code >}} + +```javascript +import { WebSocket } from 'k6/experimental/websockets'; + +export default function () { + const ws = new WebSocket('ws://localhost:10000'); + + ws.onerror = (e) => { + console.log(e); + ws.close(); + }; +} +``` + +{{< /code >}} + +The preceding example uses a WebSocket echo server, which you can run with the following command: + +{{< code >}} + +```bash +$ docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onmessage.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onmessage.md new file mode 100644 index 000000000..601711e65 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onmessage.md @@ -0,0 +1,43 @@ +--- +title: 'WebSocket.onmessage' +description: 'A handler function for message event WebSocket.' +weight: 30 +--- + +# WebSocket.onmessage + +A handler for a WebSocket connection `message` event. +For multiple, simultaneous event handlers, use [`WebSocket.addEventListener()`](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-addeventlistener). + +### Example + +_A k6 script that initiates a WebSocket connection and sets up a handler for the `message` event._ + +{{< code >}} + +```javascript +import { WebSocket } from 'k6/experimental/websockets'; + +export default function () { + const ws = new WebSocket('ws://localhost:10000'); + ws.binaryType = 'arraybuffer'; + + ws.onmessage = (data) => { + console.log('a message received'); + console.log(data); + ws.close(); + }; +} +``` + +{{< /code >}} + +The preceding example uses a WebSocket echo server, which you can run with the following command: + +{{< code >}} + +```bash +$ docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onopen.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onopen.md new file mode 100644 index 000000000..ba6cd48ef --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onopen.md @@ -0,0 +1,41 @@ +--- +title: 'WebSocket.onopen' +description: 'A handler function for WebSocket connection open event.' +weight: 30 +--- + +# WebSocket.onopen + +A handler for a WebSocket connection `open` event. +For multiple, simultaneous event handlers, use [`WebSocket.addEventListener()`](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-addeventlistener). + +### Example + +_A k6 script that initiates a WebSocket connection and sets up a handler for the `open` event._ + +{{< code >}} + +```javascript +import { WebSocket } from 'k6/experimental/websockets'; + +export default function () { + const ws = new WebSocket('ws://localhost:10000'); + + ws.onopen = () => { + console.log('WebSocket connection established!'); + ws.close(); + }; +} +``` + +{{< /code >}} + +The preceding example uses a WebSocket echo server, which you can run with the following command: + +{{< code >}} + +```bash +$ docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onping.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onping.md new file mode 100644 index 000000000..f80573450 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onping.md @@ -0,0 +1,42 @@ +--- +title: 'WebSocket.onping' +description: 'A handler function for WebSocket connection ping event.' +weight: 30 +--- + +# WebSocket.onping + +A handler for a WebSocket connection `ping` event. +For multiple, simultaneous event handlers, use [`WebSocket.addEventListener()`](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-addeventlistener). + +### Example + +_A k6 script that initiates a WebSocket connection and sets up a handler for the `ping` event._ + +{{< code >}} + +```javascript +import { WebSocket } from 'k6/experimental/websockets'; + +export default function () { + const ws = new WebSocket('wss://test-api.k6.io/ws/crocochat/publicRoom/'); + + ws.onping = () => { + console.log('A ping happened!'); + ws.close(); + }; + + ws.onclose = () => { + console.log('WebSocket connection closed!'); + } + + ws.onopen = () => { + ws.send(JSON.stringify({ 'event': 'SET_NAME', 'new_name': `Croc ${__VU}` })); + } + ws.onerror = (err) => { + console.log(err) + } +} +``` + +{{< /code >}} \ No newline at end of file diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onpong.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onpong.md new file mode 100644 index 000000000..86da7a541 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-onpong.md @@ -0,0 +1,45 @@ +--- +title: 'WebSocket.onpong' +description: 'A handler function for WebSocket connection pong event.' +weight: 30 +--- + +# WebSocket.onpong + +A handler for a WebSocket connection `pong` event. +For multiple, simultaneous event handlers, use [`WebSocket.addEventListener()`](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets/websocket/websocket-addeventlistener). + +### Example + +_A k6 script that initiates a WebSocket connection and setups a handler for the `pong` event._ + +{{< code >}} + +```javascript +import { WebSocket } from 'k6/experimental/websockets'; + +export default function () { + const ws = new WebSocket('ws://localhost:10000'); + + ws.onpong = () => { + console.log('A pong happened!'); + ws.close(); + }; + + ws.onopen = () => { + ws.ping(); + } +} +``` + +{{< /code >}} + +The preceding example uses a WebSocket echo server, which you can run with the following command: + +{{< code >}} + +```bash +$ docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-ping.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-ping.md new file mode 100644 index 000000000..32d4bf9c6 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-ping.md @@ -0,0 +1,46 @@ +--- +title: 'WebSocket.ping()' +description: 'Send a ping. Ping messages can be used to verify that the remote endpoint is responsive.' +weight: 20 +--- + +# WebSocket.ping() + +Send a ping. You can use ping messages to verify that the remote endpoint is responsive. + +### Example + +_A k6 script that initiates a WebSocket connection, sends a ping, and closes it using the `onopen` handler. The console should log `connection is alive`, since the recipient should automatically emit the `pong` event._ + +{{< code >}} + +```javascript +import { WebSocket } from 'k6/experimental/websockets'; + +export default function () { + const ws = new WebSocket('ws://localhost:10000'); + + ws.onopen = () => { + console.log('WebSocket connection established!'); + ws.ping(); + ws.close(); + }; + + ws.onpong = () => { + // As required by the spec, when the ping is received, the recipient must send back a pong. + console.log('connection is alive'); + }; +} +``` + +{{< /code >}} + +The preceding example uses a WebSocket echo server, which you can run with the following command: + +{{< code >}} + +```bash +$ docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-send.md b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-send.md new file mode 100644 index 000000000..cc8f4a32c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-experimental/websockets/websocket/websocket-send.md @@ -0,0 +1,45 @@ +--- +title: 'WebSocket.send(data)' +description: 'Send data through the connection.' +weight: 10 +--- + +# WebSocket.send(data) + +Send data through the connection. +You can use `JSON.stringify` to convert a JSON or JavaScript values to a JSON string. + +| Parameter | Type | Description | +| --------- | --------------------------------------------- | ----------------- | +| data | string / ArrayBuffer / Blob / ArrayBufferView | The data to send. | + +### Example + +_A k6 script that demonstrates how to add an event listener for the `open` WebSocket connection event sends a message and closes the connection._ + +{{< code >}} + +```javascript +import { WebSocket } from 'k6/experimental/websockets'; + +export default function () { + const ws = new WebSocket('ws://localhost:10000'); + + ws.onopen = () => { + ws.send('lorem ipsum'); + ws.close(); + }; +} +``` + +{{< /code >}} + +The preceding example uses a WebSocket echo server, which you can run with the following command: + +{{< code >}} + +```bash +$ docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/_index.md new file mode 100644 index 000000000..bafdd3fcd --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/_index.md @@ -0,0 +1,9 @@ +--- +title: 'k6/html' +description: 'The k6/html module contains functionality for HTML parsing.' +weight: 08 +--- + +# k6/html + +{{< docs/shared source="k6" lookup="javascript-api/k6-html.md" version="" >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/element/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/element/_index.md new file mode 100644 index 000000000..d15c593d5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/element/_index.md @@ -0,0 +1,164 @@ +--- +title: 'Element' +description: 'An HTML DOM element as returned by the Selection API.' +description: 'An HTML DOM element as returned by the Selection API.' +weight: 20 +--- + +# Element + +Represents a DOM element matched by a [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection), +and provides an API to inspect the element content. + +Use [Selection.get(index)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-get) to return an Element object. + +The Element object provides a similar API to the [DOM Element API](https://developer.mozilla.org/en-US/Web/API/Element) to retrieve element information. + +| Method | Description | +| ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| [selection](https://grafana.com/docs/k6//javascript-api/k6-html/element/element-selection) | The selection matching the element. | +| nodeName | The name of the element. | +| nodeType | The type of the element. | +| nodeValue | The element value. | +| id | The id of the element. | +| innerHTML | Is a DOMString representing the markup of the element's content. | +| textContent | The element content. | +| ownerDocument | [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) | +| attributes | An array of attributes. | +| firstChild | [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) | +| lastChild | [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) | +| childElementCount | The number of children elements. | +| firstElementChild | [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) | +| lastElementChild | [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) | +| previousSibling | [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) | +| nextSibling | [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) | +| previousElementSibling | [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) | +| nextElementSibling | [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) | +| parentElement | [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) | +| parentNode | [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) | +| childNodes | Array of [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) | +| children | Array of [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) | +| classList | An array of class names. | +| className | The class name string | +| lang | The value of the lang attribute. | +| toString | The element string representation. | +| hasAttribute | Boolean | +| getAttribute | getAttributeNode | +| hasAttributes | Boolean | +| hasChildNodes | Boolean | +| isSameNode | Boolean | +| isEqualNode | Boolean | +| getElementsByClassName | Return an array of [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element). | +| getElementsByTagName | Return an array of [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element). | +| querySelector | Returns the first [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) which matches the specified selector string relative to the element | +| querySelectorAll | Returns all the [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) which matches the specified selector string relative to the element | +| contains | | +| matches | Returns a Boolean indicating whether or not the element would be selected by the specified selector string | +| namespaceURI | The namespace URI of the element. | +| isDefaultNamespace | Returns a Boolean indicating whether the element has the default namespace. | + +Additionally, Element can provide more methods depending on the Element type. + +- **AnchorElement**: hash, host, hostname, port, username, password, origin, pathname, protocol, relist, search, text. + +- **ButtonElement**: form, formAction, formEnctype, formMethod, formNoValidate, formTarget, labels, name, value. + +- **CanvasElement**: width, height + +- **DataListElement**: options + +- **FieldSetElement**: elements, type, form + +- **FormElement**: elements, length, method + +- **InputElement**: form + +- **LabelElement**: control, form + +- **LegendElement**: form + +- **LinkElement**: relList + +- **MapElement**: areas, images + +- **ObjectElement**: form + +- **OptionElement**: disabled, form, index, label, text, value + +- **OutputElement**: value, labels + +- **ProgressElement**: max, value, position + +- **ScriptElement**: text + +- **SelectElement**: form, length, options, selectedOptions, selectedIndex, value + +- **StyleElement**: text + +- **TableElement**: caption, thead, tbody, tfoot, rows + +- **TableCellElement**: cellIndex, colSpan, rowSpan, headers + +- **TableRowElement**: cells, colSpan, sectionRowIndex, rowIndex + +- **VideoElement**: textTracks + +- **TitleElement**: text + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
Value term 1
+
Value term 2
+
+ `; + const sel = parseHTML(content).find('dl').children(); + + const el1 = sel.get(0); + const el2 = sel.get(1); + + console.log(el1.nodeName()); + console.log(el1.id()); + console.log(el1.textContent()); + + console.log(el2.nodeName()); + console.log(el2.id()); + console.log(el2.textContent()); + + sleep(1); +} +``` + +{{< /code >}} + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +\t6 + `; + const el = parseHTML(content).find('a').get(0); + + console.log(el.nodeName()); + console.log(el.innerHTML()); + console.log(el.host()); + console.log(el.hostname()); + console.log(el.protocol()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/element/element-selection.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/element/element-selection.md new file mode 100644 index 000000000..c029123e1 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/element/element-selection.md @@ -0,0 +1,34 @@ +--- +title: 'Element.selection()' +description: 'Retrieve the Selection matching this element.' +--- + +# Element.selection() + +Retrieve the Selection matching this element. + +Mimics `$(element)`. + +### Returns + +| Type | Description | +| ------- | ------------------------------------ | +| Selection | The Selection matching this element. | + +### Example + +{{< code >}} + +```javascript +import http from "k6/http"; + +export default () => { + let li = http.get("https://test.k6.io").html().find("li"); + li.each(function(_, element) { + let container = element.selection().closest('ul.header-icons'); + console.log("li.each", container.html()) + }); +} + +``` +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/parsehtml.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/parsehtml.md new file mode 100644 index 000000000..9e621beae --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/parsehtml.md @@ -0,0 +1,38 @@ +--- +title: 'parseHTML( src )' +description: 'Parse an HTML string and populate a Selection object.' +description: 'Parse an HTML string and populate a Selection object.' +weight: 10 +--- + +# parseHTML( src ) + +Parse an HTML string and populate a [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) object. + +| Parameter | Type | Description | +| --------- | ------ | ------------ | +| src | string | HTML source. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------------- | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A Selection object. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import http from 'k6/http'; + +export default function () { + const res = http.get('https://k6.io'); + const doc = parseHTML(res.body); // equivalent to res.html() + const pageTitle = doc.find('head title').text(); + const langAttr = doc.find('html').attr('lang'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/_index.md new file mode 100644 index 000000000..8a59f4bd5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/_index.md @@ -0,0 +1,73 @@ +--- +title: 'Selection' +description: 'A jQuery-like API for accessing HTML DOM elements.' +description: 'A jQuery-like API for accessing HTML DOM elements.' +weight: 50 +weight: 50 +--- + +# Selection + +Represents a set of nodes in a DOM tree. + +Selections have a jQuery-compatible API, but with two caveats: + +- CSS and screen layout are not processed, thus calls like css() and offset() are unavailable. +- DOM trees are read-only, you can't set attributes or otherwise modify nodes. + +(Note that the read-only nature of the DOM trees is purely to avoid a maintenance burden on code with seemingly no practical use - if a compelling use case is presented, modification can easily be implemented.) + +| Method | Description | +| ------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------ | +| [Selection.attr(name)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-attr) | Get the value of an attribute for the first element in the Selection. | +| [Selection.children([selector])](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-children) | Get the children of each element in the set of matched elements, optionally filtered by a selector. | +| [Selection.closest(selector)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-closest) | Get the first element that matches the selector by testing the element itself and traversing up through its ancestors | +| [Selection.contents()](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-contents) | Get the children of each element in the set of matched elements, including text and comment nodes. | +| [Selection.data([key])](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-data) | Return the value at the named data store for the first element in the set of matched elements. | +| [Selection.each(fn)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-each) | Iterate and execute a function for each matched element. | +| [Selection.eq(index)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-eq) | Reduce the set of matched elements to the one at the specified index. | +| [Selection.filter(selector)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-filter) | Reduce the set of matched elements to those that match the selector or pass the function's test. | +| [Selection.find(selector)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-find) | Find the selection descendants, filtered by a selector. | +| [Selection.first()](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-first) | Reduce the set of matched elements to the first in the set. | +| [Selection.get(index)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-get) | Retrieve the [Element (k6/html)](https://grafana.com/docs/k6//javascript-api/k6-html/element) matched by the selector | +| [Selection.has(selector)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-has) | Reduce the set of matched elements to those that have a descendant that matches the selector | +| [Selection.html()](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-html) | Get the HTML contents of the first element in the set of matched elements | +| [Selection.is(selector)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-is) | Check the current matched set of elements against a selector or element and return true if at least one of these elements matches the given arguments. | +| [Selection.last()](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-last) | Reduce the set of matched elements to the final one in the set. | +| [Selection.map(fn)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-map) | Pass each selection in the current matched set through a function, producing a new Array containing the return values. | +| [Selection.nextAll([selector])](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-nextall) | Get all following siblings of each element in the set of matched elements, optionally filtered by a selector. | +| [Selection.next([selector])](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-next) | Get the immediately following sibling of each element in the set of matched element | +| [Selection.nextUntil([selector], [filter])](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-nextuntil) | Get all following siblings of each element up to but not including the element matched by the selector. | +| [Selection.not(selector)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-not) | Remove elements from the set of matched elements | +| [Selection.parent([selector])](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-parent) | Get the parent of each element in the current set of matched elements, optionally filtered by a selector. | +| [Selection.parents([selector])](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-parents) | Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector. | +| [Selection.parentsUntil([selector], [filter])](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-parentsuntil) | Get the ancestors of each element in the current set of matched elements, up to but not including the element matched by the selector. | +| [Selection.prevAll([selector])](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-prevall) | Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector. | +| [Selection.prev([selector])](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-prev) | Get the immediately preceding sibling of each element in the set of matched elements. | +| [Selection.prevUntil([selector], [filter])](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-prevuntil) | Get all preceding siblings of each element up to but not including the element matched by the selector. | +| [Selection.serialize()](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-serialize) | Encode a set of form elements as a string in standard URL-encoded notation for submission. | +| [Selection.serializeArray()](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-serializearray) | Encode a set of form elements as an array of names and values. | +| [Selection.serializeObject()](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-serializeobject) | Encode a set of form elements as an object. | +| [Selection.size()](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-size) | Return the number of elements in the Selection. | +| [Selection.slice(start [, end])](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-slice) | Reduce the set of matched elements to a subset specified by a range of indices. | +| [Selection.text()](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-text) | Get the text content of the selection. | +| [Selection.toArray()](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-toarray) | Retrieve all the elements contained in the Selection, as an array. | +| [Selection.val()](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-val) | Get the current value of the first element in the set of matched elements. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import http from 'k6/http'; + +export default function () { + const res = http.get('https://k6.io'); + const doc = parseHTML(res.body); + const pageTitle = doc.find('head title').text(); + const langAttr = doc.find('html').attr('lang'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-attr.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-attr.md new file mode 100644 index 000000000..a4fabd973 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-attr.md @@ -0,0 +1,36 @@ +--- +title: 'Selection.attr(name)' +description: 'Get the value of an attribute for the first element in the Selection.' +--- + +# Selection.attr(name) + +Get the value of an attribute for the first element in the Selection. +Mimics [jquery.attr](https://api.jquery.com/attr/) + +| Parameter | Type | Description | +| --------- | ------ | -------------------------------- | +| name | string | The name of the attribute to get | + +### Returns + +| Type | Description | +| ------ | -------------------------- | +| string | The value of the attribute | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import http from 'k6/http'; + +export default function () { + const res = http.get('https://k6.io'); + const doc = parseHTML(res.body); + const langAttr = doc.find('html').attr('lang'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-children.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-children.md new file mode 100644 index 000000000..de08abd75 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-children.md @@ -0,0 +1,58 @@ +--- +title: 'Selection.children([selector])' +description: 'Get the children of each element in the set of matched elements, optionally filtered by a selector.' +--- + +# Selection.children([selector]) + +Get the children of each element in the set of matched elements, optionally filtered by a selector. +Mimics [jquery.children](https://api.jquery.com/children/) + +| Parameter | Type | Description | +| ------------------- | ------ | -------------------------------------------------------------------- | +| selector (optional) | string | A string containing a selector expression to match elements against. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ----------------------- | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | The children Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + const sel = doc.find('dl'); + + console.log(sel.children().size()); + console.log(sel.children('dt').size()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-closest.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-closest.md new file mode 100644 index 000000000..0ce23d805 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-closest.md @@ -0,0 +1,57 @@ +--- +title: 'Selection.closest(selector)' +description: 'For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.' +--- + +# Selection.closest(selector) + +For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree. +Mimics [jquery.closest](https://api.jquery.com/closest/) + +| Parameter | Type | Description | +| --------- | ------ | ------------------------------------------------------------------- | +| selector | string | A string containing a selector expression to match elements against | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ----------- | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
    +
  • I
  • +
  • II +
      +
    • A
    • +
    • B +
        +
      • 1
      • +
      • 2
      • +
      • 3
      • +
      +
    • +
    • C
    • +
    +
  • +
  • III
  • +
+ `; + const doc = parseHTML(content); + + const sel = doc.find('li.item-a').closest('ul'); + console.log(sel.attr('class')); + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-contents.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-contents.md new file mode 100644 index 000000000..264673066 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-contents.md @@ -0,0 +1,54 @@ +--- +title: 'Selection.contents()' +description: 'Get the children of each element in the set of matched elements, including text and comment nodes.' +--- + +# Selection.contents() + +Get the children of each element in the set of matched elements, including text and comment nodes. +Mimics [jquery.contents](https://api.jquery.com/contents/). + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ----------- | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + + const sel = doc.find('dt'); + + console.log(sel.contents().text()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-data.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-data.md new file mode 100644 index 000000000..14d852ef2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-data.md @@ -0,0 +1,45 @@ +--- +title: 'Selection.data([key])' +description: 'Return the value at the named data store for the first element in the set of matched elements.' +--- + +# Selection.data([key]) + +Return the value at the named data store for the first element in the set of matched elements. +Mimics [jquery.data](https://api.jquery.com/data/) + +| Parameter | Type | Description | +| -------------- | ------ | ----------------------------------------- | +| key (optional) | string | A string naming the piece of data to set. | + +### Returns + +| Type | Description | +| ------ | ---------------------------------- | +| string | The value at the named data store. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +

Hola

+ `; + + const doc = parseHTML(content); + const sel = doc.find('h1'); + + console.log(sel.data().testID); + console.log(sel.data('test-id')); + console.log(sel.data('testId')); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-each.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-each.md new file mode 100644 index 000000000..0f18da3e4 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-each.md @@ -0,0 +1,52 @@ +--- +title: 'Selection.each(fn)' +description: 'Iterate over a Selection, executing a function for each matched element.' +--- + +# Selection.each(fn) + +Iterate over a [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection), executing a function for each matched element. +Mimics [jquery.each](https://api.jquery.com/each/) + +| Parameter | Type | Description | +| --------- | -------- | --------------------------------------------------------- | +| fn | function | A function to iterate all the Elements of the Collection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + + doc.find('dl').each(function (idx, el) { + console.log(el.innerHTML()); + }); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-eq.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-eq.md new file mode 100644 index 000000000..37a577c9b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-eq.md @@ -0,0 +1,60 @@ +--- +title: 'Selection.eq(index)' +description: 'Reduce the set of matched elements to the one at the specified index.' +--- + +# Selection.eq(index) + +Reduce the set of matched elements to the one at the specified index. +Mimics [jquery.eq](https://api.jquery.com/eq/). + +| Parameter | Type | Description | +| --------- | ------ | ---------------------------------------------------------- | +| index | Number | An integer indicating the 0-based position of the element. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------ | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + + const sel = doc.find('dt'); + + console.log(sel.eq(0).html()); + console.log(sel.eq(1).html()); + console.log(sel.eq(2).html()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-filter.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-filter.md new file mode 100644 index 000000000..3eacde516 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-filter.md @@ -0,0 +1,69 @@ +--- +title: 'Selection.filter(selector)' +description: 'Reduce the set of matched elements to those that match the selector or pass the function test.' +--- + +# Selection.filter(selector) + +Reduce the set of matched elements to those that match the selector or pass the function's test. +Mimics [jquery.filter](https://api.jquery.com/filter/) + +| Parameter | Type | Description | +| --------- | -------------------------------------------------------------------------------------- | -------------------------------------------------------------------- | +| selector | function | A function used as a test for each element in the set. | +| selector | string | A string containing a selector expression to match elements against. | +| selector | [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A selection to match elements against. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | --------------------- | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | The filter selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + let sel; + const els = doc.find('dl').children(); + + sel = els.filter('#term-2'); + console.log(sel.text()); + + sel = els.filter(function (idx, el) { + return el.text() === 'definition 3-a'; + }); + console.log(sel.text()); + + sel = els.filter(doc.find('dl dt#term-1')); + console.log(sel.text()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-find.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-find.md new file mode 100644 index 000000000..65bb82d65 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-find.md @@ -0,0 +1,38 @@ +--- +title: 'Selection.find(selector)' +description: 'Find the selection descendants, filtered by a selector.' +--- + +# Selection.find(selector) + +Find the selection descendants, filtered by a selector. It returns a [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) object. +Mimics [jquery.find](https://api.jquery.com/find/) + +| Parameter | Type | Description | +| --------- | ------ | -------------------------------------------------------------------- | +| selector | string | A string containing a selector expression to match elements against. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ----------------- | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | Selection object. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import http from 'k6/http'; + +export default function () { + const res = http.get('https://k6.io'); + const doc = parseHTML(res.body); + + const titleDoc = doc.find('head title'); + const title = titleDoc.text(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-first.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-first.md new file mode 100644 index 000000000..b7273e29e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-first.md @@ -0,0 +1,54 @@ +--- +title: 'Selection.first()' +description: 'Reduce the set of matched elements to the first in the set.' +--- + +# Selection.first() + +Reduce the set of matched elements to the first in the set. +Mimics [jquery.first](https://api.jquery.com/first/). + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ----------------------------------- | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | The first element of the Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + + const sel = doc.find('dt'); + + console.log(sel.first().html()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-get.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-get.md new file mode 100644 index 000000000..3fbe50d6f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-get.md @@ -0,0 +1,59 @@ +--- +title: 'Selection.get(index)' +description: 'Retrieve the Element matched by the selector.' +--- + +# Selection.get(index) + +Retrieve the Element matched by the selector. +Mimics [jquery.get](https://api.jquery.com/get/) + +| Parameter | Type | Description | +| --------- | ------ | ---------------------------------------------------------- | +| index | Number | A zero-based integer indicating which element to retrieve. | + +### Returns + +| Type | Description | +| ------- | ------------------------------------ | +| Element | The Element matched by the selector. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + + const sel = doc.find('dl').children(); + + console.log(sel.get(0).innerHTML()); + console.log(sel.get(1).innerHTML()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-has.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-has.md new file mode 100644 index 000000000..5a418ba1b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-has.md @@ -0,0 +1,53 @@ +--- +title: 'Selection.has(selector)' +description: 'Reduce the set of matched elements to those that have a descendant that matches the selector.' +--- + +# Selection.has(selector) + +Reduce the set of matched elements to those that have a descendant that matches the selector. +Mimics [jquery.has](https://api.jquery.com/has/). + +| Parameter | Type | Description | +| --------- | ------ | -------------------------------------------------------------------- | +| selector | string | A string containing a selector expression to match elements against. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------ | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
    +
  • list item 1
  • +
  • list item 2 +
      +
    • list item 2-a
    • +
    • list item 2-b
    • +
    +
  • +
  • list item 3
  • +
  • list item 4
  • +
+ `; + const doc = parseHTML(content); + + const sel = doc.find('li').has('ul'); + + console.log(sel.html()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-html.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-html.md new file mode 100644 index 000000000..4b62bc752 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-html.md @@ -0,0 +1,54 @@ +--- +title: 'Selection.html()' +description: 'Get the HTML contents of the first element in the set of matched elements.' +--- + +# Selection.html() + +Get the HTML contents of the first element in the set of matched elements. +Mimics [jquery.html](https://api.jquery.com/html/) + +### Returns + +| Type | Description | +| ------ | --------------------------------------------------------------------- | +| string | The HTML content of the first element in the set of matched elements. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + + const sel = doc.find('dt'); + + console.log(sel.html()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-is.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-is.md new file mode 100644 index 000000000..a675c6bb3 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-is.md @@ -0,0 +1,71 @@ +--- +title: 'Selection.is(selector)' +description: 'Check the current matched set of elements against a selector or element and return true if at least one of these elements matches the given arguments.' +--- + +# Selection.is(selector) + +Check the current matched set of elements against a selector or element and return true if at least one of these elements matches the given arguments. +Mimics [jquery.is](https://api.jquery.com/is/) + +| Parameter | Type | Description | +| --------- | -------------------------------------------------------------------------------------- | -------------------------------------------------------------------- | +| selector | function | A function used as a test for each element in the set | +| selector | string | A string containing a selector expression to match elements against. | +| selector | [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A selection. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | --------------------- | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | The filter selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + + let result; + + const els = doc.find('dl').children(); + + result = els.is('dd'); + console.log(result); + + result = els.is(function (idx, el) { + return el.text() === 'hola'; + }); + console.log(result); + + result = els.is(els.first()); + console.log(result); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-last.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-last.md new file mode 100644 index 000000000..720c583c0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-last.md @@ -0,0 +1,54 @@ +--- +title: 'Selection.last()' +description: 'Reduce the set of matched elements to the final one in the set.' +--- + +# Selection.last() + +Reduce the set of matched elements to the final one in the set. +Mimics [jquery.last](https://api.jquery.com/last/). + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ----------------------------------- | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | The final element of the Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + + const sel = doc.find('dt'); + + console.log(sel.last().html()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-map.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-map.md new file mode 100644 index 000000000..b29c49273 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-map.md @@ -0,0 +1,63 @@ +--- +title: 'Selection.map(fn)' +description: 'Pass each selection in the current matched set through a function, producing a new Array containing the return values.' +--- + +# Selection.map(fn) + +Pass each selection in the current matched set through a function, producing a new Array containing the return values. +Mimics [jquery.each](https://api.jquery.com/each/) + +| Parameter | Type | Description | +| --------- | -------- | ----------------------------------------------------------- | +| fn | function | A function to iterate all the Selections of the Collection. | + +### Returns + +| Type | Description | +| ----- | --------------------------------------- | +| Array | The array containing the return values. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + + const newEls = doc + .find('dl') + .children() + .map(function (idx, el) { + return 'hola ' + el.text(); + }); + + console.log(newEls); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-next.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-next.md new file mode 100644 index 000000000..7cf046c53 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-next.md @@ -0,0 +1,50 @@ +--- +title: 'Selection.next([selector])' +description: 'Get the immediately following sibling of each element in the set of matched elements +Mimics jquery.next.' +--- + +# Selection.next([selector]) + +Get the immediately following sibling of each element in the set of matched elements +Mimics [jquery.next](https://api.jquery.com/next/). + +| Parameter | Type | Description | +| ------------------- | ------ | -------------------------------------------------------------------- | +| selector (optional) | string | A string containing a selector expression to match elements against. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------ | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
    +
  • list item 1
  • +
  • list item 2
  • +
  • list item 3
  • +
  • list item 4
  • +
  • list item 5
  • +
+ `; + const doc = parseHTML(content); + + const sel = doc.find('li.third-item').next(); + + console.log(sel.html()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-nextall.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-nextall.md new file mode 100644 index 000000000..bc2566cac --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-nextall.md @@ -0,0 +1,49 @@ +--- +title: 'Selection.nextAll([selector])' +description: 'Get all following siblings of each element in the set of matched elements, optionally filtered by a selector.' +--- + +# Selection.nextAll([selector]) + +Get all following siblings of each element in the set of matched elements, optionally filtered by a selector. +Mimics [jquery.nextAll](https://api.jquery.com/nextAll/). + +| Parameter | Type | Description | +| ------------------- | ------ | ------------------------------------------------------------------- | +| selector (optional) | string | A string containing a selector expression to match elements against | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------ | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
    +
  • list item 1
  • +
  • list item 2
  • +
  • list item 3
  • +
  • list item 4
  • +
  • list item 5
  • +
+ `; + const doc = parseHTML(content); + + const sel = doc.find('li.third-item').nextAll(); + + console.log(sel.size()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-nextuntil.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-nextuntil.md new file mode 100644 index 000000000..9e1d15700 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-nextuntil.md @@ -0,0 +1,61 @@ +--- +title: 'Selection.nextUntil([selector], [filter])' +description: 'Get all following siblings of each element in the set of matched elements, optionally filtered by a selector.' +--- + +# Selection.nextUntil([selector], [filter]) + +Get all following siblings of each element up to but not including the element matched by the selector. +Mimics [jquery.nextUntil](https://api.jquery.com/nextUntil/) + +| Parameter | Type | Description | +| ------------------- | ---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | +| selector (optional) | string \| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) \| `null` | A selector expression or object to match elements against. | +| filter (optional) | string \| `null` | A selector expression to filter matched elements. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------ | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + + const sel = doc.find('#term-2').nextUntil('dt'); + console.log(sel.size()); + + const selFilter = doc.find('#term-1').nextUntil('#term-3', 'dd'); + console.log(selFilter.size()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-not.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-not.md new file mode 100644 index 000000000..f93c948be --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-not.md @@ -0,0 +1,64 @@ +--- +title: 'Selection.not(selector)' +description: 'Remove elements from the set of matched elements.' +--- + +# Selection.not(selector) + +Remove elements from the set of matched elements. +Mimics [jquery.not](https://api.jquery.com/not/) + +| Parameter | Type | Description | +| --------- | -------- | -------------------------------------------------------------------- | +| selector | string | A string containing a selector expression to match elements against. | +| selector | function | A function used as a test for each element in the set. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------ | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + let sel = doc.find('dl').children(); + + console.log(sel.not('dt').size()); + + sel = sel.not(function (idx, item) { + return item.text().startsWith('definition'); + }); + + console.log(sel.size()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-parent.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-parent.md new file mode 100644 index 000000000..308a3251a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-parent.md @@ -0,0 +1,49 @@ +--- +title: 'Selection.parent([selector])' +description: 'Get the parent of each element in the current set of matched elements, optionally filtered by a selector.' +--- + +# Selection.parent([selector]) + +Get the parent of each element in the current set of matched elements, optionally filtered by a selector. +Mimics [jquery.parent](https://api.jquery.com/parent/). + +| Parameter | Type | Description | +| ------------------- | ------ | -------------------------------------------------------------------- | +| selector (optional) | string | A string containing a selector expression to match elements against. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------ | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
    +
  • list item 1
  • +
  • list item 2
  • +
  • list item 3
  • +
  • list item 4
  • +
  • list item 5
  • +
+ `; + const doc = parseHTML(content); + + const sel = doc.find('li.third-item').parent(); + + console.log(sel.html()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-parents.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-parents.md new file mode 100644 index 000000000..defcb8640 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-parents.md @@ -0,0 +1,49 @@ +--- +title: 'Selection.parents([selector])' +description: 'Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector.' +--- + +# Selection.parents([selector]) + +Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector. +Mimics [jquery.parents](https://api.jquery.com/parents/). + +| Parameter | Type | Description | +| ------------------- | ------ | -------------------------------------------------------------------- | +| selector (optional) | string | A string containing a selector expression to match elements against. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------ | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
    +
  • list item 1
  • +
  • list item 2
  • +
  • list item 3
  • +
  • list item 4
  • +
  • list item 5
  • +
+ `; + const doc = parseHTML(content); + + const sel = doc.find('li.third-item').parents(); + + console.log(sel.size()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-parentsuntil.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-parentsuntil.md new file mode 100644 index 000000000..b3e3f4f2b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-parentsuntil.md @@ -0,0 +1,61 @@ +--- +title: 'Selection.parentsUntil([selector], [filter])' +description: 'Get the ancestors of each element in the current set of matched elements, up to but not including the element matched by the selector.' +--- + +# Selection.parentsUntil([selector], [filter]) + +Get the ancestors of each element in the current set of matched elements, up to but not including the element matched by the selector. +Mimics [jquery.parentsUntil](https://api.jquery.com/parentsUntil/) + +| Parameter | Type | Description | +| ------------------- | ---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | +| selector (optional) | string \| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) \| `null` | A selector expression or object to match elements against. | +| filter (optional) | string \| `null` | A selector expression to filter matched elements. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------ | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + + const sel = doc.find('#term-2').parentsUntil('dt'); + console.log(sel.size()); + + const selFilter = doc.find('#term-3').parentsUntil('dt', 'dd'); + console.log(selFilter.size()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-prev.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-prev.md new file mode 100644 index 000000000..30753e12d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-prev.md @@ -0,0 +1,48 @@ +--- +title: 'Selection.prev([selector])' +description: 'Get the immediately preceding sibling of each element in the set of matched elements.' +--- + +# Selection.prev([selector]) + +Get the immediately preceding sibling of each element in the set of matched elements. +Mimics [jquery.prev](https://api.jquery.com/prev/). + +| Parameter | Type | Description | +| ------------------- | ------ | -------------------------------------------------------------------- | +| selector (optional) | string | A string containing a selector expression to match elements against. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------ | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
    +
  • list item 1
  • +
  • list item 2
  • +
  • list item 3
  • +
  • list item 4
  • +
  • list item 5
  • +
+ `; + const doc = parseHTML(content); + const sel = doc.find('li.third-item').prev(); + + console.log(sel.html()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-prevall.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-prevall.md new file mode 100644 index 000000000..94d8db99d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-prevall.md @@ -0,0 +1,49 @@ +--- +title: 'Selection.prevAll([selector])' +description: 'Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector.' +--- + +# Selection.prevAll([selector]) + +Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector. +Mimics [jquery.prevAll](https://api.jquery.com/prevAll/). + +| Parameter | Type | Description | +| ------------------- | ------ | -------------------------------------------------------------------- | +| selector (optional) | string | A string containing a selector expression to match elements against. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------ | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
    +
  • list item 1
  • +
  • list item 2
  • +
  • list item 3
  • +
  • list item 4
  • +
  • list item 5
  • +
+ `; + const doc = parseHTML(content); + + const sel = doc.find('li.third-item').prevAll(); + + console.log(sel.size()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-prevuntil.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-prevuntil.md new file mode 100644 index 000000000..aee58a4e5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-prevuntil.md @@ -0,0 +1,61 @@ +--- +title: 'Selection.prevUntil([selector], [filter])' +description: 'Get all preceding siblings of each element up to but not including the element matched by the selector.' +--- + +# Selection.prevUntil([selector], [filter]) + +Get all preceding siblings of each element up to but not including the element matched by the selector. +Mimics [jquery.prevUntil](https://api.jquery.com/prevUntil/). + +| Parameter | Type | Description | +| ------------------- | ---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | +| selector (optional) | string \| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) \| `null` | A selector expression or object to match elements against. | +| filter (optional) | string \| `null` | A selector expression to filter matched elements. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------ | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + + const sel = doc.find('#term-2').prevUntil('dt'); + console.log(sel.size()); + + const selFilter = doc.find('#term-3').prevUntil('#term-1', 'dd'); + console.log(selFilter.size()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-serialize.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-serialize.md new file mode 100644 index 000000000..c3517aa70 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-serialize.md @@ -0,0 +1,40 @@ +--- +title: 'Selection.serialize()' +description: 'Encode a set of form elements as a string in standard URL-encoded notation for submission.' +--- + +# Selection.serialize() + +Encode a set of form elements as a string in standard URL-encoded notation for submission. +Mimics [jquery.serialize](https://api.jquery.com/serialize/) + +### Returns + +| Type | Description | +| ------ | -------------------------------------------------------------------- | +| string | The URL-encoded representation of the matched form or form elements. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+ `; + + const doc = parseHTML(content); + const sel = doc.find('form'); + const serialized = sel.serialize(); + + console.log(serialized); // "username=" + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-serializearray.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-serializearray.md new file mode 100644 index 000000000..43219adb4 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-serializearray.md @@ -0,0 +1,40 @@ +--- +title: 'Selection.serializeArray()' +description: 'Encode a set of form elements as an array of names and values.' +--- + +# Selection.serializeArray() + +Encode a set of form elements as an array of names and values (`[{ name: "name", value: "value" }, ...]`). +Mimics [jquery.serializeArray](https://api.jquery.com/serializeArray/) + +### Returns + +| Type | Description | +| ----- | --------------------------------------------------------------- | +| array | Array of names and values of the matched form or form elements. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+ `; + + const doc = parseHTML(content); + const sel = doc.find('form'); + const serialized = sel.serializeArray(); + + console.log(JSON.stringify(serialized)); // [{"name": "username", "value": ""}] + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-serializeobject.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-serializeobject.md new file mode 100644 index 000000000..653b5381f --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-serializeobject.md @@ -0,0 +1,39 @@ +--- +title: 'Selection.serializeObject()' +description: 'Encode a set of form elements as an object.' +--- + +# Selection.serializeObject() + +Encode a set of form elements as an object (`{ "inputName": "value", "checkboxName": "value" }`). + +### Returns + +| Type | Description | +| ------ | ------------------------------------------------------------------------------------------------------- | +| object | Object representation of the matched form or form elements, key is field name and value is field value. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+ `; + + const doc = parseHTML(content); + const sel = doc.find('form'); + const serialized = sel.serializeObject(); + + console.log(JSON.stringify(serialized)); // {"username": ""} + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-size.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-size.md new file mode 100644 index 000000000..b21bee996 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-size.md @@ -0,0 +1,54 @@ +--- +title: 'Selection.size()' +description: 'Return the number of elements in the Selection.' +--- + +# Selection.size() + +Return the number of elements in the Selection. +Mimics [jquery.size](https://api.jquery.com/size/) + +### Returns + +| Type | Description | +| ------ | ---------------------------------------- | +| Number | The number of elements in the Selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + + const sel = doc.find('dt'); + + console.log(sel.size()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-slice.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-slice.md new file mode 100644 index 000000000..a10f8c3ab --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-slice.md @@ -0,0 +1,62 @@ +--- +title: 'Selection.slice(start [, end])' +description: 'Reduce the set of matched elements to a subset specified by a range of indices.' +--- + +# Selection.slice(start [, end]) + +Reduce the set of matched elements to a subset specified by a range of indices. +Mimics [jquery.slice](https://api.jquery.com/slice/) + +| Parameter | Type | Description | +| --------- | -------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- | +| start | Number | An integer indicating the 0-based position at which the elements begin to be selected. | +| end | Number | An integer indicating the 0-based position at which the elements stop being selected. | +| selector | [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A selection. | + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------------ | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | The new selection. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + + const els = doc.find('dl').children(); + + console.log(els.slice(4).text()); + + console.log(els.slice(2, 4).text()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-text.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-text.md new file mode 100644 index 000000000..9f4022de3 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-text.md @@ -0,0 +1,32 @@ +--- +title: 'Selection.text()' +description: 'Get the text content of the Selection.' +--- + +# Selection.text() + +Get the text content of the Selection. +Mimics [jquery.text](https://api.jquery.com/text/). + +### Returns + +| Type | Description | +| ------ | ----------------------- | +| string | Selection text content. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import http from 'k6/http'; + +export default function () { + const res = http.get('https://k6.io'); + const doc = parseHTML(res.body); + const pageTitle = doc.find('head title').text(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-toarray.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-toarray.md new file mode 100644 index 000000000..b3875ab20 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-toarray.md @@ -0,0 +1,58 @@ +--- +title: 'Selection.toArray()' +description: 'Retrieve all the elements contained in the Selection, as an array.' +--- + +# Selection.toArray() + +Retrieve all the elements contained in the Selection, as an array. +Mimics [jquery.toArray](https://api.jquery.com/toArray/). + +### Returns + +| Type | Description | +| ----------------------------------------------------------------------------------------------- | --------------------------- | +| Array of [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | Array of Selection objects. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` +
+
term 1
+
definition 1-a
+
definition 1-b
+
definition 1-c
+
definition 1-d
+ +
term 2
+
definition 2-a
+
definition 2-b
+
definition 2-c
+ +
term 3
+
definition 3-a
+
definition 3-b
+
+ `; + const doc = parseHTML(content); + + doc + .find('dl') + .children() + .toArray() + .forEach(function (item) { + console.log(item.text()); + }); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-val.md b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-val.md new file mode 100644 index 000000000..1b4147248 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-html/selection/selection-val.md @@ -0,0 +1,56 @@ +--- +title: 'Selection.val()' +description: 'Get the current value of the first element in the set of matched elements.' +--- + +# Selection.val() + +Get the current value of the first element in the set of matched elements. +Mimics [jquery.val](https://api.jquery.com/val/). + +### Returns + +| Type | Description | +| ------ | -------------------------------------------------------------- | +| string | The value of the first element in the set of matched elements. | + +### Example + +{{< code >}} + +```javascript +import { parseHTML } from 'k6/html'; +import { sleep } from 'k6'; + +export default function () { + const content = ` + + + + + + `; + const doc = parseHTML(content); + + console.log(doc.find('#text_input').val()); + console.log(doc.find('#select_one option[selected]').val()); + console.log(doc.find('#select_one').val()); + console.log(doc.find('#select_text').val()); + console.log(doc.find('#select_multi').val()); + console.log(doc.find('#textarea').val()); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/_index.md new file mode 100644 index 000000000..73c9e0e32 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/_index.md @@ -0,0 +1,9 @@ +--- +title: 'k6/http' +description: 'The k6/http module contains functionality for performing HTTP transactions.' +weight: 09 +--- + +# k6/http + +{{< docs/shared source="k6" lookup="javascript-api/k6-http.md" version="" >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/asyncrequest.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/asyncrequest.md new file mode 100644 index 000000000..8b8ddf6d2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/asyncrequest.md @@ -0,0 +1,91 @@ +--- +title: 'asyncRequest( method, url, [body], [params] )' +description: 'Issue any type of HTTP request asynchronously.' +description: 'Issue any type of HTTP request asynchronously.' +weight: 10 +--- + +# asyncRequest( method, url, [body], [params] ) + +| Parameter | Type | Description | +| ----------------- | ----------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | +| method | string | Request method (e.g. `'POST'`). Must be uppercase. | +| url | string /[HTTP URL](https://grafana.com/docs/k6//javascript-api/k6-http/url#returns) | Request URL (e.g. `'http://example.com'`). | +| body (optional) | string / object / ArrayBuffer | Request body. Objects will be `x-www-form-urlencoded` encoded. | +| params (optional) | object | [Params](https://grafana.com/docs/k6//javascript-api/k6-http/params) object containing additional request parameters. | + +### Returns + +| Type | Description | +| --------------------- | ------------------------------------------------------------------------------------------------- | +| Promise with Response | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Examples + +Using http.asyncRequest() to issue a POST request: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +const url = 'https://httpbin.test.k6.io/post'; + +export default async function () { + const data = { name: 'Bert' }; + + // Using a JSON string as body + let res = await http.asyncRequest('POST', url, JSON.stringify(data), { + headers: { 'Content-Type': 'application/json' }, + }); + console.log(res.json().json.name); // Bert + + // Using an object as body, the headers will automatically include + // 'Content-Type: application/x-www-form-urlencoded'. + res = await http.asyncRequest('POST', url, data); + console.log(res.json().form.name); // Bert +} +``` + +{{< /code >}} + +Using `http.asyncRequest()` to issue multiple requests, then [Promise.race](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race) to determine which requests finish first: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default async () => { + const urlOne = `https://httpbin.test.k6.io/delay/${randomInt(1, 5)}`; + const urlTwo = `https://httpbin.test.k6.io/delay/${randomInt(1, 5)}`; + const urlThree = `https://httpbin.test.k6.io/delay/${randomInt(1, 5)}`; + + const one = http.asyncRequest('GET', urlOne); + const two = http.asyncRequest('GET', urlTwo); + const three = http.asyncRequest('GET', urlThree); + + console.log('Racing:'); + console.log(urlOne); + console.log(urlTwo); + console.log(urlThree); + + const res = await Promise.race([one, two, three]); + console.log('winner is', res.url, 'with duration of', res.timings.duration + 'ms'); +}; + +function randomInt(min, max) { + return Math.floor(Math.random() * (max - min) + min); +} +``` + +{{< admonition type="note" >}} + +`http.asyncRequest` has no current way to abort a request. + +In the preceding script, after `res` gets the value from the fastest request, the other requests will continue to execute. +This might block the end of the iteration, because the iteration only stops once all async jobs finish. + +{{< /admonition >}} + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/batch.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/batch.md new file mode 100644 index 000000000..a0d9ec5f2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/batch.md @@ -0,0 +1,182 @@ +--- +title: 'batch( requests )' +description: 'Issue multiple HTTP requests in parallel (like e.g. browsers tend to do).' +description: 'Issue multiple HTTP requests in parallel (like e.g. browsers tend to do).' +weight: 10 +--- + +# batch( requests ) + +Batch multiple HTTP requests together to issue them in parallel over multiple TCP connections. +To set batch size, use the [batch per host](https://grafana.com/docs/k6//using-k6/k6-options/reference#batch-per-host) option. + +| Parameter | Type | Description | +| --------- | --------------- | ---------------------------------------------------------------- | +| requests | array \| object | An array or object containing requests, in string or object form | + +### Request definition + +You have multiple ways to structure batch requests: + +- In an array of arrays +- As an object or array of objects +- As an array of URL strings + +Defining batch requests as URL strings is a shortcut for GET requests. +You can use this GET shortcut in objects—name a key and give it a URL value +(refer to subsequent sections for example syntax). + +#### Array and Object + +You can define a request specified as an array or object with the following parameters. + +{{< admonition type="caution" >}} + +When you define requests as an array, you must use a specific order of items. +Note the `Position` column for the correct order. + +{{< /admonition >}} + +| Array position | Name | Type | Description | +| -------------- | ----------------- | ----------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- | +| 1 | method | string | Mandatory. The HTTP method of the request. One of GET, POST, PUT, PATCH, DELETE, HEAD or OPTION. | +| 2 | url | string /[HTTP URL](https://grafana.com/docs/k6//javascript-api/k6-http/url#returns) | Mandatory. The URL to request. | +| 3 | body (optional) | string / object / ArrayBuffer | The body of the request if relevant. Can be set to `null` if not applicable but you want to set the last `params` argument. | +| 4 | params (optional) | object | [Params](https://grafana.com/docs/k6//javascript-api/k6-http/params) like auth, custom headers and tags. | + +#### String + +If you pass an array of string values, k6 automatically parses them into a batch of `GET` requests, where the target is the value of the strings. + +### Returns + +| Type | Description | +| ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| object | The returned object contains [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) objects.

It is an array when users pass an array as `requests`, and is an ordinary object with string keys when named requests are used (see below). | + +### Example with arrays + +This example batches three URLs in arrays for parallel fetching: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const responses = http.batch([ + ['GET', 'https://test.k6.io', null, { tags: { ctype: 'html' } }], + ['GET', 'https://test.k6.io/style.css', null, { tags: { ctype: 'css' } }], + ['GET', 'https://test.k6.io/images/logo.png', null, { tags: { ctype: 'images' } }], + ]); + check(responses[0], { + 'main page status was 200': (res) => res.status === 200, + }); +} +``` + +{{< /code >}} + +### Example with request objects + +This example uses objects to define a batch of POST requests (along with custom HTTP headers in a [Params](https://grafana.com/docs/k6//javascript-api/k6-http/params) object to the request): + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const req1 = { + method: 'GET', + url: 'https://httpbin.test.k6.io/get', + }; + const req2 = { + method: 'GET', + url: 'https://test.k6.io', + }; + const req3 = { + method: 'POST', + url: 'https://httpbin.test.k6.io/post', + body: { + hello: 'world!', + }, + params: { + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + }, + }; + const responses = http.batch([req1, req2, req3]); + // httpbin.test.k6.io should return our POST data in the response body, so + // we check the third response object to see that the POST worked. + check(responses[2], { + 'form data OK': (res) => JSON.parse(res.body)['form']['hello'] == 'world!', + }); +} +``` + +{{< /code >}} + +{{< admonition type="note" >}} + +In the preceding example, `req1` can happen before `req2` or `req3`. + +{{< /admonition >}} + +### Example with array of strings + +This example uses an array of URL strings to send a batch of GET requests. + +{{< code >}} + +```javascript +import { check } from 'k6'; +import http from 'k6/http'; + +export default function () { + const responses = http.batch(['http://test.k6.io', 'http://test.k6.io/pi.php']); + + check(responses[0], { + 'main page 200': (res) => res.status === 200, + }); + + check(responses[1], { + 'pi page 200': (res) => res.status === 200, + 'pi page has right content': (res) => res.body === '3.14', + }); +} +``` + +{{< /code >}} + +### Example object with named properties + +Finally, you can also send in named requests by using an object instead of an array as the parameter to `http.batch()`. +This example mixes string URLs and request objects. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const requests = { + 'front page': 'https://k6.io', + 'features page': { + method: 'GET', + url: 'https://k6.io/features', + params: { headers: { 'User-Agent': 'k6' } }, + }, + }; + const responses = http.batch(requests); + // when accessing results, we use the name of the request as index + // in order to find the corresponding Response object + check(responses['front page'], { + 'front page status was 200': (res) => res.status === 200, + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar.md new file mode 100644 index 000000000..08ca682d8 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar.md @@ -0,0 +1,28 @@ +--- +title: 'cookieJar()' +slug: 'cookiejar-method' +description: 'Get active HTTP Cookie jar.' +weight: 10 +--- + +# cookieJar() + +Get the active cookie jar. + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------------- | +| [CookieJar](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar) | A CookieJar object. | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + const jar = http.cookieJar(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/_index.md new file mode 100644 index 000000000..cb001cf38 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/_index.md @@ -0,0 +1,43 @@ +--- +title: 'CookieJar' +description: 'Used for storing cookies, set by the server and/or added by the client.' +weight: 60 +--- + +# CookieJar + +_CookieJar_ is an object for storing cookies that are set by the server, added by the client, or both. As described in the how-to guide on using [Cookies](https://grafana.com/docs/k6//using-k6/cookies), k6 handles cookies automatically by default. If you need more control over cookies you can however create your own cookie jar and select it as the active jar (instead of the default one created by k6) for one or more requests. + +| Method | Description | +| --------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------- | +| [cookiesForURL(url)](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar/cookiejar-cookiesforurl) | Get Object of cookies where the key is the cookie name and the value is an array. | +| [set(url, name, value, [options])](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar/cookiejar-set) | Set a cookie in the jar by specifying name, value and some other optional settings like domain, path etc. | +| [clear(url)](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar/cookiejar-clear) | Delete all cookies for the given URL. | +| [delete(url, name)](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar/cookiejar-delete) | Deletes the `name` cookie for the given URL. | + +### Example + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const res1 = http.get('https://httpbin.test.k6.io/cookies/set?my_cookie=hello%20world', { + redirects: 0, + }); + const jar = http.cookieJar(); + const cookies = jar.cookiesForURL('https://httpbin.test.k6.io/'); + check(res1, { + "has cookie 'my_cookie'": (r) => cookies.my_cookie.length > 0, + 'cookie has correct value': (r) => cookies.my_cookie[0] === 'hello world', + }); + + jar.clear('https://httpbin.test.k6.io/cookies'); + + const res2 = http.get('https://httpbin.test.k6.io/cookies'); + check(res2, { + 'has status 200': (r) => r.status === 200, + "hasn't cookie 'my_cookie'": (r) => r.json().cookies.my_cookie == null, + }); +} +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-clear.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-clear.md new file mode 100644 index 000000000..b0dcc88b0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-clear.md @@ -0,0 +1,47 @@ +--- +title: 'CookieJar.clear(url)' +description: 'Delete all cookies for the given URL.' +--- + +# CookieJar.clear(url) + +| Parameter | Type | Description | +| --------- | ------ | ---------------------------------- | +| url | string | The URL to delete all cookies for. | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + http.get('https://httpbin.test.k6.io/cookies/set?one=1&two=2'); + + // We'll use httpbin's reflection to see what cookies we + // are actually sending to the server after every change + let httpbinResp; + httpbinResp = http.get('https://httpbin.test.k6.io/cookies'); + console.log(JSON.stringify(httpbinResp.json().cookies)); + // Will print '{"one":"1","two":"2"}' + + const jar = http.cookieJar(); // get the VU specific jar + jar.set('https://httpbin.test.k6.io/cookies', 'three', '3'); + httpbinResp = http.get('https://httpbin.test.k6.io/cookies'); + console.log(JSON.stringify(httpbinResp.json().cookies)); + // Will print '{"one":"1","three":"3","two":"2"}' + + jar.delete('https://httpbin.test.k6.io/cookies', 'one'); + httpbinResp = http.get('https://httpbin.test.k6.io/cookies'); + console.log(JSON.stringify(httpbinResp.json().cookies)); + // Will print '{"three":"3","two":"2"}' + + jar.clear('https://httpbin.test.k6.io/cookies'); + httpbinResp = http.get('https://httpbin.test.k6.io/cookies'); + console.log(JSON.stringify(httpbinResp.json().cookies)); + // Will print '{}' +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-cookiesforurl.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-cookiesforurl.md new file mode 100644 index 000000000..533a0ad03 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-cookiesforurl.md @@ -0,0 +1,39 @@ +--- +title: 'CookieJar.cookiesForURL(url)' +description: 'Get object with all cookies for the given URL, where the key is the cookie name and the value is an array.' +--- + +# CookieJar.cookiesForURL(url) + +| Parameter | Type | Description | +| --------- | ------ | --------------------------- | +| url | string | The URL to get cookies for. | + +### Returns + +| Type | Description | +| ------ | ----------------------------------------------------------------------------------------------------------- | +| object | A JS object with all cookies for the given URL, where the key is the cookie name and the value is an array. | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const res = http.get('https://httpbin.test.k6.io/cookies/set?my_cookie=hello%20world', { + redirects: 0, + }); + const jar = http.cookieJar(); + const cookies = jar.cookiesForURL('https://httpbin.test.k6.io/'); + check(res, { + "has cookie 'my_cookie'": (r) => cookies.my_cookie.length > 0, + 'cookie has correct value': (r) => cookies.my_cookie[0] === 'hello world', + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-delete.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-delete.md new file mode 100644 index 000000000..558dc1c24 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-delete.md @@ -0,0 +1,47 @@ +--- +title: 'CookieJar.delete(url, name)' +description: 'Delete a cookie of a specified `name` for the given URL.' +--- + +# CookieJar.delete(url, name) + +| Parameter | Type | Description | +| --------- | ------ | ------------------------------------------ | +| url | string | The URL to delete cookies for. | +| name | string | The name of the cookie you want to delete. | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const jar = http.cookieJar(); + jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie_1', 'hello world_1'); + jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie_2', 'hello world_2'); + + const res1 = http.get('https://httpbin.test.k6.io/cookies'); + check(res1, { + 'res1 has status 200': (r) => r.status === 200, + "res1 has cookie 'my_cookie_1'": (r) => r.json().cookies.my_cookie_1 !== null, + 'res1 cookie has correct value_1': (r) => r.json().cookies.my_cookie_1 == 'hello world_1', + "res1 has cookie 'my_cookie_2'": (r) => r.json().cookies.my_cookie_2 !== null, + 'res1 cookie has correct value_2': (r) => r.json().cookies.my_cookie_2 == 'hello world_2', + }); + + jar.delete('https://httpbin.test.k6.io/cookies', 'my_cookie_1'); + + const res2 = http.get('https://httpbin.test.k6.io/cookies'); + check(res2, { + 'res2 has status 200': (r) => r.status === 200, + "res2 doesn't have cookie 'my_cookie_1'": (r) => r.json().cookies.my_cookie_1 == null, + "res2 has cookie 'my_cookie_2'": (r) => r.json().cookies.my_cookie_2 !== null, + 'res2 cookie has correct value_2': (r) => r.json().cookies.my_cookie_2 == 'hello world_2', + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-set.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-set.md new file mode 100644 index 000000000..170fb114a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/cookiejar/cookiejar-set.md @@ -0,0 +1,42 @@ +--- +title: 'CookieJar.set(url, name, value, [options])' +description: 'Set a cookie in the jar by specifying url, name, value and some other optional settings like domain, path, etc.' +--- + +# CookieJar.set(url, name, value, [options]) + +Set a cookie in the jar by specifying url, name, value and some other optional settings like domain, path, etc. + +| Parameter | Type | Description | +| ------------------ | ------ | ------------------------------------------------------------------------------------------- | +| url | string | Cookie URL | +| name | string | Cookie name | +| value | string | Cookie value | +| options (optional) | object | Specific cookie settings: `domain`, `path`, `expires`, `max_age`, `secure` and `http_only`. | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const jar = http.cookieJar(); + jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie', 'hello world', { + domain: 'httpbin.test.k6.io', + path: '/cookies', + secure: true, + max_age: 600, + }); + const res = http.get('https://httpbin.test.k6.io/cookies'); + check(res, { + 'has status 200': (r) => r.status === 200, + "has cookie 'my_cookie'": (r) => r.json().cookies.my_cookie !== null, + 'cookie has correct value': (r) => r.json().cookies.my_cookie == 'hello world', + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/del.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/del.md new file mode 100644 index 000000000..8a2327025 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/del.md @@ -0,0 +1,39 @@ +--- +title: 'del( url, [body], [params] )' +description: 'Issue an HTTP DELETE request.' +description: 'Issue an HTTP DELETE request.' +weight: 10 +--- + +# del( url, [body], [params] ) + +Make a DELETE request. + +| Parameter | Type | Description | +| ---------------------------- | ----------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| url | string /[HTTP URL](https://grafana.com/docs/k6//javascript-api/k6-http/url#returns) | Request URL (e.g. `http://example.com`). | +| body (optional, discouraged) | string / object / ArrayBuffer | Request body; objects will be `x-www-form-urlencoded`. This is discouraged, because sending a DELETE request with a body has [no defined semantics](https://tools.ietf.org/html/rfc7231#section-4.3.5) and may cause some servers to reject it. | +| params (optional) | object | [Params](https://grafana.com/docs/k6//javascript-api/k6-http/params) object containing additional request parameters. | + +### Returns + +| Type | Description | +| -------- | ------------------------------------------------------------------------------------------------- | +| Response | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +const url = 'https://httpbin.test.k6.io/delete'; + +export default function () { + const params = { headers: { 'X-MyHeader': 'k6test' } }; + http.del(url, null, params); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/expected-statuses.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/expected-statuses.md new file mode 100644 index 000000000..4912667f2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/expected-statuses.md @@ -0,0 +1,37 @@ +--- +title: 'expectedStatuses( statuses )' +description: 'generates a responseCallback to check status codes' +description: 'generates a responseCallback to check status codes' +weight: 11 +--- + +# expectedStatuses( statuses ) + +Returns a callback to be used with [setResponseCallback](https://grafana.com/docs/k6//javascript-api/k6-http/set-response-callback) to mark responses as expected based only on their status codes. + +| Parameter | Type | Description | +| --------- | --------------- | -------------------------------------------------------------------------------------------------------------- | +| statuses | integer/objects | either an integer or an object like {min:100, max:300} which gives a minimum and maximum expected status codes | + +You can have as many arguments as wanted in any order. + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +// setting some pretty strange status codes as expected +http.setResponseCallback( + http.expectedStatuses(406, 500, { min: 200, max: 204 }, 302, { min: 305, max: 405 }) +); + +export default () => { + // this one will actually be marked as failed as it doesn't match any of the above listed status + // codes + http.get('https://httpbin.test.k6.io/status/205'); +}; +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/file.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/file.md new file mode 100644 index 000000000..564d131bc --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/file.md @@ -0,0 +1,43 @@ +--- +title: 'file( data, [filename], [contentType] )' +description: 'Create a file object that is used for building multi-part requests.' +description: 'Create a file object that is used for building multi-part requests.' +weight: 10 +--- + +# file( data, [filename], [contentType] ) + +Create a file object that is used for building [Multipart requests (file uploads)](https://grafana.com/docs/k6//examples/data-uploads#multipart-request-uploading-a-file). + +| Parameter | Type | Description | +| ----------- | ---------------------------- | -------------------------------------------------------------------------------- | +| data | string / Array / ArrayBuffer | File data as string, array of numbers, or an `ArrayBuffer` object. | +| filename | string | The filename to specify for this field (or "part") of the multipart request. | +| contentType | string | The content type to specify for this field (or "part") of the multipart request. | + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------ | ------------------ | +| [FileData](https://grafana.com/docs/k6//javascript-api/k6-http/filedata) | A FileData object. | + +### Example + +{{< code >}} + +```javascript +import { sleep } from 'k6'; +import { md5 } from 'k6/crypto'; +import http from 'k6/http'; + +const binFile = open('/path/to/file.bin', 'b'); + +export default function () { + const f = http.file(binFile, 'test.bin'); + console.log(md5(f.data, 'hex')); + console.log(f.filename); + console.log(f.content_type); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/filedata.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/filedata.md new file mode 100644 index 000000000..87522f4f6 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/filedata.md @@ -0,0 +1,39 @@ +--- +title: 'FileData' +description: 'Used for wrapping data representing a file when doing multipart requests (file uploads).' +description: 'Used for wrapping data representing a file when doing multipart requests (file uploads).' +weight: 60 +--- + +# FileData + +_FileData_ is an object for wrapping data representing a file when doing +[multipart requests (file uploads)](https://grafana.com/docs/k6//examples/data-uploads#multipart-request-uploading-a-file). +You create it by calling [http.file( data, [filename], [contentType] )](https://grafana.com/docs/k6//javascript-api/k6-http/file). + +| Name | Type | Description | +| --------------------- | ---------------------------- | ------------------------------------------------------------------ | +| FileData.data | string / Array / ArrayBuffer | File data as string, array of numbers, or an `ArrayBuffer` object. | +| FileData.content_type | string | The content type that will be specified in the multipart request. | +| FileData.filename | string | The filename that will be specified in the multipart request. | + +### Example + +{{< code >}} + +```javascript +import { sleep } from 'k6'; +import { md5 } from 'k6/crypto'; +import http from 'k6/http'; + +const binFile = open('/path/to/file.bin', 'b'); + +export default function () { + const f = http.file(binFile, 'test.bin'); + console.log(md5(f.data, 'hex')); + console.log(f.filename); + console.log(f.content_type); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/get.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/get.md new file mode 100644 index 000000000..70a33a370 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/get.md @@ -0,0 +1,36 @@ +--- +title: 'get( url, [params] )' +description: 'Issue an HTTP GET request.' +description: 'Issue an HTTP GET request.' +weight: 10 +--- + +# get( url, [params] ) + +Make a GET request. + +| Parameter | Type | Description | +| ----------------- | ----------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | +| url | string /[HTTP URL](https://grafana.com/docs/k6//javascript-api/k6-http/url#returns) | Request URL (e.g. `http://example.com`). | +| params (optional) | object | [Params](https://grafana.com/docs/k6//javascript-api/k6-http/params) object containing additional request parameters. | + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------ | --------------------- | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) | HTTP Response object. | + +### Example fetching a URL + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + const res = http.get('https://test.k6.io'); + console.log(JSON.stringify(res.headers)); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/head.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/head.md new file mode 100644 index 000000000..2c8d0fd54 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/head.md @@ -0,0 +1,36 @@ +--- +title: 'head( url, [params] )' +description: 'Issue an HTTP HEAD request.' +description: 'Issue an HTTP HEAD request.' +weight: 10 +--- + +# head( url, [params] ) + +Make a HEAD request. + +| Parameter | Type | Description | +| ----------------- | ----------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | +| url | string /[HTTP URL](https://grafana.com/docs/k6//javascript-api/k6-http/url#returns) | Request URL (e.g. `http://example.com`). | +| params (optional) | object | [Params](https://grafana.com/docs/k6//javascript-api/k6-http/params) object containing additional request parameters. | + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------ | --------------------- | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) | HTTP Response object. | + +### Example fetching a URL + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + const res = http.head('https://test.k6.io'); + console.log(JSON.stringify(res.headers)); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/options.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/options.md new file mode 100644 index 000000000..bfe15af36 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/options.md @@ -0,0 +1,38 @@ +--- +title: 'options( url, [body], [params] )' +description: 'Issue an HTTP OPTIONS request.' +description: 'Issue an HTTP OPTIONS request.' +weight: 10 +--- + +# options( url, [body], [params] ) + +| Parameter | Type | Description | +| ----------------- | ----------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | +| url | string /[HTTP URL](https://grafana.com/docs/k6//javascript-api/k6-http/url#returns) | Request URL (e.g. `http://example.com`). | +| body (optional) | string / object / ArrayBuffer | Request body; objects will be `x-www-form-urlencoded`. | +| params (optional) | object | [Params](https://grafana.com/docs/k6//javascript-api/k6-http/params) object containing additional request parameters. | + +### Returns + +| Type | Description | +| -------- | ------------------------------------------------------------------------------------------------- | +| Response | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +const url = 'https://httpbin.test.k6.io/'; + +export default function () { + const params = { headers: { 'X-MyHeader': 'k6test' } }; + const res = http.options(url, null, params); + console.log(res.headers['Allow']); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/params.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/params.md new file mode 100644 index 000000000..122dabf05 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/params.md @@ -0,0 +1,124 @@ +--- +title: 'Params' +description: 'Used for setting various HTTP request-specific parameters such as headers, cookies, etc.' +description: 'Used for setting various HTTP request-specific parameters such as headers, cookies, etc.' +weight: 60 +--- + +# Params + +_Params_ is an object used by the http.\* methods that generate HTTP requests. _Params_ contains request-specific options like e.g. HTTP headers that should be inserted into the request. + +| Name | Type | Description | +| ------------------------- | ----------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Params.auth` | string | The authentication method used for the request. It currently supports `digest`, `ntlm`, and `basic` authentication methods. | +| `Params.cookies` | object | Object with key-value pairs representing request scoped cookies (they won't be added to VU cookie jar)
`{cookies: { key: "val", key2: "val2" }}`

You also have the option to say that a request scoped cookie should override a cookie in the VU cookie jar:
`{cookies: { key: { value: "val", replace: true }}}` | +| `Params.headers` | object | Object with key-value pairs representing custom HTTP headers the user would like to add to the request. | +| `Params.jar` | object | http.CookieJar object to override default VU cookie jar with. Cookies added to request will be sourced from this jar and cookies set by server will be added to this jar. | +| `Params.redirects` | number | The number of redirects to follow for this request. Overrides the global test option [`maxRedirects`](https://grafana.com/docs/k6//using-k6/k6-options/reference). | +| `Params.tags` | object | Key-value pairs where the keys are names of tags and the values are tag values. Response time metrics generated as a result of the request will have these tags added to them, allowing the user to filter out those results specifically, when looking at results data. | +| `Params.timeout` | string / number | Maximum time to wait for the request to complete. Default timeout is 60 seconds (`"60s"`).
The type can also be a number, in which case k6 interprets it as milliseconds, e.g., `60000` is equivalent to `"60s"`. | +| `Params.compression` | string | Sets whether the request body should be compressed. If set to `gzip` it will use gzip to compress the body and set the appropriate `Content-Length` and `Content-Encoding` headers.

Possible values: `gzip`, `deflate`, `br`, `zstd`, and any comma-separated combination of them (for stacked compression). The default is no compression. | +| `Params.responseType` | string | ResponseType is used to specify how to treat the body of the response. The three options are:
- text: k6 will return it as a string. This might not be what you want in cases of binary data as the conversation to UTF-16 will probably break it. This is also the default if
[discardResponseBodies](https://grafana.com/docs/k6//using-k6/k6-options/reference) is set to false or not set at all.
- `binary`: k6 will return an ArrayBuffer object
- `none`: k6 will return null as the body. The whole body will be ignored. This is the default when [discardResponseBodies](https://grafana.com/docs/k6//using-k6/k6-options/reference) is set to true. | +| `Params.responseCallback` | [expectedStatuses](https://grafana.com/docs/k6//javascript-api/k6-http/expected-statuses) | sets a [responseCallback](https://grafana.com/docs/k6//javascript-api/k6-http/set-response-callback) only for this request. For performance reasons it's better to initialize it once and reference it for each time the same callback will need to be used. | + +### Example of custom HTTP headers and tags + +_A k6 script that will make an HTTP request with a custom HTTP header and tag results data with a specific tag_ + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + const params = { + cookies: { my_cookie: 'value' }, + headers: { 'X-MyHeader': 'k6test' }, + redirects: 5, + tags: { k6test: 'yes' }, + }; + const res = http.get('https://k6.io', params); +} +``` + +{{< /code >}} + +### Example using http.batch() with Params + +Here is another example using [http.batch()](https://grafana.com/docs/k6//javascript-api/k6-http/batch) with a `Params` argument: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +const url1 = 'https://api.k6.io/v3/account/me'; +const url2 = 'https://httpbin.test.k6.io/get'; +const apiToken = 'f232831bda15dd233c53b9c548732c0197619a3d3c451134d9abded7eb5bb195'; +const requestHeaders = { + 'User-Agent': 'k6', + 'Authorization': 'Token ' + apiToken, +}; + +export default function () { + const res = http.batch([ + { method: 'GET', url: url1, params: { headers: requestHeaders } }, + { method: 'GET', url: url2 }, + ]); +} +``` + +{{< /code >}} + +### Example of Digest Authentication + +Here is one example of how to use the `Params` to Digest Authentication. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + // Passing username and password as part of URL plus the auth option will authenticate using HTTP Digest authentication + const res = http.get('http://user:passwd@httpbin.test.k6.io/digest-auth/auth/user/passwd', { + auth: 'digest', + }); + + // Verify response + check(res, { + 'status is 200': (r) => r.status === 200, + 'is authenticated': (r) => r.json().authenticated === true, + 'is correct user': (r) => r.json().user === 'user', + }); +} +``` + +{{< /code >}} + +### Example of overriding discardResponseBodies + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { discardResponseBodies: true }; +export default function () {} +export function setup() { + // Get 10 random bytes as an ArrayBuffer. Without the responseType the body + // will be null. + const response = http.get('https://httpbin.test.k6.io/bytes/10', { + responseType: 'binary', + }); + // response.body is an ArrayBuffer, so wrap it in a typed array view to access + // its elements. + const bodyView = new Uint8Array(response.body); + // This will output something like `176,61,15,66,233,98,223,196,43,1` + console.log(bodyView); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/patch.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/patch.md new file mode 100644 index 000000000..81de685c9 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/patch.md @@ -0,0 +1,41 @@ +--- +title: 'patch( url, [body], [params] )' +description: 'Issue an HTTP PATCH request.' +description: 'Issue an HTTP PATCH request.' +weight: 10 +--- + +# patch( url, [body], [params] ) + +| Parameter | Type | Description | +| ----------------- | ----------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | +| url | string /[HTTP URL](https://grafana.com/docs/k6//javascript-api/k6-http/url#returns) | Request URL (e.g. `http://example.com`). | +| body (optional) | string / object / ArrayBuffer | Request body; objects will be `x-www-form-urlencoded`. | +| params (optional) | object | [Params](https://grafana.com/docs/k6//javascript-api/k6-http/params) object containing additional request parameters | + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------ | --------------------- | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) | HTTP Response object. | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +const url = 'https://httpbin.test.k6.io/patch'; + +export default function () { + const headers = { 'Content-Type': 'application/json' }; + const data = { name: 'Bert' }; + + const res = http.patch(url, JSON.stringify(data), { headers: headers }); + + console.log(JSON.parse(res.body).json.name); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/post.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/post.md new file mode 100644 index 000000000..2f1dfb0df --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/post.md @@ -0,0 +1,57 @@ +--- +title: 'post( url, [body], [params] )' +description: 'Issue an HTTP POST request.' +description: 'Issue an HTTP POST request.' +weight: 10 +--- + +# post( url, [body], [params] ) + +| Parameter | Type | Description | +| ------------------- | ----------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | +| `url` | string /[HTTP URL](https://grafana.com/docs/k6//javascript-api/k6-http/url#returns) | Request URL (e.g. `http://example.com`). | +| `body` | string / object / ArrayBuffer | Request body; objects will be `x-www-form-urlencoded`. | +| `params` (optional) | object | [Params](https://grafana.com/docs/k6//javascript-api/k6-http/params) object containing additional request parameters | + +### Returns + +| Type | Description | +| ---------- | ------------------------------------------------------------------------------------------------- | +| `Response` | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +const url = 'https://httpbin.test.k6.io/post'; +const logoBin = open('./logo.png', 'b'); + +export default function () { + let data = { name: 'Bert' }; + + // Using a JSON string as body + let res = http.post(url, JSON.stringify(data), { + headers: { 'Content-Type': 'application/json' }, + }); + console.log(res.json().json.name); // Bert + + // Using an object as body, the headers will automatically include + // 'Content-Type: application/x-www-form-urlencoded'. + res = http.post(url, data); + console.log(res.json().form.name); // Bert + + // Using a binary array as body. Make sure to open() the file as binary + // (with the 'b' argument). + http.post(url, logoBin, { headers: { 'Content-Type': 'image/png' } }); + + // Using an ArrayBuffer as body. Make sure to pass the underlying ArrayBuffer + // instance to http.post(), and not the TypedArray view. + data = new Uint8Array([104, 101, 108, 108, 111]); + http.post(url, data.buffer, { headers: { 'Content-Type': 'image/png' } }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/put.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/put.md new file mode 100644 index 000000000..5b5f2a765 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/put.md @@ -0,0 +1,41 @@ +--- +title: 'put( url, [body], [params] )' +description: 'Issue an HTTP PUT request.' +description: 'Issue an HTTP PUT request.' +weight: 10 +--- + +# put( url, [body], [params] ) + +| Parameter | Type | Description | +| ----------------- | ----------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | +| url | string /[HTTP URL](https://grafana.com/docs/k6//javascript-api/k6-http/url#returns) | Request URL (e.g. `http://example.com`). | +| body (optional) | string / object / ArrayBuffer | Request body; objects will be `x-www-form-urlencoded`. | +| params (optional) | object | [Params](https://grafana.com/docs/k6//javascript-api/k6-http/params) object containing additional request parameters. | + +### Returns + +| Type | Description | +| -------- | ------------------------------------------------------------------------------------------------- | +| Response | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +const url = 'https://httpbin.test.k6.io/put'; + +export default function () { + const headers = { 'Content-Type': 'application/json' }; + const data = { name: 'Bert' }; + + const res = http.put(url, JSON.stringify(data), { headers: headers }); + + console.log(JSON.parse(res.body).json.name); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/request.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/request.md new file mode 100644 index 000000000..4766ae22b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/request.md @@ -0,0 +1,50 @@ +--- +title: 'request( method, url, [body], [params] )' +description: 'Issue any type of HTTP request.' +description: 'Issue any type of HTTP request.' +weight: 10 +--- + +# request( method, url, [body], [params] ) + +| Parameter | Type | Description | +| ----------------- | ----------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | +| method | string | Request method (e.g. `'POST'`). Must be uppercase. | +| url | string /[HTTP URL](https://grafana.com/docs/k6//javascript-api/k6-http/url#returns) | Request URL (e.g. `'http://example.com'`). | +| body (optional) | string / object / ArrayBuffer | Request body; Objects will be `x-www-form-urlencoded` encoded. | +| params (optional) | object | [Params](https://grafana.com/docs/k6//javascript-api/k6-http/params) object containing additional request parameters. | + +### Returns + +| Type | Description | +| -------- | ------------------------------------------------------------------------------------------------- | +| Response | HTTP [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) object. | + +### Example + +Using http.request() to issue a POST request: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +const url = 'https://httpbin.test.k6.io/post'; + +export default function () { + const data = { name: 'Bert' }; + + // Using a JSON string as body + let res = http.request('POST', url, JSON.stringify(data), { + headers: { 'Content-Type': 'application/json' }, + }); + console.log(res.json().json.name); // Bert + + // Using an object as body, the headers will automatically include + // 'Content-Type: application/x-www-form-urlencoded'. + res = http.request('POST', url, data); + console.log(res.json().form.name); // Bert +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/response/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/response/_index.md new file mode 100644 index 000000000..b97cf996c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/response/_index.md @@ -0,0 +1,76 @@ +--- +title: 'Response' +description: 'Returned by the http.* methods that generate HTTP requests.' +weight: 61 +--- + +# Response + +Response is used by the http.\* methods that generate HTTP request. Those methods return one (or more, in the case of `http.batch()`) Response objects that contain HTTP response contents and performance timing measurements. + +Note that in the case of redirects, all the information in the Response object will pertain to the last request (the one that doesn't get redirected). + +| Name | Type | Description | +| ------------------------------------------------------------------------------------------------------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Response.body` | string | Response body content, often used to extract dynamic data (see examples [here](https://grafana.com/docs/k6//examples/correlation-and-dynamic-data)) and when verifying the presence of content using [checks](https://grafana.com/docs/k6//javascript-api/k6/check).

See [Params.responseType](https://grafana.com/docs/k6//javascript-api/k6-http/params) and [options.discardResponseBodies](https://grafana.com/docs/k6//using-k6/k6-options/reference) for how to discard the body when it is not needed (and to save memory) or when handling bodies with binary data. | +| `Response.cookies` | object | Response cookies. The object properties are the cookie names and the value is an array of cookie objects (with `name`, `value`, `domain`, `path`, `httpOnly`, `secure`, `maxAge` and `expires` fields). | +| `Response.error` | string | Error message if there was a non-HTTP error while trying to send the request. | +| `Response.error_code` | number | [Error code](https://grafana.com/docs/k6//javascript-api/error-codes) if there was a non-HTTP error or 4xx or 5xx HTTP error it will be set to a specific code that describes the error. (Added in 0.24.0) | +| `Response.headers` | object | Key-value pairs representing all HTTP headers sent by the server. Note that the header names are in [canonical form](https://pkg.go.dev/net/http#CanonicalHeaderKey); for example, if the server responds with "accept-encoding", the key will be "Accept-Encoding". When requesting a header by a specific name, an array of strings is returned since the header can have multiple values. You can access each value by its index - for the first value, or for single-valued header, that will be `Response.headers["my_key"][0]`. | +| `Response.ocsp.produced_at` | number | If a stapled OSCP response was provided by server, the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC, representing the time when this OCSP stapled response was signed by CA (or by CA entrusted OCSP responder) | +| `Response.ocsp.this_update` | number | If a stapled OSCP response was provided by server, the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC, representing the time at which the status being indicated was known to be correct. | +| `Response.ocsp.next_update` | number | If a stapled OSCP response was provided by server, the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC, representing the time when this OCSP stapled response will be refreshed with CA (or by CA entrusted OCSP responder). | +| `Response.ocsp.revocation_reason` | string | The reason for revocation of the certificate (if that is the status), one of the following constants: `http.OCSP_REASON_UNSPECIFIED`, `http.OCSP_REASON_KEY_COMPROMISE`, `http.OCSP_REASON_CA_COMPROMISE`,
`http.OCSP_REASON_AFFILIATION_CHANGED`,
`http.OCSP_REASON_SUPERSEDED`,
`http.OCSP_REASON_CESSATION_OF_OPERATION`,
`http.OCSP_REASON_CERTIFICATE_HOLD`,
`http.OCSP_REASON_REMOVE_FROM_CRL`,
`http.OCSP_REASON_PRIVILEGE_WITHDRAWN` or
`http.OCSP_REASON_AA_COMPROMISE`. | +| `Response.ocsp.revoked_at` | number | If a stapled OSCP response was provided by server, the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC, representing the time when this certificate was revoked (if that is the status). | +| `Response.ocsp.status` | string | The status of the certificate, one of the following constants: `http.OCSP_STATUS_GOOD`, `http.OCSP_STATUS_REVOKED`, `http.OCSP_STATUS_UNKNOWN` or `http.OCSP_STATUS_SERVER_FAILED`. | +| `Response.proto` | string | Protocol used to perform the transfer. Possible values are "HTTP/1.0", "HTTP/1.1", or "HTTP/2.0". | +| `Response.remote_ip` | string | The IP address of the server handling the request. | +| `Response.remote_port` | number | The port that was connected to on the server side. | +| `Response.request.body` | string | Request body content. | +| `Response.request.cookies` | object | Request cookies. The object properties are the cookie names and the value is an array of cookie objects (with `name`, `value` and `replace` fields). | +| `Response.request.headers` | object | Request headers. | +| `Response.request.method` | string | Request HTTP method. | +| `Response.request.url` | string | Request URL. | +| `Response.status` | number | HTTP status code returned by the server. | +| `Response.status_text` | string | _(new in k6 v0.29.0)_ HTTP status text returned by the server. | +| `Response.timings` | object | Performance timing information for the HTTP request. | +| `Response.timings.blocked` | float | Containing time (ms) spent blocked before initiating request. | +| `Response.timings.connecting` | float | Containing time (ms) spent setting up TCP connection to host. | +| `Response.timings.tls_handshaking` | float | Containing time (ms) spent handshaking TLS session with host. | +| `Response.timings.sending` | float | Containing time (ms) spent sending request. | +| `Response.timings.waiting` | float | Containing time (ms) spent waiting for server response. | +| `Response.timings.receiving` | float | Containing time (ms) spent receiving response data. | +| `Response.timings.duration` | float | Total time for the request (ms). It's equal to `sending + waiting + receiving`, i.e. how long did the remote server take to process the request and respond, without the initial DNS lookup/connection times. | +| `Response.tls_cipher_suite` | string | If a TLS session was established, the cipher suite that was used. | +| `Response.tls_version` | string | If a TLS session was established, the version of SSL/TLS that was used. | +| `Response.url` | string | The URL that was ultimately fetched (i.e. after any potential redirects). | +| [Response.clickLink( [params] )](https://grafana.com/docs/k6//javascript-api/k6-http/response/response-clicklink) | function | Parses response as HTML, looks for a specific link and does the request-level equivalent of a click on that link. | +| [Response.html()](https://grafana.com/docs/k6//javascript-api/k6-http/response/response-html) | function | Returns an object that supports [Selection.find(selector)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-find). | +| [Response.json( [selector] )](https://grafana.com/docs/k6//javascript-api/k6-http/response/response-json) | function | Parses the response body data as JSON and returns a JS object or array. This call caches the deserialized JSON data, additional calls will return the cached data. An optional selector can be specified to extract a specific part of the data, see [here for selector syntax](https://github.com/tidwall/gjson#path-syntax). | +| [Response.submitForm( [params] )](https://grafana.com/docs/k6//javascript-api/k6-http/response/response-submitform) | function | Parses response as HTML, parses the specified form (defaults to looking for a "form" element) with option to override fields and then submits form taking form's `method` and `action` into account. | + +### Example + +{{< code >}} + +```javascript +import { check } from 'k6'; +import http from 'k6/http'; + +export default function () { + const res = http.get('https://k6.io'); + for (const p in res.headers) { + if (res.headers.hasOwnProperty(p)) { + console.log(p + ' : ' + res.headers[p]); + } + } + check(res, { + 'status is 200': (r) => r.status === 200, + 'caption is correct': (r) => r.html('h1').text() == 'Example Domain', + }); +} +``` + +{{< /code >}} + +_A k6 script that will make an HTTP request and print all HTTP response headers_ diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-clicklink---params.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-clicklink---params.md new file mode 100644 index 000000000..1f9ac592c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-clicklink---params.md @@ -0,0 +1,40 @@ +--- +title: 'Response.clickLink( [params] )' +slug: 'response-clicklink' +description: 'Create and make a request corresponding to a link, found in the HTML of response, being clicked.' +--- + +# Response.clickLink( [params] ) + +Create and make a request corresponding to a link, found in the HTML of response, being clicked. By default it will look for the first `a` tag with a `href` attribute in the HTML, but this can be overridden using the `selector` option. + +This method takes an object argument where the following properties can be set: + +| Param | Type | Description | +| -------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | A selector string passed to [Selection.find(selector)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-find) to locate the link to click. By default this is `"a[href]"`. | +| params | object | A [Params](https://grafana.com/docs/k6//javascript-api/k6-http/params) object that will be forwarded to the link click request. Can be used to set headers, cookies etc. | + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------ | ----------------------- | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) | The link click response | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + // Request page with links + let res = http.get('https://httpbin.test.k6.io/links/10/0'); + + // Now, click the 4th link on the page + res = res.clickLink({ selector: 'a:nth-child(4)' }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-html.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-html.md new file mode 100644 index 000000000..37efe69b9 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-html.md @@ -0,0 +1,36 @@ +--- +title: 'Response.html()' +description: 'Parses response as HTML and populate a Selection.' +--- + +# Response.html() + +Parses response as HTML and populate a [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) object. + +### Returns + +| Type | Description | +| -------------------------------------------------------------------------------------- | ------------------ | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A Selection object | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + const res = http.get('https://stackoverflow.com'); + + const doc = res.html(); + doc + .find('link') + .toArray() + .forEach(function (item) { + console.log(item.attr('href')); + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-json.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-json.md new file mode 100644 index 000000000..94cf51edf --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-json.md @@ -0,0 +1,36 @@ +--- +title: 'Response.json( [selector] )' +description: 'Parses the response body data as JSON and returns a JS object or array.' +--- + +# Response.json( [selector] ) + +Parses the response body data as JSON and returns a JS object or array. This call caches the deserialized JSON data, additional calls will return the cached data. An optional selector can be specified to extract a specific part of the data, see [here for selector syntax](https://github.com/tidwall/gjson#path-syntax). + +This method takes an object argument where the following properties can be set: + +| Param | Type | Description | +| -------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | +| selector | string | An optional selector can be specified to extract a specific part of the data, see [here for selector syntax](https://github.com/tidwall/gjson#path-syntax). | + +### Returns + +| Type | Description | +| --------------- | ----------------------------------------- | +| Object or array | Returns the response body as JSON object. | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + const res = http.get('https://test-api.k6.io/public/crocodiles/'); + + console.log(res.json()); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-submitform---params.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-submitform---params.md new file mode 100644 index 000000000..f482b034b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/response/response-submitform---params.md @@ -0,0 +1,47 @@ +--- +title: 'Response.submitForm( [params] )' +slug: 'response-submitform' +description: 'Fill in and submit form found in HTML of response.' +--- + +# Response.submitForm( [params] ) + +Fill in and submit form found in HTML of response. By default it will look for the first `form` tag in the HTML, but this can be overridden using the `formSelector` option. To set/override the form fields you set properties of an object in the `fields` option. + +This method takes an object argument where the following properties can be set: + +| Param | Type | Description | +| -------------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| formSelector | string | A selector string passed to [Selection.find(selector)](https://grafana.com/docs/k6//javascript-api/k6-html/selection/selection-find) to locate the form to fill in and submit. By default this is `"form"`. | +| fields | object | The form fields to set/override. The keys are the form fields names and the values are the form field values. | +| submitSelector | string | A selector string used to locate the submit button in the form. By default this is `'[type="submit"]'`. | +| params | object | A [Params (k6/http)](https://grafana.com/docs/k6//javascript-api/k6-http/params) object that will be forwarded to the form submit request. Can be used to set headers, cookies etc. | + +### Returns + +| Type | Description | +| ---------------------------------------------------------------------------------------------- | ------------------------- | +| [Response (k6/http)](https://grafana.com/docs/k6//javascript-api/k6-http/response) | The form submit response. | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export default function () { + // Request page containing a form + let res = http.get('https://httpbin.test.k6.io/forms/post'); + + // Now, submit form setting/overriding some fields of the form + res = res.submitForm({ + formSelector: 'form', + fields: { custname: 'test', extradata: 'test2' }, + }); + sleep(3); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/set-response-callback.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/set-response-callback.md new file mode 100644 index 000000000..9a62e2d2e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/set-response-callback.md @@ -0,0 +1,55 @@ +--- +title: 'setResponseCallback( callback )' +description: 'set responseCallback to mark responses as expected' +description: 'set responseCallback to mark responses as expected' +weight: 10 +--- + +# setResponseCallback( callback ) + +Set the response callback to be called to determine if a response was expected/successful or not. + +The result of this is that requests will be tagged with `expected_response` `"true"` or `"false"` and `http_req_failed` will be emitted with the reverse. This does include all redirects so if for a request only 200 is the expected response and there is 301 redirect before that, the redirect will be marked as failed as only status code 200 was marked as expected. + +#### Exceptions + +Due to implementation specifics: + +- Requests with authentication `digest` are always expected to first get 401 and then to get whatever was specified. +- Requests with authentication `ntlm` will let a 401 status code on the first request as well as anything defined by `expectedStatuses` + +| Parameter | Type | Description | +| --------- | ----------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- | +| callback | [expectedStatuses](https://grafana.com/docs/k6//javascript-api/k6-http/expected-statuses) | an object returned from [expectedStatuses](https://grafana.com/docs/k6//javascript-api/k6-http/expected-statuses) | + +Currently only the very special [expectedStatuses](https://grafana.com/docs/k6//javascript-api/k6-http/expected-statuses) objects are supported but in the future it is planned that a JavaScript callback will be supported as well. By default requests with status codes between 200 and 399 are considered "expected". + +Setting the callback to `null` disables the tagging with `expected_response` and the emitting of `http_req_failed`. + +It is recommended that if a per request responseCallback is used with [Params](https://grafana.com/docs/k6//javascript-api/k6-http/params) it is actually defined once and used instead of creating it on each request. + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +http.setResponseCallback(http.expectedStatuses({ min: 200, max: 300 })); + +const only300Callback = http.expectedStatuses(300); + +export default () => { + // this will use the default response callback and be marked as successful + http.get('https://httpbin.test.k6.io/status/200'); + + // this will be marked as a failed request as it won't get the expected status code of 300 + http.get('https://httpbin.test.k6.io/status/200', { responseCallback: only300Callback }); + + http.setResponseCallback(http.expectedStatuses(301)); + // from here on for this VU only the 301 status code will be successful so on the next iteration of + // the VU the first request will be marked as failure +}; +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-http/url.md b/docs/sources/k6/v0.55.x/javascript-api/k6-http/url.md new file mode 100644 index 000000000..4162bf5b8 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-http/url.md @@ -0,0 +1,37 @@ +--- +title: 'url\`url\`' +description: 'Creates a URL with a name tag.' +description: 'Creates a URL with a name tag.' +weight: 10 +--- + +# url\`url\` + +URLs that contain dynamic parts can introduce a large number of unique URLs in the metrics stream. You can use `http.url` to set a consistent name tag in your requests to remedy this issue. Read more on [URL Grouping](https://grafana.com/docs/k6//using-k6/http-requests#url-grouping). + +| Parameter | Type | Description | +| --------- | ---------------- | ---------------------------------------- | +| url | template literal | Request URL (e.g. `http://example.com`). | + +### Returns + +| Type | Description | +| -------- | ---------------- | +| HTTP URL | HTTP URL object. | + +### Example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + for (let id = 1; id <= 100; id++) { + // tags.name="https://test.k6.io?id=${}", + http.get(http.url`https://test.k6.io?id=${id}`); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/_index.md new file mode 100644 index 000000000..b5143de1a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/_index.md @@ -0,0 +1,9 @@ +--- +title: 'k6/metrics' +description: 'k6 Custom Metrics API' +weight: 10 +--- + +# k6/metrics + +{{< docs/shared source="k6" lookup="javascript-api/k6-metrics.md" version="" >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/counter/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/counter/_index.md new file mode 100644 index 000000000..30105182d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/counter/_index.md @@ -0,0 +1,106 @@ +--- +title: 'Counter' +description: 'Counter is an object for representing a custom cumulative counter metric. It is one of the four custom metric types.' +weight: 70 +weight: 70 +--- + +# Counter + +_Counter_ is an object for representing a custom cumulative counter metric. It is one of the four custom metric types. + +| Parameter | Type | Description | +| --------- | ------ | ------------------------------ | +| `name` | string | The name of the custom metric. | + +| Method | Description | +| -------------------------------------------------------------------------------------------------------------------- | ---------------------------------- | +| [Counter.add(value, [tags])](https://grafana.com/docs/k6//javascript-api/k6-metrics/counter/counter-add) | Add a value to the counter metric. | + +## Counter usage in Thresholds + +When `Counter` is used in a threshold expression, the variable must be called `count` or `rate` (lower case). +For example: + +- `count >= 200` // value of the counter must be larger or equal to 200 +- `count < 10` // less than 10. + +### Examples + +{{< code >}} + +```javascript +import { Counter } from 'k6/metrics'; + +const myCounter = new Counter('my_counter'); + +export default function () { + myCounter.add(1); + myCounter.add(2, { tag1: 'myValue', tag2: 'myValue2' }); +} +``` + +{{< /code >}} + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { Counter } from 'k6/metrics'; + +const CounterErrors = new Counter('Errors'); + +export const options = { thresholds: { Errors: ['count<100'] } }; + +export default function () { + const res = http.get('https://test-api.k6.io/public/crocodiles/1/'); + const contentOK = res.json('name') === 'Bert'; + CounterErrors.add(!contentOK); +} +``` + +{{< /code >}} + +{{< code >}} + +```javascript +import { Counter } from 'k6/metrics'; +import { sleep } from 'k6'; +import http from 'k6/http'; + +const allErrors = new Counter('error_counter'); + +export const options = { + vus: 1, + duration: '1m', + thresholds: { + 'error_counter': [ + 'count < 10', // 10 or fewer total errors are tolerated + ], + 'error_counter{errorType:authError}': [ + // Threshold on a sub-metric (tagged values) + 'count <= 2', // max 2 authentication errors are tolerated + ], + }, +}; + +export default function () { + const auth_resp = http.post('https://test-api.k6.io/auth/token/login/', { + username: 'test-user', + password: 'supersecure', + }); + + if (auth_resp.status >= 400) { + allErrors.add(1, { errorType: 'authError' }); // tagged value creates submetric (useful for making thresholds specific) + } + + const other_resp = http.get('https://test-api.k6.io/public/crocodiles/1/'); + if (other_resp.status >= 400) { + allErrors.add(1); // untagged value + } + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/counter/counter-add-value---tags.md b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/counter/counter-add-value---tags.md new file mode 100644 index 000000000..0cb4ba4c2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/counter/counter-add-value---tags.md @@ -0,0 +1,16 @@ +--- +title: 'Counter.add(value, [tags])' +slug: 'counter-add' +description: 'Add a value to the Counter metric.' +--- + +# Counter.add(value, [tags]) + +Add a value to the `Counter` metric. + +| Parameter | Type | Description | +| --------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| value | number | The value to add to the counter. | +| tags | object | Set of [tags](https://grafana.com/docs/k6//using-k6/tags-and-groups) that will be tagged to the added data point (note that tags are added per data point and not for the entire metric). | + +[Counter examples](https://grafana.com/docs/k6//javascript-api/k6-metrics/counter#examples) diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/gauge/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/gauge/_index.md new file mode 100644 index 000000000..5e38d0625 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/gauge/_index.md @@ -0,0 +1,68 @@ +--- +title: 'Gauge' +description: 'Gauge is an object for representing a custom metric holding only the latest value added.' +weight: 71 +--- + +# Gauge + +_Gauge_ is an object for representing a custom metric holding only the latest value added. It is one of the four [custom metrics](https://grafana.com/docs/k6//javascript-api/k6-metrics). + +| Parameter | Type | Description | +| --------- | ------- | --------------------------------------------------------------------------------------------------- | +| `name` | string | The name of the custom metric. | +| `isTime` | boolean | A boolean indicating whether the values added to the metric are time values or just untyped values. | + +| Method | Description | +| -------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- | +| [Gauge.add(value, [tags])](https://grafana.com/docs/k6//javascript-api/k6-metrics/gauge/gauge-add) | Add a value to the gauge metric. Only the latest value added will be kept. | + +## Gauge usage in Thresholds + +When gauge is used in a threshold expression, the variable must be called `value` (lower case). +For example: + +- `value < 200` +- `value > 1` + +### Examples + +{{< code >}} + +```javascript +import { Gauge } from 'k6/metrics'; + +const myGauge = new Gauge('my_gauge'); + +export default function () { + myGauge.add(3); + myGauge.add(1); + myGauge.add(2, { tag1: 'value', tag2: 'value2' }); +} +``` + +{{< /code >}} + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; +import { Gauge } from 'k6/metrics'; + +const GaugeContentSize = new Gauge('ContentSize'); + +export const options = { + thresholds: { + ContentSize: ['value<4000'], + }, +}; + +export default function () { + const res = http.get('https://test-api.k6.io/public/crocodiles/1/'); + GaugeContentSize.add(res.body.length); + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/gauge/gauge-add-value---tags.md b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/gauge/gauge-add-value---tags.md new file mode 100644 index 000000000..5f58406d0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/gauge/gauge-add-value---tags.md @@ -0,0 +1,16 @@ +--- +title: 'Gauge.add(value, [tags])' +slug: 'gauge-add' +description: 'Set the value of the Gauge metric.' +--- + +# Gauge.add(value, [tags]) + +Set the value of the `Gauge` metric. + +| Parameter | Type | Description | +| --------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| value | number | The value to set the gauge to. | +| tags | object | Set of [tags](https://grafana.com/docs/k6//using-k6/tags-and-groups) that will be tagged to the added data point (note that tags are added per data point and not for the entire metric). | + +[Gauge examples](https://grafana.com/docs/k6//javascript-api/k6-metrics/gauge#examples) diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/rate/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/rate/_index.md new file mode 100644 index 000000000..d19199edc --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/rate/_index.md @@ -0,0 +1,78 @@ +--- +title: 'Rate' +description: 'Rate is an object for representing a custom metric keeping track of the percentage of added values that are non-zero.' +weight: 72 +weight: 72 +--- + +# Rate + +_Rate_ is an object for representing a custom metric keeping track of the percentage of added values that are non-zero. It is one of the four [custom metrics](https://grafana.com/docs/k6//javascript-api/k6-metrics). + +| Parameter | Type | Description | +| --------- | ------ | ------------------------------ | +| `name` | string | The name of the custom metric. | + +| Method | Description | +| ----------------------------------------------------------------------------------------------------------- | ------------------------------- | +| [Rate.add(value, [tags])](https://grafana.com/docs/k6//javascript-api/k6-metrics/rate/rate-add) | Add a value to the rate metric. | + +## Rate usage in Thresholds + +When `Rate` is used in a threshold expression, the variable must be called `rate` (lower case). +For example: + +- `rate < 0.1` // less than 10% +- `rate >= 0.9` // more or equal to 90% + +The value of the `rate` variable ranges between `0.00` and `1.00`. + +### Examples + +{{< code >}} + +```javascript +import { Rate } from 'k6/metrics'; + +const myRate = new Rate('my_rate'); + +export default function () { + myRate.add(true); + myRate.add(false); + myRate.add(1); + myRate.add(0, { tag1: 'value', tag2: 'value2' }); +} +``` + +{{< /code >}} + +{{< code >}} + +```javascript +import { Rate } from 'k6/metrics'; +import { sleep } from 'k6'; +import http from 'k6/http'; + +const errorRate = new Rate('errorRate'); + +export const options = { + vus: 1, + duration: '5m', + thresholds: { + errorRate: [ + // more than 10% of errors will abort the test + { threshold: 'rate < 0.1', abortOnFail: true, delayAbortEval: '1m' }, + ], + }, +}; + +export default function () { + const resp = http.get('https://test-api.k6.io/public/crocodiles/1/'); + + errorRate.add(resp.status >= 400); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/rate/rate-add-value---tags.md b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/rate/rate-add-value---tags.md new file mode 100644 index 000000000..fd57d5a37 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/rate/rate-add-value---tags.md @@ -0,0 +1,16 @@ +--- +title: 'Rate.add(value, [tags])' +slug: 'rate-add' +description: 'Set the value of the Rate metric.' +--- + +# Rate.add(value, [tags]) + +Set the value of the `Rate` metric. + +| Parameter | Type | Description | +| --------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| value | number | The value to add to the rate metric. | +| tags | object | Set of [tags](https://grafana.com/docs/k6//using-k6/tags-and-groups) that will be tagged to the added data point (note that tags are added per data point and not for the entire metric). | + +[Rate examples](https://grafana.com/docs/k6//javascript-api/k6-metrics/rate#examples) diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/trend/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/trend/_index.md new file mode 100644 index 000000000..08c7c5986 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/trend/_index.md @@ -0,0 +1,90 @@ +--- +title: 'Trend' +description: 'Trend is an object for representing a custom metric that allows for calculating different statistics on the added values (min, max, average or percentiles)' +weight: 73 +weight: 73 +--- + +# Trend + +_Trend_ is an object for representing a custom metric that allows for calculating different statistics on the added values (min, max, average or percentiles). It is one of the four [custom metrics](https://grafana.com/docs/k6//javascript-api/k6-metrics). + +| Parameter | Type | Description | +| --------- | ------- | --------------------------------------------------------------------------------------------------- | +| `name` | string | The name of the custom metric. | +| `isTime` | boolean | A boolean indicating whether the values added to the metric are time values or just untyped values. | + +| Method | Description | +| -------------------------------------------------------------------------------------------------------------- | -------------------------------- | +| [Trend.add(value, [tags])](https://grafana.com/docs/k6//javascript-api/k6-metrics/trend/trend-add) | Add a value to the trend metric. | + +## Trend usage in Thresholds + +When `Trend` is used in a [threshold expression](https://grafana.com/docs/k6//using-k6/thresholds), there are a range of variables that can be used. + +- `avg` for average +- `min` for minimum +- `max` for maximum +- `med` for median +- `p(N)` for specific percentile. `N` is a number between `0.0` and `100.0` meaning the percentile value to look at, e.g. `p(99.99)` means the 99.99th percentile. + +The unit of these variables and functions are all in milliseconds. + +### Example threshold expressions: + +- `p(95) < 400` // 95% of requests must finish below 400ms +- `p(99) < 1000` // 99% of requests must finish within 1s. +- `p(50) < 200` // half of requests must finish within 200ms. +- `max < 3000` // the slowest request must finish within 3s. + +> #### ⚠️ Don't use `min` and `max` in thresholds +> +> We don't recommend using `min` and `max` for specifying thresholds because these +> values represent outliers. Use percentiles instead. + +### Examples + +{{< code >}} + +```javascript +import { Trend } from 'k6/metrics'; + +const myTrend = new Trend('my_trend'); + +export default function () { + myTrend.add(1); + myTrend.add(2, { tag1: 'value', tag2: 'value2' }); +} +``` + +{{< /code >}} + +{{< code >}} + +```javascript +import { Trend } from 'k6/metrics'; +import { sleep } from 'k6'; +import http from 'k6/http'; + +const serverWaitingTimeOnLogin = new Trend('serverWaitingTimeOnLogin', true); + +export const options = { + vus: 1, + duration: '1m', + thresholds: { + serverWaitingTimeOnLogin: ['p(95) < 200'], + }, +}; + +export default function () { + const resp = http.post('https://test-api.k6.io/auth/token/login/', { + username: 'test-user', + password: 'supersecure', + }); + + serverWaitingTimeOnLogin.add(resp.timings.waiting); + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/trend/trend-add-value---tags.md b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/trend/trend-add-value---tags.md new file mode 100644 index 000000000..5aa517287 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-metrics/trend/trend-add-value---tags.md @@ -0,0 +1,16 @@ +--- +title: 'Trend.add(value, [tags])' +slug: 'trend-add' +description: 'Add a value to the Trend metric.' +--- + +# Trend.add(value, [tags]) + +Add a value to the `Trend` metric. + +| Parameter | Type | Description | +| --------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| value | number | The value to add to the trend metric. | +| tags | object | Set of [tags](https://grafana.com/docs/k6//using-k6/tags-and-groups) that will be tagged to the added data point (note that tags are added per data point and not for the entire metric). | + +[Trend examples](https://grafana.com/docs/k6//javascript-api/k6-metrics/trend#examples) diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/_index.md new file mode 100644 index 000000000..16f9595b3 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/_index.md @@ -0,0 +1,48 @@ +--- +title: 'k6/net/grpc' +description: 'k6 gRPC API' +weight: 11 +aliases: + - ./k6-experimental/grpc/ # docs/k6//javascript-api/k6-experimental/grpc/ +--- + +# k6/net/grpc + +{{< docs/shared source="k6" lookup="javascript-api/k6-net-grpc.md" version="" >}} + +## gRPC metrics + +k6 takes specific measurements for gRPC requests. +For the complete list, refer to the [Metrics reference](https://grafana.com/docs/k6//using-k6/metrics/reference#grpc). + +## Example + +{{< code >}} + +```javascript +import grpc from 'k6/net/grpc'; +import { check, sleep } from 'k6'; + +const client = new grpc.Client(); +client.load(['definitions'], 'hello.proto'); + +export default () => { + client.connect('grpcbin.test.k6.io:9001', { + // plaintext: false + }); + + const data = { greeting: 'Bert' }; + const response = client.invoke('hello.HelloService/SayHello', data); + + check(response, { + 'status is OK': (r) => r && r.status === grpc.StatusOK, + }); + + console.log(JSON.stringify(response.message)); + + client.close(); + sleep(1); +}; +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/_index.md new file mode 100644 index 000000000..29e3f0734 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/_index.md @@ -0,0 +1,107 @@ +--- +title: Client +description: 'Client is a gRPC client that can interact with a gRPC server.' +weight: 10 +aliases: + - ../k6-experimental/grpc/client/ # docs/k6//javascript-api/k6-experimental/grpc/client/ +--- + +# Client + +`Client` is a gRPC client that can interact with a gRPC server. + +| Method | Description | +| ----------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [Client.load(importPaths, ...protoFiles)](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client/client-load) | Loads and parses the given protocol buffer definitions to be made available for RPC requests. | +| [Client.loadProtoset(protosetPath)](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client/client-loadprotoset) | Loads and parses the given protoset file to be made available for RPC requests. | +| [Client.connect(address [,params])](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client/client-connect) | Opens a connection to the given gRPC server. | +| [Client.invoke(url, request [,params])](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client/client-invoke) | Makes an unary RPC for the given service/method and returns a [Response](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/response). | +| [Client.close()](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client/client-close) | Close the connection to the gRPC service. | + +### Examples + +
+ +```javascript +import grpc from 'k6/net/grpc'; + +const client = new grpc.Client(); +// Download addsvc.proto for https://grpcbin.test.k6.io/, located at: +// https://raw.githubusercontent.com/moul/pb/master/addsvc/addsvc.proto +// and put it in the same folder as this script. +client.load(null, 'addsvc.proto'); + +export default () => { + client.connect('grpcbin.test.k6.io:9001', { timeout: '5s' }); + + const response = client.invoke('addsvc.Add/Sum', { + a: 1, + b: 2, + }); + console.log(response.message.v); // should print 3 + + client.close(); +}; +``` + +
+ +
+ +```javascript +import grpc from 'k6/net/grpc'; +import { check } from 'k6'; + +const client = new grpc.Client(); +client.load([], 'authorization.proto', 'route_guide.proto'); + +export function setup() { + client.connect('auth.googleapis.com:443'); + const resp = client.invoke('google.cloud.authorization.v1.AuthService/GetAccessToken', { + username: 'john.smith@k6.io', + password: 'its-a-secret', + }); + client.close(); + return resp.message.accessToken; +} + +export default (token) => { + client.connect('route.googleapis.com:443'); + const metadata = { + authorization: `bearer ${token}`, + }; + const response = client.invoke( + 'google.cloud.route.v1.RoutingService/GetFeature', + { + latitude: 410248224, + longitude: -747127767, + }, + { metadata } + ); + check(response, { 'status is OK': (r) => r && r.status === grpc.StatusOK }); + client.close(); +}; +``` + +
+ +
+ +```javascript +import grpc from 'k6/net/grpc'; +import { check } from 'k6'; + +const client = new grpc.Client(); +client.load([], 'language_service.proto'); + +export default () => { + if (__ITER == 0) { + client.connect('language.googleapis.com:443'); + } + const response = client.invoke('google.cloud.language.v1.LanguageService/AnalyzeSentiment', {}); + check(response, { 'status is OK': (r) => r && r.status === grpc.StatusOK }); + // Do NOT close the client +}; +``` + +
diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-async-invoke.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-async-invoke.md new file mode 100644 index 000000000..ae0b8e7c4 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-async-invoke.md @@ -0,0 +1,56 @@ +--- +title: 'Client.asyncInvoke(url, request [,params])' +description: 'Asynchronously invokes an unary RPC request to the given method.' +weight: 30 +--- + +# Client.asyncInvoke(url, request [,params]) + +Asynchronously invokes an unary RPC request to the given method. + +The given method to invoke must have its RPC schema previously loaded via the [Client.load()](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client/client-load) function, otherwise an error will be thrown. + +[Client.connect()](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client/client-connect) must be called first before invoking a request, otherwise an error will be thrown. + +| Parameter | Type | Description | +| ----------------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| url | string | The gRPC method URL to invoke, in the form `/package.Service/Method`, e.g. `/google.cloud.language.v1.LanguageService/AnalyzeSentiment`. The leading slash `/` is optional. | +| request | object | The canonical request object, as-per the [Protobuf JSON Mapping](https://grafana.com/docs/k6//using-k6/protocols/grpc/#protocol-buffers-json-mapping). | +| params (optional) | object | [Params](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/params) object containing additional request parameters. | + +### Returns + +| Type | Description | +| ----------------------- | ----------------------------------------------------------------------------------------------------- | +| Promise with `Response` | gRPC [Response](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/response) object. | + +### Examples + +
+ +```javascript +import grpc from 'k6/net/grpc'; +import { check } from 'k6'; + +const client = new grpc.Client(); +client.load([], 'routeguide.proto'); + +export default () => { + client.connect('localhost:10000', { plaintext: true }); + + client + .asyncInvoke('main.RouteGuide/GetFeature', { + latitude: 410248224, + longitude: -747127767, + }) + .then((response) => { + check(response, { 'status is OK': (r) => r && r.status === grpc.StatusOK }); + console.log(response.message.name); + // output: 3 Hasta Way, Newton, NJ 07860, USA + + client.close(); + }); +}; +``` + +
diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-close.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-close.md new file mode 100644 index 000000000..302f85451 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-close.md @@ -0,0 +1,29 @@ +--- +title: 'Client.close()' +description: 'Close the connection to the gRPC service. Tear down all underlying connections.' +weight: 40 +aliases: + - ../../k6-experimental/grpc/client/client-close/ # docs/k6//javascript-api/k6-experimental/grpc/client/client-close/ +--- + +# Client.close() + +Close the connection to the gRPC service. Tear down all underlying connections. + +### Examples + +
+ +```javascript +import grpc from 'k6/net/grpc'; + +const client = new grpc.Client(); +client.load(['definitions'], 'hello.proto'); + +export default () => { + client.connect('localhost:8080'); + client.close(); +}; +``` + +
diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-connect-connect-address-params.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-connect-connect-address-params.md new file mode 100644 index 000000000..a36ddefd7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-connect-connect-address-params.md @@ -0,0 +1,135 @@ +--- +title: 'Client.connect(address [,params])' +description: 'Opens a connection to a gRPC server; will block until a connection is made or a connection error is thrown.' +weight: 20 +slug: 'client-connect' +aliases: + - ../../k6-experimental/grpc/client/client-connect/ # docs/k6//javascript-api/k6-experimental/grpc/client/client-connect/ +--- + +# Client.connect(address [,params]) + +Opens a connection to a gRPC server; will block until a connection is made or a connection error is thrown. Cannot be called during the [`init` phase](https://grafana.com/docs/k6//using-k6/test-lifecycle). + +See [Client.close()](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client/client-close) to close the connection. + +| Parameter | Type | Description | +| ----------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| address | string | The address of the gRPC server. Should be in the form: `host:port` with no protocol prefix e.g. `grpc.k6.io:443`. The host must be a literal IP address, or a host name that can be resolved to IP addresses. The port must be a literal port number or a service name e.g. `:443` or `:https`. If the host is a literal IPv6 address it must be enclosed in square brackets, as in `[2001:db8::1]:80` or `[fe80::1%zone]:80`. | +| params (optional) | object | [ConnectParams](#connectparams) object containing additional connect parameters. | + +## ConnectParams + +| Name | Type | Description | +| ------------------------------- | --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `ConnectParams.plaintext` | bool | If `true` will connect to the gRPC server using plaintext i.e. insecure. Defaults to `false` i.e. secure via TLS. | +| `ConnectParams.reflect` | boolean | Whether to use the [gRPC server reflection protocol](https://github.com/grpc/grpc/blob/master/doc/server-reflection.md) when connecting. | +| `ConnectParams.reflectMetadata` | object | Object with key-value pairs representing custom metadata the user would like to add to the reflection request. | +| `ConnectParams.timeout` | string / number | Connection timeout to use. Default timeout is `"60s"`.
The type can also be a number, in which case k6 interprets it as milliseconds, e.g., `60000` is equivalent to `"60s"`. | +| `ConnectParams.maxReceiveSize` | number | Sets the maximum message size in bytes the client can receive. Defaults to `grpc-go` default, which is 4MB. | +| `ConnectParams.maxSendSize` | number | Sets the maximum message size in bytes the client can send. Defaults to `grpc-go` default, which is approximately 2GB. | +| `ConnectParams.tls` (optional) | object | [TLS](#tls) settings of the connection. Defaults to `null`. | + +## TLS + +TLS settings of the connection. If not defined, the main TLS config from options will be used. + +| Name | Type | Description | +| -------------- | -------------- | ----------------------------------------------------- | +| `tls.cert` | string | PEM formatted client certificate. | +| `tls.key` | string | PEM formatted client private key. | +| `tls.password` | string | Password for decrypting the client's private key. | +| `tls.cacerts` | string / array | PEM formatted strings of the certificate authorities. | + +### Examples + +
+ +```javascript +import grpc from 'k6/net/grpc'; + +const client = new grpc.Client(); + +export default () => { + client.connect('localhost:8080'); +}; +``` + +
+ +
+ +```javascript +import grpc from 'k6/net/grpc'; + +const client = new grpc.Client(); + +export default () => { + client.connect('localhost:8080', { plaintext: true }); +}; +``` + +
+ +
+ +```javascript +import grpc from 'k6/net/grpc'; +import { check } from 'k6'; +import { SharedArray } from 'k6/data'; +import exec from 'k6/execution'; + +// note: the services in this example don't exist. If you would like +// to run this example, make sure to replace the URLs, and +// the cacerts, cert, key, and password variables. +const grpcArgs = new SharedArray('grpc', () => { + // Using SharedArray here so that not every VU gets a copy of every certificate a key + return [ + { + host: 'foo1.grpcbin.test.k6.io:9001', + plaintext: false, + params: { + tls: { + cacerts: [open('cacerts0.pem')], + cert: open('cert0.pem'), + key: open('key0.pem'), + }, + }, + }, + { + host: 'foo2.grpcbin.test.k6.io:9002', + params: { + plaintext: false, + tls: { + cacerts: open('cacerts1.pem'), + cert: open('cert1.pem'), + key: open('key1.pem'), + password: 'cert1-passphrase', + }, + }, + }, + ]; +}); + +const client = new grpc.Client(); + +export default () => { + if (__ITER === 0) { + // Take one config and use it for this one VU + const grpcArg = grpcArgs[exec.vu.idInTest % grpcArgs.length]; + client.connect(grpcArg.host, grpcArg.params); + } + + const response = client.invoke('hello.HelloService/SayHello', { + greeting: 'Bert', + }); + + check(response, { + 'status is OK': (r) => r && r.status === grpc.StatusOK, + }); + + console.log(JSON.stringify(response.message)); +}; +``` + +
diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-invoke.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-invoke.md new file mode 100644 index 000000000..5065c7f58 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-invoke.md @@ -0,0 +1,55 @@ +--- +title: 'Client.invoke(url, request [,params])' +description: 'Invokes an unary RPC request to the given method.' +weight: 30 +aliases: + - ../../k6-experimental/grpc/client/client-invoke/ # docs/k6//javascript-api/k6-experimental/grpc/client/client-invoke/ +--- + +# Client.invoke(url, request [,params]) + +Invokes an unary RPC request to the given method. + +The given method to invoke must have its RPC schema previously loaded via the [Client.load()](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client/client-load) function, otherwise an +error will be thrown. + +[Client.connect()](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client/client-connect) must be called first before invoking a request, otherwise an error will be thrown. + +| Parameter | Type | Description | +| ----------------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| url | string | The gRPC method URL to invoke, in the form `/package.Service/Method`, e.g. `/google.cloud.language.v1.LanguageService/AnalyzeSentiment`. The leading slash `/` is optional. | +| request | object | The canonical request object, as-per the [Protobuf JSON Mapping](https://grafana.com/docs/k6//using-k6/protocols/grpc/#protocol-buffers-json-mapping). | +| params (optional) | object | [Params](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/params) object containing additional request parameters. | + +### Returns + +| Type | Description | +| ---------- | ----------------------------------------------------------------------------------------------------- | +| `Response` | gRPC [Response](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/response) object. | + +### Examples + +
+ +```javascript +import grpc from 'k6/net/grpc'; +import { check } from 'k6'; + +const client = new grpc.Client(); +client.load([], 'routeguide.proto'); + +export default () => { + client.connect('localhost:10000', { plaintext: true }); + const response = client.invoke('main.RouteGuide/GetFeature', { + latitude: 410248224, + longitude: -747127767, + }); + check(response, { 'status is OK': (r) => r && r.status === grpc.StatusOK }); + console.log(response.message.name); + // output: 3 Hasta Way, Newton, NJ 07860, USA + + client.close(); +}; +``` + +
diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-load-protoset.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-load-protoset.md new file mode 100644 index 000000000..ec40d245c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-load-protoset.md @@ -0,0 +1,31 @@ +--- +title: 'Client.loadProtoset(protosetPath)' +slug: 'client-loadprotoset' +description: 'Loads and parses the protoset file (serialized FileDescriptor set) so they are available to the client to marshal/unmarshal the correct request and response data structures for the RPC schema.' +weight: 11 +aliases: + - ../../k6-experimental/grpc/client/client-loadprotoset/ # docs/k6//javascript-api/k6-experimental/grpc/client/client-loadprotoset/ +--- + +# Client.loadProtoset(protosetPath) + +Loads and parses the protoset file (serialized FileDescriptor set) so they are available to the client to marshal/unmarshal the correct request and response data structures for the RPC schema. + +Must be called within the [`init` phase](https://grafana.com/docs/k6//using-k6/test-lifecycle). + +| Parameter | Type | Description | +| ------------ | ------ | ---------------------------------------------------------------------------------------------------------------------------------- | +| protosetPath | string | The path of the protoset file. If no import paths are provided then "." (current directory) is assumed to be the only import path. | + +### Examples + +
+ +```javascript +import grpc from 'k6/net/grpc'; + +const client = new grpc.Client(); +client.loadProtoset('./dummy.protoset'); +``` + +
diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-load.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-load.md new file mode 100644 index 000000000..d16434f31 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/client/client-load.md @@ -0,0 +1,48 @@ +--- +title: 'Client.load(importPaths, ...protoFiles)' +description: 'Loads and parses the protocol buffer descriptors so they are available to the client to marshal/unmarshal the correct request and response data structures for the RPC schema.' +weight: 10 +aliases: + - ../../k6-experimental/grpc/client/client-load/ # docs/k6//javascript-api/k6-experimental/grpc/client/client-load/ +--- + +# Client.load(importPaths, ...protoFiles) + +Loads and parses the protocol buffer descriptors so they are available to the client to marshal/unmarshal the correct request and response data structures for the RPC schema. + +Must be called within the [`init` phase](https://grafana.com/docs/k6//using-k6/test-lifecycle). + +| Parameter | Type | Description | +| ----------- | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| importPaths | Array<string> \| `null` | The paths used to search for dependencies that are referenced in import statements in proto source files. If no import paths are provided then "." (current directory) is assumed to be the only import path. | +| protoFiles | Array<string> | [Rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) for the list of proto files to load/parse. | + +### Examples + +
+ +```javascript +import grpc from 'k6/net/grpc'; + +const client = new grpc.Client(); +client.load([], 'language_service.proto'); +``` + +
+ +
+ +```javascript +import grpc from 'k6/net/grpc'; + +const client = new grpc.Client(); + +client.load( + ['../googleapis/google'], + 'spanner/admin/instance/v1/spanner_instance_admin.proto', + 'spanner/admin/instance/v1/spanner_instance_admin.proto', + 'spanner/v1/spanner.proto' +); +``` + +
diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/constants.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/constants.md new file mode 100644 index 000000000..f8af6e828 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/constants.md @@ -0,0 +1,61 @@ +--- +title: 'Constants' +description: 'Define constants to distinguish between gRPC Response' +weight: 40 +aliases: + - ../k6-experimental/grpc/constants/ # docs/k6//javascript-api/k6-experimental/grpc/constants/ +--- + +# Constants + +Define constants to distinguish between [gRPC Response](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/response) statuses. + +| Constant | Description | +| -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `StatusOK` | OK is returned on success. | +| `StatusCanceled` | Canceled indicates the operation was canceled (typically by the caller). | +| `StatusUnknown` | Unknown error. | +| `StatusInvalidArgument` | InvalidArgument indicates the client specified an invalid argument. | +| `StatusDeadlineExceeded` | DeadlineExceeded means operation expired before completion. | +| `StatusNotFound` | NotFound means some requested entity (e.g., file or directory) was not found. | +| `StatusAlreadyExists` | AlreadyExists means an attempt to create an entity failed because one already exists. | +| `StatusPermissionDenied` | PermissionDenied indicates the caller does not have permission to execute the specified operation. | +| `StatusResourceExhausted` | ResourceExhausted indicates some resource has been exhausted, perhaps a per-user quota, or perhaps the entire file system is out of space. | +| `StatusFailedPrecondition` | FailedPrecondition indicates operation was rejected because the system is not in a state required for the operation's execution. | +| `StatusAborted` | Aborted indicates the operation was aborted, typically due to a concurrency issue like sequencer check failures, transaction aborts, etc. | +| `StatusOutOfRange` | OutOfRange means operation was attempted past the valid range. E.g., seeking or reading past end of file. | +| `StatusUnimplemented` | Unimplemented indicates operation is not implemented or not supported/enabled in this service. | +| `StatusInternal` | Internal errors. Means some invariants expected by the underlying system have been broken. | +| `StatusUnavailable` | Unavailable indicates the service is currently unavailable. This is a most likely a transient condition and may be corrected by retrying with a backoff. Note that it is not always safe to retry non-idempotent operations. | +| `StatusDataLoss` | DataLoss indicates unrecoverable data loss or corruption. | +| `StatusUnauthenticated` | Unauthenticated indicates the request does not have valid authentication credentials for the operation. | + +### Example + +{{< code >}} + +```javascript +import grpc from 'k6/net/grpc'; +import { check, sleep } from 'k6'; + +const client = new grpc.Client(); +client.load(['definitions'], 'hello.proto'); + +export default () => { + client.connect('grpcbin.test.k6.io:9001', { + // plaintext: false + }); + + const data = { greeting: 'Bert' }; + const response = client.invoke('hello.HelloService/SayHello', data); + + check(response, { + 'status is OK': (r) => r && r.status === grpc.StatusOK, + }); + + client.close(); + sleep(1); +}; +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/params.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/params.md new file mode 100644 index 000000000..e62f8d3b4 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/params.md @@ -0,0 +1,47 @@ +--- +title: 'Params' +head_title: 'gRPC.params' +description: 'Params is an object used by the gRPC methods that generate RPC requests.' +weight: 20 +aliases: + - ../k6-experimental/grpc/params/ # docs/k6//javascript-api/k6-experimental/grpc/params/ +--- + +# Params + +_Params_ is an object used by the gRPC methods that generate RPC requests. _Params_ contains request-specific options like headers that should be inserted into the request. + +| Name | Type | Description | +| ------------------------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| `Params.discardResponseMessage` | boolean | Specify if response messages should be discarded. This reduces the amount of memory required, and the amount of garbage collection, which reduces the load on the testing machine and can help produce more reliable test results. Default is `false`. | +| `Params.metadata` | object | Object with key-value pairs representing custom metadata the user would like to add to the request. Values of [keys ending with `-bin`](https://grpc.io/docs/what-is-grpc/core-concepts/#metadata) will be treated as binary data. | +| `Params.tags` | object | Key-value pairs where the keys are names of tags and the values are tag values. Response time metrics generated as a result of the request will have these tags added to them, allowing the user to filter out those results specifically, when looking at results data. | +| `Params.timeout` | string / number | Request timeout to use. Default timeout is 60 seconds (`"60s"`).
The type can also be a number, in which case k6 interprets it as milliseconds, e.g., `60000` is equivalent to `"60s"`. | + +### Example of custom metadata headers and tags + +
+ +```javascript +import grpc from 'k6/net/grpc'; + +const client = new grpc.Client(); +client.load([], 'route_guide.proto'); + +export default function () { + const req = { + latitude: 410248224, + longitude: -747127767, + }; + const params = { + metadata: { + 'x-my-header': 'k6test', + 'x-my-header-bin': new Uint8Array([1, 2, 3]), + }, + tags: { k6test: 'yes' }, + }; + const response = client.invoke('main.RouteGuide/GetFeature', req, params); +} +``` + +
diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/response.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/response.md new file mode 100644 index 000000000..09268176e --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/response.md @@ -0,0 +1,48 @@ +--- +title: 'Response' +head_title: 'gRPC.Response' +description: 'The response object of a gRPC request.' +weight: 30 +aliases: + - ../k6-experimental/grpc/response/ # docs/k6//javascript-api/k6-experimental/grpc/response/ +--- + +# Response + +| Name | Type | Description | +| ------------------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Response.status` | number | The response gRPC status code. Use the gRPC [status constants](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/constants) to check equality. | +| `Response.message` | object | The successful protobuf message, serialized to JSON. Will be `null` if `status !== grpc.StatusOK`. | +| `Response.headers` | object | Key-value pairs representing all the metadata headers returned by the gRPC server. | +| `Response.trailers` | object | Key-value pairs representing all the metadata trailers returned by the gRPC server. | +| `Response.error` | object | If `status !== grpc.StatusOK` then the error protobuf message, serialized to JSON; otherwise `null`. | + +### Example + +{{< code >}} + +```javascript +import grpc from 'k6/net/grpc'; +import { check, sleep } from 'k6'; + +const client = new grpc.Client(); +client.load(['definitions'], 'hello.proto'); + +export default () => { + client.connect('grpcbin.test.k6.io:9001', { + // plaintext: false + }); + + const data = { greeting: 'Bert' }; + const response = client.invoke('hello.HelloService/SayHello', data); + + check(response, { + 'status is OK': (r) => r && r.status === grpc.StatusOK, + }); + + client.close(); + sleep(1); +}; +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/_index.md new file mode 100644 index 000000000..dc692f71c --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/_index.md @@ -0,0 +1,208 @@ +--- +title: Stream +description: gRPC Streams +weight: 30 +aliases: + - ../k6-experimental/grpc/stream/ # docs/k6//javascript-api/k6-experimental/grpc/stream/ +--- + +# Stream + +Using a gRPC client creates a stream. The client should be connected to the server (`client.connect` called) before creating a stream. + +| Class/Method | Description | +| ----------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [Stream(client, url, [,params])](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream) | Using a connected [gRPC client](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client) and the method's URL creates a stream. Optionally, you can pass [`params` for additional configuration options](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/params) like tags, metadata, etc. | +| [Stream.write(message)](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/stream-write) | Writes a message to the stream. | +| [Stream.on(event, handler)](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/stream-on) | Sets up handler functions for various events on the gRPC stream. | +| [Stream.end()](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/stream-end) | Signals to the server that the client has finished sending. | +| [EventHandler](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/event-handler) | The handler function for various events on the gRPC stream. | +| [Metadata](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/message-metadata) | The metadata of a gRPC stream's message. | + +### Examples + +_A k6 script that sends several randomly chosen points from the pre-generated feature database with a variable delay in between. Prints the statistics when they are sent from the server._ + +{{< code >}} + +```javascript +import { Client, Stream } from 'k6/net/grpc'; +import { sleep } from 'k6'; + +const COORD_FACTOR = 1e7; + +const GRPC_ADDR = __ENV.GRPC_ADDR || '127.0.0.1:10000'; +const GRPC_PROTO_PATH = __ENV.GRPC_PROTO_PATH || '../../grpc_server/route_guide.proto'; + +const client = new Client(); +client.load([], GRPC_PROTO_PATH); + +// a sample DB of points +const DB = [ + { + location: { latitude: 407838351, longitude: -746143763 }, + name: 'Patriots Path, Mendham, NJ 07945, USA', + }, + { + location: { latitude: 408122808, longitude: -743999179 }, + name: '101 New Jersey 10, Whippany, NJ 07981, USA', + }, + { + location: { latitude: 413628156, longitude: -749015468 }, + name: 'U.S. 6, Shohola, PA 18458, USA', + }, + { + location: { latitude: 419999544, longitude: -740371136 }, + name: '5 Conners Road, Kingston, NY 12401, USA', + }, + { + location: { latitude: 414008389, longitude: -743951297 }, + name: 'Mid Hudson Psychiatric Center, New Hampton, NY 10958, USA', + }, + { + location: { latitude: 419611318, longitude: -746524769 }, + name: '287 Flugertown Road, Livingston Manor, NY 12758, USA', + }, + { + location: { latitude: 406109563, longitude: -742186778 }, + name: '4001 Tremley Point Road, Linden, NJ 07036, USA', + }, + { + location: { latitude: 416802456, longitude: -742370183 }, + name: '352 South Mountain Road, Wallkill, NY 12589, USA', + }, + { + location: { latitude: 412950425, longitude: -741077389 }, + name: 'Bailey Turn Road, Harriman, NY 10926, USA', + }, + { + location: { latitude: 412144655, longitude: -743949739 }, + name: '193-199 Wawayanda Road, Hewitt, NJ 07421, USA', + }, +]; + +export default () => { + if (__ITER == 0) { + client.connect(GRPC_ADDR, { plaintext: true }); + } + + const stream = new Stream(client, 'main.RouteGuide/RecordRoute'); + + stream.on('data', (stats) => { + console.log('Finished trip with', stats.pointCount, 'points'); + console.log('Passed', stats.featureCount, 'features'); + console.log('Travelled', stats.distance, 'meters'); + console.log('It took', stats.elapsedTime, 'seconds'); + }); + + stream.on('error', (err) => { + console.log('Stream Error: ' + JSON.stringify(err)); + }); + + stream.on('end', () => { + client.close(); + console.log('All done'); + }); + + // send 5 random items + for (let i = 0; i < 5; i++) { + const point = DB[Math.floor(Math.random() * DB.length)]; + pointSender(stream, point); + } + + // close the client stream + stream.end(); + + sleep(1); +}; + +const pointSender = (stream, point) => { + console.log( + 'Visiting point ' + + point.name + + ' ' + + point.location.latitude / COORD_FACTOR + + ', ' + + point.location.longitude / COORD_FACTOR + ); + + // send the location to the server + stream.write(point.location); + + sleep(0.5); +}; +``` + +{{< /code >}} + +_A k6 script that sends a rectangle message and results (features) are streamed back to the client._ + +{{< code >}} + +```javascript +import { Client, Stream } from 'k6/net/grpc'; +import { sleep } from 'k6'; + +const COORD_FACTOR = 1e7; + +const GRPC_ADDR = __ENV.GRPC_ADDR || '127.0.0.1:10000'; +const GRPC_PROTO_PATH = __ENV.GRPC_PROTO_PATH || '../../grpc_server/route_guide.proto'; + +const client = new Client(); + +client.load([], GRPC_PROTO_PATH); + +export default () => { + client.connect(GRPC_ADDR, { plaintext: true }); + + const stream = new Stream(client, 'main.FeatureExplorer/ListFeatures', null); + + stream.on('data', function (feature) { + console.log( + 'Found feature called "' + + feature.name + + '" at ' + + feature.location.latitude / COORD_FACTOR + + ', ' + + feature.location.longitude / COORD_FACTOR + ); + }); + + stream.on('end', function () { + // The server has finished sending + client.close(); + console.log('All done'); + }); + + stream.on('error', function (e) { + // An error has occurred and the stream has been closed. + console.log('Error: ' + JSON.stringify(e)); + }); + + // send a message to the server + stream.write({ + lo: { + latitude: 400000000, + longitude: -750000000, + }, + hi: { + latitude: 420000000, + longitude: -730000000, + }, + }); + + sleep(0.5); +}; +``` + +{{< /code >}} + +The preceding examples use a demo server, which you can run with the following command (Golang should be installed) in [k6 repository's root](https://github.com/grafana/k6): + +{{< code >}} + +```bash +$ go run -mod=mod examples/grpc_server/*.go +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/event-handler.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/event-handler.md new file mode 100644 index 000000000..bd5da9fe4 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/event-handler.md @@ -0,0 +1,49 @@ +--- +title: 'Event Handler' +description: 'The handler function for various events on the gRPC stream.' +weight: 50 +--- + +# Event Handler + +The function to call for various events on the gRPC stream. It is set up using the [`stream.on()`](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/stream-on) method. + +| Name | Type | Description | +| ---------- | ------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------- | +| `data` | object or [`Error`](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/stream-error) | It's either object with the data from server (a message) or an error object, in case of `error` event | +| `metadata` | [`Metadata`](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/message-metadata) | The object that represents the gRPC stream's message metadata. | + +### Example + +
+ +```javascript +import { Client, Stream } from 'k6/net/grpc'; +import { sleep } from 'k6'; + +const client = new Client(); +client.load([], '../../grpc_server/route_guide.proto'); + +export default () => { + if (__ITER == 0) { + client.connect('127.0.0.1:10000', { plaintext: true }); + } + + const stream = new Stream(client, 'main.RouteGuide/RecordRoute'); + + // sets up a handler for the data (server sends data) event + stream.on('data', (stats, metadata) => { + console.log('It took', stats.elapsedTime, 'seconds'); + console.log('This message has been received:', metadata.ts); + }); + + stream.on('end', function () { + // The server has finished sending + client.close(); + }); + + sleep(1); +}; +``` + +
diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/message-metadata.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/message-metadata.md new file mode 100644 index 000000000..0f89bbbd7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/message-metadata.md @@ -0,0 +1,48 @@ +--- +title: 'Metadata' +description: "The metadata of a gRPC stream's message." +weight: 60 +--- + +# Metadata + +The `Metadata` is an object that represents the gRPC stream's message. + +| Name | Type | Description | +| ------------- | ------ | -------------------------------------------------------------------------------------------- | +| `Metadata.ts` | number | Contains the timestamp of the original event. For example, when a message has been received. | + +### Example + +
+ +```javascript +import { Client, Stream } from 'k6/net/grpc'; +import { sleep } from 'k6'; + +const client = new Client(); +client.load([], '../../grpc_server/route_guide.proto'); + +export default () => { + if (__ITER == 0) { + client.connect('127.0.0.1:10000', { plaintext: true }); + } + + const stream = new Stream(client, 'main.RouteGuide/RecordRoute'); + + // sets up a handler for the data (server sends data) event + stream.on('data', (stats, metadata) => { + console.log('It took', stats.elapsedTime, 'seconds'); + console.log('This message has been received:', metadata.ts); + }); + + stream.on('end', function () { + // The server has finished sending + client.close(); + }); + + sleep(1); +}; +``` + +
diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-end.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-end.md new file mode 100644 index 000000000..8359ec2eb --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-end.md @@ -0,0 +1,49 @@ +--- +title: 'Stream.end()' +description: 'Signals to the server that the client has finished sending.' +weight: 40 +aliases: + - ../../k6-experimental/grpc/stream/stream-end/ # docs/k6//javascript-api/k6-experimental/grpc/stream/stream-end/ +--- + +# Stream.end() + +Signals to the server that the client has finished sending messages. + +### Example + +
+ +```javascript +import { Client, Stream } from 'k6/net/grpc'; +import { sleep } from 'k6'; + +const client = new Client(); +client.load([], '../../grpc_server/route_guide.proto'); + +export default () => { + if (__ITER == 0) { + client.connect('127.0.0.1:10000', { plaintext: true }); + } + + const stream = new Stream(client, 'main.RouteGuide/RecordRoute'); + + stream.on('data', (stats) => { + console.log('Finished trip with', stats.pointCount, 'points'); + console.log('Passed', stats.featureCount, 'features'); + console.log('Traveled', stats.distance, 'meters'); + console.log('It took', stats.elapsedTime, 'seconds'); + }); + + // send 2 items + stream.write({ latitude: 406109563, longitude: -742186778 }); + stream.write({ latitude: 416802456, longitude: -742370183 }); + + // send end-signal to the server + stream.end(); + + sleep(1); +}; +``` + +
diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-error.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-error.md new file mode 100644 index 000000000..685b2eb10 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-error.md @@ -0,0 +1,18 @@ +--- +title: 'Error' +head_title: 'gRPC.Error' +description: 'The error object of a gRPC stream.' +weight: 15 +aliases: + - ../../k6-experimental/grpc/stream/stream-error/ # docs/k6//javascript-api/k6-experimental/grpc/stream/stream-error/ +--- + +# Error + +The error object is the object that is passed to the `error` event handler function. + +| Name | Type | Description | +| --------------- | ------ | ---------------------------------------- | +| `Error.code` | number | A gRPC error code. | +| `Error.details` | array | A list of details attached to the error. | +| `Error.message` | string | An original error message. | diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-on.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-on.md new file mode 100644 index 000000000..e732d4016 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-on.md @@ -0,0 +1,69 @@ +--- +title: 'Stream.on()' +description: 'Set up handler functions for various events on the GRPC stream.' +weight: 10 +aliases: + - ../../k6-experimental/grpc/stream/stream-on/ # docs/k6//javascript-api/k6-experimental/grpc/stream/stream-on/ +--- + +# Stream.on() + +Set up handler functions for various events on the gRPC stream. + +| Parameter | Type | Description | +| --------- | ---------------------------------------------------------------------------------------------------------- | -------------------------------------------- | +| event | string | The event name to define a handler for. | +| handler | [`EventHandler`](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/event-handler) | The function to call when the event happens. | + +Possible events: + +| Event name | Description | +| ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| data | Emitted when the server sends data. | +| error | Emitted when an error occurs. In case of the error, an [`Error`](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/stream-error) object sends to the handler function. | +| end | Emitted when the server closes the incoming stream. | + +### Example + +
+ +```javascript +import { Client, Stream } from 'k6/net/grpc'; +import { sleep } from 'k6'; + +const client = new Client(); +client.load([], '../../grpc_server/route_guide.proto'); + +export default () => { + if (__ITER == 0) { + client.connect('127.0.0.1:10000', { plaintext: true }); + } + + const stream = new Stream(client, 'main.RouteGuide/RecordRoute'); + + // sets up a handler for the data (server sends data) event + stream.on('data', (stats) => { + console.log('Finished trip with', stats.pointCount, 'points'); + console.log('Passed', stats.featureCount, 'features'); + console.log('Traveled', stats.distance, 'meters'); + console.log('It took', stats.elapsedTime, 'seconds'); + }); + + // sets up a handler for the end event (stream closes) + stream.on('end', function () { + // The server has finished sending + client.close(); + console.log('All done'); + }); + + // sets up a handler for the error event (an error occurs) + stream.on('error', function (e) { + // An error has occurred and the stream has been closed. + console.log('Error: ' + JSON.stringify(e)); + }); + + sleep(1); +}; +``` + +
diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-write.md b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-write.md new file mode 100644 index 000000000..01dd0ffbe --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-net-grpc/stream/stream-write.md @@ -0,0 +1,45 @@ +--- +title: 'Stream.write()' +description: 'Writes a message to the stream.' +weight: 40 +aliases: + - ../../k6-experimental/grpc/stream/stream-write/ # docs/k6//javascript-api/k6-experimental/grpc/stream/stream-write/ +--- + +# Stream.write() + +Writes a message to the stream. The message is a canonical request object, as-per the [Protobuf JSON Mapping](https://grafana.com/docs/k6//using-k6/protocols/grpc/#protocol-buffers-json-mapping). + +### Example + +
+ +```javascript +import { Client, Stream } from 'k6/net/grpc'; +import { sleep } from 'k6'; + +const client = new Client(); +client.load([], '../../grpc_server/route_guide.proto'); + +export default () => { + if (__ITER == 0) { + client.connect('127.0.0.1:10000', { plaintext: true }); + } + + const stream = new Stream(client, 'main.RouteGuide/RecordRoute'); + + stream.on('data', (stats) => { + console.log('Finished trip with', stats.pointCount, 'points'); + }); + + // send an item + stream.write({ latitude: 406109563, longitude: -742186778 }); + + // send end-signal to the server + stream.end(); + + sleep(1); +}; +``` + +
diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-timers/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-timers/_index.md new file mode 100644 index 000000000..b614f44ba --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-timers/_index.md @@ -0,0 +1,29 @@ +--- +aliases: + - ./k6-experimental/timers # docs/k6//javascript-api/k6-experimental/timers +title: 'k6/timers' +description: 'k6 timers API' +weight: 11 +--- + +# k6/timers + +{{< docs/shared source="k6" lookup="javascript-api/k6-timers.md" version="" >}} + +## Example + +```javascript +export default function () { + const intervalId = setInterval(() => { + console.log('This runs every 200ms'); + }, 200); + + const timeoutId = setTimeout(() => { + console.log('This runs after 2s'); + + // clear the timeout and interval to exit k6 + clearInterval(intervalId); + clearTimeout(timeoutId); + }, 2000); +} +``` diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-ws/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/_index.md new file mode 100644 index 000000000..3299708f7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/_index.md @@ -0,0 +1,11 @@ +--- +title: 'k6/ws' +description: 'k6 WebSocket API' +weight: 12 +--- + +# k6/ws + +{{< docs/shared source="k6" lookup="ws-module.md" version="" >}} + +{{< docs/shared source="k6" lookup="javascript-api/k6-ws.md" version="" >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-ws/connect.md b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/connect.md new file mode 100644 index 000000000..be9761fed --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/connect.md @@ -0,0 +1,52 @@ +--- +title: 'connect( url, params, callback )' +description: 'Create a WebSocket connection, and provides a Socket client to interact with the service.' +description: 'Create a WebSocket connection, and provides a Socket client to interact with the service.' +weight: 10 +--- + +# connect( url, params, callback ) + +{{< docs/shared source="k6" lookup="ws-module.md" version="" >}} + +Initiate a WebSocket connection to a remote host. + +Calling connect will block the VU finalization until the WebSocket connection is closed. Instead of continuously looping the main function (`export default function() { ... }`) over an over, each VU will be halted listening to async events and executing their event handlers until the connection is closed. + +The following events can close the connection: + +- remote host close event. +- [Socket.close()](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-close). +- k6 VU interruption based on test configuration or CLI commands. + +| Parameter | Type | Description | +| --------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| url | string | Request URL (e.g. "ws://echo.websocket.org"). | +| params | object | [Params](https://grafana.com/docs/k6//javascript-api/k6-ws/params) object containing additional request parameters. | +| callback | function | The callback function that will be called when the WebSocket connection is initiated. A [Socket](https://grafana.com/docs/k6//javascript-api/k6-ws/socket) object will be passed to the function, and this object can be used to set up callbacks etc when things happen on the WebSocket connection | + +### Returns + +| Type | Description | +| ------------------------------------------------------------------------------------ | --------------------- | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) | HTTP Response object. | + +### Example + +{{< code >}} + +```javascript +import ws from 'k6/ws'; + +export default function () { + const url = 'ws://echo.websocket.org'; + const resp = ws.connect(url, null, function (socket) { + socket.on('open', function () { + console.log('WebSocket connection established!'); + socket.close(); + }); + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-ws/params.md b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/params.md new file mode 100644 index 000000000..659917264 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/params.md @@ -0,0 +1,45 @@ +--- +title: 'Params' +description: 'Used for setting various WebSocket request-specific parameters such as headers, tags, etc.' +description: 'Used for setting various WebSocket request-specific parameters such as headers, tags, etc.' +weight: 20 +--- + +# Params + +{{< docs/shared source="k6" lookup="ws-module.md" version="" >}} + +_Params_ is an object used by the WebSocket methods that generate WebSocket requests. _Params_ contains request-specific options like headers that should be inserted into the request. + +| Name | Type | Description | +| -------------------- | ------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `Params.compression` | string | Compression algorithm to be used by the WebSocket connection. The only supported algorithm currently is `deflate`. If the option is left unset or empty, it defaults to no compression. | +| `Params.jar` | [http.CookieJar](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar) | The cookie jar that will be used when making the initial HTTP request to establish the WebSocket connection. If empty, the [default VU cookie jar](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar) will be used. | +| `Params.headers` | object | Custom HTTP headers in key-value pairs that will be added to the initial HTTP request to establish the WebSocket connection. Keys are header names and values are header values. | +| `Params.tags` | object | [Custom metric tags](https://grafana.com/docs/k6//using-k6/tags-and-groups#user-defined-tags) in key-value pairs where the keys are names of tags and the values are tag values. The WebSocket connection will [generate metrics samples](https://grafana.com/docs/k6//javascript-api/k6-ws/socket#websocket-built-in-metrics) with these tags attached, allowing users to filter the results data or set [thresholds on sub-metrics](https://grafana.com/docs/k6//using-k6/thresholds#thresholds-on-tags). | + +### Example of custom metadata headers and tags + +_A k6 script that will make a WebSocket request with a custom header and tag results data with a specific tag_ + +{{< code >}} + +```javascript +import ws from 'k6/ws'; + +export default function () { + const url = 'ws://echo.websocket.org'; + const params = { + headers: { 'X-MyHeader': 'k6test' }, + tags: { k6test: 'yes' }, + }; + const res = ws.connect(url, params, function (socket) { + socket.on('open', function () { + console.log('WebSocket connection established!'); + socket.close(); + }); + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/_index.md new file mode 100644 index 000000000..8165fa067 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/_index.md @@ -0,0 +1,92 @@ +--- +title: 'Socket' +description: 'Socket is a WebSocket client to interact with a WebSocket connection.' +weight: 80 +weight: 80 +--- + +# Socket + +{{< docs/shared source="k6" lookup="ws-module.md" version="" >}} + +`Socket` is a WebSocket client to interact with a WebSocket connection. You can use it to listen various events happening on the WebSocket connection and send messages to the server. Additionally, you can use socket.setTimeout() and socket.setInterval() to execute code in the background, or repeatedly, while the WebSocket connection is open. + +| Method | Description | +| --------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| [Socket.close()](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-close) | Close the WebSocket connection. | +| [Socket.on(event, callback)](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-on) | Set up an event listener on the connection for any of the following events:
- open
- binaryMessage
- message
- ping
- pong
- close
- error. | +| [Socket.ping()](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-ping) | Send a ping. | +| [Socket.send(data)](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-send) | Send string data. | +| [Socket.sendBinary(data)](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-sendbinary) | Send binary data. | +| [Socket.setInterval(callback, interval)](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-setinterval) | Call a function repeatedly at certain intervals, while the connection is open. | +| [Socket.setTimeout(callback, period)](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-settimeout) | Call a function with a delay, if the connection is open. | + +### WebSocket built-in metrics + +`k6` automatically collects metrics when interacting with a WebSocket service through the `k6/ws` API. +Review the list in the [metrics reference](https://grafana.com/docs/k6//using-k6/metrics/reference#websockets). + +Check out the [Results output article](https://grafana.com/docs/k6//get-started/results-output) for more information about how to process the metric information. + +### Example + +{{< code >}} + +```javascript +import ws from 'k6/ws'; +import { check } from 'k6'; + +export default function () { + const url = 'ws://echo.websocket.org'; + const params = { tags: { my_tag: 'hello' } }; + + const response = ws.connect(url, params, function (socket) { + socket.on('open', function open() { + console.log('connected'); + socket.send(Date.now()); + + socket.setInterval(function timeout() { + socket.ping(); + console.log('Pinging every 1sec (setInterval test)'); + }, 1000); + }); + + socket.on('ping', function () { + console.log('PING!'); + }); + + socket.on('pong', function () { + console.log('PONG!'); + }); + + socket.on('pong', function () { + // Multiple event handlers on the same event + console.log('OTHER PONG!'); + }); + + socket.on('message', function (message) { + console.log(`Received message: ${message}`); + }); + + socket.on('close', function () { + console.log('disconnected'); + }); + + socket.on('error', function (e) { + if (e.error() != 'websocket: close sent') { + console.log('An unexpected error occured: ', e.error()); + } + }); + + socket.setTimeout(function () { + console.log('2 seconds passed, closing the socket'); + socket.close(); + }, 2000); + }); + + check(response, { 'status is 101': (r) => r && r.status === 101 }); +} +//VU execution won't be completely finished until the connection is closed. +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-close.md b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-close.md new file mode 100644 index 000000000..359e7eed1 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-close.md @@ -0,0 +1,33 @@ +--- +title: 'Socket.close([code])' +description: 'Close the WebSocket connection.' +--- + +# Socket.close([code]) + +{{< docs/shared source="k6" lookup="ws-module.md" version="" >}} + +Close the WebSocket connection. + +| Parameter | Type | Description | +| --------------- | ------ | -------------------------------------- | +| code (optional) | number | WebSocket status code. (default: 1001) | + +### Example + +{{< code >}} + +```javascript +import ws from 'k6/ws'; + +export default function () { + const url = 'ws://echo.websocket.org'; + const response = ws.connect(url, null, function (socket) { + socket.on('open', function () { + socket.close(); + }); + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-on.md b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-on.md new file mode 100644 index 000000000..a9a49e4ac --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-on.md @@ -0,0 +1,86 @@ +--- +title: 'Socket.on(event, callback)' +description: 'Set up callback functions for various events on the WebSocket connection.' +--- + +# Socket.on(event, callback) + +{{< docs/shared source="k6" lookup="ws-module.md" version="" >}} + +Set up callback functions for various events on the WebSocket connection. Multiple handlers can be defined for the same event. + +| Parameter | Type | Description | +| --------- | -------- | -------------------------------------------- | +| event | string | The event name to define a callback for. | +| callback | function | The function to call when the event happens. | + +| Event name | Description | +| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| open | Emitted when the connection is established | +| message | Emitted when a message is received from the server. | +| ping | Emitted when a ping is received from the server. The client will automatically send back a `pong`. | +| pong | Emitted when a pong is received from the server. | +| close | Emitted when the connection is closed by the client [Socket.close()](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-close) or when the server sends the `close` event with code status 1000 (normal closure). | +| error | Emitted when an error occurs. Non-normal closure errors will be forwarded. | + +### Example + +{{< code >}} + +```javascript +import ws from 'k6/ws'; +import { check } from 'k6'; + +export default function () { + const url = 'ws://echo.websocket.org'; + const params = { tags: { my_tag: 'hello' } }; + + const response = ws.connect(url, params, function (socket) { + socket.on('open', function open() { + console.log('connected'); + socket.send(Date.now()); + + socket.setInterval(function timeout() { + socket.ping(); + console.log('Pinging every 1sec (setInterval test)'); + }, 1000); + }); + + socket.on('ping', function () { + console.log('PING!'); + }); + + socket.on('pong', function () { + console.log('PONG!'); + }); + + socket.on('pong', function () { + // Multiple event handlers on the same event + console.log('OTHER PONG!'); + }); + + socket.on('message', function (message) { + console.log(`Received message: ${message}`); + }); + + socket.on('close', function () { + console.log('disconnected'); + }); + + socket.on('error', function (e) { + if (e.error() != 'websocket: close sent') { + console.log('An unexpected error occured: ', e.error()); + } + }); + + socket.setTimeout(function () { + console.log('2 seconds passed, closing the socket'); + socket.close(); + }, 2000); + }); + + check(response, { 'status is 101': (r) => r && r.status === 101 }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-ping.md b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-ping.md new file mode 100644 index 000000000..88a387bdd --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-ping.md @@ -0,0 +1,34 @@ +--- +title: 'Socket.ping()' +description: 'Send a ping. Ping messages can be used to verify that the remote endpoint is responsive.' +--- + +# Socket.ping() + +{{< docs/shared source="k6" lookup="ws-module.md" version="" >}} + +Send a ping. Ping messages can be used to verify that the remote endpoint is responsive. + +### Example + +{{< code >}} + +```javascript +import ws from 'k6/ws'; + +export default function () { + const url = 'ws://echo.websocket.org'; + const response = ws.connect(url, null, function (socket) { + socket.on('open', function () { + socket.on('pong', function () { + // As required by the spec, when the ping is received, the recipient must send back a pong. + console.log('connection is alive'); + }); + + socket.ping(); + }); + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-send.md b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-send.md new file mode 100644 index 000000000..c1f6bc07d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-send.md @@ -0,0 +1,37 @@ +--- +title: 'Socket.send(data)' +description: 'Send a data string through the connection.' +--- + +# Socket.send(data) + +{{< docs/shared source="k6" lookup="ws-module.md" version="" >}} + +Send a data string through the connection. +You can use `JSON.stringify` to convert a JSON or JavaScript values to a JSON string. + +| Parameter | Type | Description | +| --------- | ------ | ----------------- | +| data | string | The data to send. | + +### Example + +{{< code >}} + +```javascript +import ws from 'k6/ws'; + +export default function () { + const url = 'ws://echo.websocket.org'; + const response = ws.connect(url, null, function (socket) { + socket.on('open', function () { + socket.send('my-message'); + socket.send(JSON.stringify({ data: 'hola' })); + }); + }); +} +``` + +{{< /code >}} + +- See also [Socket.sendBinary(data)](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-sendbinary) diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-sendbinary.md b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-sendbinary.md new file mode 100644 index 000000000..819bde99a --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-sendbinary.md @@ -0,0 +1,41 @@ +--- +title: 'Socket.sendBinary(data)' +description: 'Send binary data through the connection.' +--- + +# Socket.sendBinary(data) + +{{< docs/shared source="k6" lookup="ws-module.md" version="" >}} + +Send binary data through the connection. + +| Parameter | Type | Description | +| --------- | ----------- | ----------------- | +| data | ArrayBuffer | The data to send. | + +### Example + +{{< code >}} + +```javascript +import ws from 'k6/ws'; + +const binFile = open('./file.pdf', 'b'); + +export default function () { + ws.connect('http://wshost/', function (socket) { + socket.on('open', function () { + socket.sendBinary(binFile); + }); + + socket.on('binaryMessage', function (msg) { + // msg is an ArrayBuffer, so we can wrap it in a typed array directly. + new Uint8Array(msg); + }); + }); +} +``` + +{{< /code >}} + +- See also [Socket.send(data)](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-send) diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-setinterval.md b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-setinterval.md new file mode 100644 index 000000000..f68bd1fb5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-setinterval.md @@ -0,0 +1,48 @@ +--- +title: 'Socket.setInterval(callback, interval)' +description: 'Call a function repeatedly, while the WebSocket connection is open.' +--- + +# Socket.setInterval(callback, interval) + +{{< docs/shared source="k6" lookup="ws-module.md" version="" >}} + +Call a function repeatedly, while the WebSocket connection is open. + +| Parameter | Type | Description | +| --------- | -------- | ----------------------------------------------------------- | +| callback | function | The function to call every `interval` milliseconds. | +| interval | number | The number of milliseconds between two calls to `callback`. | + +### Example + +{{< code >}} + +```javascript +import ws from 'k6/ws'; +import { check } from 'k6'; + +export default function () { + const url = 'ws://echo.websocket.org'; + const params = { tags: { my_tag: 'hello' } }; + + const res = ws.connect(url, params, function (socket) { + socket.on('open', function open() { + console.log('connected'); + + socket.setInterval(function timeout() { + socket.ping(); + console.log('Pinging every 1sec (setInterval test)'); + }, 1000); + }); + + socket.on('pong', function () { + console.log('PONG!'); + }); + }); + + check(res, { 'status is 101': (r) => r && r.status === 101 }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-settimeout.md b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-settimeout.md new file mode 100644 index 000000000..b23f2ebfd --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6-ws/socket/socket-settimeout.md @@ -0,0 +1,47 @@ +--- +title: 'Socket.setTimeout(callback, delay)' +description: 'Call a function at a later time, if the WebSocket connection is still open then.' +--- + +# Socket.setTimeout(callback, delay) + +{{< docs/shared source="k6" lookup="ws-module.md" version="" >}} + +Call a function at a later time, if the WebSocket connection is still open then. + +| Parameter | Type | Description | +| --------- | -------- | ---------------------------------------------- | +| callback | function | The function to call when `delay` has expired. | +| delay | number | The delay time, in milliseconds. | + +### Example + +{{< code >}} + +```javascript +import ws from 'k6/ws'; +import { sleep } from 'k6'; + +export default function () { + console.log('T0: Script started'); + const url = 'ws://echo.websocket.org'; + const response = ws.connect(url, null, function (socket) { + console.log('T0: Entered WebSockets run loop'); + socket.setTimeout(function () { + console.log('T0+1: This is printed'); + }, 1000); + socket.setTimeout(function () { + console.log('T0+2: Closing socket'); + socket.close(); + }, 2000); + socket.setTimeout(function () { + console.log('T0+3: This is not printed, because socket is closed'); + }, 3000); + }); + console.log('T0+2: Exited WebSockets run loop'); + sleep(2); + console.log('T0+4: Script finished'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6/_index.md b/docs/sources/k6/v0.55.x/javascript-api/k6/_index.md new file mode 100644 index 000000000..f46796d1b --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6/_index.md @@ -0,0 +1,9 @@ +--- +title: 'k6' +description: 'The k6 module contains k6-specific functionality.' +weight: 02 +--- + +# k6 + +{{< docs/shared source="k6" lookup="javascript-api/k6.md" version="" >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6/check.md b/docs/sources/k6/v0.55.x/javascript-api/k6/check.md new file mode 100644 index 000000000..5a155583d --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6/check.md @@ -0,0 +1,74 @@ +--- +title: 'check( val, sets, [tags] )' +description: 'Runs one or more checks on a value and generates a pass/fail result but does not throw errors or otherwise interrupt execution upon failure.' +description: 'Runs one or more checks on a value and generates a pass/fail result but does not throw errors or otherwise interrupt execution upon failure.' +--- + +# check( val, sets, [tags] ) + +Run checks on a value. A check is a test condition that can give a truthy or +falsy result. The `sets` parameter contains one or more checks, and the `check()` +function will return `false` if any of them fail. + +Note that checks are not _asserts_ in their traditional sense - a failed assertion +will throw an error, while a check will always return with a pass or a failure. +Failure conditions can then instead be controlled by thresholds, for more power and flexibility. + +| Parameter | Type | Description | +| --------------- | ------ | ---------------------------------------- | +| val | any | Value to test. | +| sets | object | Tests (checks) to run on the value. | +| tags (optional) | object | Extra tags to attach to metrics emitted. | + +### Returns + +| Type | Description | +| ------- | ------------------------------------------------------- | +| boolean | `true` if all checks have succeeded, `false` otherwise. | + +### Examples + +Using `check()` to verify that an HTTP response code was 200: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const res = http.get('https://httpbin.test.k6.io'); + check(res, { + 'response code was 200': (res) => res.status == 200, + }); +} +``` + +{{< /code >}} + +Using `check()` with a custom tag to verify that an HTTP response code was 200 and that body was 1234 bytes. The `checkOutput` can be used for any condition in your script logic: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check, fail } from 'k6'; + +export default function () { + const res = http.get('https://httpbin.test.k6.io'); + const checkOutput = check( + res, + { + 'response code was 200': (res) => res.status == 200, + 'body size was 1234 bytes': (res) => res.body.length == 1234, + }, + { myTag: "I'm a tag" } + ); + + if (!checkOutput) { + fail('unexpected response'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6/fail.md b/docs/sources/k6/v0.55.x/javascript-api/k6/fail.md new file mode 100644 index 000000000..a6773f9ed --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6/fail.md @@ -0,0 +1,43 @@ +--- +title: 'fail( [err] )' +description: 'Throws an error, failing and aborting the current VU script iteration immediately.' +description: 'Throws an error, failing and aborting the current VU script iteration immediately.' +--- + +# fail( [err] ) + +Immediately throw an error, aborting the current iteration. + +`fail()` does not abort the test, nor does it make the test exit with non-0 status. +If you are looking to fail the test by halting the execution, use [test.abort()](https://grafana.com/docs/k6//javascript-api/k6-execution/#test) instead + +`fail()` is a simple convenience wrapper on top of JavaScript's `throw()`, +because the latter cannot be used as `[expr] || throw`, which is a convenient way to write k6 test code. + +| Parameter | Type | Description | +| -------------- | ------ | ------------------------------------------ | +| err (optional) | string | Error message that gets printed to stderr. | + +### Example + +Aborting the current script iteration if a check fails: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check, fail } from 'k6'; + +export default function () { + const res = http.get('https://k6.io'); + if ( + !check(res, { + 'status code MUST be 200': (res) => res.status == 200, + }) + ) { + fail('status code was *not* 200'); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6/group.md b/docs/sources/k6/v0.55.x/javascript-api/k6/group.md new file mode 100644 index 000000000..adac288fc --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6/group.md @@ -0,0 +1,72 @@ +--- +title: 'group( name, fn )' +description: 'Runs code inside a group. Used to organize results in a test.' +--- + +# group( name, fn ) + +{{< admonition type="note" >}} + +For details about using the `check()` function with async values, refer to the jslib utils [check](https://grafana.com/docs/k6//javascript-api/jslib/utils/check/). + +{{< /admonition >}} + +Run code inside a group. Groups are used to organize results in a test. + +| Parameter | Type | Description | +| --------- | -------- | ------------------------------------------------------ | +| name | string | Name of the group. | +| fn | function | Group body - code to be executed in the group context. | + +### Returns + +| Type | Description | +| ---- | ------------------------- | +| any | The return value of _fn_. | + +## Limitations + +{{< admonition type="warning" >}} + +Avoid using `group` with async functions or asynchronous code. +If you do, k6 might apply tags in an unreliable or unintuitive way. + +{{< /admonition >}} + +If you start promise chains or use `await` within `group`, some code within the group will be waited for and tagged with the proper `group` tag, but others won't be. + +To avoid confusion, `async` functions are forbidden as `group()` arguments. That still lets users make and chain promises within a group, but doing so is unsupported and not recommended. + +For more information, refer to [k6 #2728](https://github.com/grafana/k6/issues/2728), which tracks possible solutions and provides detailed explanations. + +### Example + +{{< code >}} + +```javascript +import { group } from 'k6'; + +export default function () { + group('visit product listing page', function () { + // ... + }); + group('add several products to the shopping cart', function () { + // ... + }); + group('visit login page', function () { + // ... + }); + group('authenticate', function () { + // ... + }); + group('checkout process', function () { + // ... + }); +} +``` + +{{< /code >}} + +The above code will present the results separately depending on the group execution. + +Learn more on [Groups and Tags](https://grafana.com/docs/k6//using-k6/tags-and-groups). diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6/random-seed.md b/docs/sources/k6/v0.55.x/javascript-api/k6/random-seed.md new file mode 100644 index 000000000..74a4677fc --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6/random-seed.md @@ -0,0 +1,36 @@ +--- +title: 'randomSeed( int )' +description: 'Set seed to get a reproducible pseudo-random number using `Math.random`.' +description: 'Set seed to get a reproducible pseudo-random number using `Math.random`.' +--- + +# randomSeed( int ) + +Set seed to get a reproducible pseudo-random number using `Math.random`. + +| Parameter | Type | Description | +| --------- | ------- | --------------- | +| int | integer | The seed value. | + +### Example + +Use `randomSeed` to get the same random number in all the iterations. + +{{< code >}} + +```javascript +import { randomSeed } from 'k6'; + +export const options = { + vus: 10, + duration: '5s', +}; + +export default function () { + randomSeed(123456789); + const rnd = Math.random(); + console.log(rnd); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/javascript-api/k6/sleep.md b/docs/sources/k6/v0.55.x/javascript-api/k6/sleep.md new file mode 100644 index 000000000..f9ba44fa5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/javascript-api/k6/sleep.md @@ -0,0 +1,50 @@ +--- +title: 'sleep( t )' +description: 'Suspends VU execution for the specified duration.' +description: 'Suspends VU execution for the specified duration.' +--- + +# sleep( t ) + +Suspend VU execution for the specified duration. + +| Parameter | Type | Description | +| --------- | ------ | --------------------- | +| t | number | Duration, in seconds. | + +### Examples + +Fetching two different pages with a 0-30 second random sleep in between: + +{{< code >}} + +```javascript +import { sleep } from 'k6'; +import http from 'k6/http'; + +export default function () { + http.get('https://k6.io'); + sleep(Math.random() * 30); + http.get('https://k6.io/features'); +} +``` + +{{< /code >}} + +Using the [k6-utils](https://grafana.com/docs/k6//javascript-api/jslib/utils) library to specify a range between a minimum and maximum: + +{{< code >}} + +```javascript +import { sleep } from 'k6'; +import http from 'k6/http'; +import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; + +export default function () { + http.get('https://k6.io'); + sleep(randomIntBetween(20, 30)); + http.get('https://k6.io/features'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/k6-studio/_index.md b/docs/sources/k6/v0.55.x/k6-studio/_index.md new file mode 100644 index 000000000..cae0f85a5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/k6-studio/_index.md @@ -0,0 +1,20 @@ +--- +weight: 950 +title: k6 Studio +--- + +# k6 Studio + +{{< admonition type="note" >}} + +k6 Studio is currently in [public preview](https://grafana.com/docs/release-life-cycle/). Grafana Labs offers limited support, and breaking changes might occur prior to the application being made generally available. For bug reports or feedback, open an issue in the [k6 Studio GitHub repository](https://github.com/grafana/k6-studio/issues). + +{{< /admonition >}} + +k6 Studio is a desktop application that can help you: + +- Record a user flow from browser interactions and generate a HAR file. +- Generate and customize a k6 test script from a HAR file, including the ability to use rules to quickly iterate on the script creation process. +- Test and debug k6 scripts using a visual interface. Inspect the request and response details for any request in your script. + +Visit the [k6 Studio GitHub repository](https://github.com/grafana/k6-studio/) for more information and to download the application. diff --git a/docs/sources/k6/v0.55.x/misc/_index.md b/docs/sources/k6/v0.55.x/misc/_index.md new file mode 100644 index 000000000..43da0e488 --- /dev/null +++ b/docs/sources/k6/v0.55.x/misc/_index.md @@ -0,0 +1,10 @@ +--- +weight: 900 +title: Misc +--- + +# Misc + + + +{{< section >}} diff --git a/docs/sources/k6/v0.55.x/misc/archive.md b/docs/sources/k6/v0.55.x/misc/archive.md new file mode 100644 index 000000000..57b5431be --- /dev/null +++ b/docs/sources/k6/v0.55.x/misc/archive.md @@ -0,0 +1,247 @@ +--- +title: 'Archive Command' +description: 'A k6 archive is simply a tar file with all files needed to execute a k6 test.' +weight: 05 +--- + +# Archive Command + +## What is an archive? + +When the complexity of a k6 test goes beyond a single JS file it quickly becomes cumbersome to +find and bundle up all the dependencies (JS, [open()](https://grafana.com/docs/k6//javascript-api/init-context/open)'ed data files, TLS +client certs, etc.). k6 archives are a native way to bundle and distribute, or share, a test. + +A k6 archive is simply a [tar](https://en.wikipedia.org/wiki/Tar_%28computing%29) file with all +files needed to execute a k6 test. + +## How to create and run an archive + +Let's say that you normally execute a test using: + +{{< code >}} + +```bash +$ k6 run script.js +``` + +{{< /code >}} + +Now if you replace `run` with `archive` k6 will run the [init stage](https://grafana.com/docs/k6//using-k6/test-lifecycle) of +the code to determine which JS files are being imported and what data files are being +[`open()`](https://grafana.com/docs/k6//javascript-api/init-context/open)'ed and bundles all of the files up +into a tar file: + +{{< code >}} + +```bash +$ k6 archive script.js +``` + +{{< /code >}} + +This would produce a tar file on disk called `archive.tar` (you can change that by setting +`-O filename.tar`). It's also easy to run an archive, as `k6 run` is compatible with archive +files you can execute: + +> ### ⚠️ Overriding options +> +> As always you can override options using CLI flags or environment variables when +> running an archive. + +{{< code >}} + +```bash +$ k6 run archive.tar +``` + +{{< /code >}} + +## Use cases + +Archive files have a variety of use cases, but they all share the common need to bundle +of a test's files into a single file for easy distribution. + +### Sharing a test + +By bundling up a test into an archive it's easy to share the test with your teammates by +simply storing or sending a single tar file. As we saw in the previous section, your teammates +can execute the archive by running `k6 run archive.tar`. + +### Preparing tests for CI + +If you have a complex CI pipeline and your load tests are separated from your application +code, you could store k6 archives as build artifacts whenever the load test source code +is changed, and then pull in those k6 archives from the artifacts storage for test execution +as needed. + +### k6 Cloud Execution + +k6 offers a commercial service for running large scale and geographically +distributed load tests on managed cloud infrastructure. Cloud executed tests are triggered +from the k6 command-line via the `k6 cloud run script.js` command (similar to `k6 run`) which will +trigger an implicit creation of a k6 archive that is uploaded and distributed to k6 cloud +load generators for execution. + +#### Uploading an archive to k6 Cloud + +Under certain scenarios, such as scheduled executions, users might want to upload an archive to k6 Cloud without triggering +a test execution in the process. + +To cater to this use case, the `k6 cloud upload` command was introduced. This command allows users to upload an archive to k6 Cloud +without triggering a test execution. The command syntax is as follows: + +{{< code >}} + +```bash +k6 cloud upload archive.tar +``` + +{{< /code >}} + +### Distributed Execution + +[k6-operator](https://github.com/grafana/k6-operator#multi-file-tests) can distribute a k6 test across a Kubernetes cluster. + +When a k6 test has multiple files, you can use the archive functionality to bundle the k6 test in a single _archived_ file and pass this file to run the test. + +## Contents of an archive file + +An archive contains the original source of the JS code, any [`open()`](https://grafana.com/docs/k6//javascript-api/init-context/open)'ed +data files, [SSL/TLS client certificates](https://grafana.com/docs/k6//using-k6/protocols/ssl-tls/ssl-tls-client-certificates) as well as a +`metadata.json` with all the options (a cascading of the options set on the [CLI](https://grafana.com/docs/k6//using-k6/k6-options), +via [Environment variables](https://grafana.com/docs/k6//using-k6/k6-options) and [in-script options](https://grafana.com/docs/k6//using-k6/k6-options) +(`export let options = {...}`)). + +Let's create an archive from the following sample test. Here is the layout in the filesystem +of the files: + +{{< code >}} + +```bash +/home/johndoe/tests/api-test $ tree +. +├── utils +| └-- common.js +├── endpoints +| ├── login.js +| └-- search.js +├── node_modules +| └-- somelib +| └-- lib.js +├── data +| └-- users.json +└-- script.js +``` + +{{< /code >}} + +Now, if the current working directory is `/home/johndoe/tests/api-test/` and we run +`k6 archive script.js` we'd get a tar file called `archive.tar` (you can change the name of the +file using `-O filename.tar`). The contents of the archive file would look like something like +this: + +{{< code >}} + +```bash +├-- data +├-- files +| └-- home +| └-- nobody <-- the username has been anonymized (see section further down) +| └-- tests +| └-- api-test +| └-- data +| └-- users.json +├-- metadata.json +└-- scripts + └-- home + └-- nobody <-- the username has been anonymized (see section further down) + └-- tests + └-- api-test + ├-- script.js + ├-- utils + | └-- common.js + ├-- endpoints + | ├-- login.js + | └-- search.js + └-- node_modules + └-- somelib + └-- lib.js +``` + +{{< /code >}} + +Breaking down the file structure we get: + +**data** contains the source code of the main JS file (`script.js` in this example). + +**files** contains the full original directory tree of all [`open()`](https://grafana.com/docs/k6//javascript-api/init-context/open)'ed data files. + +**metadata.json** The resolved "default" options for this test based on [CLI flags](https://grafana.com/docs/k6//using-k6/k6-options), +[Environment variables](https://grafana.com/docs/k6//using-k6/k6-options) and [in-script options](https://grafana.com/docs/k6//using-k6/k6-options). + +**_scripts_** contains the full original directory tree of all `import`'ed JS dependencies. + +{{< code >}} + +```json +{ + "type": "js", + "options": { + "paused": null, + "vus": null, + "duration": null, + "iterations": null, + "stages": null, + "setupTimeout": null, + "teardownTimeout": null, + "rps": null, + "maxRedirects": null, + "userAgent": null, + "batch": null, + "batchPerHost": null, + "httpDebug": null, + "insecureSkipTLSVerify": null, + "tlsCipherSuites": null, + "tlsVersion": { + "min": "", + "max": "" + }, + "tlsAuth": null, + "throw": null, + "thresholds": null, + "blacklistIPs": null, + "hosts": null, + "noConnectionReuse": null, + "ext": null, + "summaryTrendStats": null, + "systemTags": [ + "url", + "name", + "check", + "error", + "tls_version", + "method", + "subproto", + "status", + "group", + "proto" + ], + "tags": null + }, + "filename": "/home/johndoe/tests/api-test/script.js", + "pwd": "/home/johndoe/tests/api-test/", + "env": {} +} +``` + +{{< /code >}} + +## What an archive file does not contain + +We try to be cautious with what we include in an archive file. Some things we do to that end: + +- We anonymize the username found in any path to JS and data file dependencies +- By default, k6 omits environment variables from the system in the archive. + However, if you use the flag `--include-system-env-vars`, k6 includes the environment variables in the archive (along with any variables passed with `--env`). + To avoid including system environment variables in the archive, you can use the k6 archive flag `--exclude-env-vars`. diff --git a/docs/sources/k6/v0.55.x/misc/glossary.md b/docs/sources/k6/v0.55.x/misc/glossary.md new file mode 100644 index 000000000..87a38eb4f --- /dev/null +++ b/docs/sources/k6/v0.55.x/misc/glossary.md @@ -0,0 +1,247 @@ +--- +title: Glossary +description: 'A list of technical terms commonly used when discussing k6, with definitions.' +weight: 07 +--- + +# Glossary + +What we talk about when we talk about k6. + +In discussion about k6, some terms have a precise, technical meaning. +If a certain term in these docs confuses you, consult this list for a definition. + + + +{{< column-list >}} +- [Application performance monitoring](#application-performance-monitoring) +- [Concurrent sessions](#concurrent-sessions) +- [Checks](#checks) +- [Custom resource](#custom-resource) +- [Data correlation](#data-correlation) +- [Data parameterization](#data-parameterization) +- [Dynamic data](#dynamic-data) +- [Endurance testing](#endurance-testing) +- [Environment variables](#environment-variables) +- [Execution segment](#execution-segment) +- [Sobek](#sobek) +- [Graceful stop](#graceful-stop) +- [Happy path](#happy-path) +- [HTTP archive](#http-archive) +- [Iteration](#iteration) +- [k6 Cloud](#k6-cloud) +- [k6 options](#k6-options) +- [Kubernetes](#kubernetes) +- [Load test](#load-test) +- [Load zone](#load-zone) +- [Lifecycle function](#lifecycle-function) +- [Metric](#metric) +- [Metric sample](#metric-sample) +- [Operator pattern](#operator-pattern) +- [Parallelism](#parallelism) +- [Reliability](#reliability) +- [Requests per second](#requests-per-second) +- [Saturation](#saturation) +- [Scenario](#scenario) +- [Scenario executor](#scenario-executor) +- [Smoke test](#smoke-test) +- [Soak test](#soak-test) +- [Stability](#stability) +- [Stress test](#stress-test) +- [System under test](#system-under-test) +- [Test run](#test-run) +- [Test concurrency](#test-concurrency) +- [Test duration](#test-duration) +- [Test script](#test-script) +- [Threshold](#threshold) +- [Throughput](#throughput) +- [Virtual user](#virtual-user) +- [YAML](#yaml) +{{< /column-list >}} + + + + + +## Application performance monitoring + +_(Or APM)_. The practice of monitoring the performance, availability, and reliability of a system. + +## Concurrent sessions + +The number of simultaneous VU requests in a test run. + +## Checks + +Checks are true/false conditions that evaluate the content of some value in the JavaScript runtime.

[Checks reference](https://grafana.com/docs/k6//using-k6/checks) + +## Custom resource + +An extension to the [Kubernetes](#kubernetes) API.

[Kubernetes reference](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/) + +## Data correlation + +The process of taking [dynamic data](#dynamic-data) received from the system under test and reusing the data in a subsequent request. +
+
+[Correlation and dynamic data example](https://grafana.com/docs/k6//examples/correlation-and-dynamic-data),[Correlation in testing APIs](https://grafana.com/docs/k6//testing-guides/api-load-testing#correlation-and-data-parameterization) + +## Data parameterization + +The process of turning test values into reusable parameters, e.g. through variables and shared arrays.

[Data parameterization examples](https://grafana.com/docs/k6//examples/data-parameterization) + +## Dynamic data + +Data that might change or will change during test runs or across test runs. Common examples are order IDs, session tokens, or timestamps.

[Correlation and dynamic data example](https://grafana.com/docs/k6//examples/correlation-and-dynamic-data) + +## Endurance testing + +A synonym for [soak testing](#soak-test). + +## Environment variables + +User-definable values which may be utilized by the operating system and other programs.

[Using environment variables](https://grafana.com/docs/k6//using-k6/environment-variables) + +## Execution segment + +A partition, or fractional portion, of an overall [test run](#test-run).

[The execution-segment options](https://grafana.com/docs/k6//using-k6/k6-options/reference#execution-segment) + +## Sobek + +A JavaScript engine written in Go. k6 binaries are embedded with Sobek, enabling test scripting in JavaScript.

[Sobek repository](https://github.com/grafana/sobek) fork of [goja](https://github.com/dop251/goja). + +## Graceful stop + +A period that lets VUs finish an iteration at the end of a load test. Graceful stops prevent abrupt halts in execution.

[Graceful stop reference](https://grafana.com/docs/k6//using-k6/scenarios/concepts/graceful-stop) + +## Happy path + +The default system behavior that happens when a known input produces an expected output. A common mistake in performance testing happens when scripts account only for the best case (in other words, the happy path). Most load tests try to discover system errors, so test scripts should include exception handling.

[Happy path (Wikipedia)](https://en.wikipedia.org/wiki/Happy_path) + +## HTTP archive + +_(Or HAR file)_. A file containing logs of browser interactions with the system under test. All included transactions are stored as JSON-formatted text. You can use these archives to generate test scripts (for example, with the har-to-k6 Converter).

[HAR 1.2 Specification](http://www.softwareishard.com/blog/har-12-spec/),[HAR converter](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings/using-the-har-converter) + +## Iteration + +A single run in the execution of the `default function`, or scenario `exec` function. You can set iterations across all VUs, or per VU.

The [test life cycle](https://grafana.com/docs/k6//using-k6/test-lifecycle) document breaks down each stage of a k6 script, including iterations in VU code. + +## JSON + +An open standard, human-readable data-serialization format originally derived from JavaScript. + +## k6 Cloud + +The proper name for the entire cloud product, comprising both k6 Cloud Execution and k6 Cloud Test Results.

[k6 Cloud docs](https://grafana.com/docs/grafana-cloud/testing/k6/) + +## k6 options + +Values that configure a k6 test run. You can set options with command-line flags, environment variables, and in the script.

[k6 Options](https://grafana.com/docs/k6//using-k6/k6-options) + +## Kubernetes + +An open-source system for automating the deployment, scaling, and management of containerized applications.

[Kubernetes website](https://kubernetes.io/) + +## Load test + +A test that assesses the performance of the system under test in terms of concurrent users or requests per second.

[Load Testing](https://grafana.com/docs/k6//testing-guides/test-types/load-testing) + +## Load zone + +The geographical instance from which a test runs.

[Private load zones](https://grafana.com/docs/grafana-cloud/testing/k6/author-run/private-load-zone-v2/),[Declare load zones from the CLI](https://grafana.com/docs/grafana-cloud/testing/k6/author-run/use-load-zones/) + +## Lifecycle function + +A function called in a specific sequence in the k6 runtime. The most important lifecycle function is the default function, which runs the VU code.

[Test lifecycle](https://grafana.com/docs/k6//using-k6/test-lifecycle) + +## Metric + +A measure of how the system performs during a test run. `http_req_duration` is an example of a built-in k6 metric. Besides built-ins, you can also create custom metrics.

[Metrics](https://grafana.com/docs/k6//using-k6/metrics) + +## Metric sample + +A single value for a metric in a test run. For example, the value of `http_req_duration` from a single VU request. + +## Operator pattern + +Extends [Kubernetes](#kubernetes), enabling cluster management of [custom resources](#custom-resource).

[Kubernetes reference](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/) + +## Parallelism + +The simultaneous execution of multiple tasks by dividing a problem into smaller independent parts. + +## Reliability + +The probability that a system under test performs as intended. + +## Requests per second + +The rate at which a test sends requests to the system under test. + +## Saturation + +A condition when a system's reaches full resource utilization and can handle no additional request. + +## Scenario + +An object in a test script that makes in-depth configurations to how VUs and iterations are scheduled. With scenarios, your test runs can model diverse traffic patterns.

[Scenarios reference](https://grafana.com/docs/k6//using-k6/scenarios) + +## Scenario executor + +An property of a [scenario](#scenario) that configures VU behavior. + +You can use executors to configure whether to designate iterations as shared between VUs or to run per VU, or to configure or whether the VU concurrency is constant or changing.

[Executor reference](https://grafana.com/docs/k6//using-k6/scenarios/executors) + +## Smoke test + +A regular load test configured for minimum load. Smoke tests verify that the script has no errors and that the system under test can handle a minimal amount of load.

[Smoke Testing](https://grafana.com/docs/k6//testing-guides/test-types/smoke-testing) + +## Soak test + +A test that tries to uncover performance and reliability issues stemming from a system being under pressure for an extended duration.

[Soak Testing](https://grafana.com/docs/k6//testing-guides/test-types/soak-testing) + +## Stability + +A system under test’s ability to withstand failures and errors. + +## Stress test + +A test that assess the availability and stability of the system under heavy load.

[Stress Testing](https://grafana.com/docs/k6//testing-guides/test-types/stress-testing) + +## System under test + +The software that the load test tests. This could be an API, a website, infrastructure, or any combination of these. + +## Test run + +An individual execution of a test script over all configured iterations.

[Running k6](https://grafana.com/docs/k6//get-started/running-k6) + +## Test concurrency + +In k6 Cloud, the number of tests running at the same time. + +## Test duration + +The length of time that a test runs. When duration is set as an option, VU code runs for as many iterations as possible in the length of time specified.

[Duration option reference](https://grafana.com/docs/k6//using-k6/k6-options/reference#duration) + +## Test script + +The actual code that defines how the test behaves and what requests it makes, along with all (or at least most) configuration needed to run the test.

[Single Request example](https://grafana.com/docs/k6//examples/single-request). + +## Threshold + +A pass/fail criteria that evaluates whether a metric reaches a certain value. Testers often use thresholds to codify SLOs.

[Threshold reference](https://grafana.com/docs/k6//using-k6/thresholds) + +## Throughput + +The rate of successful message delivery. In k6, throughput is measured in requests per second. + +## Virtual user + +_(Or VU)_. The simulated users that run separate and concurrent iterations of your test script.

[The VU option](https://grafana.com/docs/k6//using-k6/k6-options/reference#vus) + +## YAML + +Rhymes with "camel," provides a human-readable data-serialization format commonly used for configuration files. + +
diff --git a/docs/sources/k6/v0.55.x/misc/integrations.md b/docs/sources/k6/v0.55.x/misc/integrations.md new file mode 100644 index 000000000..a947d8a45 --- /dev/null +++ b/docs/sources/k6/v0.55.x/misc/integrations.md @@ -0,0 +1,88 @@ +--- +title: Integrations & Tools +weight: 01 +--- + +# Integrations & Tools + +## Test authoring + +Codeless tools to speed up the test creation. + +- [Test Builder](https://grafana.com/docs/k6//using-k6/test-authoring/test-builder) - Inspired by the Postman API Builder. Codeless UI tool to generate a k6 test quickly. +- [Browser Recorder](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings/using-the-browser-recorder) - Record a user journey to create your k6 test. + +## IDE extensions + +Code k6 scripts in your IDE of choice. Empower your development workflow with IDE extensions. + +- [Visual Studio Code Extension](https://marketplace.visualstudio.com/items?itemName=k6.k6) - Run k6 tests from VS Code. +- [IntelliJ IDEA](https://plugins.jetbrains.com/plugin/16141-k6) - Run k6 tests from IntelliJ IDEs. +- [IntelliSense](https://grafana.com/docs/k6//set-up/configure-k6-intellisense) - Get code autocompletion and in-context documentation. + +## Converters + +Generate a k6 script quickly from an existing file. + +- [HAR-to-k6](https://github.com/k6io/har-to-k6) - Convert a HAR file to a k6 script. +- [Postman-to-k6](https://github.com/apideck-libraries/postman-to-k6) - Convert a Postman collection to a k6 script. +- [OpenAPI generator](https://k6.io/blog/load-testing-your-api-with-swagger-openapi-and-k6) - Convert a Swagger/OpenAPI specification to a k6 script. + +## Result store and visualization + +k6 can output test results to various formats and 3rd-party services. + +{{< column-list >}} +- [Amazon CloudWatch](https://grafana.com/docs/k6//results-output/real-time/amazon-cloudwatch) +- [Apache Kafka](https://grafana.com/docs/k6//results-output/real-time/apache-kafka) +- [CSV](https://grafana.com/docs/k6//results-output/real-time/csv) +- [Datadog](https://grafana.com/docs/k6//results-output/real-time/datadog) +- [Dynatrace](https://grafana.com/docs/k6//results-output/real-time/dynatrace) +- [Elasticsearch](https://grafana.com/docs/k6//results-output/real-time/elasticsearch) +- [Grafana Cloud k6](https://grafana.com/docs/k6//results-output/real-time/cloud) +- [Grafana Cloud Prometheus](https://grafana.com/docs/k6//results-output/real-time/grafana-cloud-prometheus) +- [Grafana Dashboards](https://grafana.com/docs/k6//results-output/grafana-dashboards) +- [InfluxDB](https://grafana.com/docs/k6//results-output/real-time/influxdb) +- [JSON file](https://grafana.com/docs/k6//results-output/real-time/json) +- [Netdata](https://grafana.com/docs/k6//results-output/real-time/netdata) +- [New Relic](https://grafana.com/docs/k6//results-output/real-time/new-relic) +- [Prometheus](https://grafana.com/docs/k6//results-output/real-time/prometheus-remote-write) +- [StatsD](https://grafana.com/docs/k6//results-output/real-time/statsd) +- [TimescaleDB](https://grafana.com/docs/k6//results-output/real-time/timescaledb) +{{< /column-list >}} + +## Continuous Integration and Continuous Delivery + +By automating load testing with your CI / CD tools, you can quickly see when a code change has introduced a performance regression. + +{{< column-list >}} +- [AWS CodeBuild](https://k6.io/blog/integrating-k6-with-aws-codebuild/) +- [Azure Pipelines](https://k6.io/blog/integrating-load-testing-with-azure-pipelines/) +- [Bamboo](https://k6.io/blog/integrating-k6-with-bamboo/) +- [Buddy](https://k6.io/blog/integrating-k6-with-buddy-devops/) +- [CircleCI](https://k6.io/blog/integrating-load-testing-with-circleci/) +- [Flagger](https://docs.flagger.app/usage/webhooks#k6-load-tester) +- [GitHub Actions](https://k6.io/blog/load-testing-using-github-actions/) +- [GitLab](https://k6.io/blog/integrating-load-testing-with-gitlab/) +- [Google Cloud Build](https://k6.io/blog/integrating-k6-with-google-cloud-build/) +- [Jenkins](https://k6.io/blog/integrating-load-testing-with-jenkins/) +- [Keptn](https://k6.io/blog/performance-testing-in-keptn-using-k6/) +- [TeamCity](https://k6.io/blog/load-testing-using-teamcity-and-k6/) +{{< /column-list >}} + +## Chaos engineering + +- [Steadybit](https://k6.io/blog/chaos-engineering-with-k6-and-steadybit) - Using k6 and Steadybit for chaos engineering. +- [xk6-disruptor](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor) - A k6 extension for injecting faults into k6 tests. +- [ChaosToolkit](http://chaostoolkit.org/drivers/k6/) - A collection of k6 actions and probes. +- [LitmusChaos](https://litmuschaos.github.io/litmus/experiments/categories/load/k6-loadgen/) - Simulate load generation to the target application as a part of chaos testing on Kubernetes using Litmus. + +## Test management + +- [Azure Test Plan](https://medium.com/microsoftazure/load-testing-with-azure-devops-and-k6-839be039b68a) - k6 load testing with Azure DevOps. +- [TestRail](https://dev.to/kwidera/introducing-testrail-in-you-k6-tests-eck) - Introducing TestRail in your k6 tests. +- [Testkube](https://kubeshop.github.io/testkube/test-types/executor-k6) - Load testing with Testkube. +- [Tracetest](https://docs.tracetest.io/tools-and-integrations/integrations/k6) - Trace-based testing with Tracetest. +- [Xray](https://docs.getxray.app/display/XRAYCLOUD/Performance+and+load+testing+with+k6) - Using Xray to validate test results in JIRA. + +[Reach out to us](mailto:support@k6.io) if you have a tool or add-on that works well with k6, and we will list it here! diff --git a/docs/sources/k6/v0.55.x/misc/k6-rest-api.md b/docs/sources/k6/v0.55.x/misc/k6-rest-api.md new file mode 100644 index 000000000..7575bf43a --- /dev/null +++ b/docs/sources/k6/v0.55.x/misc/k6-rest-api.md @@ -0,0 +1,612 @@ +--- +title: 'k6 REST API' +description: 'With this API you can see and control different execution aspects like +number of VUs, pause or resume the test, list groups, set and get the +setup data and more.' +_build: + list: false +weight: 04 +--- + +# k6 REST API + +When k6 starts, it spins up an HTTP server with a REST API that can be used to control some +parameters of the test execution. By default, that server listens on `localhost:6565`, but +that can be modified by the `--address` CLI flag. + +With this API you can see and control different execution aspects like number of VUs, Max +VUs, pause or resume the test, list groups, set and get the setup data and so on. + +You can also find practical usage examples in +[this blog post](https://k6.io/blog/how-to-control-a-live-k6-test). + +## Get Status + +**GET** `http://localhost:6565/v1/status` + +{{< code >}} + +```bash +curl -X GET \ + http://localhost:6565/v1/status \ + -H 'Content-Type: application/json' +``` + +```json +{ + "data": { + "attributes": { + "paused": false, + "running": true, + "tainted": false, + "vus": 1 + }, + "id": "default", + "type": "status" + } +} +``` + +{{< /code >}} + +## Update Status + +**PATCH** `http://localhost:6565/v1/status` + +{{< code >}} + +```bash +curl -X PATCH \ + http://localhost:6565/v1/status \ + -H 'Content-Type: application/json' \ + -d '{ + "data": { + "attributes": { + "paused": true, + "vus": 1, + }, + "id": "default", + "type": "status" + } +}' +``` + +```json +{ + "data": { + "type": "status", + "id": "default", + "attributes": { + "paused": true, + "vus": 1, + "running": true, + "tainted": false + } + } +} +``` + +{{< /code >}} + +This endpoint lets you pause/resume a running test and set the number of `vus` and `vus-max` +during the test. + +## List Metrics + +**GET** `http://localhost:6565/v1/metrics` + +{{< code >}} + +```bash +curl -X GET \ + http://localhost:6565/v1/metrics \ + -H 'Content-Type: application/json' +``` + +```json +{ + "data": [ + { + "type": "metrics", + "id": "http_req_duration", + "attributes": { + "type": "trend", + "contains": "time", + "tainted": null, + "sample": { + "avg": 122.529465, + "max": 179.098624, + "med": 115.83006, + "min": 107.743524, + "p(90)": 136.9331272, + "p(95)": 158.01587559999996 + } + } + }, + { + "type": "metrics", + "id": "http_req_connecting", + "attributes": { + "type": "trend", + "contains": "time", + "tainted": null, + "sample": { + "avg": 11.2357072, + "max": 112.357072, + "med": 0, + "min": 0, + "p(90)": 11.235707199999961, + "p(95)": 61.796389599999884 + } + } + }, + { + "type": "metrics", + "id": "http_req_sending", + "attributes": { + "type": "trend", + "contains": "time", + "tainted": null, + "sample": { + "avg": 0.027994200000000004, + "max": 0.106594, + "med": 0.0192965, + "min": 0.017486, + "p(90)": 0.03165189999999997, + "p(95)": 0.0691229499999999 + } + } + }, + { + "type": "metrics", + "id": "http_req_waiting", + "attributes": { + "type": "trend", + "contains": "time", + "tainted": null, + "sample": { + "avg": 122.33937080000001, + "max": 179.021285, + "med": 115.74006299999999, + "min": 107.650352, + "p(90)": 136.8561833, + "p(95)": 157.93873414999996 + } + } + }, + { + "type": "metrics", + "id": "data_received", + "attributes": { + "type": "counter", + "contains": "data", + "tainted": null, + "sample": { + "count": 13830, + "rate": 1119.9222882571698 + } + } + }, + { + "type": "metrics", + "id": "http_req_blocked", + "attributes": { + "type": "trend", + "contains": "time", + "tainted": null, + "sample": { + "avg": 11.364957999999998, + "max": 113.611988, + "med": 0.004173, + "min": 0.003867, + "p(90)": 11.365557499999959, + "p(95)": 62.48877274999988 + } + } + }, + { + "type": "metrics", + "id": "http_req_receiving", + "attributes": { + "type": "trend", + "contains": "time", + "tainted": null, + "sample": { + "avg": 0.16209999999999997, + "max": 0.757392, + "med": 0.078622, + "min": 0.057306, + "p(90)": 0.2315264999999998, + "p(95)": 0.4944592499999994 + } + } + }, + { + "type": "metrics", + "id": "http_reqs", + "attributes": { + "type": "counter", + "contains": "default", + "tainted": null, + "sample": { + "count": 10, + "rate": 0.8097775041628127 + } + } + }, + { + "type": "metrics", + "id": "http_req_tls_handshaking", + "attributes": { + "type": "trend", + "contains": "time", + "tainted": null, + "sample": { + "avg": 0, + "max": 0, + "med": 0, + "min": 0, + "p(90)": 0, + "p(95)": 0 + } + } + }, + { + "type": "metrics", + "id": "data_sent", + "attributes": { + "type": "counter", + "contains": "data", + "tainted": null, + "sample": { + "count": 860, + "rate": 69.64086535800189 + } + } + }, + { + "type": "metrics", + "id": "iteration_duration", + "attributes": { + "type": "trend", + "contains": "time", + "tainted": null, + "sample": { + "avg": 1134.89821, + "max": 1238.377413, + "med": 1118.223518, + "min": 1108.405498, + "p(90)": 1185.348477, + "p(95)": 1211.8629449999999 + } + } + }, + { + "type": "metrics", + "id": "iterations", + "attributes": { + "type": "counter", + "contains": "default", + "tainted": null, + "sample": { + "count": 10, + "rate": 0.8097775041628127 + } + } + }, + { + "type": "metrics", + "id": "vus", + "attributes": { + "type": "gauge", + "contains": "default", + "tainted": null, + "sample": { + "value": 1 + } + } + } + ] +} +``` + +{{< /code >}} + +This endpoint will give you all the metrics in the current time. You can see more details on all +metrics available and how to create new ones in [Metrics](https://grafana.com/docs/k6//using-k6/metrics). + +## Get Metric + +**GET** `http://localhost:6565/v1/metrics/id` + +{{< code >}} + +```bash +curl -X GET \ + http://localhost:6565/v1/metrics/http_req_receiving \ + -H 'Content-Type: application/json' +``` + +```json +{ + "data": { + "attributes": { + "contains": "time", + "sample": { + "avg": 0.12641856097560983, + "max": 1.1397, + "med": 0.074412, + "min": 0.057858, + "p(90)": 0.208553, + "p(95)": 0.218015 + }, + "tainted": null, + "type": "trend" + }, + "id": "http_req_receiving", + "type": "metrics" + } +} +``` + +{{< /code >}} + +This endpoint will give you details for the given metric in the current time. + +You can see more on all metrics available and how to create new ones in [Metrics](https://grafana.com/docs/k6//using-k6/metrics). + +## List Groups + +**GET** `http://localhost:6565/v1/groups` + +{{< code >}} + +```bash +curl -X GET \ + http://localhost:6565/v1/groups \ + -H 'Content-Type: application/json' +``` + +```json +{ + "data": [ + { + "type": "groups", + "id": "d41d8cd98f00b204e9800998ecf8427e", + "attributes": { + "path": "", + "name": "", + "checks": null + }, + "relationships": { + "groups": { + "data": [ + { + "type": "groups", + "id": "b0470a9324a4ae563b04e9ac49fbc9cf" + } + ] + }, + "parent": { + "data": null + } + } + }, + { + "type": "groups", + "id": "b0470a9324a4ae563b04e9ac49fbc9cf", + "attributes": { + "path": "::visit homepage", + "name": "visit homepage", + "checks": null + }, + "relationships": { + "groups": { + "data": [] + }, + "parent": { + "data": { + "type": "groups", + "id": "d41d8cd98f00b204e9800998ecf8427e" + } + } + } + } + ] +} +``` + +{{< /code >}} + +This endpoint returns all groups available on the test. + +For more details on how to create groups please go to [Tags and Groups](https://grafana.com/docs/k6//using-k6/tags-and-groups). + +## Get Group + +**GET** `http://localhost:6565/v1/groups/id` + +{{< code >}} + +```bash +curl -X GET \ + http://localhost:6565/v1/groups/b0470a9324a4ae563b04e9ac49fbc9cf \ + -H 'Content-Type: application/json' +``` + +```json +{ + "data": { + "type": "groups", + "id": "b0470a9324a4ae563b04e9ac49fbc9cf", + "attributes": { + "path": "::visit homepage", + "name": "visit homepage", + "checks": null + }, + "relationships": { + "groups": { + "data": [] + }, + "parent": { + "data": { + "type": "groups", + "id": "d41d8cd98f00b204e9800998ecf8427e" + } + } + } + } +} +``` + +{{< /code >}} + +This endpoint returns the Group with the given ID. + +For more details on how to create groups, please go to [Tags and Groups](https://grafana.com/docs/k6//using-k6/tags-and-groups). + +## Get Setup Data + +**GET** `http://localhost:6565/v1/setup` + +{{< code >}} + +```bash +curl -X GET \ + http://localhost:6565/v1/setup \ + -H 'Content-Type: application/json' +``` + +```json +{ + "data": { + "type": "setupData", + "id": "default", + "attributes": { + "data": { + "a": 1 + } + } + } +} +``` + +{{< /code >}} + +This endpoint returns the current JSON-encoded setup data. + +For more detail about the setup stage please go to [Test life cycle](https://grafana.com/docs/k6//using-k6/test-lifecycle). + +## Run Setup + +**PUT** `http://localhost:6565/v1/setup` + +{{< code >}} + +```bash +curl -X POST \ + http://localhost:6565/v1/setup \ + -H 'Content-Type: application/json' +``` + +```json +{ + "data": { + "type": "setupData", + "id": "default", + "attributes": { + "data": { + "a": 1 + } + } + } +} +``` + +{{< /code >}} + +This endpoint executes the Setup stage and returns the result. + +For more detail about the setup stage please go to [Test life cycle](https://grafana.com/docs/k6//using-k6/test-lifecycle). + +## Update Setup + +**PUT** `http://localhost:6565/v1/setup` + +{{< code >}} + +```bash +curl -X PUT \ + http://localhost:6565/v1/setup \ + -H 'Content-Type: application/json' \ + -d '{ + "data": { + "attributes": { + "data": { + "a": 1, + "b": 2 + } + }, + "id": "default", + "type": "setupData" + } +}' +``` + +```json +{ + "data": { + "type": "setupData", + "id": "default", + "attributes": { + "data": { + "a": 1, + "b": 2 + } + } + } +} +``` + +{{< /code >}} + +This endpoint parses the JSON request body and sets the result as Setup data. + +For more detail about the setup stage please go to [Test life cycle](https://grafana.com/docs/k6//using-k6/test-lifecycle). + +## Stop Test + +**PATCH** `http://localhost:6565/v1/status` + +{{< code >}} + +```bash +curl -X PATCH \ + http://localhost:6565/v1/status \ + -H 'Content-Type: application/json' \ + -d '{ + "data": { + "type": "status", + "id": "default", + "attributes": { + "stopped": true + } + } +}' +``` + +```json +{ + "data": { + "type": "status", + "id": "default", + "attributes": { + "stopped": true + } + } +} +``` + +{{< /code >}} + +This call parses the JSON request body to update the status and stop a running test. diff --git a/docs/sources/k6/v0.55.x/release-notes/_index.md b/docs/sources/k6/v0.55.x/release-notes/_index.md new file mode 100644 index 000000000..967b1fd86 --- /dev/null +++ b/docs/sources/k6/v0.55.x/release-notes/_index.md @@ -0,0 +1,14 @@ +--- +aliases: + - ../misc/release-notes # docs/k6//misc/release-notes +title: Grafana k6 release notes +menuTitle: Release notes +description: Learn more about new features, breaking changes, and bug fixes for each Grafana k6 version with the release notes. +weight: 100 +--- + +# Grafana k6 release notes + +{{< section >}} + +You can find a full list of release notes in the [k6 GitHub repository](https://github.com/grafana/k6/tree/master/release%20notes). diff --git a/docs/sources/k6/v0.55.x/release-notes/v0.47.0.md b/docs/sources/k6/v0.55.x/release-notes/v0.47.0.md new file mode 100644 index 000000000..67767d29f --- /dev/null +++ b/docs/sources/k6/v0.55.x/release-notes/v0.47.0.md @@ -0,0 +1,157 @@ +--- +title: Version 0.47.0 release notes +menuTitle: v0.47.0 +description: The release notes for Grafana k6 version 0.47.0 +weight: 10000 +canonical: https://github.com/grafana/k6/blob/master/release%20notes/v0.47.0.md +--- + +# Version 0.47.0 release notes + +k6 `v0.47.0` is here 🎉! This release includes: + +## Deprecations + +- [#3347](https://github.com/grafana/k6/pull/3347) The built-in `statsd` output option has been deprecated, and users should use the [xk6-output-statsd](https://github.com/LeonAdato/xk6-output-statsd) extension instead. See [#2982](https://github.com/grafana/k6/issues/2982) for future plans. +- [#3288](https://github.com/grafana/k6/pull/3288) Loading remote modules now requires users to prepend them with `https://`. Before, k6 would try to resolve importing remote modules by prepending `https://` if it was missing. This behavior has been deprecated and will be fully removed in the next release (v0.48.0). + +## New features + +### Add gRPC's binary metadata support [#3234](https://github.com/grafana/k6/pull/3234), [xk6-grpc#46](https://github.com/grafana/xk6-grpc/pull/46) + +The k6 gRPC modules (`k6/net/grpc` and `k6/experimental/grpc`) now support handling binary metadata that uses the `-bin` postfix, according to the gRPC specification. + +```js +let resp = client.invoke( + 'grpc.testing.TestService/EmptyCall', + {}, + { metadata: { 'X-Load-Tester-bin': new Uint8Array([2, 200]) } } +); +``` + +Thanks to @sapphire-janrain for the contribution! + +### Add gRPC's reflection metadata support [#3343](https://github.com/grafana/k6/pull/3343), [xk6-grpc#46](https://github.com/grafana/xk6-grpc/pull/46) + +The k6 gRPC modules (`k6/net/grpc` and `k6/experimental/grpc`) now support adding metadata to reflection requests by using a new connection parameter [`reflectMetadata`](https://k6.io/docs/javascript-api/k6-net-grpc/client/client-connect/#connectparams). + +### Higher precision for Trend metrics in Grafana Cloud k6 [#3302](https://github.com/grafana/k6/pull/3302) + +Grafana Cloud k6 is now able to store and visualize Trend metrics up to 3 digits of precision for decimal numbers. + +### Docker support for browser-based tests [#3199](https://github.com/grafana/k6/pull/3199) + +k6 is now publishig Docker images that include Chromium web browser. This allows k6 users to run tests that use [Browser API](https://k6.io/docs/javascript-api/k6-experimental/browser/) without having to install Chrome first. Check the ["A note on running browser tests" section](https://hub.docker.com/r/grafana/k6) of the Overview page on DockerHub for details. + +### Docker images for ARM64 architecture [#3320](https://github.com/grafana/k6/pull/3320) + +The k6's release process now builds and pushes dedicated Docker images for ARM64. Check k6's [tags page](https://hub.docker.com/r/grafana/k6/tags) on DockerHub for details. + +### New authentication methods and HTTP headers API for Prometheus remote write output [xk6-output-prometheus-remote#143](https://github.com/grafana/xk6-output-prometheus-remote/pull/143), [xk6-output-prometheus-remote#145](https://github.com/grafana/xk6-output-prometheus-remote/pull/145), [xk6-output-prometheus-remote#147](https://github.com/grafana/xk6-output-prometheus-remote/pull/147) + +The experimental Prometheus remote write output now supports two new authentication methods: Bearer token and TLS certificates. Check out the [documentation](https://k6.io/docs/results-output/real-time/prometheus-remote-write/#options) to learn more about how to define them using the new environment variables. +We've also added the `K6_PROMETHEUS_RW_HTTP_HEADERS` that defines a new and more convenient way to set custom HTTP headers to pass through each flush metrics' request. + +### Improved the browser module's cookie API + +The browser module now provides a more complete and robust API for handling cookies. The cookie API was stabilized by defining a new [`Cookie` class](https://k6.io/docs/javascript-api/k6-experimental/browser/browsercontext/cookie) ([browser#1008](https://github.com/grafana/xk6-browser/pull/1008), [browser#1030](https://github.com/grafana/xk6-browser/pull/1030)) that can be used while creating and retrieving cookies. This enabled us to add a new [`browserContext.cookies([urls])`](https://k6.io/docs/javascript-api/k6-experimental/browser/browsercontext/cookies/) method ([browser#1005](https://github.com/grafana/xk6-browser/pull/1005)) that returns all cookies from the current [browser context](https://k6.io/docs/javascript-api/k6-experimental/browser/browsercontext). The new API also supports filtering cookies by URL ([browser#1016](https://github.com/grafana/xk6-browser/pull/1016)). + +That led to fixing a bug where the `expires` field was not being set correctly while adding cookies using the [`context.addCookie()`](https://k6.io/docs/javascript-api/k6-experimental/browser/browsercontext/addcookies/) method ([browser#1031](https://github.com/grafana/xk6-browser/pull/1031)). Lastly, the existing [`context.clearCookies()`](https://k6.io/docs/javascript-api/k6-experimental/browser/browsercontext/clearcookies) method was fixed to clear all cookies from the current [browser context](https://k6.io/docs/javascript-api/k6-experimental/browser/browsercontext) ([browser#1040](https://github.com/grafana/xk6-browser/pull/1040)). + +```js +const context = browser.newContext(); +context.addCookies([ + { name: 'foo', value: 'bar', url: 'https://test.k6.io' }, + { name: 'baz', value: 'qux', url: 'https://grafana.com' }, +]); + +const cookies = context.cookies('https://test.k6.io'); +console.log(cookies.length); // 1 +console.log(cookies[0].name); // foo +console.log(cookies[0].value); // bar + +context.clearCookies(); +console.log(context.cookies.length); // 0 +``` + +### Add support for browser module's `page.on('console')` [browser#1006](https://github.com/grafana/xk6-browser/pull/1006) + +Allows users to register a handler to be executed every time the `console` API methods are called from within the page's JavaScript context. The arguments passed into the handler are defined by the [ConsoleMessage](https://k6.io/docs/javascript-api/k6-experimental/browser/consolemessage/) class. + +```js +page.on('console', (msg) => { + check(msg, { + assertConsoleMessageType: (msg) => msg.type() == 'log', + assertConsoleMessageText: (msg) => msg.text() == 'this is a console.log message 42', + assertConsoleMessageArgs0: (msg) => + msg.args()[0].jsonValue() == 'this is a console.log message', + assertConsoleMessageArgs1: (msg) => msg.args()[1].jsonValue() == 42, + }); +}); + +page.evaluate(() => console.log('this is a console.log message', 42)); +``` + +## UX improvements and enhancements + +- [#3338](https://github.com/grafana/k6/pull/3338), [xk6-grpc#48](https://github.com/grafana/xk6-grpc/pull/48) Adds support for the gRPC reflection protocol v1. +- [#3290](https://github.com/grafana/k6/pull/3290) Adds error logging when executing `setup` and `teardown` via REST API. Thanks to @kmtym1998 for the contribution! +- [#3327](https://github.com/grafana/k6/pull/3327) Adds commit identifier for the k6 build when running `k6 version`. +- [#3340](https://github.com/grafana/k6/pull/3340) Updates k6 `*-with-browser` Docker images to automatically set the `no-sandbox` environment variable. +- [#3335](https://github.com/grafana/k6/pull/3335) The character limit for metric names increased from 63 to 128 after the OpenTelemetry update. k6 will return an error starting on the next release (v0.48.0) if users hit the limit. +- [browser#1007](https://github.com/grafana/xk6-browser/pull/1007) Adds a `k6` object (`window.k6 = {};`) to help identify k6 browser module tests. +- [browser#1022](https://github.com/grafana/xk6-browser/pull/1022) Refactors the `check` in `examples/fillform.js` so that it matches the type definitions and documentation for `check`. + +## Bug fixes + +- [xk6-grpc#47](https://github.com/grafana/xk6-grpc/pull/47) Fixes the premature closing of a gRPC stream when a stream's client has finished sending. Thanks to @thiagodpf for reporting! +- [#3344](https://github.com/grafana/k6/pull/3344), [xk6-grpc#49](https://github.com/grafana/xk6-grpc/pull/49) Adds support for Google's protobuf wrappers. Thanks to @zibul444 for reporting! +- [#3308](https://github.com/grafana/k6/pull/3308) Updates `goja` version, and fixes a compiler bug when a class is declared in a function with an argument. +- [browser#1039](https://github.com/grafana/xk6-browser/pull/1039) Fixes `goja` conversions while adding and retrieving cookies. +- [browser#1038](https://github.com/grafana/xk6-browser/pull/1038) Fixes read/write data race for edge case with remote browsers. +- [browser#1034](https://github.com/grafana/xk6-browser/pull/1034) Fixes `page.reload` & `page.setContent` to use the default navigation timeout over the default timeout. +- [browser#1033](https://github.com/grafana/xk6-browser/pull/1033) Fixes the `page` timeouts so it is actually used after being set. + +## Maintenance and internal improvements + +- [#3342](https://github.com/grafana/k6/pull/3342) Updates xk6-grpc to the latest version. This change brings all the latest fixes and improvements to the experimental gRPC module. +- [#3271](https://github.com/grafana/k6/pull/3271),[#3272](https://github.com/grafana/k6/pull/3272) Updates the golangci version and adds the `interfacebloat` linter. +- [#3279](https://github.com/grafana/k6/pull/3279) Fixes the CI not publishing the SBOM file on a new release. +- [#3283](https://github.com/grafana/k6/pull/3283) Updates the Go version in k6's CI used to build the binaries. +- [#3341](https://github.com/grafana/k6/pull/3341), [#3339](https://github.com/grafana/k6/pull/3339) Updates `goja`, includes runtime initialization speed-up and a fix for source indexes. +- [#3311](https://github.com/grafana/k6/pull/3311) Updates the `alpine` image version that is used as the base of the k6 Docker image. +- [browser#1043](https://github.com/grafana/xk6-browser/pull/1043), [browser#1021](https://github.com/grafana/xk6-browser/pull/1021), [browser#1019](https://github.com/grafana/xk6-browser/pull/1019), [browser#1014](https://github.com/grafana/xk6-browser/pull/1014) Fixes xk6-browser tests. +- [browser#1000](https://github.com/grafana/xk6-browser/pull/1000), [browser#1024](https://github.com/grafana/xk6-browser/pull/1024) Refines xk6-browser issue and PR templates. +- [browser#1003](https://github.com/grafana/xk6-browser/pull/1003), [browser#1009](https://github.com/grafana/xk6-browser/pull/1009), [browser#1010](https://github.com/grafana/xk6-browser/pull/1010) Internal changes to xk6-browser. +- [browser#997](https://github.com/grafana/xk6-browser/pull/997) Updates xk6-browser readme. +- [browser#962](https://github.com/grafana/xk6-browser/pull/962) CI fixes. +- [browser#1035](https://github.com/grafana/xk6-browser/pull/1035) Refactors `int64` timeout to `time.Duration`, to help avoid confusion as to whether a timeout is in milliseconds or seconds. + +## Roadmap + +### Native ECMAScript Modules support + +Work on this [epic issue](https://github.com/grafana/k6/issues/3265) has been picked up and there is some progress in the underlying implementation. + +One of the main internal changes will be dropping [Babel](https://babeljs.io), which is currently used to transpile [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) code to [CommonJS](https://en.wikipedia.org/wiki/CommonJS). + +For users, it will mean better JavaScript support as this change will automatically get [object spread](https://github.com/grafana/k6/issues/824) working and likely faster startup for _big_ scripts. In the future, this also means JavaScript compatibility will be easier to add, since it'll only need to be supported in the JavaScript VM we use - [goja](https://github.com/dop251/goja). + +There's a risk that some k6 tests using both CommonJS and ECMAScript modules syntax will change in behavior. In practice, using both should never really be done as they're not compatible. + +Because there are risks involved, we are going to do this very carefully and only once we have done a lot of tests. That means this is currently planned for v0.49.0, but with the majority of the remaining work done in the v0.48.0 cycle. + +This way, we will also be able to have most of our users test these changes from our `master` branch, using Docker images, for example. + +### Future breaking changes + +There are several changes in the next release that are entering the final stage of their deprecation period. That means the next release will include the following breaking changes: + +- [#3065](https://github.com/grafana/k6/issues/3065) Metric names will be limited to 128 characters and a set of allowed symbols. This was going to be changed to a 63 character limit to ensure compatibility with OpenTelemetry standards, but since then, the OTel limit has been updated to 255 characters. You can find more details about the changes in the linked issue. +- [#3365](https://github.com/grafana/k6/pull/3365) The `k6 converter` command will be removed. +- [#3350](https://github.com/grafana/k6/pull/3350) The `headers` param for the gRPC module will be removed. Users should use the [metadata](https://k6.io/docs/javascript-api/k6-experimental/grpc/params) property instead. +- [#3287](https://github.com/grafana/k6/issues/3287) Loading remote modules will require users to include the `https://` protocol in their URLs, otherwise it will return an error. +- CSV output no longer accepts arguments in snake case (for example, [fileName](https://k6.io/docs/results-output/real-time/csv/#csv-options) should be used instead of `file_name`). +- The `--logformat` flag for defining the Log format option will be removed. [--log-format](https://k6.io/docs/using-k6/k6-options/reference/#logformat) should be used instead. + +We recommend checking this list to see if you are impacted, and updating your scripts or CI/CD processes to avoid any issues. If you have any feedback, please open an issue. diff --git a/docs/sources/k6/v0.55.x/release-notes/v0.48.0.md b/docs/sources/k6/v0.55.x/release-notes/v0.48.0.md new file mode 100644 index 000000000..58f6814bb --- /dev/null +++ b/docs/sources/k6/v0.55.x/release-notes/v0.48.0.md @@ -0,0 +1,310 @@ +--- +title: Version 0.48.0 release notes +menuTitle: v0.48.0 +description: The release notes for Grafana k6 version 0.48.0 +weight: 9999 +canonical: https://github.com/grafana/k6/blob/master/release%20notes/v0.48.0.md +--- + +# Version 0.48.0 release notes + +k6 v0.48.0 is here 🎉! This release includes: + +- Numerous long-awaited breaking changes. +- A new `k6 new` subcommand to generate a new test script. +- A new `k6/experimental/fs` module for file interactions. +- CPU and network throttling support for the k6 browser module. + +## Breaking changes + +This release includes several breaking changes, mainly cleaning up deprecations from previous versions. They should have a straightforward migration process, and not heavily impact existing users. + +- [#3448](https://github.com/grafana/k6/pull/3448) limits metric names, aligning to both OpenTelemetry (OTEL) and Prometheus name requirements, while still being limited to 128 ASCII characters. Warnings about the limit started in [v0.45](https://github.com/grafana/k6/releases/tag/v0.45.0). +- [#3439](https://github.com/grafana/k6/pull/3439) changes the `Client` signature in `k6/experimental/redis` module. Refer to the module-related section below. +- [#3350](https://github.com/grafana/k6/pull/3350) removes the `grpc.invoke()`'s parameter `headers`, deprecated in k6 [v0.37](https://github.com/grafana/k6/releases/tag/v0.37.0). Use the `metadata` parameter instead. +- [#3389](https://github.com/grafana/k6/pull/3389) removes the `--logformat` flag, deprecated in [v0.38](https://github.com/grafana/k6/releases/tag/v0.38.0). Use the `--log-format` flag instead. +- [#3390](https://github.com/grafana/k6/pull/3390) removes all CSV output's CLI arguments, deprecated in [v0.35](https://github.com/grafana/k6/releases/tag/v0.35.0). This change makes the CSV output consistent with other output formats. +- [#3365](https://github.com/grafana/k6/pull/3365) removes the `k6 convert` CLI command, deprecated in [v0.41](https://github.com/grafana/k6/releases/tag/v0.41.0). Use the [har-to-k6](https://github.com/grafana/har-to-k6) package instead. +- [#3451](https://github.com/grafana/k6/pull/3451) removes logic that would attempt to prepend a `https://` scheme to module specifiers that were not recognized. Deprecated in k6 [v0.25](https://github.com/grafana/k6/releases/tag/v0.25.0). Use full URLs if you want to load remote modules instead. + +## New features + +### Add `k6 new` subcommand [#3394](https://github.com/grafana/k6/pull/3394) + +`k6` now has a `new` subcommand that generates a new test script. This is useful for new users who want to get started quickly, or for experienced users who want to save time when creating new test scripts. To use the subcommand, open your terminal and type: + +```bash +k6 new [filename] +``` + +If no filename is provided, k6 uses `script.js` as the default filename. The subcommand will create a new file with the provided name in the current directory, and populate it with a basic test script that can be run with `k6 run`. + +### Add a `k6/experimental/fs` module [#3165](https://github.com/grafana/k6/pull/3165) + +`k6` now has a new `k6/experimenal/fs` module providing a memory-efficient way to handle file interactions within your test scripts. It currently offers support for opening files, reading their content, seeking through it, and retrieving metadata about them. + +Unlike the traditional [open](https://grafana.com/docs/k6/latest/javascript-api/init-context/open/) function, which loads a file multiple times into memory, the filesystem module reduces memory usage by loading the file as little as possible, and sharing the same memory space between all VUs. This approach significantly reduces the memory footprint of your test script and lets you load and process large files without running out of memory. + +For more information, refer to the [module documentation](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/fs/). + +
+ Expand to see an example of the new functionality. + +This example shows the new module usage: + +```javascript +import fs from 'k6/experimental/fs'; + +// k6 doesn't support async in the init context. We use a top-level async function for `await`. +// +// Each Virtual User gets its own `file` copy. +// So, operations like `seek` or `read` won't impact other VUs. +let file; +(async function () { + file = await open('bonjour.txt'); +})(); + +export default async function () { + // About information about the file + const fileinfo = await file.stat(); + if (fileinfo.name != 'bonjour.txt') { + throw new Error('Unexpected file name'); + } + + const buffer = new Uint8Array(128); + + let totalBytesRead = 0; + while (true) { + // Read into the buffer + const bytesRead = await file.read(buffer); + if (bytesRead == null) { + // EOF + break; + } + + // Do something useful with the content of the buffer + totalBytesRead += bytesRead; + + // If bytesRead is less than the buffer size, we've read the whole file + if (bytesRead < buffer.byteLength) { + break; + } + } + + // Check that we read the expected number of bytes + if (totalBytesRead != fileinfo.size) { + throw new Error('Unexpected number of bytes read'); + } + + // Seek back to the beginning of the file + await file.seek(0, SeekMode.Start); +} +``` + +
+ +### Redis (m)TLS support and new Client constructor options [#3439](https://github.com/grafana/k6/pull/3439), [xk6-redis/#17](https://github.com/grafana/xk6-redis/pull/17) + +In this release, the `k6/experimental/redis` module receives several important updates, including breaking changes. + +#### Connection URLs + +The `Client` constructor now supports connection URLs to configure connections to Redis servers or clusters. These URLs can be in the format `redis://[[username][:password]@][host][:port][/db-number]` for standard connections, or `rediss://[[username][]:password@]][host][:port][/db-number]` for TLS-secured connections. For more details, refer to the [documentation](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/redis/). + +##### Example usage + +```javascript +import redis from 'k6/experimental/redis'; + +const redisClient = new redis.Client('redis://someusername:somepassword@localhost:6379/0'); +``` + +#### Revamped Options object + +The `Client` constructor has been updated with a new [Options](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/redis/redis-options/) object format. This change aligns the module with familiar patterns from Node.js and Deno libraries, offering enhanced flexibility and control over Redis connections. For more details, refer to the [documentation](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/redis/redis-options/). + +
+ Expand to see an example of the new functionality. + +This example shows the usage of the new `Options` object: + +```javascript +import redis from 'k6/experimental/redis'; + +const redisClient = new redis.Client({ + socket: { + host: 'localhost', + port: 6379, + }, + username: 'someusername', + password: 'somepassword', +}); +``` + +
+ +#### (m)TLS support + +The Redis module now includes (m)TLS support, enhancing security for connections. This update also improves support for Redis clusters and sentinel modes (failover). For connections using self-signed certificates, enable k6's [insecureSkipTLSVerify](https://grafana.com/docs/k6/latest/using-k6/k6-options/reference/#insecure-skip-tls-verify) option (set to `true`). + +
+ Expand to see an example of the new functionality. + +This example shows the configuration of a TLS connection: + +```javascript +import redis from 'k6/experimental/redis'; + +const redisClient = new redis.Client({ + socket: { + host: 'localhost', + port: 6379, + tls: { + ca: [open('ca.crt')], + cert: open('client.crt'), // client certificate + key: open('client.key'), // client private key + }, + }, +}); +``` + +
+ +### Add tracing instrumentation [#3445](https://github.com/grafana/k6/pull/3445) + +`k6` now supports a new _traces output_ option that allows you to configure the output for traces generated during its execution. This option can be set through the `--traces-output` argument in the `k6 run` command or by setting the `K6_TRACES_OUTPUT` environment variable. + +Currently, no traces are generated by `k6` itself, but this feature represents the first step towards richer tracing functionalities in `k6` and its extensions. + +By default traces output is set to `none`, and currently the only supported output is `otel` which uses the [opentelemetry-go](https://github.com/open-telemetry/opentelemetry-go)'s [Open Telemetry](https://opentelemetry.io/) API and SDK implementations. The format for the `otel` _traces output_ configuration is the following: + +``` +--traces-output=[,opt1=val1,opt2=val2] +``` + +Where `opt`s can be one of the following options: + +- `proto`: Specifies the protocol to use in the connection to the traces backend. Supports `grpc` _(default)_ and `http`. +- `header.`: Specifies an additional header to include in the connection to the traces backend. + +Example: + +``` +K6_TRACES_OUTPUT=https://traces.k6.io/v1/traces,proto=http,header.Authorization=Bearer token +``` + +### Add support for browser module's `page.throttleCPU` [browser#1095](https://github.com/grafana/xk6-browser/pull/1095) + +The browser module now supports throttling the CPU from chrome/chromium's perspective by using the `throttleCPU` API, which helps emulate slower devices when testing the website's frontend. It requires an argument of type `CPUProfile`, which includes a `rate` field that is a slow-down factor, where `1` means no throttling, `2` means 2x slowdown, and so on. For more details, refer to the [documentation](https://grafana.com/docs/k6/v0.48.x/javascript-api/k6-experimental/browser/page/throttlecpu). + +```js +... + const context = browser.newContext(); + const page = context.newPage(); + + try { + page.throttleCPU({ rate: 4 }); +... +``` + +### Add support for browser module's `page.throttleNetwork` [browser#1094](https://github.com/grafana/xk6-browser/pull/1094) + +The browser module now supports throttling the characteristics of the network from chrome/chromium's perspective by using the `throttleNetwork` API, which helps emulate slow network connections when testing the website's frontend. It requires an argument of type `NetworkProfile`, with a definition of: + +```ts +export interface NetworkProfile { + /* + * Minimum latency from request sent to response headers received (ms). + */ + latency: number; + + /* + * Maximal aggregated download throughput (bytes/sec). -1 disables download + * throttling. + */ + download: number; + + /* + * Maximal aggregated upload throughput (bytes/sec). -1 disables upload + * throttling. + */ + upload: number; +} +``` + +You can either define your own network profiles or use the ones we have defined by importing `networkProfiles` from the `browser` module. For more details, refer to the [documentation](https://grafana.com/docs/k6/v0.48.x/javascript-api/k6-experimental/browser/page/throttlenetwork). + +```js +import { browser, networkProfiles } from 'k6/experimental/browser'; +... + const context = browser.newContext(); + const page = context.newPage(); + + try { + page.throttleNetwork(networkProfiles['Slow 3G']); +... +``` + +## k6's documentation is moving under [grafana.com/docs/k6](https://grafana.com/docs/k6) + +It's not directly part of the k6 v0.48 release, but we believe it is worth mentioning that we're moving the documentation from [k6.io/docs](https://k6.io/docs/) to [grafana.com/docs/k6](https://grafana.com/docs/k6). + +The legacy documentation space `k6.io/docs` will be available for a while, but we encourage you to update your bookmarks and links to the new domain. + +## UX improvements and enhancements + +- [browser#1074](https://github.com/grafana/xk6-browser/pull/1074) adds a new `browser.closeContext()` [method](https://grafana.com/docs/k6/v0.48.x/javascript-api/k6-experimental/browser/closecontext) to facilitate closing the current active browser context. +- [#3370](https://github.com/grafana/k6/pull/3370) adds a new flag `--profiling-enabled` which enables exposing [pprof](https://go.dev/blog/pprof) profiling endpoints. The profiling endpoints are exposed on the same port as the HTTP REST API under the `/debug/pprof/` path. This can be useful for extension developers. +- [#3442](https://github.com/grafana/k6/pull/3442) adds a new `--version` flag, which has the same output as `k6 version` command. Thanks, @ffapitalle! +- [#3423](https://github.com/grafana/k6/pull/3423) adds an environment variable `K6_INFLUXDB_PROXY` to the InfluxDB output which allows specifying proxy. Thanks, @IvanovOleg! +- [#3398](https://github.com/grafana/k6/pull/3398) enables k6 cloud traces by default. +- [#3400](https://github.com/grafana/k6/pull/3400) sets a binary-based cloud output (a.k.a. cloud output v2) as the default version for streaming metrics from a local test run via `-o cloud`. +- [#3452](https://github.com/grafana/k6/pull/3452) adds `fsext.Abs` helper function. + +## Bug fixes + +- [#3380](https://github.com/grafana/k6/pull/3380) corrects `console.debug()`, aligning `-v` output to `--console-output` and `stdout`. +- [#3416](https://github.com/grafana/k6/pull/3416) prints the stack trace when there's an exception in `handleSummary()`. +- [#3438](https://github.com/grafana/k6/pull/3438) prevents an error on HTTP requests with `content-encoding` header and HTTP statuses known for having no body. +- [browser#1077](https://github.com/grafana/xk6-browser/pull/1077) fixes `browserContext.clearPermissions` to clear permissions without panic. +- [browser#1042](https://github.com/grafana/xk6-browser/pull/1042) fixes `browserContext.waitForEvent` which involved promisifying the `waitForEvent` API. +- [browser#1078](https://github.com/grafana/xk6-browser/pull/1078) fixes request interception deadlock to improve stability. +- [browser#1101](https://github.com/grafana/xk6-browser/pull/1101) fixes `page.$` so that it returns `null` when no matches with given selector are found. +- [#3397](https://github.com/grafana/k6/pull/3397), [#3427](https://github.com/grafana/k6/pull/3427), [#3417](https://github.com/grafana/k6/pull/3417) update `goja` dependency. Fixes a possible panic and proper handling circular types at `JSON.stringify`. Fixes an issue about dumping the correct stack trace when an error is re-thrown. +- [browser#1106](https://github.com/grafana/xk6-browser/pull/1106) fixes an NPE on NavigateFrame when navigate occurs in the same document. +- [browser#1096](https://github.com/grafana/xk6-browser/pull/1096) fixes a panic when trying to interact within nested `iframe`s. Thanks, @bandorko! + +## Maintenance and internal improvements + +- [#3378](https://github.com/grafana/k6/pull/3378) fixes usage of `gh` in GitHub actions creating the OSS release. +- [#3386](https://github.com/grafana/k6/pull/3386), [#3387](https://github.com/grafana/k6/pull/3387), [#3388](https://github.com/grafana/k6/pull/3388), [browser#1047](https://github.com/grafana/xk6-browser/pull/1047) update dependencies. +- [#3393](https://github.com/grafana/k6/pull/3393), [#3399](https://github.com/grafana/k6/pull/3399) fix lint issues in the `js` package. +- [#3381](https://github.com/grafana/k6/pull/3381) disables temporarily ARM tests on GitHub Actions. +- [#3401](https://github.com/grafana/k6/pull/3401), [#3469](https://github.com/grafana/k6/pull/3469) refactors a Makefile, removes `make ci-like-lint` in favor of `make lint`. Updates a golangci-lint version to v1.55.2. +- [#3410](https://github.com/grafana/k6/pull/3410) fixes the `tests` reference in the all rule of the Makefile. Thanks, @flyck! +- [#3402](https://github.com/grafana/k6/pull/3402) adds a test-case for the `k6 cloud`. +- [#3421](https://github.com/grafana/k6/pull/3421) updates dependencies for xk6 integration tests. +- [browser#1075](https://github.com/grafana/xk6-browser/pull/1075), [browser#1076](https://github.com/grafana/xk6-browser/pull/1076) refactors `clearPermissions` and `grantPermissions`. +- [browser#1043](https://github.com/grafana/xk6-browser/pull/1043) refines tests. +- [browser#1069](https://github.com/grafana/xk6-browser/pull/1069), [browser#1090](https://github.com/grafana/xk6-browser/pull/1090) refactor internal. +- [browser#1102](https://github.com/grafana/xk6-browser/pull/1102) uses `force` and `noWaitAfter` in `frame.newAction`. +- [#3443](https://github.com/grafana/k6/pull/3443) mentions that k6-core team aims to support the last two major golang versions for building a k6 binary. +- [#3437](https://github.com/grafana/k6/pull/3437) switches k6 cloud traces to a new hostname. +- [#3429](https://github.com/grafana/k6/pull/3429) increases timeout expectations for the `TestSetupTimeout` test. +- [#3446](https://github.com/grafana/k6/pull/3446) moves log tokenizer to `lib/strvals` package. + +## Roadmap + +### Graduating from experimental + +It has been a while since we've introduced the `k6/experimental` namespace. This namespace was specifically created to test new features before we fully committed to them. Thanks to it, we have been able to iterate on features and receive valuable feedback from the community before adding them to the core of k6. + +In the following releases, we're going to graduate `k6/experimental/grpc` and `k6/experimental/timers`. + +These modules' "experimental" versions will remain available for a couple of releases, but the goal is to remove the "experimental" imports for them in favor of the core-only imports. + +### New dashboard features + +We're happy to announce our work on a new, upcoming dashboard feature. Based on the [xk6-dashboard](https://github.com/grafana/xk6-dashboard) extension, this upcoming feature will enable you to visualize your test runs and their results in your web browser, in real time. The k6 maintainers team is starting to work towards its integration into the core of k6, and we're aiming to release it in the next couple of releases. + +While the final user-experience might differ, you can already try it out by following the instructions in the [xk6-dashboard](https://github.com/grafana/xk6-dashboard) repository. We update the extension on a regular basis as we're converging towards the first release of the feature in k6. Go ahead and give it a try! Let us know what you think about it! diff --git a/docs/sources/k6/v0.55.x/release-notes/v0.49.0.md b/docs/sources/k6/v0.55.x/release-notes/v0.49.0.md new file mode 100644 index 000000000..e91954ef8 --- /dev/null +++ b/docs/sources/k6/v0.55.x/release-notes/v0.49.0.md @@ -0,0 +1,209 @@ +--- +title: Version 0.49.0 release notes +menuTitle: v0.49.0 +description: The release notes for Grafana k6 version 0.49.0 +weight: 9998 +canonical: https://github.com/grafana/k6/blob/master/release%20notes/v0.49.0.md +--- + +# Version 0.49.0 release notes + +k6 `v0.49.0` is here 🎉! This release: + +- Adds a built-in [web dashboard](https://grafana.com/docs/k6/latest/results-output/web-dashboard/) that displays test results in real time. +- Introduces `clear` functionality to the browser module's `locator` classes. +- Merges the gRPC experimental module back into the gRPC core module. +- Enables the ability to get the selection from an element in `k6/html`. +- Collects internal modules and outputs used by a script. +- Prepares `k6/experimental/timers` for stabilization. + +## Breaking changes + +- [#3494](https://github.com/grafana/k6/pull/3494) stops updating `loadimpact/k6` docker image. If you still use it, please migrate to the [grafana/k6](https://hub.docker.com/r/grafana/k6) image. +- [browser#1111](https://github.com/grafana/xk6-browser/pull/1111) removes `timeout` option for `isVisible` and `isHidden` since the API no longer waits for the element to appear on the page. + +## New features + +### Web Dashboard + +The new [web dashboard](https://grafana.com/docs/k6/latest/results-output/web-dashboard/) brings real-time visualization to load testing. This feature allows users to monitor test progress and analyze +results dynamically, enhancing the overall testing experience. + +#### Real-time test results + +Activate this feature using the environment variable `K6_WEB_DASHBOARD=true`. For this initial release, the dashboard is not enabled by default to allow users to opt into this new experience as it evolves. + +```bash +K6_WEB_DASHBOARD=true k6 run script.js +``` + +Once enabled and the test script is running, navigate to [http://localhost:5665](http://localhost:5665) in your web browser to access the dashboard. + +![k6 Web Dashboard Overview](https://github.com/grafana/xk6-dashboard/blob/master/screenshot/k6-dashboard-overview-light.png?raw=true) + +#### Test report + +The web dashboard also offers an HTML test report (see [an example](https://github.com/grafana/xk6-dashboard/blob/master/screenshot/k6-dashboard-html-report-screen-view.png?raw=true)) for detailed analysis, enabling easy sharing and downloading capabilities for +collaboration. + +To access and download the report, click on the **Report** button in the dashboard's top right corner or use the `K6_WEB_DASHBOARD_EXPORT` environment variable. + +```bash +K6_WEB_DASHBOARD=true K6_WEB_DASHBOARD_EXPORT=test-report.html k6 run script.js +``` + +### Add `clear` to the `locator` class [browser#1149](https://github.com/grafana/xk6-browser/pull/1149) + +The new `clear` method on the `locator` class clears the text boxes and input fields. This is useful when navigating to a website where the text boxes and input fields already contain a value that needs to be cleared before filling it with a specific value. + +
+ Expand to see an example of the new functionality. + +```javascript +import { check } from 'k6'; +import { browser } from 'k6/experimental/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const context = browser.newContext(); + const page = context.newPage(); + + await page.goto('https://test.k6.io/my_messages.php', { waitUntil: 'networkidle' }); + + // To mimic an input field with existing text. + page.locator('input[name="login"]').type('admin'); + + check(page, { + not_empty: (p) => p.locator('input[name="login"]').inputValue() != '', + }); + + // Clear the text. + page.locator('input[name="login"]').clear(); + + check(page, { + empty: (p) => p.locator('input[name="login"]').inputValue() == '', + }); + + page.close(); +} +``` + +
+ +### Add tracing to the browser module [browser#1100](https://github.com/grafana/xk6-browser/pull/1100) + +The browser module now generates traces that provide a representation of its inner workings, such as API methods executed (for example `browser.newPage` and `page.goto`), page navigations, and [Web Vitals](https://grafana.com/docs/k6/latest/using-k6-browser/metrics/#googles-core-web-vitals) measurements. + +Currently, the instrumented methods are a subset of all the methods exposed by the browser module API, but this will be extended in the future. + +The traces generation for the browser module depends on the overall `k6` traces option introduced in [v0.48.0](https://github.com/grafana/k6/releases/tag/v0.48.0). Check out the [documentation](https://grafana.com/docs/k6/latest/using-k6/k6-options/reference/#traces-output) to learn more about it. + +### gRPC streaming API becomes part of the k6 core [#3490](https://github.com/grafana/k6/pull/3490) + +With this release, gRPC's streaming API becomes part of the core's `k6/net/grpc` module. The experimental `k6/experimental/grpc` has been back-merged into the core. + +You can still use import `k6/experimental/grpc` for a couple of releases, but it's deprecated and will be removed in the future (planned in k6 version `v0.51.0`). + +To migrate your scripts, replace `k6/experimental/grpc` with `k6/net/grpc` in your script imports, and the code should work as before. + +### k6/html: Extract selection from element [#3519](https://github.com/grafana/k6/pull/3519) + +[`k6/html`](https://grafana.com/docs/k6/latest/javascript-api/k6-html/) has been around for a while and allows you to search within an HTML document with a jQuery-like API called [Selection](https://grafana.com/docs/k6/latest/javascript-api/k6-html/selection/), and also has support for the more standard [Element](https://grafana.com/docs/k6/latest/javascript-api/k6-html/element/) that represents DOM element. + +For a long time, you could get an Element from a Selection using the [`.get(index)`](https://grafana.com/docs/k6/latest/javascript-api/k6-html/selection/selection-get/), but you couldn't get back to a Selection from an Element. + +This is not a common case, but one that requires quite a bit of code. For example, see the following jQuery snippet: + +```javascript +let li = http.get('https://test.k6.io').html().find('li'); +li.each(function (_, element) { + // here element is an element not a selection + // but what if for each li we want to select something more? + // in jquery that will be: + let container = $(element).closest('ul.header-icons'); + // but what should `$` do? + // in a browser there is only 1 html document that you have access to + // in k6 though you can be working with multiple ones, so `$` can't know which one it should + // work against +}); +``` + +In order to support the above example, you can use `selection`, without going to the element: + +```javascript +let li = http.get('https://test.k6.io').html().find('li'); +for (; li.size() > 0; li = li.next()) { + let ul = li.closest('ul.header-icons'); // li here is still a selection and we iterate over it. +} +``` + +This is not always possible though, and arguably isn't what most users will naturally do. + +Because of this, we have now added a new [`.selection()`](https://grafana.com/docs/k6/latest/javascript-api/k6-html/element/element-selection/) which returns a selection for its element. + +```javascript +let li = http.get('https://test.k6.io').html().find('li'); +li.each(function (_, element) { + let container = element.selection().closest('ul.header-icons'); + // .. more code +}); +``` + +Thanks to @Azhovan! :bow: :tada: + +### Collect usage data on imported internal modules and outputs [#3525](https://github.com/grafana/k6/pull/3525) + +k6 now collects usage data of the modules and outputs that are being used when the [usage report](https://grafana.com/docs/k6/latest/misc/usage-collection) is enabled. The data collection is only related to the built-in k6 modules and outputs. Private, custom modules and extensions [are never collected](https://github.com/grafana/k6/blob/f35e67902605877ebf2c5e9c8673cd7faf4cdc1e/cmd/report.go#L33-L57). The usage report is enabled by default in k6, but it is possible to opt-out using the [no-usage-report](https://grafana.com/docs/k6/latest/using-k6/k6-options/reference/#no-usage-report) option. + +We always want to improve the product, but at the same time, we need to pay attention to where we allocate our resources. Having data of what are the most used modules and outputs gives us better confidence to make decisions because we are supported by data. +The data can let us know what percentage of our users will benefit from the introduction of a new feature and also, how many of them would be impacted in case of a breaking change. + +## UX improvements and enhancements + +- [#3529](https://github.com/grafana/k6/pull/3529) enables the k6 cloud traces output by default. +- [#3440](https://github.com/grafana/k6/pull/3440) adds a fallback for using built-in certificates if the OS provides none. Thanks to `@mem` for working on it! +- [browser#1104](https://github.com/grafana/xk6-browser/pull/1104) adds support for browser module traces metadata. Users can define _key-value_ metadata that will be included as attributes in every generated span. +- [browser#1135](https://github.com/grafana/xk6-browser/pull/1135) improves the array output from `console` in the k6 logs. +- [browser#1137](https://github.com/grafana/xk6-browser/pull/1137), [browser#1145](https://github.com/grafana/xk6-browser/pull/1145) improves the error messages displayed when Chrome or Chromium isn't found. +- [#3543](https://github.com/grafana/k6/pull/3543) replaces documentation URLs to `grafana.com/docs/k6/latest/`. + +## Bug fixes + +- [#3485](https://github.com/grafana/k6/pull/3485) fixes the REST API always logging a 200 status code response, which was found as part of fixing lint issues in the code. +- [browser#1129](https://github.com/grafana/xk6-browser/pull/1129) mitigates the risk of panics when the website under test uses the `console`. +- [browser#1133](https://github.com/grafana/xk6-browser/pull/1133) fixes `BigInt` parsing. +- [browser#1108](https://github.com/grafana/xk6-browser/pull/1108), [browser#1110](https://github.com/grafana/xk6-browser/pull/1110) fixes `isVisible` and `isHidden` so that it doesn't wait for an element to match with the given `selector`, allowing it to continue on with the test script when elements are not on the page. +- [browser#1121](https://github.com/grafana/xk6-browser/pull/1121) fixes `dblClick` so that it works with `onDblClick` and performs two clicks on the specified element. +- [browser#1152](https://github.com/grafana/xk6-browser/pull/1152) fixes a nil pointer dereference when navigating around on SPA websites. + +## Maintenance and internal improvements + +- [#3204](https://github.com/grafana/k6/pull/3204) internal refactor to make future distributed execution work easier. With a small fix to tests in [#3531](https://github.com/grafana/k6/pull/3531). Thanks to @na-- :tada:. +- Lint fixes throughout the k6 code base [#3460](https://github.com/grafana/k6/pull/3460), [#3462](https://github.com/grafana/k6/pull/3462), [#3463](https://github.com/grafana/k6/pull/3463), [#3478](https://github.com/grafana/k6/pull/3478), [#3479](https://github.com/grafana/k6/pull/3479), [#3480](https://github.com/grafana/k6/pull/3480), [#3481](https://github.com/grafana/k6/pull/3481), [#3482](https://github.com/grafana/k6/pull/3482), [#3483](https://github.com/grafana/k6/pull/3483), [#3484](https://github.com/grafana/k6/pull/3484), [#3485](https://github.com/grafana/k6/pull/3485), [#3495](https://github.com/grafana/k6/pull/3495). +- [#3473](https://github.com/grafana/k6/pull/3473) refinements to the release process. +- Dependency updates across k6 [#3500](https://github.com/grafana/k6/pull/3500), [#3501](https://github.com/grafana/k6/pull/3501), [#3502](https://github.com/grafana/k6/pull/3502), [#3503](https://github.com/grafana/k6/pull/3503), [#3509](https://github.com/grafana/k6/pull/3509), [#3513](https://github.com/grafana/k6/pull/3513), [#3537](https://github.com/grafana/k6/pull/3537), [#3539](https://github.com/grafana/k6/pull/3539), [#3540](https://github.com/grafana/k6/pull/3540). +- [#3489](https://github.com/grafana/k6/pull/3489) migrates pull-requests assignment to `CODEOWNERS` from GitHub Action. +- [#3496](https://github.com/grafana/k6/pull/3496) checks for security issues with a scheduled trivy scan. +- [#3517](https://github.com/grafana/k6/pull/3517) adds unit tests to the cloadapi package. This is the first contribution by external contributor @nilskch. Thanks for this @nilskch :bow:. +- [#3520](https://github.com/grafana/k6/pull/3520) stops using deprecated by golang net.Dialer.DualStack option. +- [#3526](https://github.com/grafana/k6/pull/3526) refactors to JavaScript package test around `open` and `require` and their handling of paths. +- [#3527](https://github.com/grafana/k6/pull/3527) generates test certificates for more tests during the test. This, among other things, fixes macOS tests. +- [#3528](https://github.com/grafana/k6/pull/3528) enables macOS tests in GitHub Actions. +- [browser#1134](https://github.com/grafana/xk6-browser/pull/1134) adds a new error type when parsing objects. +- [browser#1107](https://github.com/grafana/xk6-browser/pull/1107), [browser#1109](https://github.com/grafana/xk6-browser/pull/1109) refactor internals. + +## Roadmap + +As mentioned earlier, there's work in progress to make xk6-timers stable as part of the next release. You can find more information on issue [#3297](https://github.com/grafana/k6/issues/3297). diff --git a/docs/sources/k6/v0.55.x/release-notes/v0.50.0.md b/docs/sources/k6/v0.55.x/release-notes/v0.50.0.md new file mode 100644 index 000000000..bc860ef92 --- /dev/null +++ b/docs/sources/k6/v0.55.x/release-notes/v0.50.0.md @@ -0,0 +1,183 @@ +--- +title: Version 0.50.0 release notes +menuTitle: v0.50.0 +description: The release notes for Grafana k6 version 0.50.0 +weight: 9997 +canonical: https://github.com/grafana/k6/blob/master/release%20notes/v0.50.0.md +--- + +# Version 0.50.0 release notes + +k6 `v0.50.0` is here 🎉! + +This release: + +- Adds support for uploading files from the browser module. +- Introduces the `options.cloud` option. +- Stabilizes the previously experimental timers module as the `k6/timers` module. +- Brings JSON Web Key support to the `k6/experimental/webcrypto` module. + +## Breaking changes + +- [websockets#60](https://github.com/grafana/xk6-websockets/pull/60) allows manually setting the `name` tag, which also overwrites the `url` tag with the `name` value. This change makes it consistent with the logic that was implemented in k6 v0.41. Thanks, @mkadirtan for contributing! + +### Browser APIs to Async + +In future releases, we are going to be moving most of the synchronous browser APIs to asynchronous ones (promisifying them). We expect this will affect most of our users, so we are posting this upfront before making the change. Here are the reasons for making this large breaking change: + +1. Most browser APIs use some form of long-running IO operation (networking) to perform the requested action on the web browser against the website under test. We need to avoid blocking JavaScript's runtime event loop for such operations. +2. We're going to add more asynchronous event-based APIs (such as [page.on](https://github.com/grafana/xk6-browser/issues/1227)) that our current synchronous APIs would block. +3. To align with how developers expect to work with JavaScript APIs. +4. To have better compatibility with Playwright. + +You can find a list of all the APIs that we expect to convert to async in a comment in issue [browser#428](https://github.com/grafana/xk6-browser/issues/428#issuecomment-1964020837). + +Awaiting on something that’s not a thenable just returns that value, which means you can add the `await` keyword against APIs that will become async to future proof your test scripts. + +## New features + +### Add support for uploading files from the browser module [browser#1097](https://github.com/grafana/xk6-browser/pull/1097), [browser#1244](https://github.com/grafana/xk6-browser/pull/1244) + +You can now upload files using the available input forms on the website under test. The new API is `setInputFiles` which can be called from a `page`, `frame` or `elementHandle` types. It can upload one or more files encoded in the test script. To upload files from the local file system, work with the [experimental fs module](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/fs/). + +
+Expand to see the examples. + +For the following examples, we will use the HTML file: + +```html + + +
+ + +
+ + +``` + +Uploading a file can be achieved with the following script: + +```js +// Import the k6 encoder module. +import encoding from 'k6/encoding'; +... +export default async function () { + const page = browser.newPage(); + + await page.goto(url) + + // Encode and upload some data into a plain text file called test.txt. + page.setInputFiles('input[id="upload"]', { name: 'test.txt', mimetype: 'text/plain', buffer: encoding.b64encode('Hello World') }) + + // Click on the submit button on the form to upload the file. + const submitButton = page.locator('input[type="submit"]') + await Promise.all([page.waitForNavigation(), submitButton.click()]) + + page.close(); +} +``` + +Uploading multiple files can be done with the use of an array: + +```js +page.setInputFiles('input[id="upload"]', [ + { name: 'test.txt', mimetype: 'text/plain', buffer: encoding.b64encode('Hello World') }, + { + name: 'test.json', + mimetype: 'text/json', + buffer: encoding.b64encode('{"message": "Hello World"}'), + }, +]); +``` + +
+ +Thanks to @bandorko! :bow: :tada: + +### Introducing options.cloud [#3348](https://github.com/grafana/k6/pull/3348), [#3407](https://github.com/grafana/k6/pull/3407) + +In this release, we introduce a new way of defining cloud options. From now on, you can use `options.cloud` instead of `options.ext.loadimpact`. + +To migrate, you can move the `loadimpact` object to the root of the `options` object and rename it to `cloud`. For example: + +```javascript +export let options = { + ext: { + loadimpact: { + name: 'Legacy way of defining cloud options', + projectID: 12345, + }, + }, +}; + +export let options = { + cloud: { + name: 'Current way of defining cloud options', + projectID: 12345, + }, +}; +``` + +All scripts with legacy `options.ext.loadimpact` will continue to function as before. There's no planned sunset date for the legacy option, but we highly encourage using `options.cloud` going forward. For more details about cloud options, refer to [Cloud options](https://grafana.com/docs/grafana-cloud/k6/author-run/cloud-scripting-extras/cloud-options/). + +### Timers API becomes part of the k6 core [#3587](https://github.com/grafana/k6/pull/3587) + +With this release, the timers API is no longer experimental and can be imported as `k6/timers` instead of as `k6/experimental/timers`. The later will be supported until `v0.52.0`. + +You can also contribute to the discussion on making the current timer exports globally available in [#3589](https://github.com/grafana/k6/issues/3589), or just give it a :+1:. + +### JSON Web Key support in `k6/experimental/webcrypto` module [webcrypto#61](https://github.com/grafana/xk6-webcrypto/pull/61) + +The experimental webcrypto module now supports the JSON Web Key (JWK) format, using the `importKey` and `exportKey` methods. + +This allows you to import and export keys in the JWK format for the supported algorithms. + +```js +const generatedKey = await crypto.subtle.generateKey({ name: 'AES-CBC', length: '256' }, true, [ + 'encrypt', + 'decrypt', +]); + +const exportedKey = await crypto.subtle.exportKey('jwk', generatedKey); +``` + +## UX improvements and enhancements + +- [browser#1197](https://github.com/grafana/xk6-browser/pull/1197), [browser#1202](https://github.com/grafana/xk6-browser/pull/1202), [browser#1203](https://github.com/grafana/xk6-browser/pull/1203), [browser#1221](https://github.com/grafana/xk6-browser/pull/1221) adds the ability to upload screenshots to a remote location. +- [browser#1209](https://github.com/grafana/xk6-browser/pull/1209) adds a shadow DOM usage example. +- [browser#1233](https://github.com/grafana/xk6-browser/pull/1233) returns actionable errors for `evaluate` APIs. +- [browser#1228](https://github.com/grafana/xk6-browser/pull/1228), [browser#1232](https://github.com/grafana/xk6-browser/pull/1232), [browser#1235](https://github.com/grafana/xk6-browser/pull/1235) injects the `testRunId` into the `window.k6` object for external applications to query (for example, Grafana Faro). + +### Browser Context Isolation [browser#1112](https://github.com/grafana/xk6-browser/issues/1112) + +With this release, we have overhauled and (tremendously) improved the performance and stability of the browser module. It's now possible to run tests with a larger number of VUs concurrently without any performance issues. Previously, when running tests with multiple VUs concurrently, each VU's browser context would attach to the pages from the other VUs' browser contexts. This led to unexpected behavior and performance issues and, to an extent, reduced the module's capability to run multi-VU tests. + +## Bug fixes + +- [#3653](https://github.com/grafana/k6/pull/3653) fixes a connectivity issue with non-lowercase `options.hosts`. +- [browser#1215](https://github.com/grafana/xk6-browser/pull/1215) fixes a data race during logging that panics. +- [browser#1238](https://github.com/grafana/xk6-browser/pull/1238) fixes fill functionality for textarea. Thanks @bandorko for the fix! :bow: :tada: +- [browser#1242](https://github.com/grafana/xk6-browser/pull/1242) fixes XPath evaluation on `DocumentFragment`. + +## Maintenance and internal improvements + +- [browser#1164](https://github.com/grafana/xk6-browser/pull/1164), [browser#1166](https://github.com/grafana/xk6-browser/pull/1166), [browser#1171](https://github.com/grafana/xk6-browser/pull/1171), + [browser#1173](https://github.com/grafana/xk6-browser/pull/1173), [browser#1175](https://github.com/grafana/xk6-browser/pull/1175), [browser#1179](https://github.com/grafana/xk6-browser/pull/1179), + [browser#1183](https://github.com/grafana/xk6-browser/pull/1183), [browser#1186](https://github.com/grafana/xk6-browser/pull/1186), [browser#1188](https://github.com/grafana/xk6-browser/pull/1188), + [browser#1189](https://github.com/grafana/xk6-browser/pull/1189), [browser#1190](https://github.com/grafana/xk6-browser/pull/1190), [browser#1191](https://github.com/grafana/xk6-browser/pull/1191), + [browser#1193](https://github.com/grafana/xk6-browser/pull/1193), [browser#1163](https://github.com/grafana/xk6-browser/pull/1163), [browser#1205](https://github.com/grafana/xk6-browser/pull/1205), + [browser#1217](https://github.com/grafana/xk6-browser/pull/1217) refactors internals to improve stability. +- [browser#850](https://github.com/grafana/xk6-browser/pull/850), [browser#1211](https://github.com/grafana/xk6-browser/pull/1211), [browser#1212](https://github.com/grafana/xk6-browser/pull/1212), + [browser#1214](https://github.com/grafana/xk6-browser/pull/1214), [browser#1216](https://github.com/grafana/xk6-browser/pull/1216) refactors to work with errors.Join and sets the minimum Go version to 1.20. +- [browser#1220](https://github.com/grafana/xk6-browser/pull/1220) adds more logging. +- [browser#1112](https://github.com/grafana/xk6-browser/issues/1112) fixes deadlock issues when running multiple VUs, iterations, and Chrome instances. +- [browser#1246](https://github.com/grafana/xk6-browser/issues/1246) removes logging of in-flight requests when a request fails. +- [#3586](https://github.com/grafana/k6/pull/3586) fixes file traversal for the test. +- [#3588](https://github.com/grafana/k6/pull/3588) updates `codeql` GitHub action to v3. + +* [webcrypto#62](https://github.com/grafana/xk6-webcrypto/pull/62) fixes display error message in the console and does minor maintenance. +* [webcrypto#60](https://github.com/grafana/xk6-webcrypto/pull/60) leverages some of the k6 APIs to handle JavaScript operations. +* [webcrypto#59](https://github.com/grafana/xk6-webcrypto/pull/59) makes `newTestSetup` rely on k6's modulestest. +* [webcrypto#58](https://github.com/grafana/xk6-webcrypto/pull/58) addresses linter issues related to repeated static strings. +* [3633](https://github.com/grafana/k6/pull/3633) updates k6 dependencies. diff --git a/docs/sources/k6/v0.55.x/release-notes/v0.51.0.md b/docs/sources/k6/v0.55.x/release-notes/v0.51.0.md new file mode 100644 index 000000000..fb5a391dd --- /dev/null +++ b/docs/sources/k6/v0.55.x/release-notes/v0.51.0.md @@ -0,0 +1,295 @@ +--- +title: Version 0.51.0 release notes +menuTitle: v0.51.0 +description: The release notes for Grafana k6 version 0.51.0 +weight: 9996 +canonical: https://github.com/grafana/k6/blob/master/release%20notes/v0.51.0.md +--- + +# Version 0.51.0 release notes + +k6 `v0.51.0` is here 🎉! Some special mentions included in this release: + +- [A new experimental streams module](#introduction-of-k6experimentalstreams-module-3696) +- [New algorithms for WebCrypto module](#new-features-and-updates-of-webcrypto-api-support-3714) +- [Timers are globally available](#timers-globally-available-3589) + +## Breaking changes + +### Transition browser APIs to Async + +In the last release notes [we mentioned](https://github.com/grafana/k6/blob/master/release%20notes/v0.50.0.md#browser-apis-to-async) this breaking change, and we wanted to remind and update you on the plan. In the **next** release (v0.52.0), most of the synchronous browser APIs will be migrated to be asynchronous (promisifying them). We expect this will affect most if not all of our users. + +This breaking change will require you to add `await` in front of most of the browser module APIs. Without this `await` you will witness undocumented and unknown behavior during the runtime. To make the migration simpler we advise that you work with the latest [k6 type definitions](https://grafana.com/docs/k6/latest/set-up/configure-k6-intellisense/). + +You can find a list of all the APIs that we expect to convert to async in a comment in issue [browser#428](https://github.com/grafana/xk6-browser/issues/428#issuecomment-1964020837). + +Awaiting on something that’s not a thenable just returns that value, which means you can add the `await` keyword today on the APIs that will become async to future proof your test scripts. + +Here are the reasons for making this large breaking change: + +1. Most browser APIs use some form of long-running IO operation (networking) to perform the requested action on the web browser against the website under test. We need to avoid blocking JavaScript's runtime event loop for such operations. +2. We're going to add more asynchronous event-based APIs (such as [page.on](https://github.com/grafana/xk6-browser/issues/1227)) that our current synchronous APIs would block. +3. To align with how developers expect to work with JavaScript APIs. +4. To have better compatibility with Playwright. + +As a starting point, we have migrated a single API (the `tap` method), which you can find the details below that will help visualize the upcoming breaking changes. + +### Browser `Tap` is now an async method grafana/xk6-browser#1268 + +This release converts the `Tap` method in the `browser` module into an asynchronous method. This change is necessary to ensure that the method can be used in async contexts and to align with the rest of the browser module's planned asynchronous API. To use the `Tap` method, you must now add the `await` keyword before the method call. + +Affected components: + +- [`locator.tap`](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/browser/locator/tap/) +- [`page.tap`](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/browser/page/tap/) +- [`frame.tap`](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/browser/frame/) +- [`elementHandle.tap`](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/browser/elementhandle/) + +See the following example for how to use the `Tap` method after this change: + +**Before**: + +```javascript +import browser from 'k6/experimental/browser'; + +// ... + +export default function () { + // ... + page.tap(selector, { modifiers: ['Alt', 'Control', 'Meta', 'Shift'] }); + // ... +} +``` + +**After**: + +```javascript +import browser from 'k6/experimental/browser' + +// ... + +export default function () { + // ... + await page.tap(selector, { modifiers: ["Alt", "Control", "Meta", "Shift"] }); + // ... +} +``` + +### `k6/experimental/websockets` will not default `binaryType` to `"arraybuffer"' + +As part of the stabilization of the API it needs to become as close to the specification. + +Early in the development the idea of adding `Blob` support as part was deemed feature creep and was dropped in favor of going with only `"arraybuffer"`. But the specification defaults to returning binary responses as `Blob` - which was another thing that was changed. + +While adding `Blob` is still on our radar, moving the default is always going to be a breaking change that we need to do to align with the specification. + +For this release there is now a warning that will be printed if `binaryType` is not set to `"arraybuffer"` _and_ a binary response is received. The warning will go away when `binaryType` is set to `"arraybuffer"`. + +In the next release the warning will become an error. + +More info and place for discussion can be found in an [this issue](https://github.com/grafana/xk6-websockets/issues/67). + +### `k6/experimental/grpc` is no longer available [#3530](https://github.com/grafana/k6/pull/3530) + +As the last step of the graduation process for the experimental gRPC module, we completely removed the module. It is now fully integrated into the stable `k6/net/grpc` module. So, if you haven't done this yet, replace your imports from `k6/experimental/grpc` to `k6/net/grpc`. + +### Deprecations + +The following pull requests start the process to introduce breaking changes. They are currently starting to emit warning if their condition is hit, but they will turn to return errors in the future release. +It is recommended to use the suggested alternative, or to fix the script if you see the warning message. + +- [#3681](https://github.com/grafana/k6/pull/3681) Use of not-compliant `require` expressions. +- [#3680](https://github.com/grafana/k6/pull/3680) Modules resolution of modules not previously seen during the initialization phase. +- [#3676](https://github.com/grafana/k6/pull/3676) Working directory is set to the current location when the script is provided using stdin, instead of the root folder. +- [#3530](https://github.com/grafana/k6/pull/3671) Automagically resolve modules from cdnjs and github "URLs". + +## New features + +### Introduction of `k6/experimental/streams` module [#3696](https://github.com/grafana/k6/pull/3696) + +This release of k6 introduces the new `k6/experimental/streams` module, which partially supports the JavaScript +Streams API, focusing initially on the `ReadableStream` construct. + +With the `ReadableStream`, users can define and consume data streams within k6 scripts. This is particularly useful for +efficiently handling large datasets or for processing data sequentially in a controlled flow. + +
+ Expand to see an example of stream's usage + +The following example demonstrates creating and consuming a simple stream that emits numbers until it reaches a predefined limit: + +```javascript +import { ReadableStream } from 'k6/experimental/streams'; + +function numbersStream() { + let currentNumber = 0; + + return new ReadableStream({ + start(controller) { + const fn = () => { + if (currentNumber < 5) { + controller.enqueue(++currentNumber); + setTimeout(fn, 1000); + return; + } + + controller.close(); + }; + setTimeout(fn, 1000); + }, + }); +} + +export default async function () { + const stream = numbersStream(); + const reader = stream.getReader(); + + while (true) { + const { done, value } = await reader.read(); + if (done) break; + console.log(`received number ${value} from stream`); + } + + console.log('we are done'); +} +``` + +
+ +For more advanced examples, please head to the MDN Web Docs on the [Streams API](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API). + +#### Limitations + +Currently, users can define and consume readable streams. However, this release does not include support for byte readers +and controllers, nor does it include support the `tee`, `pipeTo`, and +`pipeThrough` methods of the `ReadableStream` object. + +### New features and updates of WebCrypto API support [#3714](https://github.com/grafana/k6/pull/3714) + +This release brings support for asymmetric cryptography to the `k6/experimental/webcrypto` module. We added support of the elliptic curves algorithms ECDH ([xk6-webcrypto#67](https://github.com/grafana/xk6-webcrypto/pull/67)) and ECDSA ([xk6-webcrypto#69](https://github.com/grafana/xk6-webcrypto/pull/69)) algorithms along with new import/export key formats like `spki` and `pkcs8`. + +One of the newly added operations is `deriveBits`, which allows parties to generate a unique shared secret by using shared public and non-shared private keys. + +
+ Expand to see an example of generating a shared secret for Alice and Bob. + +```javascript +import { crypto } from 'k6/experimental/webcrypto'; + +export default async function () { + // Generate a key pair for Alice + const aliceKeyPair = await crypto.subtle.generateKey( + { + name: 'ECDH', + namedCurve: 'P-256', + }, + true, + ['deriveKey', 'deriveBits'] + ); + + // Generate a key pair for Bob + const bobKeyPair = await crypto.subtle.generateKey( + { + name: 'ECDH', + namedCurve: 'P-256', + }, + true, + ['deriveKey', 'deriveBits'] + ); + + // Derive shared secret for Alice + const aliceSharedSecret = await deriveSharedSecret(aliceKeyPair.privateKey, bobKeyPair.publicKey); + + // Derive shared secret for Bob + const bobSharedSecret = await deriveSharedSecret(bobKeyPair.privateKey, aliceKeyPair.publicKey); + + // alice shared secret and bob shared secret should be the same + console.log('alice shared secret: ' + printArrayBuffer(aliceSharedSecret)); + console.log('bob shared secret: ' + printArrayBuffer(bobSharedSecret)); +} + +async function deriveSharedSecret(privateKey, publicKey) { + return crypto.subtle.deriveBits( + { + name: 'ECDH', + public: publicKey, + }, + privateKey, + 256 + ); +} + +const printArrayBuffer = (buffer) => { + const view = new Uint8Array(buffer); + return Array.from(view); +}; +``` + +
+ +The `sign` and `verify` operations got support for ECDSA algorithm. The `sign` operation allows you to sign a message with a private key, while the `verify` operation allows you to verify the signature with a public key. + +Other notable updates and fixes: + +- [xk6-webcrypto#68](https://github.com/grafana/xk6-webcrypto/pull/68) fixes a degradation for the sign/verify operations for HMAC algorithm. +- [xk6-webcrypto#75](https://github.com/grafana/xk6-webcrypto/pull/75), [xk6-webcrypto#76](https://github.com/grafana/xk6-webcrypto/pull/76) refactor webcrypto module to be thread-safe. +- [xk6-webcrypto#74](https://github.com/grafana/xk6-webcrypto/pull/74) adds JWK import/export support for ECDH and ECDSA. Refactors JWK import/export to use only go standard library. + +See [webcrypto's module documentation](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/webcrypto/) for more details. + +### Timers globally available [#3589](https://github.com/grafana/k6/pull/3589) + +`setTimeout`, `setInterval` and related clear functions have been part of the JavaScript ecosystem, probably for as long as it has existed. + +In the previous releases we stabilized and made them available through `k6/timers` module. While the module isn't going anywhere and might get more identifiers, `setTimeout` is usually used without importing it. For this reason it is now globally available along `clearTimeout`, `setInterval` and `clearInterval`. + +No code needs to be changed, but you no longer need to import `k6/timers` to use this functionality. + +## UX improvements and enhancements + +- [#3670](https://github.com/grafana/k6/issues/3670) adds the ability to [enable profiling](https://grafana.com/docs/k6/latest/using-k6/k6-options/reference/#profiling-enabled) via environment variable. Thanks @Bablzz for your contribution! +- [#3655](https://github.com/grafana/k6/pull/3655) clarifies the error message for the validation of scenario's name. +- [#3693](https://github.com/grafana/k6/pull/3693) adds a gRPC client's `asyncInvoke` method to the `k6/net/grpc` module. It's [a non-blocking version](https://grafana.com/docs/k6/latest/javascript-api/k6-net-grpc/client/client-async-invoke/) of the `invoke` method. +- [browser#1259](https://github.com/grafana/xk6-browser/pull/1259), [browser#1260](https://github.com/grafana/xk6-browser/pull/1260) adds errors to the traces that the browser module generates. + +## Bug fixes + +- [#3708](https://github.com/grafana/k6/pull/3708) denies access to `execution.test.options` from Init context. +- [#3672](https://github.com/grafana/k6/pull/3672) picks the correct value when [SystemTags](https://grafana.com/docs/k6/latest/using-k6/k6-options/reference/#system-tags) are set via the `k6_SYSTEM_TAGS` environment variable. +- [#3657](https://github.com/grafana/k6/pull/3657) fixes a panic when `mappings` field is empty in the provided SourceMap. +- [#3717](https://github.com/grafana/k6/pull/3717) returns a correct line number when an inlined SourceMap is used. +- [browser#1261](https://github.com/grafana/xk6-browser/pull/1261) fixes dispose context canceled errors. +- [browser#1254](https://github.com/grafana/xk6-browser/pull/1254) fixes an indefinite wait when testing websites with iframes. +- [browser#1291](https://github.com/grafana/xk6-browser/pull/1291) fixes a panic on dispose of resources during a navigation. + +## Maintenance and internal improvements + +- [#3663](https://github.com/grafana/k6/pull/3663), [#3673](https://github.com/grafana/k6/pull/3673) updates several dependencies. +- [#3674](https://github.com/grafana/k6/pull/3674) updates the Go version, now k6 binary is built and tested using Go 1.22. +- [#3688](https://github.com/grafana/k6/pull/3688) updates the link to installation instructions for `golangci-lint` in the contribution guide. Thanks @yomek33 for your contribution! +- [browser#1262](https://github.com/grafana/xk6-browser/pull/1262) fixes a flaky test. +- [browser#1264](https://github.com/grafana/xk6-browser/pull/1264) removes unimplemented APIs. + +## Future plans + +### Use Blob as default value for WebSocket.binaryType + +As the changes in documentation mention, [`binaryType`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/binaryType) was by default set to `arraybuffer`, now instead it is `""` (empty string). +In a not so remote future, instead, we expect to introduce and use Blob object. + +### WebCrypto graduation + +WebCrypto got more features in the current release, and we expect to continue its expansion in the next iterations. Reaching a wider coverage will push WebCrypto module to being graduated as a stable module. + +### Streams API + +In the not so distant future, we have plans to start using the Streams API in existing modules. Our first iteration being adding a `.readable` property to the already existing [fs.File](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/fs/file) object. + +### Improve user experience for Cloud related commands + +In the near future, we intend to reiterate on `k6 cloud` and related commands to create a simplified and more ergonomic user experience. + +### Remove experimental timers module + +The `k6/experimental/timers` module is now part of the stable k6 API as `k6/timers` and via the globally available functions. The next release will make the experimental import no longer available. diff --git a/docs/sources/k6/v0.55.x/release-notes/v0.52.0.md b/docs/sources/k6/v0.55.x/release-notes/v0.52.0.md new file mode 100644 index 000000000..a87a10b5a --- /dev/null +++ b/docs/sources/k6/v0.55.x/release-notes/v0.52.0.md @@ -0,0 +1,145 @@ +--- +title: Version 0.52.0 release notes +menuTitle: v0.52.0 +description: The release notes for Grafana k6 version 0.52.0 +weight: 9995 +canonical: https://github.com/grafana/k6/blob/master/release%20notes/v0.52.0.md +--- + +# Version 0.52.0 release notes + +k6 `v0.52.0` is here 🎉! Some special mentions included in this release: + +- [We've switched to our own fork of `goja` named `sobek`](#switch-goja-to-our-own-fork-named-sobek-3775). +- [Panics are no longer captured](#panics-are-no-longer-being-captured-3777). +- [We've added experimental support for TypeScript and ES6+](#experimental-support-for-typescript-and-es6-using-esbuild-3738). +- `k6/browser` has [graduated from an experimental module](#k6browser-has-graduated-from-an-experimental-module-3793), and now has a [fully Async API](#k6browser-has-now-a-fully-async-api-browser428). + +## Breaking changes + +### Switch `goja` to our own fork named `sobek` [#3775](https://github.com/grafana/k6/pull/3775) + +To accelerate the development speed and bring ECMAScript Modules (ESM) support to k6 earlier (https://github.com/grafana/k6/issues/3265), +we have decided to create a fork of the [`goja`](https://github.com/dop251/goja/) project under the Grafana GitHub organization, +named [`sobek`](https://github.com/grafana/sobek). + +Starting on this release, k6 (and its extensions) now use `sobek` instead of the original `goja`, for all (of the +publicly exposed parts of the API) except for a couple of packages that are only used internally by k6. + +All k6 extensions linked in the docs have had a PR for this transition opened, as explained in this [comment](https://github.com/grafana/k6/issues/3773#issuecomment-2182113677). Any extension author who hasn't gotten a PR can follow the same steps. + +Find further details in [#3772](https://github.com/grafana/k6/issues/3772) and [#3773](https://github.com/grafana/k6/issues/3773). + +### Panics are no longer being captured [#3777](https://github.com/grafana/k6/pull/3777) + +Since this release, Go panics are no longer being captured by k6. This means that if a panic occurs while running a test, +the k6 process will crash, and the panic stack trace will be printed to the console. + +We decided to change this behavior because it's something that was left from the past as a safeguard, but it's not as +good as it might seem. For most cases with multiple goroutines/async, it's not enough and also makes a bunch of potential +bugs seem like less of an issue. + +Thus, this will help us to identify and fix bugs earlier, improve the overall stability of k6, and +most likely make the experience of developing k6 extensions friendlier. + +### `lib.State` no longer has `Group` [#3750](https://github.com/grafana/k6/pull/3750) + +As the result of refactoring the implementation of `group` and `check` methods, in order to decouple them, and thus +enable other future improvements, the `lib.State` object no longer has a `Group` field. + +This change should not affect most users, except for a couple of extensions, for which the use of `Group` was +already questionable: + +- `xk6-fasthttp` +- `xk6-g0` + +### Other breaking changes + +- [#3797](https://github.com/grafana/k6/pull/3797) starts using `-` as a special value for `--archive-out` to output the archive to stdout. +- [browser#1318](https://github.com/grafana/xk6-browser/pull/1318) makes the `Mouse.up` and `Mouse.down` methods no longer take x and y coordinates. Instead, they dispatch events on the current mouse position. + +## New features + +### Experimental support for TypeScript and ES6+ using esbuild [#3738](https://github.com/grafana/k6/pull/3738) + +This release of k6 introduces experimental support for TypeScript and ES6+ using esbuild, thanks to a new +[compatibility mode](https://grafana.com/docs/k6/latest/using-k6/javascript-typescript-compatibility-mode/) named `experimental_enhanced`. + +```sh +k6 run --compatibility-mode=experimental_enhanced script.js +``` + +With this new compatibility mode, the test source code is transformed using esbuild instead of Babel, which also means +that source files with the extension _".ts"_ are loaded by esbuild's TypeScript loader, which results in partial +TypeScript support: it removes the type information but doesn't provide type safety. + +### `k6/browser` has graduated from an experimental module [#3793](https://github.com/grafana/k6/pull/3793) + +The browser module is now available as `k6/browser` instead of `k6/experimental/browser`. The previous `k6/experimental/browser` module will be removed on September 23rd, 2024. Refer to [the migration guide](https://grafana.com/docs/k6/latest/using-k6-browser/migrating-to-k6-v0-52/) for more information on how to update your scripts. + +### `k6/browser` has now a fully Async API [browser#428](https://github.com/grafana/xk6-browser/issues/428) + +This release introduces a fully Async API for the `k6/browser` module. This means that nearly all the methods in the module now return promises. This change is part of the ongoing effort to make the browser module more user-friendly and easier to use. Please see [the browser documentation](https://grafana.com/docs/k6/latest/javascript-api/k6-browser) for more information on how to use the new Async API. + +Related Changes: + +- [browser#1310](https://github.com/grafana/xk6-browser/pull/1310), [browser#1311](https://github.com/grafana/xk6-browser/pull/1311), [browser#1312](https://github.com/grafana/xk6-browser/pull/1312), [browser#1316](https://github.com/grafana/xk6-browser/pull/1316), [browser#1328](https://github.com/grafana/xk6-browser/pull/1328), [browser#1337](https://github.com/grafana/xk6-browser/pull/1337), [browser#1367](https://github.com/grafana/xk6-browser/pull/1367), [browser#1366](https://github.com/grafana/xk6-browser/pull/1366), [browser#1314](https://github.com/grafana/xk6-browser/pull/1314), [browser#1332](https://github.com/grafana/xk6-browser/pull/1332), [browser#1323](https://github.com/grafana/xk6-browser/pull/1323), [browser#1355](https://github.com/grafana/xk6-browser/pull/1355), [browser#1348](https://github.com/grafana/xk6-browser/pull/1348), [browser#1364](https://github.com/grafana/xk6-browser/pull/1364) Migrates `Browser`, `BrowserContext`, `ElementHandle`, `Frame`, `JSHandle`, `Keyboard`, `Locator`, `Mouse`, `Page`, `Request`, `Response` APIs to async. + +## UX improvements and enhancements + +- [#3740](https://github.com/grafana/k6/pull/3740) enables k6 extensions to initialize `ReadableStream` objects from Go code (`io.Reader`). +- [#3798](https://github.com/grafana/k6/pull/3798) adjusts a severity level of a log message from `warn` to `debug` for cases when k6 can't detect the terminal's size. +- [#3797](https://github.com/grafana/k6/pull/3797) makes it possible to output the archive to stdout by using `-` as the `--archive-out`. Thanks to @roobre! :bow: :tada: +- [browser#1370](https://github.com/grafana/xk6-browser/issues/1370) makes the `GetAttribute` method now return `false` when the attribute is missing, making it easier to check for the presence of an attribute. +- [browser#1371](https://github.com/grafana/xk6-browser/issues/1371) makes the `TextContent` method now return `false` when the element's text content cannot be grabbed (like a JS `document`), making it easier to check for the presence of text content. +- [browser#1376](https://github.com/grafana/xk6-browser/pull/1376) makes `Request.headerValue` and `Response.headerValue` to be case-insensitive. +- [browser#1368](https://github.com/grafana/xk6-browser/pull/1368) enhances `await` usage in Javascript examples. +- [browser#1326](https://github.com/grafana/xk6-browser/pull/1326) adds forgotten `BrowserContext.browser` and `Page.context` mappings. +- [browser#1360](https://github.com/grafana/xk6-browser/pull/1360), [browser#1327](https://github.com/grafana/xk6-browser/pull/1327), [browser#1335](https://github.com/grafana/xk6-browser/pull/1335), [browser#1365](https://github.com/grafana/xk6-browser/pull/1365), [browser#1313](https://github.com/grafana/xk6-browser/pull/1313), [browser#1322](https://github.com/grafana/xk6-browser/pull/1322), [browser#1330](https://github.com/grafana/xk6-browser/pull/1330), [browser#1343](https://github.com/grafana/xk6-browser/pull/1343), [browser#1345](https://github.com/grafana/xk6-browser/pull/1345), [browser#1352](https://github.com/grafana/xk6-browser/pull/1352) turns the `Browser`, `BrowserContext`, `ElementHandle`, `JSHandle`, `Keyboard`, `Mouse`, `Locator`, and `Page` types' panics into errors for stability and better error handling. + +## Bug fixes + +- [#3774](https://github.com/grafana/k6/pull/3774) fixes a `require` warning for those tests using the stdin. +- [#3776](https://github.com/grafana/k6/pull/3776) fixes a panic caused by passing an undefined handler to timers. +- [#3779](https://github.com/grafana/k6/pull/3779) fixes a panic caused by registering an undefined handler in gRPC streams. +- [xk6-websockets#73](https://github.com/grafana/xk6-websockets/pull/73) fixes a panic caused by registering an undefined handler in WebSockets. +- [browser#1369](https://github.com/grafana/xk6-browser/pull/1369) improves `valueFromRemoteObject` `null` detection by returning a Go `nil` instead of `"null"` as a `string`. +- [browser#1386](https://github.com/grafana/xk6-browser/pull/1386) correctly handles empty string flags that don't have a value. +- [browser#1380](https://github.com/grafana/xk6-browser/pull/1380) ensures that `JSHandle.evaluate` and `JSHandle.evaluateHandle` both set themselves as the first argument. +- [browser#1346](https://github.com/grafana/xk6-browser/pull/1346) fixes an IFrame panic ("we either navigate top level or have old version of the navigated frame") that happens during navigation. +- [browser#1349](https://github.com/grafana/xk6-browser/pull/1349), [browser#1354](https://github.com/grafana/xk6-browser/pull/1354) fixes `Request` mappings. +- [browser#1334](https://github.com/grafana/xk6-browser/pull/1334) fixes an issue where clicking on a link that opens a new tab never navigates to the href link. +- [browser#1318](https://github.com/grafana/xk6-browser/pull/1318) fixes the `Mouse.move` to correctly dispatch a `down` event. +- [browser#1301](https://github.com/grafana/xk6-browser/pull/1301) fixes an error that occurs when working with a second tab and navigating to a URL. +- [browser#1387](https://github.com/grafana/xk6-browser/pull/1387) fixes a panic when the new document or request is missing. + +## Maintenance and internal improvements + +- [#3752](https://github.com/grafana/k6/pull/3752), [#3767](https://github.com/grafana/k6/pull/3767), [#3770](https://github.com/grafana/k6/pull/3770), [#3780](https://github.com/grafana/k6/pull/3780), [#3782](https://github.com/grafana/k6/pull/3782), [#3795](https://github.com/grafana/k6/pull/3795) updates several dependencies. +- [#3786](https://github.com/grafana/k6/pull/3786), [#3787](https://github.com/grafana/k6/pull/3787), [#3788](https://github.com/grafana/k6/pull/3788), [#3789](https://github.com/grafana/k6/pull/3789), [#3803](https://github.com/grafana/k6/pull/3803) updates experimental modules. +- [#3749](https://github.com/grafana/k6/pull/3749) stops printing `goja` stack traces on panics, which has been empty since a while. +- [#3760](https://github.com/grafana/k6/pull/3760) fixes race conditions in ramping-vus tests. +- [#3769](https://github.com/grafana/k6/pull/3769) removes a linter setting no longer used. +- [#3800](https://github.com/grafana/k6/pull/3800) adds test coverage for outputting the archive to stdout. +- [browser#1298](https://github.com/grafana/xk6-browser/pull/1298) splits browser mappings into separate files for better organization and maintainability. +- [browser#1321](https://github.com/grafana/xk6-browser/pull/1321) adds test helpers for async VU execution to reduce the boilerplate in the tests. +- [browser#1357](https://github.com/grafana/xk6-browser/pull/1357) updates `Group` and `Tag` usage for the latest k6. +- [browser#1361](https://github.com/grafana/xk6-browser/pull/1361) precalculates the browser version information to prevent I/O calls when the `version` and `userAgent` methods are called. This change allows the browser module to expose these methods as a sync API to be consistent with the Playwright's API. +- [browser#1377](https://github.com/grafana/xk6-browser/pull/1377) uses the `goja` fork called `sobek` in the browser module. +- [browser#1373](https://github.com/grafana/xk6-browser/pull/1373) provides sync and async APIs within the browser module for making the migration to the async API easier. + +## Roadmap + +### Native ECMAScript modules + +As mentioned above, the k6 team has forked [`goja`](https://github.com/dop251/goja/) into [`sobek`](https://github.com/grafana/sobek) and is currently working on [native ECMAScript modules support](https://github.com/grafana/k6/issues/3265). The current work in progress can be found in this [PR](https://github.com/grafana/k6/pull/3456) and any feedback is welcome. + +As part of that there likely will be some breaking changes due to the current not native support allowing stuff that shouldn't work. Like for example mixing [CommonJS](https://en.wikipedia.org/wiki/CommonJS) and ESM in the same file. Which is for example why we have added a [warning](https://github.com/grafana/k6/pull/3807) that it won't work. + +Support across multiple files is _also_ not standard but due to amount of users that seems to be mixing them _across_ files a lot of work has been done to support it. It is still likely that in the future warnings and potentially at some point future breaking changes will be enacted. + +### OpenTelemetry metrics output + +We're also happy to share that this release cycle, we've been working on [xk6-output-opentelemetry](https://grafana.com/docs/k6/latest/results-output/real-time/opentelemetry/), a k6 output extension that allows you to send k6 metrics to OpenTelemetry-compatible backends. We're looking for feedback from the community. If you're interested, please try it and let us know [via the extension repository](https://github.com/grafana/xk6-output-opentelemetry/issues)! + +Depending on the feedback, we plan to include this extension as an experimental output in the next k6 release. diff --git a/docs/sources/k6/v0.55.x/release-notes/v0.53.0.md b/docs/sources/k6/v0.55.x/release-notes/v0.53.0.md new file mode 100644 index 000000000..fc006b2c3 --- /dev/null +++ b/docs/sources/k6/v0.55.x/release-notes/v0.53.0.md @@ -0,0 +1,322 @@ +--- +title: Version 0.53.0 release notes +menuTitle: v0.53.0 +description: The release notes for Grafana k6 version 0.53.0 +weight: 9994 +canonical: https://github.com/grafana/k6/blob/master/release%20notes/v0.53.0.md +--- + +# Version 0.53.0 release notes + +k6 `v0.53.0` is here 🎉! This release includes: + +- Native ECMAScript modules support +- New experimental OpenTelemetry metrics output +- Blob support in experimental websockets module +- Consolidate cloud features and commands under `k6 cloud` +- Breaking change: remove magic URL resolutions + +## Breaking changes + +### Require is now specification compliant and always resolves based on the file it is written in [#3534](https://github.com/grafana/k6/issues/3534) + +The `require` function in k6 used to resolve identifiers based on the current "root of execution" (more on that later). In a lot of cases, that aligns with the file the `require` is written in or a file in the same folder, which leads to the same result. In a small subset of cases, this isn't the case. + +In every other implementation, and more or less by the CommonJS specification, `require` should always be relative to the file it is written in. + +This also aligns with how ESM and dynamic `import` also work. In order to align with them `require` now uses the same underlying implementation. + +There was a warning message for the last 2 releases trying to tease out cases where that would be problematic. + +
+ "root of execution" explanation + +This is very much an implementation detail that has leaked and likely a not intended one. + +Whenever a file is `require`-ed it becomes the "root of execution", and both `require` and `open` become relative to it. Once the `require` finishes, the previous "root of execution" gets restored. Outside of the `init` context execution, the main file is the "root of execution". + +Example: + +Have 3 files: +main.js + +```javascript +const s = require('./A/a.js'); +if (s() != 5) { + throw 'Bad'; +} +module.exports.default = () => {}; // just for k6 to not error +``` + +/A/a.js: + +```javascript +module.exports = function () { + return require('./b.js'); +}; +``` + +/A/b.js + +```javascript +module.exports = 5; +``` + +In this example when `require` is called in `/A/a.js` the `main.js` is once again the "root of execution". If you call the function in `/A/a.js` just after defining it though, it will work as expected. + +
+ +You can use the newly added `import.meta.resolve()` function if you want to create a path that is relevant to the currently calling module. That will let you call it outside of a helper class and provide the path to it. Refer to [docs](https://grafana.com/docs/k6/latest/javascript-api/import.meta/resolve/) for more details. + +### ECMAScript Modules (ESM) Native Support related breaking changes + +As part of the ESM native support implementation, two common broken patterns in the ecosystem became apparent. + +One is arguably a developer experience improvement, and the other is a consequence of the previous implementation. + +#### Mixing CommonJS and ESM + +Previously, k6 used a transpiler (Babel) internally to transpile ESM syntax to CommonJS. That led to all code always being CommonJS, and if you had CommonJS next to it, Babel would not complain. + +As k6 (or the underlying JS VM implementation) did not understand ESM in itself and that CommonJS is a 100% during execution feature, this was not easy to detect or prevent. + +We added a [warning](https://github.com/grafana/k6/pull/3807) in v0.52.0 to give users time for migration. + +To fix this - all you need is to stick to either CommonJS or ESM within each file. + +
+ Code examples and proposed changes + +```javascript +import { sleep } from "k6"; + +module.exports.default = func() { ...} +``` + +In the example above both ESM and CommonJS are used in the same file. + +You can either replace: + +```javascript +module.exports.default = func() {} +``` + +With the ESM syntax: + +```javascript +export default func() {} +``` + +Or replace: + +```javascript +import { sleep } from 'k6'; +``` + +With CommonJS: + +```javascript +const sleep = require('k6').sleep; +``` + +
+ +#### Imported identifier that can't be resolved are now errors + +Previous to this, if you were using the ESM syntax and imported the `foo` identifier, but the exporting file didn't export it, there wouldn't be an error. + +bar.js: + +```javascript +export const notfoo = 5; +``` + +main.js + +```javascript +import { foo } from './bar.js'; +export default function () { + foo.bar(); // throws exception here +} +``` + +The example would not error out, but when it is accessed, there would be an exception as `foo` would be `undefined`. + +With native ESM support, that is an error as defined by the specification and will occur sooner. + +This arguably improves UX/DX, but we have reports that some users have imports like this but do not use them. So, they wouldn't be getting exceptions, but they would now get errors. + +The solution, in this case, is to stop importing the not exported identifiers. + +### No more "magic" URL resolution + +For a long time, k6 has supported special _magic_ URLs that aren't really that. + +Those were URLs without a scheme that: + +1. Started with `github.com`, and if pasted to a browser won't open to a file. Their appeal was that you can more easily write them by hand if you know the path within a GitHub repo. +2. Started with `cdnjs.com`, and if pasted to a browser will open a web page with all the versions of the library. The appeal here is that you will get the latest version. + +Both of them had problems though. + +The GitHub ones seemed to have never been used by users, likely because you need to guess what the path should look like, and you can always just go get a real URL to the raw file. + +While the cdnjs ones have some more usage, they are both a lot more complicated to support, as they require multiple requests to figure out what needs to be loaded. They also change over time. In addition the only known use at the moment is based on a very old example from an issue and it is even pointing to concrete, old version, of a library. + +Given that this can be done with a normal URL, we have decided to drop support for this and have warned users for the last couple of versions. + +### Deprecated `k6/experimental/tracing` in favor of a JavaScript implementation + +`k6/experimental/tracing` is arguably not very well named, and there is a good chance we would like to use the name for actual trace and span support within k6 in the future. + +On top of that it can now be fully supported in js code, which is why [http-instrumentation-tempo +](https://grafana.com/docs/k6/latest/javascript-api/jslib/http-instrumentation-tempo/) was created. + +The JavaScript implementation is a drop-in replacement, so all you need to do is replace `k6/experimental/tracing` with `https://jslib.k6.io/http-instrumentation-tempo/1.0.0/index.js`. + +The module is [planned to be removed in v0.55.0](https://github.com/grafana/k6/pull/3855), planned for November 11th, 2024. + +### Experimental websockets now require `binaryType` to be set to receive binary messages + +As part of the stabilization of the `k6/experimental/websockets` we need to move the default value of `binaryType` to `blob`. It was previously `arraybuffer` and since the last version there was a warning that it needs to be set in order for binary messages to be received. + +That warning is now an error. + +In the future we will move the default value to `blob` and remove the error. + +## New features + +The new features include: + +- Native ESM support, which also brings some quality of life JavaScript features +- Blob support in the experimental websockets module +- Experimental OpenTelemetry metrics output +- Consolidating cloud related commands and features under `k6 cloud` + +### Native ESM support [#3456](https://github.com/grafana/k6/pull/3456) + +With this feature k6 is now ES6+ compliant natively. Which means (asterisk free) support for [the spread operator with object](https://github.com/grafana/k6/issues/824), [private class fields](https://github.com/grafana/k6/issues/2887) and [optional chaining](https://github.com/grafana/k6/issues/2168) + +But also faster startup times, more consistent errors and easier addition of features as we now only need to add them to Sobek instead of also them being supported in the internal Babel. + +
+ History of compatibility mode and ECMAScript specification compliance + +Some history: More than 6 years ago k6 started using core-js and babel to get ES6+ features. core-js is a implementation of a lot of the types and their features such as [`String.prototype.matchAll`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll) among other things, and Babel gets one piece of code that uses some syntax and returns a piece of code doing the same thing (mostly) but with different syntax. Usually with the idea of supporting newer syntax but returning code that can run on runtimes which only support old syntax. + +This is great, but it means that: + +1. For core-js each VU needs to run a bunch of JS code each initialization so it can polyfill everything that is missing +2. Babel needs to be parsed and loaded and then given files to transpile on each start. + +Both of those aren't that big problems usually, but the runtime k6 uses is fairly fast, but isn't V8. What it lacks in speed it gets back in being easy to interact with from Go, the language k6 is written in. + +But it means that now on each start it needs to do a bunch of work that adds up. + +So long time ago for people who would want to not have to do this we added [compatibility-mode=base](https://github.com/grafana/k6/pull/1206). This allowed you to potentially not use this features and get a big speedup. Or use them outside of k6 and likely still get significant speed up if you cut down on it. + +At the same time the author and maintainer of the JS runtime we used (goja) did implement a _big_ portion of what we were missing from core-js and also Babel. After some experiments to cut down the core-js we import we ended up contributing back the remaining parts and dropping the whole library. Which lead to 5 times reduction of memory per VU for simple scripts. And even for fairly complicated ones. + +With this in mind we did try to cut down Babel as well and contribute back the simpler things it was used for. This over the years lead to small pieces of what Babel did being moved to goja and then disabled in Babel. Some of those were just easy wins, some of those were things that had very bad pathological cases where using a particular syntax made transpilation times explode. + +In all of that work there always were small (or not so small) breaking changes due to many factors - sometimes our new implementation was slightly wrong and we needed to fix, sometimes more than what was in the standard was enabled in core-js or Babel, sometimes the standard changed on those. And sometimes the implementation in Babel or core-js wasn't as full and didn't account for all corner cases. + +ECMAScript Modules(ESM) is the last such feature that Babel was used for. It also happens to be likely the one _most_ people used, due to the fact that it is the standard way to reuse code and import libraries. + +While the work on this feature started over 2 years ago, it both depended on other features that weren't there yet, but also interacts with more or less every other feature that is part of the ECMAScript standard. + +Along the way there were many internal refactors as well as additional tests to make certain we can be as backwards compatible as possible. But there also ended up being things that just weren't going to be compatible, like the listed breaking changes. + +
+ +After ESM now being natively supported, compatibility-mode `base` vs `extended` has only 1 feature difference - aliasing `global` to `globalThis` to make it a bit more compatible with (old) Node.js. There is ongoing [discussion](https://github.com/grafana/k6/issues/3864) if that as well should be removed. + +For the purposes of having less intrusive changes and shipping this earlier a few things have not been implemented in k6. That includes top-level-await and dynamic import support. Both of them are likely to land in the next version. + +### `import.meta.resolve()` gets an URL from a relative path the same way `import` or `require` does [#3873](https://github.com/grafana/k6/pull/3873) + +As part of the move to ESM a lot of cases where k6 currently do not resolve the same relative path to the same file were found. Some of those were fixed - as those in `require`, but others haven't. + +It also became apparent some users do use the relativity of `require`, but also `open`. As we move to make this consistent among uses, we decided to let users have a better transition path forward. + +Using `import.meta.resolve` will give you just a new URL that can be used in all functions and it will give you the same result. + +`import.meta.resolve` uses the same algorithm and relativity as ESM import syntax. Refer to [docs](https://grafana.com/docs/k6/latest/javascript-api/import.meta/resolve/) for more details. + +### Blob support in the experimental websockets module [grafana/xk6-websockets#74](https://github.com/grafana/xk6-websockets/pull/74) + +In order to support the default `WebSocket.binaryType` type as per spec (`"blob"`), we have added support for the [`Blob` interface](https://developer.mozilla.org/en-US/docs/Web/API/Blob) as part of the features included in the `xk6-websockets` module. + +So, from now on it can be used with `import { Blob } from "k6/experimental/websockets";`. In the future, apart from graduating this module to stable, we might also want to expose the `Blob` interface globally (no imports will be required). But for now, please remind that its support is still experimental, as the entire module is. Refer to the [docs](https://grafana.com/docs/k6/latest/javascript-api/k6-experimental/websockets/blob/) for more details. + +### Experimental OpenTelemetry Output [#3834](https://github.com/grafana/k6/pull/3834) + +This release introduces a new experimental output for OpenTelemetry. This allows users to send k6 metrics to any OpenTelemetry-compatible backends. More details and usage examples can be found in the [documentation](https://grafana.com/docs/k6/latest/results-output/real-time/opentelemetry/). + +To output metrics to OpenTelemetry, use the `experimental-opentelemetry` output option: + +```bash + k6 run -o experimental-opentelemetry examples/script.js +``` + +If you have any feedback or issues, please let us know directly in [the extension repository](https://github.com/grafana/xk6-output-opentelemetry/issues). + +### Consolidating cloud features under `k6 cloud` [#3813](https://github.com/grafana/k6/pull/3813) + +This release introduces the first iteration of the revamped cloud-related commands under the `k6 cloud` command, featuring two new subcommands: + +- `k6 cloud login`: replaces `k6 login cloud` for authenticating with the cloud service. It supports token-based authentication only. The previous authentication method using email and password will still be available through the legacy `k6 login cloud` command, which is now deprecated and will be removed in a future release (no removal date set yet). + +- `k6 cloud run`: is the new official way to run k6 on the cloud service, serving as an alternative to the existing `k6 cloud` command. The `k6 cloud` command will remain available for a few more versions but will eventually function only as a wrapper for all cloud-related commands, without any direct functionality. + +## UX improvements and enhancements + +- [#3783](https://github.com/grafana/k6/pull/3783) Set correct exit code on invalid configurations. Thank you @ariasmn! +- [#3686](https://github.com/grafana/k6/pull/3868) Adjust logging of the executor lack of work. Thank you @athishaves! + +## Bug fixes + +- [#3746](https://github.com/grafana/k6/pull/3746) Fix tags for metrics from gRPC streams. Thank you @cchamplin! +- [#3845](https://github.com/grafana/k6/pull/3845) Fix logging to file sometimes missing lines. Thank you @roobre! +- [browser#1391](https://github.com/grafana/xk6-browser/pull/1391) Fix race conditions in internal event handling. + +## Maintenance and internal improvements + +- [#3792](https://github.com/grafana/k6/pull/3792), [#3817](https://github.com/grafana/k6/pull/3817), [#3863](https://github.com/grafana/k6/pull/3863) Finalize moving to Sobek, our fork of goja. +- [#3815](https://github.com/grafana/k6/pull/3815), [#3840](https://github.com/grafana/k6/pull/3840), [#3821](https://github.com/grafana/k6/pull/3821), [#3836](https://github.com/grafana/k6/pull/3836), [#3840](https://github.com/grafana/k6/pull/3840), [#3844](https://github.com/grafana/k6/pull/3844), [#3821](https://github.com/grafana/k6/pull/3821) Update dependencies. +- [#3830](https://github.com/grafana/k6/pull/3830), [#3831](https://github.com/grafana/k6/pull/3831), [#3862](https://github.com/grafana/k6/pull/3862) Refactoring and cleanup around ESM PR. +- [browser#1389](https://github.com/grafana/xk6-browser/pull/1389) Move deserialization of `BrowserContextOptions` into mapping layer. +- [#3841](https://github.com/grafana/k6/pull/3841) Add browser and cloud support information to the README. Thank you @sniku! +- [#3843](https://github.com/grafana/k6/pull/3837), [#3847](https://github.com/grafana/k6/pull/3847) `k6/experimental/timers` deprecation warning updates. +- [#3851](https://github.com/grafana/k6/pull/3851) Simplify gRPC streams metrics tags tests. +- [#3870](https://github.com/grafana/k6/pull/3870) Update tests to work with go1.22. + +## Roadmap + +### Future breaking changes + +#### Experimental browser module removal + +In the previous release, the browser module graduated from experimental to stable. The `k6/experimental/browser` module will be removed in `v0.54.0`. To keep your scripts working you need to [migrate to the `k6/browser` module](https://grafana.com/docs/k6/latest/using-k6-browser/migrating-to-k6-v0-52/). + +#### Experimental timers module removal + +The experimental timers module has been deprecated for a few versions. It both has a stable import path `k6/timers`, but also all of it's current exports are available globally. + +In the next version `v0.54.0` the experimental timers module will be removed. + +#### Experimental tracing module removal + +The experimental tracing module is deprecated in this version. In two versions(`v0.55.0`) the experimental module will be removed. + +To keep your scripts working you need to [migrate to http-instrumentation-tempo jslib](https://grafana.com/docs/k6/latest/javascript-api/jslib/http-instrumentation-tempo/#migration-from-k6experimentaltracing). + +#### StatsD removal + +In this release, we also fixed the version where we will remove the StatsD output. The StatsD output [is going to be removed in the `v0.55.0`](https://github.com/grafana/k6/pull/3849) release. If you are using the StatsD output, please consider migrating to the extension [LeonAdato/xk6-output-statsd](https://github.com/LeonAdato/xk6-output-statsd). + +#### Potentially dropping `global` from `extended` compatibility-mode + +Currently `global` is aliased to `globalThis` when `extended` compatibility-mode is used. This is currently the only difference with the `base` compatibility-mode. + +Given that this seems to have very low usage it might be dropped in the future. See the [issue](https://github.com/grafana/k6/issues/3864) for more info or if you want to comment on this. diff --git a/docs/sources/k6/v0.55.x/release-notes/v0.54.0.md b/docs/sources/k6/v0.55.x/release-notes/v0.54.0.md new file mode 100644 index 000000000..8c13ecf31 --- /dev/null +++ b/docs/sources/k6/v0.55.x/release-notes/v0.54.0.md @@ -0,0 +1,326 @@ +--- +title: Version 0.54.0 release notes +menuTitle: v0.54.0 +description: The release notes for Grafana k6 version 0.54.0 +weight: 9993 +canonical: https://github.com/grafana/k6/blob/master/release%20notes/v0.54.0.md +--- + +# Version 0.54.0 release notes + +k6 `v0.54.0` is here 🎉! This release includes: + +- A new experimental CSV module +- New `k6 cloud` commands for local execution and uploading script files +- New ECMAScript features +- Updated logo and branding + +## Breaking changes + +- [#3913](https://github.com/grafana/k6/pull/3913) changes the mapping of Golang's `math/big.Int` type to `bigint` type in k6. +- [#3922](https://github.com/grafana/k6/pull/3922) removes `lib.Min` and `lib.Max` from k6's Go API, which could affect custom extensions that rely on these functions. +- [#3838](https://github.com/grafana/k6/pull/3838) removes `k6/experimental/timers` - they are now available globally and no import is needed. +- [#3944](https://github.com/grafana/k6/pull/3944) updates to `k6/experimental/websockets`, which makes the `binaryType` default value equal to `"blob"`. With this change, `k6/experimental/websockets` is now compliant with the specification. + +## New features + +### Branding changes and logo [`#3946`](https://github.com/grafana/k6/pull/3946), [`#3953`](https://github.com/grafana/k6/pull/3953), [`#3969`](https://github.com/grafana/k6/pull/3969) + +As part of joining Grafana Labs in 2021, k6 was renamed to Grafana k6. The original k6 logo and branding was purple, which didn't fit very well next to the Grafana Labs orange logo and all its other products. + +In this release, we have a new logo in a new color, and the terminal banner has been redesigned to match the current branding more closely. + +

+ Grafana k6 logo +

+ +### New experimental CSV module for efficient CSV data handling [`#3743`](https://github.com/grafana/k6/pull/3743) + +We’ve added a new experimental CSV module to k6 for more efficient and convenient CSV parsing and streaming, addressing the limitations of preexisting JavaScript-based solutions like [papaparse](https://www.papaparse.com/). + +#### What is it? + +The CSV module offers two key features: + +- `csv.parse()`: This function parses a CSV file into a [SharedArray](https://grafana.com/docs/k6/latest/javascript-api/k6-data/sharedarray/) at once using Go-based processing for faster parsing and lower memory usage compared to JavaScript alternatives. +- `csv.Parser`: This class provides a streaming parser to read CSV files line-by-line, minimizing memory consumption and offering more control over parsing through a stream-like API. This is ideal for scenarios where memory optimization or fine-grained control of the parsing process is crucial. + +#### Benefits for users + +- **Faster Parsing**: `csv.parse` bypasses most of the JavaScript runtime, offering significant speed improvements for large files. +- **Lower Memory Usage**: Both solutions support shared memory across virtual users (VUs) with the `fs.open` function. +- **Flexibility**: Choose between full-file parsing with `csv.parse()` or memory-efficient streaming with `csv.Parser`. + +#### Tradeoffs + +- **`csv.Parse`**: Parses the entire file in the initialization phase of the test, which can increase startup time and memory usage for large files. Best suited for scenarios where performance is prioritized over memory consumption. +- **`csv.Parser`**: Reads the file line-by-line, making it more memory-efficient but potentially slower due to reading overhead for each line. Ideal for scenarios where memory usage is a concern or where fine-grained control over parsing is needed. + +#### Example usage + +
+ Expand to see an example of Parsing a full CSV file into a SharedArray. + +```javascript +import { open } from 'k6/experimental/fs'; +import csv from 'k6/experimental/csv'; +import { scenario } from 'k6/execution'; + +export const options = { + iterations: 10, +}; + +let file; +let csvRecords; +(async function () { + file = await open('data.csv'); + + // The `csv.parse` function consumes the entire file at once, and returns + // the parsed records as a SharedArray object. + csvRecords = await csv.parse(file, { delimiter: ',' }); +})(); + +export default async function () { + // The csvRecords a SharedArray. Each element is a record from the CSV file, represented as an array + // where each element is a field from the CSV record. + // + // Thus, `csvRecords[scenario.iterationInTest]` will give us the record for the current iteration. + console.log(csvRecords[scenario.iterationInTest]); +} +``` + +
+ +
+ Expand to see an example of streaming a CSV file line-by-line. + +```javascript +import { open } from 'k6/experimental/fs'; +import csv from 'k6/experimental/csv'; + +export const options = { + iterations: 10, +}; + +let file; +let parser; +(async function () { + file = await open('data.csv'); + parser = new csv.Parser(file); +})(); + +export default async function () { + // The parser `next` method attempts to read the next row from the CSV file. + // + // It returns an iterator-like object with a `done` property that indicates whether + // there are more rows to read, and a `value` property that contains the row fields + // as an array. + const { done, value } = await parser.next(); + if (done) { + throw new Error('No more rows to read'); + } + + // We expect the `value` property to be an array of strings, where each string is a field + // from the CSV record. + console.log(done, value); +} +``` + +
+ +### New `k6 cloud run --local-execution` flag for local execution of cloud tests [`#3904`](https://github.com/grafana/k6/pull/3904), and [#3931](https://github.com/grafana/k6/pull/3931) + +This release introduces the `--local-execution` flag for the k6 cloud run command, allowing you to run test executions locally while sending metrics to Grafana Cloud k6. + +```bash +k6 cloud run --local-execution script.js +``` + +By default, using the `--local-execution` flag uploads the test archive to Grafana Cloud k6. If you want to disable this upload, use the `--no-archive-upload` flag. + +The `--local-execution` flag currently functions similarly to the `k6 run -o cloud` command, which is now considered deprecated (though it is not planned to be removed). Future updates will enhance `--local-execution` with additional capabilities that the `k6 run -o cloud` command does not offer. + +### New `k6 cloud upload` command for uploading test files to the cloud [`#3906`](https://github.com/grafana/k6/pull/3906) + +We continue to refine and improve the cloud service to improve how we handle uploading test files, so we've added a new `k6 cloud upload` command that replaces the `k6 cloud --upload-only` flag, which is now considered deprecated. + +### gRPC module updates driven by contributors + +#### New `discardResponseMessage` option + +[#3877](https://github.com/grafana/k6/pull/3877) and [#3820](https://github.com/grafana/k6/pull/3820) add a new option for the gRPC module `discardResponseMessage`, which allows users to discard the messages received from the server. + +```javascript +const resp = client.invoke('main.RouteGuide/GetFeature', req, { discardResponseMessage: true }); +``` + +This reduces the amount of memory required and the amount of garbage collection, which reduces the load on the testing machine and can help produce more reliable test results. + +Thank you, @lzakharov! + +#### New argument `meta` for gRPC's stream callbacks + +[#3801](https://github.com/grafana/k6/pull/3801) adds a new argument `meta` to gRPC's stream callback, which handles the timestamp of the original event (for example, when a message has been received). + +```javascript +let stream = new grpc.Stream(client, 'main.FeatureExplorer/ListFeatures'); +stream.on('data', function (data, meta) { + // will print the timestamp when message has been received + call(meta.ts); +}); +``` + +Thank you, @cchamplin! + +#### Allow missing file descriptors for gRPC reflection + +[#3871](https://github.com/grafana/k6/pull/3871) allows missing file descriptors for gRPC reflection. + +Thank you, @Lordnibbler! + +### Sobek updates brings support of new ECMAScript features into k6 [`#3899`](https://github.com/grafana/k6/pull/3899), [`#3925`](https://github.com/grafana/k6/pull/3925), [`#3913`](https://github.com/grafana/k6/pull/3913) + +With this release, we've updated [Sobek](https://github.com/grafana/sobek) (the `ECMAScript` implementation in Go) which contains the new ECMAScript features that are now available in k6. + +This includes support for numeric literal separators: + +```javascript +const billion = 1_000_000_000; +``` + +Support for `BigInt`, the values which are too large to be represented by the number primitive: + +```javascript +const huge = BigInt(9007199254740991); +``` + +Note: Before k6 version v0.54, Golang's type `math/big.Int` mapped to another type, so this might be a breaking change for some extensions or users. + +RegExp dotAll support, where you can match newline characters with `.`: + +```javascript +const str1 = 'bar\nexample foo example'; + +const regex1 = /bar.example/s; + +console.log(regex1.dotAll); // true +``` + +Support for ES2023 Array methods: [`with`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/with), [`toSpliced`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSpliced), [`toReversed`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed) and [`toSorted`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted). + +Thank you @shiroyk for adding both the new array methods and BitInt :bow:. + +### New `setChecked` method for the browser module [`browser#1403`](https://github.com/grafana/xk6-browser/pull/1403) + +Previously, users could check or uncheck checkbox and radio button elements using the [`check`](https://grafana.com/docs/k6/latest/javascript-api/k6-browser/page/check/) and [`uncheck`](https://grafana.com/docs/k6/latest/javascript-api/k6-browser/page/uncheck/) methods. Now, we've added a `setChecked` method that allows users to set a checkbox or radio button to either the checked or unchecked state with a single method and a boolean argument. + +```javascript +await page.setChecked('#checkbox', true); // check the checkbox +await page.setChecked('#checkbox', false); // uncheck the checkbox +``` + +[Page](https://grafana.com/docs/k6/latest/javascript-api/k6-browser/page/setchecked/), [Frame](https://grafana.com/docs/k6/latest/javascript-api/k6-browser/frame/setchecked/), [ElementHandle](https://grafana.com/docs/k6/latest/javascript-api/k6-browser/elementhandle/setchecked/), and [Locator](https://grafana.com/docs/k6/latest/javascript-api/k6-browser/locator/setchecked/) now support the new `setChecked` method. + +### Async `check` function utility [`k6-utils#13`](https://github.com/grafana/k6-jslib-utils/pull/13) + +Writing concise code can be difficult when using [the k6 `check` function](https://grafana.com/docs/k6/latest/javascript-api/k6/check/) with async code since it doesn't support async APIs. A solution that we have suggested so far is to declare a temporary variable, wait for the value that is to be checked, and then check the result later. However, this approach can clutter the code with single-use declarations and unnecessary variable names, for example: + +```javascript +const checked = await p.locator('.checked').isChecked(); + +check(checked, { + checked: (c) => c, +}); +``` + +To address this limitation, we've added a version of the `check` function to [jslib.k6.io](https://jslib.k6.io/) that makes working with `async`/`await` simpler. The `check` function is a drop-in replacement for the built-in check, with added support for async code. Any `Promise`s will be awaited, and the result is reported once the operation has been completed: + +```javascript +// Import the new check function from jslib.k6.io/k6-utils +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; + +// ... + +// Use the new check function with async code +check(page, { + checked: async (p) => p.locator('.checked').isChecked(), +}); +``` + +Check out [the `check` utility function's documentation](https://grafana.com/docs/k6/latest/javascript-api/jslib/utils/check/) for more information on how to use it. + +### `k6/experimnetal/websockets` updates towards WebSockets API compatibility + +#### Support ArrayBufferViews in `send` for `k6/experimental/websockets` [#3944](https://github.com/grafana/k6/pull/3844) + +As part of making `k6/experimental/websockets` compliant with the [WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket), it now supports Uint8Array and other ArrayBufferViews directly as arguments to `send`, instead of having to specifically provide their `buffer`. + +This should make the module more compliant with libraries that use the WebSocket API. + +Thanks to @pixeldrew for [reporting](https://github.com/grafana/xk6-websockets/issues/75) this. :bow: + +#### `readyState` actually being a number [#3972](https://github.com/grafana/k6/pull/3972) + +Due to goja/Sobek internal workings, `readyState` wasn't exactly a number in JavaScript code. This had some abnormal behavior, which limited interoperability with libraries. + +This has been fixed, and `readyState` is a regular number from the JavaScript perspective. + +Thanks to @dougw-bc for [reporting](https://github.com/grafana/xk6-websockets/issues/79) this. :bow: + +### Rework of how usage is being collected internally and additional counters [`#3917`](https://github.com/grafana/k6/pull/3917) and [`#3951`](https://github.com/grafana/k6/pull/3951) + +As part of working on k6 over the years, we have often wondered if users use certain features or if they see a strange corner case behavior. + +We have usually made guesses or tried to extrapolate from experience on issues we see. Unfortunately, this isn't always easy or possible, and it's definitely very skewed. As such, we usually also add warning messages to things we intend to break to inform people and ask them to report problems. But we usually see very little activity, especially before we make changes. + +This also only works for things we want to remove, and it doesn't help us know if people use new functionality at all or if there are patterns we don't expect. + +While k6 has been collecting usage for a while, they're surface-level things, such as how many VUs were run, the k6 version, or which _internal_ modules were loaded. The system for this was also very rigid, requiring a lot of work to add simple things, such as if someone used the `require` function. + +This process has been reworked to make things easier, and a few new usage reports have been added: + +- When Grafana Cloud is used, it will also tell us which test run it is. We already have most of the information reported through metrics and from the test being executed in the cloud. But this will help us filter cloud test runs from local runs and potentially warn cloud users if they're using experimental modules that will be removed. +- The number of files parsed, as well as the number of .ts files parsed. This will help us understand if people use small or big projects and if TypeScript support is being used. +- Usage of `require`. Now that we have ESM native support, using `require` and CommonJS adds complexity. It's interesting to us whether removing this in the future - likely years, given its support in other runtimes, is feasible. +- Usage of `global`. This will help us decide if we can [drop compatibility-mode](https://github.com/grafana/k6/issues/3864) differences or even the whole concept. + +## UX improvements and enhancements + +- [#3898](https://github.com/grafana/k6/pull/3898) adds `SetupTimeout` option validation. Thank you, @tsukasaI! +- [#3930](https://github.com/grafana/k6/pull/3930) adds token validation for `k6 cloud login`, so now you get immediate feedback right after logging in. +- [#3876](https://github.com/grafana/k6/pull/3876), [#3923](https://github.com/grafana/k6/pull/3923) adjusts the process' exit code for cloud test runs where thresholds have failed. +- [#3765](https://github.com/grafana/k6/pull/3765) stops using the confusing `✓` and `✗` for `Rate` metrics, and instead uses the form: `{x} out of {y}`. + +## Bug fixes + +- [#3947](https://github.com/grafana/k6/pull/3947) fixes panic when `options` is `nil` (e.g. exported from a module where it isn't really exported). +- [#3901](https://github.com/grafana/k6/pull/3901) fixes the `cloud` command not being display in the `k6` command's help text. +- [browser#1406](https://github.com/grafana/xk6-browser/pull/1406) fixes panic on iframe attach when iframe didn't contain any UI elements. +- [browser#1420](https://github.com/grafana/xk6-browser/pull/1420) uses the VU context to control the iterations lifecycle which helps k6 shutdown in a timely manner when a test is aborted. +- [browser#1421](https://github.com/grafana/xk6-browser/pull/1421) fixes the navigation span by starting when the page starts to load so that it's a better representation of how long the test was on a page. +- [browser#1408](https://github.com/grafana/xk6-browser/pull/1408), [browser#1422](https://github.com/grafana/xk6-browser/pull/1422) fixes the `page.reload` API so handles `null` responses without exceptions. +- [browser#1435](https://github.com/grafana/xk6-browser/pull/1435) fixes an NPD when performing a `click` action. +- [browser#1438](https://github.com/grafana/xk6-browser/pull/1438) fixes a goroutine that waits indefinitely when writing to a channel. +- [#3968](https://github.com/grafana/k6/pull/3968) fixes an issue with the event system where it was dropping events which led to it hanging indefinitely. +- [browser#1442](https://github.com/grafana/xk6-browser/pull/1442) fixes `browser.close` to abort the cdp close request when the browser process has already exited. + +## Maintenance and internal improvements + +- [#3915](https://github.com/grafana/k6/pull/3915) switches `go.mod` to the go1.21, introduces toolchain. +- [#3938](https://github.com/grafana/k6/pull/3938) updates k6's CI workflows to go 1.23. +- [#3939](https://github.com/grafana/k6/pull/3939) updates Dockerfile to use go 1.23 and alpine 3.20. +- [#3909](https://github.com/grafana/k6/pull/3909) fixes `ExitCode` description typo. Thank you, @eltociear! +- [#3926](https://github.com/grafana/k6/pull/3926) documents maintenance of tc39 tests. +- [#3881](https://github.com/grafana/k6/pull/3881) adds top-level roadmap link. +- [#3945](https://github.com/grafana/k6/pull/3945) updates the endpoint where [usage reports](https://grafana.com/docs/k6/latest/set-up/usage-collection/) are sent to. +- [browser#1419](https://github.com/grafana/xk6-browser/pull/1419), [browser#1423](https://github.com/grafana/xk6-browser/pull/1423) add a new remote file upload protocol. +- [#3900](https://github.com/grafana/k6/pull/3900), [#3902](https://github.com/grafana/k6/pull/3902) update golangci-lint to 1.60.1 and add `fatcontext` and `cononicalheader` as linters. +- [#3908](https://github.com/grafana/k6/pull/3908) drops `NetTrail` internal type to simplify internal implementation on emitting iteration and data transmission metric. +- [#3933](https://github.com/grafana/k6/pull/3933) adds a `testutils.MakeMemMapFs` test helper facilitating simulating a file system in tests. +- [#3943](https://github.com/grafana/k6/pull/3943) raises `TestStreamLogsToLogger` log sending delay to improve the reliability of tests. +- [#3935](https://github.com/grafana/k6/pull/3935) refactors some js tests to remove repeated setup code. +- [#3903](https://github.com/grafana/k6/pull/3903), [#3912](https://github.com/grafana/k6/pull/3912), [#3928](https://github.com/grafana/k6/pull/3928), [#3910](https://github.com/grafana/k6/pull/3910), [#3954](https://github.com/grafana/k6/pull/3954), [#3963](https://github.com/grafana/k6/pull/3963), [#3965](https://github.com/grafana/k6/pull/3965), [#3966](https://github.com/grafana/k6/pull/3966), [#3965](https://github.com/grafana/k6/pull/3965), [#3970](https://github.com/grafana/k6/pull/3970) update dependencies. + +## Roadmap + +In version 0.52.0, the browser module [transitioned](https://github.com/grafana/k6/pull/3793) from experimental to stable. The new module is more stable and has a [full Async API](https://github.com/grafana/xk6-browser/pull/1374). To ensure your scripts continue working, you must migrate to the new `k6/browser` module and discontinue using the previous `k6/experimental/browser` module. Please see [the migration guide](https://grafana.com/docs/k6/latest/using-k6-browser/migrating-to-k6-v0-52/) for more details. diff --git a/docs/sources/k6/v0.55.x/results-output/_index.md b/docs/sources/k6/v0.55.x/results-output/_index.md new file mode 100644 index 000000000..387c30213 --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/_index.md @@ -0,0 +1,28 @@ +--- +aliases: + - ./results-output/overview # docs/k6//results-output/overview +title: Results output +description: All the ways you can look at k6 results. While the test runs, after the test runs, on an external platform, as summary statistics. +weight: 400 +--- + +# Results output + +k6 emits [metrics](https://grafana.com/docs/k6//using-k6/metrics) with timestamps at every point of the test. +You can output the metric results as either **aggregated statistics** or **individual data points**. + +- For a top-level test overview, use the [end-of-test summary](https://grafana.com/docs/k6//results-output/end-of-test). +- For granular output of all metrics (with timestamps), you [stream metrics in real time](https://grafana.com/docs/k6//results-output/real-time). + +If you stream your metrics, you can either write them to a file, like JSON, or stream them to a service, like InfluxDB. + +![A diagram of the two broad ways to handle results: aggregated and granular](/media/docs/k6-oss/k6-results-diagram.png) + +## Read more + +- [End of test summary](https://grafana.com/docs/k6//results-output/end-of-test) +- [Real time results](https://grafana.com/docs/k6//results-output/real-time) +- [Web dashboard](https://grafana.com/docs/k6//results-output/web-dashboard) +- [Ways to visualize k6 results](https://k6.io/blog/ways-to-visualize-k6-results/) +- [Build an output extension](https://grafana.com/docs/k6//extensions/create/output-extensions) +- [k6 data collection pipeline](https://grafana.com/blog/2023/08/10/understanding-grafana-k6-a-simple-guide-to-the-load-testing-tool/) diff --git a/docs/sources/k6/v0.55.x/results-output/end-of-test/_index.md b/docs/sources/k6/v0.55.x/results-output/end-of-test/_index.md new file mode 100644 index 000000000..214798e0d --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/end-of-test/_index.md @@ -0,0 +1,119 @@ +--- +title: End of test +description: When a test finishes, k6 prints a summary of results, with aggregated metrics and meta-data about the test. You can customize this, or configure the test to write granular metrics to a file. +weight: 100 +weight: 100 +--- + +# End of test + +When a test finishes, k6 prints a top-level overview of the aggregated results to `stdout`. + +```bash +checks.........................: 50.00% ✓ 45 ✗ 45 +data_received..................: 1.3 MB 31 kB/s +data_sent......................: 81 kB 2.0 kB/s +group_duration.................: avg=6.45s min=4.01s med=6.78s max=10.15s p(90)=9.29s p(95)=9.32s +http_req_blocked...............: avg=57.62ms min=7µs med=12.25µs max=1.35s p(90)=209.41ms p(95)=763.61ms +http_req_connecting............: avg=20.51ms min=0s med=0s max=1.1s p(90)=100.76ms p(95)=173.41ms +http_req_duration..............: avg=144.56ms min=104.11ms med=110.47ms max=1.14s p(90)=203.54ms p(95)=215.95ms + { expected_response:true }...: avg=144.56ms min=104.11ms med=110.47ms max=1.14s p(90)=203.54ms p(95)=215.95ms +http_req_failed................: 0.00% ✓ 0 ✗ 180 +http_req_receiving.............: avg=663.96µs min=128.46µs med=759.82µs max=1.66ms p(90)=1.3ms p(95)=1.46ms +http_req_sending...............: avg=88.01µs min=43.07µs med=78.03µs max=318.81µs p(90)=133.15µs p(95)=158.3µs +http_req_tls_handshaking.......: avg=29.25ms min=0s med=0s max=458.71ms p(90)=108.31ms p(95)=222.46ms +http_req_waiting...............: avg=143.8ms min=103.5ms med=109.5ms max=1.14s p(90)=203.19ms p(95)=215.56ms +http_reqs......................: 180 4.36938/s +iteration_duration.............: avg=12.91s min=12.53s med=12.77s max=14.35s p(90)=13.36s p(95)=13.37s +iterations.....................: 45 1.092345/s +vus............................: 1 min=1 max=19 +vus_max........................: 20 min=20 max=20 +``` + +
+ +Besides this default summary, k6 can output the results in other formats at the end of the test: + +| On this page | Result format | Read about... | +| ---------------------------------------------------------------------------------------------------- | ------------------------ | ------------------------------------------------------------- | +| [Custom summary](https://grafana.com/docs/k6//results-output/end-of-test/custom-summary) | Aggregated | Using the `handleSummary()` to make completely custom reports | +| [CSV](https://grafana.com/docs/k6//results-output/real-time/csv) | Time-stamped data points | Writing results as a CSV file, and the structure of the data | +| [JSON](https://grafana.com/docs/k6//results-output/real-time/json) | Time-stamped data points | Writing results as a JSON file, and the structure of the data | + +## The default summary + +The end-of-test summary reports details and aggregated statistics for the primary aspects of the test: + +- Summary statistics about each built-in and custom [metric](https://grafana.com/docs/k6//using-k6/metrics#built-in-metrics) (e.g. mean, median, p95, etc). +- A list of the test's [groups](https://grafana.com/docs/k6//using-k6/tags-and-groups#groups) and [scenarios](https://grafana.com/docs/k6//using-k6/scenarios) +- The pass/fail results of the test's [thresholds](https://grafana.com/docs/k6//using-k6/thresholds) and [checks](https://grafana.com/docs/k6//using-k6/checks). + +{{< code >}} + +```bash +Ramp_Up ✓ [======================================] 00/20 VUs 30s + █ GET home - https://example.com/ + + ✓ status equals 200 + + █ Create resource - https://example.com/create + + ✗ status equals 201 + ↳ 0% — ✓ 0 / ✗ 45 + + checks.........................: 50.00% ✓ 45 ✗ 45 + data_received..................: 1.3 MB 31 kB/s + data_sent......................: 81 kB 2.0 kB/s + group_duration.................: avg=6.45s min=4.01s med=6.78s max=10.15s p(90)=9.29s p(95)=9.32s + http_req_blocked...............: avg=57.62ms min=7µs med=12.25µs max=1.35s p(90)=209.41ms p(95)=763.61ms + http_req_connecting............: avg=20.51ms min=0s med=0s max=1.1s p(90)=100.76ms p(95)=173.41ms + ✗ http_req_duration..............: avg=144.56ms min=104.11ms med=110.47ms max=1.14s p(90)=203.54ms p(95)=215.95ms + { expected_response:true }...: avg=144.56ms min=104.11ms med=110.47ms max=1.14s p(90)=203.54ms p(95)=215.95ms + http_req_failed................: 0.00% ✓ 0 ✗ 180 + http_req_receiving.............: avg=663.96µs min=128.46µs med=759.82µs max=1.66ms p(90)=1.3ms p(95)=1.46ms + http_req_sending...............: avg=88.01µs min=43.07µs med=78.03µs max=318.81µs p(90)=133.15µs p(95)=158.3µs + http_req_tls_handshaking.......: avg=29.25ms min=0s med=0s max=458.71ms p(90)=108.31ms p(95)=222.46ms + http_req_waiting...............: avg=143.8ms min=103.5ms med=109.5ms max=1.14s p(90)=203.19ms p(95)=215.56ms + http_reqs......................: 180 4.36938/s + iteration_duration.............: avg=12.91s min=12.53s med=12.77s max=14.35s p(90)=13.36s p(95)=13.37s + iterations.....................: 45 1.092345/s + vus............................: 1 min=1 max=19 + vus_max........................: 20 min=20 max=20 + +ERRO[0044] some thresholds have failed +``` + +{{< /code >}} + +Above's an example of a report that k6 generated after a test run. + +- It has a scenario, `Ramp_Up` +- The requests are split into two groups: + - `GET home`, which has a check that responses are `200` (all passed) + - `Create resource`, which has a check that responses are `201` (all failed) +- The test has one threshold, requiring that 95% of requests have a duration under 200ms (failed) + +## Summary options + +k6 provides some options to filter or silence summary output: + +- The [`--summary-trend-stats` option](https://grafana.com/docs/k6//using-k6/k6-options/reference#summary-trend-stats) defines which [Trend metric](https://grafana.com/docs/k6//javascript-api/k6-metrics/trend) statistics to calculate and show. +- The [`--summary-time-unit` option](https://grafana.com/docs/k6//using-k6/k6-options/reference#summary-time-unit) forces k6 to use a fixed-time unit for all time values in the summary. +- The [`--no-summary` option](https://grafana.com/docs/k6//using-k6/k6-options/reference#no-summary) completely disables report generation, including `--summary-export` and `handleSummary()`. + +{{< collapse title="Summary export to a JSON file (Discouraged)" >}} + +### Summary export to a JSON file + +k6 also has the [`--summary-export=path/to/file.json` option](https://grafana.com/docs/k6//using-k6/k6-options/reference#summary-export), which exports some summary report data to a JSON file. + +The format of `--summary-export` is similar to the `data` parameter of the `handleSummary()` function. +Unfortunately, the `--summary-export` format is limited and has a few confusing peculiarities. +For example, groups and checks are unordered, +and threshold values are unintuitive: `true` indicates the threshold failed, and `false` that it succeeded. + +We couldn't change the `--summary-export` data format, because it would have broken backward compatibility in a feature that people depended on in CI pipelines. +But, the recommended approach to export to a JSON file is to use the `handleSummary()` function]. +The `--summary-export` option will likely be deprecated in the future. + +{{< /collapse >}} diff --git a/docs/sources/k6/v0.55.x/results-output/end-of-test/custom-summary.md b/docs/sources/k6/v0.55.x/results-output/end-of-test/custom-summary.md new file mode 100644 index 000000000..8fe107807 --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/end-of-test/custom-summary.md @@ -0,0 +1,446 @@ +--- +title: Custom summary +description: With handlesummary(), you can customize every part of your report. Change the content, redirect output, and more. +weight: 150 +--- + +# Custom summary + +With `handleSummary()`, you can completely customize your end-of-test summary. +In this document, read about: + +- How `handleSummary()` works +- How to customize the content and output location of your summary +- The data structure of the summary object + +{{< admonition type="note" >}} + +However, we plan to support the feature for k6 Cloud tests, too. +[Track progress in this issue](https://github.com/grafana/k6-cloud-feature-requests/issues/24). + +{{< /admonition >}} + +## About `handleSummary()` + +After your test runs, k6 aggregates your metrics into a JavaScript object. +The `handleSummary()` function takes this object as an argument (called `data` in all examples here). + +You can use `handleSummary()` to create a custom summary or return the default summary object. +To get an idea of what the data looks like, +run this script and open the output file, `summary.json`. + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + http.get('https://test.k6.io'); +} + +export function handleSummary(data) { + return { + 'summary.json': JSON.stringify(data), //the default data object + }; +} +``` + +{{< /code >}} + +Fundamentally, `handleSummary()` is just a function that can access a data object. +As such, you can transform the summary data into any text format: JSON, HTML, console, XML, and so on. +You can pipe your custom summary to [standard output or standard error](https://en.wikipedia.org/wiki/Standard_streams), write it to a file, or send it to a remote server. + +k6 calls `handleSummary()` at the end of the [test lifecycle](https://grafana.com/docs/k6//using-k6/test-lifecycle). + +## Use handleSummary() + +The following sections go over the `handleSummary()` syntax and provide some examples. + +To look up the structure of the summary object, refer to the reference section. + +### Syntax + +k6 expects `handleSummary()` to return a `{key1: value1, key2: value2, ...}` map that represents the summary metrics. + +The keys must be strings. +They determine where k6 displays or saves the content: + +- `stdout` for standard output +- `stderr` for standard error, +- any relative or absolute path to a file on the system (this operation overwrites existing files) + +The value of a key can have a type of either `string` or [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer). + +You can return multiple summary outputs in a script. +As an example, this `return` statement sends a report to standard output and writes the `data` object to a JSON file. + +{{< code >}} + +``` + return { + 'stdout': textSummary(data, { indent: ' ', enableColors: true }), // Show the text summary to stdout... + 'other/path/to/summary.json': JSON.stringify(data), // and a JSON with all the details... + }; +``` + +{{< /code >}} + +### Example: Extract data properties + +This minimal `handleSummary()` extracts the `median` value for the `iteration_duration` metric and prints it to standard output: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + http.get('https://test.k6.io'); +} + +export function handleSummary(data) { + const med_latency = data.metrics.iteration_duration.values.med; + const latency_message = `The median latency was ${med_latency}\n`; + + return { + stdout: latency_message, + }; +} +``` + +{{< /code >}} + +### Example: Modify default output + +If `handleSummary()` is exported, k6 _does not_ print the default summary. +However, if you want to keep the default output, you could import `textSummary` from the [K6 JS utilities library](https://jslib.k6.io/). +For example, you could write a custom HTML report to a file, and use the `textSummary()` function to print the default report to the console. + +You can also use `textSummary()` to make minor modifications to the default end-of-test summary. +To do so: + +1. Modify the `data` object however you want. +1. In your `return` statement, pass the modified object as an argument to the `textSummary()` function. + +The `textSummary()` function comes with a few options: + +| Option | Description | +| -------------- | -------------------------------------- | +| `indent` | How to start the summary indentation | +| `enableColors` | Whether to print the summary in color. | + +For example, this `handleSummary()` modifies the default summary in the following ways: + +- It deletes the `http_req_duration{expected_response:true}` sub-metric. +- It deletes all metrics whose key starts with `iteration`. +- It begins each line with the `→` character. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { textSummary } from 'https://jslib.k6.io/k6-summary/0.0.2/index.js'; + +export default function () { + http.get('https://test.k6.io'); +} + +export function handleSummary(data) { + delete data.metrics['http_req_duration{expected_response:true}']; + + for (const key in data.metrics) { + if (key.startsWith('iteration')) delete data.metrics[key]; + } + + return { + stdout: textSummary(data, { indent: '→', enableColors: true }), + }; +} +``` + +{{< /code >}} + +In the collapsible, you can use the tabs to compare default and modified reports. + +{{< collapse title="Compare the default and modified reports" >}} + +To see the output of the preceding script, +select **Modified**. +For compactness, these outputs were limited with the `summaryTrendStats` option. + +{{< code >}} + +``` + data_received..................: 63 kB 42 kB/s + data_sent......................: 830 B 557 B/s + http_req_blocked...............: med=10.39µs count=5 p(99)=451.07ms p(99.99)=469.67ms + http_req_connecting............: med=0s count=5 p(99)=223.97ms p(99.99)=233.21ms + http_req_duration..............: med=202.26ms count=5 p(99)=225.81ms p(99.99)=226.71ms + { expected_response:true }...: med=202.26ms count=5 p(99)=225.81ms p(99.99)=226.71ms + http_req_failed................: 0.00% ✓ 0 ✗ 5 + http_req_receiving.............: med=278.27µs count=5 p(99)=377.64µs p(99.99)=381.29µs + http_req_sending...............: med=47.57µs count=5 p(99)=108.42µs p(99.99)=108.72µs + http_req_tls_handshaking.......: med=0s count=5 p(99)=204.42ms p(99.99)=212.86ms + http_req_waiting...............: med=201.77ms count=5 p(99)=225.6ms p(99.99)=226.5ms + http_reqs......................: 5 3.352646/s + iteration_duration.............: med=204.41ms count=5 p(99)=654.78ms p(99.99)=672.43ms + iterations.....................: 5 3.352646/s + vus............................: 1 min=1 max=1 + vus_max........................: 1 min=1 max=1 +``` + +``` +→ data_received..............: 63 kB 39 kB/s +→ data_sent..................: 830 B 507 B/s +→ http_req_blocked...........: med=10.98µs count=5 p(99)=485.16ms p(99.99)=505.18ms +→ http_req_connecting........: med=0s count=5 p(99)=245.05ms p(99.99)=255.15ms +→ http_req_duration..........: med=208.68ms count=5 p(99)=302.87ms p(99.99)=306.61ms +→ http_req_failed............: 0.00% ✓ 0 ✗ 5 +→ http_req_receiving.........: med=206.12µs count=5 p(99)=341.05µs p(99.99)=344.13µs +→ http_req_sending...........: med=47.8µs count=5 p(99)=166.94µs p(99.99)=170.92µs +→ http_req_tls_handshaking...: med=0s count=5 p(99)=207.2ms p(99.99)=215.74ms +→ http_req_waiting...........: med=208.47ms count=5 p(99)=302.49ms p(99.99)=306.23ms +→ http_reqs..................: 5 3.054928/s +→ vus........................: 1 min=1 max=1 +→ vus_max....................: 1 min=1 max=1 +``` + +{{< /code >}} + +{{< /collapse >}} + +### Example: Make custom file format + +This script imports a helper function to turn the summary into a JUnit XML. +The output is a short XML file that reports whether the test thresholds failed. + +{{< code >}} + +```javascript +import http from 'k6/http'; + +// Use example functions to generate data +import { jUnit } from 'https://jslib.k6.io/k6-summary/0.0.2/index.js'; +import k6example from 'https://raw.githubusercontent.com/grafana/k6/master/examples/thresholds_readme_example.js'; + +export default k6example; +export const options = { + vus: 5, + iterations: 10, + thresholds: { + http_req_duration: ['p(95)<200'], // 95% of requests should be below 200ms + }, +}; + +export function handleSummary(data) { + console.log('Preparing the end-of-test summary...'); + + return { + 'junit.xml': jUnit(data), // Transform summary and save it as a JUnit XML... + }; +} +``` + +{{< /code >}} + +Output for a test that crosses a threshold looks something like this: + +```xml + + + + + +``` + +### Example: Send data to remote server + +You can also send the generated reports to a remote server (over any protocol that k6 supports). + +{{< code >}} + +```javascript +import http from 'k6/http'; + +// use example function to generate data +import k6example from 'https://raw.githubusercontent.com/grafana/k6/master/examples/thresholds_readme_example.js'; +export const options = { vus: 5, iterations: 10 }; + +export function handleSummary(data) { + console.log('Preparing the end-of-test summary...'); + + // Send the results to some remote server or trigger a hook + const resp = http.post('https://httpbin.test.k6.io/anything', JSON.stringify(data)); + if (resp.status != 200) { + console.error('Could not send summary, got status ' + resp.status); + } +} +``` + +{{< /code >}} + +{{< admonition type="note" >}} + +The last examples use imported helper functions. +These functions might change, so keep an eye on [jslib.k6.io](https://jslib.k6.io/) for the latest. + +Of course, we always welcome [PRs to the jslib](https://github.com/grafana/jslib.k6.io), too! + +{{< /admonition >}} + +## Summary data reference + +Summary data includes information about your test run time and all built-in and custom metrics (including checks). + +All metrics are in a top-level `metrics` object. +In this object, each metric has an object whose key is the name of the metric. +For example, if your `handleSummary()` argument is called `data`, +the function can access the object about the `http_req_duration` metric at `data.metrics.http_req_duration`. + +### Metric schema + +The following table describes the schema for the metrics object. +The specific values depend on the [metric type](https://grafana.com/docs/k6//using-k6/metrics): + + + +| Property | Description | +| -------------------- | ------------------------------------------------------------------------------ | +| type | String that gives the metric type | +| contains | String that describes the data | +| values | Object with the summary metric values (properties differ for each metric type) | +| thresholds | Object with info about the thresholds for the metric (if applicable) | +| thresholds.{name} | Name of threshold (object) | +| thresholds.{name}.ok | Whether threshold was crossed (boolean) | + + + +{{< admonition type="note" >}} + +If you change the default trend metrics with the [`summaryTrendStats`](https://grafana.com/docs/k6//using-k6/k6-options/reference#summary-trend-stats) option, +the keys for the values of the trend will change accordingly. + +{{< /admonition >}} + +### Example summary JSON + +To see what the summary `data` looks like in your specific test run: + +1. Add this to your handleSummary() function: + + ``` + return { 'raw-data.json': JSON.stringify(data)};` + ``` + +1. Inspect the resulting `raw-data.json` file. + + The following is an abridged example of how it might look: + +{{< code >}} + +```json +{ + "root_group": { + "path": "", + "groups": [ + // Sub-groups of the root group... + ], + "checks": [ + { + "passes": 10, + "fails": 0, + "name": "check name", + "path": "::check name" + } + // More checks... + ], + "name": "" + }, + "options": { + // Some of the global options of the k6 test run, + // Currently only summaryTimeUnit and summaryTrendStats + }, + + "state": { + "testRunDurationMs": 30898.965069 + // And information about TTY checkers + }, + + "metrics": { + // A map with metric and sub-metric names as the keys and objects with + // details for the metric. These objects contain the following keys: + // - type: describes the metric type, e.g. counter, rate, gauge, trend + // - contains: what is the type of data, e.g. time, default, data + // - values: the specific metric values, depends on the metric type + // - thresholds: any thresholds defined for the metric or sub-metric + // + "http_reqs": { + "type": "counter", + "contains": "default", + "values": { + "count": 40, + "rate": 19.768856959496336 + } + }, + "vus": { + "type": "gauge", + "contains": "default", + "values": { + "value": 1, + "min": 1, + "max": 5 + } + }, + "http_req_duration": { + "type": "trend", + "contains": "time", + "values": { + // actual keys depend depend on summaryTrendStats + + "avg": 268.31137452500013, + "max": 846.198634, + "p(99.99)": 846.1969478817999 + // ... + }, + "thresholds": { + "p(95)<500": { + "ok": false + } + } + }, + "http_req_duration{staticAsset:yes}": { + // sub-metric from threshold + "contains": "time", + "values": { + // actual keys depend on summaryTrendStats + "min": 135.092841, + "avg": 283.67766343333335, + "max": 846.198634, + "p(99.99)": 846.1973802197999 + // ... + }, + "thresholds": { + "p(99)<250": { + "ok": false + } + }, + "type": "trend" + } + // ... + } +} +``` + +{{< /code >}} + +## Custom output examples + +These examples are community contributions. +We thank everyone who has shared! + +- [Using the JUnit output with Azure Test Plan](https://medium.com/microsoftazure/load-testing-with-azure-devops-and-k6-839be039b68a) +- [Using the JUnit output with TestRail](https://dev.to/kwidera/introducing-testrail-in-you-k6-tests-eck) +- [handleSummary and custom Slack integration](https://medium.com/geekculture/k6-custom-slack-integration-metrics-are-the-magic-of-tests-527aaf613595) +- [Reporting to Xray](https://docs.getxray.app/display/XRAYCLOUD/Performance+and+load+testing+with+k6) +- [HTML reporter](https://github.com/benc-uk/k6-reporter) diff --git a/docs/sources/k6/v0.55.x/results-output/grafana-dashboards.md b/docs/sources/k6/v0.55.x/results-output/grafana-dashboards.md new file mode 100644 index 000000000..80eb0730c --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/grafana-dashboards.md @@ -0,0 +1,35 @@ +--- +title: Grafana dashboards +description: With multiple k6 output formats, you also have multiple ways to visualize test results in a Grafana dashboard. +weight: 300 +--- + +# Grafana dashboards + +You have multiple ways to query k6 results in Grafana. +Having test results in a dashboard brings various benefits: + +- Visualize your results to analyze performance during the test run or over multiple test runs. +- Correlate test results with application and system metrics in the same dashboard to get a holistic overview of your system's performance and quickly find the root causes of performance issues. + +![A grafana dashboard correlating k6 results with observability data](/media/docs/k6-oss/correlated-grafana-dashboard-grafana-cloud-k6.png) + +## Options + +With [Grafana](https://grafana.com/grafana/), you can create a custom dashboard to query and **visualize data from multiple sources and any type of backend**. +Using k6, you can [stream your local test results to any backend](https://grafana.com/docs/k6//results-output/real-time). + +The flexibility and interoperability of Grafana and k6 let you visualize test and observability data in one dashboard, regardless of where the data is stored. +The following outputs include pre-built Grafana dashboards for their storage: + +| Output | Grafana Dashboard | +| ---------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ | +| [AWSTimestream](https://github.com/leonyork/xk6-output-timestream) | [leonyork/xk6-output-timestream](https://github.com/leonyork/xk6-output-timestream/tree/main/grafana/dashboards/) | +| [InfluxDB](https://grafana.com/docs/k6//results-output/real-time/influxdb) | [grafana/xk6-output-influxdb](https://github.com/grafana/xk6-output-influxdb/tree/main/grafana/dashboards) | +| [Prometheus remote write](https://grafana.com/docs/k6//results-output/real-time/prometheus-remote-write) | [k6 Prometheus](https://grafana.com/grafana/dashboards/19665-k6-prometheus/) | +| [Prometheus remote write (Native Histograms)](https://grafana.com/docs/k6//results-output/real-time/prometheus-remote-write) | [k6 Prometheus (Native Histograms)](https://grafana.com/grafana/dashboards/18030-k6-prometheus-native-histograms/) | +| [Grafana Cloud Prometheus](https://grafana.com/docs/k6//results-output/real-time/grafana-cloud-prometheus) | [k6 Prometheus](https://grafana.com/grafana/dashboards/19665-k6-prometheus/) | +| [TimescaleDB](https://grafana.com/docs/k6//results-output/real-time/timescaledb) | [grafana/xk6-output-timescaledb](https://github.com/grafana/xk6-output-timescaledb/tree/main/grafana/dashboards) | +| ---- | [More public dashboards from the community](https://grafana.com/grafana/dashboards/?search=k6) | + +For a fully managed solution, [Grafana Cloud k6](https://grafana.com/products/cloud/k6/) is our commercial product to store, view, scale, and manage your tests with ease. It provides custom views to access your testing and analyze test results, enhanced collaboration features, and many more additional capabilities. diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/_index.md b/docs/sources/k6/v0.55.x/results-output/real-time/_index.md new file mode 100644 index 000000000..e09511065 --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/_index.md @@ -0,0 +1,53 @@ +--- +title: Real time +description: Send your time-series k6 metrics to multiple file formats and services +weight: 200 +--- + +# Real time + +Besides the [end-of-test summary](https://grafana.com/docs/k6//results-output/end-of-test), you can also view metrics as granular data points. +k6 can stream the metrics in real-time and either: + +- Write output to a file +- Send output to an external service. + +## Write to file {#write} + +Currently, k6 supports writing to the following file formats: + +- [CSV](https://grafana.com/docs/k6//results-output/real-time/csv) +- [JSON](https://grafana.com/docs/k6//results-output/real-time/json) + +## Stream to service {#service} + +You can also stream real-time metrics to: + +- [Grafana Cloud k6](https://grafana.com/docs/k6//results-output/real-time/cloud) + +As well as the following third-party services: + +- [Amazon CloudWatch](https://grafana.com/docs/k6//results-output/real-time/amazon-cloudwatch) +- [Apache Kafka](https://grafana.com/docs/k6//results-output/real-time/apache-kafka) +- [Datadog](https://grafana.com/docs/k6//results-output/real-time/datadog) +- [Dynatrace](https://grafana.com/docs/k6//results-output/real-time/dynatrace) +- [Elasticsearch](https://grafana.com/docs/k6//results-output/real-time/elasticsearch) +- [Grafana Cloud Prometheus](https://grafana.com/docs/k6//results-output/real-time/grafana-cloud-prometheus) +- [InfluxDB](https://grafana.com/docs/k6//results-output/real-time/influxdb) +- [Netdata](https://grafana.com/docs/k6//results-output/real-time/netdata) +- [New Relic](https://grafana.com/docs/k6//results-output/real-time/new-relic) +- [Prometheus remote write](https://grafana.com/docs/k6//results-output/real-time/prometheus-remote-write) +- [TimescaleDB](https://grafana.com/docs/k6//results-output/real-time/timescaledb) +- [StatsD](https://grafana.com/docs/k6//results-output/real-time/statsd) +- [Other alternative with a custom output extension](https://grafana.com/docs/k6//extensions/create/output-extensions) + +{{< admonition type="note" >}} + +This list applies to local tests, not to [cloud tests](https://grafana.com/docs/grafana-cloud/testing/k6/). + +{{< /admonition >}} + +## Read more + +- [Ways to visualize k6 results](https://k6.io/blog/ways-to-visualize-k6-results/) +- [k6 data collection pipeline](https://grafana.com/blog/2023/08/10/understanding-grafana-k6-a-simple-guide-to-the-load-testing-tool/) diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/amazon-cloudwatch.md b/docs/sources/k6/v0.55.x/results-output/real-time/amazon-cloudwatch.md new file mode 100644 index 000000000..b21f733f8 --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/amazon-cloudwatch.md @@ -0,0 +1,96 @@ +--- +title: 'Amazon CloudWatch' +description: 'You can send k6 results output to Amazon CloudWatch and later visualize them.' +weight: 00 +--- + +# Amazon CloudWatch + +{{< docs/shared source="k6" lookup="statsd-output.md" version="" >}} + +k6 can send metrics data to [Amazon CloudWatch](https://aws.amazon.com/cloudwatch/) through the [CloudWatch Agent](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Install-CloudWatch-Agent.html) by using the [xk6-output-statsd extension](https://github.com/LeonAdato/xk6-output-statsd). These metrics can then be visualized in dashboards. + +This guide covers how to: + +- Run the CloudWatch agent +- Run the k6 test +- Visualize k6 metrics in Amazon CloudWatch + +## Before you begin + +To use the StatsD output option, you have to build a k6 binary using the [xk6-output-statsd extension](https://github.com/LeonAdato/xk6-output-statsd). For more details, refer to [StatsD](https://grafana.com/docs/k6//results-output/real-time/statsd). + +## Run the CloudWatch agent + +We presume that you already have a machine that supports both running k6 and CloudWatch agent, which either runs a flavor of GNU/Linux or Windows. Just go ahead and [download](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/download-cloudwatch-agent-commandline.html) the suitable CloudWatch agent version for your operating system. + +1. Create an [IAM role](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-iam-roles-for-cloudwatch-agent.html) to send metrics to CloudWatch via the agent. Then, if you are running on Amazon EC2, just [attach](https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/iam-roles-for-amazon-ec2.html#attach-iam-role) the role to your EC2 instance, so that you can send metrics to CloudWatch. Otherwise, if you are running on-premises servers, follow this [guide](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-commandline-fleet.html#install-CloudWatch-Agent-iam_user-first). + +2. Download the CloudWatch Agent package suitable for your operating system. For example, on Debian 10 (Buster), we've used the following link. For other operating systems, please refer to this [guide](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/download-cloudwatch-agent-commandline.html): + + ```bash + $ wget https://s3.amazonaws.com/amazoncloudwatch-agent/debian/amd64/latest/amazon-cloudwatch-agent.deb + ``` + +3. Install the package: + + ```bash + $ sudo dpkg -i amazon-cloudwatch-agent.deb + ``` + +4. Configure the agent to receive data from k6. For this, create a file called "_/opt/aws/amazon-cloudwatch-agent/etc/statsd.json_" and paste the following JSON config object into it. This configuration means that the agent would listen on port number 8125, which is the default port number for k6 and StatsD. The interval for collecting metrics is 5 seconds and we don't aggregate them, since we need the raw data later in CloudWatch. + + ```json + { + "metrics": { + "namespace": "k6", + "metrics_collected": { + "statsd": { + "service_address": ":8125", + "metrics_collection_interval": 5, + "metrics_aggregation_interval": 0 + } + } + } + } + ``` + +5. Run the following command to start the agent: + + ```bash + $ sudo amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/etc/statsd.json + ``` + +6. You can check the status of the agent using the following command: + + ```bash + $ amazon-cloudwatch-agent-ctl -a status + ``` + +## Run the k6 test + +Once the agent is running, you can run your test with: + +{{< code >}} + +```bash +$ K6_STATSD_ENABLE_TAGS=true k6 run --out output-statsd script.js +``` + +{{< /code >}} + +Make sure you're using the k6 binary you built with the xk6-output-statsd extension. + +You can look at the [StatsD](https://grafana.com/docs/k6//results-output/real-time/statsd) output page for configuration options. + +## Visualize k6 metrics in Amazon CloudWatch + +Visualization of the exported metrics to CloudWatch is done by creating a dashboard and selecting desired metrics to be displayed. + +![List of k6 metrics](/media/docs/k6-oss/cloudwatch-k6-metrics.png) + +Here's an example dashboard we've created to visualize the test results. + +![k6 Dashboard on Amazon CloudWatch](/media/docs/k6-oss/cloudwatch-k6-dashboard.png) + +The above dashboard is exported as JSON and is available [here](https://github.com/k6io/example-cloudwatch-dashboards). diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/apache-kafka.md b/docs/sources/k6/v0.55.x/results-output/real-time/apache-kafka.md new file mode 100644 index 000000000..f56dc686a --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/apache-kafka.md @@ -0,0 +1,100 @@ +--- +title: 'Apache Kafka' +description: 'You can use xk6-output-kafka to send k6 metrics in real-time to Kafka, and, optionally, ingest them from InfluxDB.' +weight: 00 +--- + +# Apache Kafka + +[Apache Kafka](https://kafka.apache.org) is a stream-processing platform for handling real-time data. Using [xk6-output-kafka extension](https://github.com/grafana/xk6-output-kafka), you can send k6 metrics in real-time to Kafka, and, optionally, ingest them from InfluxDB. + +## Build the k6 version + +To build a k6 binary with the extension, first, make sure you have [Go](https://golang.org/doc/install) and [Git](https://git-scm.com/) installed on your machine. + +Then, open your terminal and run the following commands: + +```bash +# Install xk6 +go install go.k6.io/xk6/cmd/xk6@latest + +# Build the k6 binary +xk6 build --with github.com/grafana/xk6-output-kafka + +... [INFO] Build environment ready +... [INFO] Building k6 +... [INFO] Build complete: ./k6 +``` + +xk6 will create the new k6 binary in the local folder. + +{{< admonition type="note" >}} + +To learn more about how to build custom k6 versions, check out [xk6](https://github.com/grafana/xk6). + +{{< /admonition >}} + +## Run the k6 test + +You can configure the broker (or multiple ones), topic and message format directly from the command line parameter like this: + +{{< code >}} + +```bash +$ k6 run --out xk6-kafka=brokers=broker_host:8000,topic=k6 +``` + +{{< /code >}} + +or if you want multiple brokers: + +{{< code >}} + +```bash +--out xk6-kafka=brokers={broker1,broker2},topic=k6,format=json +``` + +{{< /code >}} + +You can also specify the message `format` k6 will use. By default, it will be the same as the JSON output, but you can also use the InfluxDB line protocol for direct "consumption" by InfluxDB: + +{{< code >}} + +```bash +--out xk6-kafka=brokers=someBroker,topic=someTopic,format=influxdb +``` + +{{< /code >}} + +You can even modify some of the `format` settings such as `tagsAsFields`: + +{{< code >}} + +```bash +--out xk6-kafka=brokers=someBroker,topic=someTopic,format=influxdb,influxdb.tagsAsFields={url,myCustomTag} +``` + +{{< /code >}} + +### Options + +Here is the full list of options that can be configured and passed to the extension: + +| Name | Type | Description | +| ----------------------------------- | --------- | --------------------------------------------------------------- | +| `K6_KAFKA_BROKERS` | `string` | List of brokers | +| `K6_KAFKA_TOPIC` | `string` | The name of the topic to be sent | +| `K6_KAFKA_AUTH_MECHANISM` | `string` | Authentication mechanism. Default `none`. | +| `K6_KAFKA_SASL_USER` | `string` | Kafka User | +| `K6_KAFKA_SASL_PASSWORD` | `string` | Kafka User Password | +| `K6_KAFKA_SSL` | `boolean` | | +| `K6_KAFKA_VERSION` | `string` | Kafka version. Default the latest | +| `K6_KAFKA_INSECURE_SKIP_TLS_VERIFY` | `boolean` | Whether should ignore TLS verifications | +| `K6_KAFKA_PUSH_INTERVAL` | `string` | Interval of the metrics' aggregation and upload to the endpoint | +| `K6_KAFKA_FORMAT` | `string` | Message format. `json` or `influxdb` | +| `K6_KAFKA_LOG_ERROR` | `boolean` | Boolean indicating to log kafka errors | + +## Read more + +- [xk6-output-kafka extension](https://github.com/grafana/xk6-output-kafka) +- [Integrating k6 with Apache Kafka](https://k6.io/blog/integrating-k6-with-apache-kafka) diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/cloud.md b/docs/sources/k6/v0.55.x/results-output/real-time/cloud.md new file mode 100644 index 000000000..b384a4e6f --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/cloud.md @@ -0,0 +1,147 @@ +--- +title: 'Grafana Cloud k6' +description: 'When streaming the results to the cloud, the machine - where you execute the k6 CLI command - runs the test and uploads the results to the cloud. Then, you will be able to visualize and analyze the results on the web app in real-time.' +weight: 00 +--- + +# Grafana Cloud k6 + +Besides [running cloud tests](https://grafana.com/docs/k6//get-started/running-k6#execution-modes), you can also run a test locally and stream the results to [Grafana Cloud k6](https://grafana.com/docs/grafana-cloud/testing/k6/get-started/run-cloud-tests-from-the-cli/#run-locally-and-stream-to-the-cloud). + +When streaming the results to the cloud, the machine - where you execute the k6 CLI command - runs the test and uploads the results to the cloud-based solution. Then, you will be able to visualize and analyze the results on the web app in real-time. + +## Streaming results vs. running on cloud servers + +Don't confuse `k6 cloud run --local-execution script.js` (what this page is about) with `k6 cloud run script.js`. + +Fundamentally the difference is the machine that the test runs on: + +- `k6 cloud run --local-execution` runs k6 locally and streams the results to the cloud. +- `k6 cloud run`, on the other hand, uploads your script to the cloud solution and runs the test on the cloud infrastructure. In this case you'll only see status updates in your CLI. + +In all cases you'll be able to see your test results at [k6 Cloud](https://app.k6.io) or [Grafana Cloud](https://grafana.com/products/cloud/). + +{{< admonition type="caution" >}} + +Data storage and processing are primary cloud costs, +so `k6 cloud run --local-execution` will consume VUH or test runs from your subscription. + +{{< /admonition >}} + +## Instructions + +1. (Optional) - Log in to the cloud + + Assuming you have installed k6, the first step is to log in to the cloud service. + + With the `k6 cloud login` command, you can set up your API token on the k6 machine to authenticate against the cloud service. + + Copy your token from [k6 Cloud](https://app.k6.io/account/api-token) or [Grafana Cloud k6](https://grafana.com/docs/grafana-cloud/testing/k6/author-run/tokens-and-cli-authentication/) and pass it as: + + {{< code >}} + + ```bash + $ k6 cloud login --token + ``` + + {{< /code >}} + +1. Run the tests and upload the results + + Now, k6 will authenticate you against the cloud service, and you can use the `--local-execution` option to send the k6 results to the cloud as: + + {{< code >}} + + ```bash + $ k6 cloud run --local-execution script.js + ``` + + {{< /code >}} + + Alternatively, you could skip the `k6 cloud login` command when passing your API token to the `k6 cloud run` command as: + + {{< code >}} + + ```bash + $ K6_CLOUD_TOKEN= k6 cloud run --local-execution script.js + ``` + + {{< /code >}} + + After running the command, the console shows a URL. Copy this URL and paste it in your browser's address bar to visualize the test results. + + {{< code >}} + + ```bash + execution: local + output: cloud (https://acmecorp.grafana.net/a/k6-app/runs/123456) + script: script.js + ``` + + {{< /code >}} + +![Grafana Cloud k6 Test Results](/media/docs/k6-oss/screenshot-stream-k6-results-to-grafana-cloud-k6.png) + +When you send the results to the k6 Cloud with `k6 cloud run --local-execution`, data will be +continuously streamed to the cloud. While this happens the state of the test run +will be marked as `Running`. A test run that ran its course will be marked +`Finished`. The run state has nothing to do with the test passing any +thresholds, only that the test itself is operating correctly. + +If you deliberately abort your test (e.g. by pressing _Ctrl-C_), your test will +end up as `Aborted by User`. You can still look and analyze the test data you +streamed so far. The test will just have run shorter than originally planned. + +Another possibility would be if you lose network connection with the cloud service +while your test is running. In that case the cloud service will patiently wait for +you to reconnect. In the meanwhile your test's run state will continue to +appear as `Running` on the web app. + +If no reconnection happens, the cloud service will time out after two minutes of no +data, setting the run state to `Timed out`. You can still analyze a timed out +test but you'll of course only have access to as much data as was streamed +before the network issue. + +{{< admonition type="caution" >}} + +The `k6 cloud run --local-execution script.js` command uploads the test archive k6 produces from your test script and resources to Grafana Cloud. + +This behavior can be deactivated by adding the `--no-archive-upload` option to your +command: `k6 cloud run --local-execution --no-archive-upload script.js`. + +{{< /admonition >}} + +## Advanced settings + +A few [environment variables](https://grafana.com/docs/k6//using-k6/environment-variables) can control how k6 streams results with `-o cloud`. + +When streaming, k6 will collect all data and send it to the cloud in batches. + +| Name | Description | +| ------------------------------- | ----------------------------------------------------- | +| `K6_CLOUD_METRIC_PUSH_INTERVAL` | How often to send data to the cloud (default `'1s'`). | + +k6 can also _aggregate_ the data it sends to the cloud each batch. This +reduces the amount of data sent to the cloud. Aggregation is disabled by +default. + +When using aggregation, k6 will collect incoming test data into time-buckets. +For each data-type collected in such a bucket, it will figure out the dispersion +(by default the [interquartile range][iqr]) around the median value. +Outlier data—far outside the lower and upper quartiles— is not aggregated, preventing the loss of potentially important testing information. + +| Name | Description | +| --------------------------------------------- | ---------------------------------------------------------------------------------------------------------- | +| `K6_CLOUD_AGGREGATION_PERIOD` | >0s to activate aggregation (default disabled, `'3s'` is a good example to try) | +| `K6_CLOUD_AGGREGATION_CALC_INTERVAL` | How often new data will be aggregated (default `'3s'`). | +| `K6_CLOUD_AGGREGATION_WAIT_PERIOD` | How long to wait for period samples to accumulate before aggregating them (default `'5s'`). | +| `K6_CLOUD_AGGREGATION_MIN_SAMPLES` | If fewer samples than this arrived in interval, skip aggregation (default `100`). | +| `K6_CLOUD_AGGREGATION_OUTLIER_IQR_RADIUS` | Outlier sampling from median to use for Q1 and Q3 quartiles (fraction) (default `0.25` (i.e. [IQR][iqr])). | +| `K6_CLOUD_AGGREGATION_OUTLIER_IQR_COEF_LOWER` | How many quartiles below the lower quartile are treated as non-aggregatable outliers (default `1.5`) | +| `K6_CLOUD_AGGREGATION_OUTLIER_IQR_COEF_UPPER` | How many quartiles above the upper quartile are treated as non-aggregatable outliers (default `1.3`) | + +> When running a test entirely in the cloud with `k6 cloud run`, `k6` will always +> aggregate. For that case the aggregation settings are however set by the +> cloud infrastructure and are not controllable from the CLI. + +[iqr]: https://en.wikipedia.org/wiki/Interquartile_range diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/csv.md b/docs/sources/k6/v0.55.x/results-output/real-time/csv.md new file mode 100644 index 000000000..df883e105 --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/csv.md @@ -0,0 +1,85 @@ +--- +title: 'CSV' +description: 'You can also make k6 output detailed statistics in a CSV format by using the --out option.' +weight: 00 +--- + +# CSV + +You can output granular data points in CSV format. +To do so, use `k6 run` with the `--out` flag. +Pass the path for your CSV file as the flag argument: + +{{< code >}} + +```bash +$ k6 run --out csv=test_results.csv script.js +``` + +{{< /code >}} + +You can also get the results gzipped, like this: + +{{< code >}} + +```bash +$ k6 run --out csv=test_results.gz script.js +``` + +{{< /code >}} + +To inspect the output in real time, you can use a command like `tail -f` on the file you save: + +```bash +$ tail -f test_results.csv +``` + +## CSV format + +The CSV result file will look something like this: + +{{< code >}} + +```plain +metric_name,timestamp,metric_value,check,error,error_code,group,method,name,proto,scenario,status,subproto,tls_version,url,extra_tags +http_reqs,1595325560,1.000000,,,,,GET,http://test.k6.io,HTTP/1.1,default,200,,,http://test.k6.io, +http_req_duration,1595325560,221.899000,,,,,GET,http://test.k6.io,HTTP/1.1,default,200,,,http://test.k6.io, +http_req_blocked,1595325560,225.275000,,,,,GET,http://test.k6.io,HTTP/1.1,default,200,,,http://test.k6.io, +http_req_connecting,1595325560,217.680000,,,,,GET,http://test.k6.io,HTTP/1.1,default,200,,,http://test.k6.io, +http_req_tls_handshaking,1595325560,0.000000,,,,,GET,http://test.k6.io,HTTP/1.1,default,200,,,http://test.k6.io, +http_req_sending,1595325560,0.112000,,,,,GET,http://test.k6.io,HTTP/1.1,default,200,,,http://test.k6.io, +http_req_waiting,1595325560,220.280000,,,,,GET,http://test.k6.io,HTTP/1.1,default,200,,,http://test.k6.io, +http_req_receiving,1595325560,1.507000,,,,,GET,http://test.k6.io,HTTP/1.1,default,200,,,http://test.k6.io, +vus,1595325560,1.000000,,,,,,,,,,,,, +vus_max,1595325560,20.000000,,,,,,,,,,,,, +checks,1595325561,1.000000,status is 200,,,,,,,default,,,,, +checks,1595325561,0.000000,response body,,,,,,,default,,,,, +data_sent,1595325561,76.000000,,,,,,,,default,,,,, +data_received,1595325561,11045.000000,,,,,,,,default,,,,, +iteration_duration,1595325561,1449.049580,,,,,,,,default,,,,, +iterations,1595325561,1.000000,,,,,,,,default,,,,, +``` + +{{< /code >}} + +Each entry in the report represents a metric, `metric_name`, along with its value, `metric_value`, at time, `timestamp`. +If an error happens, then the `error` along with the `error_code` fields will be populated. + +## CSV options + +k6 provides a few options to help you configure your CSV output: + + + +| option | Configures | Possible values | Default | Env. variable | +| -------------- | ------------------------------------------------ | --------------------------------------------------------------------------------------------------------------- | ---------- | ---------------------- | +| `saveInterval` | The time intervals at which k6 writes to the CSV | Either a string with time units(`"1m45s"`), or a number of milliseconds | `"1s"` | `K6_CSV_SAVE_INTERVAL` | +| `timeFormat` | The timestamp format | unix, unix_nano, unix_micro, unix_milli, [rfc3339](https://datatracker.ietf.org/doc/html/rfc3339), rfc3339_nano | unix | `K6_CSV_TIME_FORMAT` | +| `fileName` | The file name and path where output is saved | N/A | `file.csv` | `K6_CSV_FILENAME` | + + + +## Read more + +- [Metrics](https://grafana.com/docs/k6//using-k6/metrics) +- [Error codes](https://grafana.com/docs/k6//javascript-api/error-codes) diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/datadog.md b/docs/sources/k6/v0.55.x/results-output/real-time/datadog.md new file mode 100644 index 000000000..14b8abac2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/datadog.md @@ -0,0 +1,103 @@ +--- +title: 'Datadog' +description: 'Send k6 output to Datadog to visualize load test results and correlate performance testing metrics in Datadog.' +weight: 00 +--- + +# Datadog + +{{< docs/shared source="k6" lookup="statsd-output.md" version="" >}} + +k6 can send metrics to [Datadog](https://www.datadoghq.com/). That allows visualizing and correlating performance testing metrics with other monitored metrics in Datadog. + +This guide covers how to: + +- Run the Datadog Agent +- Run the k6 test +- Visualize in Datadog + +## Before you begin + +To use the StatsD output option, you have to build a k6 binary using the [xk6-output-statsd extension](https://github.com/LeonAdato/xk6-output-statsd). For more details, refer to [StatsD](https://grafana.com/docs/k6//results-output/real-time/statsd). + +## Run the Datadog Agent + +To get k6 metrics into Datadog, k6 sends metrics through the Datadog Agent, which collects, aggregates, and forwards the metrics to the Datadog platform. + +Run the Datadog Agent service as a Docker container with this command: + +{{< code >}} + +```bash +DOCKER_CONTENT_TRUST=1 \ +docker run --rm -d \ + --name datadog \ + -v /var/run/docker.sock:/var/run/docker.sock:ro \ + -v /proc/:/host/proc/:ro \ + -v /sys/fs/cgroup/:/host/sys/fs/cgroup:ro \ + -e DD_SITE="datadoghq.com" \ + -e DD_API_KEY= \ + -e DD_DOGSTATSD_NON_LOCAL_TRAFFIC=1 \ + -p 8125:8125/udp \ + datadog/agent:latest +``` + +{{< /code >}} + +Replace `` with your [Datadog API key](https://app.datadoghq.com/account/settings#api). + +If your account is registered with Datadog EU, change the value of `DD_SITE` to `datadoghq.eu`. + +{{< admonition type="note" >}} + +For additional information, refer to the Datadog Docker Agent documentation. + +{{< /admonition >}} + +### DogStatsD + +The Datadog agent includes the [DogStatsD](https://docs.datadoghq.com/developers/dogstatsd/) service to collect and aggregate metrics. DogStatsD implements the [StatsD](https://github.com/etsy/statsd) protocol with some extensions. For example, [DogStatsD tagging](https://docs.datadoghq.com/tagging/) allows to collect k6 metrics with tags to distinguish between requests for different URLs, response statuses, groups, etc. + +The instruction above runs the `DogStatsD` service in a [Docker container](https://docs.datadoghq.com/developers/dogstatsd/?tab=containeragent#agent), but it's also possible to run it either as [Host Agent](https://docs.datadoghq.com/developers/dogstatsd/?tab=hostagent#agent), [Kubernetes](https://docs.datadoghq.com/developers/dogstatsd/?tab=kubernetes#agent), and [Helm](https://docs.datadoghq.com/developers/dogstatsd/?tab=helm#agent). + +## Run the k6 test + +Once the Datadog Agent service is running, run the k6 test and send the metrics to the Agent with: + +{{< code >}} + +```bash +$ K6_STATSD_ENABLE_TAGS=true k6 run --out output-statsd script.js +``` + +{{< /code >}} + +Make sure you're using the k6 binary you built with the xk6-output-statsd extension. + +You can look at the [StatsD](https://grafana.com/docs/k6//results-output/real-time/statsd) output page for configuration options. + +## Visualize in Datadog + +While running the test, k6 sends metrics periodically to Datadog. By default, these metrics have `k6.` as the name prefix. + +You can visualize k6 metrics in real-time with the [metrics explorer](https://docs.datadoghq.com/metrics/explorer/), [monitors](https://docs.datadoghq.com/monitors/), or [custom dashboards](https://docs.datadoghq.com/graphing/dashboards/). + +![Datadog visualizing performance testing metrics](/media/docs/k6-oss/datadog-performance-testing-metrics.png) + +
+ +To learn more about all the types of k6 metrics, read the [k6 Metrics guide](https://grafana.com/docs/k6//using-k6/metrics) + +
+ +The first time Datadog detects the `k6.http_reqs` metric, the k6 integration tile is installed automatically, and the default k6 dashboard is added to your dashboard list. + +![k6 Datadog Dashboard](/media/docs/k6-oss/datadog-k6-dashboard.png) + +Optionally, you can install the k6 integration tile following these instructions: + +1. Log in to `Datadog`. +2. From the sidebar menu, choose `Integrations` > `Integrations`. +3. Search for `k6`, then select the `k6` integration. +4. Click on the `Configuration` tab option. +5. Scroll down and click on the `Install integration` button. diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/dynatrace.md b/docs/sources/k6/v0.55.x/results-output/real-time/dynatrace.md new file mode 100644 index 000000000..9ba918e31 --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/dynatrace.md @@ -0,0 +1,83 @@ +--- +title: 'Dynatrace' +description: Send k6 output to Dynatrace to visualize load test results and correlate performance testing metrics in Dynatrace. +weight: 00 +--- + +# Dynatrace + +With the [Dynatrace k6 extension](https://github.com/Dynatrace/xk6-output-dynatrace), +you can send visualize and correlate performance testing metrics with the other metrics that you monitor in Dynatrace. + +## Build the k6 version + +To build a k6 binary with the extension, first, make sure you have [Go](https://golang.org/doc/install) and [Git](https://git-scm.com/) installed on your machine. + +Then, open your terminal and run the following commands: + +```bash +# Install xk6 +go install go.k6.io/xk6/cmd/xk6@latest + +# Build the k6 binary +xk6 build --with github.com/Dynatrace/xk6-output-dynatrace + +... [INFO] Build environment ready +... [INFO] Building k6 +... [INFO] Build complete: ./k6 +``` + +xk6 will create the new k6 binary in the local folder. + +{{< admonition type="note" >}} + +To learn more about how to build custom k6 versions, check out [xk6](https://github.com/grafana/xk6). + +{{< /admonition >}} + +## Run the test + +Create a Dynatrace API token to send the data. + +{{< admonition type="caution" >}} +The Dynatrace API Token must have the scope name "metrics.ingest" (scope type `API v2`). +{{< /admonition >}} + +You can use the Dynatrace UI: + +![Dynatrace API token](/media/docs/k6-oss/dynatrace-api-token.png) + +Or a `curl` command (replace `` and the `Api-Token`): + +```bash +curl -X POST "https://.live.dynatrace.com/api/v2/apiTokens" -H "accept: application/json; charset=utf-8" -H "Content-Type: application/json; charset=utf-8" -d "{\"name\":\"\",\"scopes\":[\"metrics.ingest\"]}" -H "Authorization: Api-Token XXXXXXXX" +``` + +Use the previously built k6 binary and run the test passing the Dynatrace URL and API token as follows: + +```bash +# export dynatrace variables +export K6_DYNATRACE_URL=https://.live.dynatrace.com +export K6_DYNATRACE_APITOKEN= + +# run the test +./k6 run script.js -o output-dynatrace +``` + +Check the metrics in your Dynatrace environment, filtering for `k6`: + +![Dynatrace Test result](/media/docs/k6-oss/dynatrace-k6-metrics.png) + +![Dynatrace Test result](/media/docs/k6-oss/dynatrace-k6-test-result.png) + +### Options + +When streaming the k6 results to Dynatrace, you can configure the following Dynatrace options: + +| Name | Value | +| ------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- | +| `K6_DYNATRACE_APITOKEN` | Dynatrace API token to write the metrics. The token must have the scope `metrics.ingest API v2`. | +| `K6_DYNATRACE_FLUSH_PERIOD` | Define how often metrics are sent to Dynatrace. The default value is 1 second. | +| `K6_DYNATRACE_URL` | Dynatrace URL. The default value is `https://dynatrace.live.com`. | +| `K6_DYNATRACE_INSECURE_SKIP_TLS_VERIFY` | If `true`, the HTTP client skips TLS verification on the endpoint. The default value is `true`. | +| `K6_DYNATRACE_HEADER_` | Additional headers to include in the HTTP requests. `K6_DYNATRACE_HEADER_COOL_HEADER="cool value of the header"` | diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/elasticsearch.md b/docs/sources/k6/v0.55.x/results-output/real-time/elasticsearch.md new file mode 100644 index 000000000..8152b562b --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/elasticsearch.md @@ -0,0 +1,100 @@ +--- +title: 'Elasticsearch' +description: k6 has an output extension to store k6 metrics in Elasticsearch. This document shows you how to configure the k6 Elasticsearch integration. +weight: 00 +--- + +# Elasticsearch + +Using the [Elasticsearch k6 extension](https://github.com/elastic/xk6-output-elasticsearch), you can store k6 metrics in [Elasticsearch](https://github.com/elastic/elasticsearch) and analyze your performance results with Kibana or Grafana. + +## Build the k6 version + +To build a k6 binary with the extension, first, make sure you have [Go](https://golang.org/doc/install) and [Git](https://git-scm.com/) installed on your machine. + +Then, open your terminal and run the following commands: + +```bash +# Install xk6 +go install go.k6.io/xk6/cmd/xk6@latest + +# Build the k6 binary +xk6 build --with github.com/elastic/xk6-output-elasticsearch + +... [INFO] Build environment ready +... [INFO] Building k6 +... [INFO] Build complete: ./k6 +``` + +xk6 will create the new k6 binary in the local folder. + +{{< admonition type="note" >}} + +To learn more about how to build custom k6 versions, check out [xk6](https://github.com/grafana/xk6). + +{{< /admonition >}} + +## Run the test + +Check that the Elasticsearch instance to store the k6 metrics is running. + +If you're running on Elasticsearch cloud, use the previous k6 binary and run the test passing the cloud credentials as follows: + +```bash +# export cloud configuration +export K6_ELASTICSEARCH_CLOUD_ID=your-cloud-id-here +export K6_ELASTICSEARCH_USER=your-user-here +export K6_ELASTICSEARCH_PASSWORD=your-password-here + +# run the test +./k6 run script.js -o output-elasticsearch +``` + +k6 runs the test script and sends the metrics in real-time to Elasticsearch. + +You can also send the metrics to a local Elasticsearch cluster: + +```bash +# export local url +export K6_ELASTICSEARCH_URL=http://localhost:9200 + +# run the test +./k6 run script.js -o output-elasticsearch +``` + +{{< admonition type="caution" >}} +Security and self-signed certificates for non-cloud clusters are not yet supported. +{{< /admonition >}} + +You can now connect to Elasticsearch and query the [k6 metrics](https://grafana.com/docs/k6//using-k6/metrics) stored in the `k6-metrics` index. +The following example uses an unsecured local Elasticsearch, version `7.17.9`: + +```bash +curl -XGET 'http://localhost:9200/k6-metrics/_search?pretty' -H 'Content-Type: application/json' -d' +{ + "sort": [ + { + "Time": { + "order": "desc" + } + } + ], + "size": 10 +}' +``` + +Or use [Kibana Discover](https://www.elastic.co/guide/en/kibana/7.17/discover.html): + +![Kibana Discover k6 results](/media/docs/k6-oss/kibana-discover-test-result.png) + +### Options + +Here is the full list of options that can be configured and passed to the extension: + +| Name | Value | +| ------------------------------- | ---------------------------------------------------------------------------------- | +| `K6_ELASTICSEARCH_CLOUD_ID` | Elasticsearch `cloud.id`, which can be found in the Elastic Cloud web console. | +| `K6_ELASTICSEARCH_FLUSH_PERIOD` | Define how often metrics are sent to Elasticsearch. The default value is 1 second. | +| `K6_ELASTICSEARCH_URL` | Elasticsearch URL. | +| `K6_ELASTICSEARCH_USER` | Elasticsearch username. | +| `K6_ELASTICSEARCH_PASSWORD` | Elasticsearch password. | diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/grafana-cloud-prometheus.md b/docs/sources/k6/v0.55.x/results-output/real-time/grafana-cloud-prometheus.md new file mode 100644 index 000000000..03024d199 --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/grafana-cloud-prometheus.md @@ -0,0 +1,93 @@ +--- +title: 'Grafana Cloud Prometheus' +description: > + How to upload the test result metrics to Grafana Cloud using Grafana Cloud Prometheus and the k6 output for Prometheus remote write' +weight: 00 +--- + +# Grafana Cloud Prometheus + +{{< admonition type="caution" >}} + +This page includes instructions for running a local test that sends the test results to a Prometheus instance in Grafana Cloud. + +For running and managing cloud tests in Grafana Cloud, check out [Grafana Cloud k6](https://grafana.com/docs/grafana-cloud/testing/k6/). + +{{< /admonition >}} + +You can send your k6 results to a Prometheus instance running in [Grafana Cloud](https://grafana.com/products/cloud) for visualization and analysis. By bringing k6 metrics into Grafana, you can correlate them with other observability metrics. + +While this topic uses Grafana Cloud as an example, this approach is compatible with any remote write capable Prometheus installation. + +## Set up Grafana Cloud Prometheus + +Before you start, you need the following: + +- A Grafana Cloud account ([sign up](https://grafana.com/products/cloud/)). + The free plan includes 10,000 Prometheus series. +- The URL, username, and password of your Grafana Cloud Prometheus instance to configure the integration. + +After you've set up your account, follow these steps: + +1. Log in to `Grafana.com` and visit the [Cloud Portal](https://grafana.com/docs/grafana-cloud/fundamentals/cloud-portal/). + Select the **Details** of your Prometheus service. + + ![Grafana Cloud Portal](/media/docs/k6-oss/grafana_cloud_portal.png) + +1. Copy the URL of the Remote Write Endpoint, along with the Username and Instance ID. + + ![Grafana Cloud Prometheus Configuration](/media/docs/k6-oss/grafana_cloud_prometheus_configuration-v2.png) + +1. In the **Password / API Token** section, create and copy a [Grafana Cloud Access Policy Token](https://grafana.com/docs/grafana-cloud/account-management/authentication-and-permissions/access-policies/) with the `metrics:write` scope (permission). The token will be used as a password. + + ![Create API Key](/media/docs/k6-oss/grafana_cloud_create_api_key_metrics_publisher-v2.png) + +## Run the test + +Now, pass the Username, Access Policy Token, and Remote Write Endpoint of the Grafana Cloud Prometheus Configuration to the k6 binary: + +```bash +K6_PROMETHEUS_RW_USERNAME=USERNAME \ +K6_PROMETHEUS_RW_PASSWORD=ACCESS_POLICY_API_TOKEN \ +K6_PROMETHEUS_RW_SERVER_URL=REMOTE_WRITE_ENDPOINT \ +k6 run -o experimental-prometheus-rw script.js +``` + +## Visualize test results + +To visualize test results with Grafana, you can import the [k6 Prometheus dashboard by Grafana k6](https://grafana.com/grafana/dashboards/19665-k6-prometheus/). + +On the Dashboards UI: + +- Click `New` and select `Import`. +- Paste the Grafana URL or ID of the dashboard, and click `Load`. +- Select the Prometheus data source, and click `Import`. + +![k6 Prometheus Dashboard](/media/docs/k6-oss/k6-prometheus-dashboard-part1.png) + +Optionally, when running the test, you can set the `testid` tag as a [wide test tag](https://grafana.com/docs/k6//using-k6/tags-and-groups/#test-wide-tags) to filter results of a particular test run on this dashboard (or in PromQL queries). `testid` can be any unique string that allows you to identify the test run. + +{{< code >}} + +```bash +K6_PROMETHEUS_RW_USERNAME=USERNAME \ +K6_PROMETHEUS_RW_PASSWORD=ACCESS_POLICY_API_TOKEN \ +K6_PROMETHEUS_RW_SERVER_URL=REMOTE_WRITE_ENDPOINT \ +k6 run -o experimental-prometheus-rw --tag testid=TEST_ID script.js +``` + +{{< /code >}} + +Additionally, you can also use the [Explore UI](https://grafana.com/docs/grafana/latest/explore/) to query k6 time series, design your visualization panels, and add them to any of your existing dashboards. + +![Explore k6 metrics in Grafana Cloud](/media/docs/k6-oss/grafana_cloud_explore_k6_metrics_from_extension.png) + +All the k6 time series have a **k6\_** prefix. +For more details, refer to the documentation on the [mapping of k6 metrics with Prometheus metrics](https://grafana.com/docs/k6//results-output/real-time/prometheus-remote-write#metrics-mapping). + +It's also important to understand the default [Trend metric conversion](https://grafana.com/docs/k6//results-output/real-time/prometheus-remote-write#trend-metric-conversions) process and the format and querying limitations. The [`K6_PROMETHEUS_RW_TREND_STATS` option](https://grafana.com/docs/k6//results-output/real-time/prometheus-remote-write#options) allows you to convert trend metrics to multiple Prometheus time series. For instance, `K6_PROMETHEUS_RW_TREND_STATS=p(95),p(99),max,min` transforms each k6 trend metric into four Prometheus metrics as follows: + +- `k6_*_p95` +- `k6_*_p99` +- `k6_*_max` +- `k6_*_min` diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/influxdb.md b/docs/sources/k6/v0.55.x/results-output/real-time/influxdb.md new file mode 100644 index 000000000..64b8e6dee --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/influxdb.md @@ -0,0 +1,80 @@ +--- +title: 'InfluxDB' +description: 'k6 has an output extension to store k6 metrics in InfluxDB v2. This document shows you how to configure this integration.' +weight: 00 +--- + +# InfluxDB + +Using the [InfluxDB extension](https://github.com/grafana/xk6-output-influxdb), you can store k6 metrics in [InfluxDB v2.0](https://docs.influxdata.com/influxdb/v2.0/) and analyze your performance results with Grafana or [other tools](https://docs.influxdata.com/influxdb/cloud-serverless/query-data/tools/). + +## Build the k6 version + +To build a k6 binary with the extension, first, make sure you have [Go](https://golang.org/doc/install) and [Git](https://git-scm.com/) installed on your machine. + +Then, open your terminal and run the following commands: + +```bash +# Install xk6 +go install go.k6.io/xk6/cmd/xk6@latest + +# Build the k6 binary +xk6 build --with github.com/grafana/xk6-output-influxdb + +... [INFO] Build environment ready +... [INFO] Building k6 +... [INFO] Build complete: ./k6 +``` + +xk6 will create the new k6 binary in the local folder. + +{{< admonition type="note" >}} + +To learn more about how to build custom k6 versions, check out [xk6](https://github.com/grafana/xk6). + +{{< /admonition >}} + +## Run the test + +Check that the InfluxDB instance to store the k6 metrics is running. + +Use the previous k6 binary and run the test passing the following [options](#options): + +```bash +K6_INFLUXDB_ORGANIZATION="" \ +K6_INFLUXDB_BUCKET="" \ +K6_INFLUXDB_TOKEN="" \ +K6_INFLUXDB_ADDR="" \ +./k6 run script.js -o xk6-influxdb +``` + +k6 runs the test script and sends the [k6 metrics](https://grafana.com/docs/k6//using-k6/metrics) in real-time to the InfluxDB instance. You can now select the bucket to [query](https://docs.influxdata.com/influxdb/v2.7/query-data/) and [visualize](https://docs.influxdata.com/influxdb/v2.7/visualize-data/) the stored k6 metrics, for example, using the [InfluxDB Data Explorer](https://docs.influxdata.com/influxdb/v2.7/query-data/execute-queries/data-explorer/). + +
+ +![InfluxDB Data Explorer / k6 bucket](/media/docs/k6-oss/influxdb-data-explorer-k6-bucket.png) + +## Options + +Here is the full list of options that can be configured and passed to the extension: + +| ENV | Default | Description | +| ----------------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| K6_INFLUXDB_ORGANIZATION | | Your InfluxDB organization name. [View organizations](https://docs.influxdata.com/influxdb/v2.7/organizations/). | +| K6_INFLUXDB_BUCKET | | The bucket name to store k6 metrics data. [Manage buckets](https://docs.influxdata.com/influxdb/v2.7/organizations/buckets/). | +| K6_INFLUXDB_TOKEN | | An API token that provides authorized access to store data. [Manage API tokens](https://docs.influxdata.com/influxdb/v2.7/security/tokens/). | +| K6_INFLUXDB_ADDR | http://localhost:8086 | The address of the InfluxDB instance. | +| K6_INFLUXDB_PUSH_INTERVAL | 1s | The flush's frequency of the `k6` metrics. | +| K6_INFLUXDB_CONCURRENT_WRITES | 4 | Number of concurrent requests for flushing data. It is useful when a request takes more than the expected time (more than flush interval). | +| K6_INFLUXDB_TAGS_AS_FIELDS | vu:int,iter:int,url | A comma-separated string to set `k6` metrics as non-indexable fields (instead of tags). An optional type can be specified using :type as in vu:int will make the field integer. The possible field types are int, bool, float and string, which is the default. Example: vu:int,iter:int,url:string,event_time:int. | +| K6_INFLUXDB_INSECURE | false | When `true`, it will skip `https` certificate verification. | +| K6_INFLUXDB_PRECISION | 1ns | The timestamp [Precision](https://docs.influxdata.com/influxdb/v2.7/reference/glossary/#precision). | +| K6_INFLUXDB_HTTP_PROXY | | Sets an HTTP proxy for the InfluxDB output. | + +## Grafana Dashboards + +You can use Grafana to query and visualize data from an InfluxDB instance. The instructions are available on [InfluxDB](https://docs.influxdata.com/influxdb/v2.7/tools/grafana/) and [Grafana](https://grafana.com/docs/grafana/latest/datasources/influxdb/). + +You can also build a [custom Grafana dashboard](https://grafana.com/docs/k6//results-output/grafana-dashboards) to visualize the testing results in your own way. + +For testing purposes, the [influxdb extension](https://github.com/grafana/xk6-output-influxdb) repository includes a [docker-compose setup](https://github.com/grafana/xk6-output-influxdb#docker-compose) with two basic dashboards. diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/json.md b/docs/sources/k6/v0.55.x/results-output/real-time/json.md new file mode 100644 index 000000000..1febba1a6 --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/json.md @@ -0,0 +1,148 @@ +--- +title: 'JSON' +description: 'You can also make k6 output detailed statistics in JSON format by using the --out option.' +weight: 00 +--- + +# JSON + +You can output granular data points in JSON format. +To do so, use `k6 run` with the `--out` flag. +Pass the path for your JSON file as the flag argument: + +{{< code >}} + +```bash +$ k6 run --out json=test_results.json script.js +``` + +```docker +$ docker run -it --rm \ + -v :/scripts \ + -v :/jsonoutput \ + grafana/k6 run --out json=/jsonoutput/my_test_result.json /scripts/script.js + +# Note that the docker user must have permission to write to ! +``` + +{{< /code >}} + +Or if you want to get the result gzipped, like this: + +{{< code >}} + +```bash +$ k6 run --out json=test_results.gz script.js +``` + +```docker +$ docker run -it --rm \ + -v :/scripts \ + -v :/jsonoutput \ + grafana/k6 run --out json=/jsonoutput/my_test_result.gz /scripts/script.js + +# Note that the docker user must have permission to write to ! +``` + +{{< /code >}} + +To inspect the output in real time, you can use a command like `tail -f` on the file you save: + +```bash +$ tail -f test_results.json +``` + +## JSON format + +The JSON output has lines as follows: + +{{< code >}} + +```json +{"type":"Metric","data":{"type":"gauge","contains":"default","tainted":null,"thresholds":[],"submetrics":null},"metric":"vus"} +{"type":"Point","data":{"time":"2017-05-09T14:34:45.625742514+02:00","value":5,"tags":null},"metric":"vus"} +{"type":"Metric","data":{"type":"trend","contains":"time","tainted":null,"thresholds":["avg<1000"],"submetrics":null},"metric":"http_req_duration"} +{"type":"Point","data":{"time":"2017-05-09T14:34:45.239531499+02:00","value":459.865729,"tags":{"group":"::my group::json","method":"GET","status":"200","url":"https://httpbin.test.k6.io/get"}},"metric":"http_req_duration"} +``` + +{{< /code >}} + +Each line either has information about a metric, or logs a data point (sample) for a metric. +Lines consist of three items: + +- `type` - can have the values [Metric](#metric) or [Point](#point) where `Metric` means the line is declaring a metric, and `Point` is an actual data point (sample) for a metric. +- `data` - is a dictionary that contains lots of stuff, varying depending on the `"type"` above. +- `metric` - the name of the metric. + +### Metric + +This line has metadata about a metric. Here, `"data"` contains the following: + +- `"type"` - the metric type ("gauge", "rate", "counter" or "trend") +- `"contains"` - information on the type of data collected (can e.g. be "time" for timing metrics) +- `"tainted"` - has this metric caused a threshold to fail? +- `"threshold"` - are there any thresholds attached to this metric? +- `"submetrics"` - any derived metrics created as a result of adding a threshold using tags. + +### Point + +This line has actual data samples. Here, `"data"` contains these fields: + +- `"time"` - timestamp when the sample was collected +- `"value"` - the actual data sample; time values are in milliseconds +- `"tags"` - dictionary with tagname-tagvalue pairs that can be used when filtering results data + +## Processing JSON output + +You can use [jq][jq_url] to process the k6 JSON output. + +You can quickly create [filters][jq_filters_url] to return a particular metric of the JSON file: + +{{< code >}} + +```bash +$ jq '. | select(.type=="Point" and .metric == "http_req_duration" and .data.tags.status >= "200")' myscript-output.json +``` + +{{< /code >}} + +And calculate an aggregated value of any metric: + +{{< code >}} + +```bash +$ jq '. | select(.type=="Point" and .metric == "http_req_duration" and .data.tags.status >= "200") | .data.value' myscript-output.json | jq -s 'add/length' +``` + +{{< /code >}} + +{{< code >}} + +```bash +$ jq '. | select(.type=="Point" and .metric == "http_req_duration" and .data.tags.status >= "200") | .data.value' myscript-output.json | jq -s min +``` + +{{< /code >}} + +{{< code >}} + +```bash +$ jq '. | select(.type=="Point" and .metric == "http_req_duration" and .data.tags.status >= "200") | .data.value' myscript-output.json | jq -s max +``` + +{{< /code >}} + +For more advanced cases, check out the [jq Manual][jq_manual_url] + +[jq_url]: https://stedolan.github.io/jq/ 'jq_url' +[jq_filters_url]: https://stedolan.github.io/jq/manual/#Basicfilters 'jq_filters_url' +[jq_manual_url]: https://stedolan.github.io/jq/manual/ 'jq_manual_url' + +## Summary export + +If you want to see only the aggregated data, you can export the end-of-test summary to a JSON file. +For more details, refer to the `handleSummary()` topic in the [end-of-test summary docs](https://grafana.com/docs/k6//results-output/end-of-test). + +## Read more + +- [Metrics](https://grafana.com/docs/k6//using-k6/metrics) diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/netdata.md b/docs/sources/k6/v0.55.x/results-output/real-time/netdata.md new file mode 100644 index 000000000..af03f5bb5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/netdata.md @@ -0,0 +1,54 @@ +--- +title: 'Netdata' +description: 'You can send k6 output to Netdata. With this integration, visualize test results with zero configuration, in seconds' +weight: 00 +--- + +# Netdata + +{{< docs/shared source="k6" lookup="statsd-output.md" version="" >}} + +k6 can send performance testing metrics to [Netdata](https://netdata.cloud). This enables the user to start monitoring their k6 experiments right away, as Netdata is a monitoring tool with: + +- Auto-configuration and auto-detection of data sources +- Automatic organization of metrics into **meaningful** charts and visualization +- Per-second metric granularity + +## Before you begin + +To use the StatsD output option, you have to build a k6 binary using the [xk6-output-statsd extension](https://github.com/LeonAdato/xk6-output-statsd). For more details, refer to [StatsD](https://grafana.com/docs/k6//results-output/real-time/statsd). + +## Run Netdata + +Netdata runs on many different systems and platforms. The easiest way to download and run Netdata is through the `kickstart` script: + +```bash +bash <(curl -Ss https://my-netdata.io/kickstart.sh) +``` + +Alternatively, you can read more about installing and running Netdata in their [documentation](https://learn.netdata.cloud/docs/get-started/). + +## Setup Netdata for K6 + +Netdata runs a fully functional [StatsD](https://github.com/netdata/netdata/blob/master/collectors/statsd.plugin/README.md) server by default and we have included a default configuration file for k6 metrics. + +## Run the k6 test + +```bash +k6 run --out output-statsd script.js +``` + +Make sure you're using the k6 binary you built with the xk6-output-statsd extension. + +**Caveat**: By default, Netdata binds the StatsD server to `localhost`. That means that if Netdata and k6 are in different hosts, you will need to edit the configuration file of Netdata. + +1. Visit the [StatsD documentation](https://github.com/netdata/netdata/blob/master/collectors/statsd.plugin/README.md) for a reference on the configuration options. We are interested in the `#bind to` option. +2. Use `sudo ./edit-config netdata.conf` from inside the directory where Netdata stores its configuration files (e.g `/etc/netdata/`) and add `bind to=udp:0.0.0.0:8125`. + +## Visualize in Netdata + +Netdata will automatically create charts for your application, as illustrated in the [documentation](https://github.com/netdata/netdata/blob/master/collectors/statsd.plugin/k6.md). + +Simply head over to `localhost:19999` (assuming that you are running Netdata on the same machine) and find the k6 section. If you had opened Netdata before running the experiment, you will need to refresh the dashboard page. + +![Netdata k6 dashboard](/media/docs/k6-oss/netdata-k6-dashboard.png) diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/newrelic.md b/docs/sources/k6/v0.55.x/results-output/real-time/newrelic.md new file mode 100644 index 000000000..2377d8624 --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/newrelic.md @@ -0,0 +1,176 @@ +--- +title: 'New Relic' +description: 'You can send k6 output to New Relic. With this integration visualize load test results and correlate them your New Relic telemetry data, create and share reports, and alert on k6 telemetry.' +weight: 00 +slug: 'new-relic' +--- + +# New Relic + +{{< docs/shared source="k6" lookup="statsd-output.md" version="" >}} + +k6 can send telemetry data to [New Relic](https://newrelic.com/) through the New Relic [StatsD integration](https://docs.newrelic.com/docs/integrations/host-integrations/host-integrations-list/statsd-monitoring-integration-version-2). Within New Relic you can find your k6 performance data alongside your real users data and server side performance. This data can be visualized in dashboards and shared with others, used to compare load impact with system performance, and alert on metrics too. + +This guide covers running the New Relic integration: + +- Run the New Relic StatsD integration +- Run the k6 test +- Visualize k6 telemetry in New Relic + +## Before you begin + +To use the StatsD output option, you have to build a k6 binary using the [xk6-output-statsd extension](https://github.com/LeonAdato/xk6-output-statsd). For more details, refer to [StatsD](https://grafana.com/docs/k6//results-output/real-time/statsd). + +## Run the New Relic StatsD integration + +To get k6 metrics into New Relic, k6 sends metrics to the New Relic StatsD integration which will take care of collecting, aggregate, format and send the telemetry to the New Relic Telemetry Data Platform. You can run this with or without a New Relic agent. + +Run the New Relic integration as a Docker container with this command: + +{{< code >}} + +```bash +docker run --rm \ + -d \ + --name newrelic-statsd \ + -h $(hostname) \ + -e NR_ACCOUNT_ID= \ + -e NR_API_KEY="" \ + -p 8125:8125/udp \ + newrelic/nri-statsd:latest +``` + +{{< /code >}} + +Replace `` with your [New Relic Account ID](https://docs.newrelic.com/docs/accounts/accounts-billing/account-setup/account-id#:~:text=If%20you%20have%20a%20single,account%20ID%20is%20displayed%20there.) and `` with your [New Relic Insert API Key](https://docs.newrelic.com/docs/insights/insights-data-sources/custom-data/introduction-event-api#register). + +If your account is hosted in the New Relic EU region, then also add this to the above command: `-e NR_EU_REGION=true \` + +The _required_ environment variables used in the above command are: + +| Name | Value | +| --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `NR_ACCOUNT_ID` | The Account ID used in New Relic You can find your account ID [here](https://docs.newrelic.com/docs/accounts/accounts-billing/account-setup/account-id#:~:text=If%20you%20have%20a%20single,account%20ID%20is%20displayed%20there.). | +| `NR_API_KEY` | The Insert API Key for your New Relic account to send k6 telemetry to the account ID specified above. You can generate an Insert API key [here](https://docs.newrelic.com/docs/insights/insights-data-sources/custom-data/introduction-event-api#register). | + +_Optional_ environment variables you can use: + +| Name | Value | +| ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `NR_EU_REGION` | Setting this to `true` tells the integration your account is housed in the New Relic EU region. | +| `TAGS` | Setting tags in key:value format separated by a space lets you further understand your data in New Relic. For example identifying different test runs or machines running the tests. In the docker command add: `-e TAGS="k6Test:myExampleTest someKey:someValue" \`. | +| `NR_LOG_METRICS` | Setting this to `true` activates verbose logging for the integration. | + +### About the New Relic integration + +The New Relic StatsD integration installed above can run standalone. Installing a New Relic agent is optional. + +Everything provided in the command above is enough to send k6 performance metrics to New Relic. You can optionally however [add further configuration](https://docs.newrelic.com/docs/integrations/host-integrations/host-integrations-list/statsd-monitoring-integration-version-2#configure), [further define metrics and their formats](https://docs.newrelic.com/docs/integrations/host-integrations/host-integrations-list/statsd-monitoring-integration-version-2#metric-format) (you can however do this on the New Relic side configuration), [add custom tags](https://docs.newrelic.com/docs/integrations/host-integrations/host-integrations-list/statsd-monitoring-integration-version-2#add-tags), and [create alerts](https://docs.newrelic.com/docs/integrations/host-integrations/host-integrations-list/statsd-monitoring-integration-version-2#alerts). This is covered in the optional table below. + +## Run the k6 test + +Once the integration is running, run the k6 test and send the metrics to the integration with: + +{{< code >}} + +```bash +$ K6_STATSD_ENABLE_TAGS=true k6 run --out output-statsd script.js +``` + +{{< /code >}} + +Make sure you're using the k6 binary you built with the xk6-output-statsd extension. + +You can look at the [StatsD](https://grafana.com/docs/k6//results-output/real-time/statsd) output page for configuration options. + +## Visualisation in New Relic + +As your k6 test is running, k6 is sending performance metrics to the New Relic StatsD integration which in turn is sending these metrics to the New Relic Telemetry Data Platform. These will be prefixed with `k6.` so you can identify them. + +![k6 metrics as seen in the New Relic data explorer](/media/docs/k6-oss/new-relic-data-explorer.png) + +You can visualize the metrics sent from this integration in the [data explorer](https://docs.newrelic.com/docs/insights/use-insights-ui/explore-data/metric-explorer-search-chart-metrics-sent-new-relic-agents) in the top right of New Relic (_query your data_). + +![Sample New Relic k6 dashboard](/media/docs/k6-oss/new-relic-dashboard.png) + +You can also add these metrics to [dashboards](https://docs.newrelic.com/docs/query-your-data/explore-query-data/dashboards/introduction-new-relic-one-dashboards) and [alert on k6 metrics](https://docs.newrelic.com/docs/alerts-applied-intelligence/new-relic-alerts/alert-conditions/create-nrql-alert-conditions). + +### Example NRQL Queries + +{{< admonition type="note" >}} + +New Relic doesn't support calculating percentiles from metric data, which is the data format sent by this k6 output. See [this New Relic forum post](https://discuss.newrelic.com/t/percentiles-of-values-from-metrics-api-with-nrql-not-working/95832) and [the documentation about the metric data type](https://docs.newrelic.com/docs/data-apis/understand-data/metric-data/query-metric-data-type/) for details. + +{{< /admonition >}} + +Here are some example NRQL queries you can easily copy and paste into widgets in a New Relic dashboard, you can however stick with the [chart builder](https://docs.newrelic.com/docs/query-your-data/explore-query-data/query-builder/introduction-query-builder). Find all your k6 Metrics under the metrics tab, prefixed with `k6.` + +**Number of Virtual Users** + +{{< code >}} + +```plain +SELECT latest(k6.vus) FROM Metric TIMESERIES +``` + +{{< /code >}} + +**Max, Median, and Average Request Duration** + +{{< code >}} + +```plain +SELECT max(k6.http_req_duration.summary) AS 'Max Duration', average(k6.http_req_duration.median) AS 'Median', average(k6.http_req_duration.mean) AS 'Avg' FROM Metric TIMESERIES +``` + +{{< /code >}} + +**Rate of Requests** + +{{< code >}} + +```plain +SELECT rate(max(k6.http_reqs), 1 seconds) FROM Metric TIMESERIES +``` + +{{< /code >}} + +**Data Sent and Data Received** + +{{< code >}} + +```plain +SELECT sum(k6.data_received) as 'Data Received', max(k6.data_sent) AS 'Data Sent' FROM Metric TIMESERIES +``` + +{{< /code >}} + +**Histogram bucketing Requests** + +{{< code >}} + +```plain +SELECT histogram(`k6.http_reqs`, 80, 20) FROM Metric +``` + +{{< /code >}} + +**Change in the number of Requests** + +{{< code >}} + +```plain +SELECT derivative(k6.http_reqs, 30 seconds) AS 'Rate /reqs' FROM Metric TIMESERIES +``` + +{{< /code >}} + +**Scrolling List of all k6 Performance Metrics** + +{{< code >}} + +```plain +SELECT uniques(metricName) FROM Metric WHERE metricName LIKE 'k6%' LIMIT MAX +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/opentelemetry.md b/docs/sources/k6/v0.55.x/results-output/real-time/opentelemetry.md new file mode 100644 index 000000000..6f7df46ef --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/opentelemetry.md @@ -0,0 +1,61 @@ +--- +title: 'OpenTelemetry' +description: 'Use the OpenTelemetry experimental output to send test results in OTEL metrics format.' +weight: 00 +--- + +# OpenTelemetry + +Grafana k6 can push test run metrics in the [OpenTelemetry (OTEL) metrics format](https://opentelemetry.io/docs/specs/otel/metrics/) to an OTEL metrics collector or a metrics backend that supports the OTEL metrics format by using the experimental OpenTelemetry output `--out experimental-opentelemetry`. + +Note that the `experimental-opentelemetry` became a part of k6 only with k6 v0.53.0, so prior that version you should use the [xk6-output-opentelemetry extension](https://github.com/grafana/xk6-output-opentelemetry/). + +For any feedback, bugs or suggestions feel free to [open an issue](https://github.com/grafana/xk6-output-opentelemetry/issues) directly in the output repository. Contributions are also welcome! + +## Metrics mapping + +k6 converts all [k6 metric types](https://grafana.com/docs/k6//using-k6/metrics) to an equivalent [OTEL metric type](https://opentelemetry.io/docs/specs/otel/metrics/data-model/#timeseries-model), and all k6 tags to OTEL attributes. When possible, the units are also passed with the metrics. + +| k6 Metric | OpenTelemetry Metric | +| --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Counter | `Float64CounterOption` | +| Gauge | `Float64ObservableGauge` | +| Rate | Split into two `Int64Counter` counters named `metric_name.occurred` and `metric_name.total`. `metric_name.occurred` counts only the number of non-zero occurrences, and `metric_name.total` registers the total number of positive and negative occurrences. This might change in the future, refer to [xk6-output-opentelemetry#12](https://github.com/grafana/xk6-output-opentelemetry/issues/12) for more details. | +| Trend | `Float64Histogram` | + +## Run the k6 test + +You can use the `--out experimental-opentelemetry` option when running your tests to use this extension: + +{{< code >}} + +```bash +$ K6_OTEL_GRPC_EXPORTER_INSECURE=true K6_OTEL_METRIC_PREFIX=k6_ k6 run --tag test-id=123 -o experimental-opentelemetry examples/script.js +``` + +{{< /code >}} + +## Configuration + +The following options can be configured: + +| Name | Value | +| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `K6_OTEL_SERVICE_NAME` | The name of the service to use for the metrics exporting. Default value is `k6`. | +| `K6_OTEL_SERVICE_VERSION` | The version of the service to use for the metrics exporting. Default is version of the k6, e.g. `v0.54.0` | +| `K6_OTEL_METRIC_PREFIX` | Metric prefix. Default is empty. | +| `K6_OTEL_FLUSH_INTERVAL` | How frequently to flush metrics from k6 metrics engine. Default is `1s`. | +| `K6_OTEL_EXPORT_INTERVAL` | Configures the intervening time between metrics exports. Default is `10s`. | +| `K6_OTEL_HEADERS` | Configures headers in the W3C Correlation-Context format without additional semi-colon delimited metadata (i.e. "k1=v1,k2=v2"). Passes the headers to the exporter. | +| `K6_OTEL_TLS_INSECURE_SKIP_VERIFY` | Disables server certificate verification. | +| `K6_OTEL_TLS_CERTIFICATE` | Configures the path to the root CA certificate file for TLS credentials. If it is not provided but TLS is enabled then the host's root CAs set is used. | +| `K6_OTEL_TLS_CLIENT_CERTIFICATE` | Configures the path to the client certificate file. | +| `K6_OTEL_TLS_CLIENT_KEY` | Configures the path to the client key file. | +| `K6_OTEL_EXPORTER_TYPE` | Configures the type of exporter to use. Valid options are `http` and `grpc`. Default is `grpc`. | +| `K6_OTEL_GRPC_EXPORTER_INSECURE` | Disables client transport security for the gRPC exporter. | +| `K6_OTEL_GRPC_EXPORTER_ENDPOINT` | Configures the gRPC exporter endpoint. Default is `localhost:4317`. | +| `K6_OTEL_HTTP_EXPORTER_INSECURE` | Disables client transport security for the HTTP exporter. | +| `K6_OTEL_HTTP_EXPORTER_ENDPOINT` | Configures the HTTP exporter endpoint. Must be host and port only, without scheme. Default is `localhost:4318`. | +| `K6_OTEL_HTTP_EXPORTER_URL_PATH` | Configures the HTTP exporter path. Default is `/v1/metrics`. | + +You can also use the OpenTelemetry SDK configuration environment variables to configure the OpenTelemetry output, like the [gRPC exporter configuration](https://pkg.go.dev/go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc@v1.26.0). The `K6_OTEL_*` environment variables take precedence over the OpenTelemetry SDK configuration environment variables. diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/prometheus-remote-write.md b/docs/sources/k6/v0.55.x/results-output/real-time/prometheus-remote-write.md new file mode 100644 index 000000000..8d519f981 --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/prometheus-remote-write.md @@ -0,0 +1,258 @@ +--- +title: 'Prometheus remote write' +description: 'Use the Prometheus remote write output to send test results to any Prometheus remote write endpoint.' +weight: 00 +--- + +# Prometheus remote write + +{{< docs/shared source="k6" lookup="experimental-module.md" version="" >}} + +[Prometheus remote write](https://prometheus.io/docs/concepts/remote_write_spec/) is a protocol that makes it possible to reliably propagate data in real-time from a sender to a receiver. +It has multiple compatible [implementations](https://prometheus.io/docs/concepts/remote_write_spec/#compatible-senders-and-receivers) and [storage integrations](https://prometheus.io/docs/prometheus/latest/storage/#remote-storage-integrations). + +For instance, when using the `experimental-prometheus-rw` output, k6 can send test-result metrics to the remote-write endpoint and store them in Prometheus. + +The output, during the `k6 run` execution, gets all the generated time-series data points for the [k6 metrics](https://grafana.com/docs/k6//using-k6/metrics). +It then generates the equivalent Prometheus time series and sends them to the Prometheus remote write endpoint. + +## Metrics mapping + +All [k6 metric types](https://grafana.com/docs/k6//using-k6/metrics) are converted into an equivalent [Prometheus metric type](https://prometheus.io/docs/concepts/metric_types/). +The output maps the metrics into time series with Name labels. +As much as possible, k6 respects the [naming best practices](https://prometheus.io/docs/practices/naming) that the Prometheus project defines: + +- All time series are prefixed with the `k6_` namespace. +- All time series are suffixed with the base unit of the sample value (if k6 knows what the base unit is). +- Trends and rates have the relative suffixes, to make them more discoverable. + +| k6 | Prometheus | Name label | +| ------- | ----------------------------------------------------------------------------------------------------------- | -------------------- | +| Counter | Counter | `k6_*_total` | +| Gauge | Gauge | `k6_*_` | +| Rate | Gauge | `k6_*_rate` | +| Trend | [Counter and Gauges (default)](#1-counter-and-gauges) or [Native Histogram](#2-prometheus-native-histogram) | `k6_*_` | + +## Trend metric conversions + +This output provides two distinct mechanisms to send [k6 Trend metrics](https://grafana.com/docs/k6//using-k6/metrics) to Prometheus: + +1. [Counter and Gauge metrics](#1-counter-and-gauges) (default) +1. [Prometheus Native histogram](#2-prometheus-native-histogram) + +Both options provide efficient storage of test results while providing high-precision queries. + +Note that k6 aggregates trend metric data before sending it to Prometheus in both options. The reasons for aggregating data are: + +- Prometheus stores data in a millisecond precision (`ms`), but k6 metrics collect data points with higher accuracy, nanosecond (`ns`). +- A load test could generate vast amounts of data points. High-precision raw data could quickly become expensive and complex to scale and is unnecessary when analyzing performance trends. + +### 1. Counter and gauges + +By default, Prometheus supports [Counter and Gauge Metric types](https://prometheus.io/docs/concepts/metric_types/). Therefore, this option is the default of this output and converts all the k6 `Trend` metrics to Counter and Gauges Prometheus metrics. + +You can configure how to convert all the k6 trend metrics with the [`K6_PROMETHEUS_RW_TREND_STATS` option](#options) that accepts a comma-separated list of stats functions: `count`, `sum`, `min`, `max`, `avg`, `med`, `p(x)`. The default is `p(99)`. + +Given the list of stats functions, k6 converts all trend metrics to the respective math functions as Prometheus metrics. + +For example, `K6_PROMETHEUS_RW_TREND_STATS=p(90),p(95),max` transforms each trend metric into three Prometheus metrics as follows: + +- `k6_*_p90` +- `k6_*_p95` +- `k6_*_max` + +This option provides a configurable solution to represent `Trend` metrics in Prometheus but has the following drawbacks: + +- Convert a k6 `Trend` metric to several Prometheus metrics. +- It is impossible to aggregate some gauge values (especially percentiles). +- It uses a memory-expensive k6 data structure. + +### 2. Prometheus native histogram + +To address the limitations of the previous option, you can convert k6 trend metrics to high-fidelity histograms enabling [Prometheus native histograms](https://prometheus.io/docs/concepts/metric_types/#histogram). + +With this option, each k6 trend metric maps to its corresponding Prometheus histogram metric: `k6_*`. You can then query them using Prometheus histogram functions, such as [histogram_quantile()](https://prometheus.io/docs/prometheus/latest/querying/functions/#histogram_quantile). + +{{< admonition type="note" >}} + +🌟 To learn the benefits and outcomes of using Histograms, watch [High-resolution Histograms in Prometheus](https://www.youtube.com/watch?v=F72Tk8iaWeA). + +⚠️ Note that Native Histogram is an experimental feature released in Prometheus v2.40.0, and other remote write implementations might not support it yet. In the future, when Prometheus makes this feature stable, k6 will consider using it as the default conversion method for Trend metrics. + +{{< /admonition >}} + +## Send test metrics to a remote write endpoint + +To use remote write in Prometheus 2.x, enable the feature flag [--web.enable-remote-write-receiver](https://prometheus.io/docs/prometheus/latest/feature_flags/#remote-write-receiver). For remote write storage options, refer to the [Prometheus docs](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write). + +1. To send k6 metrics to a **remote write endpoint without native histograms**: + + - Set up a running remote write endpoint and ensure k6 can reach it. + + - Run your k6 script with the `--out` flag and the URL of the RW endpoint as follows: + + **Trend stats** + + ```bash + K6_PROMETHEUS_RW_SERVER_URL=http://localhost:9090/api/v1/write \ + k6 run -o experimental-prometheus-rw script.js + ``` + + **HTTP Basic Authentication** + + ```bash + K6_PROMETHEUS_RW_SERVER_URL=http://localhost:9090/api/v1/write \ + K6_PROMETHEUS_RW_USERNAME=USERNAME \ + K6_PROMETHEUS_RW_PASSWORD=PASSWORD \ + k6 run -o experimental-prometheus-rw script.js + ``` + + - Optionally, pass the `K6_PROMETHEUS_RW_TREND_STATS` to gain the ability to query additional stats for trend metrics. The default is `p(99)`. + + {{< code >}} + + ```bash + K6_PROMETHEUS_RW_SERVER_URL=http://localhost:9090/api/v1/write \ + K6_PROMETHEUS_RW_TREND_STATS=p(95),p(99),min,max \ + k6 run -o experimental-prometheus-rw script.js + ``` + + {{< /code >}} + +1. To send k6 metrics to a **remote write endpoint with native histograms**: + + - Enable the feature flag [--enable-feature=native-histograms](https://prometheus.io/docs/prometheus/latest/feature_flags/#native-histograms) in Prometheus 2.40.0 or higher. Set up a running remote write endpoint and ensure k6 can reach it. + + - Run your k6 script with the `--out` flag, enabling the `K6_PROMETHEUS_RW_TREND_AS_NATIVE_HISTOGRAM` option, and the URL of the RW endpoint as follows: + + **Native Histogram** + + ```bash + K6_PROMETHEUS_RW_SERVER_URL=http://localhost:9090/api/v1/write \ + K6_PROMETHEUS_RW_TREND_AS_NATIVE_HISTOGRAM=true \ + k6 run -o experimental-prometheus-rw script.js + ``` + + **HTTP Basic Authentication** + + ```bash + K6_PROMETHEUS_RW_SERVER_URL=http://localhost:9090/api/v1/write \ + K6_PROMETHEUS_RW_TREND_AS_NATIVE_HISTOGRAM=true \ + K6_PROMETHEUS_RW_USERNAME=USERNAME \ + K6_PROMETHEUS_RW_PASSWORD=PASSWORD \ + k6 run -o experimental-prometheus-rw script.js + ``` + +When running the previous `k6 run` commands, k6 starts sending time-series metrics to Prometheus. +All the time series have a [`k6_` prefix](#metrics-mapping). +In the Prometheus Web UI, they appear like this: + +![k6 metrics as seen in the Prometheus UI](/media/docs/k6-oss/query-k6-metrics-in-the-prometheus-web-ui.png) + +## Options + +k6 has special options for remote write output. + +| Name | Type | Description | +| -------------------------------------------- | ------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `K6_PROMETHEUS_RW_SERVER_URL` | `string` | URL of the Prometheus remote write implementation's endpoint. Default is `http://localhost:9090/api/v1/write` | +| `K6_PROMETHEUS_RW_HEADERS_` | `string` | Additional header to include in the HTTP requests. It can be set using the described format, for example `K6_PROMETHEUS_RW_HEADERS_CUSTOM-HEADER-KEY=custom-header-value`. | +| `K6_PROMETHEUS_RW_HTTP_HEADERS` | A comma-separated list of key-values | Additional headers to include in the HTTP requests. `K6_PROMETHEUS_RW_HTTP_HEADERS=key1:value1,key2:value2`. | +| `K6_PROMETHEUS_RW_PUSH_INTERVAL` | `string` | Interval between the metrics' aggregation and upload to the endpoint. Default is `5s`. | +| `K6_PROMETHEUS_RW_TREND_AS_NATIVE_HISTOGRAM` | `boolean` | If true, maps all the defined trend metrics as [Native Histograms](#2-prometheus-native-histogram). Default is `false`. | +| `K6_PROMETHEUS_RW_TREND_STATS` | list of `string` | If Native Histogram is not enabled, then it defines the stats functions to map for all of the defined trend metrics. It's a comma-separated list of stats functions to include (e.g. `p(90),avg,sum`). Check the trend section to see the list of supported stats. Default is `p(99)`. | +| `K6_PROMETHEUS_RW_INSECURE_SKIP_TLS_VERIFY` | `boolean` | If true, the HTTP client skips TLS verification on the endpoint. Default is `false`. | +| `K6_PROMETHEUS_RW_STALE_MARKERS` | `boolean` | If true, the output at the end of the test marks all the seen time series as stale. Default is `false`. | +| `K6_PROMETHEUS_RW_USERNAME` | `string` | Username for the HTTP Basic authentication at the Prometheus remote write endpoint. | +| `K6_PROMETHEUS_RW_PASSWORD` | `string` | Password for the HTTP Basic authentication at the Prometheus remote write endpoint. | +| `K6_PROMETHEUS_RW_CLIENT_CERTIFICATE` | `string` | A path to the PEM (Privacy-Enhanced Mail) formatted client certificate. | +| `K6_PROMETHEUS_RW_CLIENT_CERTIFICATE_KEY` | `string` | A path to the PEM formatted client private key. | +| `K6_PROMETHEUS_RW_BEARER_TOKEN` | `string` | Sets the Authorization Bearer Token Header. | +| `K6_PROMETHEUS_RW_SIGV4_REGION` | `string` | Sets the AWS region where the workspace is. Along with the others `K6_PROMETHEUS_RW_SIGV4_*` configurations enables signing requests. | +| `K6_PROMETHEUS_RW_SIGV4_ACCESS_KEY` | `string` | Sets the AWS access key. | +| `K6_PROMETHEUS_RW_SIGV4_SECRET_KEY` | `string` | Sets the AWS secret key. | + +### Stale trend metrics + +This k6 output can mark the time series at the end of the test as stale. +To enable the stale marker option, set the `K6_PROMETHEUS_RW_STALE_MARKERS` environment variable to `true`. + +By default, the metrics are active for 5 minutes after the last flushed sample. +They are automatically marked as stale after. +For details about staleness, refer to the [Prometheus docs](https://prometheus.io/docs/prometheus/latest/querying/basics/#staleness). + +## Time series visualization + +To visualize time series with Grafana, you can use the [Explore UI](https://grafana.com/docs/grafana/latest/explore/) or import any of the existing pre-built dashboards: + +- [k6 Prometheus dashboard by Grafana k6](https://grafana.com/grafana/dashboards/19665-k6-prometheus/) +- [k6 Prometheus (Native Histograms) dashboard by Grafana k6](https://grafana.com/grafana/dashboards/18030-k6-prometheus-native-histograms/) +- [Other public dashboards available from the community](https://grafana.com/grafana/dashboards/?search=k6&dataSource=prometheus) + +If you are a Grafana Cloud user, please refer to the [Grafana Cloud Prometheus docs](https://grafana.com/docs/k6//results-output/real-time/grafana-cloud-prometheus). + +For a local environment, the [`xk6-output-prometheus-remote` repository](https://github.com/grafana/xk6-output-prometheus-remote) includes a docker-compose setup that provisions the `k6 Prometheus` and `k6 Prometheus (Native Histograms)` dashboards: + +![Provisioned k6 Prometheus Dashboards](/media/docs/k6-oss/list-provisioned-prometheus-dashboards.png) + +### Docker compose example + +Clone the repository to get started and follow these steps for using the [docker-compose.yml](https://github.com/grafana/xk6-output-prometheus-remote/blob/main/docker-compose.yml) file that starts _Prometheus_ and _Grafana_: + +1. Start the docker compose environment. + + {{< code >}} + + ```shell + docker compose up -d prometheus grafana + ``` + + {{< /code >}} + + {{< code >}} + + ```shell + # Output + Creating xk6-output-prometheus-remote_grafana_1 ... done + Creating xk6-output-prometheus-remote_prometheus_1 ... done + ``` + + {{< /code >}} + + Prometheus is started with Native Histogram enabled. You can use the same Prometheus instance to receive k6 trend metrics as native histograms or multiple metric stats. + +1. Run the k6 test with one of the options detailed on [Send test metrics to a remote write endpoint](#send-test-metrics-to-a-remote-write-endpoint). + + **Trend stats** + + ```bash + K6_PROMETHEUS_RW_TREND_STATS=p(95),p(99),min,max \ + k6 run -o experimental-prometheus-rw script.js + ``` + + **Native Histograms** + + ```bash + K6_PROMETHEUS_RW_TREND_AS_NATIVE_HISTOGRAM=true \ + k6 run -o experimental-prometheus-rw script.js + ``` + + Optionally, you can set the `testid` tag as a [wide test tag](https://grafana.com/docs/k6//using-k6/tags-and-groups/#test-wide-tags) to segment metrics into discrete test runs and filter specific test results on the pre-built Grafana dashboards or in PromQL queries. `testid` can be any unique string that let you clearly identify the test run. + + **Trend stats** + + ```bash + K6_PROMETHEUS_RW_TREND_STATS=p(95),p(99),min,max \ + k6 run -o experimental-prometheus-rw --tag testid= script.js + ``` + + **Native Histograms** + + ```bash + K6_PROMETHEUS_RW_TREND_AS_NATIVE_HISTOGRAM=true \ + k6 run -o experimental-prometheus-rw --tag testid= script.js + ``` + +1. After running the test, visit [http://localhost:3000](http://localhost:3000). If you enabled native histograms, select the **k6 Prometheus (Native Histograms)** dashboard; otherwise, select the **k6 Prometheus** Dashboard. + + ![k6 Prometheus Dashboard](/media/docs/k6-oss/k6-prometheus-dashboard.png) diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/statsd.md b/docs/sources/k6/v0.55.x/results-output/real-time/statsd.md new file mode 100644 index 000000000..e943a3de7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/statsd.md @@ -0,0 +1,60 @@ +--- +title: 'StatsD' +description: 'k6 has a built-in output to a StatsD service.' +weight: 00 +--- + +# StatsD + +{{< docs/shared source="k6" lookup="statsd-output.md" version="" >}} + +k6 can push test metrics to a [StatsD](https://github.com/statsd/statsd) service by using the [xk6-output-statsd extension](https://github.com/LeonAdato/xk6-output-statsd). + +## Build the k6 version + +To build a k6 binary with the extension, first, make sure you have [Go](https://golang.org/doc/install) and [Git](https://git-scm.com/) installed on your machine. + +Then, open your terminal and run the following commands: + +```bash +# Install xk6 +go install go.k6.io/xk6/cmd/xk6@latest + +# Build the k6 binary +xk6 build --with github.com/LeonAdato/xk6-output-statsd + +... [INFO] Build environment ready +... [INFO] Building k6 +... [INFO] Build complete: ./k6 +``` + +xk6 will create the new k6 binary in the local folder. + +{{< admonition type="note" >}} + +To learn more about how to build custom k6 versions, check out [xk6](https://github.com/grafana/xk6). + +{{< /admonition >}} + +## Run the k6 test + +Using the k6 binary you built in the previous step, you can use the `--out output-statsd` option when running your tests to use this extension: + +{{< code >}} + +```bash +$ ./k6 run --out output-statsd script.js +``` + +{{< /code >}} + +The following options can be configured: + +| Name | Value | +| ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- | +| `K6_STATSD_ADDR` | Address of the statsd service, currently only UDP is supported. The default value is `localhost:8125`. | +| `K6_STATSD_NAMESPACE` | The namespace used as a prefix for all the metric names. The default value is `k6`. | +| `K6_STATSD_PUSH_INTERVAL` | Configure how often data batches are sent. The default value is `1s`. | +| `K6_STATSD_BUFFER_SIZE` | The buffer size. The default value is `20`. | +| `K6_STATSD_ENABLE_TAGS` | If `true` enables sending tags. `false` by default as old versions of statsd, prior to v0.9.0 did not support tags. | +| `K6_STATSD_TAG_BLOCKLIST` | This is a comma-separated list of tags that should NOT be sent to statsd. For example, "tag1,tag2". The default value is `vu,iter,url`. | diff --git a/docs/sources/k6/v0.55.x/results-output/real-time/timescaledb.md b/docs/sources/k6/v0.55.x/results-output/real-time/timescaledb.md new file mode 100644 index 000000000..64c0ccf47 --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/real-time/timescaledb.md @@ -0,0 +1,74 @@ +--- +title: 'TimescaleDB' +description: k6 has an output extension to store k6 metrics in TimescaleDB. This document shows you how to configure the k6 TimescaleDB integration. +weight: 00 +--- + +# TimescaleDB + +Using the [TimescaleDB k6 extension](https://github.com/grafana/xk6-output-timescaledb), you can store k6 metrics in [TimescaleDB](https://www.timescale.com/) and analyze your performance results with SQL and dashboards. The extension repository includes two Grafana dashboards. + +## Build the k6 version + +To build a k6 binary with the extension, first, make sure you have [Go](https://golang.org/doc/install) and [Git](https://git-scm.com/) installed on your machine. + +Then, open your terminal and run the following commands: + +```bash +# Install xk6 +go install go.k6.io/xk6/cmd/xk6@latest + +# Build the k6 binary +xk6 build --with github.com/grafana/xk6-output-timescaledb + +... [INFO] Build environment ready +... [INFO] Building k6 +... [INFO] Build complete: ./k6 +``` + +xk6 will create the new k6 binary in the local folder. + +{{< admonition type="note" >}} + +To learn more about how to build custom k6 versions, check out [xk6](https://github.com/grafana/xk6). + +{{< /admonition >}} + +## Run the test + +Check that the TimescaleDB instance is running. Copy the Postgres connection string of the database, which will store the k6 metrics. + +Use the previous k6 binary and run the test passing the Postgres connection string to the [output option](https://grafana.com/docs/k6//using-k6/k6-options/reference#results-output) as follows: + +```bash +k6 run script.js -o timescaledb=postgresql://:@:/ +``` + +k6 runs the test script and sends the metrics in real-time to the TimescaleDB instance. You can now connect to TimescaleDB and query the [k6 metrics](https://grafana.com/docs/k6//using-k6/metrics). + +```bash +k6=# SELECT metric,AVG (value) FROM samples GROUP BY metric; +``` + +### Options + +Here is the full list of options that can be configured and passed to the extension: + +| Name | Value | +| ------------------------------ | ------------------------------------------------------------------------------------- | +| `K6_TIMESCALEDB_PUSH_INTERVAL` | Define how often metrics are sent to TimescaleDB. The default value is 1s (1 second). | + +## Grafana Dashboards + +The extension repository includes a [docker-compose setup](https://github.com/grafana/xk6-output-timescaledb/#docker-compose) with two pre-built dashboards to: + +- list test runs +- visualize the results of a test run + +![TimescaleDB list test runs](/media/docs/k6-oss/timescaledb-dashboard-test-runs.png) + +![TimescaleDB k6 results](/media/docs/k6-oss/timescaledb-dashboard-test-result.png) + +## Read more + +- [Store k6 metrics in TimescaleDB and visualize with Grafana](https://k6.io/blog/store-k6-metrics-in-timescaledb-and-visualize-with-grafana/) diff --git a/docs/sources/k6/v0.55.x/results-output/web-dashboard/_index.md b/docs/sources/k6/v0.55.x/results-output/web-dashboard/_index.md new file mode 100644 index 000000000..7f2f370ab --- /dev/null +++ b/docs/sources/k6/v0.55.x/results-output/web-dashboard/_index.md @@ -0,0 +1,85 @@ +--- +title: Web dashboard +description: Track test results in real-time with the web-dashboard and generate HTML test reports directly from your web browser. +weight: 200 +--- + +# Web dashboard + +k6 provides a built-in web dashboard that you can enable to visualize and monitor your tests results in real-time. + +![Web dashboard screenshot](/media/docs/k6-oss/web-dashboard-overview.png) + +The dashboard provides a real-time overview of the performance observed by k6 while a test is running, and can help you identify potential reliability issues as they occur. + +## How to use + +The web dashboard is a built-in feature of k6. You can enable it by setting the `K6_WEB_DASHBOARD` environment variable to `true` when running your test script, for example: + +```shell +K6_WEB_DASHBOARD=true k6 run script.js +``` + +```shell +K6_WEB_DASHBOARD=true ./k6 run script.js + + /\ |‾‾| /‾‾/ /‾‾/ + /\ / \ | |/ / / / + / \/ \ | ( / ‾‾\ + / \ | |\ \ | (‾) | + / __________ \ |__| \__\ \_____/ .io + + execution: local + script: ../extensions/xk6-dashboard/script.js + web dashboard: http://127.0.0.1:5665 + output: - +``` + +By default, the web dashboard is available on localhost port `5665`. You can change the host and port by using the [dashboard options](#dashboard-options). + +{{< admonition type="note" >}} + +The k6 process waits to exit as long as there's at least one open browser window for the dashboard extension. + +In certain environments, such as a CI/CD pipeline, the k6 process has to exit after the test run completes. In that case, it's advisable to disable the HTTP port by setting it to `-1`. + +{{< /admonition >}} + +## Generate HTML test reports + +You can generate detailed, downloadable HTML reports directly from the web dashboard or the command line. These reports are self-contained, making them ideal for sharing with your team. + +![HTML test report screenshot](/media/docs/k6-oss/web-dashboard-report.png) + +### Generate report from web dashboard + +To generate a report from the web dashboard, click **Report** on the dashboard's menu. + +![HTML test report generation button](/media/docs/k6-oss/web-dashboard-report-button.png) + +### Generate report from the command line + +To automatically generate a report from the command line once the test finishes running, use the `K6_WEB_DASHBOARD_EXPORT` option. For example: + +```shell +K6_WEB_DASHBOARD=true K6_WEB_DASHBOARD_EXPORT=html-report.html k6 run script.js +``` + +{{< admonition type="note" >}} + +The report only includes graphs if the test duration is greater than three times the aggregation period value, set by the `K6_WEB_DASHBOARD_PERIOD` variable. + +{{< /admonition >}} + +## Dashboard options + +The web dashboard can be configured using environment variables: + +| Environment variable | Description | Default value | +| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | +| `K6_WEB_DASHBOARD` | Enable the web dashboard | `false` | +| `K6_WEB_DASHBOARD_HOST` | Host to bind the web dashboard to | `localhost` | +| `K6_WEB_DASHBOARD_PORT` | Port to bind the web dashboard to | `5665` | +| `K6_WEB_DASHBOARD_PERIOD` | Period in seconds to update the web dashboard | `10s` | +| `K6_WEB_DASHBOARD_OPEN` | Open the web dashboard in the default browser | `false` | +| `K6_WEB_DASHBOARD_EXPORT ` | Filename to automatically export the HTML test report to at the end of the test run. By default, the value is empty and the report isn't exported. | `` | diff --git a/docs/sources/k6/v0.55.x/set-up/_index.md b/docs/sources/k6/v0.55.x/set-up/_index.md new file mode 100644 index 000000000..f14c94ac0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/_index.md @@ -0,0 +1,10 @@ +--- +weight: 150 +title: Set up +--- + +# Set up + + + +{{< section depth=2 >}} diff --git a/docs/sources/k6/v0.55.x/set-up/configure-your-code-editor.md b/docs/sources/k6/v0.55.x/set-up/configure-your-code-editor.md new file mode 100644 index 000000000..1b9a69b5f --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/configure-your-code-editor.md @@ -0,0 +1,47 @@ +--- +aliases: + - ./configure-k6-intellisense # docs/k6//set-up/configure-k6-intellisense + - ./intellisense # docs/k6//set-up/intellisense + - ../misc/intellisense # docs/k6//misc/intellisense +title: 'Configure your code editor' +description: 'k6 has its TypeScript Type Definition that you can configure with your editor to unlock code editing features.' +weight: 100 +--- + +# Configure your code editor + +[IntelliSense](https://code.visualstudio.com/docs/editor/intellisense) refers to code editing features like **intelligent code completion** and **quick access to documentation**. These features can significantly improve the developer experience and productivity when working on k6 scripts in your editor of choice. Notable features are: + +- Auto-completion of k6 functions, methods, and classes. +- Auto imports of k6 modules. +- Access to k6 documentation when writing and hovering code. + +![Intellisense enabled in a code editor, showing autocompletion of k6 libraries](/media/docs/k6-oss/intellisense-k6-demo.gif) + +## Install k6 type definitions + +k6 has its [TypeScript Type Definition](https://www.npmjs.com/package/@types/k6) that you can configure with your editor to unlock code editing features. + +In Visual Studio Code and IntelliJ IDEA Ultimate, you can configure IntelliSense to recognize the [k6 JavaScript API](https://grafana.com/docs/k6//javascript-api) by installing the k6 Types with a package manager. + +```bash +# create a `package.json` file +$ npm init --yes + +# install the k6 types as dev dependency +$ npm install --save-dev @types/k6 +``` + +## Code editor extensions + +You can also find k6 code editor extensions for Visual Studio Code and IntelliJ IDEA: + +- [Visual Studio Code - k6 Extension](https://marketplace.visualstudio.com/items?itemName=k6.k6) +- [IntelliJ IDEA - k6 Plugin](https://plugins.jetbrains.com/plugin/16141-k6) + +## Next steps + +Now that you have k6 configured in your code editor, you can: + +- Head over to [Running k6](https://grafana.com/docs/k6//get-started/running-k6/) to learn how to create and run your first test. +- Configure your editor for [TypeScript support](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Editor-Support). diff --git a/docs/sources/k6/v0.55.x/set-up/fine-tune-os.md b/docs/sources/k6/v0.55.x/set-up/fine-tune-os.md new file mode 100644 index 000000000..e78e95710 --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/fine-tune-os.md @@ -0,0 +1,352 @@ +--- +aliases: + - ../misc/fine-tuning-os # docs/k6//misc/fine-tuning-os + - ./fine-tuning-os +title: 'Fine-tune OS' +description: 'Learn how to inspect the OS imposed limits of your system, and tweak them to be able to run larger tests.' +weight: 200 +--- + +# Fine-tune OS + +{{< admonition type="note" >}} + +This page focuses on helping users run large test scripts locally. If you're just getting started with k6, and haven't run into a `Too Many Open Files` error, you can skip these instructions. + +{{< /admonition >}} + +When running large test scripts locally, users sometimes run into limits within their OS that prevent them from making the necessary number of requests to complete the test. +This limit usually manifests itself in a `Too Many Open Files` error. +These limits, if unchanged, can be a severe bottleneck if you choose to run a bigger or complicated test locally on your machine. + +This article shows you how to inspect the OS-imposed limits of your system, tweak them, and scale for larger tests. + +Important to note here is that everything that we cover in this article needs to be approached with a healthy dose of caution. +As with any changes to your OS, we discourage blindly changing your system settings to a specific value. You should document ways of testing that shows a clear before-and-after relation. +E.g. before changing MSL / TIME_WAIT period, confirm that you’re experiencing the issue (error messages, netstat, ss, etc.), change settings conservatively, re-run the test, and note any improvement. +This way you can gauge the effect of the optimization, find any negative side-effects, and come up with a range of recommended values. + +## Before you begin + +- The following modifications have been tested for Linux, and macOS Sierra 10.12 and above. If you're using an older macOS version, the process for changing these settings might differ. + +## Network resource limit + +Unix operating system derivatives such as GNU/Linux, BSDs and macOS, can limit the amount of system resources available to a process to safeguard system stability. This includes the total amount of memory, CPU time, or amount of open files a single process is allowed to manage. + +Since in Unix everything is a file, including network connections, application testing tools that heavily use the network, such as k6, might reach the configured limit of allowed open files, depending on the amount of network connections used in a particular test. + +As mentioned in our opening section, this results in a message like the following being shown during a test: + +```bash +WARN[0127] Request Failed error="Get http://example.com/: dial tcp example.com: socket: too many open files" +``` + +This message means that the network resource limit has been reached, which will prevent k6 from creating new connections, thus altering the test result. In some cases this may be desired, to measure overall system performance, for example, but in most cases this will be a bottleneck towards testing the HTTP server and web application itself. + +The next sections describe ways to increase this resource limit, and allow k6 to run tests with hundreds or thousands of concurrent VUs from a single system. + +### Viewing limits configuration + +Unix systems have two types of resource limits: + +- **Hard limits.** The absolute maximum allowed for each user, and can be configured only by the root user. +- **Soft limits.** These can be configured by each user, but cannot be above the hard limit setting. + +### Linux + +On GNU/Linux, you can see the configured limits with the ulimit command. + +`ulimit -Sa` will show all soft limits for the current user: + +```bash +$ ulimit -Sa +core file size (blocks, -c) 0 +data seg size (kbytes, -d) unlimited +scheduling priority (-e) 0 +file size (blocks, -f) unlimited +pending signals (-i) 3736 +max locked memory (kbytes, -l) 16384 +max memory size (kbytes, -m) unlimited +open files (-n) 1024 +pipe size (512 bytes, -p) 8 +POSIX message queues (bytes, -q) 819200 +real-time priority (-r) 0 +stack size (kbytes, -s) 8192 +cpu time (seconds, -t) unlimited +max user processes (-u) 3736 +virtual memory (kbytes, -v) unlimited +file locks (-x) unlimited +``` + +While `ulimit -Ha` will show all hard limits for the current user: + +```bash +$ ulimit -Ha +core file size (blocks, -c) unlimited +data seg size (kbytes, -d) unlimited +scheduling priority (-e) 0 +file size (blocks, -f) unlimited +pending signals (-i) 3736 +max locked memory (kbytes, -l) 16384 +max memory size (kbytes, -m) unlimited +open files (-n) 1048576 +pipe size (512 bytes, -p) 8 +POSIX message queues (bytes, -q) 819200 +real-time priority (-r) 0 +stack size (kbytes, -s) unlimited +cpu time (seconds, -t) unlimited +max user processes (-u) 3736 +virtual memory (kbytes, -v) unlimited +file locks (-x) unlimited +``` + +Note the difference of open files being a maximum of 1024 for the soft limit, while it's 1048576 for the hard limit. + +### macOS + +In macOS however, you will have a couple of different system imposed limits to take into consideration. + +The first one is `launchctl limit maxfiles` which prints the per-process limits which are specified also as a soft limit and a hard limit. When a soft limit is exceeded a process may receive a signal (for example, if the CPU time or file size is exceeded), but it will be allowed to continue execution until it reaches the hard limit (or modifies its resource limit). +`kern.maxfiles` is the limit of total file descriptors on the entire system - the sum total of all the open files for all processes plus all the files the kernel has open for its own purposes. + +`sysctl kern.maxfiles` + +`sysctl kern.maxfilesperproc` + +So, to reiterate, running commands above will show you the system limits on open files and running processes. + +## Changing limits configuration + +The first thing you should consider before changing the configuration is the amount of network connections you expect your test to require. The http_reqs metric in the k6 result summary can hint at this, but a baseline calculation of number of max. VUs \* number of HTTP requests in a single VU iteration will deliver a fair approximation. Note that k6 also deals with text files and other resources that count towards the "open files" quota, but network connections are the biggest consumers. + +### Disabling limits in macOS + +Before we can change any system imposed limits in macOS we will need to disable a security feature put in place to prevent us in doing so. You will need to disable System Integrity Protection that was introduced in OS X El Capitan to prevent certain system-owned files and directories from being modified by processes without the proper privileges. + +To disable it you will need to restart your Mac and hold down `Command + R` while it boots. This will boot it into Recovery Mode. + +There you should navigate to `Utilities` which are located in the menu bar at the top of the screen, then open `Terminal`. Once you have it open, enter the following command: + +`csrutil disable` + +Once you press enter and close the Terminal, you can reboot your Mac normally and log into your account. + +### Changing soft limits + +#### Linux + +So, let's say that we want to run a 1000 VU test which makes 4 HTTP requests per iteration. In this case we could increase the open files limit to 5000, to account for additional non-network file usage. This can be done with the following command: + +```bash +$ ulimit -n 5000 +``` + +This changes the limit only for the current shell session. + +If we want to persist this change for future sessions, we can add this to a shell startup file. For Bash this would be: + +```bash +$ echo "ulimit -n 5000" >> ~/.bashrc +``` + +#### macOS + +If the soft limit is too low, set the current session to (values written here are usually close to default ones) : + +```bash +sudo launchctl limit maxfiles 65536 200000 +``` + +Since sudo is needed, you are prompted for a password. + +### Changing hard limits + +#### Linux + +If the above command results in an error like cannot modify limit: Operation not permitted or value exceeds hard limit, that means that the hard limit is too low, which as mentioned before, can only be changed by the root user. + +This can be done by modifying the `/etc/security/limits.conf` file. + +For example, to set both soft and hard limits of the amount of open files per process for the alice account, open `/etc/security/limits.conf` as root in your text editor of choice and add the following lines: + +```bash +alice soft nofile 5000 +alice hard nofile 1048576 +``` + +The new limits will be in place after logging out and back in. + +Alternatively, \* hard nofile 1048576 would apply the setting for all non-root user accounts, and root hard nofile 1048576 for the root user. See the documentation in that file or man bash for the ulimit command documentation. + +#### macOS + +Next step will be to configure your new file limits. Open terminal and paste the following command: + +```bash +sudo nano /Library/LaunchDaemons/limit.maxfiles.plist +``` + +This will open a text editor inside your terminal window where you will be prompted to provide your user password and then paste the following: + +```markup + + + + + Label + limit.maxfiles + ProgramArguments + + launchctl + limit + maxfiles + 64000 + 524288 + + RunAtLoad + + ServiceIPC + + + +``` + +Pressing `Control + X` will save the changes and exit the editor. By pasting and saving this we have introduced two different limitations to your maxfiles limit. The first one (64000) is a soft limit, which if reached, will prompt your Mac to prepare to stop allowing new file opens but still let them open. If the second one is reached (524288), a hard limit, you will again start seeing your old friend, the 'too many open files' error message. + +We will use the same procedure to increase the processes limit next. + +While in Terminal create a similar file with this command: + +```bash +sudo nano /Library/LaunchDaemons/limit.maxproc.plist +``` + +Again, after prompted for your password, you can paste the following and save and close with `Control + X` + +```markup + + + + + Label + limit.maxproc + ProgramArguments + + launchctl + limit + maxproc + 2048 + 4096 + + RunAtLoad + + ServiceIPC + + + +``` + +All that is left after this is to reboot your Mac back to the Recovery Mode, open the Terminal, turn the SIP back on with `csrutil enable` and check if the limits were changed with commands we used at the beginning. + +In most cases these limits should be enough to run most of your simple tests locally for some time, but you can modify the files above to any values you will need in your testing. + +{{< admonition type="warning" >}} + +Please be aware that all of these limitations are put in place to protect your operating system from files and applications that are poorly written and might leak memory like in huge quantities. We would suggest not going too overboard with the values, or you might find your system slowing down to a crawl if or when it runs out of RAM. + +{{< /admonition >}} + +## Local port range + +When creating an outgoing network connection the kernel allocates a local (source) port for the connection from a range of available ports. + +### GNU/Linux + +On GNU/Linux you can see this range with: + +```bash +$ sysctl net.ipv4.ip_local_port_range net.ipv4.ip_local_port_range = 32768 60999 +``` + +While 28,231 ports might be sufficient for most use cases, this might be a limiting factor if you’re testing with thousands of connections. You can increase it with, for example: + +```bash +sysctl -w net.ipv4.ip_local_port_range="16384 65000" +``` + +Be aware that this range applies to both TCP and UDP, so be conservative with the values you choose and increase as needed. + +To make the changes permanent, add `net.ipv4.ip_local_port_range=16384 65000` to `/etc/sysctl.conf`. +Last resort tweaks +If you still experience network issues with the above changes, consider enabling net.ipv4.tcp_tw_reuse: + +```bash +sysctl -w net.ipv4.tcp_tw_reuse=1 +``` + +This will enable a feature to quickly reuse connections in TIME_WAIT state, potentially yielding higher throughput. + +### macOS/Linux + +On macOS the default ephemeral port range is 49152 to 65535, for a total of 16384 ports. You can check this with the sysctl command: + +```bash +$ sysctl net.inet.ip.portrange.first net.inet.ip.portrange.last + +net.inet.ip.portrange.first: 49152 +net.inet.ip.portrange.last: 65535 +``` + +Once you run out of ephemeral ports, you will normally need to wait until the TIME_WAIT state expires (2 \* maximum segment lifetime) until you can reuse a particular port number. You can double the number of ports by changing the range to start at 32768, which is the default on Linux and Solaris. (The maximum port number is 65535 so you cannot increase the high end.) + +```bash +$ sudo sysctl -w net.inet.ip.portrange.first=32768 + +net.inet.ip.portrange.first: 49152 -> 32768 +``` + +Note that the official range designated by IANA is 49152 to 65535, and some firewalls may assume that dynamically assigned ports fall within that range. You may need to reconfigure your firewall to use a larger range outside of your local network. + +## General optimizations + +This section goes over some optimizations that are not necessarily dependant on your OS, but may affect your testing. + +### RAM usage + +Depending on the particular k6 test: maximum number of VUs used, number and size of JavaScript dependencies, and complexity of the test script itself, k6 can consume large amounts of system RAM during test execution. While the development is focused on reducing RAM usage as much as possible, a single test run might use tens of gigabytes of RAM under certain scenarios. + +As a baseline, count each VU instance to require between 1MB and 5MB of RAM, depending on your script complexity and dependencies. This is roughly between 1GB and 5GB of required system RAM for a 1,000 VU test, so make sure that sufficient physical RAM is available to meet your test demands. + +If you need to decrease the RAM usage, you could use the option `--compatibility-mode=base`. Read more on [JavaScript Compatibility Mode](https://grafana.com/docs/k6//using-k6/javascript-compatibility-mode). + +### Virtual memory + +In addition to physical RAM, ensure that the system is configured with an appropriate amount of virtual memory, or swap space, in case higher memory usage bursts are required. + +You can see the status and amount of available swap space on your system with the commands swapon or free. + +We won't go into swap configuration details here, but you can find several guides online. + +### Network performance + +Because k6 can generate and sustain large amounts of network traffic, it also stresses the network stack of modern operating systems. Under certain loads or network conditions it's possible to achieve higher throughput and better performance by tweaking some network settings of the operating system or restructuring the network conditions of the test. + +### TCP TIME_WAIT period + +TCP network applications, such as web clients and servers, are assigned a network socket pair (a unique combination of local address, local port, remote address, and remote port) for each incoming or outgoing connection. Typically this socket pair is used for a single HTTP request/response session, and closed soon after. However, even after a connection is successfully closed by the application, the kernel might still reserve resources for quickly reopening the same socket if a new matching TCP segment arrives. This also occurs during network congestion where some packets get lost in transmission. This places the socket in a TIME_WAIT state, and is released once the TIME_WAIT period expires. This period is typically configured between 15 seconds and 2 minutes. + +The problem some applications like k6 might run into is causing a high number of connections to end up in the TIME_WAIT state, which can prevent new network connections being created. + +In these scenarios, before making changes to the system network configuration, which might have adverse side-effects for other applications, it's preferable to first take some common testing precautions. +Use different server ports or IPs + +Since sockets are uniquely created for a combination of local address, local port, remote address and remote port, a safe workaround for avoiding TIME_WAIT congestion is using different server ports or IP addresses. + +For example, you can configure your application to run on ports :8080, :8081, :8082, etc. and spread out your HTTP requests across these endpoints. + +## Read more + +- [Running large tests](https://grafana.com/docs/k6//testing-guides/running-large-tests) +- [JavaScript Compatibility Mode](https://grafana.com/docs/k6//using-k6/javascript-compatibility-mode) diff --git a/docs/sources/k6/v0.55.x/set-up/install-k6/_index.md b/docs/sources/k6/v0.55.x/set-up/install-k6/_index.md new file mode 100644 index 000000000..2169312fe --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/install-k6/_index.md @@ -0,0 +1,90 @@ +--- +aliases: + - ../get-started/installation # docs/k6//get-started/installation + - ./installation +title: 'Install k6' +description: 'k6 has packages for Linux, Mac, and Windows. As alternatives, you can also using a Docker container or a standalone binary.' +weight: 01 +--- + +# Install k6 + +k6 has packages for Linux, Mac, and Windows. Alternatively, you can use a Docker container or a standalone binary. + +## Linux + +### Debian/Ubuntu + +```bash +sudo gpg -k +sudo gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69 +echo "deb [signed-by=/usr/share/keyrings/k6-archive-keyring.gpg] https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list +sudo apt-get update +sudo apt-get install k6 +``` + +### Fedora/CentOS + +Using `dnf` (or `yum` on older versions): + +```bash +sudo dnf install https://dl.k6.io/rpm/repo.rpm +sudo dnf install k6 +``` + +## MacOS + +Using [Homebrew](https://brew.sh/): + +```bash +brew install k6 +``` + +## Windows + +If you use the [Chocolatey package manager](https://chocolatey.org/) you can install the unofficial k6 package with: + +``` +choco install k6 +``` + +If you use the [Windows Package Manager](https://github.com/microsoft/winget-cli), install the official packages from the k6 manifests [(created by the community)](https://github.com/microsoft/winget-pkgs/tree/master/manifests/k/k6/k6): + +``` +winget install k6 --source winget +``` + +Alternatively, you can download and run [the latest official installer](https://dl.k6.io/msi/k6-latest-amd64.msi). + +## Docker + +```bash +docker pull grafana/k6 +``` + +We also have a separate image you can use with `chromium` installed to run k6 browser tests. + +```bash +docker pull grafana/k6:master-with-browser +``` + +## Download the k6 binary + +Our [GitHub Releases page](https://github.com/grafana/k6/releases) has a standalone binary for all platforms. After downloading and extracting the archive for your platform, place the `k6` or `k6.exe` binary in your `PATH` to run `k6` from any location. + +## Using k6 extensions + +If you use one or more [k6 extensions](https://grafana.com/docs/k6//extensions), you need a k6 binary built with your desired extensions. +Head to [Explore extension](https://grafana.com/docs/k6//extensions/explore) to get started. + +## Troubleshooting + +If installation fails, check the [list of common installation issues](https://grafana.com/docs/k6//set-up/install-k6/troubleshooting). +If your problem is not listed and persists, reach out via the channel `#community-discussion` on our [official Slack](https://k6io.slack.com/), or report it on our [community forum](https://community.grafana.com/). + +## Next steps + +Now that you have k6 installed in your machine, you can: + +- Head over to [Running k6](https://grafana.com/docs/k6//get-started/running-k6/) to learn how to create and run your first test. +- Refer to [Configure k6 Intellisense](https://grafana.com/docs/k6//set-up/configure-k6-intellisense/) to set up code editing features to your code editor, such as auto-completion of k6 functions. diff --git a/docs/sources/k6/v0.55.x/set-up/install-k6/troubleshooting.md b/docs/sources/k6/v0.55.x/set-up/install-k6/troubleshooting.md new file mode 100644 index 000000000..2246b0469 --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/install-k6/troubleshooting.md @@ -0,0 +1,40 @@ +--- +aliases: + - ../get-started/troubleshooting # docs/k6//get-started/troubleshooting + - ../get-started/installation/troubleshooting/ # docs/k6//get-started/installation/troubleshooting +title: 'Troubleshooting' +description: 'Instructions to fix the most common installation issues.' +weight: 200 +--- + +# Troubleshooting + +## System lacks ca-certificates or gnupg2 + +Some Linux distributions don't come bundled with the `ca-certificates` and `gnupg2` packages. +If you use such a distribution, you'll need to install them with: + +```bash +sudo apt-get update && sudo apt-get install -y ca-certificates gnupg2 +``` + +This example is for Debian/Ubuntu and derivatives. Consult your distribution's documentation if you use another one. + +## Behind a firewall or proxy + +Some users have reported that they can't download the key from Ubuntu's keyserver. +When they run the `gpg` command, their firewalls or proxies block their requests to download. +If this issue affects you, you can try this alternative: + +```bash +curl -s https://dl.k6.io/key.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/k6-archive-keyring.gpg +``` + +## Old rpm-based Linux distributions + +Distributions like Amazon Linux 2 and CentOS before version 8 don't support the PGP V4 signature we use. +You'll need to disable the verification when you install k6: + +```bash +sudo yum install --nogpgcheck k6 +``` diff --git a/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/_index.md b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/_index.md new file mode 100644 index 000000000..06687090b --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/_index.md @@ -0,0 +1,19 @@ +--- +weight: 150 +title: Set up distributed k6 +--- + +# Set up distributed k6 + +It's possible to run large load tests even when using a single node, or single machine. But, depending on your use case, you might also want to run a distributed Grafana k6 test in your own infrastructure. + +A couple of reasons why you might want to do this: + +- You run your application in Kubernetes and would like k6 to be executed in the same fashion as all your other infrastructure components. +- You want to run your tests within your private network for security and/or privacy reasons. + +[k6 Operator](https://github.com/grafana/k6-operator) is a Kubernetes operator that you can use to run distributed k6 tests in your cluster. + +This section includes the following topics: + +{{< section depth=2 >}} diff --git a/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/install-k6-operator.md b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/install-k6-operator.md new file mode 100644 index 000000000..315ec003c --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/install-k6-operator.md @@ -0,0 +1,114 @@ +--- +weight: 100 +title: Install k6 Operator +--- + +# Install k6 Operator + +This guide provides step-by-step instructions on how to install k6 Operator. + +## Before you begin + +To install k6 Operator, you'll need: + +- A Kubernetes cluster, along with access to it. +- [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl). + +## Deploy the operator + +There are three different options that you can use to deploy the k6 Operator. + +### Deploy with bundle + +The easiest way to install the operator is with bundle: + +```bash +curl https://raw.githubusercontent.com/grafana/k6-operator/main/bundle.yaml | kubectl apply -f - +``` + +Bundle includes default manifests for k6 Operator, including a `k6-operator-system` namespace and k6 Operator deployment with the latest tagged Docker image. Customizations can be made on top of this manifest as needed, for example, with `kustomize`. + +### Deploy with Helm + +Helm releases of k6 Operator are published together with other Grafana Helm charts. You can install it with the following commands: + +```bash +helm repo add grafana https://grafana.github.io/helm-charts +helm repo update +helm install k6-operator grafana/k6-operator +``` + +You can also pass additional configuration options with a `values.yaml` file: + +```bash +helm install k6-operator grafana/k6-operator -f values.yaml +``` + +Refer to the [k6 Operator samples folder](https://github.com/grafana/k6-operator/blob/main/charts/k6-operator/samples/customAnnotationsAndLabels.yaml) for an example file. + +You can find a complete list of Helm options in the [k6 Operator charts folder](https://github.com/grafana/k6-operator/blob/main/charts/k6-operator/README.md). + +### Deploy with Makefile + +In order to install the operator with a Makefile, you'll need: + +- [go](https://go.dev/doc/install) +- [kustomize](https://kubectl.docs.kubernetes.io/installation/kustomize/) + +A more manual, low-level way to install the k6 operator is by running the command below: + +```bash +make deploy +``` + +This method may be more useful for development of the k6 Operator, depending on specifics of the setup. + +## Install the CRD + +The k6 Operator includes custom resources called `TestRun`, `PrivateLoadZone`, and `K6`. They're automatically installed when you do a deployment or install a bundle, but you can also manually install them by running: + +```bash +make install +``` + +{{< admonition type="warning" >}} + +The `K6` CRD has been replaced by the `TestRun` CRD and will be deprecated in the future. We recommend using the `TestRun` CRD. + +{{< /admonition >}} + +## Watch namespace + +By default, the k6 Operator watches the `TestRun` and `PrivateLoadZone` custom resources in all namespaces. You can also configure the k6 Operator to watch a specific namespace by setting the `WATCH_NAMESPACE` environment variable for the operator's deployment: + +```yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: k6-operator-controller-manager + namespace: k6-operator-system +spec: + template: + spec: + containers: + - name: manager + image: ghcr.io/grafana/k6-operator:controller-v0.0.14 + env: + - name: WATCH_NAMESPACE + value: 'some-ns' +# ... +``` + +## Uninstall k6 Operator + +You can remove all of the resources created by the k6 Operator with `bundle`: + +```bash +curl https://raw.githubusercontent.com/grafana/k6-operator/main/bundle.yaml | kubectl delete -f - +``` + +Or with the `make` command: + +```bash +make delete +``` diff --git a/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/troubleshooting.md b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/troubleshooting.md new file mode 100644 index 000000000..2c298217b --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/troubleshooting.md @@ -0,0 +1,260 @@ +--- +weight: 400 +title: Troubleshooting +--- + +# Troubleshooting + +This topic includes instructions to help you troubleshoot common issues with the k6 Operator. + +## Common tricks + +### Test your script locally + +Always run your script locally before trying to run it with the k6 Operator: + +```bash +k6 run script.js +``` + +If you're using environment variables or CLI options, pass them in as well: + +```bash +MY_ENV_VAR=foo k6 run script.js --tag my_tag=bar +``` + +That ensures that the script has correct syntax and can be parsed with k6 in the first place. Additionally, running locally can help you check if the configured options are doing what you expect. If there are any errors or unexpected results in the output of `k6 run`, make sure to fix those prior to deploying the script elsewhere. + +### `TestRun` deployment + +#### The pods + +In case of one `TestRun` Custom Resource (CR) creation with `parallelism: n`, there are certain repeating patterns: + +1. There will be `n + 2` Jobs (with corresponding Pods) created: initializer, starter, and `n` runners. +1. If any of these Jobs didn't result in a Pod being deployed, there must be an issue with that Job. Some commands that can help here: + + ```bash + kubectl get jobs -A + kubectl describe job mytest-initializer + ``` + +1. If one of the Pods was deployed but finished with `Error`, you can check its logs with the following command: + + ```bash + kubectl logs mytest-initializer-xxxxx + ``` + +If the Pods seem to be working but not producing an expected result and there's not enough information in the logs, you can use the k6 [verbose option](https://grafana.com/docs/k6//using-k6/k6-options/#options) in the `TestRun` spec: + +```yaml +apiVersion: k6.io/v1alpha1 +kind: TestRun +metadata: + name: k6-sample +spec: + parallelism: 2 + script: + configMap: + name: 'test' + file: 'test.js' + arguments: --verbose +``` + +#### k6 Operator + +Another source of info is the k6 Operator itself. It's deployed as a Kubernetes `Deployment`, with `replicas: 1` by default, and its logs together with observations about the Pods from the previous section usually contain enough information to help you diagnose any issues. With the standard deployment, the logs of the k6 Operator can be checked with: + +```bash +kubectl -n k6-operator-system -c manager logs k6-operator-controller-manager-xxxxxxxx-xxxxx +``` + +#### Inspect `TestRun` resource + +After you deploy a `TestRun` CR, you can inspect it the same way as any other resource: + +```bash +kubectl describe testrun my-testrun +``` + +Firstly, check if the spec is as expected. Then, see the current status: + +```yaml +Status: + Conditions: + Last Transition Time: 2024-01-17T10:30:01Z + Message: + Reason: CloudTestRunFalse + Status: False + Type: CloudTestRun + Last Transition Time: 2024-01-17T10:29:58Z + Message: + Reason: TestRunPreparation + Status: Unknown + Type: TestRunRunning + Last Transition Time: 2024-01-17T10:29:58Z + Message: + Reason: CloudTestRunAbortedFalse + Status: False + Type: CloudTestRunAborted + Last Transition Time: 2024-01-17T10:29:58Z + Message: + Reason: CloudPLZTestRunFalse + Status: False + Type: CloudPLZTestRun + Stage: error +``` + +If `Stage` is equal to `error`, you can check the logs of k6 Operator. + +Conditions can be used as a source of info as well, but it's a more advanced troubleshooting option that should be used if the previous steps weren't enough to diagnose the issue. Note that conditions that start with the `Cloud` prefix only matter in the setting of k6 Cloud test runs, for example, for cloud output and PLZ test runs. + +### `PrivateLoadZone` deployment + +If the `PrivateLoadZone` CR was successfully created in Kubernetes, it should become visible in your account in Grafana Cloud k6 (GCk6) interface soon afterwards. If it doesn't appear in the UI, then there is likely a problem to troubleshoot. + +First, go over the [guide](https://grafana.com/docs/grafana-cloud/k6/author-run/private-load-zone-v2/) to double-check if all the steps have been done correctly and successfully. + +Unlike `TestRun` deployment, when a `PrivateLoadZone` is first created, there are no additional resources deployed. So, the only source for troubleshooting are the logs of k6 Operator. See the [previous subsection](#k6-operator) on how to access its logs. Any errors there might be a hint to diagnose the issue. Refer to [PrivateLoadZone: subscription error](#privateloadzone-subscription-error) for more details. + +### Running tests in `PrivateLoadZone` + +Each time a user runs a test in a PLZ, for example with `k6 cloud run script.js`, there is a corresponding `TestRun` being deployed by the k6 Operator. This `TestRun` will be deployed in the same namespace as its `PrivateLoadZone`. If the test is misbehaving, for example, it errors out, or doesn't produce the expected result, then you can check: + +1. If there are any messages in the GCk6 UI. +2. If there are any messages in the output of the `k6 cloud run` command. +3. The resources and their logs, the same way as with a [standalone `TestRun` deployment](#testrun-deployment) + +## Common scenarios + +### Issues with environment variables + +Refer to [Environment variables](https://github.com/grafana/k6-operator/blob/main/docs/env-vars.md) for details on how to pass environment variables to the k6 Operator. + +### Tags not working + +Tags are a rather common source of errors when using the k6 Operator. For example, the following tags would lead to parsing errors: + +```yaml + arguments: --tag product_id="Test A" + # or + arguments: --tag foo=\"bar\" +``` + +You can see those errors in the logs of either the initializer or the runner Pod, for example: + +```bash +time="2024-01-11T11:11:27Z" level=error msg="invalid argument \"product_id=\\\"Test\" for \"--tag\" flag: parse error on line 1, column 12: bare \" in non-quoted-field" +``` + +This is a common problem with escaping the characters. You can find an [issue](https://github.com/grafana/k6-operator/issues/211) in the k6 Operator repository that can be upvoted. + +### Initializer logs an error but it's not about tags + +This can happen because of lack of attention to the [preparation](#preparation) step. One command that you can use to help diagnose issues with your script is the following: + +```bash +k6 inspect --execution-requirements script.js +``` + +That command is a shortened version of what the initializer Pod is executing. If the command produces an error, there's a problem with the script itself and it should be solved outside of the k6 Operator. The error itself may contain a hint to what's wrong, such as a syntax error. + +If the standalone `k6 inspect --execution-requirements` executes successfully, then it's likely a problem with `TestRun` deployment specific to your Kubernetes setup. A couple of recommendations here are: + +- Review the output of the initializer Pod: is it logged by the k6 process or by something else? + - :information_source: k6 Operator expects the initializer logs to contain only the output of `k6 inspect`. If there are any other log lines present, then the k6 Operator will fail to parse it and the test won't start. Refer to this [issue](https://github.com/grafana/k6-operator/issues/193) for more details. +- Check events in the initializer Job and Pod as they may contain another hint about what's wrong. + +### Non-existent ServiceAccount + +A ServiceAccount can be defined as `serviceAccountName` in a PrivateLoadZone, and as `runner.serviceAccountName` in a TestRun CRD. If the specified ServiceAccount doesn't exist, k6 Operator will successfully create Jobs but corresponding Pods will fail to be deployed, and the k6 Operator will wait indefinitely for Pods to be `Ready`. This error can be best seen in the events of the Job: + +```bash +kubectl describe job plz-test-xxxxxx-initializer +... +Events: + Warning FailedCreate 57s (x4 over 2m7s) job-controller Error creating: pods "plz-test-xxxxxx-initializer-" is forbidden: error looking up service account plz-ns/plz-sa: serviceaccount "plz-sa" not found +``` + +k6 Operator doesn't try to analyze such scenarios on its own, but you can refer to the following [issue](https://github.com/grafana/k6-operator/issues/260) for improvements. + +#### How to fix + +To fix this issue, the incorrect `serviceAccountName` must be corrected, and the TestRun or PrivateLoadZone resource must be re-deployed. + +### Non-existent `nodeSelector` + +`nodeSelector` can be defined as `nodeSelector` in a PrivateLoadZone, and as `runner.nodeSelector` in the TestRun CRD. + +This case is very similar to the [ServiceAccount](#non-existent-serviceaccount): the Pod creation will fail, but the error is slightly different: + +```bash +kubectl describe pod plz-test-xxxxxx-initializer-xxxxx +... +Events: + Warning FailedScheduling 48s (x5 over 4m6s) default-scheduler 0/1 nodes are available: 1 node(s) didn't match Pod's node affinity/selector. +``` + +#### How to fix + +To fix this issue, the incorrect `nodeSelector` must be corrected and the TestRun or PrivateLoadZone resource must be re-deployed. + +### Insufficient resources + +A related problem can happen when the cluster does not have sufficient resources to deploy the runners. There's a higher probability of hitting this issue when setting small CPU and memory limits for runners or using options like `nodeSelector`, `runner.affinity` or `runner.topologySpreadConstraints`, and not having a set of nodes matching the spec. Alternatively, it can happen if there is a high number of runners required for the test (via `parallelism` in TestRun or during PLZ test run) and autoscaling of the cluster has limits on the maximum number of nodes, and can't provide the required resources on time or at all. + +This case is somewhat similar to the previous two: the k6 Operator will wait indefinitely and can be monitored with events in Jobs and Pods. If it's possible to fix the issue with insufficient resources on-the-fly, for example, by adding more nodes, k6 Operator will attempt to continue executing a test run. + +### OOM of a runner Pod + +If there's at least one runner Pod that OOM-ed, the whole test will be [stuck](https://github.com/grafana/k6-operator/issues/251) and will have to be deleted manually: + +```bash +kubectl -f my-test.yaml delete +# or +kubectl delete testrun my-test +``` + +In case of OOM, it makes sense to review the k6 script to understand what kind of resource usage this script requires. It may be that the k6 script can be improved to be more performant. Then, set the `spec.runner.resources` in the TestRun CRD, or `spec.resources` in the PrivateLoadZone CRD accordingly. + +### PrivateLoadZone: subscription error + +If there's an issue with your Grafana Cloud k6 subscription, there will be a 400 error in the logs with the message detailing the problem. For example: + +```bash +"Received error `(400) You have reached the maximum Number of private load zones your organization is allowed to have. Please contact support if you want to create more.`. Message from server ``" +``` + +To fix this issue, check your organization settings in Grafana Cloud k6 or contact Support. + +### PrivateLoadZone: Wrong token + +There can be two major problems with the authentication token: + +1. If the token wasn't created, or was created in a wrong location, the logs will show the following error: + + ```bash + Failed to load k6 Cloud token {"namespace": "plz-ns", "name": "my-plz", "reconcileID": "67c8bc73-f45b-4c7f-a9ad-4fd0ffb4d5f6", "name": "token-with-wrong-name", "secretNamespace": "plz-ns", "error": "Secret \"token-with-wrong-name\" not found"} + ``` + +2. If the token contains a corrupted value, or it's not an organizational token, the logs will show the following error: + + ```bash + "Received error `(403) Authentication token incorrect or expired`. Message from server ``" + ``` + +### PrivateLoadZone: Networking setup + +If you see any dial or connection errors in the logs of the k6 Operator, it makes sense to double-check the networking setup. For a PrivateLoadZone to operate, outbound traffic to Grafana Cloud k6 [must be allowed](https://grafana.com/docs/grafana-cloud/k6/author-run/private-load-zone-v2/#before-you-begin). To check the reachability of Grafana Cloud k6 endpoints: + +```bash +kubectl apply -f https://k8s.io/examples/admin/dns/dnsutils.yaml +kubectl exec -it dnsutils -- nslookup ingest.k6.io +kubectl exec -it dnsutils -- nslookup api.k6.io +``` + +For more resources on troubleshooting networking, refer to the [Kubernetes docs](https://kubernetes.io/docs/tasks/administer-cluster/dns-debugging-resolution/). + +### PrivateLoadZone: Insufficient resources + +The PrivateLoadZone insufficient resources problem is similar to [insufficient resources issue](#insufficient-resources). But, when running a PrivateLoadZone test, the k6 Operator will wait only for a timeout period. When the timeout period is up, the test will be aborted by Grafana Cloud k6 and marked as such, both in the PrivateLoadZone and in Grafana Cloud k6. In other words, there is a time limit to fix this issue without restarting the test run. diff --git a/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/upgrade-k6-operator.md b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/upgrade-k6-operator.md new file mode 100644 index 000000000..2a46ef392 --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/upgrade-k6-operator.md @@ -0,0 +1,10 @@ +--- +weight: 200 +title: Upgrade k6 Operator +_build: + list: false +--- + +# Upgrade k6 Operator + + diff --git a/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/_index.md b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/_index.md new file mode 100644 index 000000000..48ddb3b67 --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/_index.md @@ -0,0 +1,10 @@ +--- +weight: 300 +title: Usage +--- + +# Usage + +This section includes the following topics: + +{{< section depth=2 >}} diff --git a/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/common-options.md b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/common-options.md new file mode 100644 index 000000000..43d95d862 --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/common-options.md @@ -0,0 +1,57 @@ +--- +weight: 300 +title: Common options +--- + +# Common options + + + +The only options that are required as part of the `TestRun` CRD spec are `script` and `parallelism`. This guide covers some of the most common options. + +## Parallelism + +`parallelism` defines how many instances of k6 runners you want to create. Each instance is assigned an equal execution segment. For instance, if your test script is configured to run 200 VUs and `parallelism` is set to 4, the k6 Operator creates four k6 jobs, each running 50 VUs to achieve the desired VU count. + +## Separate + +`separate: true` indicates that the jobs created need to be distributed across different nodes. This is useful if you're running a test with a really high VU count and want to make sure the resources of each node won't become a bottleneck. + +## Service account + +If you want to use a custom Service Account you'll need to pass it into both the starter and the runner object: + +```yaml +apiVersion: k6.io/v1alpha1 +kind: TestRun +metadata: + name: +spec: + script: + configMap: + name: '' + runner: + serviceAccountName: + starter: + serviceAccountName: +``` + +## Runner + +Defines options for the test runner pods. The non-exhaustive list includes: + +- Passing resource limits and requests. +- Passing in labels and annotations. +- Passing in affinity and anti-affinity. +- Passing in a custom image. + +## Starter + +Defines options for the starter pod. The non-exhaustive list includes: + +- Passing in a custom image. +- Passing in labels and annotations. + +## Initializer + +By default, the initializer Job is defined with the same options as the runner Jobs, but its options can be overwritten by setting `.spec.initializer`. diff --git a/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/executing-k6-scripts-with-testrun-crd.md b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/executing-k6-scripts-with-testrun-crd.md new file mode 100644 index 000000000..64a7c626c --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/executing-k6-scripts-with-testrun-crd.md @@ -0,0 +1,219 @@ +--- +weight: 100 +title: Run k6 scripts with TestRun CRD +--- + +# Run k6 scripts with TestRun CRD + +This guide covers how you can configure your k6 scripts to run using the k6 Operator. + +## Defining test scripts + +There are several ways to configure scripts in the `TestRun` CRD. The operator uses `configMap`, `volumeClaim` and `localFile` to serve test scripts to the jobs. + +### ConfigMap + +The main way to configure a script is to create a `ConfigMap` with the script contents: + +```bash +kubectl create configmap my-test --from-file /path/to/my/test.js +``` + +Then specify it in `TestRun`: + +```bash + script: + configMap: + name: my-test + file: test.js +``` + +{{< admonition type="note" >}} + +A single `ConfigMap` has a character limit of 1048576 bytes. If you need to have a larger test file, you have to use a `volumeClaim` or a `localFile` instead. + +{{< /admonition >}} + +### VolumeClaim + +If you have a PVC with the name `stress-test-volumeClaim` containing your script and any other supporting files, you can pass it to the test like this: + +```yaml +spec: + script: + volumeClaim: + name: 'stress-test-volumeClaim' + # test.js should exist inside /test/ folder. + # All the js files and directories test.js is importing + # should be inside the same directory as well. + file: 'test.js' +``` + +The pods will expect to find the script files in the `/test/` folder. If `volumeClaim` fails, that's the first place to check. The latest initializer pod doesn't generate any logs and when it can't find the file, it exits with an error. Refer to [this GitHub issue](https://github.com/grafana/k6-operator/issues/143) for potential improvements. + +#### Sample directory structure + +``` +├── test +│ ├── requests +│ │ ├── stress-test.js +│ ├── test.js +``` + +In the preceding example, `test.js` imports a function from `stress-test.js` and these files would look like this: + +```js +// test.js +import stressTest from './requests/stress-test.js'; + +export const options = { + vus: 50, + duration: '10s', +}; + +export default function () { + stressTest(); +} +``` + +```js +// stress-test.js +import { sleep, check } from 'k6'; +import http from 'k6/http'; + +export default () => { + const res = http.get('https://test-api.k6.io'); + check(res, { + 'status is 200': () => res.status === 200, + }); + sleep(1); +}; +``` + +### LocalFile + +If the script is present in the filesystem of a custom runner image, it can be accessed with the `localFile` option: + +```yaml +spec: + parallelism: 4 + script: + localFile: /test/test.js + runner: + image: +``` + +{{< admonition type="note" >}} + +If there is any limitation on the usage of `volumeClaim` in your cluster, you can use the `localFile` option. We recommend using `volumeClaim` if possible. + +{{< /admonition >}} + +### Multi-file tests + +In case your k6 script is split between multiple JavaScript files, you can create a `ConfigMap` with several data entries like this: + +```bash +kubectl create configmap scenarios-test --from-file test.js --from-file utils.js +``` + +If there are too many files to specify manually, using `kubectl` with a folder might be an option as well: + +```bash +kubectl create configmap scenarios-test --from-file=./test +``` + +Alternatively, you can create an archive with k6: + +```bash +k6 archive test.js [args] +``` + +The `k6 archive` command creates an `archive.tar` in your current folder. You can then use that file in the `configmap`, similarly to a JavaScript script: + +```bash +kubectl create configmap scenarios-test --from-file=archive.tar +``` + +If you use an archive, you must edit your YAML file for the `TestRun` deployment so that the `file` option is set to the correct entrypoint for the `k6 run` command: + +```yaml +# ... +spec: + script: + configMap: + name: 'crocodile-stress-test' + file: 'archive.tar' # <-- change here +``` + +## Run tests + +Tests are executed by applying the custom resource `TestRun` to a cluster where the k6 Operator is running. Additional optional properties of the `TestRun` CRD allow you to control some key aspects of a distributed execution. For example: + +```yaml +# k6-resource.yml + +apiVersion: k6.io/v1alpha1 +kind: TestRun +metadata: + name: k6-sample +spec: + parallelism: 4 + script: + configMap: + name: k6-test + file: test.js + separate: false + runner: + image: + metadata: + labels: + cool-label: foo + annotations: + cool-annotation: bar + securityContext: + runAsUser: 1000 + runAsGroup: 1000 + runAsNonRoot: true + resources: + limits: + cpu: 200m + memory: 1000Mi + requests: + cpu: 100m + memory: 500Mi + starter: + image: + metadata: + labels: + cool-label: foo + annotations: + cool-annotation: bar + securityContext: + runAsUser: 2000 + runAsGroup: 2000 + runAsNonRoot: true +``` + +A `TestRun` CR is created with this command: + +```bash +kubectl apply -f /path/to/your/k6-resource.yml +``` + +## Clean up resources + +After completing a test run, you need to clean up the test jobs that were created: + +```bash +kubectl delete -f /path/to/your/k6-resource.yml +``` + +Alternatively, you can configure the automatic deletion of all resources with the `cleanup` option: + +```yaml +spec: + cleanup: 'post' +``` + +With the `cleanup` option set, k6 Operator removes the `TestRun` CRD and all created resources once the test run ends. diff --git a/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/extensions.md b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/extensions.md new file mode 100644 index 000000000..e65403275 --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/extensions.md @@ -0,0 +1,61 @@ +--- +weight: 200 +title: Use k6 Operator with k6 extensions +--- + +# Use k6 Operator with k6 extensions + +By default, the k6 Operator uses `ghcr.io/grafana/k6-operator:latest-runner` as the container image for the test jobs. + +If you want to use k6 [extensions](https://grafana.com/docs/k6//extensions/) built with [xk6](https://github.com/grafana/xk6), you'll need to create your own image and override the `image` property on the `TestRun` Kubernetes resource. + +For example, this is a `Dockerfile` that builds a k6 binary with the `xk6-output-influxdb` extension: + +```Dockerfile +# Build the k6 binary with the extension +FROM golang:1.20 as builder + +RUN go install go.k6.io/xk6/cmd/xk6@latest + +# For our example, we'll add support for output of test metrics to InfluxDB v2. +# Feel free to add other extensions using the '--with ...'. +RUN xk6 build \ + --with github.com/grafana/xk6-output-influxdb@latest \ + --output /k6 + +# Use the operator's base image and override the k6 binary +FROM grafana/k6:latest +COPY --from=builder /k6 /usr/bin/k6 +``` + +You can build the image based on this `Dockerfile` by executing: + +```bash +docker build -t k6-extended:local . +``` + +After the build completes, you can push the resulting `k6-extended:local` image to an image repository accessible to your Kubernetes cluster. + +You can then use that image as follows: + +```yaml +# k6-resource-with-extensions.yml + +apiVersion: k6.io/v1alpha1 +kind: TestRun +metadata: + name: k6-sample-with-extensions +spec: + parallelism: 4 + script: + configMap: + name: my-stress-test + file: test.js + runner: + image: k6-extended:local + env: + - name: K6_OUT + value: xk6-influxdb=http://influxdb.somewhere:8086/demo +``` + +Note that this examples overrides the default image with `k6-extended:latest`, and it includes environment variables that are required by the `xk6-output-influxdb` extension. diff --git a/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/k6-operator-to-gck6.md b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/k6-operator-to-gck6.md new file mode 100644 index 000000000..600decab7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/k6-operator-to-gck6.md @@ -0,0 +1,73 @@ +--- +weight: 250 +title: Use the k6 Operator with Grafana Cloud k6 +--- + +# Use the k6 Operator with Grafana Cloud k6 + +Grafana Cloud k6 is the Grafana Cloud offering of k6, which gives you access to all of k6 capabilities, while Grafana handles the infrastructure, storage, and metrics aggregation and insights from your tests. + +When using the k6 Operator, you can still leverage Grafana Cloud k6 to get access to the metric storage and analysis that the platform offers. + +There are two ways to use the k6 Operator with Grafana Cloud k6: Private Load Zones and Cloud output. + +## Before you begin + +To use the k6 Operator with Grafana Cloud k6, you’ll need: + +- A [Grafana Cloud account](https://grafana.com/auth/sign-up/create-user). + +## Private Load Zones + +Private Load Zones (PLZ) are load zones that you can host inside your network by using the k6 Operator. You can start a cloud test in a PLZ by referencing it by name from your script, and the test will run in the nodes of your Kubernetes cluster. + +Refer to [Set up private load zones](https://grafana.com/docs/grafana-cloud/testing/k6/author-run/private-load-zone-v2/) for more details. + +## Cloud output + +With k6, you can send the [output from a test run to Grafana Cloud k6](https://grafana.com/docs/k6//results-output/real-time/cloud) with the `k6 run --out cloud script.js` command. This feature is also available in the k6 Operator if you have a Grafana Cloud account. + +{{< admonition type="note" >}} + +The cloud output option only supports a `parallelism` value of 20 or less. + +{{< /admonition >}} + +To use this option in k6 Operator, set the argument in YAML: + +```yaml +# ... +script: + configMap: + name: '' +arguments: --out cloud +# ... +``` + +Then, if you installed operator with bundle or Helm, create a secret with the following command: + +```bash +kubectl -n k6-operator-system create secret generic my-cloud-token \ + --from-literal=token= && kubectl -n k6-operator-system label secret my-cloud-token "k6cloud=token" +``` + +Alternatively, if you installed operator with a Makefile, you can uncomment the cloud output section in `config/default/kustomization.yaml` and copy your token from Grafana Cloud k6 there: + +```yaml +# Uncomment this section if you need cloud output and copy-paste your token +secretGenerator: + - name: cloud-token + literals: + - token= + options: + annotations: + kubernetes.io/service-account.name: k6-operator-controller + labels: + k6cloud: token +``` + +After updating the file, run `make deploy`. + +After these steps, you can run k6 with the cloud output and default values of `projectID` and `name`. + +Refer to [Cloud options](https://grafana.com/docs/grafana-cloud/testing/k6/author-run/cloud-scripting-extras/cloud-options/#cloud-options) for details on how to change the `projectID` and `name` options. diff --git a/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/reference.md b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/reference.md new file mode 100644 index 000000000..f6f8b6d06 --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/reference.md @@ -0,0 +1,12 @@ +--- +weight: 500 +title: Reference +_build: + list: false +--- + +# Reference + + + +{{< section depth=2 >}} diff --git a/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/scheduling-tests.md b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/scheduling-tests.md new file mode 100644 index 000000000..02fc7503a --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/set-up-distributed-k6/usage/scheduling-tests.md @@ -0,0 +1,106 @@ +--- +weight: 400 +title: Schedule k6 tests +--- + +# Schedule k6 tests + +While the k6 Operator doesn't support scheduling k6 tests directly, you can schedule tests with the `CronJob` object from Kubernetes directly. The `CronJob` would run on a schedule and execute the creation and deletion of the `TestRun` object. + +Running these tests requires a little more setup than a standalone test run. + +## Create a `ConfigMap` with k6 scripts + +Refer to [Run k6 scripts with `TestRun` CRD](https://grafana.com/docs/k6//set-up/set-up-distributed-k6/usage/executing-k6-scripts-with-testrun-crd/) for details on how to create a `ConfigMap` with k6 scripts. + +## Create a ConfigMap of the YAML file for the `TestRun` job + + + +When using the `make deploy` installation method, add a `configMapGenerator` to the `kustomization.yaml`: + +```yaml +configMapGenerator: + - name: -config + files: + - .yaml +``` + +## Create a `ServiceAccount` for the `CronJob` + +For the `CronJob` to be able to create and delete `TestRun` objects, create a service account: + +```yaml +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: k6- +rules: + - apiGroups: + - k6.io + resources: + - testruns + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +--- +kind: RoleBinding +apiVersion: rbac.authorization.k8s.io/v1 +metadata: + name: k6- +roleRef: + kind: Role + name: k6- + apiGroup: rbac.authorization.k8s.io +subjects: + - kind: ServiceAccount + name: k6- + namespace: +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: k6- +``` + +## Create a `CronJob` + +This is an example of how to define a `CronJob` in a YAML file: + +```yaml +# snapshotter.yml +apiVersion: batch/v1beta1 +kind: CronJob +metadata: + name: -cron +spec: + schedule: '' + concurrencyPolicy: Forbid + jobTemplate: + spec: + template: + spec: + serviceAccount: k6 + containers: + - name: kubectl + image: bitnami/kubectl + volumeMounts: + - name: k6-yaml + mountPath: /tmp/ + command: + - /bin/bash + args: + - -c + - 'kubectl delete -f /tmp/.yaml; kubectl apply -f /tmp/.yaml' + restartPolicy: OnFailure + volumes: + - name: k6-yaml + configMap: + name: -config +``` diff --git a/docs/sources/k6/v0.55.x/set-up/usage-collection.md b/docs/sources/k6/v0.55.x/set-up/usage-collection.md new file mode 100644 index 000000000..f8ff7d2f3 --- /dev/null +++ b/docs/sources/k6/v0.55.x/set-up/usage-collection.md @@ -0,0 +1,35 @@ +--- +aliases: + - ../misc/usage-collection # docs/k6//misc/usage-collection +title: 'Usage collection' +description: 'By default, k6 sends a usage report each time it is run, so that we can track how often people use it. This report can be turned off by setting an environment variable or option.' +weight: 300 +--- + +# Usage collection + +By default, k6 sends an anonymous usage report each time it is run, so that we can track relevant information to be able to build the product making better data-driven decisions. Prioritizing the features that benefit the most and reducing the impact of changes. + +The report can be turned off by setting the [no usage report](https://grafana.com/docs/k6//using-k6/k6-options/reference/#no-usage-report) option setting the environment variable `K6_NO_USAGE_REPORT` or by adding the flag `--no-usage-report` when executing k6. + +The usage report does not contain any information about what you are testing. The contents are the following: + +- The k6 version (string, e.g. "0.17.2") +- Max VUs configured (number) +- Test duration (number) +- Total stages duration (number) +- VU iterations configured (number) +- The running program's operating system target (`darwin`, `freebsd`, `linux`...) +- The running program's architecture target (386, amd64, arm, s390x...) +- The list of JavaScript imported modules (`k6/http`, `k6/experimental/webcrypto`, ...) +- The list of used outputs (`json`, `influxdb`, ...) +- The test run ID if the test was executed in the cloud or was outputed to it +- The number of parsed files and how many were TypeScript files. +- The number of times `require` was called. +- Whether `global` was accessed. + +> Only k6 built-in JavaScript modules and outputs are considered. Private modules and custom extensions are excluded. + +This report is sent to an HTTPS server that collects statistics on k6 usage. + +k6 is an open-source project and for those interested, the actual code that generates and sends the usage report can be directly reviewed [here](https://github.com/grafana/k6/blob/master/cmd/report.go). diff --git a/docs/sources/k6/v0.55.x/shared/blocking-aws-blockquote.md b/docs/sources/k6/v0.55.x/shared/blocking-aws-blockquote.md new file mode 100644 index 000000000..fab1e9208 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/blocking-aws-blockquote.md @@ -0,0 +1,12 @@ +--- +title: jslib/aws module blocking admonition +--- + +{{< admonition type="caution" >}} + +In some cases, using this library's operations might impact performance and skew your test results. +
+
+To ensure accurate results, consider executing these operations in the `setup` and `teardown` [lifecycle functions]({{< relref "../using-k6/test-lifecycle" >}}). These functions run before and after the test run and have no impact on the test results. + +{{< /admonition >}} diff --git a/docs/sources/k6/v0.55.x/shared/browser-module-wip.md b/docs/sources/k6/v0.55.x/shared/browser-module-wip.md new file mode 100644 index 000000000..23d531c75 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/browser-module-wip.md @@ -0,0 +1,9 @@ +--- +title: k6/experimental/browser module admonition +--- + +{{< admonition type="caution" >}} + +This API is a work in progress. Some of the following functionalities might behave unexpectedly. + +{{< /admonition >}} diff --git a/docs/sources/k6/v0.55.x/shared/browser/env-var-options.md b/docs/sources/k6/v0.55.x/shared/browser/env-var-options.md new file mode 100644 index 000000000..b232b3555 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/browser/env-var-options.md @@ -0,0 +1,45 @@ +--- +title: browser/options +--- + +| Environment Variable | Description | +| ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| K6_BROWSER_ARGS | Extra command line arguments to include when launching browser process. See [this link](https://peter.sh/experiments/chromium-command-line-switches/) for a list of Chromium arguments. Note that arguments should not start with `--` (see the command example below). | +| K6_BROWSER_DEBUG | All CDP messages and internal fine grained logs will be logged if set to `true`. | +| K6_BROWSER_EXECUTABLE_PATH | Override search for browser executable in favor of specified absolute path. | +| K6_BROWSER_HEADLESS | Show browser GUI or not. `true` by default. | +| K6_BROWSER_IGNORE_DEFAULT_ARGS | Ignore any of the [default arguments](https://grafana.com/docs/k6//using-k6-browser/options/#default-arguments) included when launching a browser process. | +| K6_BROWSER_TIMEOUT | Default timeout for initializing the connection to the browser instance. `'30s'` if not set. | +| K6_BROWSER_TRACES_METADATA | Sets additional _key-value_ metadata that is included as attributes in every span generated from browser module traces. Example: `K6_BROWSER_TRACES_METADATA=attr1=val1,attr2=val2`. This only applies if traces generation is enabled, refer to [Traces output](https://grafana.com/docs/k6//using-k6/k6-options/reference#traces-output) for more details. | + +The following command passes the browser options as environment variables to launch a headful browser with custom arguments. + +{{< code >}} + +```bash +$ K6_BROWSER_HEADLESS=false K6_BROWSER_ARGS='show-property-changed-rects' k6 run script.js +``` + +```docker +# WARNING! +# The grafana/k6:master-with-browser image launches a Chrome browser by setting the +# 'no-sandbox' argument. Only use it with trustworthy websites. +# +# As an alternative, you can use a Docker SECCOMP profile instead, and overwrite the +# Chrome arguments to not use 'no-sandbox' such as: +# docker container run --rm -i -e K6_BROWSER_ARGS='' --security-opt seccomp=$(pwd)/chrome.json grafana/k6:master-with-browser run - set "K6_BROWSER_HEADLESS=false" && set "K6_BROWSER_ARGS='show-property-changed-rects' " && k6 run script.js +``` + +```windows-powershell +PS C:\k6> $env:K6_BROWSER_HEADLESS="false" ; $env:K6_BROWSER_ARGS='show-property-changed-rects' ; k6 run script.js +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/shared/browser/web-vital-core-metrics.md b/docs/sources/k6/v0.55.x/shared/browser/web-vital-core-metrics.md new file mode 100644 index 000000000..e58820667 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/browser/web-vital-core-metrics.md @@ -0,0 +1,13 @@ +--- +title: browser/webvital-core-metrics +--- + + + +| Metric name | Description | +| --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| browser_web_vital_cls | Measures the visual stability on a webpage by quantifying the amount of unexpected layout shift of visible page content. Refer to [Cumulative Layout Shift](https://web.dev/cls/) for more information. | +| browser_web_vital_fid | Measures the responsiveness of a web page by quantifying the delay between a user’s first interaction, such as clicking a button, and the browser’s response. Refer to [First Input Delay](https://web.dev/fid/) for more information. | +| browser_web_vital_lcp | Measures the time it takes for the largest content element on a page to become visible. Refer to [Largest Contentful Paint](https://web.dev/lcp/) for more information. | + + diff --git a/docs/sources/k6/v0.55.x/shared/browser/web-vital-other-metrics.md b/docs/sources/k6/v0.55.x/shared/browser/web-vital-other-metrics.md new file mode 100644 index 000000000..0cc9f76dc --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/browser/web-vital-other-metrics.md @@ -0,0 +1,13 @@ +--- +title: browser/webvital-other-metrics +--- + + + +| Metric name | Description | +| ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| browser_web_vital_fcp | Measures the time it takes for the browser to render the first DOM element on the page, whether that's a text, image or header. Refer to [First Contentful Paint](https://web.dev/fcp/) for more information. | +| browser_web_vital_inp | An experimental metric that measures a page's responsiveness. Refer to [Interaction to Next Paint](https://web.dev/inp/) for more information. | +| browser_web_vital_ttfb | Measures the time it takes between the browser request and the start of the response from a server. Refer to [Time to First Byte](https://web.dev/ttfb/) for more information. | + + diff --git a/docs/sources/k6/v0.55.x/shared/crypto-module.md b/docs/sources/k6/v0.55.x/shared/crypto-module.md new file mode 100644 index 000000000..40e85a13f --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/crypto-module.md @@ -0,0 +1,12 @@ +--- +title: k6/crypto module admonition +--- + +{{< admonition type="note" >}} + +A module with a better and standard API exists. +
+
+The new [k6/experimental/webcrypto API]({{< relref "../javascript-api/k6-experimental/webcrypto" >}}) partially implements the [WebCryptoAPI](https://www.w3.org/TR/WebCryptoAPI/), supporting more features than [k6/crypto]({{< relref "../javascript-api/k6-crypto" >}}). + +{{< /admonition >}} diff --git a/docs/sources/k6/v0.55.x/shared/experimental-module.md b/docs/sources/k6/v0.55.x/shared/experimental-module.md new file mode 100644 index 000000000..cc4337281 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/experimental-module.md @@ -0,0 +1,15 @@ +--- +title: Experimental module admonition +--- + +{{< admonition type="caution" >}} + +This is an experimental module. +
+
+While we intend to keep experimental modules as stable as possible, we may need to introduce breaking changes. This could happen at future k6 releases until the module becomes fully stable and graduates as a k6 core module. For more information, refer to the [extension graduation process]({{< relref "../extensions/explanations/extension-graduation" >}}). +
+
+Experimental modules maintain a high level of stability and follow regular maintenance and security measures. Feel free to [open an issue](https://github.com/grafana/k6/issues) if you have any feedback or suggestions. + +{{< /admonition >}} diff --git a/docs/sources/k6/v0.55.x/shared/experimental-timers-module.md b/docs/sources/k6/v0.55.x/shared/experimental-timers-module.md new file mode 100644 index 000000000..30e2ebf0c --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/experimental-timers-module.md @@ -0,0 +1,11 @@ +--- +title: Experimental timers module admonition +--- + +{{< admonition type="caution" >}} + +The experimental module `k6/experimental/timers` has graduated, and its functionality is now available globally and through the [`k6/timers` module](https://grafana.com/docs/k6//javascript-api/k6-timers/). The `k6/experimental/timers` is deprecated and will be removed. + +To migrate your scripts, remove the `k6/experimental/timers` imports. + +{{< /admonition >}} diff --git a/docs/sources/k6/v0.55.x/shared/index.md b/docs/sources/k6/v0.55.x/shared/index.md new file mode 100644 index 000000000..ca03031f1 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/index.md @@ -0,0 +1,3 @@ +--- +headless: true +--- diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/import.meta.md b/docs/sources/k6/v0.55.x/shared/javascript-api/import.meta.md new file mode 100644 index 000000000..e47e624d2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/import.meta.md @@ -0,0 +1,9 @@ +--- +title: 'javascript-api/import.meta' +--- + +`import.meta` is only available in ECMAScript modules, but not CommonJS ones. + +| Function | Description | +| -------------------------------------------------------------------------------------------------- | --------------------------------------------------------- | +| [import.meta.resolve](https://grafana.com/docs/k6//javascript-api/import.meta/resolve) | resolve path to URL the same way that an ESM import would | diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/init-context.md b/docs/sources/k6/v0.55.x/shared/javascript-api/init-context.md new file mode 100644 index 000000000..54a71771e --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/init-context.md @@ -0,0 +1,11 @@ +--- +title: javascript-api/init-context +--- + +Before the k6 starts the test logic, code in the _init context_ prepares the script. +A few functions are available only in init context. +For details about the runtime, refer to the [Test lifecycle](https://grafana.com/docs/k6//using-k6/test-lifecycle). + +| Function | Description | +| ----------------------------------------------------------------------------------------------------- | ---------------------------------------------------- | +| [open( filePath, [mode] )](https://grafana.com/docs/k6//javascript-api/init-context/open) | Opens a file and reads all the contents into memory. | diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/jslib.md b/docs/sources/k6/v0.55.x/shared/javascript-api/jslib.md new file mode 100644 index 000000000..52c13015b --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/jslib.md @@ -0,0 +1,14 @@ +--- +title: javascript-api/jslib +--- + +[jslib](https://grafana.com/docs/k6//javascript-api/jslib) is a collection of JavaScript libraries maintained by the k6 team that can be used in k6 scripts. + +| Library | Description | +| ------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------- | +| [aws](https://grafana.com/docs/k6//javascript-api/jslib/aws) | Library allowing to interact with Amazon AWS services | +| [httpx](https://grafana.com/docs/k6//javascript-api/jslib/httpx) | Wrapper around [k6/http](https://grafana.com/docs/k6//javascript-api/#k6http) to simplify session handling | +| [k6chaijs](https://grafana.com/docs/k6//javascript-api/jslib/k6chaijs) | BDD assertion style | +| [http-instrumentation-pyroscope](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-pyroscope) | Library to instrument k6/http to send baggage headers for pyroscope to read back | +| [http-instrumentation-tempo](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-tempo) | Library to instrument k6/http to send tracing data | +| [utils](https://grafana.com/docs/k6//javascript-api/jslib/utils) | Small utility functions useful in every day load testing | diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/k6-browser-api.md b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-browser-api.md new file mode 100644 index 000000000..bad855771 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-browser-api.md @@ -0,0 +1,18 @@ +--- +title: javascript-api/k6-browser-api +--- + +| k6 Class | Description | +| ---------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext) {{< docs/bwipt >}} | Enables independent browser sessions with separate [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page)s, cache, and cookies. | +| [ElementHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/elementhandle) {{< docs/bwipt >}} | Represents an in-page DOM element. | +| [Frame](https://grafana.com/docs/k6//javascript-api/k6-browser/frame) {{< docs/bwipt >}} | Access and interact with the [`Page`](https://grafana.com/docs/k6//javascript-api/k6-browser/page).'s `Frame`s. | +| [JSHandle](https://grafana.com/docs/k6//javascript-api/k6-browser/jshandle) | Represents an in-page JavaScript object. | +| [Keyboard](https://grafana.com/docs/k6//javascript-api/k6-browser/keyboard) | Used to simulate the keyboard interactions with the associated [`Page`](https://grafana.com/docs/k6//javascript-api/k6-browser/page). | +| [Locator](https://grafana.com/docs/k6//javascript-api/k6-browser/locator) | The Locator API makes it easier to work with dynamically changing elements. | +| [Mouse](https://grafana.com/docs/k6//javascript-api/k6-browser/mouse) | Used to simulate the mouse interactions with the associated [`Page`](https://grafana.com/docs/k6//javascript-api/k6-browser/page). | +| [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page) {{< docs/bwipt >}} | Provides methods to interact with a single tab in a browser. | +| [Request](https://grafana.com/docs/k6//javascript-api/k6-browser/request) {{< docs/bwipt >}} | Used to keep track of the request the [`Page`](https://grafana.com/docs/k6//javascript-api/k6-browser/page) makes. | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-browser/response) {{< docs/bwipt >}} | Represents the response received by the [`Page`](https://grafana.com/docs/k6//javascript-api/k6-browser/page). | +| [Touchscreen](https://grafana.com/docs/k6//javascript-api/k6-browser/touchscreen) | Used to simulate touch interactions with the associated [`Page`](https://grafana.com/docs/k6//javascript-api/k6-browser/page). | +| [Worker](https://grafana.com/docs/k6//javascript-api/k6-browser/worker) | Represents a [WebWorker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API). | diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/k6-browser-module.md b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-browser-module.md new file mode 100644 index 000000000..5d3737a95 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-browser-module.md @@ -0,0 +1,12 @@ +--- +title: javascript-api/k6-browser-module +--- + +| Method | Description | +| ------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [browser.closeContext()](https://grafana.com/docs/k6//javascript-api/k6-browser/closecontext) | Closes the current [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext). | +| [browser.context()](https://grafana.com/docs/k6//javascript-api/k6-browser/context) | Returns the current [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext). | +| [browser.isConnected](https://grafana.com/docs/k6//javascript-api/k6-browser/isconnected) {{< docs/bwipt id="453" >}} | Indicates whether the [CDP](https://chromedevtools.github.io/devtools-protocol/) connection to the browser process is active or not. | +| [browser.newContext([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/newcontext/) {{< docs/bwipt id="455" >}} | Creates and returns a new [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext). | +| [browser.newPage([options])](https://grafana.com/docs/k6//javascript-api/k6-browser/newpage) {{< docs/bwipt id="455" >}} | Creates a new [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page) in a new [BrowserContext](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext) and returns the page. Pages that have been opened ought to be closed using [`Page.close`](https://grafana.com/docs/k6//javascript-api/k6-browser/page/close). Pages left open could potentially distort the results of Web Vital metrics. | +| [browser.version()](https://grafana.com/docs/k6//javascript-api/k6-browser/version) | Returns the browser application's version. | diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/k6-crypto.md b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-crypto.md new file mode 100644 index 000000000..39794a926 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-crypto.md @@ -0,0 +1,25 @@ +--- +title: javascript-api/k6-crypto +--- + +The [`k6/crypto` module](https://grafana.com/docs/k6//javascript-api/k6-crypto) provides common hashing functionality available in the GoLang [crypto](https://golang.org/pkg/crypto/) package. + +| Function | Description | +| ----------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- | +| [createHash(algorithm)](https://grafana.com/docs/k6//javascript-api/k6-crypto/createhash) | Create a Hasher object, allowing the user to add data to hash multiple times, and extract hash digests along the way. | +| [createHMAC(algorithm, secret)](https://grafana.com/docs/k6//javascript-api/k6-crypto/createhmac) | Create an HMAC hashing object, allowing the user to add data to hash multiple times, and extract hash digests along the way. | +| [hmac(algorithm, secret, data, outputEncoding)](https://grafana.com/docs/k6//javascript-api/k6-crypto/hmac) | Use HMAC to sign an input string. | +| [md4(input, outputEncoding)](https://grafana.com/docs/k6//javascript-api/k6-crypto/md4) | Use MD4 to hash an input string. | +| [md5(input, outputEncoding)](https://grafana.com/docs/k6//javascript-api/k6-crypto/md5) | Use MD5 to hash an input string. | +| [randomBytes(int)](https://grafana.com/docs/k6//javascript-api/k6-crypto/randombytes) | Return an array with a number of cryptographically random bytes. | +| [ripemd160(input, outputEncoding)](https://grafana.com/docs/k6//javascript-api/k6-crypto/ripemd160) | Use RIPEMD-160 to hash an input string. | +| [sha1(input, outputEncoding)](https://grafana.com/docs/k6//javascript-api/k6-crypto/sha1) | Use SHA-1 to hash an input string. | +| [sha256(input, outputEncoding)](https://grafana.com/docs/k6//javascript-api/k6-crypto/sha256) | Use SHA-256 to hash an input string. | +| [sha384(input, outputEncoding)](https://grafana.com/docs/k6//javascript-api/k6-crypto/sha384) | Use SHA-384 to hash an input string. | +| [sha512(input, outputEncoding)](https://grafana.com/docs/k6//javascript-api/k6-crypto/sha512) | Use SHA-512 to hash an input string. | +| [sha512_224(input, outputEncoding)](https://grafana.com/docs/k6//javascript-api/k6-crypto/sha512_224) | Use SHA-512/224 to hash an input string. | +| [sha512_256(input, outputEncoding)](https://grafana.com/docs/k6//javascript-api/k6-crypto/sha512_256) | Use SHA-512/256 to hash an input string. | + +| Class | Description | +| ---------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [Hasher](https://grafana.com/docs/k6//javascript-api/k6-crypto/hasher) | Object returned by [crypto.createHash()](https://grafana.com/docs/k6//javascript-api/k6-crypto/createhash). It allows adding more data to be hashed and to extract digests along the way. | diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/k6-data.md b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-data.md new file mode 100644 index 000000000..9e2bfac29 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-data.md @@ -0,0 +1,9 @@ +--- +title: javascript-api/k6-data +--- + +The [`k6/data` module](https://grafana.com/docs/k6//javascript-api/k6-data) provides helpers to work with data. + +| Class/Method | Description | +| ------------------------------------------------------------------------------------------ | ------------------------------------------------------------- | +| [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) | read-only array like structure that shares memory between VUs | diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/k6-encoding.md b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-encoding.md new file mode 100644 index 000000000..85e1fcad4 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-encoding.md @@ -0,0 +1,11 @@ +--- +title: javascript-api/k6-encoding +--- + +The [`k6/encoding` module](https://grafana.com/docs/k6//javascript-api/k6-encoding) provides [base64](https://en.wikipedia.org/wiki/Base64) +encoding/decoding as defined by [RFC4648](https://tools.ietf.org/html/rfc4648). + +| Function | Description | +| ------------------------------------------------------------------------------------------------------------------------ | ----------------------- | +| [b64decode(input, [encoding], [format])](https://grafana.com/docs/k6//javascript-api/k6-encoding/b64decode/) | Base64 decode a string. | +| [b64encode(input, [encoding])](https://grafana.com/docs/k6//javascript-api/k6-encoding/b64encode/) | Base64 encode a string. | diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/k6-errors-list.md b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-errors-list.md new file mode 100644 index 000000000..72d35070a --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-errors-list.md @@ -0,0 +1,33 @@ +--- +title: javascript-api/k6-errors-list +--- + +- 1000: A generic error that isn't any of the ones listed below. +- 1010: A non-TCP network error - this is a place holder there is no error currently known to trigger it. +- 1020: An invalid URL was specified. +- 1050: The HTTP request has timed out. +- 1100: A generic DNS error that isn't any of the ones listed below. +- 1101: No IP for the provided host was found. +- 1110: Blacklisted IP was resolved or a connection to such was tried to be established. +- 1111: Blacklisted hostname using The [Block Hostnames](https://grafana.com/docs/k6//using-k6/k6-options/reference#block-hostnames) option. +- 1200: A generic TCP error that isn't any of the ones listed below. +- 1201: A "broken pipe" on write - the other side has likely closed the connection. +- 1202: An unknown TCP error - We got an error that we don't recognize but it is from the operating system and has `errno` set on it. The message in `error` includes the operation(write,read) and the errno, the OS, and the original message of the error. +- 1210: General TCP dial error. +- 1211: Dial timeout error - the timeout for the dial was reached. +- 1212: Dial connection refused - the connection was refused by the other party on dial. +- 1213: Dial unknown error. +- 1220: Reset by peer - the connection was reset by the other party, most likely a server. +- 1300: General TLS error +- 1310: Unknown authority - the certificate issuer is unknown. +- 1311: The certificate doesn't match the hostname. +- 1400 to 1499: error codes that correspond to the [HTTP 4xx status codes for client errors](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_errors) +- 1500 to 1599: error codes that correspond to the [HTTP 5xx status codes for server errors](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_Server_errors) +- 1600: A generic HTTP/2 error that isn't any of the ones listed below. +- 1610: A general HTTP/2 GoAway error. +- 1611 to 1629: HTTP/2 GoAway errors with the value of the specific [HTTP/2 error code](https://tools.ietf.org/html/rfc7540#section-7) added to 1611. +- 1630: A general HTTP/2 stream error. +- 1631 to 1649: HTTP/2 stream errors with the value of the specific [HTTP/2 error code](https://tools.ietf.org/html/rfc7540#section-7) added to 1631. +- 1650: A general HTTP/2 connection error. +- 1651 to 1669: HTTP/2 connection errors with the value of the specific [HTTP/2 error code](https://tools.ietf.org/html/rfc7540#section-7) added to 1651. +- 1701: Decompression error. diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/k6-execution-intro.md b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-execution-intro.md new file mode 100644 index 000000000..60e5749eb --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-execution-intro.md @@ -0,0 +1,12 @@ +--- +title: javascript-api/k6-execution-into +--- + +The [`k6/execution` module](https://grafana.com/docs/k6//javascript-api/k6-execution) provides the capability to get information about the current test execution state inside the test script. You can read in your script the execution state during the test execution and change your script logic based on the current state. + +`k6/execution` provides the test execution information with the following properties: + +- [instance](https://grafana.com/docs/k6//javascript-api/k6-execution#instance) +- [scenario](https://grafana.com/docs/k6//javascript-api/k6-execution#scenario) +- [test](https://grafana.com/docs/k6//javascript-api/k6-execution#test) +- [vu](https://grafana.com/docs/k6//javascript-api/k6-execution#vu) diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/k6-experimental.md b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-experimental.md new file mode 100644 index 000000000..cdd6ee25d --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-experimental.md @@ -0,0 +1,12 @@ +--- +title: javascript-api/k6-experimental +--- + +| Modules | Description | +| ------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------- | +| [csv](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs) | Provides support for efficient and convinient of parsing CSV files. | +| [fs](https://grafana.com/docs/k6//javascript-api/k6-experimental/fs) | Provides a memory-efficient way to handle file interactions within your test scripts. | +| [redis](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis) | Functionality to interact with [Redis](https://redis.io/). | +| [streams](https://grafana.com/docs/k6//javascript-api/k6-experimental/streams) | Provides an implementation of the Streams API specification, offering support for defining and consuming readable streams. | +| [webcrypto](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto) | Implements the [WebCrypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API). | +| [websockets](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets) | Implements the browser's [WebSocket API](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket). | diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/k6-html.md b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-html.md new file mode 100644 index 000000000..4ac449f06 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-html.md @@ -0,0 +1,14 @@ +--- +title: javascript-api/k6-html +--- + +The [`k6/html` module](https://grafana.com/docs/k6//javascript-api/k6-html) contains functionality for HTML parsing. + +| Function | Description | +| ------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | +| [parseHTML(src)](https://grafana.com/docs/k6//javascript-api/k6-html/parsehtml) | Parse an HTML string and populate a [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) object. | + +| Class | Description | +| -------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | +| [Element](https://grafana.com/docs/k6//javascript-api/k6-html/element) | An HTML DOM element as returned by the [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) API. | +| [Selection](https://grafana.com/docs/k6//javascript-api/k6-html/selection) | A jQuery-like API for accessing HTML DOM elements. | diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/k6-http.md b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-http.md new file mode 100644 index 000000000..0dd17aefd --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-http.md @@ -0,0 +1,30 @@ +--- +title: javascript-api/k6-http +--- + +The [`k6/http` module](https://grafana.com/docs/k6//javascript-api/k6-http) contains functionality for performing HTTP transactions. + +| Function | Description | +| ------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------- | +| [batch( requests )](https://grafana.com/docs/k6//javascript-api/k6-http/batch) | Issue multiple HTTP requests in parallel (like e.g. browsers tend to do). | +| [cookieJar()](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar-method) | Get active HTTP Cookie jar. | +| [del( url, [body], [params] )](https://grafana.com/docs/k6//javascript-api/k6-http/del) | Issue an HTTP DELETE request. | +| [file( data, [filename], [contentType] )](https://grafana.com/docs/k6//javascript-api/k6-http/file) | Create a file object that is used for building multi-part requests. | +| [get( url, [params] )](https://grafana.com/docs/k6//javascript-api/k6-http/get) | Issue an HTTP GET request. | +| [head( url, [params] )](https://grafana.com/docs/k6//javascript-api/k6-http/head) | Issue an HTTP HEAD request. | +| [options( url, [body], [params] )](https://grafana.com/docs/k6//javascript-api/k6-http/options) | Issue an HTTP OPTIONS request. | +| [patch( url, [body], [params] )](https://grafana.com/docs/k6//javascript-api/k6-http/patch) | Issue an HTTP PATCH request. | +| [post( url, [body], [params] )](https://grafana.com/docs/k6//javascript-api/k6-http/post) | Issue an HTTP POST request. | +| [put( url, [body], [params] )](https://grafana.com/docs/k6//javascript-api/k6-http/put) | Issue an HTTP PUT request. | +| [request( method, url, [body], [params] )](https://grafana.com/docs/k6//javascript-api/k6-http/request) | Issue any type of HTTP request. | +| [asyncRequest( method, url, [body], [params] )](https://grafana.com/docs/k6//javascript-api/k6-http/asyncrequest) | Issue any type of HTTP request asynchronously. | +| [setResponseCallback(expectedStatuses)](https://grafana.com/docs/k6//javascript-api/k6-http/set-response-callback) | Sets a response callback to mark responses as expected. | +| [url\`url\`](https://grafana.com/docs/k6//javascript-api/k6-http/url) | Creates a URL with a name tag. Read more on [URL Grouping](https://grafana.com/docs/k6//using-k6/http-requests#url-grouping). | +| [expectedStatuses( statusCodes )](https://grafana.com/docs/k6//javascript-api/k6-http/expected-statuses) | Create a callback for setResponseCallback that checks status codes. | + +| Class | Description | +| -------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | +| [CookieJar](https://grafana.com/docs/k6//javascript-api/k6-http/cookiejar) | Used for storing cookies, set by the server and/or added by the client. | +| [FileData](https://grafana.com/docs/k6//javascript-api/k6-http/filedata) | Used for wrapping data representing a file when doing multipart requests (file uploads). | +| [Params](https://grafana.com/docs/k6//javascript-api/k6-http/params) | Used for setting various HTTP request-specific parameters such as headers, cookies, etc. | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-http/response) | Returned by the http.\* methods that generate HTTP requests. | diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/k6-metrics.md b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-metrics.md new file mode 100644 index 000000000..d419f7655 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-metrics.md @@ -0,0 +1,12 @@ +--- +title: javascript-api/k6-metrics +--- + +The [`k6/metrics` module](https://grafana.com/docs/k6//javascript-api/k6-metrics) provides functionality to [create custom metrics](https://grafana.com/docs/k6//using-k6/metrics/create-custom-metrics) of various types. + +| Metric type | Description | +| ------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- | +| [Counter](https://grafana.com/docs/k6//javascript-api/k6-metrics/counter) | A metric that cumulatively sums added values. | +| [Gauge](https://grafana.com/docs/k6//javascript-api/k6-metrics/gauge) | A metric that stores the min, max and last values added to it. | +| [Rate](https://grafana.com/docs/k6//javascript-api/k6-metrics/rate) | A metric that tracks the percentage of added values that are non-zero. | +| [Trend](https://grafana.com/docs/k6//javascript-api/k6-metrics/trend) | A metric that calculates statistics on the added values (min, max, average, and percentiles). | diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/k6-net-grpc.md b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-net-grpc.md new file mode 100644 index 000000000..15d3b782a --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-net-grpc.md @@ -0,0 +1,23 @@ +--- +title: javascript-api/k6-net-grpc +--- + +The [`k6/net/grpc` module](https://grafana.com/docs/k6//javascript-api/k6-net-grpc) provides a [gRPC](https://grpc.io/) client for Remote Procedure Calls (RPC) over HTTP/2. + +| Class/Method | Description | +| -------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [Client](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client) | gRPC client used for making RPC calls to a gRPC Server. | +| [Client.load(importPaths, ...protoFiles)](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client/client-load) | Loads and parses the given protocol buffer definitions to be made available for RPC requests. | +| [Client.connect(address [,params])](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client/client-connect) | Connects to a given gRPC service. | +| [Client.invoke(url, request [,params])](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client/client-invoke) | Makes an unary RPC for the given service/method and returns a [Response](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/response). | +| [Client.asyncInvoke(url, request [,params])](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client/client-async-invoke) | Asynchronously makes an unary RPC for the given service/method and returns a Promise with [Response](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/response). | +| [Client.close()](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/client/client-close) | Close the connection to the gRPC service. | +| [Params](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/params) | RPC Request specific options. | +| [Response](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/response) | Returned by RPC requests. | +| [Constants](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/constants) | Define constants to distinguish between [gRPC Response](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/response) statuses. | +| [Stream(client, url, [,params])](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream) | Creates a new gRPC stream. | +| [Stream.on(event, handler)](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/stream-on) | Adds a new listener to one of the possible stream events. | +| [Stream.write(message)](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/stream-write) | Writes a message to the stream. | +| [Stream.end()](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/stream-end) | Signals to the server that the client has finished sending. | +| [EventHandler](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/event-handler) | The function to call for various events on the gRPC stream. | +| [Metadata](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/message-metadata) | The metadata of a gRPC stream’s message. | diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/k6-timers.md b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-timers.md new file mode 100644 index 000000000..b17e4669f --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-timers.md @@ -0,0 +1,18 @@ +--- +title: javascript-api/k6-timers +--- + +The [`k6/timers` module](https://grafana.com/docs/k6//javascript-api/k6-timers) implements timers to work with k6's event loop. They mimic the functionality found in browsers and other JavaScript runtimes. + +| Function | Description | +| :---------------------------------------------------------------------------- | :--------------------------------------------------- | +| [setTimeout](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) | Sets a function to be run after a given timeout. | +| [clearTimeout](https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout) | Clears a previously set timeout with `setTimeout`. | +| [setInterval](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) | Sets a function to be run on a given interval. | +| [clearInterval](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) | Clears a previously set interval with `setInterval`. | + +{{< admonition type="note" >}} + +The timer methods are available globally, so you can use them in your script without including an import statement. + +{{< /admonition >}} diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/k6-ws.md b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-ws.md new file mode 100644 index 000000000..70d8c4a0e --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/k6-ws.md @@ -0,0 +1,21 @@ +--- +title: javascript-api/k6-ws +--- + +The [`k6/ws` module](https://grafana.com/docs/k6//javascript-api/k6-ws) provides a [WebSocket](https://en.wikipedia.org/wiki/WebSocket) client implementing the [WebSocket protocol](http://www.rfc-editor.org/rfc/rfc6455.txt). + +| Function | Description | +| --------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [connect( url, params, callback )](https://grafana.com/docs/k6//javascript-api/k6-ws/connect) | Create a WebSocket connection, and provides a [Socket](https://grafana.com/docs/k6//javascript-api/k6-ws/socket) client to interact with the service. The method blocks the test finalization until the connection is closed. | + +| Class/Method | Description | +| --------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| [Params](https://grafana.com/docs/k6//javascript-api/k6-ws/params) | Used for setting various WebSocket connection parameters such as headers, cookie jar, compression, etc. | +| [Socket](https://grafana.com/docs/k6//javascript-api/k6-ws/socket) | WebSocket client used to interact with a WS connection. | +| [Socket.close()](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-close) | Close the WebSocket connection. | +| [Socket.on(event, callback)](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-on) | Set up an event listener on the connection for any of the following events:
- open
- binaryMessage
- message
- ping
- pong
- close
- error. | +| [Socket.ping()](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-ping) | Send a ping. | +| [Socket.send(data)](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-send) | Send string data. | +| [Socket.sendBinary(data)](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-sendbinary) | Send binary data. | +| [Socket.setInterval(callback, interval)](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-setinterval) | Call a function repeatedly at certain intervals, while the connection is open. | +| [Socket.setTimeout(callback, period)](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-settimeout) | Call a function with a delay, if the connection is open. | diff --git a/docs/sources/k6/v0.55.x/shared/javascript-api/k6.md b/docs/sources/k6/v0.55.x/shared/javascript-api/k6.md new file mode 100644 index 000000000..77769db0c --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/javascript-api/k6.md @@ -0,0 +1,13 @@ +--- +title: javascript-api/k6 +--- + +The [`k6` module](https://grafana.com/docs/k6//javascript-api/k6) contains k6-specific functionality. + +| Function | Description | +| -------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- | +| [check(val, sets, [tags])](https://grafana.com/docs/k6//javascript-api/k6/check) | Runs one or more checks on a value and generates a pass/fail result but does not throw errors or otherwise interrupt execution upon failure. | +| [fail([err])](https://grafana.com/docs/k6//javascript-api/k6/fail) | Throws an error, failing and aborting the current VU script iteration immediately. | +| [group(name, fn)](https://grafana.com/docs/k6//javascript-api/k6/group) | Runs code inside a group. Used to organize results in a test. | +| [randomSeed(int)](https://grafana.com/docs/k6//javascript-api/k6/random-seed) | Set seed to get a reproducible pseudo-random number using `Math.random`. | +| [sleep(t)](https://grafana.com/docs/k6//javascript-api/k6/sleep) | Suspends VU execution for the specified duration. | diff --git a/docs/sources/k6/v0.55.x/shared/statsd-output.md b/docs/sources/k6/v0.55.x/shared/statsd-output.md new file mode 100644 index 000000000..5503cbd8b --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/statsd-output.md @@ -0,0 +1,11 @@ +--- +title: StatsD module admonition +--- + +{{< admonition type="note" >}} + +The built-in StatsD output has been deprecated on k6 v0.47.0 and removed in v0.55.0. You can continue to stream metrics to StatsD by using the [xk6-output-statsd extension](https://github.com/LeonAdato/xk6-output-statsd), or using the [OpenTelemetry output](https://grafana.com/docs/k6//results-output/real-time/opentelemetry/) depending on your use case. + +For more information on the reason behind this change, you can follow [this issue](https://github.com/grafana/k6/issues/2982) in the k6 repository. + +{{< /admonition >}} diff --git a/docs/sources/k6/v0.55.x/shared/webcrypto/supported-encrypt-decrypt.md b/docs/sources/k6/v0.55.x/shared/webcrypto/supported-encrypt-decrypt.md new file mode 100644 index 000000000..64049cda7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/webcrypto/supported-encrypt-decrypt.md @@ -0,0 +1,7 @@ +--- +title: webcrypto/supported encrypt/decrypt +--- + +| AES-CBC | AES-CTR | AES-GCM | RSA-OAEP | +| :---------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------- | :------- | +| ✅ [AesCbcParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/aescbcparams) | ✅ [AesCtrParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/aesctrparams) | ✅ [AesGcmParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/aesgcmparams) | ✅ [RsaOaepParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/rsaoaepparams) | diff --git a/docs/sources/k6/v0.55.x/shared/webcrypto/supported-key-methods-formats.md b/docs/sources/k6/v0.55.x/shared/webcrypto/supported-key-methods-formats.md new file mode 100644 index 000000000..d2545a747 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/webcrypto/supported-key-methods-formats.md @@ -0,0 +1,7 @@ +--- +title: webcrypto/supported key methods formats +--- + +- `ECDH` and `ECDSA` algorithms have support for `pkcs8`, `spki`, `raw` and `jwk` formats. +- `RSA-OAEP`, `RSASSA-PKCS1-v1_5` and `RSA-PSS` algorithms have support for `pkcs8`, `spki` and `jwk` formats. +- `AES-*` and `HMAC` algorithms have currently support for `raw` and `jwk` formats. diff --git a/docs/sources/k6/v0.55.x/shared/webcrypto/supported-key-methods.md b/docs/sources/k6/v0.55.x/shared/webcrypto/supported-key-methods.md new file mode 100644 index 000000000..9ff5c1416 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/webcrypto/supported-key-methods.md @@ -0,0 +1,7 @@ +--- +title: webcrypto/supported key methods +--- + +| AES-CBC | AES-CTR | AES-GCM | AES-KW | ECDH | ECDSA | HMAC | RSA-OAEP | RSASSA-PKCS1-v1_5 | RSA-PSS | +| :---------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------- | :----- | :------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------- | +| ✅ [AesCbcParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/aescbcparams) | ✅ [AesCtrParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/aesctrparams) | ✅ [AesGcmParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/aesgcmparams) | ❌ | ✅ [EcdhKeyDeriveParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/ecdhkeyderiveparams/) | ✅ [EcdsaParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/ecdsaparams/) | ✅ [HmacKeyGenParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/hmackeygenparams/) | ✅ [RsaHashedImportParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/rsahashedimportparams/) | ✅ [RsaHashedImportParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/rsahashedimportparams/) | ✅ [RsaHashedImportParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/rsahashedimportparams/) | diff --git a/docs/sources/k6/v0.55.x/shared/webcrypto/supported-sign-verify.md b/docs/sources/k6/v0.55.x/shared/webcrypto/supported-sign-verify.md new file mode 100644 index 000000000..e7b6dc148 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/webcrypto/supported-sign-verify.md @@ -0,0 +1,7 @@ +--- +title: webcrypto/supported sign/verify +--- + +| ECDSA | HMAC | RSASSA-PKCS1-v1_5 | RSA-PSS | +| :--------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------- | :---------------- | :------ | +| ✅ [EcdsaParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/ecdsaparams/) | ✅ [HmacKeyGenParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/hmackeygenparams/) | ✅ | ✅ [RsaPssParams](https://grafana.com/docs/k6//javascript-api/k6-experimental/webcrypto/rsapssparams/) | diff --git a/docs/sources/k6/v0.55.x/shared/ws-module.md b/docs/sources/k6/v0.55.x/shared/ws-module.md new file mode 100644 index 000000000..6018082a9 --- /dev/null +++ b/docs/sources/k6/v0.55.x/shared/ws-module.md @@ -0,0 +1,15 @@ +--- +title: k6/ws module admonition +--- + +{{< admonition type="note" >}} + +A module with a better and standard API exists. +
+
+The new [k6/experimental/websockets API]({{< relref "../javascript-api/k6-experimental/websockets" >}}) partially implements the [WebSockets API living standard](https://websockets.spec.whatwg.org/). +
+
+When possible, we recommend using the new API. It uses a global event loop for consistency with other k6 APIs and better performance. + +{{< /admonition >}} diff --git a/docs/sources/k6/v0.55.x/testing-guides/_index.md b/docs/sources/k6/v0.55.x/testing-guides/_index.md new file mode 100644 index 000000000..fbbfe67ef --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/_index.md @@ -0,0 +1,11 @@ +--- +title: 'Testing guides' +description: 'A series of guides to help you defining your load testing strategies.' +weight: 500 +--- + +# Testing guides + +This section provides a list of frequently asked guides to help you set your testing strategy for common cases. + +{{< section >}} diff --git a/docs/sources/k6/v0.55.x/testing-guides/api-load-testing.md b/docs/sources/k6/v0.55.x/testing-guides/api-load-testing.md new file mode 100644 index 000000000..7a9b9bce0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/api-load-testing.md @@ -0,0 +1,629 @@ +--- +title: 'API load testing' +head_title: 'Intro to API Load Testing: The k6 Guide' +description: 'Load testing APIs has many facets. This guide introduces you to performance testing and provides best practices to load test your APIs with k6.' +weight: 01 +noindex: true +--- + +# API load testing + +An API load test generally starts with small loads on isolated components. +As your testing matures, your strategy can consider how to test the API more completely. +In this process, you'll test your API with more requests, longer durations, and on a wider test scope—from isolated components to complete end-to-end workflows. + +When you design your API tests, first consider _why_ you want to test the API at all: + +- What flows or components do you want to test? +- How will you run the test? +- What criteria determine acceptable performance? + +Once you can answer these questions, your API testing strategy will likely follow something like this procedure: + +1. **Script the test.** Write user flows, parameterize test data, and group URLs. +1. **Assert performance and correctness.** Use Checks to assert system responses and use Thresholds to ensure that the system performs within your SLOs. +1. **Model and generate load.** Choose the executors to correctly model the workload that's appropriate to your test goals. Make sure the load generators are located where they should be. +1. **Iterate over your test suite.** Over time, you'll be able to reuse script logic (e.g., a user log-in flow or a throughput configuration). You'll also be able to run tests with a wider scope or as a part of your automated testing suite. + +The following sections provide specific explanations and examples of the steps in this process. + +## Identify the components to test + +Before you start testing, identify the components you want to test. +Do you want to test a single endpoint or an entire flow? + +The following script uses the [k6 HTTP module](https://grafana.com/docs/k6//javascript-api/k6-http/) to test a single endpoint. + +```javascript +import http from 'k6/http'; + +export default function () { + const payload = JSON.stringify({ + name: 'lorem', + surname: 'ipsum', + }); + const headers = { 'Content-Type': 'application/json' }; + http.post('https://httpbin.test.k6.io/post', payload, { headers }); +} +``` + +This is a minimal test, with one call to one component. +Generally, your test suite will progress from scripts like this to more complex and complete workflows. +In this process, your test suite will advance through the [testing pyramid](https://martinfowler.com/articles/practical-test-pyramid.html) as follows: + +- **Testing an isolated API**. Hammering an API endpoint like [ab](https://httpd.apache.org/docs/2.4/programs/ab.html) to test the baseline performance, breaking point, or availability. If a component doesn’t meet performance requirements, it is a bottleneck. Generally, the load is set in requests per second. +- **Testing integrated APIs**. Testing one or multiple APIs that interact with other internal or external APIs. Your focus might be on testing one system or various. +- **Testing end-to-end API flows**. Simulating realistic interactions between APIs to test the system as a whole. The focus is often on frequent and critical user scenarios. + +Your load test suite should include a wide range of tests. +But, when you start, start small and simple, +testing individual APIs and uncomplicated integration tests. + +## Determined the reason for the test + +Before you configure test load, you should know what traffic patterns you want to test the API for. +A load test typically aims to do one of two things: + +- Validate reliability under expected traffic +- Discover problems and system limits under unusual traffic. + +For example, your team might create one set of tests for frequent user flows on average traffic, and another set to find breaking points in the API. +Even if the test logic stays the same, its load might change. + +The test goal determines the test type, which in turn determines the test load. +Consider the following test types, which correspond to different goals load profiles: + +- [Smoke test](https://grafana.com/docs/k6//testing-guides/test-types/smoke-testing). Verify the system functions with minimal load. +- [“Average” load test](https://grafana.com/docs/k6//testing-guides/test-types/load-testing). Discover how the system functions with typical traffic. +- [Stress test](https://grafana.com/docs/k6//testing-guides/test-types/stress-testing). Discover how the system functions with the load of peak traffic. +- [Spike test](https://grafana.com/docs/k6//testing-guides/test-types/spike-testing). Discover how the system functions with sudden and massive increases in traffic. +- [Breakpoint test](https://grafana.com/docs/k6//testing-guides/test-types/breakpoint-testing). Progressively ramp traffic to discover system breaking points. +- [Soak test](https://grafana.com/docs/k6//testing-guides/test-types/soak-testing). Discover whether or when the system degrades under loads of longer duration. + +The test types that you choose inform how you plan and structure your test. +But each application, organization, and testing project differs. +Our recommendation is always: + +> **"Start simple and test frequently. Iterate and grow the test suite".** + +Once you've decided on the load profile, you can schedule it with k6 options. + +## Model the workload + +To configure the workload, use [test options](https://grafana.com/docs/k6//using-k6/k6-options/). +The test load configures the traffic generated by the test. k6 provides two broad ways to model load: + +- Through _virtual users_ (VUs), to simulate concurrent users +- Through _requests per second_, to simulate raw, real-world throughput + +{{< admonition type="note" >}} + +Generally, your load tests should add [sleep time](https://grafana.com/docs/k6//javascript-api/k6/sleep). +Sleep time helps control the load generator and better simulates the traffic patterns of human users. + +However, when it comes to API load tests, these recommendations about sleep come with a few qualifications. +If testing an isolated component, you might care only about performance under a pre-determined throughput. +But, even in this case, sleep can help you avoid overworking the load generator, and including a few randomized milliseconds of sleep can avoid accidental concurrency. + +When testing the API against normal, human-run workflows, add sleep as in a normal test. + +{{< /admonition >}} + +### Virtual users + +When you model load according to VUs, the basic load options are: + +- [`vus`](https://grafana.com/docs/k6//using-k6/k6-options/reference/#vus) +- [`duration`](https://grafana.com/docs/k6//using-k6/k6-options/reference/#duration) +- [`iterations`](https://grafana.com/docs/k6//using-k6/k6-options/reference/#iterations) + +You can define these options in the test script. In the following test, 50 concurrent users continuously run the `default` flow for 30 seconds. + +```javascript +import http from 'k6/http'; + +export const options = { + vus: 50, + duration: '30s', +}; + +export default function () { + const payload = JSON.stringify({ + name: 'lorem', + surname: 'ipsum', + }); + const headers = { 'Content-Type': 'application/json' }; + http.post('https://httpbin.test.k6.io/post', payload, { headers }); +} +``` + +### Request rate + +When analyzing API endpoint performance, the load is generally reported by request rate—either requests per second or per minute. + +To configure workloads according to a target request rate, use the [constant arrival rate executor](https://grafana.com/docs/k6//using-k6/scenarios/executors/constant-arrival-rate). + +`constant-arrival-rate` sets a constant rate of iterations that execute the script function. +Each iteration can generate one or multiple requests. + +To reach a request-rate target (`RequestsRate`), follow this approach: + +1. Set the rate frequency to the time unit of the target. Per second or per minute. +1. Get the number of requests per iteration (`RequestsPerIteration`). +1. Set the iteration rate to the requests per second target divided by the number of requests per iteration. + `rate` = `RequestsRate ÷ RequestsPerIteration`. + +To reach target of 50 reqs/s with the previous example: + +1. Set the `timeUnit` options to `1s`. +1. The number of requests per iteration is 1. +1. Set the `rate` option to 50/1 (so it equals 50). + +```javascript +import http from 'k6/http'; + +export const options = { + scenarios: { + my_scenario1: { + executor: 'constant-arrival-rate', + duration: '30s', // total duration + preAllocatedVUs: 50, // to allocate runtime resources preAll + + rate: 50, // number of constant iterations given `timeUnit` + timeUnit: '1s', + }, + }, +}; + +export default function () { + const payload = JSON.stringify({ + name: 'lorem', + surname: 'ipsum', + }); + const headers = { 'Content-Type': 'application/json' }; + http.post('https://httpbin.test.k6.io/post', payload, { headers }); +} +``` + +This test outputs the total number of HTTP requests and RPS on the `http_reqs` metric: + +```bash +# the reported value is close to the 50 RPS target + http_reqs......................: 1501 49.84156/s + +# the iteration rate is the same as rps, because each iteration runs only one request +iterations.....................: 1501 49.84156/s +``` + +For a more extensive example, refer to this post about [generating a constant request rate](https://k6.io/blog/how-to-generate-a-constant-request-rate-with-the-new-scenarios-api/) + +With the `constant-arrival-rate` executor, load is constant through the test. +To ramp the request rate up or down, use the [`ramping-arrival-rate`](https://grafana.com/docs/k6//using-k6/scenarios/executors/ramping-arrival-rate) executor instead. + +For all ways to model the load in k6, refer to [Scenarios](https://grafana.com/docs/k6//using-k6/scenarios). + +## Verify functionality with Checks + +Traditionally, performance tests care most about: + +- _Latency_, how fast the system responds +- _Availability_, how often the system returns errors. + +The `http_req_duration` metric reports the latency, +and `http_req_failed` reports the error rate for HTTP requests. +The previous test run provided the following results: + +```bash +http_req_duration..............: avg=106.14ms min=102.54ms med=104.66ms max=198.93ms p(90)=113.78ms p(95)=114.58ms + { expected_response:true }...: avg=106.14ms min=102.54ms med=104.66ms max=198.93ms p(90)=113.78ms p(95)=114.58ms +http_req_failed................: 0.00% ✓ 0 ✗ 1501 +``` + +Your test analysis might need to go beyond what's available with default metrics. +For more meaningful results analysis, you might also want to validate functionalities and report errors. + +Some application failures happen only under certain load conditions, such as high traffic. +These errors are hard to find. +To find the cause of failures more quickly, instrument your APIs and verify that requests get the expected responses. +To verify application logic in k6, you can use _Checks_. + +[Checks](https://grafana.com/docs/k6//using-k6/checks) validate conditions during the test execution. +For example, you can use checks verify and track API responses. +With checks, you can confirm expected API responses, such as the HTTP status or any returned data. + +Our script now verifies the HTTP response status, headers, and payload. + +```javascript +import { check } from 'k6'; +import http from 'k6/http'; + +export const options = { + scenarios: { + my_scenario1: { + executor: 'constant-arrival-rate', + duration: '30s', // total duration + preAllocatedVUs: 50, // to allocate runtime resources + + rate: 50, // number of constant iterations given `timeUnit` + timeUnit: '1s', + }, + }, +}; + +export default function () { + const payload = JSON.stringify({ + name: 'lorem', + surname: 'ipsum', + }); + const headers = { 'Content-Type': 'application/json' }; + const res = http.post('https://httpbin.test.k6.io/post', payload, { headers }); + + check(res, { + 'Post status is 200': (r) => res.status === 200, + 'Post Content-Type header': (r) => res.headers['Content-Type'] === 'application/json', + 'Post response name': (r) => res.status === 200 && res.json().json.name === 'lorem', + }); +} +``` + +In this snippet, all checks succeeded: + +```bash +my_scenario1 ✓ [======================================] 00/50 VUs 30s 50.00 iters/s + ✓ Post status is 200 + ✓ Post Content-Type header + ✓ Post response name +``` + +After the load increased to 300 requests per second, the results returned 8811 successful requests and 7 failures: + +```bash +my_scenario1 ✓ [======================================] 000/300 VUs 30s 300.00 iters/s + ✗ Post status is 200 + ↳ 99% — ✓ 8811 / ✗ 7 + ✗ Post Content-Type header + ↳ 99% — ✓ 8811 / ✗ 7 + ✗ Post response name + ↳ 99% — ✓ 8811 / ✗ 7 +``` + +By default, a failed check doesn't fail or abort the test. +In this regard, a check differs from how assertions work for other types of testing. +A load test can run thousands or millions of script iterations, each with dozens of assertions. + +**Some rate of failure is acceptable**, as determined by your SLO's "number of nines" or your organization's error budget. + +## Test your reliability goals with Thresholds + +Every test should have a goal. +Engineering organizations set their reliability goals using [Service Level Objectives](https://en.wikipedia.org/wiki/Service-level_objective) (SLOs) to validate availability, performance, or any performance requirements. + +SLOs maybe defined at distinct scopes, such as on the level of an infrastructure component, of an API, or of the entire application. +Some example SLOs could be: + +- That 99% of APIs returning product information respond in less than 600ms. +- That 99.99% of failed log-in requests respond in less than 1000ms. + +**Design your load tests with pass/fail criteria to validate SLOs**, reliability goals, or other important metrics. +To ensure your system achieves its SLOs, test them frequently, both in pre-production and production environments. + +In k6, you can use [Thresholds](https://grafana.com/docs/k6//using-k6/thresholds) to set the test pass/fail criteria. +This script codifies two SLOs in the `thresholds` object, one about error rate (availability) and one about request duration (latency). + +```javascript +export const options = { + thresholds: { + http_req_failed: ['rate<0.01'], // http errors should be less than 1% + http_req_duration: ['p(95)<200'], // 95% of requests should be below 200ms + }, + scenarios: { + my_scenario1: { + executor: 'constant-arrival-rate', + duration: '30s', // total duration + preAllocatedVUs: 50, // to allocate runtime resources + + rate: 50, // number of constant iterations given `timeUnit` + timeUnit: '1s', + }, + }, +}; +``` + +When k6 runs a test, the test output indicates whether the metrics were within the thresholds, ✅, or whether they crossed them, ❌. +In this output, the test met both thresholds. + +```bash +✓ http_req_duration..............: avg=104.7ms min=101.87ms med=103.92ms max=120.68ms p(90)=107.2ms p(95)=111.38ms + { expected_response:true }...: avg=104.7ms min=101.87ms med=103.92ms max=120.68ms p(90)=107.2ms p(95)=111.38ms +✓ http_req_failed................: 0.00% ✓ 0 ✗ 1501 +``` + +When the test fails, the k6 CLI returns a non-zero exit code—a necessary condition for test automation. +As an example of a failed test, here's the output for a test with a threshold that 95 percent of requests finish in under 50ms, `http_req_duration:["p(95)<50"]`: + +```bash +running (0m30.1s), 00/50 VUs, 1501 complete and 0 interrupted iterations +my_scenario1 ✓ [======================================] 00/50 VUs 30s 50.00 iters/s + + ✓ Post status is 200 + ✓ Post Content-Type header + ✓ Post response name + + checks.........................: 100.00% ✓ 4503 ✗ 0 + data_received..................: 1.3 MB 45 kB/s + data_sent......................: 313 kB 10 kB/s + http_req_blocked...............: avg=9.26ms min=2µs med=14µs max=557.32ms p(90)=25µs p(95)=46µs + http_req_connecting............: avg=3.5ms min=0s med=0s max=113.46ms p(90)=0s p(95)=0s + ✗ http_req_duration..............: avg=105.14ms min=102.01ms med=103.86ms max=171.56ms p(90)=112.4ms p(95)=113.18ms + { expected_response:true }...: avg=105.14ms min=102.01ms med=103.86ms max=171.56ms p(90)=112.4ms p(95)=113.18ms + ✓ http_req_failed................: 0.00% ✓ 0 ✗ 1501 + http_req_receiving.............: avg=202.86µs min=17µs med=170µs max=4.69ms p(90)=264µs p(95)=341µs + http_req_sending...............: avg=97.56µs min=11µs med=63µs max=5.56ms p(90)=98µs p(95)=133µs + http_req_tls_handshaking.......: avg=4.14ms min=0s med=0s max=169.35ms p(90)=0s p(95)=0s + http_req_waiting...............: avg=104.84ms min=101.88ms med=103.6ms max=171.52ms p(90)=112.18ms p(95)=112.85ms + http_reqs......................: 1501 49.834813/s + iteration_duration.............: avg=115.18ms min=102.51ms med=104.66ms max=704.99ms p(90)=113.68ms p(95)=115.54ms + iterations.....................: 1501 49.834813/s + vus............................: 50 min=50 max=50 + vus_max........................: 50 min=50 max=50 + +ERRO[0030] some thresholds have failed +``` + +## Scripting considerations + +If you have scripted tests before, implementing k6 scripts should seem familiar. +k6 tests are written in JavaScript, and the design of the k6 API has similarities with other testing frameworks. + +But, unlike other tests, load tests run their scripts hundreds, thousands, or millions of times. +The presence of load creates a few specific concerns. +When you load test APIs with k6, consider the following aspects of your script design. + +### Data parameterization + +_Data parameterization_ happens when you replace hard-coded test data with dynamic values. +Parameterization makes it easier to manage a load test with varied users and API calls. +A common case for parameterization happens when you want to use different `userID` and `password` values for each virtual user or iteration. + +For example, consider a JSON file with a list of user info such as: + +{{< code >}} + +```json +{ + "users": [ + { "username": "lorem", "surname": "ipsum" }, + { "username": "dolorem", "surname": "ipsum" } + ] +} +``` + +{{< /code >}} + +You can parameterize the users with the [`SharedArray`](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) object as follows: + +```javascript +import { check } from 'k6'; +import http from 'k6/http'; +import { SharedArray } from 'k6/data'; + +const users = new SharedArray('users.json', function () { + return JSON.parse(open('./users.json')).users; +}); + +export const options = {}; + +export default function () { + // now, user data is not the same for all the iterations + const user = users[Math.floor(Math.random() * users.length)]; + const payload = JSON.stringify({ + name: user.username, + surname: user.surname, + }); + + const headers = { 'Content-Type': 'application/json' }; + const res = http.post('https://httpbin.test.k6.io/post', payload, { + headers, + }); + + check(res, { + 'Post status is 200': (r) => res.status === 200, + 'Post Content-Type header': (r) => res.headers['Content-Type'] === 'application/json', + 'Post response name': (r) => res.status === 200 && res.json().json.name === user.username, + }); +} +``` + +To read more about data parameterization, check out the [parameterization examples](https://grafana.com/docs/k6//examples/data-parameterization) and [Execution context variables](https://grafana.com/docs/k6//using-k6/execution-context-variables). + +### Error handling and acceptance of failures + +**Remember to implement error handling in the test logic**. +Under sufficiently heavy load, the SUT fails and starts to respond with errors. +Though a test might be designed to induce failures, sometimes we focus on only the best-case scenario and forget the importance of accounting for errors. + +The test script must handle API errors to avoid runtime exceptions and to ensure that it tests how the SUT behaves under saturation according to the test goals. +For example, we could extend our script to do some operation that depends on the result of the previous request: + +```javascript +import { check } from 'k6'; +import http from 'k6/http'; +import { SharedArray } from 'k6/data'; + +const users = new SharedArray('users.json', function () { + return JSON.parse(open('./users.json')).users; +}); + +export const options = {}; + +export default function () { + const user = users[Math.floor(Math.random() * users.length)]; + const payload = JSON.stringify({ + name: user.username, + surname: user.surname, + }); + const headers = { 'Content-Type': 'application/json' }; + const res = http.post('https://httpbin.test.k6.io/post', payload, { + headers, + }); + + check(res, { + 'Post status is 200': (r) => res.status === 200, + 'Post Content-Type header': (r) => res.headers['Content-Type'] === 'application/json', + 'Post response name': (r) => res.status === 200 && res.json().json.name === user.username, + }); + + if (res.status === 200) { + // enters only successful responses + // otherwise, it triggers an exception + const delPayload = JSON.stringify({ name: res.json().json.name }); + http.patch('https://httpbin.test.k6.io/patch', delPayload, { headers }); + } +} +``` + +### Test reuse and modularization + +Load testing can be vast in scope, and it may involve different types of tests. +Generally, teams start with simple or critical load tests and continue adding tests for new use cases, user flows, traffic patterns, features, systems, etc. + +In this process, load testing suites grow over time. +To minimize repetitive work, try to **reuse test scripts early** and to **modularize test functions and logic.** +If you script common scenarios in reusable modules, it's easier to create different types of load tests. +The process of creating a new load test goes like this: + +1. Create a new test file. +1. Configure the specific load and other options. +1. Import the scenario. + +As your testing matures, consider creating tests that [combine multiple scenarios](https://grafana.com/docs/k6//using-k6/scenarios/advanced-examples/#combine-scenarios) to simulate more diverse traffic. + +### Dynamic URLs for one endpoint + +By default, when you access the same API endpoint with different URLs―for example, `http://example.com/posts/${id}`―k6 reports the endpoint results separately. +This may create an unnecessary amount of metrics. + +To group the results of the endpoint, use [URL grouping](https://grafana.com/docs/k6//using-k6/http-requests/#url-grouping). + +## Load generator locations + +When you plan the test, consider the locations of your _load generators_, the machines that run the test. +Sometimes, running the test from a specific location is a test requirement. +Other times, you might just choose the location based on convenience or practicality. +Either way, when you set the location of the load generator, keep the following in mind: + +- **Required locations.** To compare performance or ensure accurate results, some load tests need to measure the latency from specific locations. These tests launch the load generators from locations that match their user's region. +- **Optional locations.** Other tests try to measure against a performance baseline—how the system performance changes from a particular performance status or time. To avoid skewed latency results, ensure that the location of the load generator is constant across test tuns, and avoid running the tests from locations that are too close to the SUT. + +### Internal APIs + +End-to-end API tests try to replicate real-world user flows, which access public APIs from external systems. +Other APIs are internal and unreachable from outside. +The need to run internal tests is common when testing API integrations and isolated endpoints. + +If the API is in an internal or restricted environment, you can use k6 to test it in a few different ways: + +- Run the test from your private network using the k6 run command or the [Kubernetes operator](https://github.com/grafana/k6-operator). Optionally, store the test results in [k6 Cloud](https://grafana.com/docs/k6//results-output/real-time/cloud) or other [external services](https://grafana.com/docs/k6//results-output/real-time/). +- For cloud tests: + - [Open your firewall](https://grafana.com/docs/grafana-cloud/testing/k6/reference/cloud-ips/) for cloud test traffic. + - Run the cloud test from your [Kubernetes clusters](https://grafana.com/docs/grafana-cloud/testing/k6/author-run/private-load-zone-v2/). + +## Supplementary tools + +You might want to use k6 in conjunction with other API tools. + +### Integrate with API tools + +The tooling around REST APIs is vast, but there's not much focus on performance testing. +k6 provides a few converters to +help you incorporate the wider API tooling ecosystem into your load tests: + +- [Postman-to-k6 converter](https://github.com/apideck-libraries/postman-to-k6): to create a k6 test from a Postman collection. + + ```bash + postman-to-k6 collection.json -o k6-script.js + ``` + +- [OpenAPI k6 generator](https://k6.io/blog/load-testing-your-api-with-swagger-openapi-and-k6/#api-load-testing-with-swaggeropenapi-specification): to create a k6 test from an Open API (formerly Swagger) definition. + + ```bash + openapi-generator-cli generate -i my-api-spec.json -g k6 + ``` + +These tools generate a k6 test that you can edit and run as usual: + +```bash +k6 run k6-script.js +``` + +Depending on the test type, the converters could help you quickly create your first tests or help onboard new users to k6. +Even so, we recommend you get familiar with the [k6 Javascript API](https://grafana.com/docs/k6//javascript-api) and script your own tests. + +### Using proxy recorders + +Another option is to auto-generate a k6 test from a recorded session. These scripts might help you start building more complex end-to-end and integration tests. + +The [har-to-k6 converter](https://github.com/grafana/har-to-k6) creates the k6 test from a recorded session in HAR format which collects HTTP traffic. + +```bash +har-to-k6 archive.tar -o k6-script.js +``` + +The generated k6 test can be edited and run as usual: + +```bash +k6 run k6-script.js +``` + +To export a recorded session to HAR format, use a proxy recorder such as [Fiddler proxy](https://www.telerik.com/fiddler/fiddler-everywhere) or [GitLab HAR recorder](https://gitlab.com/gitlab-org/security-products/har-recorder/). + +As with the previous converters, the recorder can help prototype tests. +Again, we recommend learning to write your test scripts. + +## Beyond HTTP APIs + +Due to the popularity of the web and REST APIs, this guide has used the term focused on HTTP APIs. But APIs are not restricted to the HTTP protocol. + +By default, k6 supports testing the following protocols: + +- [HTTP/1.1, HTTP/2](https://grafana.com/docs/k6//javascript-api/k6-http/) +- [WebSockets](https://grafana.com/docs/k6//javascript-api/k6-ws/) +- [Redis (experimental)](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis/) +- [gRPC](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/) + +```javascript +import grpc from 'k6/net/grpc'; +import { check, sleep } from 'k6'; + +const client = new grpc.Client(); +client.load(['definitions'], 'hello.proto'); + +export default () => { + client.connect('grpcbin.test.k6.io:9001'); + + const data = { greeting: 'Bert' }; + const response = client.invoke('hello.HelloService/SayHello', data); + + check(response, { + 'status is OK': (r) => r && r.status === grpc.StatusOK, + }); + + client.close(); + sleep(1); +}; +``` + +But modern software is not built only based on these protocols. Modern infrastructure and applications rely on other API protocols to provide new capabilities or improve their performance, throughput, and reliability. + +To test the performance and capacity of these systems, the testing tool should be able to generate protocol-specific requests against their APIs. + +If k6 doesn't support a protocol you need, you can use (or create) [extensions](https://grafana.com/docs/k6//extensions). +The list of extensions is long: + +- Avro +- ZeroMQ +- Ethereum +- STOMP +- MLLP +- NATS +- and [more](https://grafana.com/docs/k6//extensions/explore). diff --git a/docs/sources/k6/v0.55.x/testing-guides/automated-performance-testing.md b/docs/sources/k6/v0.55.x/testing-guides/automated-performance-testing.md new file mode 100644 index 000000000..8647bd06d --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/automated-performance-testing.md @@ -0,0 +1,326 @@ +--- +title: 'Automated performance testing' +head_title: 'How to Automate Performance Testing: The k6 Guide' +description: 'Performance testing automation is about establishing a repeatable and consistent process that checks reliability issues at different stages of the development and release cycle.' +weight: 02 +--- + +# Automated performance testing + +Performance testing automation is about establishing **a repeatable and consistent process that checks reliability issues** at different stages of the development and release cycle. For instance, you could run performance tests from CI/CD pipelines and nightly jobs, or manually trigger load tests and monitor their impact in real-time. + +In performance testing, automation does not remove the need to run tests manually. It’s about planning performance tests as part of your Software Development Life Cycle (SDLC) for **continuous performance testing**. + +This guide provides general recommendations to help you plan and define a strategy for running automated performance tests: + +- Which tests to automate? +- Which environment to test? +- What frequency and how to run tests? +- How to analyze performance results? + +Please note that this guide assumes you are familiar with k6 and already have performance tests. If you are new to performance testing or k6, we recommend looking at our [get-started resources](https://grafana.com/docs/k6//get-started/resources#learning). + +Before we dive in, let's consider the "why" behind automation and how it unlocks the full benefits of your performance testing efforts. + +## Why automate performance tests + +Whether it’s a website loading in under a second, API responses in milliseconds, or instantaneous fault responses, performance is critical as it directly impacts the end-user experience. However, an organizational challenge is that performance may often not receive the recognition of a feature or requirement. + +Performance is still intangible in many organizations, which react only when bad things happen. Automation changes this approach - **from reactive to proactive**. + +In performance testing, it's crucial to establish routines to be consistent in our practices. Automation is necessary to create a performance testing habit, and boost some of its [benefits](https://k6.io/why-your-organization-should-perform-load-testing/), including: + +- **Improve testing coverage, confidence, and maintenance**: Automation creates a constant and iterative process for various types of testing. This continuous effort in performance testing leads to expanded test coverage, enhanced test maintenance, and increased confidence in testing outcomes. +- **Detect issues earlier**: Automating performance tests as part of the software delivery process can ensure applications meet reliability goals while catching issues earlier in the SDLC. +- **Collaborate across teams**: Automation prompts teams to outline a strategy and plan across the SDLC and departments. It fosters engineering leaders to advocate for reliability and implement shared practices. + +Without automation, the lack of a shared framework often leads to isolated and sporadic activities. Automation helps drive continuous performance and reliability testing, introducing a **more efficient and effective testing process**. + +### More than CI/CD + +Automation often refers to running tests with pass/fail conditions as part of the release process within CI/CD pipelines. However, not all performance tests are suited for CI/CD workflows, nor are they solely about providing a Pass/Fail (green/red) status and acting as a release gatekeeper. + +[Automation into CI/CD pipelines](/integrations/#continuous-integration-and-continuous-delivery) is an option, but it's not the only method to schedule the execution of performance tests. When creating a performance testing plan, it’s important to remember that there are different ways to run performance tests in a frequent basis: + +- Cron and cron job runners. +- Cloud testing tools, such as [scheduling in Grafana Cloud k6](https://grafana.com/docs/grafana-cloud/testing/k6/author-run/schedule-a-test/). +- Test management tools with automation capabilities. +- Trigger manual tests. Include this as a step in your release checklist process. + +Running tests only from CI/CD tools on software changes limits the objectives of our performance tests. A complete performance testing strategy can include CI/CD tests, cron-based tests, or even manual tests to address various testing purposes: + +## Determine the purpose of the tests + +The first step in the process is reviewing your existing or planned tests and understanding each test's purpose. Can the test serve additional purposes if executed regularly? Some common goals are: + +- Comparing current performance against an existing performance baseline. +- Understanding variances over time in key performance metrics. Observing flat or changing trends. +- Detecting regressions of new releases. +- Testing Service Level Objectives (SLOs) on a regular basis. +- Testing critical areas during the release process. +- Setting quality gates in the CI/CD pipelines. + +When considering a consistent and ongoing purpose for each test, you discover which tests to automate, any lacking functionality, and missing tests in your test suite. It also guides you in determining the best time to run each test and how. + +## Choose which tests to automate + +Performance tests can generally be divided into two aspects: + +- Test scenario (test case): What is the test verifying? +- Test workload (test load): How much traffic and which traffic pattern? + +Your test suite should incorporate a diverse range of tests that can verify critical areas of your system using distinct [load test types](https://grafana.com/docs/k6//testing-guides/test-types/). + +Any existing test that you wish to run on a frequent basis is a candidate for automation. Fundamentally, automation is about running tests frequently and consistently, whether that's daily, weekly, or annually. + +When designing your performance test suite for automation, consider two key points: start simple and modularize your tests. + +- **Start simple and iterate**: Your test suite, and consequently test coverage, will expand as the team learns and encounters reliability issues to investigate. +- **Modularize your test suite**: In k6, you can separate the scenario and workload logic and reuse them across different tests. That simplifies the process of creating tests with various traffic patterns for different purposes. Modularization also allows reusing common logic across multiple tests. + +When planning test coverage or automation, consider starting with tests that: + +- Verify the core functionality crucial to the product and business. +- Evaluate the performance in scenarios with high traffic. +- Track key performance metrics to observe their trends and compare against their baselines. +- Validate reliability goals or SLOs with Pass/Fail criteria. + +## Model the scenarios and workload + +Once one or multiple tests have been selected, you should determine the various types of traffic that need to be tested. + +Let’s illustrate an example with two simple tests: one test to assess the performance of a GET endpoint and one test to verify a checkout process. + +The next step is to identify the traffic the system under test (SUT) handles for these tests. In this case, we could utilize our analytics and monitoring tools to find the typical traffic patterns for the GET endpoint and checkout flow. + +Depending on the type of traffic, we can create different kinds of tests: + +- [Smoke](https://grafana.com/docs/k6//testing-guides/test-types/smoke-testing): Test for script errors and verify SUT with minimal traffic. +- [Average-Load](https://grafana.com/docs/k6//testing-guides/test-types/load-testing): Test for regular/normal traffic. +- [Stress](https://grafana.com/docs/k6//testing-guides/test-types/stress-testing): Test for maximum expected traffic. +- [Spike](https://grafana.com/docs/k6//testing-guides/test-types/spike-testing): Test for a surge of traffic. +- [Soak](https://grafana.com/docs/k6//testing-guides/test-types/soak-testing): Test for a prolonged period of traffic. + +In our example, we decided on the following workload for the two scenarios: + +| Test scenario | Smoke | Average | Stress | Spike | Soak | +| ---------------- | ------------ | --------------- | ---------------- | ------------ | ---- | +| GET endpoint | 1 iteration | 100 reqs/s - 3m | 1500 reqs/s - 5m | | | +| Checkout process | 3 iterations | 50 VUs - 5m | | 200 VUs - 1m | | + +> We recommend always creating average-load tests for baseline comparisons and smoke tests to validate test script errors before executing larger tests. + +In our example, we have tests that use the same test scenario with distinct workloads. This pattern is extremely common. In this case, the ability to reuse the scenario logic across tests simplifies both test creation and maintenance. A common pattern for organizing tests is prefixing them with the type of workload: + +- `smoke-get-api.js`:   imports the common scenario and set 1 iteration. +- `load-get-api.js`:     imports the common scenario and set 100 reqs/s during 3m. +- `stress-get-api.js`: imports the common scenario and set 1500 reqs/s during 3m. + +_To learn more about configuring workloads in k6, check out [Scenarios](https://grafana.com/docs/k6//using-k6/scenarios#scenario-executors)._ + +## Decide the testing frequency for each environment + +The next step is to decide which environment to test and its frequency. Each organization has different environments, and their purpose might also vary from one organization to another. + +Here are some common environments found at organizations, and general guidelines of what kind of testing to use them for. + +### Development environment + +This environment, whether the personal machine or its dedicated environment, might not include all the components of the system. It is commonly used for preliminary testing before deploying the application to a more comprehensive environment. + +This environment is great for verifying the basic functionality of our tests by running smoke tests. + +In this type of environment, debugging and building our performance tests is more common than any type of automation. However, if your project structure permits, you can also schedule the execution of smoke tests on project changes. + +### QA environment + +This environment often deploys the entire application but with minimal infrastructure resources. It’s like a low-scale staging environment that all teams can use to test functional aspects and find regressions for new features. + +Given the infrastructure does not closely match the production environment, this type of QA environment is unsuitable for assessing the performance and scalability of the application. + +However, validating the functional aspects of our testing with smoke tests can help to catch errors earlier in this environment. Additionally, it verifies that the same script can run in larger load tests later. + +Run all the available smoke tests: end-to-end, integration, and unit test types. Schedule these tests as part of the suite of automated tests executed in the CI flow. + +### Pre-release and ephemeral environments + +These environments are available to test upcoming releases, with each organization using them differently as part of their unique release process. + +As a general rule on pre-release environments, we should run our larger tests with quality gates, Pass/Fail criteria that validate SLOs or reliability goals. In k6, you can do that by using [Thresholds](https://grafana.com/docs/k6//using-k6/thresholds) in `options` as follows: + +```javascript +export const options = { + thresholds: { + // http errors should be less than 1% + 'http_req_failed': ['rate<0.01'], + // 90% of requests should be below 600ms + 'http_req_duration': ['p(90)<600'], + // 95% of requests tagged as static content should be below 200ms + 'http_req_duration{type:staticContent}': ['p(99)<250'], + // the error rate of my custom metric should be below 5% + 'my_custom_metric': ['rate<0.05'], + }, +}; +``` + +However, it can be challenging to effectively assess all reliability goals. Frequently, you’ll encounter “false positives” and “true negatives” when testing with distinct types of load. + +For larger tests, verifying the release based “only” on a Pass/Fail status can create a false sense of security in your performance testing and release process. + +We recommend keeping the pre-release environment available for a few hours or days to thoroughly test the entire system. Our recommendations include: + +- Allocating a period of one to several days for validating the release. +- Executing all the existing average-load, stress, and spike tests. +- Executing each test at least twice consecutively. +- Scheduling all tests to run periodically, for instance, every 4 hours or 8 hours. + +### Staging/pre-production + +In some cases, the staging environment acts like the “Pre-release” environment. If so, follow the strategy mentioned in the previous section. + +The staging environment is always available and consistently updated with the latest changes. It’s generally suitable for assessing performance changes like performance trends, regressions, or improvements. + +In this case, we should choose the tests that assess key performance indicators and schedule them for consistent execution to collect metrics over a period. Start by selecting a few tests and scheduling their runs two to three times per week. + +Like in the pre-release environment, we suggest executing each test at least twice consecutively, allowing us to ignore unreliable tests. + +As we aim to find performance changes, consider scaling the workload of the test according to the staging infrastructure, which often does not match the scale of the production environment. + +### Production + +Typically, the previous testing environments do not perfectly mirror the production environment, with differences in test data, infrastructure resources, and scalability policies. + +Testing in production provides real-world insights that cannot be achieved in other environments. However, production testing requires a careful approach to handling and storing test data in production and avoiding impacting real users. + +A low-risk common practice is to utilize smoke tests for synthetic testing, also called [synthetic monitoring](https://grafana.com/docs/grafana-cloud/testing/synthetic-monitoring/create-checks/checks/k6/). Testing production with minimal load is safe. Schedule smoke tests every five minutes, establishing Pass/Fail test conditions and an effective alerting mechanism. For instance, if six consecutive test runs fail, send an alert. + +If release strategies like Blue/Green or Canary deployments are in place, run load tests against the Green or new version to validate the release. It's an ideal moment to see how SLOs behave in production. + +Also, consider scheduling nightly tests or when the system handles less traffic. The goal is not to stress the system, but to consistently gather performance results to compare changes and analyze performance trends. For instance, schedule tests with half of the average traffic level on a weekly basis. + +### Example plan + +| Test | Deployment Env. | Type | Workload | Automation | Frequency | +| ---------------- | --------------- | -------- | ---------------- | -------------------------------------- | ----------------------------------------- | +| Checkout process | QA | Smoke | 1 iteration | CI flow | Branch changes | +| Checkout process | Pre-release | Average | 50 VUs - 5m | Scheduled during QA/Pre-release period | 3 times per day during pre-release period | +| Checkout process | Pre-release | Spike | 200 VUs - 1m | Scheduled during QA/Pre-release period | 3 times per day during pre-release period | +| Checkout process | Staging | Average | 50 VUs - 5m | Schedule | 2 times per week | +| | | | | | | +| GET endpoint | QA | Smoke | 1 iteration | CI flow | Branch changes | +| GET endpoint | Pre-release | Average | 100 reqs/s - 3m | Scheduled during QA/Pre-release period | 3 times per day during pre-release period | +| GET endpoint | Pre-release | Stress | 1500 reqs/s - 5m | Scheduled during QA/Pre-release period | 3 times per day during pre-release period | +| GET endpoint | Staging | Average | 100 reqs/s - 3m | Schedule | 2 times per week | +| GET endpoint | Production | 50% Avg. | 50 reqs/s - 3m | Schedule on minimal traffic | Weekly | + +## Plan the result analysis process + +Following the previous steps, you should now have an initial performance testing plan. Now, let’s see how we can analyze and interpret performance results. + +The first step is learning what options you have for outputting performance results. If you’re using k6, there are a few [options you can choose from](https://grafana.com/docs/k6//results-output/). You can review those options and the [k6 metrics](https://grafana.com/docs/k6//using-k6/metrics) to decide on a long-term solution to analyze the results of your test automation plan. + +Here are some questions to consider when creating your result analysis process. + +### How to store your performance results + +In k6, you can get the [aggregated results](https://grafana.com/docs/k6//results-output/end-of-test) at the end of a test, or [time series metrics in real-time](https://grafana.com/docs/k6//results-output/real-time). Both options allow you to customize the output. + +The process we recommend is: + +- Select a storage backend. +- Understand how it stores your test data and its particular capabilities. +- Learn how to query and visualize results and any limitations. +- Establish a policy for deleting old test results, and make sure to retain key performance results for future comparisons like baseline performance data. +- Test the solution and decide on a long-term storage choice to avoid frequent changes to this critical component. + +### Which key performance metrics will be your focus + +Think about the goal of each particular test, and make sure you track the metrics that depend on your test goals. + +k6 provides [built-in metrics](https://grafana.com/docs/k6//using-k6/metrics/reference) that aggregate all the interactions against the SUT. You can also utilize [tags](https://grafana.com/docs/k6//using-k6/tags-and-groups#user-defined-tags) and custom metrics to categorize and filter results for one interaction or particular types. + +Consider defining your own performance criteria and their visualization. For instance, set different colors for what is good, acceptable, slightly concerning, or wrong so you can quickly visualize if a particular set of performance results are ok. + +Think about performance changes. Is there any particular metric to compare changes or track its trend over time? Most of the test visualization choices focus on the results of individual test runs. Consider implementing a way to visualize the result of a critical performance metric over time so you can identify any changes and trends. + +### How often will you analyze results + +Consider creating dashboards and custom notifications that can quickly provide an overview of the latest results of any automated tests. These dashboards are the first line to indicate issues requiring investigation. + +Additionally, we recommend setting up alerts for important issues. Think about priority and non-priority levels and follow-up actions. Consider these [tips to design alerts](https://grafana.com/docs/grafana/latest/alerting/#design-your-alerting-system). + +### Correlate testing and observability data + +Last but not least, set up proper instrumentation of the SUT and understand the monitoring and observability in place for system and application data. + +Performance testing results can highlight poor performance, such as slow responses. However, it does not show what happens internally on the SUT, such as a slow SQL query or CPU and memory saturation. + +To bridge the gap, work out a way to correlate testing results with how you instrument your infrastructure and application code. For instance, connecting or building custom dashboards with test results or using [trace data from your tests](https://grafana.com/docs/k6//javascript-api/jslib/http-instrumentation-tempo/). + +Continuous testing helps detect issues and performance degradations, whether from test results or system data. Proper observability will help to find the root cause. + +### Example plan + +As we finalize our planning, we can organize our test plan considering how often tests are run, the options to analyze their results, and follow-up actions. For example: + +| Deployment Env. | Test type | Frequency | Alerts | Test overview | Action | +| --------------- | ---------------------------------------- | --------------------------------------------------- | -------------------------------- | ----------------------------------- | -------------------------------------------------------- | +| QA | Smoke tests | CI on Branch changes | CI | | Fix broken tests | +| Pre-release | All performance tests except smoke tests | 2 or 3 times daily during the QA/pre-release period | | | Validate the release after assessing performance results | +| Staging | Baseline performance tests | Schedule 2 times per week | Only for critical issues | Custom dashboard | Oversee test results | +| Production | Baseline performance tests | Weekly schedule | Priority and Non-priority issues | Custom dashboards and notifications | Oversee test results | +| Production | Synthetic tests | Hourly schedule | Custom alerts | | Respond to alerts | + +## Considerations + +### Start simple and then iterate + +When starting your performance test plan or automation, it is common to think about having a few dozen scenarios to test. Start small to avoid planning paralysis. + +We recommend beginning with a few distinct tests across testing environments. + +Over time, you can add more tests, and your performance test suite will gradually increase its test coverage. + +Focus on proving your test automation plan and solution across the software release process. A successful implementation will pave the way for collaborating with other teams and promoting the value of continuous performance testing. + +### Test consistency is critical + +One of the primary objectives of continuous performance testing is assessing changes in the key metrics that define reliability and performance goals. To achieve this, we need to compare the value of these metrics between test runs over a period. + +It’s critical to compare test run results of the same test. Otherwise, you’re comparing apples with oranges. Compare identical test runs, the same workload, running the same scenario with the same test data against the same environment. + +Make sure not to introduce variance between test runs. If changes are necessary, rename or create a new test and start comparing test results from scratch. + +Additionally, we recommend scheduling the execution of the same test twice and almost consecutively. This collects one extra test run result for better comparison and allows us to ignore a potentially unreliable test. + +### Establish how to stop automated and manual tests + +Certain performance tests, especially those involving heavy-load tests, might cause outages. Automating the execution of risky tests without supervision may not be desirable, but that doesn't mean you should avoid them. + +These tests require controlled test execution and real-time analysis of test results, allowing them to be stopped before the system becomes unresponsive. + +Similarly, you might want to stop a test when the system begins to produce a flood of errors. When a system becomes completely overloaded, continuing the test execution often doesn't provide more meaningful insights and merely consumes resources. + +To stop a k6 test, learn how to use the [`abortOnFail` threshold option](https://grafana.com/docs/k6//using-k6/thresholds#abort) or integrate with the k6 CLI or Grafana Cloud k6. + +### Complement automation with a repeatable QA process + +We mentioned this at the beginning of the guide: automation in performance testing is about establishing a repeatable and consistent testing process. + +You should also plan the frequency of tests that are manually triggered and require supervision of the system during their execution. To ensure these different cases are consistently tested, set reminders and document them as part of the QA process and release checklists. Common examples are: + +- Running soak tests quarterly. +- Running heavy-stress tests 2 months before an important seasonal event. +- Running heavy-load tests for major releases in a pre-release environment. + +### Quality gates in CI/CD may result in false assurance + +Quality gates in performance tests are often defined as [Pass/Fail criteria](https://grafana.com/docs/k6//using-k6/thresholds) that verify the release meets its reliability goals. + +However, setting up reliable quality gates is challenging when testing thousands or millions of interactions. The test script, the SLO for that particular environment, and the Pass/Fail criteria could easily be wrong. + +Assume reliability checks may have false negatives (and vice versa); ensure performance tests don't block releases wrongly. + +Unless your verification process is mature, do not rely entirely on Pass/Fail results to guarantee the reliability of releases. If unsure, start utilizing Pass/Fail results to warn about possible issues for deeper investigation, and continuously tweak the criteria until becoming confident. + +Moreover, note that the load test duration often takes between 3 to 15 minutes or more; thus, introducing performance testing into CI/CD significantly increases the time of the release process. This is another reason we advise not to run larger tests in pipelines meant for automatic deployment. Instead, plan one or more days for performance testing pre-releases in a dedicated environment. diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/_index.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/_index.md new file mode 100644 index 000000000..b355110ed --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/_index.md @@ -0,0 +1,59 @@ +--- +aliases: + - ../javascript-api/xk6-disruptor/get-started # docs/k6//javascript-api/xk6-disruptor/get-started/ +title: 'Injecting faults with xk6-disruptor' +description: 'xk6-disruptor is a k6 extension providing fault injection capabilities to test system reliability under turbulent conditions.' +weight: 06 +--- + +# Injecting faults with xk6-disruptor + +[xk6-disruptor](https://github.com/grafana/xk6-disruptor) is an extension that adds fault injection capabilities to k6. It implements the principles of the Chaos Engineering discipline to test the reliability of our applications under turbulent conditions such as delays and response errors. + +Key features include: + +- Everything as code. Facilitate test reuse and collaboration between teams without learning a new DSL. +- Fast to adopt with no day-two surprises. [No need to deploy and maintain](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/how--it-works) a fleet of agents or operators. +- Easy to extend and integrate with other types of k6 tests. No need to try to glue multiple tools together to get the job done. + +## Capabilities + +Currently, the disruptor is intended to test applications running in Kubernetes. Other platforms are not supported at this time. + +It provides a Javascript API to inject different [faults](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/) in HTTP and gRPC requests, such as errors and delays, into the selected Kubernetes [Pods](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/) or [Services](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/). + +Other types of faults and disruptors will be introduced in the future. The [Roadmap](https://github.com/grafana/xk6-disruptor/blob/main/ROADMAP.md) presents the project's goals for the coming months regarding new functionalities and enhancements. + +## Use cases + +The disruptor lets you test the resiliency of distributed applications by introducing errors in the requests served by your services. + +The disruptor does not try to reproduce root causes, such as failed application instances or degraded computing or network resources. +It focuses on reproducing the side effects of such failures, so you can focus on understanding the propagation of errors between internal and public services and improving the error handling in your application. + +This way, the disruptor makes reliability tests repeatable and predictable while limiting their blast radius. +These are essentials to test applications deployed on shared infrastructures such as pre-production and testing environments. + +Common use cases are: + +- Test resilient policies such as backoff, timeouts, retries, etc. +- Test the fallback functionality when internal failures arise. +- Test SLOs under common internal failures. +- Test application performance when experiencing delays. +- Add fault injection to existing performance tests. + +## Learn more + +Lear more about [Fault injection](https://k6.io/blog/democratize-chaos-testing/) and [Building Resilience early in the development cycle](https://k6.io/blog/building-resilience-early-in-the-development-cycle/). + +Check the [first steps](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/first-steps) to get started with the disruptor. + +Follow the [examples of injecting faults in different scenarios](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/examples). + +Visit the [interactive demo environment in Killercoda](https://killercoda.com/grafana-xk6-disruptor/scenario/killercoda) and try the disruptor in a demo application without having to do any setup. + +## Contributing + +For any unexpected behavior, please search the [GitHub issues](https://github.com/grafana/xk6-disruptor/issues) first. + +And if you are interested in contributing to the development of this project, check the [contributing guide](https://github.com/grafana/xk6-disruptor/blob/main/docs/01-development/01-contributing.md). diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/_index.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/_index.md new file mode 100644 index 000000000..cb6796777 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/_index.md @@ -0,0 +1,18 @@ +--- +title: 'xk6-disruptor examples' +description: 'Examples of how to use the xk6-disruptor extension to introduce faults in k6 tests.' +weight: 07 +--- + +# xk6-disruptor examples + +In this section, we present some examples of using the `xk6-disruptor` extension to introduce faults in `k6` tests. + +- [Injecting gRPC faults into a Service](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/examples/inject-grpc-faults-into-service) +- [Injecting HTTP faults into a Pod](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/examples/inject-http-faults-into-pod) +- [Interactive demo](https://killercoda.com/grafana-xk6-disruptor/scenario/killercoda) (Killercoda) + +To follow the instructions of the examples, check first the system under test meets the [requirements](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/requirements) to receive faults, in particular: + +- You have configured the credentials to access the Kubernetes cluster. +- This cluster exposes the service using an external IP. diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/demo-environment.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/demo-environment.md new file mode 100644 index 000000000..6b907d123 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/demo-environment.md @@ -0,0 +1,8 @@ +--- +title: 'Interactive demo' +weight: 03 +--- + +# Interactive demo + +Visit the [interactive demo environment in Killercoda](https://killercoda.com/grafana-xk6-disruptor/scenario/killercoda) and try the disruptor in a demo application without having to do any setup. diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/inject-grpc-faults-into-service.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/inject-grpc-faults-into-service.md new file mode 100644 index 000000000..8b1a892c0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/inject-grpc-faults-into-service.md @@ -0,0 +1,439 @@ +--- +title: 'Inject gRPC faults into Service' +description: 'This example shows how to test the effect of faults injected in the gRPC requests served by a service.' +weight: 01 +--- + +# Inject gRPC faults into Service + +This example shows a way to use the [ServiceDisruptor](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor) to test the effect of faults injected in the gRPC requests served by a service. + +The complete [source code](#source-code) is at the end of this document. The next sections examine the code in detail. + +The example uses [grpcpbin](https://grpcbin.test.k6.io), a simple request/response application that offers endpoints for testing different gRPC requests. + +The test requires `grpcpbin` to be deployed in a cluster in the namespace `grpcbin` and exposed with an external IP. The IP address is expected in the environment variable `GRPC_HOST`. + +For the Kubernetes manifests and the instructions on how to deploy it to a cluster, refer to the [test setup](#test-setup) section at the end of this document. To learn more about how to get the external IP address, refer to [Expose your application](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/expose--your-application). + +## Initialization + +The initialization code imports the external dependencies required by the test. The [ServiceDisruptor](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor) class imported from the `xk6-disruptor` extension provides functions for injecting faults in services. The [k6/net/grpc](https://grafana.com/docs/k6//javascript-api/k6-net-grpc) module provides functions for executing gRPC requests. The [check](https://grafana.com/docs/k6//using-k6/checks) function verifies the results from the requests. + +```javascript +import { ServiceDisruptor } from 'k6/x/disruptor'; +import grpc from 'k6/net/grpc'; +import { check } from 'k6'; +``` + +We also create a grpc client and load the protobufs definitions for the [HelloService](https://github.com/moul/pb/blob/master/hello/hello.proto) service. + + + +```javascript +const client = new grpc.Client(); +client.load(['pb'], 'hello.proto'); +``` + + + +## Test Load + +The test load is generated by the `default` function, which connects to the `grpcbin` service using the IP and port obtained from the environment variable `GRPC_HOST` and invokes the `SayHello` method of the `hello.HelloService` service. Finally, The status code of the response is checked. When faults are injected, this check should fail. + + + +```javascript +export default function () { + client.connect(__ENV.GRPC_HOST, { + plaintext: true, + timeout: '1s', + }); + + const data = { greeting: 'Bert' }; + const response = client.invoke('hello.HelloService/SayHello', data, { + timeout: '1s', + }); + client.close(); + + check(response, { + 'status is OK': (r) => r && r.status === grpc.StatusOK, + }); +} +``` + + + +## Fault injection + +The `disrupt` function creates a `ServiceDisruptor` for the `grpcbin` service in the namespace `grpcbin`. + +The gRPC faults are injected by calling the [ServiceDisruptor.injectGrpcFaults](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/injectgrpcfaults) method using a fault definition that introduces a delay of `300ms` on each request and an error with status code `13` ("Internal error") in `10%` of the requests. + + + +```javascript +export function disrupt() { + if (__ENV.SKIP_FAULTS == '1') { + return; + } + + const fault = { + averageDelay: '300ms', + statusCode: grpc.StatusInternal, + errorRate: 0.1, + port: 9000, + }; + const disruptor = new ServiceDisruptor('grpcbin', 'grpcbin'); + disruptor.injectGrpcFaults(fault, '30s'); +} +``` + + + +Notice the following code snippet in the `injectFaults` function above: + +```javascript +export function disrupt() { + if (__ENV.SKIP_FAULTS == '1') { + return; + } + //... +} +``` + +This code makes the function return without injecting faults if the `SKIP_FAULTS` environment variable is passed to the execution of the test with a value of "1". We will use this option to obtain a [baseline execution](#baseline-execution) without faults. + +## Scenarios + +This test defines two [scenarios](https://grafana.com/docs/k6//using-k6/scenarios) to be executed. The `load` scenario applies the test load to the `grpcpbin` application for `30s` invoking the `default` function. The `disrupt` scenario invokes the `disrupt` function to inject a fault in the gRPC requests to the target application. + +```javascript +export const options = { + scenarios: { + load: { + executor: 'constant-arrival-rate', + rate: 100, + preAllocatedVUs: 10, + maxVUs: 100, + exec: 'default', + startTime: '0s', + duration: '30s', + }, + disrupt: { + executor: 'shared-iterations', + iterations: 1, + vus: 1, + exec: 'disrupt', + startTime: '0s', + }, + }, +}; +``` + +{{< admonition type="note" >}} + +The `disrupt` scenario uses a `shared-iterations` executor with one iteration and one `VU`. This setting ensures the `disrupt` function is executed only once. Executing this function multiples times concurrently may have unpredictable results. + +{{< /admonition >}} + +## Executions + +{{< admonition type="note" >}} + +The commands in this section assume the `xk6-disruptor` binary is available in your current directory. This location can change depending on the installation process and the platform. Refer to [Installation](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/installation) for details on how to install it in your environment. + +{{< /admonition >}} + +### Baseline execution + +We will first execute the test without introducing faults to have an baseline using the following command: + +{{< code >}} + +```bash +xk6-disruptor --env SKIP_FAULTS=1 --env GRPC_HOST=$GRPC_HOST run grpc-faults.js +``` + +```windows-powershell +xk6-disruptor --env SKIP_FAULTS=1 --env "GRPC_HOST=$Env:GRPC_HOST" run grpc-faults.js +``` + +{{< /code >}} + +Notice the argument `--env SKIP_FAULT=1`, which makes the `disrupt` function return without injecting any fault as explained in the [fault injection](#fault-injection) section. Also notice the `--env GRPC_HOST` argument, which passes the external IP used to access the `grpcbin` application. + +You should get an output similar to the following (use the `Expand` button to see all output). + +{{< code >}} + +``` + /\ |‾‾| /‾‾/ /‾‾/ + /\ / \ | |/ / / / + / \/ \ | ( / ‾‾\ + / \ | |\ \ | (‾) | + / __________ \ |__| \__\ \_____/ .io + + execution: local + script: scripts/grpc-faults.js + output: - + + scenarios: (100.00%) 2 scenarios, 101 max VUs, 10m30s max duration (incl. graceful stop): + * disrupt: 1 iterations shared among 1 VUs (maxDuration: 10m0s, exec: disrupt, gracefulStop: 30s) + * load: 100.00 iterations/s for 30s (maxVUs: 10-100, exec: default, gracefulStop: 30s) + + +running (00m30.0s), 000/011 VUs, 3001 complete and 0 interrupted iterations +disrupt ✓ [======================================] 1 VUs 00m00.0s/10m0s 1/1 shared iters +load ✓ [======================================] 000/010 VUs 30s 100.00 iters/s + + ✓ status is OK + + checks...............: 100.00% ✓ 3000 ✗ 0 + data_received........: 416 kB 14 kB/s + data_sent............: 630 kB 21 kB/s + grpc_req_duration....: avg=1.36ms min=512.43µs med=1.25ms max=26.62ms p(90)=1.75ms p(95)=2.09ms + iteration_duration...: avg=4.4ms min=52.86µs med=4.01ms max=66.26ms p(90)=5.32ms p(95)=6.35ms + iterations...........: 3001 100.028406/s + vus..................: 10 min=10 max=10 + vus_max..............: 11 min=11 max=11 +``` + +{{< /code >}} + +### Fault injection + +We repeat the execution injecting the faults. Notice we have removed the `--env SKIP_FAULTS=1` argument. + +{{< code >}} + +```bash +xk6-disruptor --env GRPC_HOST=$GRPC_HOST run grpc-faults.js +``` + +```windows-powershell +xk6-disruptor --env "GRPC_HOST=$Env:GRPC_HOST" run grpc-faults.js +``` + +{{< /code >}} + +{{< code >}} + +``` + /\ |‾‾| /‾‾/ /‾‾/ + /\ / \ | |/ / / / + / \/ \ | ( / ‾‾\ + / \ | |\ \ | (‾) | + / __________ \ |__| \__\ \_____/ .io + + execution: local + script: scripts/grpc-faults.js + output: - + + scenarios: (100.00%) 2 scenarios, 101 max VUs, 10m30s max duration (incl. graceful stop): + * disrupt: 1 iterations shared among 1 VUs (maxDuration: 10m0s, exec: disrupt, gracefulStop: 30s) + * load: 100.00 iterations/s for 30s (maxVUs: 10-100, exec: default, gracefulStop: 30s) + + +running (00m50.6s), 000/033 VUs, 2975 complete and 0 interrupted iterations +disrupt ✓ [======================================] 1 VUs 00m50.6s/10m0s 1/1 shared iters +load ✓ [======================================] 000/032 VUs 30s 100.00 iters/s + + ✗ status is OK + ↳ 89% — ✓ 2668 / ✗ 306 + + checks...............: 89.71% ✓ 2668 ✗ 306 + data_received........: 405 kB 8.0 kB/s + data_sent............: 615 kB 12 kB/s + dropped_iterations...: 26 0.513628/s + grpc_req_duration....: avg=270.51ms min=502.12µs med=302.34ms max=310.48ms p(90)=303.25ms p(95)=303.57ms + iteration_duration...: avg=290.05ms min=1.96ms med=304.86ms max=50.61s p(90)=306.36ms p(95)=306.89ms + iterations...........: 2975 58.770842/s + vus..................: 1 min=1 max=33 + vus_max..............: 33 min=25 max=33 +``` + +{{< /code >}} + +### Comparison + +Let's take a closer look at the results for the requests on each scenario. We can observe that in the `base` scenario requests duration has an percentile 95 of `2.09ms` and `100%` of requests pass the check. The `faults` scenario has a percentile 96 of `303.57ms` and only `89%` of requests pass the check (or put in another way, `11%` of requests failed), closely matching the fault definition. + +| Execution | P95 Req. Duration | Passed Checks | +| --------------- | ----------------- | ------------- | +| Baseline | 2.09ms | 100% | +| Fault injection | 303.57ms | 89% | + +## Source Code + +{{< collapse title="grpc-faults.js" >}} + +```javascript +import { ServiceDisruptor } from 'k6/x/disruptor'; +import grpc from 'k6/net/grpc'; +import { check } from 'k6'; + +const client = new grpc.Client(); +client.load(['pb'], 'hello.proto'); + +export default function () { + client.connect(__ENV.GRPC_HOST, { + plaintext: true, + timeout: '1s', + }); + + const data = { greeting: 'Bert' }; + const response = client.invoke('hello.HelloService/SayHello', data, { + timeout: '1s', + }); + client.close(); + + check(response, { + 'status is OK': (r) => r && r.status === grpc.StatusOK, + }); +} + +export function disrupt() { + if (__ENV.SKIP_FAULTS == '1') { + return; + } + + const disruptor = new ServiceDisruptor('grpcbin', 'grpcbin'); + + // inject errors in requests + const fault = { + averageDelay: '300ms', + statusCode: grpc.StatusInternal, + errorRate: 0.1, + port: 9000, + }; + disruptor.injectGrpcFaults(fault, '30s'); +} + +export const options = { + scenarios: { + load: { + executor: 'constant-arrival-rate', + rate: 100, + preAllocatedVUs: 10, + maxVUs: 100, + exec: 'default', + startTime: '0s', + duration: '30s', + }, + disrupt: { + executor: 'shared-iterations', + iterations: 1, + vus: 1, + exec: 'disrupt', + startTime: '0s', + }, + }, +}; +``` + +{{< /collapse >}} + +{{< collapse title="pb/hello.proto" >}} + +``` +syntax = "proto2"; + +package hello; + +service HelloService { + rpc SayHello(HelloRequest) returns (HelloResponse); + rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse); + rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse); + rpc BidiHello(stream HelloRequest) returns (stream HelloResponse); +} + +message HelloRequest { + optional string greeting = 1; +} + +message HelloResponse { + required string reply = 1; +} +``` + +{{< /collapse >}} + +## Test setup + +The tests requires the deployment of the `grpcbin` application. The application must also be accessible using an external IP available in the `GRPC_HOST` environment variable. + +The following [manifests](#manifests) define the resources required for deploying the application and exposing it as a LoadBalancer service. + +You can deploy the application using the following commands: + +```bash +# Create Namespace +kubectl apply -f namespace.yaml +namespace/grpcbin created + +# Deploy Pod +kubectl apply -f pod.yaml +pod/grpcbin created + +# Expose Pod as a Service +kubectl apply -f service.yaml +service/grpcbin created +``` + +You must set the environment variable `GRPC_HOST` with the external IP address and port used to access the `grpcbin` service from the test script. + +Learn more about how to get the external IP address in the [Expose your application](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/expose--your-application). + +### Manifests + +{{< collapse title="namespace.yaml" >}} + +```yaml +apiVersion: 'v1' +kind: Namespace +metadata: + name: grpcbin +``` + +{{< /collapse >}} + +{{< collapse title="pod.yaml" >}} + +```yaml +kind: 'Pod' +apiVersion: 'v1' +metadata: + name: grpcbin + namespace: grpcbin + labels: + app: grpcbin +spec: + containers: + - name: grpcbin + image: moul/grpcbin + ports: + - name: http + containerPort: 9000 +``` + +{{< /collapse >}} + +{{< collapse title="service.yaml" >}} + +```yaml +apiVersion: 'v1' +kind: 'Service' +metadata: + name: grpcbin + namespace: grpcbin +spec: + selector: + app: grpcbin + type: 'NodePort' + ports: + - port: 9000 + targetPort: 9000 +``` + +{{< /collapse >}} diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/inject-http-faults-into-pod.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/inject-http-faults-into-pod.md new file mode 100644 index 000000000..11741a9b5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/examples/inject-http-faults-into-pod.md @@ -0,0 +1,423 @@ +--- +title: 'Inject HTTP faults into Pod' +description: 'This example shows how to test the effect of faults injected in the HTTP requests served by a pod.' +weight: 02 +--- + +# Inject HTTP faults into Pod + +This example shows how [PodDisruptor](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor) can be used for testing the effect of faults injected in the HTTP requests served by a pod. + +You will find the complete [source code](#source-code) at the end of this document. Next sections examine the code in detail. + +The example uses [httpbin](https://httpbin.org), a simple request/response application that offers endpoints for testing different HTTP requests. + +The test requires `httpbin` to be deployed in a cluster in the namespace `httpbin` and exposed with an external IP. the IP address is expected in the environment variable `SVC_IP`. + +You will find the Kubernetes manifests and the instructions of how to deploy it to a cluster in the [test setup](#test-setup) section at the end of this document. You can learn more about how to get the external IP address in the [expose your application](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/expose--your-application) section. + +## Initialization + +The initialization code imports the external dependencies required by the test. The `PodDisruptor` class imported from the `xk6-disruptor` extension provides functions for injecting faults in pods. The [k6/http](https://grafana.com/docs/k6//javascript-api/k6-http) module provides functions for executing HTTP requests. + +```javascript +import { PodDisruptor } from 'k6/x/disruptor'; +import http from 'k6/http'; +``` + +## Test Load + +The test load is generated by the `default` function, which executes a request to the `httpbin` pod using the IP obtained from the environment variable `SVC_IP`. The test makes requests to the endpoint `delay/0.1` which will return after `0.1` seconds (`100ms`). + + + +```javascript +export default function (data) { + http.get(`http://${data.SVC_IP}/delay/0.1`); +} +``` + + + +{{< admonition type="note" >}} + +The test uses the `delay` endpoint which return after the requested delay. It requests a `0.1s` (`100ms`) delay to ensure the baseline scenario (see scenarios below) has meaningful statistics for the request duration. If we were simply calling a locally deployed http server (for example `nginx`), the response time would exhibit a large variation between a few microseconds to a few milliseconds. Having `100ms` as baseline response time has proved to offer more consistent results. + +{{< /admonition >}} + +## Fault injection + +The `disrupt` function creates a `PodDisruptor` using a selector that matches pods in the namespace `httpbin` with the label `app: httpbin`. + +The http faults are injected by calling the [PodDisruptor.injectHTTPFaults](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/injecthttpfaults) method using a fault definition that introduces a delay of `50ms` on each request and an error code `500` in `10%` of the requests. + + + +```javascript +export function disrupt(data) { + if (__ENV.SKIP_FAULTS == '1') { + return; + } + const selector = { + namespace: namespace, + select: { + labels: { + app: 'httpbin', + }, + }, + }; + const podDisruptor = new PodDisruptor(selector); + // delay traffic from one random replica of the deployment + const fault = { + averageDelay: '50ms', + errorCode: 500, + errorRate: 0.1, + }; + podDisruptor.injectHTTPFaults(fault, '30s'); +} +``` + + + +Notice the following code snippet in the `injectFaults` function above: + +```javascript +export function disrupt(data) { + if (__ENV.SKIP_FAULTS == '1') { + return; + } + //... +} +``` + +This code makes the function return without injecting faults if the `SKIP_FAULTS` environment variable is passed to the execution of the test with a value of "1". We will use this option to obtain a [baseline execution](#baseline-execution) without faults. + +## Scenarios + +This test defines two [scenarios](https://grafana.com/docs/k6//using-k6/scenarios) to be executed. The `load` scenario applies the test load to the `httpbin` application for `30s` invoking the `default` function. The `disrupt` scenario invokes the `disrupt` function to inject a fault in the HTTP requests of the target application. + +```javascript +export const options = { + scenarios: { + load: { + executor: 'constant-arrival-rate', + rate: 100, + preAllocatedVUs: 10, + maxVUs: 100, + exec: 'default', + startTime: '0s', + duration: '30s', + }, + disrupt: { + executor: 'shared-iterations', + iterations: 1, + vus: 1, + exec: 'disrupt', + startTime: '0s', + }, + }, +}; +``` + +{{< admonition type="note" >}} + +Notice that the `disrupt` scenario uses a `shared-iterations` executor with one iteration and one `VU`. This setting ensures the `disrupt` function is executed only once. Executing this function multiples times concurrently may have unpredictable results. + +{{< /admonition >}} + +## Executions + +{{< admonition type="note" >}} + +The commands in this section assume the `xk6-disruptor` binary is available in your current directory. This location can change depending on the installation process and the platform. Refer to the [installation section](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/installation) for details on how to install it in your environment. + +{{< /admonition >}} + +### Baseline execution + +We will first execute the test without introducing faults to have an baseline using the following command: + +{{< code >}} + +```bash +xk6-disruptor run --env SKIP_FAULTS=1 --env SVC_IP=$SVC_IP disrupt-pod.js +``` + +```windows-powershell +xk6-disruptor run --env SKIP_FAULTS=1 --env "SVC_IP=$Env:SVC_IP" disrupt-pod.js +``` + +{{< /code >}} + +Notice the argument `--env SKIP_FAULT=1`, which makes the `disrupt` function return without injecting any fault as explained in the [fault injection](#fault-injection) section. Also notice the `--env SVC_IP` argument, which passes the external IP used to access the `httpbin` application. + +You should get an output similar to the one shown below (click `Expand` button to see all output). + +{{< code >}} + +``` + /\ |‾‾| /‾‾/ /‾‾/ + /\ / \ | |/ / / / + / \/ \ | ( / ‾‾\ + / \ | |\ \ | (‾) | + / __________ \ |__| \__\ \_____/ .io + + execution: local + script: test.js + output: - + + scenarios: (100.00%) 2 scenarios, 101 max VUs, 10m30s max duration (incl. graceful stop): + * disrupt: 1 iterations shared among 1 VUs (maxDuration: 10m0s, exec: disrupt, gracefulStop: 30s) + * load: 100.00 iterations/s for 30s (maxVUs: 10-100, exec: default, gracefulStop: 30s) + + +running (00m30.1s), 000/014 VUs, 2998 complete and 0 interrupted iterations +disrupt ✓ [======================================] 1 VUs 00m00.0s/10m0s 1/1 shared iters +load ✓ [======================================] 000/013 VUs 30s 100.00 iters/s + + data_received..................: 1.4 MB 46 kB/s + data_sent......................: 267 kB 8.9 kB/s + dropped_iterations.............: 4 0.132766/s + http_req_blocked...............: avg=8.08µs min=2.36µs med=5.8µs max=543.79µs p(90)=8.68µs p(95)=10.5µs + http_req_connecting............: avg=1.25µs min=0s med=0s max=418.63µs p(90)=0s p(95)=0s + http_req_duration..............: avg=103.22ms min=101.65ms med=103.13ms max=121.7ms p(90)=104.01ms p(95)=104.4ms + { expected_response:true }...: avg=103.22ms min=101.65ms med=103.13ms max=121.7ms p(90)=104.01ms p(95)=104.4ms + http_req_failed................: 0.00% ✓ 0 ✗ 2997 + http_req_receiving.............: avg=133.5µs min=45.06µs med=131.23µs max=879.43µs p(90)=193.48µs p(95)=223.04µs + http_req_sending...............: avg=31.14µs min=11.46µs med=29.37µs max=171.68µs p(90)=40.48µs p(95)=47.53µs + http_req_tls_handshaking.......: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s + http_req_waiting...............: avg=103.05ms min=101.54ms med=102.98ms max=121.56ms p(90)=103.82ms p(95)=104.2ms + http_reqs......................: 2997 99.474844/s + iteration_duration.............: avg=103.34ms min=109.86µs med=103.28ms max=121.92ms p(90)=104.19ms p(95)=104.63ms + iterations.....................: 2998 99.508035/s + vus............................: 13 min=11 max=13 + vus_max........................: 14 min=12 max=14 +``` + +{{< /code >}} + +### Fault injection + +We repeat the execution injecting the faults. Notice we have removed the `--env SKIP_FAULTS=1` argument. + +{{< code >}} + +```bash +xk6-disruptor run --env SVC_IP=$SVC_IP disrupt-pod.js +``` + +```windows-powershell +xk6-disruptor run --env "SVC_IP=$Env:SVC_IP" disrupt-pod.js +``` + +{{< /code >}} + +{{< code >}} + +``` + + /\ |‾‾| /‾‾/ /‾‾/ + /\ / \ | |/ / / / + / \/ \ | ( / ‾‾\ + / \ | |\ \ | (‾) | + / __________ \ |__| \__\ \_____/ .io + + execution: local + script: disrupt-pod.js + output: - + + scenarios: (100.00%) 2 scenarios, 101 max VUs, 10m30s max duration (incl. graceful stop): + * disrupt: 1 iterations shared among 1 VUs (maxDuration: 10m0s, exec: disrupt, gracefulStop: 30s) + * load: 100.00 iterations/s for 30s (maxVUs: 10-100, exec: default, gracefulStop: 30s) + + +running (00m31.1s), 000/018 VUs, 2995 complete and 0 interrupted iterations +disrupt ✓ [======================================] 1 VUs 00m31.1s/10m0s 1/1 shared iters +load ✓ [======================================] 000/017 VUs 30s 100.00 iters/s + + data_received..................: 1.1 MB 34 kB/s + data_sent......................: 267 kB 8.6 kB/s + dropped_iterations.............: 7 0.224798/s + http_req_blocked...............: avg=9.81µs min=2.59µs med=5.93µs max=489.67µs p(90)=7.88µs p(95)=9.5µs + http_req_connecting............: avg=2.48µs min=0s med=0s max=367.63µs p(90)=0s p(95)=0s + http_req_duration..............: avg=142.15ms min=50.33ms med=153.79ms max=165.85ms p(90)=154.8ms p(95)=155.12ms + { expected_response:true }...: avg=151.9ms min=101.81ms med=153.9ms max=165.85ms p(90)=154.86ms p(95)=155.17ms + http_req_failed................: 9.65% ✓ 289 ✗ 2705 + http_req_receiving.............: avg=80.92µs min=28.32µs med=77.33µs max=352.19µs p(90)=105.09µs p(95)=123.68µs + http_req_sending...............: avg=30.43µs min=11.27µs med=29.37µs max=287.71µs p(90)=37.42µs p(95)=41.84µs + http_req_tls_handshaking.......: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s + http_req_waiting...............: avg=142.04ms min=50.25ms med=153.68ms max=165.76ms p(90)=154.69ms p(95)=155.01ms + http_reqs......................: 2994 96.149356/s + iteration_duration.............: avg=152.64ms min=50.43ms med=153.93ms max=31.12s p(90)=154.97ms p(95)=155.29ms + iterations.....................: 2995 96.18147/s + vus............................: 1 min=1 max=18 + vus_max........................: 18 min=12 max=18 + +``` + +{{< /code >}} + +### Comparison + +Let's take a closer look at the results for the requests on each scenario. We can observe that he `base` scenario has an average of `103ms` and an error rate of `0%` while the `faults` scenario has a median around `151.9ms` and an error rate of nearly `10%`, matching the definition of the faults defined in the disruptor. + +| Execution | Avg. Response | Failed requests | +| --------------- | ------------- | --------------- | +| Baseline | 103.22ms | 0.00% | +| Fault injection | 151.9 ms | 9.65% | + +{{< admonition type="note" >}} + +Notice we have used the average response time reported as `expected_response:true` because this metric only consider successful requests while `http_req_duration` considers all requests, including those returning a fault. + +{{< /admonition >}} + +## Source Code + +{{< collapse title="disrupt-pod.js" >}} + +```javascript +import { PodDisruptor } from 'k6/x/disruptor'; +import http from 'k6/http'; + +export default function (data) { + http.get(`http://${__ENV.SVC_IP}/delay/0.1`); +} + +export function disrupt(data) { + if (__ENV.SKIP_FAULTS == '1') { + return; + } + + const selector = { + namespace: 'httpbin', + select: { + labels: { + app: 'httpbin', + }, + }, + }; + const podDisruptor = new PodDisruptor(selector); + + // delay traffic from one random replica of the deployment + const fault = { + averageDelay: '50ms', + errorCode: 500, + errorRate: 0.1, + }; + podDisruptor.injectHTTPFaults(fault, '30s'); +} + +export const options = { + scenarios: { + load: { + executor: 'constant-arrival-rate', + rate: 100, + preAllocatedVUs: 10, + maxVUs: 100, + exec: 'default', + startTime: '0s', + duration: '30s', + }, + disrupt: { + executor: 'shared-iterations', + iterations: 1, + vus: 1, + exec: 'disrupt', + startTime: '0s', + }, + }, +}; +``` + +{{< /collapse >}} + +## Test setup + +The tests requires the deployment of the `httpbin` application. The application must also be accessible using an external IP available in the `SVC_IP` environment variable. + +The [manifests](#manifests) below define the resources required for deploying the application and exposing it as a LoadBalancer service. + +You can deploy the application using the following commands: + +```bash +# Create Namespace +kubectl apply -f namespace.yaml +namespace/httpbin created + +# Deploy Pod +kubectl apply -f pod.yaml +pod/httpbin created + +# Expose Pod as a Service +kubectl apply -f service.yaml +service/httpbin created +``` + +You can retrieve the resources using the following command: + +```bash +kubectl -n httpbin get all +NAME READY STATUS RESTARTS AGE +pod/httpbin 1/1 Running 0 1m + +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE +service/httpbin LoadBalancer 10.96.169.78 172.18.255.200 80:31224/TCP 1m +``` + +You must set the environment variable `SVC_IP` with the external IP address and port used to access the `httpbin` service from the test script. + +You can learn more about how to get the external IP address in the [expose your application](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/expose--your-application) section. + +### Manifests + +{{< collapse title="namespace.yaml" >}} + +```yaml +apiVersion: 'v1' +kind: Namespace +metadata: + name: httpbin +``` + +{{< /collapse >}} + +{{< collapse title="pod.yaml" >}} + +```yaml +kind: 'Pod' +apiVersion: 'v1' +metadata: + name: httpbin + namespace: httpbin + labels: + app: httpbin +spec: + containers: + - name: httpbin + image: kennethreitz/httpbin + ports: + - name: http + containerPort: 80 +``` + +{{< /collapse >}} + +{{< collapse title="service.yaml" >}} + +```yaml +apiVersion: 'v1' +kind: 'Service' +metadata: + name: httpbin + namespace: httpbin +spec: + selector: + app: httpbin + type: 'LoadBalancer' + ports: + - port: 80 + targetPort: 80 +``` + +{{< /collapse >}} diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/expose-your-application.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/expose-your-application.md new file mode 100644 index 000000000..9cddf9b63 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/expose-your-application.md @@ -0,0 +1,107 @@ +--- +title: 'Expose your application' +description: 'How to make your applications accessible from the test scripts.' +weight: 04 +aliases: + - ./expose--your-application/ +--- + +# Expose your application + +To access your application from the test scripts, you must assign it an external IP in the cluster where it's running. +How you do this depends on the platform you use to deploy the application. +The following sections explain the different approaches. + +## Using port-forwarding + +`kubectl`'s `port-forward` command allows accessing applications running in a Kubernetes cluster using a local port. + +For example, the following command will make `my-service`'s port `80` accessible as `localhost:8080`: + +```bash +kubectl port-forward svc/my-service 8080:80 +Forwarding from 127.0.0.1:8080 -> 80 +``` + +### Limitations using port forwarding + +To be able to inject faults, `xk6-disruptor` must [install an agent on each target](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/how--it-works) that intercepts the requests and applies the desired disruptions. This process requires any existing connection to the targets to be redirected to the agent. + +Due to an existing bug in `kubectl`, the process of installing the disruptor can potentially [break the port forwarding](https://github.com/grafana/xk6-disruptor/issues/254). Notice that this issue happens only if the faults are injected in the service that is exposed using port forward. If the faults are injected in another service not exposed by port-forwarding, there shouldn't be any issue. + +Until this issue is solved in `kubectl`, tests using port forwarding to access a service should ensure the agent is installed in the targets before any traffic is sent by the test. + +The simplest way to accomplish this is to ensure the scenario that executes the load (#2) starts after the scenario that injects the faults (#1): + +```js + scenarios: { + disrupt: { // #1 inject faults + executor: 'shared-iterations', + iterations: 1, + vus: 1, + exec: "disrupt", + startTime: "0s", + }, + load: { // #2 execute load + executor: 'constant-arrival-rate', + rate: 100, + preAllocatedVUs: 10, + maxVUs: 100, + exec: "default", + startTime: '20s', // give time for the agents to be installed + duration: "30s", + } + } +``` + +## As a LoadBalancer service + +A service of type [`LoadBalancer`](https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/) receives an external IP from an external load-balancer provider. +How you configure the load balancer depends on the your cluster's deployment platform and configuration. +The following sections provide guidelines to expose your application when running in common development environments. +If your cluster is deployed in a public cloud, check your cloud provider documentation. + +If the service that you want your tests to access is not defined as a load balancer, you can change the service type with the following command. The service will then receive an external IP. + +{{< code >}} + +```bash +kubectl -n patch svc -p '{"spec": {"type": "LoadBalancer"}}' +``` + +```windows-powershell +kubectl -n patch svc -p '{\"spec\": {\"type\": \"LoadBalancer\"}}' +``` + +{{< /code >}} + +You can retrieve the external IP address and store it in an environment variable (`SVC_IP` in this example) using the following command: + +{{< code >}} + +```bash +SVC_IP=$(kubectl -n get svc --output jsonpath='{.status.loadBalancer.ingress[0].ip}') +``` + +```windows-powershell +$Env:SVC_IP=$(kubectl -n get svc --output jsonpath='{.status.loadBalancer.ingress[0].ip}') +``` + +{{< /code >}} + +### Configuring a LoadBalancer in Kind + +[Kind](https://kind.sigs.k8s.io/) is a tool to run local Kubernetes clusters using a Docker container to emulate nodes. +You can use kind for local development or CI. +Services deployed in a kind cluster can be exposed to be accessed from the host machine [using metallb as a load balancer](https://kind.sigs.k8s.io/docs/user/loadbalancer). + +### Configuring a LoadBalancer in Minikube + +[Minikube](https://github.com/kubernetes/minikube) implements a local Kubernetes cluster that supports different technologies to virtualize the cluster's infrastructure, such as containers, VMs or bare metal. + +Minikube's tunnel command runs as a process, creating a network route on the host to the service CIDR of the cluster. +It uses the cluster’s IP address as a gateway. The tunnel command exposes the external IP directly to any program that is running on the host operating system. + +```bash +minikube tunnel +``` diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/first-steps.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/first-steps.md new file mode 100644 index 000000000..366288245 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/first-steps.md @@ -0,0 +1,42 @@ +--- +title: 'xk6-disruptor first steps' +description: 'xk6-disruptor is a k6 extension providing fault injection capabilities to k6.' +weight: 01 +--- + +# xk6-disruptor first steps + +[xk6-disruptor](https://github.com/grafana/xk6-disruptor) is an extension that adds fault injection capabilities to k6. + +It provides a Javascript [API](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/) to inject [faults](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults) such as errors and delays into HTTP and gRPC requests served by selected Kubernetes [Pods](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor) or [Services](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor). + +```javascript +import { ServiceDisruptor } from 'k6/x/disruptor'; + +export default function () { + // Create a new disruptor that targets a service + const disruptor = new ServiceDisruptor('app-service', 'app-namespace'); + + // Disrupt the targets by injecting delays and faults into HTTP request for 30 seconds + const fault = { + averageDelay: '500ms', + errorRate: 0.1, + errorCode: 500, + }; + disruptor.injectHTTPFaults(fault, '30s'); +} +``` + +## Next steps + +Explore the fault injection [API](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/) + +See [step-by-step examples](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/examples). + +Visit the [interactive demo environment](https://killercoda.com/grafana-xk6-disruptor/scenario/killercoda). + +Learn the basics of using the disruptor in your test project: + +- [Requirements](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/requirements) + +- [Installation](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/installation) diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/how-it-works.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/how-it-works.md new file mode 100644 index 000000000..d5414ac12 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/how-it-works.md @@ -0,0 +1,38 @@ +--- +title: 'How xk6-disruptor works' +description: 'A brief description of the components of the xk6-disruptor and how they work when inject faults in a target system.' +weight: 05 +aliases: + - ./how--it-works/ +--- + +# How xk6-disruptor works + +xk6-disruptor consists of two main components: + +- **The xk6-disruptor extension** provides a Javascript API for injecting faults into a target system using the xk6-disruptor-agent as a backend. This API is built around a collection of **disruptors**. Each disruptor targets a type of component in the system (for example Pods or cluster Nodes). +- **The xk6-disruptor-agent** injects faults into the target system where it runs. It is provided as an Docker image that you can pull from the [xk6-disruptor repository](https://github.com/grafana/xk6-disruptor/pkgs/container/xk6-disruptor-agent). + +The xk6-disruptor extension installs the agent in the target and sends commands to inject the desired faults. How this happens depends on the type of disruptor, as described in the following sections. + +## PodDisruptor + +The following diagram shows how PodDisruptor works: + +1. The PodDisruptor selects the target pods based on the selector attributes defined in the [constructor](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/constructor) +2. The PodDisruptor attaches the xk6-disruptor-agent to each of the target pods +3. When a fault is injected (e.g. calling the [injectHTTTFault](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/injecthttpfaults)) the PodDisruptor sends a command to the agents to inject the fault in their respective pods + +![How PodDisruptor works](/media/docs/k6-oss/xk6-disruptor-how-pod-disruptor-works.png) + +## ServiceDisruptor + +The ServiceDisruptor works as a wrapper around a PodDisruptor, which targets the pods that back the service. + +1. The ServiceDisruptor uses the definition of the service specified in the [constructor](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/constructor) to create a pod selector that matches the pods that back the service. +2. The ServiceDisruptor creates a PodDisruptor using this pod selector. +3. The PodDisruptor installs the agent in the target pods. + +From this point, the PodDisruptor works as described before. + +![How ServiceDisruptor works](/media/docs/k6-oss/xk6-disruptor-how-service-disruptor-works.png) diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/installation.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/installation.md new file mode 100644 index 000000000..3c86e105f --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/installation.md @@ -0,0 +1,44 @@ +--- +title: 'Installation' +description: 'A step-by-step guide on how to install xk6-disruptor.' +weight: 03 +--- + +# Installation + +xk6-disruptor is a [k6 extension](https://grafana.com/docs/k6//extensions). You have to run a k6 version built with the disruptor extension to use the [disruptor APIs](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/) in your k6 tests. + +The following sections explain the different options to get this custom binary. + +## Download a release binary + +The quickest way to get started is to [download a release binary from GitHub](https://github.com/grafana/xk6-disruptor/releases). + +## Build from source + +You can also use [xk6](https://github.com/grafana/xk6) to build a k6 binary. + +To learn more about xk6, refer to [Build a k6 binary using Go](https://grafana.com/docs/k6//extensions/build-k6-binary-using-go). + +To build the k6 binary with the xk6-disruptor extension: + +1. Ensure you have [Go 1.19](https://golang.org/doc/install) and [Git](https://git-scm.com/) installed. +2. Run the following commands in a terminal: + +{{< code >}} + +```bash +# Install xk6 +go install go.k6.io/xk6/cmd/xk6@latest + +# Clone the xk6-disruptor code +git clone https://github.com/grafana/xk6-disruptor.git +cd xk6-disruptor + +# Build the custom binary +xk6 build --output xk6-disruptor --with github.com/grafana/xk6-disruptor=. +``` + +{{< /code >}} + +xk6 will create the `xk6-disruptor` binary in the current working directory. diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/requirements.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/requirements.md new file mode 100644 index 000000000..56e82bd76 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/requirements.md @@ -0,0 +1,29 @@ +--- +title: 'Requirements' +description: 'Requirements for using xk6-disruptor in your test scripts' +weight: 02 +--- + +# Requirements + +The xk6-disruptor is a k6 extension. +To use it in a k6 test script, you need to bundle a k6 extension that includes the disruptor. +Refer to the [Installation](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/installation) section for instructions on how to get this custom build. + +xk6-disruptor needs to interact with the Kubernetes cluster on which the application under test is running. +To do so, you must have the credentials to access the cluster in a [kubeconfig](https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/) file. +Ensure that this file is pointed to by the `KUBECONFIG` environment variable or that it is located at the default location, `$HOME/.kube/config`. + +{{< admonition type="note" >}} + +xk6-disruptor requires Kubernetes version 1.23 or higher + +{{< /admonition >}} + +`xk6-disruptor` requires the [grafana/xk6-disruptor-agent](https://github.com/grafana/xk6-disruptor/pkgs/container/xk6-disruptor-agent) image for injecting the [disruptor agent](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/how--it-works) into the disruption targets. Kubernetes clusters can be configured to restrict the download of images from public repositories. You need to ensure this image is available in the cluster where the application under test is running. Additionally, the xk6-disruptor-agent must run with network access privileges. Kubernetes clusters [can be configured to restrict the privileges of containers](https://kubernetes.io/docs/concepts/security/pod-security-admission/). +If you find an error similar to the following when using the xk6-disruptor, contact your cluster administrator and request the necessary privileges. + +> ERROR\[0000\] error creating PodDisruptor: pods "nginx" is forbidden: violates PodSecurity "baseline:latest": non-default capabilities (container "xk6-agent" must not include "NET_ADMIN", "NET_RAW" in securityContext.capabilities.add) + +You also need to ensure your test application is accessible from the machine where the tests run. +Refer to [Expose your application](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/expose--your-application) section for instructions on how to make your application available from outside the Kubernetes cluster.. diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/_index.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/_index.md new file mode 100644 index 000000000..8d85cd115 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/_index.md @@ -0,0 +1,16 @@ +--- +aliases: + - ../../javascript-api/xk6-disruptor # docs/k6//javascript-api/xk6-disruptor +title: xk6-disruptor API +description: 'An overview of the API for xk6-disruptor.' +weight: 06 +--- + +# xk6-disruptor API + +The xk6-disruptor API is organized around _disruptors_ that affect specific targets such as pods or services. These disruptors can inject different types of [faults](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults) on their targets. + +| Class | Description | +| ---------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------- | +| [PodDisruptor](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor) | Targets the Pods that match selection attributes such as labels. | +| [ServiceDisruptor](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor) | Targets the Pods that back a Kubernetes Service | diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/_index.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/_index.md new file mode 100644 index 000000000..3832f7bb1 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/_index.md @@ -0,0 +1,17 @@ +--- +aliases: + - ../../../javascript-api/xk6-disruptor/faults # docs/k6//javascript-api/xk6-disruptor/faults +title: 'Faults' +description: 'xk6-disruptor: Fault Description' +weight: 100 +--- + +# Faults + +A fault is as an abnormal condition that affects a system component and which may lead to a failure. + +| Fault type | Description | +| --------------------------------------------------------------------------------------------------------------------- | ------------------------------------------- | +| [gRPC Fault](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/grpc) | Fault affecting gRPC requests from a target | +| [HTTP Fault](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/http) | Fault affecting HTTP requests from a target | +| [Pod Termination Fault](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/pod-termination) | Fault terminating a number of target Pods | diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/grpc.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/grpc.md new file mode 100644 index 000000000..501645ad0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/grpc.md @@ -0,0 +1,41 @@ +--- +aliases: + - ../../../../javascript-api/xk6-disruptor/faults/grpc # docs/k6//javascript-api/xk6-disruptor/faults/grpc +title: 'gRPC' +description: 'xk6-disruptor: gRPC Fault attributes' +weight: 01 +--- + +# gRPC + +A gRPC Fault describes the characteristics of the faults to be injected in the gRPC requests served by a target. + +A gRPC fault is described by the following attributes: + +| Attribute | Type | Description | +| -------------- | ------ | -------------------------------------------------------------------------------------------------------------- | +| averageDelay | string | average delay added to requests represented as a string (default `0`) | +| delayVariation | string | variation in the injected delay (default `0`) | +| statusMessage | string | message to be returned when an error is injected | +| statusCode | number | status to be returned when an error is injected | +| errorRate | number | rate of requests that will return an error, represented as a float in the range `0.0` to `1.0` (default `0.0`) | +| exclude | string | comma-separated list of services to be excluded from disruption | +| port | number | port on which the requests will be intercepted | + +{{< admonition type="note" >}} + +`averageDelay` and `delayVariation` are applied to all requests affected by the fault, regardless of the value of `errorRate`. `statusCode` is returned only to a fraction of requests defined by `errorRate`. + +{{< /admonition >}} + +## Example + +This example defines a gRPC fault that introduces a delay of `50ms` in all requests and returns a status code `13` in `10%` of the requests. + +```javascript +const fault = { + averageDelay: '50ms', + statusCode: 10, + errorRate: 0.1, +}; +``` diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/http.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/http.md new file mode 100644 index 000000000..43cc8573b --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/http.md @@ -0,0 +1,41 @@ +--- +aliases: + - ../../../../javascript-api/xk6-disruptor/faults/http # docs/k6//javascript-api/xk6-disruptor/faults/http +title: 'HTTP' +description: 'xk6-disruptor: HTTP Fault attributes' +weight: 02 +--- + +# HTTP + +A HTTP Fault describes the characteristics of the faults to be injected in the HTTP requests served by a target. + +A HTTP fault is described by the following attributes: + +| Attribute | Type | Description | +| -------------- | ------ | -------------------------------------------------------------------------------------------------------------- | +| averageDelay | string | average delay added to requests represented as a string (default `0`) | +| delayVariation | string | variation in the injected delay (default `0`) | +| errorBody | string | body to be returned when an error is injected | +| errorCode | number | error code to return | +| errorRate | number | rate of requests that will return an error, represented as a float in the range `0.0` to `1.0` (default `0.0`) | +| exclude | string | comma-separated list of urls to be excluded from disruption (e.g. /health) | +| port | number | port on which the requests will be intercepted | + +{{< admonition type="note" >}} + +`averageDelay` and `delayVariation` are applied to all requests affected by the fault, regardless of the value of `errorRate`. `errorCode` is returned only to a fraction of requests defined by `errorRate`. + +{{< /admonition >}} + +## Example + +This example defines a HTTP fault that introduces a delay of `50ms` in all requests and returns an error code `500` in `10%` of the requests. + +```javascript +const fault = { + averageDelay: '50ms', + errorCde: 500, + errorRate: 0.1, +}; +``` diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/pod-termination.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/pod-termination.md new file mode 100644 index 000000000..f9772aa90 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/pod-termination.md @@ -0,0 +1,35 @@ +--- +aliases: + - ../../../../javascript-api/xk6-disruptor/faults/pod-termination # docs/k6//javascript-api/xk6-disruptor/faults/pod-termination +title: 'Pod Termination' +description: 'xk6-disruptor: Pod Termination Fault attributes' +weight: 03 +--- + +# Pod Termination + +A Pod Termination Fault allows terminating either a fixed number or a percentage of the pods that matching a selector or back a service. + +A Pod Termination fault is defined by the following attributes: + +| Attribute | Type | Description | +| --------- | --------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| count | integer or percentage | the number of pods to be terminated. It can be specified as a integer number or as a percentage, for example `30%`, that defines the fraction of target pods to be terminated | + +{{< admonition type="note" >}} + +If the count is a percentage and there are no enough elements in the target pod list, the number is rounded up. +For example '25%' of a list of 2 target pods will terminate one pod. +If the list of target pods is not empty, at least one pod is always terminated. + +{{< /admonition >}} + +## Example + +This example defines a PorTermination fault that will terminate `30%` of target pods + +```javascript +const fault = { + count: '30%', +}; +``` diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/_index.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/_index.md new file mode 100644 index 000000000..b29a080c5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/_index.md @@ -0,0 +1,65 @@ +--- +aliases: + - ../../../javascript-api/xk6-disruptor/poddisruptor # docs/k6//javascript-api/xk6-disruptor/poddisruptor +title: 'PodDisruptor' +description: 'xk6-disruptor: PodDisruptor class' +weight: 200 +--- + +# PodDisruptor + +The `PodDisruptor` class can inject different types of faults into the pods that match a selection criteria. + +To construct a `PodDisruptor`, use the [PodDisruptor() constructor](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/constructor). + +## Methods + +| Method | Description | +| -------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | +| [PodDisruptor.injectGrpcFaults()](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/injectgrpcfaults) | Inject [gRPC faults](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/grpc) in the target Pods | +| [PodDisruptor.injectHTTPFaults()](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/injecthttpfaults) | Inject [HTTP faults](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/http) in the target Pods | +| PodDisruptor.targets() | Returns the list of target Pods of the PodDisruptor | +| [PodDisruptor.terminatePods()](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/terminate-pods) | executes a [Pod Termination fault](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/pod-termination) in the target Pods | + +## Example + +This example: + +- Creates a selector that matches all pods in the `default` namespace with the `run=nginx` label +- Injects a delay of 100ms and makes 10 percent of requests return an http response code `500`. + +```javascript +import { PodDisruptor } from 'k6/x/disruptor'; + +const selector = { + namespace: 'default', + select: { + labels: { + run: 'nginx', + }, + }, +}; + +const fault = { + averageDelay: '100ms', + errorRate: 0.1, + errorCode: 500, +}; + +export default function () { + const disruptor = new PodDisruptor(selector); + disruptor.injectHTTPFaults(fault, '30s'); +} +``` + +{{< admonition type="note" >}} + +You can test this script by first creating a pod running nginx with the command below, assuming you have [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl) installed in your environment: + +```bash +$ kubectl run nginx --image=nginx +``` + +You can also use the [xk6-kubernetes](https://github.com/grafana/xk6-kubernetes) extension for creating these resources from your test script. + +{{< /admonition >}} diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/constructor.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/constructor.md new file mode 100644 index 000000000..543c6035e --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/constructor.md @@ -0,0 +1,58 @@ +--- +aliases: + - ../../../../javascript-api/xk6-disruptor/poddisruptor/constructor # docs/k6//javascript-api/xk6-disruptor/poddisruptor/constructor +title: 'Constructor' +description: 'xk6-disruptor: PodDisruptor constructor' +weight: 100 +--- + +# Constructor + +The `PodDisruptor()` constructor creates a new instance of a [PodDisruptor](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor) class. + +| Parameter | Type | Description | +| ------------------ | ------ | ----------------------------------------------------------------- | +| selector | object | [criteria](#selector) for selecting the target pods | +| options (optional) | object | [options](#options) for controlling the behavior of the disruptor | + +### Selector + +The `selector` defines the criteria a pod must satisfy to be a valid target: + +| Attribute | Type | Description | +| --------- | ------ | ------------------------------------------------------------------------------------------- | +| namespace | string | namespace the selector will look for pods | +| select | object | [attributes](#pod-attributes) that a pod must match to be selected | +| exclude | object | [attributes](#pod-attributes) that exclude a pod (even if it matches the select attributes) | + +You can use the following attributes to select or exclude pods: + +### Pod attributes + +| Attribute | Type | Description | +| --------- | ------ | ------------------------------------------------------------ | +| labels | object | map with the labels to be matched for selection or exclusion | + +### Options + +The `options` control the creation and behavior of the `PodDisruptor`: + +| Attribute | Type | Description | +| ------------- | ------ | ----------------------------------------------------------------------------------- | +| injectTimeout | string | maximum time to wait for the disruptor to be ready in the target pods (default 30s) | + +## Example + + + +```javascript +const selector = { + namespace: 'my-namespace', + select: { + labels: { + app: 'my-app', + }, + }, +}; +const podDisruptor = new PodDisruptor(selector); +``` diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/injectgrpcfaults.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/injectgrpcfaults.md new file mode 100644 index 000000000..94c583e86 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/injectgrpcfaults.md @@ -0,0 +1,38 @@ +--- +aliases: + - ../../../../javascript-api/xk6-disruptor/poddisruptor/injectgrpcfaults # docs/k6//javascript-api/xk6-disruptor/poddisruptor/injectgrpcfaults +title: 'injectGrpcFaults()' +description: 'xk6-disruptor: PodDisruptor.injectGrpcFaults method' +weight: 200 +--- + +# injectGrpcFaults() + +injectGrpcFaults injects gRPC faults in the requests served by a target Pod. + +| Parameter | Type | Description | +| ------------------ | ------ | ---------------------------------------------------------------------------------------------------------------------------------- | +| fault | object | description of the [gRPC faults](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/grpc) to be injected | +| duration | string | duration of the disruption | +| options (optional) | object | [options](#options) that control the injection of the fault | + +## options + +The injection of the fault is controlled by the following options: + +| Option | Type | Description | +| --------- | ------ | ----------------------------------------------------------------------------------- | +| proxyPort | number | port the agent will use to listen for requests in the target pods ( default `8000`) | + +## Example + + + +```javascript +const fault = { + averageDelay: '50ms', + statusCode: 13, + errorRate: 0.1, +}; +disruptor.injectGrpcFaults(fault, '30s'); +``` diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/injecthttpfaults.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/injecthttpfaults.md new file mode 100644 index 000000000..b8ca9fba8 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/injecthttpfaults.md @@ -0,0 +1,48 @@ +--- +aliases: + - ../../../../javascript-api/xk6-disruptor/poddisruptor/injecthttpfaults # docs/k6//javascript-api/xk6-disruptor/poddisruptor/injecthttpfaults +title: 'injectHTTPFaults()' +description: 'xk6-disruptor: PodDisruptor.injectHTTPFaults method' +weight: 300 +--- + +# injectHTTPFaults() + +injectHTTPFaults injects HTTP faults in the requests served by a target Pod. + +| Parameter | Type | Description | +| ------------------ | ------ | ---------------------------------------------------------------------------------------------------------------------------------- | +| fault | object | description of the [http faults](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/http) to be injected | +| duration | string | duration of the disruption | +| options (optional) | object | [options](#options) that control the injection of the fault | + +## options + +The injection of the fault is controlled by the following options: + +| Option | Type | Description | +| --------- | ------ | ----------------------------------------------------------------------------------- | +| proxyPort | number | port the agent will use to listen for requests in the target pods ( default `8000`) | + +{{< admonition type="note" >}} + +When injecting faults you may find the following error message during the test execution: + +WARN\[0035\] Request Failed error="read tcp 172.18.0.1:43564->172.18.255.200:80: read: connection reset by peer" + +This is normal and means that one request was "in transit" at the time the faults were injected, causing the request to fail from a network connection reset. + +{{< /admonition >}} + +## Example + + + +```javascript +const fault = { + averageDelay: '50ms', + errorCode: 500, + errorRate: 0.1, +}; +disruptor.injectHTTPFaults(fault, '30s'); +``` diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/terminate-pods.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/terminate-pods.md new file mode 100644 index 000000000..f0751d31f --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/terminate-pods.md @@ -0,0 +1,26 @@ +--- +aliases: + - ../../../../javascript-api/xk6-disruptor/poddisruptor/terminate-pods # docs/k6//javascript-api/xk6-disruptor/poddisruptor/terminate-pods +title: 'terminatePods()' +description: 'xk6-disruptor: PodDisruptor.terminatePods method' +weight: 400 +--- + +# terminatePods() + +`terminatePods` terminates a number of the pods matching the selector configured in the PodDisruptor. + +| Parameter | Type | Description | +| --------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------- | +| fault | object | description of the [Pod Termination fault](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/pod-termination) | + +## Example + + + +```javascript +const fault = { + count: 2, +}; +disruptor.terminatePods(fault); +``` diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/_index.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/_index.md new file mode 100644 index 000000000..faf4c052b --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/_index.md @@ -0,0 +1,57 @@ +--- +aliases: + - ../../../javascript-api/xk6-disruptor/servicedisruptor # docs/k6//javascript-api/xk6-disruptor/servicedisruptor +title: 'ServiceDisruptor' +description: 'xk6-disruptor: ServiceDisruptor class' +weight: 300 +--- + +# ServiceDisruptor + +The `ServiceDisruptor` class can inject different types of faults into the pods that back a Kubernetes service. + +To construct a `ServiceDisruptor`, use the [ServiceDisruptor() constructor](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/constructor). + +## Methods + +| Method | Description | +| ---------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | +| [ServiceDisruptor.injectGrpcFaults()](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/injectgrpcfaults) | Inject [gRPC faults](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/grpc) in the target Pods | +| [ServiceDisruptor.injectHTTPFaults()](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/injecthttpfaults) | Inject [HTTTP faults](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/http) in the target Pods | +| ServiceDisruptor.targets() | Returns the list of target Pods of the ServiceDisruptor | +| [ServiceDisruptor.terminatePods()](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/terminate-pods) | executes a [Pod Termination fault](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/pod-termination) in the target Pods | + +## Example + +The following example: + +- Creates a disruptor for the `nginx` service +- Injects a delay of 100ms and a 10 percent of requests that return an http response code `500`. + +```javascript +import { ServiceDisruptor } from 'k6/x/disruptor'; + +const fault = { + averageDelay: '100ms', + errorRate: 0.1, + errorCode: 500, +}; + +export default function () { + const disruptor = new ServiceDisruptor('nginx', 'default'); + disruptor.injectHTTPFaults(fault, '30s'); +} +``` + +{{< admonition type="note" >}} + +You can test this script by creating first a pod running nginx and exposing it as a service with the commands below, assuming you have [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl) installed in your environment: + +```bash +> kubectl run nginx --image=nginx +> kubectl expose pod nginx --port 80 +``` + +You can also use the [xk6-kubernetes](https://github.com/grafana/xk6-kubernetes) extension for creating these resources from your test script. + +{{< /admonition >}} diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/constructor.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/constructor.md new file mode 100644 index 000000000..9ae546463 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/constructor.md @@ -0,0 +1,33 @@ +--- +aliases: + - ../../../../javascript-api/xk6-disruptor/servicedisruptor/constructor # docs/k6//javascript-api/xk6-disruptor/servicedisruptor/constructor +title: 'Constructor' +description: 'xk6-disruptor: ServiceDisruptor constructor' +weight: 100 +--- + +# Constructor + +The `ServiceDisruptor()` constructor creates a new instance of a [ServiceDisruptor](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor) class. + +| Parameter | Type | Description | +| ------------------ | ------ | ----------------------------------------------------------------- | +| service | string | name of the service | +| namespace | string | namespace on which the service is defined | +| options (optional) | object | [options](#options) for controlling the behavior of the disruptor | + +### Options + +The following options control the creation and behavior of the `ServiceDisruptor`: + +| Attribute | Type | Description | +| ------------- | ------ | ----------------------------------------------------------------------------------- | +| injectTimeout | string | maximum time for waiting the disruptor to be ready in the target pods (default 30s) | + +## Example + + + +```javascript +const disruptor = new ServiceDisruptor('my-service', 'my-namespace'); +``` diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/injectgrpcfaults.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/injectgrpcfaults.md new file mode 100644 index 000000000..6125ae14c --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/injectgrpcfaults.md @@ -0,0 +1,38 @@ +--- +aliases: + - ../../../../javascript-api/xk6-disruptor/servicedisruptor/injectgrpcfaults # docs/k6//javascript-api/xk6-disruptor/servicedisruptor/injectgrpcfaults +title: 'injectGrpcFaults' +description: 'xk6-disruptor: ServiceDisruptor.injectGrpcFaults method' +weight: 200 +--- + +# injectGrpcFaults + +injectGrpcFaults injects gRPC faults in the requests served by a target Service. + +| Parameters | Type | Description | +| ------------------ | ------ | ---------------------------------------------------------------------------------------------------------------------------------- | +| fault | object | description of the [gRPC faults](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/grpc) to be injected | +| duration | string | duration of the disruption | +| options (optional) | object | [options](#options) that control the injection of the fault | + +## Options + +The injection of the fault is controlled by the following options: + +| Option | Type | Description | +| --------- | ------ | ----------------------------------------------------------------------------------- | +| proxyPort | number | port the agent will use to listen for requests in the target pods ( default `8000`) | + +## Example + + + +```javascript +const fault = { + averageDelay: '50ms', + statusCode: 13, + errorRate: 0.1, +}; +disruptor.injectGrpcFaults(fault, '30s'); +``` diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/injecthttpfaults.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/injecthttpfaults.md new file mode 100644 index 000000000..4344d3fb0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/injecthttpfaults.md @@ -0,0 +1,48 @@ +--- +aliases: + - ../../../../javascript-api/xk6-disruptor/servicedisruptor/injecthttpfaults # docs/k6//javascript-api/xk6-disruptor/servicedisruptor/injecthttpfaults +title: 'injectHTTPFaults' +description: 'xk6-disruptor: ServiceDisruptor.injectHTTPFaults method' +weight: 300 +--- + +# injectHTTPFaults + +injectHTTPFaults injects HTTP faults in the requests served by a target Service. + +| Parameters | Type | Description | +| ------------------ | ------ | ---------------------------------------------------------------------------------------------------------------------------------- | +| fault | object | description of the [http faults](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/http) to be injected | +| duration | string | duration of the disruption | +| options (optional) | object | [options](#options) that control the injection of the fault | + +## Options + +The injection of the fault is controlled by the following options: + +| Option | Type | Description | +| --------- | ------ | ----------------------------------------------------------------------------------- | +| proxyPort | number | port the agent will use to listen for requests in the target pods ( default `8000`) | + +{{< admonition type="note" >}} + +When injecting faults you may find the following error message during the test execution: + +WARN\[0035\] Request Failed error="read tcp 172.18.0.1:43564->172.18.255.200:80: read: connection reset by peer". + +This is normal and means that one request was "in transit" at the time the faults were injected causing the request to fail due to a network connection reset. + +{{< /admonition >}} + +## Example + + + +```javascript +const fault = { + averageDelay: '50ms', + errorCode: 500, + errorRate: 0.1, +}; +disruptor.injectHTTPFaults(fault, '30s'); +``` diff --git a/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/terminate-pods.md b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/terminate-pods.md new file mode 100644 index 000000000..31bda6022 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/terminate-pods.md @@ -0,0 +1,26 @@ +--- +aliases: + - ../../../../javascript-api/xk6-disruptor/servicedisruptor/terminate-pods # docs/k6//javascript-api/xk6-disruptor/servicedisruptor/terminate-pods +title: 'terminatePods()' +description: 'xk6-disruptor: ServiceDisruptor.terminatePods method' +weight: 400 +--- + +# terminatePods() + +`terminatePods` terminates a number of pods that belong to the service specified in the ServiceDisruptor. + +| Parameter | Type | Description | +| --------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------- | +| fault | object | description of the [Pod Termination fault](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults/pod-termination) | + +## Example + + + +```javascript +const fault = { + count: 2, +}; +disruptor.terminatePods(fault); +``` diff --git a/docs/sources/k6/v0.55.x/testing-guides/load-testing-websites.md b/docs/sources/k6/v0.55.x/testing-guides/load-testing-websites.md new file mode 100644 index 000000000..9c53224f7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/load-testing-websites.md @@ -0,0 +1,345 @@ +--- +title: 'Load testing websites' +head_title: 'How to Load Test a Website: The k6 Guide' +description: 'Do you know how many users your site can handle? This guide answers the WHY and WHEN you should load test your website and gives you the best practices for load testing websites or web apps with k6. Let’s get started.' +weight: 03 +noindex: true +--- + +# Load testing websites + +This doc explains some key concepts about load testing websites, including: + +- The difference between backend and frontend performance testing +- When to choose between protocol-based, browser-based, or hybrid scripts +- Recommended practices about how to load test websites + +All load tests try to simulate real user traffic to prevent failures, improve reliability, and release new code with confidence. +But your approach to load testing must adapt to the type of application you want to test. + +In this guide, learn about strategies to test websites, including specific recommendations for complex scenario scripting and test execution. + +## Load testing approaches + +When you approach the load test, first consider the following perspectives: + +- Backend vs. frontend performance +- Protocol-based, browser-based, or hybrid load testing +- Component testing vs. end-to-end testing + +## Backend vs. frontend performance + +Performance has a significant influence on the user experience of a website. + +For example, when users want to visualize or interact with some information, they expect the website to react quickly. + +To measure performance, testers often look at response times, which are affected by two main factors: + +- Frontend performance +- Backend performance + +### Frontend performance + +Frontend performance testing verifies application performance on the interface level, measuring round-trip metrics that consider how and when page elements appear on the screen. It's concerned with the end-user experience of an application, usually involving a browser. + +![Snapshots over time of webpage elements rendering on a browser](/media/docs/k6-oss/frontend-rendering.png 'Snapshots over time of webpage elements rendering on a browser') + +Frontend performance testing excels at identifying issues on a micro level but does not expose issues in the underlying architecture of a system. + +Because it primarily measures a single user's experience of the system, frontend performance testing tends to be easier to run on a small scale. +Frontend performance testing has metrics that are distinct from backend performance testing. Frontend performance tests for things like: + +- Whether the pages of the application are optimized to render quickly on a user's screen +- How long it takes a user to interact with the UI elements of the application. + +Some concerns when doing this type of performance testing are its dependency on fully integrated environments and the cost of scaling. You can test frontend performance only once the application code and infrastructure have been integrated with a user interface. Tools to automate frontend testing are also inherently more resource-intensive, so they can be costly to run at scale and are not suitable for high load tests. + +### Backend performance + +Backend performance testing targets the underlying application servers to see how they behave under production-like conditions. When it comes to websites, while frontend performance involves how assets included in the page are rendered, backend performance focuses on how those assets are processed by the application servers, served to users, and downloaded by browsers. + +![Backend components](/media/docs/k6-oss/backend-performance.png 'Backend components of an application') + +Backend testing is broader in scope than frontend performance testing. API testing can be used to target specific components or integrated components, meaning that application teams have more flexibility and higher chances of finding performance issues earlier. Backend testing is less resource-intensive than frontend performance testing and is thus more suitable for generating high load. + +### Which one should you test? + +It depends! Ideally, both. + +Frontend testing tools are executed on the client side and are limited in scope: they do not provide enough information about backend components for fine-tuning beyond the user interface. + +This limitation can lead to false confidence in overall application performance when the amount of traffic against an application increases. While the frontend component of response time remains more or less constant, the backend component of response time increases exponentially with the number of concurrent users: + +![Backend performance often contributes significantly to overall user experience](/media/docs/k6-oss/frontend-backend-load-testing.png 'Backend performance often contributes significantly to overall user experience') + +Testing _only_ frontend performance ignores a large part of the application, one more susceptible to increased failures and performance bottlenecks at higher levels of load. + +Testing _only_ backend performance, on the other hand, ignores "the first mile" of user experience and breadth. Backend testing involves messaging at the protocol level rather than interacting with page elements the way a real user would. It verifies the foundation of an application rather than the highest layer of it that a user ultimately sees. + +Testing both frontend and backend performance leads to the best overall performance and user experience for your application. Ignoring one or the other exposes you to performance bottlenecks that significantly decrease a user's satisfaction with your application. + +However, if your testing is smaller in scope, you can choose to focus on either frontend or backend with the goal of eventually building a test suite that encompasses both. + +## Component testing vs. end-to-end testing + +When you're testing a web app, you may wonder whether how you structure your test scripts. Consider the following two methods. + +### Component testing + +One way you could load test a web app is to load test its components. Maybe you know there are issues with a specific functionality due to previous production issues, or maybe you'd like to target components that are business-critical to reduce risk exposure. + +In these cases, the type of test you write may: + +- be protocol-based +- call only those API endpoints that are relevant +- have smaller or no sleep/think time +- be focused on eventually stress testing a component or service or finding its breaking point + +Testing in this way is more flexible. With protocol testing, you can specify the endpoints or servers you'd like to hit, and narrow your test's target that way. You can test specific functionalities while skipping others that come chronologically before it in a standard user flow. You can more finely control the type of traffic that is generated. If you're recreating a specific mix of requests, you can script those requests and reproduce them in a repeatable manner. + +Doing component testing for load may not always require that your script behave like an end user. In fact, it may be necessary to artificially inflate traffic to more quickly reproduce issues, or to deflate traffic to reduce the noise in the logs. By the nature of this type of testing, scripts don't contain the full flow of a request that you would expect to see in production, so realism is not a priority. + +### End-to-end testing + +You could also do end-to-end testing against a web app. End-to-end testing seeks to replicate real user behaviour and track its effects across the entire stack. When doing end-to-end testing, you might: + +- do protocol-level, browser-level, or hybrid load testing +- replicate the actions in a typical user flow +- think about the performance of the entire workflow as well as how long a request took to be processed by each component +- write scripts that access the application the same way users would, such as by browsing to the homepage before finding their way to other parts of the website + +This type of load testing is broader in scope than component testing, but shallower in terms of depth. With end-to-end testing, you get a better idea of the full user experience of the application as a whole. However, it can also be more complex to troubleshoot, as you have more components to monitor and more places to look for issues that are found. + +End-to-end testing is based on real user behavior, so it's often important that end-to-end test scripts also be realistic. + +## Protocol-based, browser-based, or hybrid load testing + +The decision of whether to test the frontend, backend, or both will also affect the type of load testing you should carry out and the kind of scripts you should write. + +### Protocol-based load testing + +Protocol-based load testing verifies the backend performance of an application by simulating the requests underlying user actions. For websites, this commonly involves HTTP requests that bypass the user interface of your application and are sent directly to a server or application component. + +For example, a protocol-based load testing script might request all the resources on a webpage from the application servers, but those resources are merely downloaded. The response times reported by a purely protocol-based script do not include frontend metrics such as the time taken for images to render on a browser. The load is generated by simulating many requests sent to application servers. + +While protocol-based load testing may seem to lend itself better to component testing, you can also do end-to-end website testing with protocol-level scripts. + +#### Sample protocol-based test script + +The following is an example of a protocol-based load testing script in k6 that fetches the homepage, along with resources embedded into the page. + +```javascript +import http from 'k6/http'; +import { sleep, check } from 'k6'; + +export function Homepage() { + const params = { + 'sec-ch-ua': '"Chromium";v="94", "Google Chrome";v="94", ";Not A Brand";v="99"', + 'accept-encoding': 'gzip, deflate, br', + 'accept-language': 'en-GB,en;q=0.9', + }; + + // 01. Go to the homepage + let responses = http.batch([ + ['GET', 'https://mywebsite.com/', params], + ['GET', 'https://mywebsite.com/style.min.css', params], + ['GET', 'https://website.com/header.png', params], + ['GET', 'https://website.com/polyfill.min.js', params], + ]); + check(responses, { + 'Homepage loaded': (r) => JSON.stringify(r).includes('Welcome to my site'), + }); + + sleep(4); + + // 02. View products + responses = http.batch([ + ['GET', 'https://mywebsite.com/products', params], + ['GET', 'https://mywebsite.com/style.css', params], + ['GET', 'https://website.com/product1.jpg', params], + ['GET', 'https://website.com/product2.jpg', params], + ['GET', 'https://website.com/displaylist.js', params], + ]); + check(responses, { + 'Products loaded': (r) => JSON.stringify(r).includes('Add to Cart'), + }); + + sleep(1); +} +``` + +[Recording browser traffic](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings/using-the-browser-recorder) helps you prototype to test websites on the protocol level. + +### Browser-based load testing + +Browser-based load testing verifies the frontend performance of an application by simulating real users using a browser to access your website. + +For example, a browser-based load testing script might include instructions to navigate to a page, click on a button, and type out text on a form. Those user actions then trigger underlying requests on the protocol layer, but only user actions are scripted in browser-based testing. + +Unlike protocol-based load testing, browser-based load testing scripts generate load by starting multiple instances of browsers and interacting with your application the way real users would. Testing at the browser level can also be the only option for testing Single-Page Applications where a lot of the application logic is executed by client-side scripts. + +Scripting on the browser level usually requires the use of different tools from the ones used to test at the protocol level. +However, k6 now has a module called [k6 browser](https://grafana.com/docs/k6//using-k6-browser/) that allows the creation of browser-based test scripts alongside protocol-based ones. + +#### Sample browser-based test script + +The following is an example of a browser-based load testing script in k6 using the browser module on a dummy website. Instead of making an HTTP request, the script views the homepage, then looks for and clicks on a link to the product page. + +{{< code >}} + + + +```javascript +import { browser } from 'k6/browser'; + +export default async function () { + const page = await browser.newPage(); + + // 01. Go to the homepage + try { + await page.goto('https://mywebsite.com'); + + await page.waitForSelector('p[class="woocommerce-result-count"]"]'); + await page.screenshot({ path: 'screenshots/01_homepage.png' }); + + await page.waitForTimeout(4000); + + // 02. View products + const element = page.locator( + 'a[class="woocommerce-LoopProduct-link woocommerce-loop-product__link"]' + ); + await element.click(); + await page.waitForSelector('button[name="add-to-cart"]'); + await page.screenshot({ path: 'screenshots/02_view-product.png' }); + + await page.waitForTimeout(1000); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} + +#### Tips for writing browser-level scripts + +The following steps can help you get started with a browser-level test script. + +**Script user actions, not requests.** Determine what a user might do for a particular task, and script interactions with elements on the browser level. For example, script what buttons the user clicks on. + +**Identify unique selectors.** Once you have identified which page elements a user interacts with, use the Element Inspector for DevTools in your browser to find a unique, static, and simple way to identify each element. The script needs selectors to find the right element to interact with. + +**Use elements to verify responses.** After every action, use [locators](https://grafana.com/docs/k6//javascript-api/k6-browser/locator) to search for elements on the page that you would expect to find. This verification helps ensure that the script has reached the expected page. + +**Take screenshots for every action while debugging.** One of the advantages of browser-based testing is the ability to take screenshots. After every user interaction the script simulates, use [page.screenshot](https://grafana.com/docs/k6//javascript-api/k6-browser/page) to save a visual image of what the script encountered for later troubleshooting. + +### Hybrid load testing + +Hybrid load testing is a combination of protocol-based and browser-based load testing. While you can use two tools or two scripts to execute different types of load tests (one protocol-based and one browser-based), having both types of tests executed by the same script and the same tool is ideal. Aggregating results between different tools can be difficult at best and inconsistent at worst. + +![Hybrid of frontend and backend performance testing](/media/docs/k6-oss/hybrid-testing.png 'In hybrid load testing, both the frontend and backend are tested') + +A best practice in hybrid load testing is to generate most of the load using the protocol-level test, and then to run a smaller number of the browser-level testing scripts. This approach: + +- reduces the number of load generators needed, since protocol-level testing requires fewer machines to generate the same load +- measures backend and frontend performance in the same test execution +- provides a single source of aggregated output at the end +- reduces complexity in the creation and maintenance of scripts + +## Scripting considerations + +When you script a test for a website, consider these recommendations. + +### Consider factors that affect script realism + +**Record your user journey.** Using the browser recorder can facilitate initial test script creation by capturing all the embedded resources on webpages. Check out the [Session Recording guide](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings/) to learn more about how to auto-generate your load test from a user session. + +**Correlate data.** Recordings often don't take into account dynamic values that are generated anew each time a request is made. Go through the recorded requests and determine whether you need to [extract values from previous responses](https://grafana.com/docs/k6//examples/correlation-and-dynamic-data) and use parameters in subsequent requests. This practice ensures your VUs behave more like real users would. + +**Include or exclude static resources.** Determine whether you should include or exclude static resources on pages such as images, JavaScript, etc. Consider including them if you want to measure overall user experience. Consider excluding them if you are using a Content Delivery Network (CDN) that is under a separate Service Level Agreement (SLA). + +**Exclude third-party requests.** Don't load test servers that you don't own. Many applications make calls to third-party providers for authentication, social sharing, and marketing analytics. Disable these requests unless you have permission to include them in your tests. + +**Use concurrent requests.** To mimic the way modern browsers download some requests in parallel, use [batching](https://grafana.com/docs/k6//javascript-api/k6-http/batch). + +**Determine cache and cookie behaviour.** k6 automatically resets cookies between iterations, but you can also [change this behavior](https://grafana.com/docs/k6//using-k6/k6-options/reference/#no-cookies-reset) if maintaining cookies would be more realistic. + +**Use dynamic think time and pacing.** Consider adding varying [delays](https://grafana.com/docs/k6//javascript-api/k6/sleep) so you don't artificially stagger a script with completely uniform delays. + +**Use test data.** Real users typically don't search for or submit the same data repeatedly. Consider adding a [test data file](https://grafana.com/docs/k6//examples/data-parameterization) for the script to iterate through. + +**Model test parameters and load profile after production.** In k6, you can use [test options](https://grafana.com/docs/k6//using-k6/k6-options/how-to) to determine the exact shape and profile of your load test script. Select the appropriate [executors](https://grafana.com/docs/k6//using-k6/scenarios/executors) for the job. + +### Create a reusable framework + +**Use tags and groups.** Organizing requests by [tagging and grouping them](https://grafana.com/docs/k6//using-k6/tags-and-groups) helps you consolidate like metrics and makes your test scripts more understandable by others. + +**Use scenarios.** When combining protocol-based and browser-based tests, use [scenarios](https://grafana.com/docs/k6//using-k6/scenarios) to independently control their test parameters and [executors](https://grafana.com/docs/k6//using-k6/scenarios/executors). + +**Modularize scripts.** Use [modules](https://grafana.com/docs/k6//using-k6/modules) to separate and organize functions for protocol-level testing and browser-level testing, and then use a test runner script to execute them. This approach means different scripts can be versioned and changed without affecting each other. + +**Integrate your tests into your CI pipeline.** Adopting a "tests as code" approach enables you to tie your load tests more closely into your project's existing CI/CD processes, helping you get the most value out of every test. + +### Test with thresholds in mind + +**Create thresholds for both types of testing.** Some browser-level and protocol-level metrics cannot be combined because they don't measure the same thing. Set [thresholds](https://grafana.com/docs/k6//using-k6/thresholds) for relevant metrics from both the browser-level script and the protocol-level one. + +### When possible, use hybrid load testing + +**Use protocol-based scripts to generate majority of the load.** When writing the test scenario, use the protocol-based requests to simulate most of the traffic and use fewer VUs for the browser-based requests. Relying on the protocol-level traffic helps keep resource utilization on load generators down. + +## Execution considerations + +When you run tests, consider the test environment and the load generator location. + +### Run your tests in the appropriate environment + +Testing in pre-production environments and in production environments both add value. + +**Testing in pre-production environments** (staging, test, system integration testing, user acceptance testing, and production replica environments) enables you to identify performance defects early, which can save a lot of time and effort (and reputation) later on. Running in test environments also means you can often afford to be more aggressive with your tests. However, it's also more critical to get your load profiles right, and the test results you get may not necessarily apply to production. + +**Testing in production** yields the most accurate results, but it's also more risky. Often, testing production is the only feasible alternative. You can reduce the risk of impact to real customers while testing in production by using lower levels of load when running load tests during peak hours, schedule tests for off-peak hours, choosing load test types that are less risky, using techniques like [synthetic monitoring](https://grafana.com/docs/grafana-cloud/testing/synthetic-monitoring/create-checks/checks/k6/) that generate less traffic, using real user monitoring tools to get snapshots of user performance at load, and ensuring that your observability stack is working at peak efficiency. + +### Run tests where your customers are + +The location of the load generator(s), where the traffic is coming _from_, can also have an impact on your test results. The question is: where are your end users located? + +**On-premises load testing** can be ideal when testing early on in the development a website, or when there are machines that can be repurposed as load generators. However, testing entirely from within a corporate network may also yield false positives, in that response times reported are significantly lower than if the same application servers were accessed from across the country. + +**Load testing on the cloud** is an essential part of the testing strategy for many public-facing websites. Using load generators on the cloud gives you access to test in different states and geographical countries, creating a mix of load generators proportional to your users' locations. Cloud load generators are easier to provision and cheaper to maintain in the long run than on-premise ones. Load testing on the cloud can help you include the effects of network latency in your tests and yield results that are more realistic. + +## Recommendations + +Here are some recommendations to help you plan, script, and execute your load tests for websites. + +If you want to test the last-mile user experience of your website: + +- focus on **frontend performance**, +- write **browser-based test scripts**, +- and consider doing more realistic **end-to-end** tests of the user flow. + +If you want to test the underlying infrastructure of your website: + +- focus on **backend performance**, +- write **protocol-based test scripts**, +- and consider starting with **component testing** and then gradually increasing the scope. + +If your website is meant for internal or has limited access: + +- Use **on-premise load generators** located within the network that most of your users access the website from. + +If your website is external and public-facing: + +- Use **cloud load generators** in the load zones where your users reside. + +Test in **pre-production environments** when you can, but also consider **testing in production** in a limited capacity. + +Load testing websites can be complex due to the number of viable testing approaches available, the scope for performance testing, and the potential effects of releasing undertested code. By following the recommendations we've put forward here, you can tailor your testing more closely to your objectives. + +## Read more + +- [Browser testing with k6 browser](https://grafana.com/docs/k6//using-k6-browser/) +- [Load test types](https://grafana.com/docs/k6//testing-guides/test-types/) +- [Session recording guide](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings/) +- [Determining concurrent users in your load tests](https://k6.io/blog/monthly-visits-concurrent-users) +- [Data correlation in your test script](https://grafana.com/docs/k6//examples/correlation-and-dynamic-data) diff --git a/docs/sources/k6/v0.55.x/testing-guides/running-distributed-tests.md b/docs/sources/k6/v0.55.x/testing-guides/running-distributed-tests.md new file mode 100644 index 000000000..03db565ff --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/running-distributed-tests.md @@ -0,0 +1,416 @@ +--- +title: 'Running distributed tests' +description: 'How to run distributed tests in Kubernetes' +weight: 05 +--- + +# Running distributed tests + +It has already been established that k6 can [run large load tests](https://grafana.com/docs/k6//testing-guides/running-large-tests) from a single instance, but what about _multiple instances running a single test_? + +Several reasons why you may wish to run a distributed test include: + +- Your [system under test](https://grafana.com/docs/k6//misc/glossary#system-under-test) (SUT) should be accessed from multiple IP addresses. +- A fully optimized node cannot produce the load required by your extremely large test. +- Kubernetes is already your preferred operations environment. + +For scenarios such as these, we've created the [k6-operator](https://github.com/grafana/k6-operator). + +## Introducing k6-operator + +[k6-operator](https://github.com/grafana/k6-operator) is an implementation of the [operator pattern](https://grafana.com/docs/k6//misc/glossary#operator-pattern) in Kubernetes, defining [custom resources](https://grafana.com/docs/k6//misc/glossary#custom-resource) in Kubernetes. +The intent is to automate tasks that a _human operator_ would normally do; tasks like provisioning new application components, changing configurations, or resolving run-time issues. + +The k6-operator defines the custom `TestRun` resource type and listens for changes to, or creation of, `TestRun` objects. +Each `TestRun` object references a k6 test script, configures the environment, and specifies the number of instances, as `parallelism`, for a test run. +Once a change is detected, the operator will react by modifying the cluster state, spinning up k6 test jobs as needed. + +## Get started with k6-operator + +Let's walk through the process for getting started with the k6-operator. +The only requirement being access to a Kubernetes cluster and having the appropriate access and tooling. + +{{< admonition type="note" >}} + +This process can be performed on a local Kubernetes cluster running within [Docker](https://docs.docker.com/get-docker/)! +Using [kind](https://kind.sigs.k8s.io/) or [k3d](https://k3d.io/) are awesome options to experiment with the process. + +{{< /admonition >}} + +- [Introducing k6-operator](#introducing-k6-operator) +- [Get started with k6-operator](#get-started-with-k6-operator) +- [1. Install the operator](#1-install-the-operator) +- [2. Create a test script](#2-create-a-test-script) +- [3. Add test scripts](#3-add-test-scripts) + - [Add as a ConfigMap](#add-as-a-configmap) + - [Add inside a PersistentVolume](#add-inside-a-persistentvolume) +- [4. Create a custom resource](#4-create-a-custom-resource) + - [Script in a ConfigMap](#script-in-a-configmap) + - [Script in a PersistentVolume](#script-in-a-persistentvolume) + - [Configure the environment](#configure-the-environment) + - [Change command-line arguments](#change-command-line-arguments) +- [5. Run your test](#5-run-your-test) +- [6. When things go wrong](#6-when-things-go-wrong) +- [See also](#see-also) + +## 1. Install the operator + +The first step to running distributed tests in Kubernetes is to install the operator if not already installed in the cluster. +At this time, installation does require having the project source code downloaded onto your system. +Installation commands must be run from the source directory. + +{{< admonition type="note" >}} + +Besides privileged access to a Kubernetes cluster, installation will require that the system performing the installation has the following tools installed: + +- [Git](https://git-scm.com/downloads) +- [Go](https://go.dev/doc/install) +- [Kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl) +- [Kustomize](https://kubectl.docs.kubernetes.io/installation/kustomize/) +- [Make](https://www.gnu.org/software/make/) + +{{< /admonition >}} + +From your command-line, execute the following: + +```shell +git clone https://github.com/grafana/k6-operator && cd k6-operator +``` + +Ensure that your `kubectl` tool is set for the appropriate Kubernetes cluster. +Then, from the `k6-operator` directory, you may now perform the installation: + +```shell +make deploy +``` + +By default, the operator will be installed into a new namespace, `k6-operator-system`. +You can verify the successful installation by listing available resources within the namespace: + +```shell +kubectl get pod -n k6-operator-system +``` + +After a few moments, your resulting status should become `Running` as shown below: + +{{< code >}} + +```bash +NAME READY STATUS RESTARTS AGE +k6-operator-controller-manager-7664957cf7-llw54 2/2 Running 0 160m +``` + +{{< /code >}} + +You are now ready to start create and execute test scripts! + +## 2. Create a test script + +Creating k6 test scripts for Kubernetes is no different from creating the script for the command-line. +If you haven’t already created test cases for your system, then we suggest having a read through one of our guides for creating tests for websites and APIs/microservices: + +- [Website testing guide](https://grafana.com/docs/k6//testing-guides/load-testing-websites) +- [API testing guide](https://grafana.com/docs/k6//testing-guides/api-load-testing) + +In general, it is advised to start small and expand on your scripts over iterations. +So let's start simple and create a `test.js` with the following content: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + vus: 10, + duration: '10s', +}; + +export default function () { + http.get('https://test.k6.io/'); + sleep(1); +} +``` + +{{< /code >}} + +{{< admonition type="note" >}} + +While creating scripts, [run them locally](https://grafana.com/docs/k6//get-started/running-k6#running-local-tests) before publishing to your cluster. +This can give you immediate feedback if you have errors in your script. + +{{< /admonition >}} + +Let's go ahead and verify our script is valid by performing a brief test: + +```shell +k6 run test.js +``` + +We should see a successful execution and resulting output summary. + +## 3. Add test scripts + +In order for your test scripts to be run, they must be published into your cluster. +Two main methods for this are using [ConfigMap](https://kubernetes.io/docs/concepts/configuration/configmap/) or [PersistentVolume](https://kubernetes.io/docs/concepts/storage/persistent-volumes/) resources. + +### Add as a ConfigMap + +Using a `ConfigMap` is a quick and straightforward mechanism for adding your test scripts to Kubernetes. +The `kubectl` tool provides a convenient method to create a new `ConfigMap` from a local script. + +Let's create our ConfigMap as `my-test` with the content of our `test.js` script we created in the previous step: + +```shell +kubectl create configmap my-test --from-file test.js +``` + +{{< admonition type="caution" >}} + +Limitations exist on how large your test script can be when deployed within a `ConfigMap`. +Kubernetes imposes a size limit of 1,048,576 bytes (1 MiB) for the data, therefore if your test scripts exceed this limit, you'll need to mount a `PersistentVolume`. + +Check the [motivations](https://kubernetes.io/docs/concepts/configuration/configmap/#motivation) for when you should use a `ConfigMap` versus a `PersistentVolume`. + +{{< /admonition >}} + +You should see confirmation with `configmap/my-test created`. + +### Add inside a PersistentVolume + +Setting up a `PersistentVolume` is beyond the scope of this guide, but enables access to a shared filesystem from your Kubernetes cluster via `PersistentVolumeClaim`. + +When using this option, organize your test scripts in the applicable filesystem just as you would locally. +This mechanism is ideal when breaking up monolithic scripts into reusable [modules](https://grafana.com/docs/k6//using-k6/modules). + +{{< admonition type="note" >}} + +Organizing your test scripts was part of the discussion during [episode #76](https://www.youtube.com/watch?v=zDtEzp_JUOE&utm=k6-guides) of k6 Office Hours. + +{{< /admonition >}} + +{{< admonition type="caution" >}} + +When using a `PersistentVolume`, the operator will expect all test scripts to be contained within a directory named `/test/`. + +{{< /admonition >}} + +To learn more about creating `PersistentVolume` and `PersistentVolumeClaim` resources, review the [Kubernetes documentation](https://kubernetes.io/docs/concepts/storage/persistent-volumes/). + +## 4. Create a custom resource + +During [installation](#1-install-the-operator), the `TestRun` [Custom Resource definition](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/) was added to the Kubernetes API. +The data we provide in the custom resource `TestRun` object should contain all the information necessary for the k6-operator to start a distributed load test. + +Specifically, the main elements defined within the `TestRun` object relate to the name and location of the test script to run, and the amount of [parallelism](https://grafana.com/docs/k6//misc/glossary#parallelism) to utilize. + +{{< admonition type="note" >}} + +The `TestRun` custom resource provides many configuration options to control the initialization and execution of tests. +For the full listing of possible options, please refer to the project [source](https://github.com/grafana/k6-operator/blob/main/config/crd/bases/k6.io_testruns.yaml) and [README](https://github.com/grafana/k6-operator/blob/main/README.md). + +{{< /admonition >}} + +The following examples will show some common variations for the custom resource: + +### Script in a ConfigMap + +When the test script to be executed is contained within a `ConfigMap` resource, we specify the script details within the `configMap` block of [YAML](https://grafana.com/docs/k6//misc/glossary#yaml). +The `name` is the name of the ConfigMap and the `file` is the key-value for the entry. + +Let's create the file `run-k6-from-configmap.yaml` with the following content: + +{{< code >}} + +```yaml +apiVersion: k6.io/v1alpha1 +kind: TestRun +metadata: + name: run-k6-from-configmap +spec: + parallelism: 4 + script: + configMap: + name: my-test + file: test.js +``` + +{{< /code >}} + +Recall when the script was [added as a ConfigMap](#add-as-a-configmap) for our configuration values. +We created the ConfigMap named `my-test`. +The test script content was added to the map using the filename as the key-value, therefore the `file` value is `test.js`. + +The amount of `parallelism` is up to you; how many pods do you want to split the test amongst? +The operator will split the workload between the pods using [execution segments](https://grafana.com/docs/k6//misc/glossary#execution-segment). + +{{< admonition type="caution" >}} + +It is important that the `ConfigMap` and `CustomResource` are created in the same [Namespace](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/). + +{{< /admonition >}} + +### Script in a PersistentVolume + +If the test script to be executed is contained within a `PersistentVolume`, creation of a [PersistentVolumeClaim](https://kubernetes.io/docs/concepts/storage/persistent-volumes/#persistentvolumeclaims) will be required. +We won't go into the details of PersistentVolumes and PersistentVolumeClaims, but to learn more, you should review the [Kubernetes documentation](https://kubernetes.io/docs/concepts/storage/persistent-volumes/). + +Assume we've created a `PersistentVolumeClaim` named `my-volume-claim` against a `PersistentVolume` containing the test script `/test/test.js`, we can create the file `run-k6-from-volume.yaml` with the following content: + +{{< code >}} + +```yaml +apiVersion: k6.io/v1alpha1 +kind: TestRun +metadata: + name: run-k6-from-volume +spec: + parallelism: 4 + script: + volumeClaim: + name: my-volume-claim + # File is relative to /test/ directory within volume + file: test.js +``` + +{{< /code >}} + +{{< admonition type="caution" >}} + +It is important that the `PersistentVolumeClaim` and `CustomResource` are created in the same [Namespace](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/). + +{{< /admonition >}} + +### Configure the environment + +Not everything should be included directly in your scripts. +Well written scripts will allow for variability to support multiple scenarios and to avoid hard-coding values that tend to change. +These could be anything from passwords to target urls, in addition to system options. + +We can pass this data as [environment variables](https://grafana.com/docs/k6//misc/glossary#environment-variables) for use with each pod executing your script. +This can be defined explicitly within the `TestRun` resource, or by referencing a `ConfigMap` or `Secret`. + +{{< code >}} + +```yaml +apiVersion: k6.io/v1alpha1 +kind: TestRun +metadata: + name: run-k6-with-vars +spec: + parallelism: 4 + script: + configMap: + name: my-test + file: test.js + runner: + env: + - name: MY_CUSTOM_VARIABLE + value: 'this is my variable value' + envFrom: + - configMapRef: + name: my-config-vars + - secretRef: + name: my-secrets-vars +``` + +{{< /code >}} + +{{< admonition type="note" >}} + +The above YAML introduces the `runner` section. This section applies to each pod that will be running a portion of your test, based upon the desired `parallelism`. + +{{< /admonition >}} + +Now, with the referenced resources, our test scripts can [use environment variables](https://grafana.com/docs/k6//using-k6/environment-variables) as in the following: + +```javascript +export function setup() { + console.log(`Variable is set as: ${__ENV.MY_CUSTOM_VARIABLE}`); +} +``` + +### Change command-line arguments + +[k6 options](https://grafana.com/docs/k6//using-k6/k6-options/) can be specified in many ways, one being the command-line. +Specifying options via command-line can still be accomplished when using the operator as shown with the following example: + +{{< code >}} + +```yaml +apiVersion: k6.io/v1alpha1 +kind: TestRun +metadata: + name: run-k6-with-args +spec: + parallelism: 4 + script: + configMap: + name: my-test + file: test.js + arguments: --tag testid=run-k6-with-args --log-format json +``` + +{{< /code >}} + +With the above arguments, we're adding a [test-wide custom tag](https://grafana.com/docs/k6//using-k6/tags-and-groups#test-wide-tags) to metrics and changing the output format of logs to [JSON](https://grafana.com/docs/k6//misc/glossary#json). + +{{< admonition type="note" >}} + +Be sure to visit the [options reference](https://grafana.com/docs/k6//using-k6/k6-options/reference) for a listing of available options. + +{{< /admonition >}} + +## 5. Run your test + +Tests are executed by applying the custom resource `TestRun` to a cluster where the operator is running. +The test configuration is applied as in the following: + +```shell +kubectl apply -f /path/to/your/k6-resource.yaml +``` + +After completing a test run, you need to clean up the test jobs created. +This is done by running the following command: + +```shell +kubectl delete -f /path/to/your/k6-resource.yaml +``` + +{{< admonition type="note" >}} + +If you make use of [real-time results output](https://grafana.com/docs/k6//results-output/real-time), e.g. [Prometheus Remote Write](https://grafana.com/docs/k6//results-output/real-time/prometheus-remote-write), use the `cleanup` option to automatically remove resources upon test completion as with the following example: + +```yaml +apiVersion: k6.io/v1alpha1 +kind: TestRun +metadata: + name: run-k6-with-realtime +spec: + parallelism: 4 + # Removes all resources upon completion + cleanup: post + script: + configMap: + name: my-test + file: test.js + arguments: -o experimental-prometheus-rw +``` + +{{< /admonition >}} + +## 6. When things go wrong + +Sadly nothing works perfectly all the time, so knowing where you can go for help is important. + +Be sure to search the [k6-operator category in the community forum](https://community.grafana.com/c/grafana-k6/k6-operator/73). +k6 has a growing and helpful community of engineers working with k6-operator, so there's a good chance your issue has already been discussed and overcome. +It's also in these forums where you'll be able to get help from members of the k6 development team. + +## See also + +Here are some additional resources to help on your learning journey: + +- [Distributed load testing using Kubernetes with k6 (k6 Office Hours #72)](https://www.youtube.com/watch?v=5d5zxsGz8L4) +- [Demo for k6-operator](https://github.com/javaducky/demo-k6-operator) +- [Blog: Running distributed k6 tests on Kubernetes](https://k6.io/blog/running-distributed-tests-on-k8s/) diff --git a/docs/sources/k6/v0.55.x/testing-guides/running-large-tests.md b/docs/sources/k6/v0.55.x/testing-guides/running-large-tests.md new file mode 100644 index 000000000..bca75b367 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/running-large-tests.md @@ -0,0 +1,369 @@ +--- +title: 'Running large tests' +description: 'How to run large-scale k6 tests without distributed-execution' +weight: 04 +--- + +# Running large tests + +k6 can generate a lot of load from a single machine. With proper monitoring and script optimization, you might be able to run a rather large load test without needing [distributed execution](#distributed-execution). This document explains how to launch such a test, and some of the aspects you should be aware of. + +Maximizing the load a machine generates is a multi-faceted process, which includes: + +- Changing operating system settings to increase the default network and user limits. +- Monitoring the load-generator machine to ensure adequate resource usage. +- Designing efficient tests, with attention to scripting, k6 options, and file uploads. +- Monitoring the test run to detect errors logged by k6, which could indicate limitations of the load generator machine or the [system under test](https://grafana.com/docs/k6//misc/glossary#system-under-test) (SUT). + +A single k6 process efficiently uses all CPU cores on a load generator machine. Depending on the available resources, and with the guidelines described in this document, a single instance of k6 can run 30,000-40,000 simultaneous users (VUs). +In some cases, this number of VUs can generate up to 300,000 HTTP [requests per second](https://grafana.com/docs/k6//misc/glossary#requests-per-second) (RPS). + +Unless you need more than 100,000-300,000 requests per second (6-12M requests per minute), a single instance of k6 is likely sufficient for your needs. +Read on to learn about how to get the most load from a single machine. + +## OS fine-tuning + +Modern operating systems are configured with a fairly low limit of the number of concurrent network connections an application can create. This is a safe default since most programs don't need to open thousands of concurrent TCP connections like k6. However, if we want to use the full network capacity and achieve maximum performance, we need to change some of these default settings. + +On a GNU/Linux machine, run the following commands as the `root` user: + +```bash +sysctl -w net.ipv4.ip_local_port_range="1024 65535" +sysctl -w net.ipv4.tcp_tw_reuse=1 +sysctl -w net.ipv4.tcp_timestamps=1 +ulimit -n 250000 +``` + +These commands enable reusing network connections, increase the limit of network connections, and range of local ports. + +The `sysctl` commands apply immediately for the entire system, and will reset to their default values if you restart the network service or reboot the machine. The `ulimit` command applies only for the current shell session, and you'll need to run it again for it to be set in another shell instance. + +For detailed information about these settings, how to make them permanent, and instructions for macOS, check out our ["Fine tuning OS" article](https://grafana.com/docs/k6//set-up/fine-tune-os). + +## Hardware considerations + +As load increases, you need to pay attention to hardware constraints. + + + +### Network + +Network throughput of the machine is an important consideration when running large tests. Many AWS EC2 machines come with connections of 1Gbit/s, which may limit the load k6 can generate. + +When running the test, use a tool like `iftop` in the terminal to view the amount of network traffic generated in real time. +If the traffic is constant at 1Gbit/s, your test is probably limited by the network card. Consider upgrading to a different EC2 instance. + +### CPU + +k6 is heavily multi-threaded, and will effectively utilize all available CPU cores. + +The amount of CPU you need depends on your test script and associated files. +Regardless of the test file, you can assume that large tests require a significant amount of CPU power. +We recommend that you size the machine to have at least 20% idle cycles (up to 80% used by k6, 20% idle). +If k6 uses 100% of the CPU to generate load, the test will experience throttling limits, and this may +cause the result metrics to have a much larger response time than in reality. + +### Memory + +k6 can use a lot of memory, though [more efficiently than some other load testing tools](https://k6.io/blog/comparing-best-open-source-load-testing-tools#memory-usage). +Memory consumption heavily depends on your test scenarios. To estimate the memory requirement of your test, +run the test on your development machine with 100VUs and multiply the consumed memory by the target number of VUs. + +Simple tests use ~1-5MB per VU. (1000VUs = 1-5GB). +Tests that use file uploads, or load large JS modules, can consume tens of megabytes per VU. +Note that each VU has a copy of all JS modules your test uses. +To share memory between VUs, consider using [SharedArray](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray), or an external data store, such as [Redis](https://grafana.com/docs/k6//javascript-api/k6-experimental/redis). + +If you're using [swap space](https://en.wikipedia.org/wiki/Memory_paging), consider disabling it. If the system runs out of physical memory, the process of swapping memory to much slower secondary storage will have erratic effects on performance and system stability. Which likely will invalidate any test results as the load generator had different performance in different parts of the test. Instead, plan ahead for the memory usage you expect your tests to achieve, and ensure that you have enough physical RAM for the usage to not exceed 90%. + +## Monitoring the load generator + +The previous section described how hardware limits the load your machine can generate. +Keeping this in mind, you should monitor resources while the test runs—especially when running tests for the first time. + +The easiest way to monitor the load generator is to open multiple terminals on the machine: one to run k6, and others to monitor the CPU, memory, and network. +We recommend the following tools: + +- CPU and memory: [htop](https://en.wikipedia.org/wiki/Htop) or [nmon](https://nmon.sourceforge.net/). +- Network: [iftop](https://en.wikipedia.org/wiki/Iftop) or [nmon](https://nmon.sourceforge.net/). + +If you prefer graphical applications, use the system monitoring tool from your OS (e.g. GNOME System +Monitor or Plasma System Monitor), or standalone tools like [SysMonTask](https://github.com/KrispyCamel4u/SysMonTask). + +Here's a screenshot of 3 terminal sessions showing k6, iftop and htop. +![k6 iftop htop](/media/docs/k6-oss/large-scale-testing-3-terminals.png) + +### Monitoring guidelines + +As the test runs, these are good indicators to monitor: + +- **CPU utilization stays within 80%.** + If all CPU cores are 100% utilized during the test run, you might notice performance degradation in your test results. + +- **Network utilization is at an acceptable level.** + Depending on your test, you might expect to fully saturate the network bandwidth, or this might be a signal that your test is bound by the available bandwidth. In other scenarios, you might want to minimize network usage to keep costs down. + +- **Memory utilization doesn't exceed 90%.** + If you're close to exhausting available physical RAM, the system might start swapping memory to disk, which will affect performance and system stability (note that [we recommend disabling swap space altogether](#memory)). In extreme cases, running out of memory on Linux will cause the system to end the k6 process. + +## Error handling should be resilient + +When running [large stress tests](https://grafana.com/docs/k6//testing-guides/test-types/stress-testing), your script shouldn't assume anything about the HTTP response. +An oversight of some scripts is to test with only the [happy path](https://grafana.com/docs/k6//misc/glossary#happy-path 'The default behavior that happens when the system returns no errors') in mind. + +For example, in k6 scripts, we often see something like this _happy path_ check: + +```javascript +import { check } from 'k6'; +import http from 'k6/http'; + +const res = http.get('https://test.k6.io'); +const checkRes = check(res, { + 'Homepage body size is 11026 bytes': (r) => r.body.length === 11026, +}); +``` + +Code like this runs fine when the SUT is not overloaded and returns proper responses. +When the system starts to fail, this check won't work as expected. + +The issue is that the check assumes that the response always has a body. +But if the server is failing, `r.body` might not exist. +In such cases, the check itself won't work as expected, and an error such as the following will be returned: + +```bash +ERRO[0625] TypeError: Cannot read property 'length' of undefined +``` + +To fix this issue, make your checks resilient to any response type. + +The following change will handle the exception. + +{{< code >}} + +```javascript +import { check } from 'k6'; +import http from 'k6/http'; + +const res = http.get('https://test.k6.io'); +const checkRes = check(res, { + 'Homepage body size is 11026 bytes': (r) => r.body && r.body.length === 11026, +}); +``` + +{{< /code >}} + +Refer to subsequent sections for a list of [common errors](#common-errors). + +## k6 options to reduce resource use + +The following k6 settings can reduce the performance costs of running large tests. + +### Save memory with `discardResponseBodies` + +By default, k6 loads the response body of the request into memory. This causes much higher memory consumption and is often unnecessary. + +To instruct k6 to not process all response bodies, set `discardResponseBodies` in the options object like this: + +```javascript +export const options = { + discardResponseBodies: true, +}; +``` + +If you need the response body for some requests, override the option with [Params.responseType](https://grafana.com/docs/k6//javascript-api/k6-http/params). + +### When streaming, use `--no-thresholds` and `--no-summary` + +If you're running a local test and streaming results to the cloud (`k6 run -o cloud`), you might want to disable the terminal summary +and local threshold calculation, because the cloud service will display the summary and calculate the thresholds. + +Without these options, the operations will be duplicated by both the local machine and the cloud servers. +This will save some memory and CPU cycles. + +Here are all the mentioned flags, all in one: + +```bash +k6 run scripts/website.js \ + -o cloud \ + --vus=20000 \ + --duration=10m \ + --no-thresholds \ + --no-summary +``` + +## Script optimizations + +To squeeze more performance out of the hardware, consider optimizing the code of the test script itself. +Some of these optimizations involve limiting how you use certain k6 features. +Other optimizations are general to all JavaScript programming. + +### Limit resource-intensive k6 operations + +Some features of the k6 API entail more computation to execute. +To maximize load generation, you might need to limit how your script uses the following. + + + +Checks and groups +: k6 records the result of every individual check and group separately. If you are using many checks and groups, you may consider removing them to boost performance. + +Custom metrics +: Similar to checks, values for custom metrics (Trend, Counter, Gauge and Rate) are recorded separately. Consider minimizing the usage of custom metrics. + +Thresholds with abortOnFail +: If you have configured [abortOnFail thresholds](https://grafana.com/docs/k6//using-k6/thresholds#aborting-a-test-when-a-threshold-is-crossed), k6 needs to evaluate the result constantly to verify that the threshold wasn't crossed. Consider removing this setting. + +URL grouping +: k6 v0.41.0 introduced a change to support time-series metrics. A side effect of this is that every unique URL creates a new time-series object, which may consume more RAM than expected. +: To solve this, use the [URL grouping](https://grafana.com/docs/k6//using-k6/http-requests#url-grouping) feature. + + + +### JavaScript optimizations + +Finally, if the preceding suggestions are insufficient, you might be able to do some general JavaScript optimizations: + +- Avoid deeply nested `for` loops. +- Avoid references to large objects in memory as much as possible. +- Keep external JS dependencies to a minimum. +- Perform tree shaking of the k6 script if you have a build pipeline, and so on. + +Refer to this article about [garbage collection](https://javascript.info/garbage-collection) in the V8 runtime. While the JavaScript VM that k6 uses is very different and runs on Go, the general principles apply. Note that memory leaks are still possible in k6 scripts, and, if not fixed, they might exhaust RAM much more quickly. + +## File upload considerations + +File uploads can affect both resource usage and cloud costs. + + + +Network throughput +: The network throughput of the load generator machine and of the SUT are likely bottlenecks for file uploads. + +Memory +: k6 needs a significant amount of memory when uploading files. Every VU is independent and has its own memory, and files will be copied in all VUs that upload them. You can follow [issue #1931](https://github.com/grafana/k6/issues/1931) that aims to improve this situation. + +Data transfer costs +: k6 can upload a large amount of data in a very short period of time. Make sure you understand the data transfer costs before commencing a large scale test. +: [Outbound Data Transfer is expensive in AWS EC2](https://www.cloudmanagementinsider.com/data-transfer-costs-everything-you-need-to-know/). The price ranges between $0.08 to $0.20 per GB depending on the region. +If you use the cheapest region, the cost is about $0.08 per GB. Uploading 1TB, therefore, costs about $80. A long-running test can cost several hundreds of dollars in data transfer alone. +: If your infrastructure is already hosted on AWS, consider running your load generator machine within the same AWS region and availability zone. In some cases, this traffic will be much cheaper or even free. For additional data cost-saving tips, check this [article on how to reduce data transfer costs on AWS](https://www.stormit.cloud/blog/aws-data-transfer-pricing-how-to-reduce-costs/). Our examples are made with AWS in mind. However, the same suggestions also apply to other cloud providers. + +Virtual server costs +: The AWS EC2 instances are relatively cheap. Even the largest instance we have used in this benchmark (m5.24xlarge) costs only $4.6 per hour. +: Make sure you turn off the load generator servers once you are done with your testing. A forgotten EC2 server might cost thousands of dollars per month. +**Tip:** it's often possible to launch "spot instances" of the same hardware for 10-20% of the cost. + + + +## Common errors + +If you run into errors during the execution, it helps to know whether they were caused by the load generator or by the failing SUT. + +### read: connection reset by peer + +This is caused by the target system resetting the TCP connection. It happens when the Load Balancer or the server itself isn't able to handle the traffic. + +```bash +WARN[0013] Request Failed error="Get http://test.k6.io: read tcp 172.31.72.209:35288->63.32.205.136:80: read: connection reset by peer" +``` + +### context deadline exceeded + +This happens when k6 can send a request, but the target system doesn't respond in time. The default timeout in k6 is 60 seconds. If your system doesn't produce the response in this time frame, this error will appear. + +```bash +WARN[0064] Request Failed error="Get http://test.k6.io: context deadline exceeded" +``` + +### dial tcp 52.18.24.222:80: i/o timeout + +This error is similar to `context deadline exceeded`, but in this case, k6 wasn't even able to make an HTTP request. The target system can't establish a TCP connection. + +```bash +WARN[0057] Request Failed error="Get http://test.k6.io/: dial tcp 52.18.24.222:80: i/o timeout" +``` + +### socket: too many open files + +This error means that the load generator can't open TCP sockets because it reached the limit of open file descriptors. +Make sure that your limit is set sufficiently high. +Refer to the ["Fine tuning OS" article](https://grafana.com/docs/k6//set-up/fine-tune-os#viewing-limits-configuration). + +```bash +WARN[0034] Request Failed error="Get http://test.k6.io/: dial tcp 99.81.83.131:80: socket: too many open files" +``` + +{{< admonition type="note" >}} + +Decide what level of errors is acceptable. At large scale, some errors are always present. +If you make 50M requests with 100 failures, this is generally a good result (0.00002% errors). + +{{< /admonition >}} + +## Benchmarking k6 + +We've executed a few large tests on different EC2 machines to see how much load k6 can generate. Our general observation is that k6 scales proportionally to the hardware. A machine with 2x more resources can generate roughly 2x more traffic. The limit to this scalability is the number of open connections. A single Linux machine can open up to 65,535 sockets per IP address (refer to [RFC 6056](https://www.rfc-editor.org/rfc/rfc6056#section-2) for details). This means that a maximum of 65k requests can be sent simultaneously on a single machine. This is a theoretical limit that will depend on the [configured ephemeral port range](https://en.wikipedia.org/wiki/Ephemeral_port), whether HTTP/2 is in use, which uses request multiplexing and can achieve higher throughput, and other factors. + +Primarily, though, the RPS limit depends on the response time of the SUT. If responses are delivered in 100ms, the theoretical RPS limit is 650,000 for HTTP/1 requests to a single remote address. + +We maintain a [repository](https://github.com/grafana/k6-benchmarks) with some scripts used to benchmark k6 and create reports. These tests are run for every new k6 version, and you can see the results in the [`results/` directory](https://github.com/grafana/k6-benchmarks/tree/master/results). + +## Distributed execution + +In load testing, _distributed execution_ happens when a load is distributed across multiple machines. + +Users often look for the distributed execution mode to run large-scale tests. Although a single k6 instance can generate enormous load, distributed execution is necessary to: + +- Simulate load from multiple locations simultaneously. +- Scale the load of your test beyond what a single machine can handle. + +In k6, you can split the load of a test across multiple k6 instances using the [`execution-segment`](https://grafana.com/docs/k6//using-k6/k6-options/reference#execution-segment) option. For example: + +{{< code >}} + +```bash +## split the load of my-script.js across two machines +k6 run --execution-segment "0:1/2" --execution-segment-sequence "0,1/2,1" my-script.js +k6 run --execution-segment "1/2:1" --execution-segment-sequence "0,1/2,1" my-script.js +``` + +```bash +## split the load of my-script.js across three machines +k6 run --execution-segment "0:1/3" --execution-segment-sequence "0,1/3,2/3,1" my-script.js +k6 run --execution-segment "1/3:2/3" --execution-segment-sequence "0,1/3,2/3,1" my-script.js +k6 run --execution-segment "2/3:1" --execution-segment-sequence "0,1/3,2/3,1" my-script.js +``` + +```bash +## split the load of my-script.js across four machines +k6 run --execution-segment "0:1/4" --execution-segment-sequence "0,1/4,2/4,3/4,1" my-script.js +k6 run --execution-segment "1/4:2/4" --execution-segment-sequence "0,1/4,2/4,3/4,1" my-script.js +k6 run --execution-segment "2/4:3/4" --execution-segment-sequence "0,1/4,2/4,3/4,1" my-script.js +k6 run --execution-segment "3/4:1" --execution-segment-sequence "0,1/4,2/4,3/4,1" my-script.js +``` + +{{< /code >}} + +However, at this moment, the distributed execution mode of k6 is not entirely functional. The current limitations are: + +- k6 does not provide the functionality of a "primary" instance to coordinate the distributed execution of the test. Alternatively, you can use the [k6 REST API](https://grafana.com/docs/k6//misc/k6-rest-api) and [`--paused`](https://grafana.com/docs/k6//using-k6/k6-options/reference#paused) to synchronize the multiple k6 instances' execution. +- Each k6 instance evaluates [Thresholds](https://grafana.com/docs/k6//using-k6/thresholds) independently - excluding the results of the other k6 instances. If you want to disable the threshold execution, use [`--no-thresholds`](https://grafana.com/docs/k6//using-k6/k6-options/reference#no-thresholds). +- k6 reports the metrics individually for each instance. Depending on how you store the load test results, you'll have to aggregate some metrics to calculate them correctly. + +With the limitations mentioned above, we built a [Kubernetes operator](https://github.com/grafana/k6-operator) to distribute the load of a k6 test across a **Kubernetes cluster**. For further instructions, check out [the tutorial for running distributed k6 tests on Kubernetes](https://k6.io/blog/running-distributed-tests-on-k8s/). + +> The k6 goal is to support a native open-source solution for distributed execution. If you want to follow the progress, subscribe to the [distributed execution issue](https://github.com/grafana/k6/issues/140) on GitHub. + +## Large-scale tests in the cloud + +[Grafana Cloud k6](https://grafana.com/products/cloud/k6/), our commercial offering, provides an instant solution for running large-scale tests, among other [benefits](https://grafana.com/docs/grafana-cloud/testing/k6/). + +If you aren't sure whether OSS or Cloud is a better fit for your project, we recommend reading this [white paper](https://k6.io/what-to-consider-when-building-or-buying-a-load-testing-solution) to learn more about the risks and features to consider when building a scalable solution. + +## Read more + +- [Fine tuning OS](https://grafana.com/docs/k6//set-up/fine-tune-os) +- [JavaScript Compatibility Mode](https://grafana.com/docs/k6//using-k6/javascript-compatibility-mode) + +- [A biased comparison of the best open source load testing tools](https://k6.io/blog/comparing-best-open-source-load-testing-tools) +- [White paper: what to consider when building or buying a load testing solution](https://k6.io/what-to-consider-when-building-or-buying-a-load-testing-solution) diff --git a/docs/sources/k6/v0.55.x/testing-guides/synthetic-monitoring.md b/docs/sources/k6/v0.55.x/testing-guides/synthetic-monitoring.md new file mode 100644 index 000000000..89c926ec9 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/synthetic-monitoring.md @@ -0,0 +1,14 @@ +--- +title: 'Synthetic monitoring' +description: 'Use k6 as synthetic monitoring tool, scheduling k6 smoke tests for continuous production monitoring.' +weight: 03 +noindex: true +--- + +# Synthetic monitoring + +You can use k6 as synthetic monitoring tool by scheduling k6 smoke tests for continuous production monitoring. + +For a simple demo stack, see this [tutorial on using `cron` and `mail`](https://k6.io/blog/performance-monitoring-with-cron-and-k6/). + +For a more comprehensive solution, you can use [Grafana Cloud Synthetic Monitoring](https://grafana.com/docs/grafana-cloud/testing/synthetic-monitoring/create-checks/checks/k6/) to proactively monitor your application and services in production. You can run k6 smoke tests at frequent intervals, receive alerts on potential issues, and manage incidents directly from [Grafana Cloud](https://grafana.com/products/cloud/). diff --git a/docs/sources/k6/v0.55.x/testing-guides/test-types/_index.md b/docs/sources/k6/v0.55.x/testing-guides/test-types/_index.md new file mode 100644 index 000000000..82ce95e2c --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/test-types/_index.md @@ -0,0 +1,95 @@ +--- +title: 'Load test types' +description: 'A series of conceptual articles explaining the different types of load tests. Learn about planning, running, and interpreting different tests for different performance goals.' +weight: -10 +cascade: + noindex: true +--- + +# Load test types + +Many things can go wrong when a system is under load. +The system must run numerous operations simultaneously and respond to different requests from a variable number of users. +To prepare for these performance risks, teams use load testing. + +But a good load-testing strategy requires more than just executing a single script. +Different patterns of traffic create different risk profiles for the application. +For comprehensive preparation, teams must test the system against different _test types_. + +![Overview of load test shapes](/media/docs/k6-oss/chart-load-test-types-overview.png) + +## Different tests for different goals + +Start with smoke tests, then progress to higher loads and longer durations. + +The main types are as follows. Each type has its own article outlining its essential concepts. + +- [**Smoke tests**](https://grafana.com/docs/k6//testing-guides/test-types/smoke-testing) validate that your script works and that the system performs adequately under minimal load. + +- [**Average-load test**](https://grafana.com/docs/k6//testing-guides/test-types/load-testing) assess how your system performs under expected normal conditions. + +- [**Stress tests**](https://grafana.com/docs/k6//testing-guides/test-types/stress-testing) assess how a system performs at its limits when load exceeds the expected average. + +- [**Soak tests**](https://grafana.com/docs/k6//testing-guides/test-types/soak-testing) assess the reliability and performance of your system over extended periods. + +- [**Spike tests**](https://grafana.com/docs/k6//testing-guides/test-types/spike-testing) validate the behavior and survival of your system in cases of sudden, short, and massive increases in activity. + +- [**Breakpoint tests**](https://grafana.com/docs/k6//testing-guides/test-types/breakpoint-testing) gradually increase load to identify the capacity limits of the system. + +{{< admonition type="note" >}} + +In k6 scripts, configure the load configuration using [`options`](https://grafana.com/docs/k6//get-started/running-k6#using-options) or [`scenarios`](https://grafana.com/docs/k6//using-k6/scenarios). This separates workload configuration from iteration logic. + +{{< /admonition >}} + +## Test-type cheat sheet + +The following table provides some broad comparisons. + +| Type | VUs/Throughput | Duration | When? | +| --------------------------------------------------------------------------------------------------- | --------------------- | -------------------------- | ------------------------------------------------------------------------------------------------------------------ | +| [Smoke](https://grafana.com/docs/k6//testing-guides/test-types/smoke-testing) | Low | Short (seconds or minutes) | When the relevant system or application code changes. It checks functional logic, baseline metrics, and deviations | +| [Load](https://grafana.com/docs/k6//testing-guides/test-types/load-testing) | Average production | Mid (5-60 minutes) | Often to check system maintains performance with average use | +| [Stress](https://grafana.com/docs/k6//testing-guides/test-types/stress-testing) | High (above average) | Mid (5-60 minutes) | When system may receive above-average loads to check how it manages | +| [Soak](https://grafana.com/docs/k6//testing-guides/test-types/soak-testing) | Average | Long (hours) | After changes to check system under prolonged continuous use | +| [Spike](https://grafana.com/docs/k6//testing-guides/test-types/spike-testing) | Very high | Short (a few minutes) | When the system prepares for seasonal events or receives frequent traffic peaks | +| [Breakpoint](https://grafana.com/docs/k6//testing-guides/test-types/breakpoint-testing) | Increases until break | As long as necessary | A few times to find the upper limits of the system | + +## General recommendations + +When you write and run different test types in k6, consider the following. + +### Start with a smoke test + +Start with a [smoke test](https://grafana.com/docs/k6//testing-guides/test-types/smoke-testing). +Before beginning larger tests, validate that your scripts work as expected and that your system performs well with a few users. + +After you know that the script works and the system responds correctly to minimal load, +you can move on to average-load tests. +From there, you can progress to more complex load patterns. + +### The specifics depend on your use case + +Systems have different architectures and different user bases. As a result, the correct load testing strategy is highly dependent on the risk profile for your organization. Avoid thinking in absolutes. + +For example, k6 can model load by either number of VUs or by number of iterations per second ([open vs. closed](https://grafana.com/docs/k6//using-k6/scenarios/concepts/open-vs-closed)). +When you design your test, consider which pattern makes sense for the type. + +What's more, **no single test type eliminates all risk.** +To assess different failure modes of your system, incorporate multiple test types. +The risk profile of your system determines what test types to emphasize: + +- Some systems are more at risk of longer use, in which case soaks should be prioritized. +- Others are more at risk of intensive use, in which case stress tests should take precedence. + +In any case, **no single test can uncover all issues**. + +What's more, the categories themselves are relative to use cases. A stress test for one application is an average-load test for another. Indeed, no consensus even exists about the names of these test types (each of the following topics provides alternative names). + +### Aim for simple designs and reproducible results + +While the specifics are greatly context-dependent, what's constant is that you want to make results that you can compare and interpret. + +Stick to simple load patterns. For all test types, directions is enough: ramp-up, plateau, ramp-down. + +Avoid "rollercoaster" series where load increases and decreases multiple times. These will waste resources and make it hard to isolate issues. diff --git a/docs/sources/k6/v0.55.x/testing-guides/test-types/breakpoint-testing.md b/docs/sources/k6/v0.55.x/testing-guides/test-types/breakpoint-testing.md new file mode 100644 index 000000000..b213d535a --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/test-types/breakpoint-testing.md @@ -0,0 +1,130 @@ +--- +title: 'Breakpoint testing' +description: 'Breakpoint tests aim to find system limits. They increase load until the system fails.' +weight: 07 +--- + +# Breakpoint testing + +Breakpoint testing aims to find system limits. Reasons you might want to know the limits include: + +- To tune or care for the system's weak spots to relocate those higher limits at higher levels. +- To help plan remediation steps in those cases and prepare for when the system nears those limits. + +In other words, knowing where and how a system starts to fail helps prepare for such limits. + +A breakpoint ramps to unrealistically high numbers. +This test commonly has to be stopped manually or automatically as thresholds start to fail. When these problems appear, the system has reached its limits. + +![Overview of a breakpoint test](/media/docs/k6-oss/chart-breakpoint-test-overview.png) + +The breakpoint test is another test type with no clear naming consensus. +In some testing conversation, it's also known as capacity, point load, and limit testing. + +## When to run a breakpoint test + +Teams execute a breakpoint test whenever they must know their system's diverse limits. Some conditions that may warrant a breakpoint test include the following: + +- The need to know if the system's load expects to grow continuously +- If current resource consumption is considered high +- After significant changes to the code-base or infrastructure. + +How often to run this test type depends on the risk of reaching the system limits and the number of changes to provision infrastructure components. + +Once the breakpoint runs and the system limits have been identified, you can repeat the test after the tuning exercise to validate how it impacted limits. Repeat the test-tune cycle until the team is satisfied. + +## Considerations + +- **Avoid breakpoint tests in elastic cloud environments.** + + The elastic environment may grow as the test moves further, finding only the limit of your cloud account bill. If this test runs on a cloud environment, **turning off elasticity on all the affected components is strongly recommended**. + +- **Increase the load gradually.** + + A sudden increase may make it difficult to pinpoint why and when the system starts to fail. + +- **System failure could mean different things to different teams** + + You might want to identify each of the following failure points: + + - Degraded performance. The response times increased, and user experience decreased. + - Troublesome performance. The response times get to a point where the user experience severely degrades. + - Timeouts. Processes are failing due to extremely high response times. + - Errors. The system starts responding with HTTP error codes. + - System failure. The system collapsed. + +- **You can repeat this test several times** + + Repeating after each tuning might let the you push the system further. + +- **Run breakpoints only when the system is known to perform under all other test types.** + + The breakpoint test might go far if the system performs poorly with the previous testing types. + +## Breakpoint testing in k6 + +The breakpoint test is straightforward. Load slowly ramps up to a considerably high level. +It has no plateau, ramp-down, or other steps. And it generally fails before reaching the indicated point. + +k6 offers two ways to increase the activity: increasing VUs or increasing throughput ([open and closed models](https://grafana.com/docs/k6//using-k6/scenarios/concepts/open-vs-closed)). +Different from other load test types, which should be stopped when the system degrades to a certain point, breakpoint load increases even as the system starts to degrade. +That makes it recommendable to use [ramping-arrival-rate](https://grafana.com/docs/k6//using-k6/scenarios/executors/ramping-arrival-rate) for a breakpoint test. + +The test keeps increasing load or VUs until it reaches the defined breaking point or system limits, at which point the test stops or is aborted. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + // Key configurations for breakpoint in this section + executor: 'ramping-arrival-rate', //Assure load increase if the system slows + stages: [ + { duration: '2h', target: 20000 }, // just slowly ramp-up to a HUGE load + ], +}; + +export default () => { + const urlRes = http.get('https://test-api.k6.io'); + sleep(1); + // MORE STEPS + // Here you can have more steps or complex script + // Step1 + // Step2 + // etc. +}; +``` + +{{< /code >}} + +![The shape of the breakpoint test as configured in the preceding script](/media/docs/k6-oss/chart-breakpoint-test-k6-script-example.png) + +The test must be stopped before it completes the scheduled execution. +You can stop the test manually or with a threshold: + +- To stop k6 manually in the CLI, press `Ctrl+C` in Linux or Windows, and `Command .` in Mac. +- To stop the test using a threshold, you must define `abortOnFail` as true. + +For details, refer to [Thresholds](https://grafana.com/docs/k6//using-k6/thresholds). + +## Results analysis + +A breakpoint test must cause system failure. +The test helps identify the failure points of our system and how the system behaves once it reaches its limits. + +Once the system limits are identified, the team has two choices: accept them or tune the system. + +If the decision is to accept the limits, the test results help teams prepare and act when the system is nearing such limits. + +These actions could be: + +- Prevent reaching such limits +- Grow system resources +- Implement corrective actions for the system behavior at its limit +- Tune the system to stretch its limits + +If the action taken is to tune the system, tune, then repeat the breakpoint test to find where and whether the system limits moved. + +A team must determine the number of repetitions of the breakpoint test, how much the system can be tuned, and how far its limits can be tuned after each exercise. diff --git a/docs/sources/k6/v0.55.x/testing-guides/test-types/load-testing.md b/docs/sources/k6/v0.55.x/testing-guides/test-types/load-testing.md new file mode 100644 index 000000000..905f225f6 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/test-types/load-testing.md @@ -0,0 +1,109 @@ +--- +title: 'Average-load testing' +head_title: 'What is Load Testing? How to create a Load Test in k6' +description: 'An average-load test assesses the performance of your system in terms of concurrent users or requests per second.' +weight: 02 +--- + +# Average-load testing + +An average-load test assesses how the system performs under typical load. Typical load might be a regular day in production or an average moment. + +Average-load tests simulate the number of concurrent users and requests per second that reflect average behaviors in the production environment. This type of test typically increases the throughput or VUs gradually and keeps that average load for some time. Depending on the system's characteristics, the test may stop suddenly or have a short ramp-down period. + +![Overview of an average-load test](/media/docs/k6-oss/chart-average-load-test-overview.png) + +Since “load test” might refer to all types of tests that simulate traffic, this guide uses the name _average-load test_ to avoid confusion. +In some testing conversation, this test also might be called a day-in-life test or volume test. + +## When to run an average-load test + +Average-Load testing helps understand whether a system meets performance goals on a typical day (commonplace load). _Typical day_ here means when an average number of users access the application at the same time, doing normal, average work. + +You should run an average-load test to: + +- Assess the performance of your system under a typical load. +- Identify early degradation signs during the ramp-up or full load periods. +- Assure that the system still meets the performance standards after system changes (code and infrastructure). + +## Considerations + +When you prepare an average-load test, consider the following: + +- **Know the specific number of users and the typical throughput per process in the system.** + + To find this, look through APMs or analytic tools that provide information from the production environment. If you can't access such tools, the business must provide these estimations. + +- **Gradually increase load to the target average.** + + That is, use a _ramp-up period_. This period usually lasts between 5% and 15% of the total test duration. A ramp-up period has many essential uses: + + - It gives your system time to warm up or auto-scale to handle the traffic. + - It lets you compare response times between the low-load and average-load stages. + - If you run tests using our cloud service, a ramp up lets the automated performance alerts understand the expected behavior of your system. + +- **Maintain average for a period longer than the ramp up.** + + Aim for an average duration at least five times longer than the ramp-up to assess the performance trend over a significant period of time. + +- **Consider a ramp-down period.** + + The ramp down is when virtual user activity gradually decreases. The ramp down usually lasts as long as the ramp up or a bit less. + +## Average-load testing in k6 + +{{< admonition type="note" >}} + +If this is your first time running load tests, we recommend starting small or configuring the ramp-up to be slow. Your application and infrastructure might not be as rock solid as you think. We've had thousands of users run load tests that quickly crash their applications (or staging environments). + +{{< /admonition >}} + +The goal of an average-load test is to simulate the average amount of activity on a typical day in production. The pattern follows this sequence: + +1. Increase the script's activity until it reaches the desired number of users and throughput. +1. Maintain that load for a while +1. Depending on the test case, stop the test or let it ramp down gradually. + +Configure load in the `options` object: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + // Key configurations for avg load test in this section + stages: [ + { duration: '5m', target: 100 }, // traffic ramp-up from 1 to 100 users over 5 minutes. + { duration: '30m', target: 100 }, // stay at 100 users for 30 minutes + { duration: '5m', target: 0 }, // ramp-down to 0 users + ], +}; + +export default () => { + const urlRes = http.get('https://test-api.k6.io'); + sleep(1); + // MORE STEPS + // Here you can have more steps or complex script + // Step1 + // Step2 + // etc. +}; +``` + +{{< /code >}} + +This script logic has only one request (to open a web page). Your test behavior likely has more steps. If you would like to see more complex tests that use groups, checks, thresholds, and helper functions, refer to [Examples](https://grafana.com/docs/k6//examples). + +The VU or throughput chart of an average-load test looks similar to this: + +![The shape of the average-load test as configured in the preceding script](/media/docs/k6-oss/chart-average-load-test-k6-script-example.png 'Note that the number of users or throughput starts at 0, gradually ramps up to the desired value, and stays there for the indicated period. Then load ramps down for a short period.') + +## Results analysis + +An initial outcome for the average-load test appears during the ramp-up period to find whether the response time degrades as the load increases. Some systems might even fail during the ramp-up period. + +The test validates if the system's performance and resource consumption stay stable during the period of full load, as some systems may display erratic behavior in this period. + +Once you know your system performs well and survives a typical load, you may need to push it further to determine how it behaves at above-average conditions. Some of these above-average conditions are known as [Stress tests](https://grafana.com/docs/k6//testing-guides/test-types/stress-testing). diff --git a/docs/sources/k6/v0.55.x/testing-guides/test-types/smoke-testing.md b/docs/sources/k6/v0.55.x/testing-guides/test-types/smoke-testing.md new file mode 100644 index 000000000..ca56f2ca7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/test-types/smoke-testing.md @@ -0,0 +1,81 @@ +--- +title: 'Smoke testing' +head_title: 'What is Smoke Testing? How to create a Smoke Test in k6' +description: 'A Smoke test is a minimal load test to run when you create or modify a script.' +weight: 01 +--- + +# Smoke testing + +Smoke tests have a minimal load. +Run them to verify that the system works well under minimal load and to gather baseline performance values. + +This test type consists of running tests with a few VUs — more than 5 VUs could be considered a mini-load test. + +Similarly, the test should execute for a short period, either a low number of [iterations](https://grafana.com/docs/k6//using-k6/k6-options/reference#iterations) or a [duration](https://grafana.com/docs/k6//using-k6/k6-options/reference#duration) from seconds to a few minutes maximum. + +![Overview of a smoke test](/media/docs/k6-oss/chart-smoke-test-overview.png) + +In some testing conversation, smoke tests are also called shakeout tests. + +## When to run a Smoke test + +Teams should run smoke tests whenever a test script is created or updated. Smoke testing should also be done whenever the relevant application code is updated. + +It's a good practice to run a smoke test as a first step, with the following goals: + +- Verify that your test script doesn't have errors. +- Verify that your system doesn't throw any errors (performance or system related) when under minimal load. +- Gather baseline performance metrics of your system’s response under minimal load. +- With simple logic, to serve as a [synthetic test](https://grafana.com/docs/grafana-cloud/testing/synthetic-monitoring/create-checks/checks/k6/) to monitor the performance and availability of production environments. + +## Considerations + +When you prepare a smoke test, consider the following: + +- **Each time you create or update a script, run a smoke test** + + Because smoke tests verify test scripts, try to run one every time you create or update a script. Avoid running other test types with untested scripts. + +- **Keep throughput small and duration short** + + Configure your test script to be executed by a small number of VUs (from 2 to 20) with few iterations or brief durations (30 seconds to 3 minutes). + +## Smoke testing in k6 + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check, sleep } from 'k6'; + +export const options = { + vus: 3, // Key for Smoke test. Keep it at 2, 3, max 5 VUs + duration: '1m', // This can be shorter or just a few iterations +}; + +export default () => { + const urlRes = http.get('https://test-api.k6.io'); + sleep(1); + // MORE STEPS + // Here you can have more steps or complex script + // Step1 + // Step2 + // etc. +}; +``` + +{{< /code >}} + +The following script is an example smoke test. You can copy it, change the endpoints, and start testing. For more comprehensive test logic, refer to [Examples](https://grafana.com/docs/k6//examples). +The VU chart of a smoke test should look similar to this. + +![The shape of the smoke test as configured in the preceding script](/media/docs/k6-oss/chart-smoke-test-k6-script-example.png) + +## Results analysis + +The smoke test initially validates that your script runs without errors. If any script-related errors appear, correct the script before trying any more extensive tests. + +On the other hand, if you notice poor performance with these low VU numbers, report it, fix your environment, and try again with a smoke test before any further tests. + +Once your smoke test shows zero errors and the performance results seem acceptable, you can proceed to other test types. diff --git a/docs/sources/k6/v0.55.x/testing-guides/test-types/soak-testing.md b/docs/sources/k6/v0.55.x/testing-guides/test-types/soak-testing.md new file mode 100644 index 000000000..9b5e89342 --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/test-types/soak-testing.md @@ -0,0 +1,101 @@ +--- +title: 'Soak testing' +head_title: 'What is Soak Testing? How to create a Soak Test in k6' +description: 'A Soak Test tests the reliability and performance of your system over extended periods of use.' +weight: 05 +--- + +# Soak testing + +Soak testing is another variation of the Average-Load test. It focuses on extended periods, analyzing the following: + +- The system's degradation of performance and resource consumption over extended periods. +- The system's availability and stability during extended periods. + +The soak test differs from an average-load test in test duration. In a soak test, the peak load duration (usually an average amount) extends several hours and even days. +Though the duration is considerably longer, the ramp-up and ramp-down periods of a soak test are the same as an average-load test. + +![Overview of a soak test](/media/docs/k6-oss/chart-soak-test-overview.png) + +In some testing conversation, a soak test might be called an endurance, constant high load, or stamina test. + +## When to perform a Soak test + +Most systems must stay turned on and keep working for days, weeks, and months without intervention. This test verifies the system stability and reliability over extended periods of use. + +This test type checks for common performance defects that show only after extended use. Those problems include response time degradation, memory or other resource leaks, data saturation, and storage depletion. + +## Considerations + +When you prepare to run a soak test, consider the following: + +- **Configure the duration to be considerably longer than any other test.** + + Some typical values are 3, 4, 8, 12, 24, and 48 to 72 hours. + +- **If possible, re-use the average-load test script** + + Changing only the peak durations for the aforementioned values. + +- **Don't run soak tests before running smoke and average-load tests.** + + Each test uncovers different problems. + Running this first may cause confusion and resource waste. + +- **Monitor the backend resources and code efficiency.** + Since we are checking for system degradation, monitoring the backend resources and code efficiency is highly recommended. + Of all test types, backend monitoring is especially important for soak tests. + +## Soak testing in k6 + +The soak test is almost the same as the average-load test. The only difference is the increased duration of the load plateau. + +1. Increase the load until it reaches an average number of users or throughput. +1. Maintain that load for **a considerably longer time.** +1. Finally, depending on the test case, stop or ramp down gradually. + +Configure the load duration in the `options` object: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + // Key configurations for Soak test in this section + stages: [ + { duration: '5m', target: 100 }, // traffic ramp-up from 1 to 100 users over 5 minutes. + { duration: '8h', target: 100 }, // stay at 100 users for 8 hours!!! + { duration: '5m', target: 0 }, // ramp-down to 0 users + ], +}; + +export default () => { + const urlRes = http.get('https://test-api.k6.io'); + sleep(1); + // MORE STEPS + // Here you can have more steps or complex script + // Step1 + // Step2 + // etc. +}; +``` + +{{< /code >}} + +For more complex behavior, refer to [Examples](https://grafana.com/docs/k6//examples). + +Notice that, as in an average-load test, peak load plateaus at 100. VUs. +The difference is in duration. +In this soak, peak load maintains for 8 hours rather than some minutes. + +![The shape of the soak test as configured in the preceding script](/media/docs/k6-oss/chart-soak-test-k6-script-example.png) + +## Results analysis + +If we execute this test after the previous types, we should have a system performing well under previous scenarios. In this test, monitor for changes in any performance metric as time passes. Try to correlate any impact with backend measurement changes that indicate degradation over time. Such changes can be gradual degradations, as mentioned, and sudden changes (improvements, too) in response time and backend hardware resources. Backend resources to check are RAM consumed, CPU, Network, and growth of cloud resources, among others. + +The expected outcome is that the performance and resource utilization of the backend stays stable or within expected variations. + +After you run all the previous test types, you know your system performs well at many different loads: small, average, high, and extended. diff --git a/docs/sources/k6/v0.55.x/testing-guides/test-types/spike-testing.md b/docs/sources/k6/v0.55.x/testing-guides/test-types/spike-testing.md new file mode 100644 index 000000000..fa2e29a3a --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/test-types/spike-testing.md @@ -0,0 +1,94 @@ +--- +title: 'Spike testing' +description: 'Spike tests simulate moments of short, extreme load' +weight: 06 +--- + +# Spike testing + +A spike test verifies whether the system survives and performs under sudden and massive rushes of utilization. + +Spike tests are useful when the system may experience events of sudden and massive traffic. +Examples of such events include ticket sales (Taylor Swift), product launches (PS5), broadcast ads (Super Bowl), process deadlines (tax declaration), and seasonal sales (Black Friday). Also, spikes in traffic could be caused by more frequent events such as rush hours, a particular task, or a use case. + +Spike testing increases to extremely high loads in a very short or non-existent ramp-up time. +Usually, it has no plateau period or is very brief, as real users generally do not stick around doing extra steps in these situations. In the same way, the ramp-down is very fast or non-existent, letting the process iterate only once. + +This test might include different processes than the previous test types, as spikes often aren't part of an average day in production. It may also require adding, removing, or modifying processes on the script that were not in the average-load tests. + +Occasionally, teams should revamp the system to allow or prioritize resources for the high-demand processes during the event. + +![Overview of a spike test](/media/docs/k6-oss/chart-spike-test-overview.png) + +## When to perform a spike test + +This test must be executed when the system expects to receive a sudden rush of activity. + +When the system expects this type of behavior, the spike test helps identify how the system will behave and if it will survive the sudden rush of load. The load is considerably above the average and might focus on a different set of processes than the other test types. + +## Considerations + +When preparing for a spike test, consider the following: + +- **Focus on key processes in this test type.** + + Assess whether the spike in traffic triggers the same or different processes from the other test types. Create test logic accordingly. + +- **The test often won't finish.** + + Errors are common under these scenarios. + +- **Run, tune, repeat.** + + When your system is at risk of spike events, the team must run a spikes test and tune the system several times. + +- **Monitor.** + + Backend monitoring is a must for successful outcomes of this test. + +## Spike testing in k6 + +The key differentiators of the spike test are the simulation of sudden and very high loads. It lacks a plateau (full load) duration or is usually brief. + +Sometimes, the test may require a load plateau for some time. If a plateau is needed, it's generally short. A ramp-down can also be quick or unnecessary as the goal is to suddenly increase the system's load. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + // Key configurations for spike in this section + stages: [ + { duration: '2m', target: 2000 }, // fast ramp-up to a high point + // No plateau + { duration: '1m', target: 0 }, // quick ramp-down to 0 users + ], +}; + +export default () => { + const urlRes = http.get('https://test-api.k6.io'); + sleep(1); + // MORE STEPS + // Add only the processes that will be on high demand + // Step1 + // Step2 + // etc. +}; +``` + +{{< /code >}} + +In a spike test, load quickly increases to an extreme level. +The ramp-down period follows when the test reaches the maximum, returning to 0 quickly. + +A spike test gets its name from the shape of its load when represented graphically. + +![The shape of the spike test as configured in the preceding script](/media/docs/k6-oss/chart-spike-test-k6-script-example.png 'Note that the load goes from 0 to peak in three minutes: an abrupt increase.') + +## Results analysis + +Some performance metrics to assess in spike tests include pod speeds, recovery times after the load rush, time to return to normal, or the behavior on crucial system processes during the overload. + +Finding how the system responds (if it survives) to the sudden rush helps to optimize it to guarantee that it can perform during a real event. In some events, the load is so high that the whole system may have to be optimized to deal with the key processes. In these cases, repeat the test until the system confidence is high. diff --git a/docs/sources/k6/v0.55.x/testing-guides/test-types/stress-testing.md b/docs/sources/k6/v0.55.x/testing-guides/test-types/stress-testing.md new file mode 100644 index 000000000..8cf097d7d --- /dev/null +++ b/docs/sources/k6/v0.55.x/testing-guides/test-types/stress-testing.md @@ -0,0 +1,108 @@ +--- +title: 'Stress testing' +head_title: 'What is Stress Testing? How to create a Stress Test in k6' +description: 'Stress tests assess the limits of your system and stability under extreme conditions.' +weight: 03 +--- + +# Stress testing + +Stress testing assesses how the system performs when loads are heavier than usual. + +The load pattern of a stress test resembles that of an average-load test. The main difference is higher load. +To account for higher load, the ramp-up period takes longer in proportion to the load increase. +Similarly, after the test reaches the desired load, it might last for slightly longer than it would in the average-load test. + +![Overview of a stress test](/media/docs/k6-oss/chart-stress-test-overview.png) + +In some testing conversation, stress tests might also be called rush-hour, surge, or scale tests. + +## When to perform a Stress test + +Stress tests verify the stability and reliability of the system under conditions of heavy use. +Systems may receive higher than usual workloads on unusual moments such as process deadlines, paydays, rush hours, ends of the workweek, and many other behaviors that might cause frequent higher-than-average traffic. + +## Considerations + +When you run a stress test, consider the following: + +- **Load should be higher than what the system experiences on average.** + + Some testers might have default targets for stress tests—say an increase upon average load by 50 or 100 percent—there's no fixed percentage. + + The load simulated in a Stress test depends on the stressful situations that the system may be subject to. Sometimes this may be just a few percentage points above that average. Other times, it can be 50 to 100% higher, as mentioned. Some stressful situations can be twice, triple, or even orders of magnitude higher. + + Define load according to the risk load patterns that the system may receive. + +* **Only run stress tests after running average-load tests.** + + Identify performance issues under average-load tests before trying anything more challenging. + This sequence is essential. + +- **Re-use the Average-Load test script.** + + Modify the parameters to have higher load or VUs. + +- **Expect worse performance compared to average load.** + + This test determines how much the performance degrades with the extra load and whether the system survives it. A well-performant system should respond with consistent response times when handling a constant workload for an extended period. + +## Stress testing in k6 + +The load in a stress test resembles load in an average-load test. +The difference is that it reaches a higher level of load. + +1. Increase the script's activity further in a slower ramp-up until it reaches an above-average number of users or throughput. +1. Maintain that load for a while. +1. Depending on the test case, stop or ramp down gradually. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + // Key configurations for Stress in this section + stages: [ + { duration: '10m', target: 200 }, // traffic ramp-up from 1 to a higher 200 users over 10 minutes. + { duration: '30m', target: 200 }, // stay at higher 200 users for 30 minutes + { duration: '5m', target: 0 }, // ramp-down to 0 users + ], +}; + +export default () => { + const urlRes = http.get('https://test-api.k6.io'); + sleep(1); + // MORE STEPS + // Here you can have more steps or complex script + // Step1 + // Step2 + // etc. +}; +``` + +{{< /code >}} + +For more complex behavior, refer to [Examples](https://grafana.com/docs/k6//examples). + +The VU or throughput chart of a Stress test looks similar to this: + +![The shape of the stress test as configured in the preceding script](/media/docs/k6-oss/chart-stress-test-k6-script-example.png) + +Note that in the same way as the average-load test, the Stress test starts at 0 and increases beyond the point tested in the average-load type. The ramp-up and ramp-down periods are longer to allow a more realistic response. + +{{< admonition type="note" >}} + +Run stress tests only after smoke and average-load tests. Running this test type earlier may be wasteful and make it hard to pinpoint problems if they appear at low volumes or at loads under the average utilization. + +{{< /admonition >}} + +## Results analysis + +Like the average-load test, an initial outcome for the Stress test shows up during the ramp-up period to identify response time degradation as the load increases further than the average utilization. Commonly, the performance degrades, and even the system's stability crashes as we push the system further than the average-load test. + +During the full load period, verification is vital if the system's performance and resource consumption stays stable with a higher load. + +Now that you know that your system can handle outstanding load events, the teams generally check if the system performs well over extended periods. +That is, they run a [Soak test](https://grafana.com/docs/k6//testing-guides/test-types/soak-testing). diff --git a/docs/sources/k6/v0.55.x/using-k6-browser/_index.md b/docs/sources/k6/v0.55.x/using-k6-browser/_index.md new file mode 100644 index 000000000..bc689640c --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6-browser/_index.md @@ -0,0 +1,129 @@ +--- +title: Using k6 browser +description: 'The browser module brings browser automation and end-to-end testing to k6 while supporting core k6 features. Interact with real browsers and collect frontend metrics as part of your k6 tests.' +weight: 300 +--- + +# Using k6 browser + +The [Browser module](https://github.com/grafana/xk6-browser) brings browser automation and end-to-end web testing to k6 while supporting core k6 features. It adds browser-level APIs to interact with browsers and collect frontend performance metrics as part of your k6 tests. + +This module aims to provide rough compatibility with the Playwright API, so you don’t need to learn a completely new API. + +{{< admonition type="note" >}} + +To work with the browser module, make sure you are using the latest [k6 version](https://github.com/grafana/k6/releases), and install a Chromium-based browser on your machine (such as [Google Chrome](https://www.google.com/chrome/)). + +{{< /admonition >}} + +Watch the video below to learn more about k6 browser. + +{{< youtube id="N7VJ9X5yAKo" >}} + +## Use case for browser testing + +The main use case for the browser module is to test performance on the browser level. Browser-level testing provides a way to measure user experience and find issues that are difficult to catch on the protocol level. Browser-level testing can help you answer questions like: + +- When my application is receiving thousands of simultaneous requests from the protocol-level, what happens to the frontend? +- How can I get metrics specific to browsers, like total page load time? +- Are all my elements interactive on the frontend? +- Are there any loading spinners that take a long time to disappear? + +## A simple browser test + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, + thresholds: { + checks: ['rate==1.0'], + }, +}; + +export default async function () { + const context = await browser.newContext(); + const page = await context.newPage(); + + try { + await page.goto("https://test.k6.io/my_messages.php"); + + await page.locator('input[name="login"]').type("admin"); + await page.locator('input[name="password"]').type("123"); + + await Promise.all([ + page.waitForNavigation(), + page.locator('input[type="submit"]').click(), + ]); + + await check(page.locator("h2"), { + 'header': async h2 => await h2.textContent() == "Welcome, admin!" + }); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} + +The preceding code launches a Chromium-based browser, visits the application and mimics a user logging in to the application. Once submitted, it checks if the text of the header matches what is expected. + +After running the test, the following [browser metrics](https://grafana.com/docs/k6//using-k6-browser/metrics) will be reported. + +{{< code >}} + +```bash + /\ |‾‾| /‾‾/ /‾‾/ + /\ / \ | |/ / / / + / \/ \ | ( / ‾‾\ + / \ | |\ \ | (‾) | + / __________ \ |__| \__\ \_____/ .io + + execution: local + script: test.js + output: - + + scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop): + * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s) + + +running (00m01.3s), 0/1 VUs, 1 complete and 0 interrupted iterations +ui ✓ [======================================] 1 VUs 00m01.3s/10m0s 1/1 shared iters + + ✓ header + + browser_data_received.......: 2.6 kB 2.0 kB/s + browser_data_sent...........: 1.9 kB 1.5 kB/s + browser_http_req_duration...: avg=215.4ms min=124.9ms med=126.65ms max=394.64ms p(90)=341.04ms p(95)=367.84ms + browser_http_req_failed.....: 0.00% ✓ 0 ✗ 3 + browser_web_vital_cls.......: avg=0 min=0 med=0 max=0 p(90)=0 p(95)=0 + browser_web_vital_fcp.......: avg=344.15ms min=269.2ms med=344.15ms max=419.1ms p(90)=404.11ms p(95)=411.6ms + browser_web_vital_fid.......: avg=200µs min=200µs med=200µs max=200µs p(90)=200µs p(95)=200µs + browser_web_vital_inp.......: avg=8ms min=8ms med=8ms max=8ms p(90)=8ms p(95)=8ms + browser_web_vital_lcp.......: avg=419.1ms min=419.1ms med=419.1ms max=419.1ms p(90)=419.1ms p(95)=419.1ms + browser_web_vital_ttfb......: avg=322.4ms min=251ms med=322.4ms max=393.8ms p(90)=379.52ms p(95)=386.66ms + ✓ checks......................: 100.00% ✓ 1 ✗ 0 + data_received...............: 0 B 0 B/s + data_sent...................: 0 B 0 B/s + iteration_duration..........: avg=1.28s min=1.28s med=1.28s max=1.28s p(90)=1.28s p(95)=1.28s + iterations..................: 1 0.777541/s + vus.........................: 1 min=1 max=1 + vus_max.....................: 1 min=1 max=1 +``` + +{{< /code >}} + +This gives you a representation of browser performance, via the web vitals, as well as the HTTP requests that came from the browser. diff --git a/docs/sources/k6/v0.55.x/using-k6-browser/metrics.md b/docs/sources/k6/v0.55.x/using-k6-browser/metrics.md new file mode 100644 index 000000000..d1b36d76b --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6-browser/metrics.md @@ -0,0 +1,178 @@ +--- +title: 'Browser metrics' +description: 'An overview of the different browser performance metrics that the browser module tracks.' +weight: 03 +--- + +# Browser metrics + +Follow along to learn about: + +- Google's Core Web Vitals and why they are important +- How to analyze the browser metrics output +- How to set thresholds for your browser metrics +- How to use the Performance API to measure web performance. + +## Google's Core Web Vitals + +The k6 browser module emits metrics based on the [Core Web Vitals](https://web.dev/vitals/#core-web-vitals). + +Google introduced these metrics to provided unified signals to assess user experience on the web. +The vitals are composed of three important metrics to help user experience when using your web application. + +- Loading performance +- Interactivity +- And visual stability + +### Why web vitals + +The Core Web Vitals are one of [Google's Page Experience Signals](https://developers.google.com/search/docs/appearance/page-experience). A positive page experience naturally leads to better quality and better search engine rankings. These golden metrics help you understand which areas of your frontend application need optimization so your pages can rank higher than similar content. + +Existing browser measures, such as `Load` and `DOMContentLoaded` times, no longer accurately reflect user experience very well. +Relying on these load events does not give the correct metric to analyze critical performance bottlenecks that your page might have. Google's Web Vitals is a better measure of your page performance and its user experience. + +## Browser metrics + +The following Core Web Vital metrics are collected when using the k6 browser module. + +{{< docs/shared source="k6" lookup="browser/web-vital-core-metrics.md" version="" >}} + +Additionally, k6 also collects other Web Vital metrics. + +{{< docs/shared source="k6" lookup="browser/web-vital-other-metrics.md" version="" >}} + +To review the complete list of k6 metrics, refer to the [Metrics reference](https://grafana.com/docs/k6//using-k6/metrics/reference). + +## Understanding the browser metrics output + +When a browser test finishes, k6 reports a top-level overview of the aggregated browser metrics output. +The following snippet is an example: + +{{< admonition type="note" >}} + +As Google also recommends measuring the 75th percentile for each web vital metric, there will still be future tweaks to improve the summary output. + +{{< /admonition >}} + +```bash + browser_data_received.......: 2.6 kB 2.0 kB/s + browser_data_sent...........: 1.9 kB 1.5 kB/s + browser_http_req_duration...: avg=215.4ms min=124.9ms med=126.65ms max=394.64ms p(90)=341.04ms p(95)=367.84ms + browser_http_req_failed.....: 0.00% ✓ 0 ✗ 3 + browser_web_vital_cls.......: avg=0 min=0 med=0 max=0 p(90)=0 p(95)=0 + browser_web_vital_fcp.......: avg=344.15ms min=269.2ms med=344.15ms max=419.1ms p(90)=404.11ms p(95)=411.6ms + browser_web_vital_fid.......: avg=200µs min=200µs med=200µs max=200µs p(90)=200µs p(95)=200µs + browser_web_vital_inp.......: avg=8ms min=8ms med=8ms max=8ms p(90)=8ms p(95)=8ms + browser_web_vital_lcp.......: avg=419.1ms min=419.1ms med=419.1ms max=419.1ms p(90)=419.1ms p(95)=419.1ms + browser_web_vital_ttfb......: avg=322.4ms min=251ms med=322.4ms max=393.8ms p(90)=379.52ms p(95)=386.66ms +``` + +You can also visualize these results in different ways depending on your team's needs. For more information, check out our blog post on [visualizing k6 results](https://k6.io/blog/ways-to-visualize-k6-results/). + +## Set thresholds for your browser metrics + +The browser module can use all key k6 functionalities, such as [Thresholds](https://grafana.com/docs/k6//using-k6/thresholds). + +To set thresholds for your browser metrics: + +1. Add the metric you want to check. +1. Specify its threshold value. + +As the following example shows, you can also pass in different URLs if you're going to set a threshold for other pages, especially when your script contains page navigations. + +{{< admonition type="caution" >}} + +Currently, you can only use URLs to specify thresholds for different pages. If you use [Groups](https://grafana.com/docs/k6//using-k6/tags-and-groups/#groups), the metrics are not correctly grouped as described in [#721](https://github.com/grafana/xk6-browser/issues/721). + +{{< /admonition >}} + +{{< code >}} + +```javascript +export const options = { + thresholds: { + 'browser_web_vital_lcp': ['p(90) < 1000'], + 'browser_web_vital_inp{url:https://test.k6.io/}': ['p(90) < 80'], + 'browser_web_vital_inp{url:https://test.k6.io/my_messages.php}': ['p(90) < 100'], + }, +}; +``` + +{{< /code >}} + +When the test is run, you should see a similar output as the one below. + +```bash + browser_web_vital_inp..........................: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s + ✓ { url:https://test.k6.io/ }..................: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s + ✓ { url:https://test.k6.io/my_messages.php }...: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s +✓ browser_web_vital_lcp..........................: avg=460.1ms min=460.1ms med=460.1ms max=460.1ms p(90)=460.1ms p(95)=460.1ms + browser_web_vital_ttfb.........................: avg=339.3ms min=258.9ms med=339.3ms max=419.7ms p(90)=403.62ms p(95)=411.66ms +``` + +## Measure custom metrics + +When using the k6 browser `page.evaluate` function, you can call the [Performance API](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API) to measure the performance of web applications. For example, if you want to measure the time it takes for your users to complete actions, such as a search feature, you can use the [`performance.mark`](https://developer.mozilla.org/en-US/docs/Web/API/Performance/mark) method to add a timestamp in your browser's performance timeline. +Using the [`performance.measure`](https://developer.mozilla.org/en-US/docs/Web/API/Performance/measure) method, you can also measure the time difference between two performance markers. The time duration that `performance.measure` returns can be added as a custom metric in k6 browser using [Trends](https://grafana.com/docs/k6//javascript-api/k6-metrics/trend/). + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import { Trend } from 'k6/metrics'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +const myTrend = new Trend('total_action_time', true); + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/browser.php'); + await page.evaluate(() => window.performance.mark('page-visit')); + + await page.locator('#checkbox1').check(); + await page.locator('#counter-button').click(); + await page.locator('#text1').fill('This is a test'); + + await page.evaluate(() => window.performance.mark('action-completed')); + + // Get time difference between visiting the page and completing the actions + await page.evaluate(() => + window.performance.measure('total-action-time', 'page-visit', 'action-completed') + ); + + const totalActionTime = await page.evaluate( + () => + JSON.parse(JSON.stringify(window.performance.getEntriesByName('total-action-time')))[0] + .duration + ); + + myTrend.add(totalActionTime); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} + +After you run the test, you should see a similar output as the one below. + +```bash + iteration_duration..........: avg=1.06s min=1.06s med=1.06s max=1.06s p(90)=1.06s p(95)=1.06s + iterations..................: 1 0.70866/s + total_action_time.............: avg=295.3ms min=295.3ms med=295.3ms max=295.3ms p(90)=295.3ms p(95)=295.3ms +``` diff --git a/docs/sources/k6/v0.55.x/using-k6-browser/migrating-to-k6-v0-52.md b/docs/sources/k6/v0.55.x/using-k6-browser/migrating-to-k6-v0-52.md new file mode 100644 index 000000000..3210f7958 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6-browser/migrating-to-k6-v0-52.md @@ -0,0 +1,324 @@ +--- +aliases: + - ./migrating-to-k6-v0-46/ # docs/k6//using-k6-browser/migrating-to-k6-v0-46 +title: 'Migrating browser scripts to k6 v0.52' +description: 'A migration guide to ease the process of transitioning to the new k6 browser module version bundled with k6 v0.52' +weight: 05 +--- + +# Migrating browser scripts to k6 v0.52 + +This guide outlines the key changes you will need to make when moving your existing k6 experimental browser test scripts to the _latest_ [k6 browser module](https://grafana.com/docs/k6//javascript-api/k6-browser) version bundled with [k6 0.52](https://github.com/grafana/k6/releases/tag/v0.52.0). + +## Key changes + +In the latest release of k6, we have graduated k6 browser module out of experimental and is now under the import `k6/browser`. Migrating to this is a breaking change that will affect _all scripts_ that use the experimental k6 browser module. The breaking changes are: + +1. Converted most of the k6 browser module APIs to asynchronous (async) APIs. That means they will return a `promise` that will `resolve` to a value when the API call succeeds or `reject` when an error occurs. +2. A side effect of making this async changes is that you will need to use [the `check` utility function from jslib.k6.io](https://grafana.com/docs/k6//javascript-api/jslib/utils/check/) instead of working with the k6 [check](http://grafana.com/docs/k6//javascript-api/k6/check/) API. +3. Finally, it's also worth reiterating that the [group](http://grafana.com/docs/k6//javascript-api/k6/group/) API still doesn't work with async APIs. + +If you are interested in the rationale behind this change, refer to the [v0.51 release notes](https://github.com/grafana/k6/releases/tag/v0.51.0). + +{{< admonition type="note" >}} + +The experimental import (`k6/experimental/browser`) and the corresponding synchronous APIs will be supported up until the 23rd of September 2024. + +{{< /admonition >}} + +## Migrating to async + +To ensure your scripts work with the latest release of the k6 browser module, you must: + +1. Change the import from `k6/experimental/browser` to `k6/browser`. +2. Ensure that all the synchronous (sync) APIs that have been migrated over to async are handled appropriately. In most cases, you will only need to add `await` in front of the API call. + +For example, before: + +{{< code >}} + + + +```javascript +import { browser } from 'k6/experimental/browser'; + +... + +export default async function () { + const page = browser.newPage(); + ... +} +``` + +{{< /code >}} + +And now: + +{{< code >}} + + + +```javascript +import { browser } from 'k6/browser'; + +... + +export default async function () { + const page = await browser.newPage(); + ... +} +``` + +{{< /code >}} + +You might have already encountered async APIs when working with the browser module, such as [page.click](http://grafana.com/docs/k6//javascript-api/k6-browser/page/click/), so the use of `async` and `await` keywords might be familiar to you. + +Below is a screenshot of a comparison between a generic browser test in `v0.51` and `v0.52` to help visualize the change: + +![v0.51 & v0.52 script comparison](/media/docs/k6-oss/browser-v0.52-migration-script-comparison-3.png) + +### The affected APIs + +{{< collapse title="List of APIs to be migrated to async" >}} + +| Class | Method | +| -------------- | ----------------------- | +| Browser | closeContext | +| Browser | context | +| Browser | newContext | +| Browser | newPage | +| Browser | userAgent | +| BrowserContext | addCookies | +| BrowserContext | addInitScript | +| BrowserContext | browser | +| BrowserContext | clearCookies | +| BrowserContext | clearPermissions | +| BrowserContext | close | +| BrowserContext | cookies | +| BrowserContext | grantPermissions | +| BrowserContext | newPage | +| BrowserContext | setGeolocation | +| BrowserContext | setOffline | +| ElementHandle | $ | +| ElementHandle | $$ | +| ElementHandle | boundingBox | +| ElementHandle | check | +| ElementHandle | contentFrame | +| ElementHandle | dblclick | +| ElementHandle | dispatchEvent | +| ElementHandle | fill | +| ElementHandle | focus | +| ElementHandle | getAttribute | +| ElementHandle | hover | +| ElementHandle | innerHTML | +| ElementHandle | innerText | +| ElementHandle | inputValue | +| ElementHandle | isChecked | +| ElementHandle | isDisabled | +| ElementHandle | isEditable | +| ElementHandle | isEnabled | +| ElementHandle | isHidden | +| ElementHandle | isVisible | +| ElementHandle | ownerFrame | +| ElementHandle | press | +| ElementHandle | screenshot | +| ElementHandle | scrollIntoViewIfNeeded | +| ElementHandle | selectOption | +| ElementHandle | selectText | +| ElementHandle | setInputFiles | +| ElementHandle | tap | +| ElementHandle | textContent | +| ElementHandle | type | +| ElementHandle | uncheck | +| ElementHandle | waitForElementState | +| ElementHandle | waitForSelector | +| Frame | $ | +| Frame | $$ | +| Frame | check | +| Frame | content | +| Frame | dblclick | +| Frame | dispatchEvent | +| Frame | evaluate | +| Frame | evaluateHandle | +| Frame | fill | +| Frame | focus | +| Frame | frameElement | +| Frame | getAttribute | +| Frame | hover | +| Frame | innerHTML | +| Frame | innerText | +| Frame | inputValue | +| Frame | isChecked | +| Frame | isDisabled | +| Frame | isEditable | +| Frame | isEnabled | +| Frame | isHidden | +| Frame | isVisible | +| Frame | press | +| Frame | selectOption | +| Frame | setContent | +| Frame | setInputFiles | +| Frame | tap | +| Frame | textContent | +| Frame | title | +| Frame | type | +| Frame | uncheck | +| Frame | waitForLoadState | +| Frame | waitForSelector | +| Frame | waitForTimeout | +| JSHandle | asElement | +| JSHandle | dispose | +| JSHandle | evaluate | +| JSHandle | evaluateHandle | +| JSHandle | getProperties | +| JSHandle | jsonValue | +| Keyboard | down | +| Keyboard | insertText | +| Keyboard | press | +| Keyboard | type | +| Keyboard | up | +| Locator | check | +| Locator | clear | +| Locator | dblclick | +| Locator | dispatchEvent | +| Locator | fill | +| Locator | focus | +| Locator | getAttribute | +| Locator | hover | +| Locator | innerHTML | +| Locator | innerText | +| Locator | inputValue | +| Locator | isChecked | +| Locator | isDisabled | +| Locator | isEditable | +| Locator | isEnabled | +| Locator | isHidden | +| Locator | isVisible | +| Locator | press | +| Locator | selectOption | +| Locator | tap | +| Locator | textContent | +| Locator | type | +| Locator | uncheck | +| Locator | waitFor | +| Mouse | click | +| Mouse | dblclick | +| Mouse | down | +| Mouse | move | +| Mouse | up | +| Page | $ | +| Page | $$ | +| Page | bringToFront | +| Page | check | +| Page | close | +| Page | content | +| Page | dblclick | +| Page | dispatchEvent | +| Page | emulateMedia | +| Page | emulateVisionDeficiency | +| Page | evaluate | +| Page | evaluateHandle | +| Page | fill | +| Page | focus | +| Page | getAttribute | +| Page | hover | +| Page | innerHTML | +| Page | innerText | +| Page | inputValue | +| Page | isChecked | +| Page | isDisabled | +| Page | isEditable | +| Page | isEnabled | +| Page | isHidden | +| Page | isVisible | +| Page | on | +| Page | opener | +| Page | press | +| Page | reload | +| Page | screenshot | +| Page | selectOption | +| Page | setContent | +| Page | setExtraHTTPHeaders | +| Page | setInputFiles | +| Page | setViewportSize | +| Page | tap | +| Page | textContent | +| Page | throttleCPU | +| Page | throttleNetwork | +| Page | title | +| Page | type | +| Page | uncheck | +| Page | viewportSize | +| Page | waitForLoadState | +| Page | waitForSelector | +| Page | waitForTimeout | +| Request | allHeaders | +| Request | headerValue | +| Request | headersArray | +| Request | postData | +| Request | resourceType | +| Request | response | +| Request | size | +| Request | timing | +| Response | allHeaders | +| Response | body | +| Response | headerValue | +| Response | headerValues | +| Response | headersArray | +| Response | json | +| Response | securityDetails | +| Response | serverAddr | +| Response | size | +| Touchscreen | tap | + +{{< /collapse >}} + +## Working with k6 check + +The k6 `check` API will not `await` promises, so calling a function that returns a `Promise`, for example `locator.textContent()`, inside one of the predicates will not work. Instead, you can work with [the jslib.k6.io's `check` utility function](https://grafana.com/docs/k6/latest/javascript-api/jslib/utils/check/). + +For example, before: + +{{< code >}} + + + +```javascript +check(page.locator('h2'), { + header: lo => lo.textContent() == 'Welcome, admin!', +}); +``` + +{{< /code >}} + +And now: + +{{< code >}} + + + +```javascript +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; + +// ... + +await check(page.locator('h2'), { + 'header': async lo => await lo.textContent() === 'Welcome, admin!' +}); +``` + +{{< /code >}} + +## Groups + +A note on [groups](https://grafana.com/docs/k6//using-k6/tags-and-groups/#groups), they don't work with async APIs either, there is no workaround as of yet. Here's the [GitHub issue](https://github.com/grafana/k6/issues/2728) that you can follow to keep up-to-date with relevant news on a group API that works with async APIs. + +## Migrating from k6 v0.45 or below + +If you use k6 v0.45 or below, first review [how to migrate to v0.46](https://grafana.com/docs/k6/v0.51.x/migrating-to-k6-v0-46). This guide details the breaking changes introduced in v0.46 for the `browser` APIs and provides instructions on how to migrate to them. + +# Where to get extra help + +With the release of k6 `v0.52`, you'll also be able to find the latest type definitions and updates to the [k6 browser documentation](https://grafana.com/docs/k6//javascript-api/k6-browser). Refer to [Configure k6 IntelliSense](http://grafana.com/docs/k6//set-up/configure-k6-intellisense/) for details on how to work with the type definition file in your project. + +For all the details, review the CHANGELOG for [k6 version 0.52](https://github.com/grafana/k6/releases/tag/v0.52.0). You can also ask questions in [the community forum](https://community.grafana.com/c/grafana-k6/k6-browser/79) if you need help. diff --git a/docs/sources/k6/v0.55.x/using-k6-browser/options.md b/docs/sources/k6/v0.55.x/using-k6-browser/options.md new file mode 100644 index 000000000..7aff80673 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6-browser/options.md @@ -0,0 +1,89 @@ +--- +title: 'Browser options' +description: 'An overview of the different options you can use to customize the browser module behavior when running browser tests.' +weight: 04 +--- + +# Browser options + +To enable browser testing, add the `browser` configuration within the `options` property of the [Scenario options](https://grafana.com/docs/k6//using-k6/scenarios/#options). + +```javascript +export const options = { + scenarios: { + foo: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; +``` + +## Script options + +| Option | Description | +| ------------------------- | ------------------------------------------------------------------- | +| type(required) | Name of the browser running the test. Options include `'chromium'`. | + +## Environment variable options + +You can customize the behavior of the browser module by passing environment variables. + +{{< admonition type="note" >}} + +Customizing browser options via environment variables is unsupported when [running browser tests in Grafana Cloud k6](https://grafana.com/docs/grafana-cloud/testing/k6/get-started/run-your-first-browser-tests/). + +{{< /admonition >}} + +{{< docs/shared source="k6" lookup="browser/env-var-options.md" version="" >}} + +### Default arguments + +List of default arguments included when launching the browser process. You can pass one or more of the arguments to the `K6_BROWSER_IGNORE_DEFAULT_ARGS` environment variable when starting a test for the ones you want to ignore. + +{{< admonition type="note" >}} + +The starting '--' have been omitted from the argument names in these lists. + +{{< /admonition >}} + +| Argument | Value | Description | +| -------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| disable-background-networking | `true` | Disables several subsystems which run network requests in the background. This is used during network performance testing to avoid measurement noise. | +| enable-features | NetworkService,
NetworkServiceInProcess | Comma-separated list of feature names to enable. | +| disable-background-timer-throttling | `true` | Disables task throttling of timer tasks from background pages. | +| disable-backgrounding-occluded-windows | `true` | Disables backgrounding renders for occluded windows. Done for tests to avoid nondeterministic behavior. | +| disable-breakpad | `true` | Disables the crash reporting. | +| disable-component-extensions
-with-background-pages | `true` | Disables default component extensions with background pages. Useful for performance tests where these pages may interfere with results. | +| disable-default-apps | `true` | Disables the installation of default apps on the first run. This is used during automated testing. | +| disable-dev-shm-usage | `true` | The /dev/shm partition is too small in certain VM environments, causing Chrome to fail or crash. This flag provides a work-around for this issue (a temporary directory will always be used to create anonymous shared memory files). | +| disable-extensions | `true` | Disables extensions. | +| disable-features | ImprovedCookieControls,
LazyFrameLoading,
GlobalMediaControls,
DestroyProfileOnBrowserClose,
MediaRouter,
AcceptCHFrame | Comma-separated list of feature names to disable. | +| disable-hang-monitor | `true` | Suppresses hang monitor dialogs in renderer processes. This may allow slow unload handlers on a page to prevent the tab from closing, but the Task Manager can be used to terminate the offending process in this case. | +| disable-ipc-flooding-protection | `true` | Disables the IPC flooding protection. It is activated by default. Some javascript functions can be used to flood the browser process with IPC. This protection limits the rate at which they can be used. | +| disable-popup-blocking | `true` | Disables pop-up blocking. | +| disable-prompt-on-repost | `true` | Usually, when the user attempts to navigate to a page that was the result of a post request, the browser prompts to make sure that's the intention of the user. This switch may be used to disable that check during automated testing. | +| disable-renderer-backgrounding | `true` | Prevents renderer process backgrounding when set. | +| force-color-profile | `srgb` | Forces all monitors to be treated as though they have the specified color profile. Accepted values are "srgb" and "generic-rgb" (currently used by Mac layout tests) and "color-spin-gamma24" (used by layout tests). | +| metrics-recording-only | `true` | Enables the recording of metrics reports but disables reporting. This executes all the code that a normal client would use for reporting, except the report is dropped rather than sent to the server. This is useful for finding issues in the metrics code during UI and performance tests. | +| no-first-run | `true` | Skips the "First Run" tasks, whether or not it's the first run, and the "What's New" page. This does not drop the "First Run" sentinel and thus doesn't prevent "First Run" from occurring the next time Chromium is launched without this flag. It also does not update the last "What's New" milestone, so it does not prevent "What's New" from occurring the next time Chromium is launched without this flag. | +| enable-automation | `true` | Enables indication that the browser is controlled by automation. | +| password-store | `basic` | Specifies which encryption storage backend to use. The possible values are kwallet, kwallet5, gnome, gnome-keyring, gnome-libsecret, and basic. Any other value will lead to Chromium detecting the best backend automatically. | +| use-mock-keychain | `true` | Uses mock keychain on Mac to prevent the blocking permissions dialog about: "Chrome wants to use your confidential information stored in your keychain." | +| no-service-autorun | `true` | Disables the service process from adding itself as an autorun process. This does not delete existing autorun registrations, it just prevents the service from registering a new one. | +| no-startup-window | `true` | Does not automatically open a browser window on startup (used when launching Chrome for the purpose of hosting background apps). | +| no-default-browser-check | `true` | Disables the default browser check. Useful for UI/browser tests where we want to avoid having the default browser info-bar displayed. | +| headless | `true`/`false` | Run in headless mode, i.e., without a UI or display server dependencies. Set by `K6_BROWSER_HEADLESS` environment variable (default true). | +| window-size | `800,600` | Sets the initial window size. Provided as string in the format "800,600". | + +Additionally, the following arguments are set in headless mode (when `K6_BROWSER_HEADLESS` is `true`, which is the default option): + +| Argument | Value | Description | +| --------------- | ------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| hide-scrollbars | `true` | Prevents creating scrollbars for web content. Useful for taking consistent screenshots. | +| mute-audio | `true` | Mutes audio sent to the audio device so it is not audible during automated testing. | +| blink-settings | primaryHoverType=2,availableHoverTypes=2,
primaryPointerType=4,availablePointerTypes=4 | Sets blink settings. Format is [=],[=],... The names are declared in [settings.json5](https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/frame/settings.json5) from chromium project. For boolean type, use "true", "false", or omit '=' part to set to true. For enum type, use the int value of the enum value. | diff --git a/docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/_index.md b/docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/_index.md new file mode 100644 index 000000000..c8136cce6 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/_index.md @@ -0,0 +1,14 @@ +--- +title: 'Recommended practices' +description: 'A list of different examples and recommended practices when working with the k6 browser module' +weight: 100 +--- + +# Recommended practices + +This section presents some examples and recommended practices when working with the `k6 browser` module to leverage browser automation as part of your k6 tests. + +- [Hybrid approach to performance](https://grafana.com/docs/k6//using-k6-browser/recommended-practices/hybrid-approach-to-performance) +- [Page object model pattern](https://grafana.com/docs/k6//using-k6-browser/recommended-practices/page-object-model-pattern) +- [Selecting elements](https://grafana.com/docs/k6//using-k6-browser/recommended-practices/selecting-elements) +- [Simulate user input delay](https://grafana.com/docs/k6//using-k6-browser/recommended-practices/simulate-user-input-delay) diff --git a/docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/hybrid-approach-to-performance.md b/docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/hybrid-approach-to-performance.md new file mode 100644 index 000000000..f046d7b26 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/hybrid-approach-to-performance.md @@ -0,0 +1,213 @@ +--- +title: 'Hybrid approach to performance' +heading: 'Hybrid performance with k6 browser' +head_title: 'Hybrid performance with k6 browser' +description: 'An example on how to implement a hybrid approach to performance with k6 browser' +weight: 01 +--- + +# Hybrid performance with k6 browser + +An alternative approach to [browser-based load testing](https://grafana.com/docs/k6//testing-guides/load-testing-websites/#browser-based-load-testing) that's much less resource-intensive is combining a small number of virtual users for a browser test with a large number of virtual users for a protocol-level test. + +You can achieve hybrid performance in multiple ways, often by using different tools. To simplify the developer experience, you can combine k6 browser with core k6 features to write hybrid tests in a single script. + +## Browser and HTTP test + +The code below shows an example of combining a browser and HTTP test in a single script. While the script exposes the backend to the typical load, it also checks the frontend for any unexpected issues. It also defines thresholds to check both HTTP and browser metrics against pre-defined SLOs. + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; +import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; +import http from 'k6/http'; + +const BASE_URL = __ENV.BASE_URL || 'https://quickpizza.grafana.com'; + +export const options = { + scenarios: { + load: { + exec: 'getPizza', + executor: 'ramping-vus', + stages: [ + { duration: '5s', target: 5 }, + { duration: '10s', target: 5 }, + { duration: '5s', target: 0 }, + ], + startTime: '10s', + }, + browser: { + exec: 'checkFrontend', + executor: 'constant-vus', + vus: 1, + duration: '30s', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, + thresholds: { + http_req_failed: ['rate<0.01'], + http_req_duration: ['p(95)<500', 'p(99)<1000'], + browser_web_vital_fcp: ['p(95) < 1000'], + browser_web_vital_lcp: ['p(95) < 2000'], + }, +}; + +export function getPizza() { + const restrictions = { + maxCaloriesPerSlice: 500, + mustBeVegetarian: false, + excludedIngredients: ['pepperoni'], + excludedTools: ['knife'], + maxNumberOfToppings: 6, + minNumberOfToppings: 2, + }; + + const res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), { + headers: { + 'Content-Type': 'application/json', + 'X-User-ID': randomIntBetween(1, 30000), + }, + }); + + check(res, { + 'status is 200': (res) => res.status === 200, + }); +} + +export async function checkFrontend() { + const page = await browser.newPage(); + + try { + await page.goto(BASE_URL); + + await check(page.locator('h1'), { + 'header': async lo => await lo.textContent() == 'Looking to break out of your pizza routine?' + }); + + await Promise.all([ + page.locator('//button[. = "Pizza, Please!"]').click(), + page.waitForTimeout(500), + ]); + await page.screenshot({ path: `screenshots/${__ITER}.png` }); + + await check(page.locator('div#recommendations'), { + 'recommendation': async lo => await lo.textContent() != '', + }); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} + +If you save that script to a local file named `test.js`, you can run it with: + +```bash +k6 run test.js +``` + +The script also includes a common best practice by defining a `BASE_URL` variable, and using the [environment variable](https://grafana.com/docs/k6//using-k6/environment-variables/) value `__ENV.BASE_URL` if it exists. That's useful if you want to use the same script for multiple environments, such as staging and production, and you could pass that value to your script with the command: + +```bash +k6 run -e BASE_URL=https://quickpizza.grafana.com test.js +``` + +## Browser and failure injection test + +You can also run a browser test together with a failure injection test by using the [xk6-disruptor](https://github.com/grafana/xk6-disruptor) extension. This approach lets you find issues in your frontend if any services it depends on are suddenly injected with failures, such as delays or server errors. + +The following code shows an example of how you could use the xk6-disruptor extension to introduce faults to a Kubernetes service. At the same time, the `browser` scenario runs to ensure the frontend application is free of any unexpected errors that may not have been handled properly. + +To find out more information about injecting faults to your service, check out the [Get started with xk6-disruptor guide](https://grafana.com/docs/k6//testing-guides/injecting-faults-with-xk6-disruptor/first-steps/). + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; +import { ServiceDisruptor } from 'k6/x/disruptor'; + +const BASE_URL = __ENV.BASE_URL; + +export const options = { + scenarios: { + disrupt: { + executor: 'shared-iterations', + iterations: 1, + vus: 1, + exec: 'disrupt', + }, + browser: { + executor: 'constant-vus', + vus: 1, + duration: '10s', + startTime: '10s', + exec: 'browser', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, + thresholds: { + browser_web_vital_fcp: ['p(95) < 1000'], + browser_web_vital_lcp: ['p(95) < 2000'], + }, +}; + +// Add faults to the service by introducing a delay of 1s and 503 errors to 10% of the requests. +const fault = { + averageDelay: '1000ms', + errorRate: 0.1, + errorCode: 503, +}; + +export function disrupt() { + const disruptor = new ServiceDisruptor('pizza-info', 'pizza-ns'); + const targets = disruptor.targets(); + if (targets.length == 0) { + throw new Error('expected list to have one target'); + } + + disruptor.injectHTTPFaults(fault, '20s'); +} + +export async function checkFrontend() { + const page = await browser.newPage(); + + try { + await page.goto(BASE_URL); + await check(page.locator('h1'), { + 'header': async lo => await lo.textContent() == 'Looking to break out of your pizza routine?' + }); + + await Promise.all([ + page.locator('//button[. = "Pizza, Please!"]').click(), + page.waitForTimeout(500), + ]); + await page.screenshot({ path: `screenshots/${__ITER}.png` }); + + await check(page.locator('div#recommendations'), { + recommendation: async lo => await lo.textContent() != '', + }); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} + +## Recommended practices + +- **Start small**. Start with a small number of browser-based virtual users. A good starting point is to have 10% virtual users or less to monitor the user experience for your end-users, while the script emulates around 90% of traffic from the protocol level. +- **Combine browser test with different load testing types**. To fully understand the impact of different traffic patterns on your end-user experience, experiment with running your browser test with different [load testing types](https://grafana.com/docs/k6//testing-guides/test-types/). +- **Focus on high-risk user journeys as a start**. Identify the high-risk user journeys first so you can start monitoring the web performance metrics for them while your backend applications are being exposed to high traffic or service faults. diff --git a/docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/page-object-model-pattern.md b/docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/page-object-model-pattern.md new file mode 100644 index 000000000..150ea0f00 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/page-object-model-pattern.md @@ -0,0 +1,97 @@ +--- +title: 'Page object model' +heading: 'Page object model with k6 browser' +head_title: 'Page object model with k6 browser' +description: 'An example on how to implement page object model design pattern with k6 browser' +weight: 02 +--- + +# Page object model + +When working with large test suites, a popular design pattern to improve your code’s maintainability and readability is the [page object model](https://martinfowler.com/bliki/PageObject.html). + +A page object commonly represents an HTML page or significant elements/components within a page, such as a header or a footer. It is a form of encapsulation that hides the details of the UI structure from other places, such as your test files. Through page object models, any changes you need to make on a specific page or element within a page are constrained into a single place, resulting in ease of maintenance and avoiding code duplication. + +Since k6 browser aims to provide rough compatibility with the Playwright API, you can leverage any existing page objects you have and easily re-use them with your k6 browser tests. + +## Implementation + +Let's take an example of a website with a booking form added to the homepage. Imagine you want to write a test that checks that a user can fill out the booking form successfully. + +To model a page object for the homepage, we've created a page object class called `homepage.js`. Different locators are created inside the constructor so that when the homepage class is instantiated, the page locator elements are ready to be used. + +The `homepage.js` class also contains different methods for: + +- Navigating to the homepage +- Submitting the form +- Getting the verification message + +When locators need to be updated or other specific changes related to the homepage are made, you only need to update the `homepage.js` class. + +{{< code >}} + +```javascript +import { bookingData } from '../data/booking-data.js'; + +export class Homepage { + constructor(page) { + this.page = page; + this.nameField = page.locator('[data-testid="ContactName"]'); + this.emailField = page.locator('[data-testid="ContactEmail"]'); + this.phoneField = page.locator('[data-testid="ContactPhone"]'); + this.subjectField = page.locator('[data-testid="ContactSubject"]'); + this.descField = page.locator('[data-testid="ContactDescription"]'); + this.submitButton = page.locator('#submitContact'); + this.verificationMessage = page.locator('.row.contact h2'); + } + + async goto() { + await this.page.goto('https://myexamplewebsite/'); + } + + async submitForm() { + const { name, email, phone, subject, description } = bookingData; + + this.nameField.type(name); + this.emailField.type(email); + this.phoneField.type(phone); + this.subjectField.type(subject); + this.descField.type(description); + await this.submitButton.click(); + } + + async getVerificationMessage() { + return this.verificationMessage.innerText(); + } +} +``` + +{{< /code >}} + +You can import the `Homepage` class within your test class and invoke the methods you need. This makes the code easier to understand and enforces the separation between your test and business logic. + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import { expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.0/index.js'; + +import { Homepage } from '../pages/homepage.js'; +import { bookingData } from '../data/booking-data.js'; + +export default async function () { + const page = await browser.newPage(); + + const { name } = bookingData; + + const homepage = new Homepage(page); + await homepage.goto(); + await homepage.submitForm(); + + expect(await homepage.getVerificationMessage()).to.contain(name); + + await page.close(); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/select-elements.md b/docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/select-elements.md new file mode 100644 index 000000000..422b24a0a --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/select-elements.md @@ -0,0 +1,33 @@ +--- +aliases: + - ./selecting-elements # docs/k6//using-k6-browser/recommended-practices/selecting-elements +title: 'Select elements' +description: 'A guide on how to select elements with the browser module.' +weight: 03 +--- + +# Select elements + +Selectors are strings that represents a specific DOM element on the page. When writing browser-level tests, it's recommended to use selectors that are robust to avoid test flakiness when the DOM structure changes. + +Currently, the browser module supports the standard **CSS and XPath selectors**. + +{{< admonition type="note" >}} + +Text-based selectors are currently not supported. This will be supported in future releases. + +{{< /admonition >}} + +## Recommended practices + +The selectors that you choose should not be tightly coupled to any behaviour or styling changes. If your application is prone to changes frequently, it's recommended to use user-facing attributes or custom data attributes as these are not tightly coupled to the element. + +| Selector | Notes | +| ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| ✅ `page.locator('[aria-label="Login"]')` | User-facing attributes such as ARIA labels are rarely changed so these are great candidates. | +| ✅ `page.locator('[data-test="login"]')` | Custom data attributes are not tightly coupled to the element. | +| ✅ `page.locator('//button[text()="Submit"]')` | Text selectors are also great as text content rarely changes. While we don't support text-based selectors as of yet, xpath can be used as a workaround. | +| ⚠️ `page.locator('#login-btn')` | Selecting an element via its ID is also recommended if the ID doesn't change. | +| ⚠️ `page.locator('.login-btn')` | Selecting an element via its class name should be kept to a minimum as class names can be duplicated. | +| ❌ `page.locator('button')` | Generic elements are not recommended because this has no context. | +| ❌ `page.locator('/html[1]/body[1]/main[1]` | Absolute paths are not recommended as these are tightly coupled to the DOM structure. | diff --git a/docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/simulate-user-input-delay.md b/docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/simulate-user-input-delay.md new file mode 100644 index 000000000..7c606f2e4 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6-browser/recommended-practices/simulate-user-input-delay.md @@ -0,0 +1,317 @@ +--- +title: 'Simulate user input delay' +description: 'A guide on how to simulate user input delay.' +weight: 04 +--- + +# Simulate user input delay + +On this page, you'll learn how to best work with `sleep` in `k6` and the various `wait*` prepended methods available in `k6/browser` to simulate user input delay, wait for navigations, and wait for element state changes. By the end of this page, you should be able to successfully use the correct API where necessary. + +{{< admonition type="note" >}} + +While using the `sleep` or `page.waitForTimeout` functions to wait for element state changes may seem helpful, it's best to avoid them to prevent flakey tests. Instead, it's better to use the other `wait*` prepended methods listed on this page. + +{{< /admonition >}} + +## What is `sleep`? + +[sleep](https://grafana.com/docs/k6//javascript-api/k6/sleep) is a first class function built into k6. It's main use is to _"suspend VU execution for the specified duration"_ which is most useful when you want to simulate user input delay, such as: + +- Navigating to a page. +- Sleeping for one second to simulate a user looking for a specific element on the page. +- Clicking on the element. + +{{< admonition type="warning" >}} + +`sleep` is a synchronous function that blocks the JavaScript event loop, which means that all asynchronous work will also be suspended until `sleep` completes. + +The browser module predominantly provides asynchronous APIs, so it's best to avoid working with `sleep`. Instead, _use the [page.waitForTimeout](#pagewaitfortimeout) function_. + +{{< /admonition >}} + +## What is `wait*`? + +In the browser modules there are various asynchronous APIs that can be used to wait for certain states: + +| Method | Description | +| ---------------------------------------------------------- | --------------------------------------------------------------------------- | +| [browserContext.waitForEvent](#browsercontextwaitforevent) | Waits for the selected event to fire and returns its value. | +| [page.waitForFunction](#pagewaitforfunction) | Waits for the given function to return a truthy value. | +| [page.waitForLoadState](#pagewaitforloadstate) | Waits for the specified page life cycle event. | +| [page.waitForNavigation](#pagewaitfornavigation) | Waits for the navigation to complete after one starts. | +| [page.waitForTimeout](#pagewaitfortimeout) | Waits the given time. _Use this instead of `sleep` in your frontend tests_. | +| [locator.waitFor](#locatorwaitfor) | Wait for the element to be in a particular state. | + +### browserContext.waitForEvent + +[browserContext.waitForEvent](https://grafana.com/docs/k6//javascript-api/k6-browser/browsercontext/waitforevent) is used when waiting for specific events to fire, which then returns the element associated to that event. At the moment the only event that is available to use is `page`. It can be useful when you want to track and retrieve a new tab opening. When working with a predicate function, it can be used to wait for a specific page to open before returning `true`. + +{{< code >}} + + + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/'); + + await page.keyboard.down('ControlOrMeta'); + + // Open the link in a new tab with the help of the meta key. + // Wait for the new page to be created. + const browserContext = browser.context(); + const [newTab] = await Promise.all([ + browserContext.waitForEvent('page'), + page.locator('a[href="/my_messages.php"]').click(), + ]); + + await page.keyboard.up('ControlOrMeta'); + + // Wait for the new page (tab) to load. + await newTab.waitForLoadState('load'); + + // Take screenshots of each page. + await page.screenshot({ path: `screenshot-page.png` }); + await newTab.screenshot({ path: `screenshot-newTab.png` }); + + await newTab.close(); + await page.close(); +} +``` + +{{< /code >}} + +### page.waitForFunction + +[page.waitForFunction](https://grafana.com/docs/k6//javascript-api/k6-browser/page/waitforfunction) is useful when you want more control over when a test progresses with a javascript function that returns true when a condition (or many conditions) is met. It can be used to poll for changes in the DOM or non-DOM elements and variables. + +{{< code >}} + + + +```javascript +import { browser } from 'k6/browser'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + // Setting up the example that will mutate the h1 element by setting the + // h1 elements text value to "Hello". + await page.evaluate(() => { + setTimeout(() => { + const el = document.createElement('h1'); + el.innerHTML = 'Hello'; + document.body.appendChild(el); + }, 1000); + }); + + // Waiting until the h1 element has mutated. + const ok = await page.waitForFunction("document.querySelector('h1')", { + polling: 'mutation', + timeout: 2000, + }); + + await check(ok, { + 'waitForFunction successfully resolved': async (ok) => (await ok.innerHTML()) == 'Hello', + }); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} + +### page.waitForLoadState + +[page.waitForLoadState](https://grafana.com/docs/k6//javascript-api/k6-browser/page/waitforloadstate) is useful when there’s no explicit navigation, but you need to wait for the page or network to settle. This is mainly used when working with single-page applications or when no full page reloads happen. + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + // Goto a SPA + await page.goto(''); + + // ... perform some actions that reload part of the page. + + // waits for the default `load` event. + await page.waitForLoadState(); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} + +### page.waitForNavigation + +[page.waitForNavigation](https://grafana.com/docs/k6//javascript-api/k6-browser/page/waitfornavigation) is a very useful API when performing other actions that could start a page navigation, and they don't automatically wait for the navigation to end. Usually, you'll find it in our examples with a `click` API call. Note that [goto](https://grafana.com/docs/k6//javascript-api/k6-browser/page/goto) is an example of an API that _doesn't_ require `waitForNavigation` since it will automatically wait for the navigation to complete before returning. + +It's important to call this in a [Promise.all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) along with the API that will cause the navigation to start. + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/my_messages.php'); + + await page.locator('input[name="login"]').type('admin'); + await page.locator('input[name="password"]').type('123'); + + const submitButton = page.locator('input[type="submit"]'); + + // The click action will start a navigation, and the waitForNavigation + // will help the test wait until the navigation completes. + await Promise.all([page.waitForNavigation(), submitButton.click()]); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} + +### locator.waitFor + +[locator.waitFor](https://grafana.com/docs/k6//javascript-api/k6-browser/locator/waitfor/) will wait until the element meets the waiting criteria. It's useful when dealing with dynamic websites where elements may take time to appear or change state. For example, if elements load after some delay due to async calls, or because of slow JavaScript execution. + +{{< code >}} + +```js +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + await page.goto('https://test.k6.io/browser.php'); + const text = page.locator('#input-text-hidden'); + await text.waitFor({ + state: 'hidden', + }); +} +``` + +{{< /code >}} + +### page.waitForTimeout + +[page.waitForTimeout](https://grafana.com/docs/k6//javascript-api/k6-browser/page/waitfortimeout) will wait the given amount of time. It's functionally the same as k6's [sleep](#whatissleep), but it's asynchronous, which means it will not block the event loop and allows the background tasks to continue to be worked on. + +{{< code >}} + +```js +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + browser: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io'); + + // Slow the test down to mimic a user looking for the element on the page. + await page.waitForTimeout(1000); + + // ... perform the next action + } finally { + await page.close(); + } +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/using-k6-browser/running-browser-tests.md b/docs/sources/k6/v0.55.x/using-k6-browser/running-browser-tests.md new file mode 100644 index 000000000..84f1cbad9 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6-browser/running-browser-tests.md @@ -0,0 +1,356 @@ +--- +title: 'Running browser tests' +description: 'Follow along to learn how to run a browser test, interact with elements on the page, wait for page navigation, write assertions and run both browser-level and protocol-level tests in a single script.' +weight: 02 +--- + +# Running browser tests + +Follow along to learn how to: + +1. Run a test +2. Interact with elements on your webpage +3. Wait for page navigation +4. Run both browser-level and protocol-level tests in a single script + +## Run a test + +To run a simple local script: + +1. Copy the following code, paste it into your favorite editor, and save it as `script.js`. + + Note that providing an `executor` and setting the `browser` scenario option's `type` to `chromium` is mandatory. Please see the [options](https://grafana.com/docs/k6//using-k6/k6-options) and [scenarios](https://grafana.com/docs/k6//using-k6/scenarios) documentation for more information. + + {{< code >}} + + ```javascript + import { browser } from 'k6/browser'; + + export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, + thresholds: { + checks: ['rate==1.0'], + }, + }; + + export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/'); + await page.screenshot({ path: 'screenshots/screenshot.png' }); + } finally { + await page.close(); + } + } + ``` + + {{< /code >}} + + The preceding code imports the `browser` [the browser module](https://grafana.com/docs/k6//javascript-api/k6-browser)), and uses its `newPage` method to open a new page. + + After getting the page, you can interact with it using the [Page](https://grafana.com/docs/k6//javascript-api/k6-browser/page) methods. This example visits a test URL and takes a screenshot of the page. + + Subsequently, the page is closed. This allows for the freeing up of allocated resources and enables the accurate calculation of [Web Vital metrics](https://grafana.com/docs/k6//using-k6-browser/metrics). + + {{< admonition type="note" >}} + + Starting from v0.52.0 the browser module API has been converted to an asynchronous API. That means that most of the methods now return promises. Refer to the [migration guide](https://grafana.com/docs/k6//using-k6-browser/migrating-to-k6-v0-52/) to learn more about the changes and how to update your scripts. + + {{< /admonition >}} + +1. Then, run the test on your terminal with this command: + + {{< code >}} + + ```bash + $ k6 run script.js + ``` + + ```docker + # WARNING! + # The grafana/k6:master-with-browser image launches a Chrome browser by setting the + # 'no-sandbox' argument. Only use it with trustworthy websites. + # + # As an alternative, you can use a Docker SECCOMP profile instead, and overwrite the + # Chrome arguments to not use 'no-sandbox' such as: + # docker container run --rm -i -e K6_BROWSER_ARGS='' --security-opt seccomp=$(pwd)/chrome.json grafana/k6:master-with-browser run - k6 run script.js + ``` + + ```powershell + PS C:\k6> k6 run script.js + ``` + + {{< /code >}} + + You can also use [the browser module options](https://grafana.com/docs/k6//using-k6-browser/options) to customize the launching of a browser process. For instance, you can start a headful browser using the previous test script with this command. + + {{< code >}} + + ```bash + $ K6_BROWSER_HEADLESS=false k6 run script.js + ``` + + ```docker + # WARNING! + # The grafana/k6:master-with-browser image launches a Chrome browser by setting the + # 'no-sandbox' argument. Only use it with trustworthy websites. + # + # As an alternative, you can use a Docker SECCOMP profile instead, and overwrite the + # Chrome arguments to not use 'no-sandbox' such as: + # docker container run --rm -i -e K6_BROWSER_ARGS='' --security-opt seccomp=$(pwd)/chrome.json grafana/k6:master-with-browser run - set "K6_BROWSER_HEADLESS=false" && k6 run script.js + ``` + + ```powershell + PS C:\k6> $env:K6_BROWSER_HEADLESS=false ; k6 run script.js + ``` + + {{< /code >}} + + {{< admonition type="note" >}} + + When using Docker to run k6 browser tests, make sure you have pulled the correct image with Chromium built-in. See [k6 Installation via Docker](https://grafana.com/docs/k6//set-up/install-k6#docker) for more information. + + {{< /admonition >}} + +1. Optional step: running browser tests in Docker on Mac computers with Apple Silicon: + + 1. Make sure you’re running [the latest Docker](https://docs.docker.com/engine/install/) version. + + 2. Update [Rosetta]() and export an environment variable with the following: + + ```bash + $ softwareupdate --install-rosetta + $ export DOCKER_DEFAULT_PLATFORM=linux/amd64 + ``` + + 3. Select VirtuoFS in **Settings** > **General** > **VirtuoFS**. + + 4. Enable the Rosetta emulation in **Settings** > **Features in development** > **Use Rosetta for x86/amd64 emulation on Apple Silicon**. + + 5. Restart Docker. + + 6. Run the browser image with the following command (adds the `--platform` flag): + + ```bash + $ docker run --rm -i --platform linux/amd64 -v $(pwd):/home/k6/screenshots -e K6_BROWSER_HEADLESS=false grafana/k6:master-with-browser run - /javascript-api/k6-browser/locator) object, which you can later use to interact with the element. + +To find out which selectors the browser module supports, check out [Selecting Elements](https://grafana.com/docs/k6//using-k6-browser/recommended-practices/selecting-elements). + +{{< admonition type="note" >}} + +You can also use `page.$()` instead of `page.locator()`. You can find the differences between `page.locator()` and `page.$` in the [Locator API documentation](https://grafana.com/docs/k6//javascript-api/k6-browser/locator). + +{{< /admonition >}} + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, + thresholds: { + checks: ['rate==1.0'], + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/my_messages.php'); + + // Enter login credentials + await page.locator('input[name="login"]').type('admin'); + await page.locator('input[name="password"]').type('123'); + + await page.screenshot({ path: 'screenshots/screenshot.png' }); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} + +The preceding code creates and returns a Locator object with the selectors for both login and password passed as arguments. + +Within the Locator API, various methods such as `type()` can be used to interact with the elements. The `type()` method types a text to an input field. + +## Asynchronous operations + +Since many browser operations happen asynchronously, and to follow the Playwright API more closely, we are working on migrating most of the browser module methods to be asynchronous as well. + +At the moment, methods such as `page.goto()`, `page.waitForNavigation()` and `Element.click()` return [JavaScript promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises), and scripts must be written to handle this properly. + +To avoid timing errors or other race conditions in your script, if you have actions that load up a different page, you need to make sure that you wait for that action to finish before continuing. + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, + thresholds: { + checks: ['rate==1.0'], + }, +}; + +export default async function () { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/my_messages.php'); + + await page.locator('input[name="login"]').type('admin'); + await page.locator('input[name="password"]').type('123'); + + const submitButton = page.locator('input[type="submit"]'); + + await Promise.all([ + page.waitForNavigation(), + submitButton.click(), + ]); + + await check(page.locator('h2'), { + 'header': async lo => await lo.textContent() == 'Welcome, admin!' + }); + } finally { + await page.close(); + } +} +``` + +{{< /code >}} + +The preceding code uses `Promise.all([])` to wait for the two promises to be resolved before continuing. Since clicking the submit button causes page navigation, `page.waitForNavigation()` is needed because the page won't be ready until the navigation completes. This is required because there can be a race condition if these two actions don't happen simultaneously. + +Then, you can use [`check`](https://grafana.com/docs/k6//javascript-api/k6/check) from the k6 API to assert the text content of a specific element. Finally, you close the page and the browser. + +## Run both browser-level and protocol-level tests in a single script + +The real power of the browser module shines when it’s combined with the existing features of k6. A common scenario that you can try is to mix a smaller subset of browser-level tests with a larger protocol-level test which can simulate how your website responds to various performance events. This approach is what we refer to as [hybrid load testing](https://grafana.com/docs/k6//testing-guides/load-testing-websites#hybrid-load-testing) and provides advantages such as: + +- testing real user flows on the frontend while generating a higher load in the backend +- measuring backend and frontend performance in the same test execution +- increased collaboration between backend and frontend teams since the same tool can be used + +To run a browser-level and protocol-level test concurrently, you can use [scenarios](https://grafana.com/docs/k6//using-k6/scenarios). + +{{< admonition type="note" >}} + +Keep in mind that there is an additional performance overhead when it comes to spinning up a browser VU and that the resource usage will depend on the system under test. + +{{< /admonition >}} + +{{< code >}} + +```javascript +import { browser } from 'k6/browser'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; +import http from 'k6/http'; + +export const options = { + scenarios: { + browser: { + executor: 'constant-vus', + exec: 'browserTest', + vus: 1, + duration: '10s', + options: { + browser: { + type: 'chromium', + }, + }, + }, + news: { + executor: 'constant-vus', + exec: 'news', + vus: 20, + duration: '1m', + }, + }, +}; + +export async function browserTest() { + const page = await browser.newPage(); + + try { + await page.goto('https://test.k6.io/browser.php'); + + await page.locator('#checkbox1').check(); + + await check(page.locator('#checkbox-info-display'), { + 'checkbox is checked': async lo => + await lo.textContent() === 'Thanks for checking the box' + }); + } finally { + await page.close(); + } +} + +export function news() { + const res = http.get('https://test.k6.io/news.php'); + + check(res, { + 'status is 200': (r) => r.status === 200, + }); +} +``` + +{{< /code >}} + +The preceding code contains two scenarios. One for the browser-level test called `browser` and one for the protocol-level test called `news`. Both scenarios are using the [constant-vus executor](https://grafana.com/docs/k6//using-k6/scenarios/executors/constant-vus) which introduces a constant number of virtual users to execute as many iterations as possible for a specified amount of time. + +Since it's all in one script, this allows for greater collaboration amongst teams. diff --git a/docs/sources/k6/v0.55.x/using-k6/_index.md b/docs/sources/k6/v0.55.x/using-k6/_index.md new file mode 100644 index 000000000..898616301 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/_index.md @@ -0,0 +1,10 @@ +--- +weight: 200 +title: Using k6 +--- + +# Using k6 + + + +{{< section >}} diff --git a/docs/sources/k6/v0.55.x/using-k6/checks.md b/docs/sources/k6/v0.55.x/using-k6/checks.md new file mode 100644 index 000000000..1cfbbc038 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/checks.md @@ -0,0 +1,154 @@ +--- +title: 'Checks' +description: 'Checks are like asserts but differ in that they do not halt the execution, instead, they just store the result of the check, pass or fail, and let the script execution continue.' +weight: 03 +--- + +# Checks + +Checks validate boolean conditions in your test. +Testers often use checks to validate that the system is responding with the expected content. +For example, a check could validate that a POST request has a `response.status == 201`, or that the body is of a certain size. + +Checks are similar to what many testing frameworks call an _assert_, but **failed checks do not cause the test to abort or finish with a failed status**. +Instead, k6 keeps track of the rate of failed checks as the test continues to run + +Each check creates a [rate metric](https://grafana.com/docs/k6//using-k6/metrics). +To make a check abort or fail a test, you can combine it with a [Threshold](https://grafana.com/docs/k6//using-k6/thresholds). + +## Check for HTTP response code + +Checks are great for codifying assertions relating to HTTP requests and responses. +For example, this snippet makes sure the HTTP response code is a 200: + +{{< code >}} + +```javascript +import { check } from 'k6'; +import http from 'k6/http'; + +export default function () { + const res = http.get('http://test.k6.io/'); + check(res, { + 'is status 200': (r) => r.status === 200, + }); +} +``` + +{{< /code >}} + +## Check for text in response body + +Sometimes, even an HTTP 200 response contains an error message. +In these situations, consider adding a check to verify the response body, like this: + +{{< code >}} + +```javascript +import { check } from 'k6'; +import http from 'k6/http'; + +export default function () { + const res = http.get('http://test.k6.io/'); + check(res, { + 'verify homepage text': (r) => + r.body.includes('Collection of simple web-pages suitable for load testing'), + }); +} +``` + +{{< /code >}} + +## Check for response body size + +To verify the size of the response body, you can use a check like this: + +{{< code >}} + +```javascript +import { check } from 'k6'; +import http from 'k6/http'; + +export default function () { + const res = http.get('http://test.k6.io/'); + check(res, { + 'body size is 11,105 bytes': (r) => r.body.length == 11105, + }); +} +``` + +{{< /code >}} + +## See percentage of checks that passed + +When a script includes checks, the summary report shows how many of the tests' checks passed: + +{{< code >}} + +```bash +$ k6 run script.js + + ... + ✓ is status 200 + + ... + checks.........................: 100.00% ✓ 1 ✗ 0 + data_received..................: 11 kB 12 kB/s +``` + +{{< /code >}} + +In this example, note that the check "is status 200" succeeded 100% of the times it was called. + +## Add multiple checks + +You can also add multiple checks within a single [check()](https://grafana.com/docs/k6//javascript-api/k6/check) statement: + +{{< code >}} + +```javascript +import { check } from 'k6'; +import http from 'k6/http'; + +export default function () { + const res = http.get('http://test.k6.io/'); + check(res, { + 'is status 200': (r) => r.status === 200, + 'body size is 11,105 bytes': (r) => r.body.length == 11105, + }); +} +``` + +{{< /code >}} + +When this test executes, the output will look something like this: + +{{< code >}} + +```bash +$ k6 run checks.js + + ... + ✓ is status 200 + ✓ body size is 11,105 bytes + + ... + checks.........................: 100.00% ✓ 2 ✗ 0 + data_received..................: 11 kB 20 kB/s +``` + +{{< /code >}} + +{{< admonition type="note" >}} + +When a check fails, the script will continue executing successfully and will not return a 'failed' exit status. +If you need the whole test to fail based on the results of a check, you have to [combine checks with thresholds](https://grafana.com/docs/k6//using-k6/thresholds#fail-a-load-test-using-checks). +This is particularly useful in specific contexts, such as integrating k6 into your CI pipelines or receiving alerts when scheduling your performance tests. + +{{< /admonition >}} + +## Read more + +- [Check Javascript API](https://grafana.com/docs/k6//javascript-api/k6/check) +- [Failing a load test using checks](https://grafana.com/docs/k6//using-k6/thresholds#fail-a-load-test-using-checks) +- [k6chaijs](https://grafana.com/docs/k6//javascript-api/jslib/k6chaijs): use BDD assertions in k6 diff --git a/docs/sources/k6/v0.55.x/using-k6/cookies.md b/docs/sources/k6/v0.55.x/using-k6/cookies.md new file mode 100644 index 000000000..183a7cbcd --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/cookies.md @@ -0,0 +1,284 @@ +--- +title: 'Cookies' +description: 'k6 will transparently manage the receiving, storage and sending of cookies as described above, so that testing of your cookie-based web site or app will just work.' +weight: 09 +--- + +# Cookies + +HTTP Cookies are used by websites and apps to store pieces of stateful information on user devices. +Through the `Set-Cookie` HTTP header, a server tells a client what information it wants stored on the user machine. + +The user's browser stores the cookie data and associates it with the hostname of the server. +For each subsequent request to that hostname, it includes the stored cookie data in a +`Cookie` header. + +You can then control more specific rules for whether to send the cookie data or not, +including limiting it to specific subdomains or paths. +You can also set an expiry date on the cookie and tell the browser to send it only over encrypted (SSL/TLS) connections. + +## Cookies with k6 + +For most purposes, k6 transparently manages the reception, storage, and transmission of cookies as described. +Testing of your cookie-based web site or app will _just work_ without requiring any special action of you. + +In some cases, though, you might want more control over cookies. +k6 provides multiple options to do this. +You can: + +- [Directly manipulate HTTP headers](https://grafana.com/docs/k6//javascript-api/k6-http/params), +- Use the more ergonomic cookie API. + +The following section shows how to use the Cookie API. + +## Setting simple cookies + +To simulate that a cookie has previously been set by a browser and is now supposed to be included +in subsequent requests to the server, include the cookie in the `cookies` request parameter: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + http.get('https://httpbin.test.k6.io/cookies', { + cookies: { + my_cookie: 'hello world', + }, + }); +} +``` + +{{< /code >}} + +This applies only to the cookie for the request in question. +It isn't sent for any subsequent requests. +To send the cookie for subsequent requests, add it to a cookie jar. +By default, k6 has a cookie jar for each VU, which you can interact with to set and inspect cookies: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + const jar = http.cookieJar(); + jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie', 'hello world'); + http.get('https://httpbin.test.k6.io/cookies'); +} +``` + +{{< /code >}} + +The per-VU cookie jar stores all cookies received from the server in a `Set-Cookie` header. +You can also create "local cookie jars" that override the per-VU cookie jar (shown in a subsequent section). + +You can also override a cookie that is already part of the per-VU cookie jar: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const jar = http.cookieJar(); + jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie', 'hello world'); + + const cookies = { + my_cookie: { + value: 'hello world 2', + replace: true, + }, + }; + + const res = http.get('https://httpbin.test.k6.io/cookies', { + cookies, + }); + + check(res.json(), { + 'cookie has correct value': (b) => b.cookies.my_cookie == 'hello world 2', + }); +} +``` + +{{< /code >}} + +## Accessing cookies + +To see which cookies were set for a particular response, look in the `cookies` property of the response object: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const res = http.get('https://httpbin.test.k6.io/cookies/set?my_cookie=hello%20world', { + redirects: 0, + }); + check(res, { + "has cookie 'my_cookie'": (r) => r.cookies.my_cookie.length > 0, + 'cookie has correct value': (r) => r.cookies.my_cookie[0].value === 'hello world', + }); +} +``` + +{{< /code >}} + +The response object's `cookies` property is a map where the key is the cookie name and the value +is an array of response cookie objects. +This array can support multiple cookies that have the same name but different `domain` or `path` attributes, as specified in [RFC6265](https://tools.ietf.org/html/rfc6265#section-5.3). + +## Properties of a response cookie object + +A response cookie object contains the following properties: + +| Property | Type | Description | +| --------- | --------- | ------------------------------------------------------------------------------------------------------------- | +| name | `string` | the name of the cookie | +| value | `string` | the value of the cookie | +| domain | `string` | domain deciding what hostnames this cookie should be sent to | +| path | `string` | limiting the cookie to only be sent if the path of the request matches this value | +| expires | `string` | when the cookie expires, this needs to be in the RFC1123 format looking like: `Mon, 02 Jan 2006 15:04:05 MST` | +| max_age | `number` | used for the same purpose as expires but defined as the number of seconds a cookie will be valid | +| secure | `boolean` | if true, the cookie will only be sent over an encrypted (SSL/TLS) connection | +| http_only | `boolean` | if true, the cookie would not be exposed to JavaScript in a browser environment | + +## Inspecting a cookie in the jar + +To see which cookies are set and stored in the cookie jar for a particular URL, +use the `cookieForURL()` method of the cookie jar object: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const res = http.get('https://httpbin.test.k6.io/cookies/set?my_cookie=hello%20world', { + redirects: 0, + }); + const jar = http.cookieJar(); + const cookies = jar.cookiesForURL('https://httpbin.test.k6.io/'); + check(res, { + "has cookie 'my_cookie'": (r) => cookies.my_cookie.length > 0, + 'cookie has correct value': (r) => cookies.my_cookie[0] === 'hello world', + }); +} +``` + +{{< /code >}} + +The `cookies` object returned by the jar's `cookiesForURL()` method is a map where the key is the +cookie name and the value is an array of cookie values (strings). It is an array to support +multiple cookies having the same name (but different `domain` and/or `path` attributes), which +is part of [RFC6265](https://tools.ietf.org/html/rfc6265#section-5.3). + +## Setting advanced cookies with attributes + +To set cookies that more tightly controls the behavior of the cookie we must add the cookie to a +cookie jar. An example: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const jar = http.cookieJar(); + jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie', 'hello world', { + domain: 'httpbin.test.k6.io', + path: '/cookies', + secure: true, + max_age: 600, + }); + const res = http.get('https://httpbin.test.k6.io/cookies'); + check(res, { + 'has status 200': (r) => r.status === 200, + "has cookie 'my_cookie'": (r) => r.cookies.my_cookie[0] !== null, + 'cookie has correct value': (r) => r.cookies.my_cookie[0].value == 'hello world', + }); +} +``` + +{{< /code >}} + +## Local cookie jars + +Besides the per-VU cookie jar, you can also create local cookie jars to override the per-VU +cookie jar on a per-request basis: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const jar = new http.CookieJar(); + + // Add cookie to local jar + const cookieOptions = { + domain: 'httpbin.test.k6.io', + path: '/cookies', + secure: true, + max_age: 600, + }; + jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie', 'hello world', cookieOptions); + + // Override per-VU jar with local jar for the following request + const res = http.get('https://httpbin.test.k6.io/cookies', { jar }); + check(res, { + 'has status 200': (r) => r.status === 200, + "has cookie 'my_cookie'": (r) => r.cookies.my_cookie[0] !== null, + 'cookie has correct value': (r) => r.cookies.my_cookie[0].value == 'hello world', + }); +} +``` + +{{< /code >}} + +## Examples + +{{< code >}} + +```javascript +// Example showing two methods how to log all cookies (with attributes) from a HTTP response. +import http from 'k6/http'; + +function logCookie(c) { + // Here we log the name and value of the cookie along with additional attributes. + // For full list of attributes see: + // https://grafana.com/docs/k6/using-k6/cookies#properties-of-a-response-cookie-object + const output = ` + ${c.name}: ${c.value} + tdomain: ${c.domain} + tpath: ${c.path} + texpires: ${c.expires} + thttpOnly: ${c.http_only} + `; + console.log(output); +} +export default function () { + const res = http.get('https://www.google.com/'); + + // Method 1: Use for-loop and check for non-inherited properties + for (const name in res.cookies) { + if (res.cookies.hasOwnProperty(name) !== undefined) { + logCookie(res.cookies[name][0]); + } + } + + // Method 2: Use ES6 Map to loop over Object entries + new Map(Object.entries(res.cookies)).forEach((v, k) => { + logCookie(v[0]); + }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/using-k6/environment-variables.md b/docs/sources/k6/v0.55.x/using-k6/environment-variables.md new file mode 100644 index 000000000..23a00b344 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/environment-variables.md @@ -0,0 +1,126 @@ +--- +title: 'Environment variables' +description: 'You can access any environment variables from your k6 script code and use this to supply your VUs with configuration information.' +weight: 11 +--- + +# Environment variables + +Often, scripts need only minor tweaks to be reusable in different contexts. +Rather than creating several separate scripts for these different contexts or environments, you can use [environment variables](https://grafana.com/docs/k6//misc/glossary#environment-variables) to make parts of your script tweakable. + +You can use environment variables for two main purposes: + +1. Passing environment variables to the k6 Script +2. Configuring [k6 Options](https://grafana.com/docs/k6//using-k6/k6-options/how-to) with environment variables + +## Passing environment variables to the k6 script + +In k6, the environment variables are exposed through a global `__ENV` variable, a JS object. +For reference, see the script example below: + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export default function () { + const res = http.get(`http://${__ENV.MY_HOSTNAME}/`); + sleep(1); +} +``` + +The recommended option to pass environment variables to your testing script is to use one or more [`-e` / `--env` CLI flags](https://grafana.com/docs/k6//using-k6/k6-options/reference#supply-environment-variables) +(this command works the same for all platforms): + +{{< code >}} + +```bash +$ k6 run -e MY_HOSTNAME=test.k6.io script.js +``` + +{{< /code >}} + +> #### ⚠ The `-e` flag does not configure options +> +> This flag just provides variables to the script, which the script can use or ignore. +> For example, `-e K6_ITERATIONS=120` does _not_ configure the script iterations. +> +> Compare this behavior with `K6_ITERATIONS=120 k6 run script.js`, which _does_ set iterations. + +{{< collapse title="Using System Environment Variables" >}} + +A second option to pass environment variables is to source them from the local system. + +```bash +$ MY_HOSTNAME=test.k6.io k6 run script.js +``` + +```windows +C:\k6> set "MY_HOSTNAME=test.k6.io" && k6 run script.js +``` + +```powershell +PS C:\k6> $env:MY_HOSTNAME="test.k6.io"; k6 run script.js +``` + +#### ⚠️ Warning + +By default, passing system environment variables doesn't work for `k6 archive`, `k6 cloud run`, and `k6 inspect`. +This is a security measure to avoid the risk of uploading sensitive data to k6 Cloud. +To override this mode, specify [--include-system-env-vars](https://grafana.com/docs/k6//using-k6/k6-options/reference#include-system-env-vars). + +{{< /collapse >}} + +## Configure k6 options with environment variables + +You can also configure k6 [options](https://grafana.com/docs/k6//using-k6/k6-options/how-to) with environment variables. +Consider this script: + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export default function () { + const res = http.get('https://test.k6.io'); + sleep(1); +} +``` + +By default, a local run of this script will execute a single iteration with one virtual user(VU). +To modify the default behavior, pass [k6 options](https://grafana.com/docs/k6//using-k6/k6-options/how-to) as environment variables. +For example, this snippet configures the script to run 10 virtual users for a duration of 10 seconds: + +{{< code >}} + +```bash +$ K6_VUS=10 K6_DURATION=10s k6 run script.js +``` + +```windows +C:\k6> set "K6_VUS=10 K6_DURATION=10s" && k6 run script.js +``` + +```powershell +PS C:\k6> $env:K6_VUS=10 ; $env:K6_DURATION="10s" ; k6 run script.js +``` + +{{< /code >}} + +As the preceding example shows, you need to prefix `K6_` in the environment variable name for k6 to evaluate it as an option parameter. +However, be aware that not all options are supported as environment variables. +You can confirm whether one is by checking the [documentation for each option](https://grafana.com/docs/k6//using-k6/k6-options/reference). + +Note that when you define options in multiple places, there's an [order of precedence](https://grafana.com/docs/k6//using-k6/k6-options/how-to) that determines the option to use. +To ensure you're always working with the highest precedence, use command-line flags instead of environment variables: + +{{< code >}} + +```bash +$ k6 run -e MY_HOSTNAME=test.k6.io --duration 10s --vus 10 script.js +``` + +{{< /code >}} + +## Read more + +- [Manage environment variables in k6 Cloud](https://grafana.com/docs/grafana-cloud/testing/k6/author-run/cloud-scripting-extras/cloud-environment-variables/) diff --git a/docs/sources/k6/v0.55.x/using-k6/execution-context-variables.md b/docs/sources/k6/v0.55.x/using-k6/execution-context-variables.md new file mode 100644 index 000000000..ff998d083 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/execution-context-variables.md @@ -0,0 +1,138 @@ +--- +title: 'Execution context variables' +description: 'k6/execution module provides the capability to get information about the current test execution state inside the test script' +weight: 12 +--- + +# Execution context variables + +In some cases, it's really useful to have information about the script's current test-execution state. For example, you might want to + +- Have different VUs run different test logic +- Use different data for each VU and iteration +- Figure out the stage that a test is in + +To solve these issues, you can use _execution context variables_. + +## k6/execution + +The [k6/execution](https://grafana.com/docs/k6//javascript-api/k6-execution) module exposes details about the current execution state, such as _the name of the currently executed scenario_, _how many VUs are currently active_, and more. +The module provides test-execution information via three properties: + +| Property | Meta-information and execution details about | +| ----------------------------------------------------------------------------------------- | -------------------------------------------- | +| [instance](https://grafana.com/docs/k6//javascript-api/k6-execution#instance) | The current running k6 instance | +| [scenario](https://grafana.com/docs/k6//javascript-api/k6-execution#scenario) | The current running scenario | +| [vu](https://grafana.com/docs/k6//javascript-api/k6-execution#vu) | The current VU and iteration | + +## Example: log all context variables + +If you want to experiment with what each context variable looks like as a test runs, +you can copy this template literal into one of your test scripts. + +Note that this omits the `abort` variable, since that function would abort the test. + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +export default function () { + console.log(`Execution context + +Instance info +------------- +Vus active: ${exec.instance.vusActive} +Iterations completed: ${exec.instance.iterationsCompleted} +Iterations interrupted: ${exec.instance.iterationsInterrupted} +Iterations completed: ${exec.instance.iterationsCompleted} +Iterations active: ${exec.instance.vusActive} +Initialized vus: ${exec.instance.vusInitialized} +Time passed from start of run(ms): ${exec.instance.currentTestRunDuration} + +Scenario info +------------- +Name of the running scenario: ${exec.scenario.name} +Executor type: ${exec.scenario.executor} +Scenario start timestamp: ${exec.scenario.startTime} +Percenatage complete: ${exec.scenario.progress} +Iteration in instance: ${exec.scenario.iterationInInstance} +Iteration in test: ${exec.scenario.iterationInTest} + +Test info +--------- +All test options: ${exec.test.options} + +VU info +------- +Iteration id: ${exec.vu.iterationInInstance} +Iteration in scenario: ${exec.vu.iterationInScenario} +VU ID in instance: ${exec.vu.idInInstance} +VU ID in test: ${exec.vu.idInTest} +VU tags: ${exec.vu.tags}`); +} +``` + +{{< /code >}} + +For detailed reference, refer to the [k6/execution module](https://grafana.com/docs/k6//javascript-api/k6-execution). + +## Additional examples + +- [Getting unique data once](https://grafana.com/docs/k6//examples/data-parameterization#retrieving-unique-data) +- [Timing operations](https://grafana.com/docs/k6//javascript-api/k6-execution#timing-operations) +- [Executing different code blocks](https://grafana.com/docs/k6//javascript-api/k6-execution#script-naming) + +{{< collapse title="_VU and _ITER (discouraged)" >}} + +{{< admonition type="note" >}} + +k6 v0.34.0 introduced the **k6/execution** module. If you are using a version of k6 that doesn't have this module, use `__VU` and `__ITER`. + +{{< /admonition >}} + +`__VU` and `__ITER` are both global variables with execution-context information that k6 makes available to the test script. + +- `__ITER`: A numeric counter with the current iteration number for a specific VU. Zero-based. +- `__VU`: Current VU number in use. k6 assigns the value incrementally for each new VU instance, starting from one. The variable is 0 when executing the setup and teardown functions. + +### Running in k6 Cloud + +When you run tests in [Grafana Cloud k6](https://grafana.com/docs/grafana-cloud/testing/k6/), the **\_\_VU** value is per server/load generator. + +### Examples + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; +export default function () { + http.get('http://test.k6.io'); + console.log(`VU: ${__VU} - ITER: ${__ITER}`); + sleep(1); +} +``` + +You can use execution-context variables to configure different test behaviors and parameterizations. +A typical use case is a load test that simulates different users performing a login flow. + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; +export default function () { + const email = `user+${__VU}@mail.com`; + const payload = JSON.stringify({ email: email, password: 'test' }); + const params = { headers: { 'Content-Type': 'application/json' } }; + http.post('http://test.k6.io/login', payload, params); + console.log(email); + // .. continue the user flow + sleep(1); +} +``` + +{{< /collapse >}} + +## Grafana Cloud k6 context variables + +Grafana Cloud k6 injects additional context variables into a running cloud test. These variables provide information about the server (`K6_CLOUDRUN_INSTANCE_ID`), load zone (`K6_CLOUDRUN_LOAD_ZONE`),instance ID (`K6_CLOUDRUN_INSTANCE_ID`), and cloud distribution (`K6_CLOUDRUN_DISTRIBUTION`). + +Refer to [Cloud execution context variables](https://grafana.com/docs/grafana-cloud/testing/k6/author-run/cloud-scripting-extras/cloud-execution-context-variables/) for more details. diff --git a/docs/sources/k6/v0.55.x/using-k6/http-debugging.md b/docs/sources/k6/v0.55.x/using-k6/http-debugging.md new file mode 100644 index 000000000..8878c51be --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/http-debugging.md @@ -0,0 +1,102 @@ +--- +title: 'HTTP debugging' +description: "Things don't always work as expected. For those cases there is a handy CLI flag, --http-debug, that is good to be aware of." +_build: + list: false +weight: 17 +--- + +# HTTP debugging + +Things don't always work as expected. For those cases, enabling the [--http-debug](https://grafana.com/docs/k6//using-k6/k6-options/reference#http-debug) option will log HTTP requests and responses to help you debugging the script. + +- `--http-debug` logs the HTTP requests and responses, skipping the body. +- `--http-debug="full"` logs the HTTP requests and responses, including the full body. + +## Example + +Given the following script: + +{{< code >}} + +```bash +import http from "k6/http"; + +export default function() { + http.get("https://google.com/"); +} +``` + +{{< /code >}} + +If we run it using `k6 run --http-debug script.js` we get output that looks like this: + +{{< code >}} + +```bash + /\ |‾‾| /‾‾/ /‾/ + /\ / \ | |_/ / / / + / \/ \ | | / ‾‾\ + / \ | |‾\ \ | (_) | + / __________ \ |__| \__\ \___/ .io + + execution: local + output: - + script: script.js + + duration: -, iterations: 1 + vus: 1, max: 1 + +Request: [----------------------------------------------------------] starting + +GET / HTTP/1.1 +Host: google.com +User-Agent: k6/0.20.0 (https://k6.io/); +Accept-Encoding: gzip + + +RedirectResponse: +HTTP/2.0 302 Found +Content-Length: 267 +Alt-Svc: hq=":443"; ma=2592000; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="42,41,39,35" +Cache-Control: private +Content-Type: text/html; charset=UTF-8 +Date: Thu, 22 Mar 2018 13:39:44 GMT +Location: https://www.google.se/?gfe_rd=cr&dcr=0&ei=ILKzWqiODOaxX_LBi5AG +Referrer-Policy: no-referrer + + +RedirectRequest: +GET /?gfe_rd=cr&dcr=0&ei=ILKzWqiODOaxX_LBi5AG HTTP/1.1 +Host: www.google.se +User-Agent: k6/0.19.0 (https://k6.io/); +Referer: https://google.com/ +Accept-Encoding: gzip + + +Response: +HTTP/2.0 200 OK +Connection: close +Accept-Ranges: none +Alt-Svc: hq=":443"; ma=2592000; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="42,41,39,35" +Cache-Control: private, max-age=0 +Content-Type: text/html; charset=ISO-8859-1 +Date: Thu, 22 Mar 2018 13:39:44 GMT +Expires: -1 +P3p: CP="This is not a P3P policy! See g.co/p3phelp for more info." +Server: gws +Set-Cookie: 1P_JAR=2018-03-22-13; expires=Sat, 21-Apr-2018 13:39:44 GMT; path=/; domain=.google.se +Set-Cookie: NID=126=asTcm5s3-jMa1-pquG09m9lpUrowy-OtzZ9cUomBuKeJePbvwJAZe5wCtiyLITj9_RrlWLf6DTQ8ufpdB39MCRV-zUpfXAUw8XUVMWtgdU1gbtnQ9rssin56333g9Hyo; expires=Fri, 21-Sep-2018 13:39:44 GMT; path=/; domain=.google.se; HttpOnly +Vary: Accept-Encoding +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 1; mode=block + +... + +``` + +{{< /code >}} + +## Read more + +- [Debugging Using a Web Proxy](https://k6.io/blog/k6-load-testing-debugging-using-a-web-proxy/) diff --git a/docs/sources/k6/v0.55.x/using-k6/http-requests.md b/docs/sources/k6/v0.55.x/using-k6/http-requests.md new file mode 100644 index 000000000..ddd72265a --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/http-requests.md @@ -0,0 +1,224 @@ +--- +title: 'HTTP Requests' +description: 'Define the HTTP requests and methods you want to use. k6 adds tags to the requests, making it easier to filter results. You can customize tags as you wish.' +weight: 01 +--- + +# HTTP Requests + +When you create a new load test, one of the first steps is to define the HTTP requests that you would like to test. + +## Make HTTP Requests {#making-http-requests} + +A GET request looks like this: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + http.get('http://test.k6.io'); +} +``` + +{{< /code >}} + +For something slightly more complex, this example shows a POST request with an email/password authentication payload: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + const url = 'http://test.k6.io/login'; + const payload = JSON.stringify({ + email: 'aaa', + password: 'bbb', + }); + + const params = { + headers: { + 'Content-Type': 'application/json', + }, + }; + + http.post(url, payload, params); +} +``` + +{{< /code >}} + +## Available methods + +The [http module](https://grafana.com/docs/k6//javascript-api/k6-http) handles all kinds of HTTP requests and methods. + +| Name | Value | +| ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------- | +| [batch()](https://grafana.com/docs/k6//javascript-api/k6-http/batch) | Issue multiple HTTP requests in parallel (like e.g. browsers tend to do). | +| [del()](https://grafana.com/docs/k6//javascript-api/k6-http/del) | Issue an HTTP DELETE request. | +| [get()](https://grafana.com/docs/k6//javascript-api/k6-http/get) | Issue an HTTP GET request. | +| [head()](https://grafana.com/docs/k6//javascript-api/k6-http/head) | Issue an HTTP HEAD request. | +| [options()](https://grafana.com/docs/k6//javascript-api/k6-http/options) | Issue an HTTP OPTIONS request. | +| [patch()](https://grafana.com/docs/k6//javascript-api/k6-http/patch) | Issue an HTTP PATCH request. | +| [post()](https://grafana.com/docs/k6//javascript-api/k6-http/post) | Issue an HTTP POST request. | +| [put()](https://grafana.com/docs/k6//javascript-api/k6-http/put) | Issue an HTTP PUT request. | +| [request()](https://grafana.com/docs/k6//javascript-api/k6-http/request) | Issue any type of HTTP request. | + +## Follow redirects + +By default, k6 automatically follows a set number of redirects before stopping and returning the last response. You can customize this by using: + +- The `maxRedirects` option to customize the number of redirects globally. Refer to [Max redirects](https://grafana.com/docs/k6//using-k6/k6-options/reference/#max-redirects) for more details. +- The `Params.redirects` property to customize the number of redirects for a specific request, which overrides the `maxRedirects` option. Refer to [Params](https://grafana.com/docs/k6//javascript-api/k6-http/params/) for more details. + +## HTTP Request Tags + +k6 automatically applies [tags](https://grafana.com/docs/k6//using-k6/tags-and-groups#section-tags) to your HTTP requests. +You can use these tags to filter your results and organize your analysis. + +| Name | Description | +| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| expected_response | By default, response statuses between 200 and 399 are `true`. Change the default behavior with [setResponseCallback](https://grafana.com/docs/k6//javascript-api/k6-http/set-response-callback). | +| group | When the request runs inside a [group](https://grafana.com/docs/k6//javascript-api/k6/group), the tag value is the group name. Default is empty. | +| name | Defaults to URL requested | +| method | Request method (`GET`, `POST`, `PUT` etc.) | +| scenario | When the request runs inside a [scenario](https://grafana.com/docs/k6//using-k6/scenarios), the tag value is the scenario name. Default is `default`. | +| status | response status | +| url | defaults to URL requested | + +The following snippet shows a JSON example of how a test-result data point is logged. +In this example, the metric is the duration of an HTTP request. + +Note how the `tags` object groups data. + +{{< code >}} + +```json +{ + "type": "Point", + "metric": "http_req_duration", + "data": { + "time": "2017-06-02T23:10:29.52444541+02:00", + "value": 586.831127, + "tags": { + "expected_response": "true", + "group": "", + "method": "GET", + "name": "http://test.k6.io", + "scenario": "default", + "status": "200", + "url": "http://test.k6.io" + } + } +} +``` + +{{< /code >}} + +## Group URLs under one tag {#url-grouping} + +By default, tags have a `name` field that holds the value of the request URL. +If your test has dynamic URL paths, you might not want this behavior, which could bring a large number of unique URLs into the metrics stream. +For example, the following code accesses 100 different URLs: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + for (let id = 1; id <= 100; id++) { + http.get(`http://example.com/posts/${id}`); + } +} +// tags.name=\"http://example.com/posts/1\", +// tags.name=\"http://example.com/posts/2\", +``` + +{{< /code >}} + +You might prefer to report this data in a single metric: +To aggregate data from dynamic URLs, explicitly set a `name` tag: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + for (let id = 1; id <= 100; id++) { + http.get(`http://example.com/posts/${id}`, { + tags: { name: 'PostsItemURL' }, + }); + } +} +// tags.name=\"PostsItemURL\", +// tags.name=\"PostsItemURL\", +``` + +{{< /code >}} + +That code would produce JSON output like this: + +{{< code >}} + +```json +// For http://example.com/1, note that the url is not present in the JSON. + +{ + "type":"Point", + "metric":"http_req_duration", + "data": { + "time":"2017-06-02T23:10:29.52444541+02:00", + "value":586.831127, + "tags": { + "method":"GET", + "name":"PostsItemURL", + "status":"200", + "url":"PostsItemURL" + } + } +} + +// and for http://example.com/2 + +{ + "type":"Point", + "metric":"http_req_duration", + "data": { + "time":"2017-06-02T23:10:29.58582529+02:00", + "value":580.839273, + "tags": { + "method":"GET", + "name":"PostsItemURL", + "status":"200", + "url":"PostsItemURL" + } + } +} +``` + +{{< /code >}} + +Note how these two objects have the same `name`, despite having different URLs. +If you filter the results for the tag `name: PostsItemURL`, the results include all data points from all 100 URLs. + +As an alternative, you can also use the `http.url` wrapper to set the `name` tag with a string template value: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + for (let id = 1; id <= 100; id++) { + http.get(http.url`http://example.com/posts/${id}`); + } +} +// tags.name="http://example.com/posts/${}", +// tags.name="http://example.com/posts/${}", +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/using-k6/javascript-typescript-compatibility-mode.md b/docs/sources/k6/v0.55.x/using-k6/javascript-typescript-compatibility-mode.md new file mode 100644 index 000000000..c99cfd8b0 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/javascript-typescript-compatibility-mode.md @@ -0,0 +1,123 @@ +--- +aliases: + - ./javascript-compatibility-mode/ # /docs/k6//using-k6/javascript-compatibility-mode/ +title: JavaScript and TypeScript compatibility mode +menuTitle: JavaScript and TypeScript mode +excerpt: 'k6 supports running test scripts with different ECMAScript and TypeScript compatibility modes using --compatibility-mode' +weight: 19 +--- + +# JavaScript and TypeScript compatibility mode + +You can write k6 tests in various ECMAScript versions: + +- ES6+ JavaScript with ES modules (ESM). +- ES6+ JavaScript with CommonJS modules. + +k6 supports both module types and most ES6+ features in all k6 execution modes: local, distributed, and cloud. + +Additionally, k6 also has experimental support for [esbuild](https://esbuild.github.io/), to transpile TypeScript (TS) code. + +Some users prefer to bundle their test code outside k6. For this reason, k6 offers three JavaScript compatibility modes: + +- [Extended mode](#extended-mode): The default option. +- [Experimental enhanced mode](#experimental-enhanced-mode): The experimental option, supporting TS. +- [Base mode](#base-mode): After v0.53.0 this is almost the same as extended, but doesn't alias `global` to `globalThis` - a nodejs compatibility. + +When running tests, you can change the mode by using the `--compatibility-mode` option: + +| Env | CLI | Code / Config file | Default | +| ----------------------- | ---------------------- | ------------------ | ------------ | +| `K6_COMPATIBILITY_MODE` | `--compatibility-mode` | N/A | `"extended"` | + +## Extended mode + +By default, k6 uses the `--compatibility-mode=extended` mode: + +{{< code >}} + +```bash +$ k6 run script.js +``` + +{{< /code >}} + +After v0.53.0 the only difference with base is that `global` (node's global variable) is aliased to the value of `globalThis`. + +## Experimental enhanced mode + +{{< code >}} + +```cli +$ k6 run --compatibility-mode=experimental_enhanced script.ts +``` + +```env +$ K6_COMPATIBILITY_MODE=experimental_enhanced k6 run script.ts +``` + +{{< /code >}} + +The experimental enhanced mode is similar to the extended mode, but it uses [esbuild](https://esbuild.github.io/) instead of Babel to transpile TypeScript (TS) code. + +TypeScript support is partial as it removes the type information but doesn't provide type safety. + +## Base mode + +{{< code >}} + +```cli +$ k6 run --compatibility-mode=base script.js +``` + +```env +$ K6_COMPATIBILITY_MODE=base k6 run script.js +``` + +{{< /code >}} + +After v0.53.0 there isn't a big reason to use this. Before that it was dropping ESM support for usually improved startup speed. + +### CommonJS Example + +{{< code >}} + +```javascript +const http = require('k6/http'); +const k6 = require('k6'); + +module.exports.options = { + vus: 10, + duration: '30s', +}; + +module.exports.default = function () { + http.get('http://test.k6.io'); + k6.sleep(1); +}; +``` + +{{< /code >}} + +> ### ⚠️ About require() +> +> Note that `require()` is a custom k6 implementation of module +> loading, which doesn't behave in the same way as the +> [require() call in Node.js](https://nodejs.org/api/modules.html#modules_require_id). +> Specifically, it only handles loading of built-in k6 modules, +> scripts on the local filesystem, and remote scripts over HTTP(S), +> but it does _not_ support the +> [Node.js module resolution algorithm](https://nodejs.org/api/modules.html#modules_all_together). + +## Bundling with Babel outside of k6 + +The examples below demonstrate the use of Babel with bundlers like [Webpack](https://webpack.js.org/) and [Rollup](https://rollupjs.org/): + +- [k6-template-es6](https://github.com/grafana/k6-template-es6): Template using Webpack and Babel to bundle k6 tests. +- [k6-rollup-example](https://github.com/grafana/k6-rollup-example): Example using Rollup and Babel to bundle a testing project. + +## Read more + +- [Running large tests](https://grafana.com/docs/k6//testing-guides/running-large-tests): Optimize k6 for better performance. +- [k6 Modules](https://grafana.com/docs/k6//using-k6/modules): Different options to import modules in k6. +- [k6 Archive Command](https://grafana.com/docs/k6//misc/archive): The `k6 archive` command bundles all k6 test dependencies into a `tar` file, which can then be used for execution. It may also reduce the execution startup time. diff --git a/docs/sources/k6/v0.55.x/using-k6/k6-options/_index.md b/docs/sources/k6/v0.55.x/using-k6/k6-options/_index.md new file mode 100644 index 000000000..c56357608 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/k6-options/_index.md @@ -0,0 +1,16 @@ +--- +title: 'Options' +slug: '/k6-options' +description: 'Options configure test-run behavior. You can set options in multiple locations. Examples for how to use options, and a complete reference.' +weight: 05 +--- + +# Options + +Options configure test-run behavior. +For example, options are how you define test tags, thresholds, user agents, and the number of virtual users and iterations. + +| On this page | Read about... | +| ------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | +| [How to use options](https://grafana.com/docs/k6//using-k6/k6-options/how-to) | How to set options in different places (with examples), how to override options, and how to access the value of an option as the test runs. | +| [Options reference](https://grafana.com/docs/k6//using-k6/k6-options/reference) | Every option, with examples, defaults, and descriptions. | diff --git a/docs/sources/k6/v0.55.x/using-k6/k6-options/how-to.md b/docs/sources/k6/v0.55.x/using-k6/k6-options/how-to.md new file mode 100644 index 000000000..75893079a --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/k6-options/how-to.md @@ -0,0 +1,229 @@ +--- +title: 'How to use options' +slug: '/how-to' +description: 'How to set options in different places (with examples), how to override options, and how to access the value of an option as the test runs.' +weight: 01 +--- + +# How to use options + +k6 provides multiple places to set options: + +- In CLI flags +- In environment variables +- In the script `options` object +- In a configuration file + +Most likely, your use case will determine where you want to set the particular options for a particular test. +You can also access option values as your test runs. + +## Order of precedence + +![Options passed as command-line flags override all other options: defaults < script options < environment variables < command-line flags](/media/docs/k6-oss/order-of-precedence.png) + +You can set options in multiple places. +If there are conflicts, k6 uses the option from the place with the highest _order of precedence_. + +1. First, k6 uses the option's default value. +1. Next, k6 uses the options set in a configuration file via the `--config` flag. +1. Then, k6 uses the script value (if set). +1. After, k6 uses the environment variable (if set). +1. Finally, k6 takes the value from the CLI flag (if set). + +That is, **command-line flags have the highest order of precedence**. + +## Where to set options + +Sometimes, how you set options is a matter of personal preference. +Other times, the context of your test dictates the most sensible place to put your options. + +- **Options in the script to version control and keep tests tidy** + + The script `options` object is generally the best place to put your options. + This provides automatic version control, allows for easy reuse, and lets you modularize your script. + +- **CLI flags to set options on the fly** + + When you want to run a quick test, command-line flags are convenient. + + You can also use command-line flags to override files in your script (as determined by the [order of precedence](#order-of-precedence)). + For example, if your script file sets the test duration at 60 seconds, you could use a CLI flag to run a one-time shorter test. + With a flag like `--duration 30s`, the test would be half as long but otherwise identical. + +- **Environment variables to set options from your build chain** + + For example, you could derive the option from a variable in your Docker container definition, CI UI, or vault—wherever you declare environment variables. + + The [block hostnames](https://grafana.com/docs/k6//using-k6/k6-options/reference#block-hostnames) option is an example of an option that works well with environment variables. + +## Examples of setting options + +The following JS snippets show some examples of how you can set options. + +### Set options in the script + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + hosts: { 'test.k6.io': '1.2.3.4' }, + stages: [ + { duration: '1m', target: 10 }, + { duration: '1m', target: 20 }, + { duration: '1m', target: 0 }, + ], + thresholds: { http_req_duration: ['avg<100', 'p(95)<200'] }, + noConnectionReuse: true, + userAgent: 'MyK6UserAgentString/1.0', +}; + +export default function () { + http.get('http://test.k6.io/'); +} +``` + +{{< /code >}} + +### Set options with environment variables + +You can also set the options from the previous example through environment variables and command-line flags: + +{{< code >}} + +```bash +$ K6_NO_CONNECTION_REUSE=true K6_USER_AGENT="MyK6UserAgentString/1.0" k6 run script.js + +$ k6 run --no-connection-reuse --user-agent "MyK6UserAgentString/1.0" script.js +``` + +```windows +C:\k6> set "K6_NO_CONNECTION_REUSE=true" && set "K6_USER_AGENT=MyK6UserAgentString/1.0" && k6 run script.js + +C:\k6> k6 run --no-connection-reuse --user-agent "MyK6UserAgentString/1.0" script.js +``` + +```powershell +PS C:\k6> $env:K6_NO_CONNECTION_REUSE=true; $env:K6_USER_AGENT="MyK6UserAgentString/1.0"; k6 run script.js + +PS C:\k6> k6 run --no-connection-reuse --user-agent "MyK6UserAgentString/1.0" script.js +``` + +{{< /code >}} + +### Set options from k6 variables + +With the `--env` flag, you can use the CLI to define k6 variables. +Then, you can use the variable to dynamically define an option's value in the script file. + +For example, you could define a variable for your user agent like this: + +```bash +k6 run script.js --env MY_USER_AGENT="hello" +``` + +Then, your script could then set the `userAgent` option based on the variable's value. +This allows for quick configuration. + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + userAgent: __ENV.MY_USER_AGENT, +}; + +export default function () { + http.get('http://test.k6.io/'); +} +``` + +{{< /code >}} + +> **Note**: Though this method uses the `--env` flag, this is not the same as using an environment variable. +> For an explanation, refer to the [environment variables document](https://grafana.com/docs/k6//using-k6/environment-variables). + +### Set options with the --config flag + +k6 includes a [default configuration file](https://grafana.com/docs/k6//using-k6/k6-options/reference#config) that you can edit, or you can create a new file and then use a CLI flag to point to that file. +If you use it, the options take the _second lowest order of precedence_ (after defaults). +If you set options anywhere else, they will override the `--config` flag options. + +Use the `--config` flag to declare the file path to your options. + +```bash +k6 run --config options.json script.js +``` + +This command would set test options according to the values in the `options.json` file. + +{{< code >}} + +```json +{ + "hosts": { + "test.k6.io": "1.2.3.4" + }, + "stages": [ + { + "duration": "1m", + "target": 10 + }, + { + "duration": "1m", + "target": 30 + }, + { + "duration": "1m", + "target": 0 + } + ], + "thresholds": { + "http_req_duration": ["avg<100", "p(95)<200"] + }, + "noConnectionReuse": true, + "userAgent": "MyK6UserAgentString/1.0" +} +``` + +{{< /code >}} + +For an alternative way to separate configuration from logic, you can use the `JSON.parse()` method in your script file: + +```javascript +// load test config, used to populate exported options object: +const testConfig = JSON.parse(open('./config/test.json')); +// combine the above with options set directly: +export const options = testConfig; +``` + +## Get an option value from the script + +The `k6/execution` API provides a [test.options](https://grafana.com/docs/k6//javascript-api/k6-execution#test) object. +With `test.options`, you can access the consolidated and derived options of your script as the test runs. + +A common use of this feature is to log the value of a tag, but there are many possibilities. +For example, this script accesses the value of the test's current stage: + +{{< code >}} + +```javascript +import exec from 'k6/execution'; + +export const options = { + stages: [ + { duration: '5s', target: 100 }, + { duration: '5s', target: 50 }, + ], +}; + +export default function () { + console.log(exec.test.options.scenarios.default.stages[0].target); // 100 +} +``` + +{{< /code >}} + +
diff --git a/docs/sources/k6/v0.55.x/using-k6/k6-options/reference.md b/docs/sources/k6/v0.55.x/using-k6/k6-options/reference.md new file mode 100644 index 000000000..ec7c188a9 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/k6-options/reference.md @@ -0,0 +1,1559 @@ +--- +title: 'Options reference' +slug: '/reference' +description: 'A complete list of all k6 options, with descriptions, defaults, and examples of how to set the option in your script, config files, environment variables, or CLI.' +weight: 02 +--- + +# Options reference + +Options define test-run behavior. +Most options can be passed in multiple places. +If an option is defined in multiple places, k6 chooses the value from the highest [order of precedence](https://grafana.com/docs/k6//using-k6/k6-options/how-to#order-of-precedence). + +## Quick reference of options + +Each option has its own detailed reference in a separate section. + +| Option | Description | +| ------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [Address](#address) | Address of the REST API server | +| [Batch](#batch) | Max number of simultaneous connections of a `http.batch()` call | +| [Batch per host](#batch-per-host) | Max number of simultaneous connections of a `http.batch()` call for a host | +| [Blacklist IP](#blacklist-ip) | Blacklist IP ranges from being called | +| [Block hostnames](#block-hostnames) | Block any requests to specific hostnames | +| [Compatibility mode](#compatibility-mode) | Support running scripts with different ECMAScript modes | +| [Config](#config) | Specify the config file in JSON format to read the options values | +| [Console output](#console-output) | Redirects logs logged by `console` methods to the provided output file | +| [Discard response bodies](#discard-response-bodies) | Specify whether response bodies should be discarded | +| [DNS](#dns) | Configure DNS resolution behavior | +| [Duration](#duration) | A string specifying the total duration of the test run; together with the [vus option](#vus), it's a shortcut for a single [scenario](https://grafana.com/docs/k6//using-k6/scenarios) with a [constant VUs executor](https://grafana.com/docs/k6//using-k6/scenarios/executors/constant-vus) | +| [Execution segment](#execution-segment) | Limit execution to a segment of the total test | +| [Exit on running](#exit-on-running) | Exits when test reaches the running status | +| [Cloud options](#cloud-options) | An object used to set configuration options for cloud parameters. | +| [Hosts](#hosts) | An object with overrides to DNS resolution | +| [HTTP debug](#http-debug) | Log all HTTP requests and responses | +| [Include system Env vars](#include-system-env-vars) | Pass the real system environment variables to the runtime | +| [Insecure skip TLS verify](#insecure-skip-tls-verify) | A boolean specifying whether k6 should ignore TLS verifications for connections established from code | +| [Iterations](#iterations) | A number specifying a fixed number of iterations to execute of the script; together with the [vus option](#vus), it's a shortcut for a single [scenario](https://grafana.com/docs/k6//using-k6/scenarios) with a [shared iterations executor](https://grafana.com/docs/k6//using-k6/scenarios/executors/shared-iterations) | +| [Linger](#linger) | A boolean specifying whether k6 should linger around after test run completion | +| [Local IPs](#local-ips) | A list of local IPs, IP ranges, and CIDRs from which VUs will make requests | +| [Log output](#log-output) | Configuration about where logs from k6 should be send | +| [LogFormat](#logformat) | Specify the format of the log output | +| [Max redirects](#max-redirects) | The maximum number of HTTP redirects that k6 will follow | +| [Minimum iteration duration](#minimum-iteration-duration) | Specify the minimum duration for every single execution | +| [No color](#no-color) | A boolean specifying whether colored output is disabled | +| [No connection reuse](#no-connection-reuse) | A boolean specifying whether k6 should disable keep-alive connections | +| [No cookies reset](#no-cookies-reset) | This disables resetting the cookie jar after each VU iteration | +| [No summary](#no-summary) | disables the [end-of-test summary](https://grafana.com/docs/k6//results-output/end-of-test) | +| [No setup](#no-setup) | A boolean specifying whether `setup()` function should be run | +| [No teardown](#no-teardown) | A boolean specifying whether `teardown()` function should be run | +| [No thresholds](#no-thresholds) | Disables threshold execution | +| [No usage report](#no-usage-report) | A boolean specifying whether k6 should send a usage report | +| [No VU connection reuse](#no-vu-connection-reuse) | A boolean specifying whether k6 should reuse TCP connections | +| [Paused](#paused) | A boolean specifying whether the test should start in a paused state | +| [Profiling Enabled](#profiling-enabled) | Enables profiling endpoints | +| [Quiet](#quiet) | A boolean specifying whether to show the progress update in the console or not | +| [Results output](#results-output) | Specify the results output | +| [RPS](#rps) | The maximum number of requests to make per second globally (discouraged, use [arrival-rate executors](https://grafana.com/docs/k6//using-k6/scenarios/concepts/open-vs-closed) instead) | +| [Scenarios](#scenarios) | Define advanced execution scenarios | +| [Setup timeout](#setup-timeout) | Specify how long the `setup()` function is allow to run before it's terminated | +| [Show logs](#show-logs) | A boolean specifying whether the cloud logs are printed out to the terminal | +| [Stages](#stages) | A list of objects that specify the target number of VUs to ramp up or down; shortcut option for a single [scenario](https://grafana.com/docs/k6//using-k6/scenarios) with a [ramping VUs executor](https://grafana.com/docs/k6//using-k6/scenarios/executors/ramping-vus) | +| [Supply environment variable](#supply-environment-variables) | Add/override environment variable with `VAR=value` | +| [System tags](#system-tags) | Specify which System Tags will be in the collected metrics | +| [Summary export](#summary-export) | Output the [end-of-test summary](https://grafana.com/docs/k6//results-output/end-of-test) report to a JSON file (discouraged, use [handleSummary()](https://grafana.com/docs/k6//results-output/end-of-test/custom-summary) instead) | +| [Summary trend stats](#summary-trend-stats) | Define stats for trend metrics in the [end-of-test summary](https://grafana.com/docs/k6//results-output/end-of-test) | +| [Summary time unit](#summary-time-unit) | Time unit to be used for _all_ time values in the [end-of-test summary](https://grafana.com/docs/k6//results-output/end-of-test) | +| [Tags](#tags) | Specify tags that should be set test-wide across all metrics | +| [Teardown timeout](#teardown-timeout) | Specify how long the teardown() function is allowed to run before it's terminated | +| [Thresholds](#thresholds) | Configure under what conditions a test is successful or not | +| [Throw](#throw) | A boolean specifying whether to throw errors on failed HTTP requests | +| [TLS auth](#tls-auth) | A list of TLS client certificate configuration objects | +| [TLS cipher suites](#tls-cipher-suites) | A list of cipher suites allowed to be used by in SSL/TLS interactions with a server | +| [TLS version](#tls-version) | String or object representing the only SSL/TLS version allowed | +| [Traces output](#traces-output) | Configuration about where traces from k6 should be sent | +| [User agent](#user-agent) | A string specifying the User-Agent header when sending HTTP requests | +| [Verbose](#verbose) | A boolean specifying whether verbose logging is enabled | +| [VUs](#vus) | A number specifying the number of VUs to run concurrently | + +The following sections detail all available options that you can be specify within a script. + +It also documents the equivalent command line flag, environment variables or option when executing `k6 run ...` +and `k6 cloud run ...`, which you can use to override options specified in the code. + +## Address + +Address of the API server. When executing scripts with `k6 run` an HTTP server with a REST API is spun up, +which can be used to control some of the parameters of the test execution. +By default, the server listens on `localhost:6565`. Read more on [k6 REST API](https://grafana.com/docs/k6//misc/k6-rest-api). + +| Env | CLI | Code / Config file | Default | +| --- | ----------------- | ------------------ | ---------------- | +| N/A | `--address`, `-a` | N/A | `localhost:6565` | + +{{< code >}} + +```bash +$ k6 run --address "localhost:3000" script.js +``` + +{{< /code >}} + +## Batch + +The maximum number of simultaneous/parallel connections in total that an +[`http.batch()`](https://grafana.com/docs/k6//javascript-api/k6-http/batch) call in a VU can make. If you have a +`batch()` call that you've given 20 URLs to and `--batch` is set to 15, then the VU will make 15 +requests right away in parallel and queue the rest, executing them as soon as a previous request is +done and a slot opens. Available in both the `k6 run` and the `k6 cloud run` commands + +| Env | CLI | Code / Config file | Default | +| ---------- | --------- | ------------------ | ------- | +| `K6_BATCH` | `--batch` | `batch` | `20` | + +{{< code >}} + +```javascript +export const options = { + batch: 15, +}; +``` + +{{< /code >}} + +## Batch per host + +The maximum number of simultaneous/parallel connections for the same hostname that an +[`http.batch()`](https://grafana.com/docs/k6//javascript-api/k6-http/batch) call in a VU can make. If you have a +`batch()` call that you've given 20 URLs to the _same_ hostname and `--batch-per-host` is set to 5, then the VU will make 5 +requests right away in parallel and queue the rest, executing them as soon as a previous request is +done and a slot opens. This will not run more request in parallel then the value of `batch`. Available in both the `k6 run` and the `k6 cloud run` commands + +| Env | CLI | Code / Config file | Default | +| ------------------- | ------------------ | ------------------ | ------- | +| `K6_BATCH_PER_HOST` | `--batch-per-host` | `batchPerHost` | `6` | + +{{< code >}} + +```javascript +export const options = { + batchPerHost: 5, +}; +``` + +{{< /code >}} + +## Blacklist IP + +Blacklist IP ranges from being called. Available in `k6 run` and `k6 cloud run` commands. + +| Env | CLI | Code / Config file | Default | +| ------------------ | ---------------- | ------------------ | ------- | +| `K6_BLACKLIST_IPS` | `--blacklist-ip` | `blacklistIPs` | `null` | + +{{< code >}} + +```javascript +export const options = { + blacklistIPs: ['10.0.0.0/8'], +}; +``` + +{{< /code >}} + +## Block hostnames + +Blocks hostnames based on a list of glob match strings. The pattern matching string can have a single +`*` at the beginning such as `*.example.com` that will match anything before that such as +`test.example.com` and `test.test.example.com`. +Available in `k6 run` and `k6 cloud run` commands. + +| Env | CLI | Code / Config file | Default | +| -------------------- | ------------------- | ------------------ | ------- | +| `K6_BLOCK_HOSTNAMES` | `--block-hostnames` | `blockHostnames` | `null` | + +{{< code >}} + +```javascript +export const options = { + blockHostnames: ['test.k6.io', '*.example.com'], +}; +``` + +{{< /code >}} + +{{< code >}} + +```bash +$ k6 run --block-hostnames="test.k6.io,*.example.com" script.js +``` + +{{< /code >}} + +## Compatibility mode + +Support running scripts with different ECMAScript compatibility modes. + +Read about the different modes on the [JavaScript Compatibility Mode documentation](https://grafana.com/docs/k6//using-k6/javascript-compatibility-mode). + +| Env | CLI | Code / Config file | Default | +| ----------------------- | ---------------------- | ------------------ | ------------ | +| `K6_COMPATIBILITY_MODE` | `--compatibility-mode` | N/A | `"extended"` | + +{{< code >}} + +```bash +$ k6 run --compatibility-mode=base script.js +``` + +{{< /code >}} + +## Config + +Specify the config file in JSON format. +If the config file is not specified, k6 looks for `config.json` in the `loadimpact/k6` directory inside the regular directory for configuration files on the operating system. +Default config locations on different operating systems are as follows: + +| OS | Default Config Path | +| ---------- | --------------------------------------------------------------- | +| Unix-based | `${HOME}/.config/loadimpact/k6/config.json` | +| macOS | `${HOME}/Library/Application Support/loadimpact/k6/config.json` | +| Windows | `%AppData%/loadimpact/k6/config.json` | + +Available in `k6 run` and `k6 cloud run` commands: + +| Env | CLI | Code / Config file | Default | +| --- | ------------------------------ | ------------------ | ------- | +| N/A | `--config `, `-c ` | N/A | `null` | + +{{< admonition type="note" >}} + +When running tests in k6 Cloud and using a non-default config.json file, +specify the cloud token inside your config file to authenticate. + +{{< /admonition >}} + +## Console output + +Redirects logs logged by `console` methods to the provided output file. Available in `k6 cloud run` and `k6 run` commands. + +| Env | CLI | Code / Config file | Default | +| ------------------- | ------------------ | ------------------ | ------- | +| `K6_CONSOLE_OUTPUT` | `--console-output` | N/A | `null` | + +{{< code >}} + +```bash +$ k6 run --console-output "loadtest.log" script.js +``` + +{{< /code >}} + +## Discard response bodies + +Specify if response bodies should be discarded by changing the default value of +[responseType](https://grafana.com/docs/k6//javascript-api/k6-http/params) to `none` for all HTTP requests. Highly recommended to be set +to `true` and then only for the requests where the response body is needed for scripting +to set [responseType](https://grafana.com/docs/k6//javascript-api/k6-http/params) to `text` or `binary`. Lessens the amount of memory +required and the amount of GC - reducing the load on the testing machine, and probably producing +more reliable test results. + +| Env | CLI | Code / Config file | Default | +| ---------------------------- | --------------------------- | ----------------------- | ------- | +| `K6_DISCARD_RESPONSE_BODIES` | `--discard-response-bodies` | `discardResponseBodies` | `false` | + +{{< code >}} + +```javascript +export const options = { + discardResponseBodies: true, +}; +``` + +{{< /code >}} + +## DNS + +This is a composite option that provides control of DNS resolution behavior with +configuration for cache expiration (TTL), IP selection strategy and IP version +preference. The TTL field in the DNS record is currently not read by k6, so the +`ttl` option allows manual control over this behavior, albeit as a fixed value +for the duration of the test run. + +Note that DNS resolution is done only on new HTTP connections, and by default k6 +will try to reuse connections if HTTP keep-alive is supported. To force a certain +DNS behavior consider enabling the [`noConnectionReuse`](#no-connection-reuse) +option in your tests. + +| Env | CLI | Code / Config file | Default | +| -------- | ------- | ------------------ | ---------------------------------------- | +| `K6_DNS` | `--dns` | `dns` | `ttl=5m,select=random,policy=preferIPv4` | + +Possible `ttl` values are: + +- `0`: no caching at all - each request will trigger a new DNS lookup. +- `inf`: cache any resolved IPs for the duration of the test run. +- any time duration like `60s`, `5m30s`, `10m`, `2h`, etc.; if no unit is specified (e.g. `ttl=3000`), k6 assumes milliseconds. + +Possible `select` values are: + +- `first`: always pick the first resolved IP. +- `random`: pick a random IP for every new connection. +- `roundRobin`: iterate sequentially over the resolved IPs. + +Possible `policy` values are: + +- `preferIPv4`: use IPv4 addresses if available, otherwise fall back to IPv6. +- `preferIPv6`: use IPv6 addresses if available, otherwise fall back to IPv4. +- `onlyIPv4`: only use IPv4 addresses, ignore any IPv6 ones. +- `onlyIPv6`: only use IPv6 addresses, ignore any IPv4 ones. +- `any`: no preference, use all addresses. + +Here are some configuration examples: + +```bash +K6_DNS="ttl=5m,select=random,policy=preferIPv4" k6 cloud run script.js +``` + +{{< code >}} + +```javascript +export const options = { + dns: { + ttl: '1m', + select: 'roundRobin', + policy: 'any', + }, +}; +``` + +{{< /code >}} + +## Duration + +A string specifying the total duration a test run should be run for. During this time each +VU will execute the script in a loop. Available in `k6 run` and `k6 cloud run` commands. + +Together with the [`vus` option](#vus), `duration` is a shortcut for a single [scenario](https://grafana.com/docs/k6//using-k6/scenarios) with a [constant VUs executor](https://grafana.com/docs/k6//using-k6/scenarios/executors/constant-vus). + +| Env | CLI | Code / Config file | Default | +| ------------- | ------------------ | ------------------ | ------- | +| `K6_DURATION` | `--duration`, `-d` | `duration` | `null` | + +{{< code >}} + +```javascript +export const options = { + vus: 100, + duration: '3m', +}; +``` + +{{< /code >}} + +## Cloud options + +An object used to set configuration options for Grafana Cloud k6. For more information about available parameters, refer to [Cloud options](https://grafana.com/docs/grafana-cloud/testing/k6/author-run/cloud-scripting-extras/cloud-options/). + +| Env | CLI | Code / Config file | Default | +| --- | --- | ------------------ | ------- | +| N/A | N/A | `cloud` | `null` | + +This is an example of how to specify the test name (test runs/executions with the same name will be +logically grouped for trending and comparison) when streaming results to +[Grafana Cloud k6](https://grafana.com/docs/grafana-cloud/testing/k6/). + +{{< code >}} + +```javascript +export const options = { + cloud: { + name: 'My test name', + }, +}; +``` + +Previously, the `cloud` object was known as `ext.loadimpact`. + +{{< /code >}} + +## Execution segment + +These options specify how to partition the test run and which segment to run. +If defined, k6 will scale the number of VUs and iterations to be run for that +segment, which is useful in distributed execution. Available in `k6 run` and +`k6 cloud run` commands. + +| Env | CLI | Code / Config file | Default | +| --- | ------------------------------ | -------------------------- | ------- | +| N/A | `--execution-segment` | `executionSegment` | `"0:1"` | +| N/A | `--execution-segment-sequence` | `executionSegmentSequence` | `"0,1"` | + +For example, to run 25% of a test, you would specify `--execution-segment '25%'`, +which would be equivalent to `--execution-segment '0:1/4'`, i.e. run the first +1/4 of the test. +To ensure that each instance executes a specific segment, also specify the full +segment sequence, e.g. `--execution-segment-sequence '0,1/4,1/2,1'`. +This way one instance could run with `--execution-segment '0:1/4'`, another with +`--execution-segment '1/4:1/2'`, etc. and there would be no overlap between them. + + + +## Exit on running + +A boolean, specifying whether the script should exit once the test status reaches `running`. +When running scripts with `k6 cloud run` by default scripts will run until the test reaches a finalized status. +This could be problematic in certain environments (think of Continuous Integration and Delivery pipelines), +since you'd need to wait until the test ends up in a finalized state. + +With this option, you can exit early and let the script run in the background. Available in `k6 cloud run` command. + +| Env | CLI | Code / Config file | Default | +| -------------------- | ------------------- | ------------------ | ------- | +| `K6_EXIT_ON_RUNNING` | `--exit-on-running` | N/A | `false` | + +{{< code >}} + +```bash +$ k6 cloud run --exit-on-running script.js +``` + +{{< /code >}} + +## Hosts + +An object with overrides to DNS resolution, similar to what you can do with `/etc/hosts` on +Linux/Unix or `C:\Windows\System32\drivers\etc\hosts` on Windows. For instance, you could set +up an override which routes all requests for `test.k6.io` to `1.2.3.4`. + +k6 also supports ways to narrow or widen the scope of your redirects: + +- You can redirect only from or to certain ports. +- Starting from v0.42.0, you can use an asterisk (`*`) as a wild card at the start of the host name to avoid repetition. For example, `*.k6.io` would apply the override for all subdomains of `k6.io`. + +{{< admonition type="note" >}} + +This does not modify the actual HTTP `Host` header, but rather where it will be routed. + +{{< /admonition >}} + +| Env | CLI | Code / Config file | Default | +| --- | --- | ------------------ | ------- | +| N/A | N/A | `hosts` | `null` | + +{{< code >}} + +```javascript +export const options = { + hosts: { + 'test.k6.io': '1.2.3.4', + 'test.k6.io:443': '1.2.3.4:8443', + '*.grafana.com': '1.2.3.4', + }, +}; +``` + +{{< /code >}} + +The preceding code will redirect requests made to `test.k6.io` to `1.2.3.4`, keeping the same port. If the request is done to port `443`, it will redirect it to port `8443` instead. It will also redirect requests to any subdomain of `grafana.com` to `1.2.3.4`. + +## HTTP debug + +Log all HTTP requests and responses. Excludes body by default, to include body use +`--http-debug=full`. Available in `k6 run` and `k6 cloud run` commands. + +Read more [here](https://grafana.com/docs/k6//using-k6/http-debugging). + +| Env | CLI | Code / Config file | Default | +| --------------- | --------------------------------------- | ------------------ | ------- | +| `K6_HTTP_DEBUG` | `--http-debug`,
`--http-debug=full` | `httpDebug` | `false` | + +{{< code >}} + +```javascript +export const options = { + httpDebug: 'full', +}; +``` + +{{< /code >}} + +## Include system env vars + +Pass the real system [environment variables](https://grafana.com/docs/k6//using-k6/environment-variables) to the runtime. Available in `k6 run` and `k6 cloud run` commands. + +| Env | CLI | Code / Config file | Default | +| --- | --------------------------- | ------------------ | ---------------------------------------------------------------------------------------------------- | +| N/A | `--include-system-env-vars` | N/A | `true` for `k6 run`, but `false` for all other commands to prevent inadvertent sensitive data leaks. | + +{{< code >}} + +```bash +$ k6 run --include-system-env-vars ~/script.js +``` + +{{< /code >}} + +## Insecure skip TLS verify + +A boolean, true or false. When this option is enabled (set to true), all of the verifications that +would otherwise be done to establish trust in a server provided TLS certificate will be ignored. +This only applies to connections created from code, such as HTTP requests. +Available in `k6 run` and `k6 cloud run` commands + +| Env | CLI | Code / Config file | Default | +| ----------------------------- | ---------------------------- | ----------------------- | ------- | +| `K6_INSECURE_SKIP_TLS_VERIFY` | `--insecure-skip-tls-verify` | `insecureSkipTLSVerify` | `false` | + +{{< code >}} + +```javascript +export const options = { + insecureSkipTLSVerify: true, +}; +``` + +{{< /code >}} + +## Iterations + +An integer value, specifying the total number of iterations of the `default` function to execute in the test run, as opposed to specifying a duration of time during which the script would run in a loop. Available both in the `k6 run` and `k6 cloud run` commands. + +Together with the [`vus` option](#vus), `iterations` is a shortcut for a single [scenario](https://grafana.com/docs/k6//using-k6/scenarios) with a [shared iterations executor](https://grafana.com/docs/k6//using-k6/scenarios/executors/shared-iterations). + +By default, the maximum duration of a `shared-iterations` scenario is 10 minutes. You can adjust that time via the `maxDuration` option of the scenario, or by also specifying the [`duration` global shortcut option](#duration). + +**Note that iterations aren't fairly distributed with this option, and a VU that executes faster will complete more iterations than others.** Each VU will try to complete as many iterations as possible, ["stealing"](https://en.wikipedia.org/wiki/Work_stealing) them from the total number of iterations for the test. So, depending on iteration times, some VUs may complete more iterations than others. If you want guarantees that every VU will complete a specific, fixed number of iterations, [use the per-VU iterations executor](https://grafana.com/docs/k6//using-k6/scenarios/executors/per-vu-iterations). + +| Env | CLI | Code / Config file | Default | +| --------------- | -------------------- | ------------------ | ------- | +| `K6_ITERATIONS` | `--iterations`, `-i` | `iterations` | `1` | + +{{< code >}} + +```javascript +export const options = { + vus: 5, + iterations: 10, +}; +``` + +{{< /code >}} + +Or, to run 10 VUs 10 times each: + +{{< code >}} + +```javascript +export const options = { + vus: 10, + iterations: 100, +}; +``` + +{{< /code >}} + +## Linger + +A boolean, true or false, specifying whether the k6 process should linger around after test +run completion. Available in the `k6 run` command. + +| Env | CLI | Code / Config file | Default | +| ----------- | ---------------- | ------------------ | ------- | +| `K6_LINGER` | `--linger`, `-l` | `linger` | `false` | + +{{< code >}} + +```javascript +export const options = { + linger: true, +}; +``` + +{{< /code >}} + +## Local IPs + +A list of IPs, IP ranges and CIDRs from which VUs will make requests. The IPs will be sequentially +given out to VUs. This option doesn't change anything on the OS level so the IPs need to be already +configured on the OS level for k6 to use them. Also IPv4 CIDRs with more than 2 +IPs don't include the first and last IP as they are reserved for referring to the network itself and +the broadcast address respectively. + +This option can be used for splitting the network traffic from k6 between multiple network cards, thus potentially increasing the available network throughput. For example, if you have 2 NICs, you can run k6 with `--local-ips=","` to balance the traffic equally between them - half of the VUs will use the first IP and the other half will use the second. This can scale to any number of NICs, and you can repeat some local IPs to give them more traffic. For example, `--local-ips=",,,"` will split VUs between 3 different source IPs in a 25%:25%:50% ratio. + +Available in the `k6 run` command. + +| Env | CLI | Code / Config file | Default | +| -------------- | ------------- | ------------------ | ------- | +| `K6_LOCAL_IPS` | `--local-ips` | N/A | N/A | + +{{< code >}} + +```bash +$ k6 run --local-ips=192.168.20.12-192.168.20.15,192.168.10.0/27 script.js +``` + +{{< /code >}} + +## Log output + +This option specifies where to send logs to and another configuration connected to it. Available in the `k6 run` command. + +| Env | CLI | Code / Config file | Default | +| --------------- | -------------- | ------------------ | -------- | +| `K6_LOG_OUTPUT` | `--log-output` | N/A | `stderr` | + +{{< code >}} + +```bash +$ k6 run --log-output=stdout script.js +``` + +{{< /code >}} + +Possible values are: + +- none - disable +- stdout - send to the standard output +- stderr - send to the standard error output (this is the default) +- loki - send logs to a loki server +- file - write logs to a file + +### Loki + +Use the `log-output` option to configure [Loki](https://grafana.com/oss/loki/) as follows. +For additional instructions and a step-by-step guide, check out the [Loki tutorial](https://k6.io/blog/using-loki-to-store-and-query-k6-logs/). + +{{< code >}} + +```bash +$ k6 run --log-output=loki=http://127.0.0.1:3100/loki/api/v1/push,label.something=else,label.foo=bar,limit=32,level=info,pushPeriod=5m32s,msgMaxSize=1231 script.js +``` + +{{< /code >}} + +Where all but the url in the beginning are not required. +The possible keys with their meanings and default values: + +| key | description | default value | +| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- | +| `nothing` | the endpoint to which to send logs | `http://127.0.0.1:3100/loki/api/v1/push` | +| allowedLabels | if set k6 will send only the provided labels as such and all others will be appended to the message in the form `key=value`. The value of the option is in the form `[label1,label2]` | N/A | +| label.`labelName` | adds an additional label with the provided key and value to each message | N/A | +| header.`headerName` | adds an additional HTTP header with the provided header name and value to each HTTP request made to Loki | N/A | +| limit | the limit of message per pushPeriod, an additional log is send when the limit is reached, logging how many logs were dropped | 100 | +| level | the minimal level of a message so it's send to loki | all | +| pushPeriod | at what period to send log lines | 1s | +| profile | whether to print some info about performance of the sending to loki | false | +| msgMaxSize | how many symbols can there be at most in a message. Messages bigger will miss the middle of the message with an additional few characters explaining how many characters were dropped. | 1048576 | + +### File + +The file can be configured as below, where an explicit file path is required: + +{{< code >}} + +```bash +$ k6 run --log-output=file=./k6.log script.js +``` + +{{< /code >}} + +A valid file path is the unique mandatory field, the other optional fields listed below: + +| key | description | default value | +| ----- | --------------------------------------------------------------------------------------------------------------------- | ------------- | +| level | the minimal level of a message to write out of (in ascending order): trace, debug, info, warning, error, fatal, panic | trace | + +## LogFormat + +A value specifying the log format. By default, k6 includes extra debug information like date and log level. The other options available are: + +- `json`: print all the debug information in JSON format. + +- `raw`: print only the log message. + +| Env | CLI | Code / Config file | Default | +| --------------- | -------------------- | ------------------ | ------- | +| `K6_LOG_FORMAT` | `--log-format`, `-f` | N/A | | + +{{< code >}} + +```bash +$ k6 run --log-format raw test.js +``` + +{{< /code >}} + +## Max redirects + +The maximum number of HTTP redirects that k6 will follow before giving up on a request and +erroring out. Available in both the `k6 run` and the `k6 cloud run` commands. + +| Env | CLI | Code / Config file | Default | +| ------------------ | ----------------- | ------------------ | ------- | +| `K6_MAX_REDIRECTS` | `--max-redirects` | `maxRedirects` | `10` | + +{{< code >}} + +```javascript +export const options = { + maxRedirects: 10, +}; +``` + +{{< /code >}} + +## Minimum iteration duration + +Specifies the minimum duration of every single execution (i.e. iteration) of the `default` +function. Any iterations that are shorter than this value will cause that VU to sleep for +the remainder of the time until the specified minimum duration is reached. + +| Env | CLI | Code / Config file | Default | +| --------------------------- | -------------------------- | ---------------------- | -------------- | +| `K6_MIN_ITERATION_DURATION` | `--min-iteration-duration` | `minIterationDuration` | `0` (disabled) | + +{{< code >}} + +```javascript +export const options = { + minIterationDuration: '10s', +}; +``` + +{{< /code >}} + +## No color + +A boolean specifying whether colored output is disabled. Available in `k6 run` and `k6 cloud run` commands. + +| Env | CLI | Code / Config file | Default | +| --- | ------------ | ------------------ | ------- | +| N/A | `--no-color` | N/A | `false` | + +{{< code >}} + +```bash +$ k6 run --no-color script.js +``` + +{{< /code >}} + +## No connection reuse + +A boolean, true or false, specifying whether k6 should disable keep-alive connections. +Available in `k6 run` and `k6 cloud run` commands. + +| Env | CLI | Code / Config file | Default | +| ------------------------ | ----------------------- | ------------------- | ------- | +| `K6_NO_CONNECTION_REUSE` | `--no-connection-reuse` | `noConnectionReuse` | `false` | + +{{< code >}} + +```javascript +export const options = { + noConnectionReuse: true, +}; +``` + +{{< /code >}} + +## No cookies reset + +This disables the default behavior of resetting the cookie jar after each VU iteration. If +it's enabled, saved cookies will be persisted across VU iterations. + +| Env | CLI | Code / Config file | Default | +| --------------------- | --- | ------------------ | ------- | +| `K6_NO_COOKIES_RESET` | N/A | `noCookiesReset` | `false` | + +{{< code >}} + +```javascript +export const options = { + noCookiesReset: true, +}; +``` + +{{< /code >}} + +## No summary + +Disables [end-of-test summary](https://grafana.com/docs/k6//results-output/end-of-test) generation, +including calls to [`handleSummary()`](https://grafana.com/docs/k6//results-output/end-of-test/custom-summary) and `--summary-export`. + +Available in the `k6 run` command. + +| Env | CLI | Code / Config file | Default | +| --------------- | -------------- | ------------------ | ------- | +| `K6_NO_SUMMARY` | `--no-summary` | N/A | `false` | + +{{< code >}} + +```bash +$ k6 run --no-summary ~/script.js +``` + +{{< /code >}} + +## No setup + +A boolean specifying whether `setup()` function should be run. Available in `k6 cloud run` and `k6 run` commands. + +| Env | CLI | Code / Config file | Default | +| ------------- | ------------ | ------------------ | ------- | +| `K6_NO_SETUP` | `--no-setup` | N/A | `false` | + +{{< code >}} + +```bash +$ k6 run --no-setup script.js +``` + +{{< /code >}} + +## No teardown + +A boolean specifying whether `teardown()` function should be run. Available in `k6 cloud run` and `k6 run` commands. + +| Env | CLI | Code / Config file | Default | +| ---------------- | --------------- | ------------------ | ------- | +| `K6_NO_TEARDOWN` | `--no-teardown` | N/A | `false` | + +{{< code >}} + +```bash +$ k6 run --no-teardown script.js +``` + +{{< /code >}} + +## No thresholds + +Disables threshold execution. Available in the `k6 run` command. + +| Env | CLI | Code / Config file | Default | +| ------------------ | ----------------- | ------------------ | ------- | +| `K6_NO_THRESHOLDS` | `--no-thresholds` | N/A | `false` | + +{{< code >}} + +```bash +$ k6 run --no-thresholds ~/script.js +``` + +{{< /code >}} + +## No usage report + +A boolean, true or false. By default, k6 sends a usage report each time it is run, so that we can +track how often people use it. If this option is set to true, no usage report will be made. To +learn more, have a look at the [Usage reports](https://grafana.com/docs/k6//set-up/usage-collection) documentation. Available in +`k6 run` commands. + +| Env | CLI | Config file | Default | +| -------------------- | ------------------- | ----------------- | ------- | +| `K6_NO_USAGE_REPORT` | `--no-usage-report` | `noUsageReport`\* | `false` | + +{{< code >}} + +```bash +$ k6 run --no-usage-report ~/script.js +``` + +{{< /code >}} + +\* Note that this option is not supported in the exported script options, but can be specified in a configuration file. + +## No VU connection reuse + +A boolean, true or false, specifying whether k6 should reuse TCP connections between iterations +of a VU. Available in `k6 run` and `k6 cloud run` commands. + +| Env | CLI | Code / Config file | Default | +| --------------------------- | -------------------------- | --------------------- | ------- | +| `K6_NO_VU_CONNECTION_REUSE` | `--no-vu-connection-reuse` | `noVUConnectionReuse` | `false` | + +{{< code >}} + +```javascript +export const options = { + noVUConnectionReuse: true, +}; +``` + +{{< /code >}} + +## Paused + +A boolean, true or false, specifying whether the test should start in a paused state. To resume +a paused state you'd use the `k6 resume` command. Available in `k6 run` and `k6 cloud run` commands. + +| Env | CLI | Code / Config file | Default | +| ----------- | ---------------- | ------------------ | ------- | +| `K6_PAUSED` | `--paused`, `-p` | `paused` | `false` | + +{{< code >}} + +```javascript +export const options = { + paused: true, +}; +``` + +{{< /code >}} + +## Profiling Enabled + +Enables [pprof](https://pkg.go.dev/net/http/pprof) profiling endpoints under the k6's REST API [address](#address). These endpoints help debug and profile k6 itself. k6's REST API should be enabled as well. + +| Env | CLI | Code / Config file | Default | +| ---------------------- | --------------------- | ------------------ | ------------------------------------ | +| `K6_PROFILING_ENABLED` | `--profiling-enabled` | N/A | `http://localhost:6565/debug/pprof/` | + +{{< code >}} + +```bash +$ k6 run --profiling-enabled script.js +``` + +{{< /code >}} + +## Quiet + +A boolean, true or false, that disables the progress update bar on the console output. Available in `k6 run` and `k6 cloud run` commands. + +| Env | CLI | Code / Config file | Default | +| --- | --------------- | ------------------ | ------- | +| N/A | `--quiet`, `-q` | N/A | `false` | + +{{< code >}} + +```bash +$ k6 run script.js -d 20s --quiet +``` + +{{< /code >}} + +## Results output + +Specify the results output. +For information on all available outputs and how to configure them, +refer to [Results output](https://grafana.com/docs/k6//results-output/). + +| Env | CLI | Code / Config file | Default | +| -------- | ------------- | ------------------ | ------- | +| `K6_OUT` | `--out`, `-o` | N/A | `null` | + +{{< code >}} + +```bash +$ k6 run --out influxdb=http://localhost:8086/k6 script.js +``` + +{{< /code >}} + +## RPS + +The maximum number of requests to make per second, in total across all VUs. Available in `k6 run` and `k6 cloud run` commands. + +{{< admonition type="caution" >}} + +**This option is discouraged.** +
+
+The `--rps` option has caveats and is difficult to use correctly. +
+
+For example, in the cloud or distributed execution, this option affects every k6 instance independently. +That is, it is not sharded like VUs are. +
+
+We strongly recommend the [arrival-rate executors](https://grafana.com/docs/k6//using-k6/scenarios/concepts/open-vs-closed) to simulate constant RPS instead of this option. + +{{< /admonition >}} + +| Env | CLI | Code / Config file | Default | +| -------- | ------- | ------------------ | --------------- | +| `K6_RPS` | `--rps` | `rps` | `0` (unlimited) | + +{{< code >}} + +```javascript +export const options = { + rps: 500, +}; +``` + +{{< /code >}} + +> ### Considerations when running in the cloud +> +> The option is set per load generator which means that the value you set in the options object of your test script will be multiplied by the number of load generators your test run is using. At the moment we are hosting 300 VUs per load generator instance. In practice that means that if you set the option for 100 rps, and run a test with 1000 VUs, you will spin up 4 load gen instances and effective rps limit of your test run will be 400 + +## Scenarios + +Define one or more execution patterns, with various VU and iteration scheduling +settings, running different exported functions (besides `default`!), using different +environment variables, tags, and more. + +See the [Scenarios](https://grafana.com/docs/k6//using-k6/scenarios) article for details and more examples. + +Available in `k6 run` and `k6 cloud run` commands. + +| Env | CLI | Code / Config file | Default | +| --- | --- | ------------------ | ------- | +| N/A | N/A | `scenarios` | `null` | + +{{< code >}} + +```javascript +export const options = { + scenarios: { + my_api_scenario: { + // arbitrary scenario name + executor: 'ramping-vus', + startVUs: 0, + stages: [ + { duration: '5s', target: 100 }, + { duration: '5s', target: 0 }, + ], + gracefulRampDown: '10s', + env: { MYVAR: 'example' }, + tags: { my_tag: 'example' }, + }, + }, +}; +``` + +{{< /code >}} + +## Setup timeout + +Specify how long the `setup()` function can run before it's terminated and the test fails. If defined, the value must be positive. + +| Env | CLI | Code / Config file | Default | +| ------------------ | --- | ------------------ | ------- | +| `K6_SETUP_TIMEOUT` | N/A | `setupTimeout` | `"60s"` | + +{{< code >}} + +```javascript +export const options = { + setupTimeout: '30s', +}; +``` + +{{< /code >}} + +## Show logs + +A boolean specifying whether the cloud logs are printed out to the terminal. Available in `k6 cloud run` command. + +| Env | CLI | Code / Config file | Default | +| --- | ------------- | ------------------ | ------- | +| N/A | `--show-logs` | N/A | `true` | + +{{< code >}} + +```bash +$ k6 cloud run --show-logs=false script.js +``` + +{{< /code >}} + +## Stages + +A list of VU `{ target: ..., duration: ... }` objects that specify the target number of VUs to +ramp up or down to for a specific period. Available in `k6 run` and `k6 cloud run` commands. + +It is a shortcut option for a single [scenario](https://grafana.com/docs/k6//using-k6/scenarios) with a [ramping VUs executor](https://grafana.com/docs/k6//using-k6/scenarios/executors/ramping-vus). If used together with the [VUs](#vus) option, the `vus` value is used as the `startVUs` option of the executor. + +| Env | CLI | Code / Config file | Default | +| ----------- | ------------------------------------------------------- | ------------------ | ------------------------------ | +| `K6_STAGES` | `--stage :`, `-s :` | `stages` | Based on `vus` and `duration`. | + +{{< code >}} + +```javascript +// The following config would have k6 ramping up from 1 to 10 VUs for 3 minutes, +// then staying flat at 10 VUs for 5 minutes, then ramping up from 10 to 35 VUs +// over the next 10 minutes before finally ramping down to 0 VUs for another +// 3 minutes. + +export const options = { + stages: [ + { duration: '3m', target: 10 }, + { duration: '5m', target: 10 }, + { duration: '10m', target: 35 }, + { duration: '3m', target: 0 }, + ], +}; +``` + +```bash +$ k6 run --stage 5s:10,5m:20,10s:5 script.js + +# or... + +$ K6_STAGES="5s:10,5m:20,10s:5" k6 run script.js +``` + +```windows +C:\k6> k6 run --stage 5s:10,5m:20,10s:5 script.js + +# or... + +C:\k6> set "K6_STAGES=5s:10,5m:20,10s:5" && k6 run script.js + +``` + +```powershell +C:\k6> k6 run --stage 5s:10,5m:20,10s:5 script.js + +# or... + +C:\k6> $env:K6_STAGES="5s:10,5m:20,10s:5"; k6 run script.js + +``` + +{{< /code >}} + +## Summary export + +Save the end-of-test summary report to a JSON file that includes data for all test metrics, checks and thresholds. +This is useful to get the aggregated test results in a machine-readable format, for integration with dashboards, external alerts, CI pipelines, etc. + +While this feature is not deprecated yet, [we now discourage it](https://grafana.com/docs/k6//results-output/end-of-test#summary-export-to-a-json-file). +For a better, more flexible JSON export, as well as export of the summary data to different formats (e.g. JUnit/XUnit/etc. XML, HTML, .txt) and complete summary customization, use the [`handleSummary()` function](https://grafana.com/docs/k6//results-output/end-of-test/custom-summary). + +Available in the `k6 run` command. + +| Env | CLI | Code / Config file | Default | +| ------------------- | ----------------------------- | ------------------ | ------- | +| `K6_SUMMARY_EXPORT` | `--summary-export ` | N/A | `null` | + +{{< code >}} + +```bash +$ k6 run --summary-export export.json script.js + +# or... + +$ K6_SUMMARY_EXPORT="export.json" k6 run script.js +``` + +```windows +C:\k6> k6 run --summary-export export.json script.js + +# or... + +C:\k6> set "K6_SUMMARY_EXPORT=export.json" && k6 run script.js + +``` + +```powershell +C:\k6> k6 run --summary-export export.json script.js + +# or... + +C:\k6> $env:K6_SUMMARY_EXPORT="export.json"; k6 run script.js + +``` + +{{< /code >}} + +See an example file on the [Results Output](https://grafana.com/docs/k6//get-started/results-output#summary-export) page. + +## Supply environment variables + +Add/override an [environment variable](https://grafana.com/docs/k6//using-k6/environment-variables) with `VAR=value`in a k6 script. Available in `k6 run` and `k6 cloud` commands. + +To make the system environment variables available in the k6 script via `__ENV`, use the [`--include-system-env-vars` option](#include-system-env-vars). + +{{< admonition type="note" >}} + +**The `-e` flag does not configure options.** + +This flag just provides variables to the script, which the script can use or ignore. +For example, `-e K6_ITERATIONS=120` does _not_ configure the script iterations. + +Compare this behavior with `K6_ITERATIONS=120 k6 run script.js`, which _does_ set iterations. + +{{< /admonition >}} + +| Env | CLI | Code / Config file | Default | +| --- | ------------- | ------------------ | ------- | +| N/A | `--env`, `-e` | N/A | `null` | + +{{< code >}} + +```bash +$ k6 run -e FOO=bar ~/script.js +``` + +{{< /code >}} + +## System tags + +Specify which [system tags](https://grafana.com/docs/k6//using-k6/tags-and-groups#system-tags) will be in the collected +metrics. Some collectors like the `cloud` one may require that certain system tags be used. +You can specify the tags as an array from the JS scripts or as a comma-separated list via the +CLI. Available in `k6 run` and `k6 cloud` commands + +| Env | CLI | Code / Config file | Default | +| ---------------- | --------------- | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------- | +| `K6_SYSTEM_TAGS` | `--system-tags` | `systemTags` | `proto`,`subproto`,`status`,`method`,`url`,`name`,`group`,`check`,`error`,`error_code`,`tls_version`,`scenario`,`service`,`expected_response` | + +{{< code >}} + +```javascript +export const options = { + systemTags: ['status', 'method', 'url'], +}; +``` + +{{< /code >}} + +## Summary time unit + +Define which time unit will be used for _all_ time values in the [end-of-test summary](https://grafana.com/docs/k6//results-output/end-of-test). Possible values are `s` (seconds), `ms` (milliseconds) and `us` (microseconds). If no value is specified, k6 will use mixed time units, choosing the most appropriate unit for each value. + +| Env | CLI | Code / Config file | Default | +| ---------------------- | --------------------- | ------------------ | ------- | +| `K6_SUMMARY_TIME_UNIT` | `--summary-time-unit` | `summaryTimeUnit` | `null` | + +{{< code >}} + +```javascript +export const options = { + summaryTimeUnit: 'ms', +}; +``` + +{{< /code >}} + +## Summary trend stats + +Define which stats for [`Trend` metrics](https://grafana.com/docs/k6//javascript-api/k6-metrics/trend) (e.g. response times, group/iteration durations, etc.) will be shown in the [end-of-test summary](https://grafana.com/docs/k6//results-output/end-of-test). Possible values include `avg` (average), `med` (median), `min`, `max`, `count`, as well as arbitrary percentile values (e.g. `p(95)`, `p(99)`, `p(99.99)`, etc.). + +For further summary customization and exporting the summary in various formats (e.g. JSON, JUnit/XUnit/etc. XML, HTML, .txt, etc.), use the [`handleSummary()` function](https://grafana.com/docs/k6//results-output/end-of-test/custom-summary). + +| Env | CLI | Code / Config file | Default | +| ------------------------ | ----------------------- | ------------------- | ----------------------------- | +| `K6_SUMMARY_TREND_STATS` | `--summary-trend-stats` | `summaryTrendStats` | `avg,min,med,max,p(90),p(95)` | + +{{< code >}} + +```javascript +export const options = { + summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(95)', 'p(99)', 'p(99.99)', 'count'], +}; +``` + +{{< /code >}} + +{{< code >}} + +```bash +$ k6 run --summary-trend-stats="avg,min,med,max,p(90),p(99.9),p(99.99),count" ./script.js +``` + +{{< /code >}} + +## Tags + +Specify tags that should be set test wide across all metrics. If a tag with the same name has +been specified on a request, check or custom metrics it will have precedence over a test wide +tag. Available in `k6 run` and `k6 cloud` commands. + +| Env | CLI | Code / Config file | Default | +| --- | ------------------ | ------------------ | ------- | +| N/A | `--tag NAME=VALUE` | `tags` | `null` | + +{{< code >}} + +```javascript +export const options = { + tags: { + name: 'value', + }, +}; +``` + +{{< /code >}} + +## Teardown timeout + +Specify how long the `teardown()` function is allowed to run before it's terminated and the test +fails. + +| Env | CLI | Code / Config file | Default | +| --------------------- | --- | ------------------ | ------- | +| `K6_TEARDOWN_TIMEOUT` | N/A | `teardownTimeout` | `"60s"` | + +{{< code >}} + +```javascript +export const options = { + teardownTimeout: '30s', +}; +``` + +{{< /code >}} + +## Thresholds + +A collection of threshold specifications to configure under what condition(s) a test is considered +successful or not, when it has passed or failed, based on metric data. To learn more, have a look +at the [Thresholds](https://grafana.com/docs/k6//using-k6/thresholds) documentation. Available in `k6 run` commands. + +| Env | CLI | Code / Config file | Default | +| --- | --- | ------------------ | ------- | +| N/A | N/A | `thresholds` | `null` | + +{{< code >}} + +```javascript +export const options = { + thresholds: { + 'http_req_duration': ['avg<100', 'p(95)<200'], + 'http_req_connecting{cdnAsset:true}': ['p(95)<100'], + }, +}; +``` + +{{< /code >}} + +## Throw + +A boolean, true or false, specifying whether k6 should throw exceptions when certain errors occur, or if it should just log them with a warning. Behaviors that currently depend on this option: + +- Failing to make [HTTP requests](https://grafana.com/docs/k6//javascript-api/k6-http), e.g. due to a network error. +- Adding invalid values to [custom metrics](https://grafana.com/docs/k6//using-k6/metrics#custom-metrics). +- Setting invalid [per-VU metric tags](https://grafana.com/docs/k6//javascript-api/k6-execution#tags). + +Available in `k6 run` and `k6 cloud` commands. + +| Env | CLI | Code / Config file | Default | +| ---------- | --------------- | ------------------ | ------- | +| `K6_THROW` | `--throw`, `-w` | `throw` | `false` | + +{{< code >}} + +```javascript +export const options = { + throw: true, +}; +``` + +{{< /code >}} + +## TLS auth + +A list of TLS client certificate configuration objects. `domains` and `password` are optional, but `cert` and `key` are required. + +| Env | CLI | Code / Config file | Default | +| --- | --- | ------------------ | ------- | +| N/A | N/A | `tlsAuth` | `null` | + +{{< code >}} + +```javascript +export const options = { + tlsAuth: [ + { + domains: ['example.com'], + cert: open('mycert.pem'), + key: open('mycert-key.pem'), + password: 'mycert-passphrase', + }, + ], +}; +``` + +{{< /code >}} + +## TLS cipher suites + +A list of cipher suites allowed to be used by in SSL/TLS interactions with a server. +For a full listing of available ciphers go [here](https://golang.org/pkg/crypto/tls/#pkg-constants). + +{{< admonition type="caution" >}} + +Due to limitations in the underlying [go implementation](https://github.com/golang/go/issues/29349), changing the ciphers for TLS 1.3 is _not_ supported and will do nothing. + +{{< /admonition >}} + +| Env | CLI | Code / Config file | Default | +| --- | --- | ------------------ | ------------------------- | +| N/A | N/A | `tlsCipherSuites` | `null` (Allow all suites) | + +{{< code >}} + +```javascript +export const options = { + tlsCipherSuites: ['TLS_RSA_WITH_RC4_128_SHA', 'TLS_RSA_WITH_AES_128_GCM_SHA256'], +}; +``` + +{{< /code >}} + +## TLS version + +Either a string representing the only SSL/TLS version allowed to be used in interactions with a +server, or an object specifying the "min" and "max" versions allowed to be used. + +| Env | CLI | Code / Config file | Default | +| --- | --- | ------------------ | --------------------------- | +| N/A | N/A | `tlsVersion` | `null` (Allow all versions) | + +{{< code >}} + +```javascript +export const options = { + tlsVersion: 'tls1.2', +}; +``` + +{{< /code >}} + +{{< code >}} + +```javascript +export const options = { + tlsVersion: { + min: 'ssl3.0', + max: 'tls1.2', + }, +}; +``` + +{{< /code >}} + +## Traces output + +This option specifies where to send traces to. Available in the `k6 run` command. + +| Env | CLI | Code / Config file | Default | +| ------------------ | ----------------- | ------------------ | ------- | +| `K6_TRACES_OUTPUT` | `--traces-output` | N/A | `none` | + +{{< code >}} + +```bash +$ k6 run --traces-output=otel script.js +``` + +{{< /code >}} + +Possible values are: + +- none - disable +- otel - send traces to an OTEL compatible backend + +### OTEL + +Use the `traces-output` option to configure [Open Telemetry](https://opentelemetry.io/) compatible output as follows. + +{{< code >}} + +```bash +$ k6 run --traces-output=otel=http://127.0.0.1:4318,proto=http,header.AdditionalHeader=example script.js +``` + +{{< /code >}} + +Where none of the options are required. +The possible keys with their meanings and default values: + +| key | description | default value | +| ------------------- | ---------------------------------------------------------------------------------------------------------------------- | ----------------------- | +| `nothing` | the endpoint to which send traces to | `http://127.0.0.1:4317` | +| proto | the protocol to use when connecting with the traces backend | `grpc` | +| header.`headerName` | adds an additional HTTP header with the provided header name and value to each HTTP request made to the traces backend | N/A | + +## Upload only (deprecated) + +{{< admonition type="caution" >}} + +The "Upload only" option is deprecated and will be removed in a future release. Use the `k6 cloud upload` command instead. + +{{< /admonition >}} + +A boolean specifying whether the test should just be uploaded to the cloud, but not run it. Available in `k6 cloud` command. + +This would be useful if you would like to update a given test and run it later. For example, updating test scripts of a scheduled test from the CI pipelines. + +| Env | CLI | Code / Config file | Default | +| ---------------------- | --------------- | ------------------ | ------- | +| `K6_CLOUD_UPLOAD_ONLY` | `--upload-only` | N/A | `false` | + +{{< code >}} + +```bash +$ k6 cloud run --upload-only script.js +``` + +{{< /code >}} + +## User agent + +A string specifying the user-agent string to use in `User-Agent` headers when sending HTTP +requests. +If you pass an empty string, no `User-Agent` header is sent. +Available in `k6 run` and `k6 cloud` commands + +| Env | CLI | Code / Config file | Default | +| --------------- | -------------- | ------------------ | --------------------------------------------------------------------- | +| `K6_USER_AGENT` | `--user-agent` | `userAgent` | `k6/0.27.0 (https://k6.io/)` (depending on the version you're using)` | + +{{< code >}} + +```javascript +export const options = { + userAgent: 'MyK6UserAgentString/1.0', +}; +``` + +{{< /code >}} + +## Verbose + +A boolean specifying whether verbose logging is enabled. Available in `k6 run` and `k6 cloud` commands. + +| Env | CLI | Code / Config file | Default | +| --- | ----------------- | ------------------ | ------- | +| N/A | `--verbose`, `-v` | N/A | `false` | + +{{< code >}} + +```bash +$ k6 run --verbose script.js +``` + +{{< /code >}} + +## VUs + +An integer value specifying the number of VUs to run concurrently, used together with the [iterations](#iterations) or [duration](#duration) options. If you'd like more control look at the [`stages`](#stages) option or [scenarios](https://grafana.com/docs/k6//using-k6/scenarios). + +Available in `k6 run` and `k6 cloud` commands. + +| Env | CLI | Code / Config file | Default | +| -------- | ------------- | ------------------ | ------- | +| `K6_VUS` | `--vus`, `-u` | `vus` | `1` | + +{{< code >}} + +```javascript +export const options = { + vus: 10, + duration: '1h', +}; +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/using-k6/metrics/_index.md b/docs/sources/k6/v0.55.x/using-k6/metrics/_index.md new file mode 100644 index 000000000..0ec430e26 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/metrics/_index.md @@ -0,0 +1,117 @@ +--- +title: 'Metrics' +description: 'This section covers the important aspect of metrics management in k6. How and what kind of metrics k6 collects automatically (_built-in_ metrics), and what custom metrics you can make k6 collect.' +weight: 02 +--- + +# Metrics + +_Metrics_ measure how a system performs under test conditions. +By default, k6 automatically collects built-in metrics. +Besides built-ins, you can also make custom metrics. + +Metrics fall into four broad types: + +- **Counters** sum values. +- **Gauges** track the smallest, largest, and latest values. +- **Rates** track how frequently a non-zero value occurs. +- **Trends** calculates statistics for multiple values (like mean, mode or percentile). + +To make a test fail a certain criteria, you can write a [Threshold](https://grafana.com/docs/k6//using-k6/thresholds) based on the metric criteria (the specifics of the expression depend on the metric type). +To filter metrics, you can use [Tags and groups](https://grafana.com/docs/k6//using-k6/tags-and-groups). +You can also export metrics in various summary and granular formats, as documented in [Results output](https://grafana.com/docs/k6//results-output). + +| On this page... | Read about... | +| -------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- | +| [Built-in metrics](https://grafana.com/docs/k6//using-k6/metrics/reference) | Each built-in metric for each supported [protocol](https://grafana.com/docs/k6//using-k6/protocols) | +| [Create custom metrics](https://grafana.com/docs/k6//using-k6/metrics/create-custom-metrics) | How to build your own metric for each metric type | + +## What metrics to look at? + +Each metric provides a different perspective on performance. +So the best metric for your analysis depends on your goals. + +However, if you're unsure about the metrics to focus on, you can start with the metrics that measure the requests, errors, and duration (the criteria of the [RED method](https://grafana.com/blog/2018/08/02/the-red-method-how-to-instrument-your-services/)). + +- `http_reqs`, to measure requests +- `http_req_failed`, to measure error rate +- `http_req_duration`, to measure duration + +{{< admonition type="note" >}} + +In other terminology, these metrics measure traffic (in requests), availability (in error rate), and latency (in request duration). +SREs might recognize these metrics as three of the [four Golden Signals](https://sre.google/sre-book/monitoring-distributed-systems/#xref_monitoring_golden-signals). + +{{< /admonition >}} + +## Example output + +An aggregated summary of all _built-in_ and custom metrics outputs to `stdout` when you run a test: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export default function () { + http.get('https://test-api.k6.io/'); +} +``` + +{{< /code >}} + +The preceding script outputs something like this: + +{{< code >}} + +```bash +$ k6 run script.js + + /\ |‾‾| /‾‾/ /‾‾/ + /\ / \ | |/ / / / + / \/ \ | ( / ‾‾\ + / \ | |\ \ | (‾) | + / __________ \ |__| \__\ \_____/ .io + + execution: local + script: http_get.js + output: - + + scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop): + * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s) + + +running (00m03.8s), 0/1 VUs, 1 complete and 0 interrupted iterations +default ✓ [======================================] 1 VUs 00m03.8s/10m0s 1/1 iters, 1 per VU + + data_received..................: 22 kB 5.7 kB/s + data_sent......................: 742 B 198 B/s + http_req_blocked...............: avg=1.05s min=1.05s med=1.05s max=1.05s p(90)=1.05s p(95)=1.05s + http_req_connecting............: avg=334.26ms min=334.26ms med=334.26ms max=334.26ms p(90)=334.26ms p(95)=334.26ms + http_req_duration..............: avg=2.7s min=2.7s med=2.7s max=2.7s p(90)=2.7s p(95)=2.7s + { expected_response:true }...: avg=2.7s min=2.7s med=2.7s max=2.7s p(90)=2.7s p(95)=2.7s + http_req_failed................: 0.00% ✓ 0 ✗ 1 + http_req_receiving.............: avg=112.41µs min=112.41µs med=112.41µs max=112.41µs p(90)=112.41µs p(95)=112.41µs + http_req_sending...............: avg=294.48µs min=294.48µs med=294.48µs max=294.48µs p(90)=294.48µs p(95)=294.48µs + http_req_tls_handshaking.......: avg=700.6ms min=700.6ms med=700.6ms max=700.6ms p(90)=700.6ms p(95)=700.6ms + http_req_waiting...............: avg=2.7s min=2.7s med=2.7s max=2.7s p(90)=2.7s p(95)=2.7s + http_reqs......................: 1 0.266167/s + iteration_duration.............: avg=3.75s min=3.75s med=3.75s max=3.75s p(90)=3.75s p(95)=3.75s + iterations.....................: 1 0.266167/s + vus............................: 1 min=1 max=1 + vus_max........................: 1 min=1 max=1 +``` + +{{< /code >}} + +In that output, all the metrics that start with `http`, `iteration`, and `vu` are _built-in_ metrics, which get written to stdout at the end of a test. +For details of all metrics, refer to the [Metrics reference](https://grafana.com/docs/k6//using-k6/metrics/reference). + +## Metric name restrictions + +Metric names must comply with OpenTelemetry and [Prometheus limitations](https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels). The character limit is the same limit k6 had before restricting the character set down. + +That means metrics names must: + +- Include up to 128 symbols (ASCII letters, numbers, or underscores). +- Start with a letter or an underscore. diff --git a/docs/sources/k6/v0.55.x/using-k6/metrics/create-custom-metrics.md b/docs/sources/k6/v0.55.x/using-k6/metrics/create-custom-metrics.md new file mode 100644 index 000000000..56d2cc020 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/metrics/create-custom-metrics.md @@ -0,0 +1,108 @@ +--- +title: Create custom metrics +description: How to build custom k6 metrics for each metric type. +weight: 200 +--- + +# Create custom metrics + +Besides the [built-in metrics](https://grafana.com/docs/k6//using-k6/metrics/reference), you can create custom metrics. +For example, you can compute a metric for your business logic, or use the [Response.timings](https://grafana.com/docs/k6//javascript-api/k6-http/response) object to create a metric for a specific set of endpoints. + +Each metric type has a [constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor) to create a custom metric. +The constructor creates a metric object of the declared type. Each type has an `add` method to take metric measurements. + +If you need help constructing a custom metric, read the following sections of this document. +They document the procedure and provide examples. +If you're comfortable with these already, you might prefer to read the [reference documentation for each metric constructor](https://grafana.com/docs/k6//javascript-api/k6-metrics). +Each topic has examples to make a custom metric and create [thresholds](https://grafana.com/docs/k6//using-k6/thresholds) from it. + +## Create a custom metric + +{{< admonition type="note" >}} + +Custom metrics must be created in [init context](https://grafana.com/docs/k6//using-k6/test-lifecycle). +This limits memory and ensures that k6 can validate that all thresholds are evaluating defined metrics. + +{{< /admonition >}} + +The generic procedure to create a custom metric is as follows: + +1. Import the [`k6/metrics`](https://grafana.com/docs/k6//javascript-api/k6-metrics) module. Optionally, specify the type of metrics you want to create with a named import: + + ```javascript + import { Trend } from 'k6/metrics'; + ``` + +1. In init context, construct a new custom-metric object. + + For example, the following creates a custom trend. The object in the script is called `myTrend`, and its metric appears in the results output as `waiting_time`. + + ```javascript + const myTrend = new Trend('waiting_time'); + ``` + +1. In the VU iteration code, use the `add` method to take a measurement. + +## Example: create a trend metric for waiting time + +This VU code makes a request then adds the timing value of the request to the `myTrend` object. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { Trend } from 'k6/metrics'; + +const myTrend = new Trend('waiting_time'); + +export default function () { + const r = http.get('https://httpbin.test.k6.io'); + myTrend.add(r.timings.waiting); + console.log(myTrend.name); // waiting_time +} +``` + +{{< /code >}} + +## View custom metric results + +Custom metrics appear in [Results output](https://grafana.com/docs/k6//results-output) in both the end-of-test summary and in the granular data points. +Each metric type has specific aggregation methods. +You can also optionally [tag](https://grafana.com/docs/k6//using-k6/tags-and-groups) any value for a custom metric. +You can use these tags to filter test results. + +Here's how the output of the preceding script might look in the end-of-test summary. +Since the metric is a trend, k6 calculates various trends based on the number of values and their summation. + +{{< code >}} + +```bash +$ k6 run script.js + + ... + INFO[0001] waiting_time source=console + + ... + iteration_duration.............: avg=1.15s min=1.15s med=1.15s max=1.15s p(90)=1.15s p(95)=1.15s + iterations.....................: 1 0.864973/s + waiting_time...................: avg=265.245396 min=265.245396 med=265.245396 max=265.245396 p(90)=265.245396 p(95)=265.245396 +``` + +{{< /code >}} + +{{< admonition type="note" >}} + +Custom metrics are collected from VU threads only at the end of a VU iteration. +For long-running scripts, custom metrics might appear only after the test runs a while. + +{{< /admonition >}} + +## Read more + +The [`k6/metrics`](https://grafana.com/docs/k6//javascript-api/k6-metrics) has examples for defining each type of custom metric, and for defining a threshold on the metric. + +- [Counter](https://grafana.com/docs/k6//javascript-api/k6-metrics/counter) +- [Gauge](https://grafana.com/docs/k6//javascript-api/k6-metrics/gauge) +- [Rate](https://grafana.com/docs/k6//javascript-api/k6-metrics/rate) +- [Trend](https://grafana.com/docs/k6//javascript-api/k6-metrics/trend) diff --git a/docs/sources/k6/v0.55.x/using-k6/metrics/reference.md b/docs/sources/k6/v0.55.x/using-k6/metrics/reference.md new file mode 100644 index 000000000..be88d444f --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/metrics/reference.md @@ -0,0 +1,92 @@ +--- +title: Built-in metrics +description: A reference of built-in metrics for different supported protocols. +weight: 100 +--- + +# Built-in metrics + +Every k6 test emits built-in and [custom metrics](https://grafana.com/docs/k6//using-k6/metrics/create-custom-metrics). +Each supported protocol also has its specific metrics. + +## Standard built-in metrics + +k6 always collects the following metrics, no matter what protocol the test uses: + +| Metric Name | Type | Description | +| ------------------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| checks | Rate | The rate of successful checks. | +| data_received | Counter | The amount of received data. [This example covers how to track data for an individual URL](https://grafana.com/docs/k6//examples/track-transmitted-data-per-url). | +| data_sent | Counter | The amount of data sent. [Track data for an individual URL](https://grafana.com/docs/k6//examples/track-transmitted-data-per-url) to track data for an individual URL. | +| dropped_iterations | Counter | The number of iterations that weren't started due to lack of VUs (for the arrival-rate executors) or lack of time (expired maxDuration in the iteration-based executors). Refer to [Dropped iterations](https://grafana.com/docs/k6//using-k6/scenarios/concepts/dropped-iterations) for more details. | +| iteration_duration | Trend | The time to complete one full iteration, including time spent in `setup` and `teardown`. To calculate the duration of the iteration's function for the specific scenario, [try this workaround](https://grafana.com/docs/k6//using-k6/workaround-iteration-duration). | +| iterations | Counter | The aggregate number of times the VUs execute the JS script (the `default` function). | +| vus | Gauge | Current number of active virtual users | +| vus_max | Gauge | Max possible number of virtual users (VU resources are [pre-allocated](https://grafana.com/docs/k6//using-k6/scenarios/concepts/arrival-rate-vu-allocation), to avoid affecting performance when scaling up load). | + +## HTTP-specific built-in metrics {#http} + +These metrics are generated only when the test makes HTTP requests. + +{{< admonition type="note" >}} + +For all `http_req_*` metrics, **the timestamp is emitted the end of the request.** +In other words, the timestamp happens when k6 receives the end of the response body, or the request times out. + +{{< /admonition >}} + +| Metric Name | Type | Description | +| ------------------------ | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| http_req_blocked | Trend | Time spent blocked (waiting for a free TCP connection slot) before initiating the request. `float` | +| http_req_connecting | Trend | Time spent establishing TCP connection to the remote host. `float` | +| http_req_duration | Trend | Total time for the request. It's equal to `http_req_sending + http_req_waiting + http_req_receiving` (i.e. how long did the remote server take to process the request and respond, without the initial DNS lookup/connection times). `float` | +| http_req_failed | Rate | The rate of failed requests according to [setResponseCallback](https://grafana.com/docs/k6//javascript-api/k6-http/set-response-callback). | +| http_req_receiving | Trend | Time spent receiving response data from the remote host. `float` | +| http_req_sending | Trend | Time spent sending data to the remote host. `float` | +| http_req_tls_handshaking | Trend | Time spent handshaking TLS session with remote host | +| http_req_waiting | Trend | Time spent waiting for response from remote host (a.k.a. “time to first byte”, or “TTFB”). `float` | +| http_reqs | Counter | How many total HTTP requests k6 generated. | + +## Browser metrics {#browser} + +The [k6 browser module](https://grafana.com/docs/k6//using-k6-browser) emits its own metrics based on the [Core Web Vitals](https://web.dev/vitals/#core-web-vitals). + +These core metrics will evolve over time when technology changes, but for now, k6 tracks the following core web vitals: + +{{< docs/shared source="k6" lookup="browser/web-vital-core-metrics.md" version="" >}} + +### Other Web Vitals + +Apart from the Core Web Vitals, the browser module also reports [Other Web Vitals](https://web.dev/vitals/#other-web-vitals). + +{{< docs/shared source="k6" lookup="browser/web-vital-other-metrics.md" version="" >}} + +## Built-in WebSocket metrics {#websockets} + +`k6` emits the following metrics when interacting with a WebSocket service through the [`experimental`](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets) or legacy websockets API. + +| Metric name | Type | Description | +| ------------------- | ------- | ------------------------------------------------------------------------------------------------------------ | +| ws_connecting | Trend | Total duration for the WebSocket connection request. | +| ws_msgs_received | Counter | Total number of messages received | +| ws_msgs_sent | Counter | Total number of messages sent | +| ws_ping | Trend | Duration between a ping request and its pong reception | +| ws_session_duration | Trend | Duration of the WebSocket session. Time between the start of the connection and the end of the VU execution. | +| ws_sessions | Counter | Total number of started WebSocket sessions. | + +## Built-in gRPC metrics {#grpc} + +k6 emits the following metrics when it interacts with a service through the [`gRPC`](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/) API. + +| Metric Name | Type | Description | +| -------------------------- | ------- | ----------------------------------------- | +| grpc_req_duration | Trend | Time to receive response from remote host | +| grpc_streams | Counter | Total number of started streams | +| grpc_streams_msgs_received | Counter | Total number of messages received | +| grpc_streams_msgs_sent | Counter | Total number of messages sent | + +{{< admonition type="note" >}} + +Steams-related metrics (`grpc_streams*`) are available only on `k6` version `0.49.0` or higher or when using the `k6/experimental/grpc` module, which is available on `k6` version `0.45.0`. + +{{< /admonition >}} diff --git a/docs/sources/k6/v0.55.x/using-k6/modules.md b/docs/sources/k6/v0.55.x/using-k6/modules.md new file mode 100644 index 000000000..dd8abd49d --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/modules.md @@ -0,0 +1,436 @@ +--- +title: 'Modules' +description: 'While writing test scripts, it is common to import different modules, or part of modules, for +usage throughout the script. In k6, it is possible to import three different kinds of modules.' +weight: 07 +--- + +# Modules + +## Import modules + +It's common to import modules, or parts of modules, to use in your test scripts. +In k6, you can import different kinds of modules: + +- [Built-in modules](#built-in-modules) +- [Local modules](#local-modules) +- [Remote modules](#remote-modules) +- [Extension modules](#extension-modules) + +### Built-in modules + +k6 provides many built-in modules for core functionalities. +For example, the `http` client make requests against the +system under test. +For the full list of built-in modules, refer to the [API documentation](https://grafana.com/docs/k6//javascript-api). + +```javascript +import http from 'k6/http'; +``` + +### Local modules + +These modules are stored on the local filesystem, and accessed either through relative or absolute filesystem paths. + +k6 adopts a **browser-like module resolution** and doesn't support [Node.js module resolution](https://nodejs.org/api/modules.html#modules_all_together). File names for `imports` must be fully specified, such as `./helpers.js`. + +```javascript +//my-test.js +import { someHelper } from './helpers.js'; + +export default function () { + someHelper(); +} +``` + +```javascript +//helpers.js +export function someHelper() { + // ... +} +``` + +### Remote modules + +These modules are accessed over HTTP(S), from a public source like GitHub, any CDN, or +from any publicly accessible web server. The imported modules are downloaded and executed at +runtime, making it extremely important to **make sure you trust the code before including +it in a test script**. + +For example, [jslib](https://grafana.com/docs/k6//javascript-api/jslib) is a set of k6 JavaScript libraries available as remote HTTPS modules. They can be downloaded and imported as local modules or directly imported as remote modules. + +```javascript +import { randomItem } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; + +export default function () { + randomItem(); +} +``` + +You can also build your custom Javascript libraries and distribute them via a public web hosting. For reference, [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws) and [k6-rollup-example](https://github.com/grafana/k6-rollup-example) host their modules as GitHub release assets. + +### Extension modules + +Like the [k6 APIs](https://grafana.com/docs/k6//javascript-api), you can build custom modules in Go code and expose them as JavaScript modules. These custom Go-to-JS modules are known as [k6 extensions](https://grafana.com/docs/k6//extensions). + +Below is an example that imports the `k6/x/kubernetes` module from the [xk6-kubernetes](https://github.com/grafana/xk6-kubernetes) extension. + +```javascript +import { Kubernetes } from 'k6/x/kubernetes'; + +const podSpec = { + apiVersion: 'v1', + kind: 'Pod', + metadata: { name: 'busybox', namespace: 'testns' }, + spec: { + containers: [ + { + name: 'busybox', + image: 'busybox', + command: ['sh', '-c', 'sleep 30'], + }, + ], + }, +}; +export default function () { + const kubernetes = new Kubernetes(); + kubernetes.create(podSpec); + const pods = kubernetes.list('Pod', 'testns'); + pods.map(function (pod) { + console.log(pod.metadata.name); + }); +} +``` + +How do k6 extensions (Go-to-JS modules) work? For enhanced performance, the k6 engine is written in Go and embeds a JavaScript VM ([sobek](https://github.com/grafana/sobek)) to execute JavaScript test code. That allows you to build your modules in Go code and import them as JavaScript as usual. + +To learn more about using or creating k6 extensions, refer to the [Extension documentation](https://grafana.com/docs/k6//extensions). + +## Share JavaScript modules + +As mentioned previously, users can import custom JavaScript libraries by loading either local or remote modules. Because of that, we have two options to import JavaScript modules, along with various methods to distribute them. + +{{< admonition type="note" >}} + +The following options for distributing and sharing JavaScript libraries are available for both custom and other public libraries. + +{{< /admonition >}} + +### Remote modules + +You can host your modules in a public webserver like GitHub and any CDN and be imported remotely. + +```javascript +// As GitHub release assets +import { + WorkloadConfig, + sayHello, +} from 'https://github.com/grafana/k6-rollup-example/releases/download/v0.0.2/index.js'; + +// or hosted in a CDN +import { randomIntBetween, randomItem } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js'; +``` + +When the library consists of multiple files and modules, you may want to bundle these modules to create public releases. Here are some examples for reference: + +- Using Webpack: [k6-jslib-utils](https://github.com/grafana/k6-jslib-utils) and [k6-jslib-aws](https://github.com/grafana/k6-jslib-aws). +- Using Rollup: [test-commons](https://github.com/grafana/k6-rollup-example/). + +Be aware that k6 automatically executes remote modules, making it crucial to trust the source code of these remote modules. There is a **risk of altering the remote modules with certain hosting mechanisms**. To mitigate this security risk, some users prefer to download and import the modules locally to ensure full control of the source code. + +### Local modules + +In this example, the previous remote modules have been downloaded to the `lib` folder of the testing project and imported as follows: + +```javascript +import { WorkloadConfig, sayHello } from './libs/test-commons.js'; + +import { randomIntBetween, randomItem } from './libs/k6-utils.js'; +``` + +Another option to distribute libraries is to use a package manager tool like npm, which enables version locking and the linking of local libraries. The latter can be useful during development. + +Although k6 does not resolve node modules, you can utilize a Bundler to load npm dependencies, as shown in the [k6-rollup-example](https://github.com/grafana/k6-rollup-example). + +## Use Node.js modules + +{{< admonition type="caution" >}} + +k6 isn't Node.js or a browser. Packages that rely on APIs provided by Node.js, for instance the `os` and `fs` modules, won't work in k6. The same goes for browser-specific APIs, such as the `window` object. + +{{< /admonition >}} + +In a JavaScript project running Node.js, modules are imported using either `import` or `require()`, using the Node.js module resolution algorithm. This means that a user can import modules by name, without providing the full filesystem path to the module. For instance: + +```javascript +import { ClassInAModule } from 'cool-module'; +``` + +The `import` statement would be automatically resolved by the node resolution algorithm by searching: + +- The current directory +- Any `node_modules` folder in the directory +- Any `node_modules` folder in a parent directory, up to the closest `package.json` file. + +As the implementation of `import` in k6 lacks support for the node module resolution algorithm, Node.js modules that resolve external dependencies need to be transformed into a self-contained, isolated, bundle. + +That's done with the help of a bundling tool, such as Webpack, which analyses the test script, identifies all external dependencies, and then creates a self-contained bundle including everything necessary to run the script. + +If the test script has no external dependencies, already has them vendored in a k6 compatible way, or only uses ES5.1+ features, using a bundler isn't necessary. + +### Pick a bundler + +You can use any bundler that supports transpilation. Popular ones include, but are not limited to, [webpack](https://github.com/webpack/webpack), [parcel](https://github.com/parcel-bundler/parcel), [rollup](https://github.com/rollup/rollup) and [browserify](https://github.com/browserify/browserify). + +Due to its flexibility, ease of use, relatively low resource consumption, and known compatibility with k6, it is recommended to use [webpack](https://github.com/webpack/webpack). + +You can also use these two example repositories as a starting point: + +- [k6-template-es6](https://github.com/grafana/k6-template-es6): Template using Webpack and Babel to enable ES6 features in k6 tests. +- [k6-rollup-example](https://github.com/grafana/k6-rollup-example/): Example using Rollup to bundle k6 tests and release a shared library. + +### Webpack setup example + +Setting up a Babel and Webpack project from scratch might sound like a big undertaking, but +is usually accomplished within minutes. Start by creating a project folder and initializing +`npm`: + +```bash +$ mkdir ./example-project && \ + cd "$_" && \ + npm init -y +``` + +#### Install packages + +Then, install the packages needed: + +```bash +$ npm install --save-dev \ + webpack \ + webpack-cli \ + @types/k6 \ + babel-loader \ + @babel/core \ + @babel/preset-env \ + core-js +``` + +| Package | Usage | +| :---------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [webpack](https://github.com/webpack/webpack) | The bundler part of Webpack | +| [webpack-cli](https://github.com/webpack/webpack-cli) | The CLI part of Webpack, which allows us to use it from the terminal | +| [@types/k6](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/k6) | k6 Typescript definition | +| [babel-loader](https://github.com/babel/babel-loader) | A loader used by Webpack to leverage babel functionality while bundling | +| [@babel/core](https://github.com/babel/babel/tree/master/packages/babel-core) | The core functionality of Babel | +| [@babel/preset-env](https://github.com/babel/babel/tree/master/packages/babel-preset-env) | A smart preset using [browserlist](https://github.com/browserslist/browserslist), [compat-table](https://github.com/kangax/compat-table) and [electron-to-chromium](https://github.com/Kilian/electron-to-chromium) to determine what code to transpile and polyfill. | +| [core-js](https://github.com/zloirock/core-js) | A modular standard library for JS including polyfills | + +#### Configure Webpack + +Once these packages have been added, the next step will be to set up a `webpack.config.js` file: + +```javascript +const path = require('path'); + +module.exports = { + mode: 'production', + entry: { + login: './src/login.test.js', + signup: './src/signup.test.js', + }, + output: { + path: path.resolve(__dirname, 'dist'), // eslint-disable-line + libraryTarget: 'commonjs', + filename: '[name].bundle.js', + }, + module: { + rules: [{ test: /\.js$/, use: 'babel-loader' }], + }, + target: 'web', + externals: /k6(\/.*)?/, +}; +``` + +`Mode` + +Tells Webpack to automatically use the optimizations associated with the `mode`. +Additional details available in [the webpack docs](https://webpack.js.org/configuration/mode/). + +`Entry` + +The files Webpack will use as its entry points while performing the bundling. From these points, +Webpack will automatically traverse all imports recursively until every possible dependency path has +been exhausted. For instance: + +```javascript +// login.test.js + +import { SomeService } from './some.service.js'; + +const svc = new SomeService(); +``` + +and: + +```javascript +// some.service.js + +import * as lodash from 'lodash'; + +export class SomeService { + constructor() { + this._ = lodash; + } +} +``` + +would result in Webpack bundling `login.test.js`, `some.service.js` and all upstream dependencies +utilized by `lodash`. + +`Output` + +The `path` key takes an absolute path which is where the finished bundle will be placed. In +this example, `path.resolve` is used to concatenate `__dirname` and `'dist'` into an absolute +path. + +The `libraryTarget` key configures how the library will be exposed. Setting it to `commonjs` +will result in it being exported using `module.exports`. Additional details available in [the +Webpack docs](https://webpack.js.org/configuration/output/#outputlibrarytarget). + +The `filename` key, as the name suggests, configures the name of the finished bundles. In this +example, the [template string](https://webpack.js.org/configuration/output/#template-strings) `[name]` +is used to add a dynamic part to the output filename. + +#### Add a bundle command + +Open the `package.json` file and add a new script entry, used for running the bundling process. + +```diff +{ + "name": "bundling-example", + "description": "", + "version": "0.1.0", + "private": true, + "scripts": { ++ "bundle": "webpack" + } + ... +} +``` + +#### Bundle tests + +Running webpack will now output two different test bundles, that may be executed independently: + +```bash +$ npm run bundle +# ... +$ tree dist + +dist +├── login.bundle.js +└── signup.bundle.js + +0 directories, 2 files +``` + +#### Run tests + +```bash +$ npm run bundle +# ... +$ k6 run dist/login.bundle.js +# ... +``` + +```bash +$ npm run bundle +# ... +$ k6 run dist/signup.bundle.js \ + --vus 10 \ + --duration 10s +# ... +``` + +## Use TypeScript + +k6 supports partial TypeScript support with the `experimental_enhanced` compatibility mode. For more details, refer to [JavaScript and TypeScript compatibility mode](https://grafana.com/docs/k6//using-k6/javascript-typescript-compatibility-mode/). + +## Use modules with Docker + +Built-in and remote modules work out of the box when running k6 in a Docker container like the [Grafana k6 Docker image](https://hub.docker.com/r/grafana/k6). + +### Local modules + +To run k6 with Docker and import a local module, you must make sure to mount the necessary folders from the host into the container, using [Docker volumes](https://docs.docker.com/engine/admin/volumes/volumes/). Thus, k6 can see all the JS modules it needs to import. + +For example, say you have the following structure on your host machine: + +- `/home/k6/example/src/index.js` +- `/home/k6/example/src/modules/module.js` + +{{< code >}} + +```javascript +import { hello_world } from './modules/module.js'; + +export default function () { + hello_world(); +} +``` + +{{< /code >}} + +{{< code >}} + +```javascript +export function hello_world() { + console.log('Hello world'); +} +``` + +{{< /code >}} + +To run index.js and make the modules available for import we execute the following Docker command with the `/home/k6/example/src` host folder mounted at `/src` in the container: + +{{< code >}} + +```bash +$ docker run --rm -v /home/k6/example/src:/src -i grafana/k6 run /src/index.js +``` + +{{< /code >}} + +Note that on Windows, you also need to make sure that your drive in question, say `C:\`, +has been marked for sharing in the Docker Desktop settings. + +### Extension modules + +The official [Grafana k6 Docker image](https://hub.docker.com/r/grafana/k6) includes the k6 release binary but lacks additional k6 extensions. Therefore, using the official Docker container to run a k6 test that requires an extension will fail. + +To run k6 with extensions in Docker, create a Docker image that includes the k6 binary with any extension you may want to use. Define a `Dockerfile` with the necessary [xk6 build](https://grafana.com/docs/k6//extensions/build-k6-binary-using-go#breaking-down-the-xk6-command) instructions as follows: + +```bash +FROM grafana/xk6:latest + +RUN GCO_ENABLED=0 xk6 build \ + --with github.com/grafana/xk6-kubernetes@latest + +ENTRYPOINT ["./k6"] +``` + +After building your custom k6 Docker image, you can [run k6 with Docker](https://grafana.com/docs/k6//get-started/running-k6/) as usual. + +Alternatively, you can implement a multistage Dockerfile build such as shown on this [Dockerfile example](https://github.com/grafana/xk6-output-prometheus-remote/blob/main/Dockerfile). + +## Read more + +- [Guidelines for organizing your k6 performance testing suite](https://grafana.com/blog/2024/04/30/organizing-your-grafana-k6-performance-testing-suite-best-practices-to-get-started/): Recommendations for large performance testing projects. +- [JavaScript tools, shared libraries, multiple repos, and central teams](https://grafana.com/blog/2024/05/02/setting-up-your-grafana-k6-performance-testing-suite-javascript-tools-shared-libraries-and-more/): Options for structuring your performance testing projects and sharing k6 libraries with other teams. +- [JSLib](https://grafana.com/docs/k6//javascript-api/jslib): A collection of k6 JavaScript libraries maintained by Grafana Labs and available as remote modules. +- [Extensions directory](https://grafana.com/docs/k6//extensions/explore): A collection of k6 extensions maintained by Grafana Labs and the community. +- [k6-rollup-example](https://github.com/grafana/k6-rollup-example): Example using Rollup and Babel to bundle a common library and testing suite. +- [k6-template-es6](https://github.com/grafana/k6-template-es6): Template using Webpack and Babel to bundle k6 tests into CommonJS modules and polyfill ES+ features. +- [k6-template-typescript](https://github.com/grafana/k6-template-typescript): Template using Webpack and Babel to use TypeScript in your k6 scripts. +- [JavaScript Compatibility Mode](https://grafana.com/docs/k6//using-k6/javascript-compatibility-mode): An option to change the ECMAScript version supported by k6. diff --git a/docs/sources/k6/v0.55.x/using-k6/protocols/_index.md b/docs/sources/k6/v0.55.x/using-k6/protocols/_index.md new file mode 100644 index 000000000..5396ee1f9 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/protocols/_index.md @@ -0,0 +1,39 @@ +--- +title: 'Protocols' +description: 'Out of the box k6 comes with support for a few protocols: HTTP / WebSockets / gRPC / ...' +weight: 10 +--- + +# Protocols + +Out of the box, k6 supports the following protocols: + +- HTTP/1.1 +- [HTTP/2](https://grafana.com/docs/k6//using-k6/protocols/http-2) +- [WebSockets](https://grafana.com/docs/k6//using-k6/protocols/websockets) +- [gRPC](https://grafana.com/docs/k6//using-k6/protocols/grpc) + +You can use k6 on more protocols with xk6. + +## Upgrade to HTTP/2 is automatic + +By default, k6 uses HTTP/1.1 when it contacts a server. +If the server reports to k6 that it supports [HTTP/2](https://grafana.com/docs/k6//using-k6/protocols/http-2), k6 upgrades the connection to HTTP/2 instead. + +This is all automatic: +you don't need to do anything special for either the initial use of HTTP/1.1 or the potential protocol upgrade. +However, you might want to verify which protocol is actually being +used for a transaction. +This verification requires an extra step. + +Using [WebSockets](https://grafana.com/docs/k6//using-k6/protocols/websockets) is a bit different, for both the test structure and the VU lifecycle. + +## Extend protocol support with xk6 + +xk6 is a separate CLI tool that lets you build custom k6 binaries. +Community contributors have already added support for additional protocols, +with extensions for SQL, Kafka, ZeroMQ, Redis, and more. +Refer to the [full list of extensions](https://grafana.com/docs/k6//extensions/explore) and find the ones you want to build. Then follow the [xk6 guide](https://grafana.com/docs/k6//extensions/build-k6-binary-using-go). + +You can build your own extension, too. +See the [tutorials](https://grafana.com/docs/k6//extensions/create) to get started. diff --git a/docs/sources/k6/v0.55.x/using-k6/protocols/grpc.md b/docs/sources/k6/v0.55.x/using-k6/protocols/grpc.md new file mode 100644 index 000000000..611f59eaf --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/protocols/grpc.md @@ -0,0 +1,342 @@ +--- +title: 'gRPC' +description: 'gRPC is a lightweight open-source RPC framework. Starting with k6 v0.29.0, we support unary gRPC requests.' +weight: 03 +--- + +# gRPC + +## Overview + +[gRPC](https://grpc.io/) is a lightweight, open-source RPC framework. +It was originally developed by Google, with version 1.0 released in August 2016. +Since then, it's gained much attention and wide adoption. + +Whereas JSON transmits as human-readable text, gRPC is binary. +The binary format makes data transfer faster and more compact. +In the benchmarks we've seen, gRPC has proved much faster than REST, gRPC's more traditional, JSON-based counterpart. +The messages and services used for gRPC are described in `.proto` files, containing definitions for [Protocol Buffers](https://en.wikipedia.org/wiki/Protocol_Buffers) (protobuf). + +## Load testing gRPC services with k6 + +Starting on k6 v0.49.0, k6 supports unary gRPC requests and streaming as part of the `k6/net/grpc` core module. + +### gRPC definitions + +Before interacting with a gRPC service, k6 needs to learn the definitions of the messages and services. + +One way to do that is to explicitly use the `Client.load()` method and load the client definitions from the local file system. The method accepts a list of import paths and a list of `.proto` files. k6 then loads all the definitions from the files and their dependencies. + +{{< code >}} + +```javascript +import { Client } from 'k6/net/grpc'; + +const client = new Client(); +client.load(['definitions'], 'hello.proto'); +``` + +{{< /code >}} + +Alternatively, you can dynamically load the definitions by using the gRPC reflection protocol. To enable reflection, you can pass the `reflect: true` option to `Client.connect()`. k6 then loads all the definitions from the server and their dependencies. + +This option is only possible if the server has been instrumented with reflection support. + +{{< code >}} + +```javascript +import { Client } from 'k6/net/grpc'; + +const client = new Client(); +client.connect('127.0.0.1:10000', { reflect: true }); +``` + +{{< /code >}} + +### Unary gRPC requests + +Unary calls work the same way as regular HTTP requests. A single request is sent to a server, and the server replies with a single response. + +{{< code >}} + +```javascript +import { Client, StatusOK } from 'k6/net/grpc'; +import { check, sleep } from 'k6'; + +const client = new Client(); +client.load(['definitions'], 'hello.proto'); + +export default () => { + client.connect('127.0.0.1:10000', {}); + + const data = { greeting: 'Bert' }; + const response = client.invoke('hello.HelloService/SayHello', data); + + check(response, { + 'status is OK': (r) => r && r.status === StatusOK, + }); + + console.log(JSON.stringify(response.message)); + + client.close(); + sleep(1); +}; +``` + +{{< /code >}} + +### Server gRPC streaming + +In server streaming mode, the client sends a single request to the server, and the server replies with multiple responses. + +The example below demonstrates server streaming. + +{{< code >}} + +```javascript +import { Client, Stream } from 'k6/net/grpc'; +import { sleep } from 'k6'; + +const COORD_FACTOR = 1e7; + +const client = new Client(); + +export default () => { + if (__ITER == 0) { + client.connect('127.0.0.1:10000', { plaintext: true, reflect: true }); + } + + const stream = new Stream(client, 'main.FeatureExplorer/ListFeatures', null); + + stream.on('data', function (feature) { + console.log( + `Found feature called "${feature.name}" at ${feature.location.latitude / COORD_FACTOR}, ${ + feature.location.longitude / COORD_FACTOR + }` + ); + }); + + stream.on('end', function () { + // The server has finished sending + client.close(); + console.log('All done'); + }); + + // send a message to the server + stream.write({ + lo: { + latitude: 400000000, + longitude: -750000000, + }, + hi: { + latitude: 420000000, + longitude: -730000000, + }, + }); + + sleep(0.5); +}; +``` + +{{< /code >}} + +In the example script, k6 connects to a gRPC server, creates a stream, and sends a message to the server with latitude and longitude coordinates. When the server sends data back, it logs the feature name and its location. When the server finishes sending data, it closes the client connection and logs a completion message. + +### Client gRPC streaming + +The client streaming mode is the opposite of the server streaming mode. The client sends multiple requests to the server, and the server replies with a single response. + +The example below demonstrates client streaming. + +{{< code >}} + +```javascript +import { Client, Stream } from 'k6/net/grpc'; +import { sleep } from 'k6'; + +const COORD_FACTOR = 1e7; +const client = new Client(); + +// a sample points collection +const points = [ + { + location: { latitude: 407838351, longitude: -746143763 }, + name: 'Patriots Path, Mendham, NJ 07945, USA', + }, + { + location: { latitude: 408122808, longitude: -743999179 }, + name: '101 New Jersey 10, Whippany, NJ 07981, USA', + }, + { + location: { latitude: 413628156, longitude: -749015468 }, + name: 'U.S. 6, Shohola, PA 18458, USA', + }, + { + location: { latitude: 419999544, longitude: -740371136 }, + name: '5 Conners Road, Kingston, NY 12401, USA', + }, + { + location: { latitude: 414008389, longitude: -743951297 }, + name: 'Mid Hudson Psychiatric Center, New Hampton, NY 10958, USA', + }, +]; + +export default () => { + if (__ITER == 0) { + client.connect('127.0.0.1:10000', { plaintext: true, reflect: true }); + } + + const stream = new Stream(client, 'main.RouteGuide/RecordRoute'); + + stream.on('data', (stats) => { + console.log(`Finished trip with ${stats.pointCount} points`); + console.log(`Passed ${stats.featureCount} features`); + console.log(`Travelled ${stats.distance} meters`); + console.log(`It took ${stats.elapsedTime} seconds`); + }); + + stream.on('end', () => { + client.close(); + console.log('All done'); + }); + + // send 3 random points + for (let i = 0; i < 3; i++) { + const point = points[Math.floor(Math.random() * points.length)]; + pointSender(stream, point); + } + + // close the client stream + stream.end(); +}; + +const pointSender = (stream, point) => { + console.log( + `Visiting point ${point.name} ${point.location.latitude / COORD_FACTOR}, ${ + point.location.longitude / COORD_FACTOR + }` + ); + + // send the location to the server + stream.write(point.location); + + sleep(0.5); +}; +``` + +{{< /code >}} + +In the example script, k6 establishes a connection to a gRPC server, creates a stream, and sends three random points. The server responds with statistics about the trip, which are logged to the console. The code also handles the end of the stream, closing the client and logging a completion message. + +### Bidirectional gRPC streaming + +In bi-directional streaming mode, the client and the server may send multiple messages. + +From the API perspective, it combines the client and server streaming modes, so the code is similar to the examples above. + +### Streaming error handling + +To catch errors that occur during streaming, you can use the `error` event handler. + +The handler receives [an error object](https://grafana.com/docs/k6//javascript-api/k6-net-grpc/stream/stream-error/). + +{{< code >}} + +```javascript +import { Client, Stream } from 'k6/net/grpc'; + +const client = new Client(); +const stream = new Stream(client, 'main.RouteGuide/RecordRoute'); + +stream.on('error', function (e) { + // An error has occurred and the stream has been closed. + console.log('Error: ' + JSON.stringify(e)); +}); +``` + +{{< /code >}} + +### Protocol Buffers JSON Mapping + +It's important to note how k6 handles requests and messages. First, it tries to marshal the request/message in JSON format. Then, k6 uses the [protojson](https://pkg.go.dev/google.golang.org/protobuf/encoding/protojson) package to encode or decode to a Protobuf message. + +A limitation during this process is that the object you pass as a request/message must be serializable. That means structs like `Map` don't work. + +The benefit of using `protojson` is the canonical JSON encoding support. The [Protocol Buffers documentation](https://protobuf.dev/programming-guides/proto3/#json) describes this mapping. + +#### Examples + +For instance, if you import `"google/protobuf/wrappers.proto"` and your proto-definitions look like this: + +```proto +syntax = "proto3"; + +package testing; + +import "google/protobuf/wrappers.proto"; + +service Service { + rpc SayHey(google.protobuf.StringValue) returns (google.protobuf.StringValue); + rpc DoubleInteger(google.protobuf.Int64Value) returns (google.protobuf.Int64Value); +} + +``` + +When passing a message, you should use a string or an integer, not an object. As a result, you will receive a type that has already been marshaled. + +{{< code >}} + +```javascript +import { Client } from 'k6/net/grpc'; +const client = new Client(); + +// an example of passing a string +const respString = client.invoke('testing.Service/SayHey', 'John'); +if (respString.message !== 'hey John') { + throw new Error("expected to get 'hey John', but got a " + respString.message); +} + +// an example of passing an integer +const respInt = client.invoke('testing.Service/DoubleInteger', '3'); +if (respInt.message !== '6') { + throw new Error("expected to get '6', but got a " + respInt.message); +} +``` + +{{< /code >}} + +Another example could be usage of `oneof`. Let's say you have a proto-definition like this: + +```proto +syntax = "proto3"; + +package testing; + +service Service { + rpc Test(Foo) returns (Foo) {} +} + +message Foo { + oneof Bar { + string code = 1; + uint32 id = 2; + } +} +``` + +In this case, you should pass an object either with `code` or `id` fields. + +{{< code >}} + +```javascript +import { Client } from 'k6/net/grpc'; +const client = new Client(); + +// calling RPC with filled code field +const respWithCode = client.invoke('testing.Service/Test', { code: 'abc-123' }); + +// calling RPC with filled id field +const respWithID = client.invoke('testing.Service/Test', { id: 123 }); +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/using-k6/protocols/http-2.md b/docs/sources/k6/v0.55.x/using-k6/protocols/http-2.md new file mode 100644 index 000000000..e27aeb6b7 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/protocols/http-2.md @@ -0,0 +1,50 @@ +--- +title: 'HTTP/2' +description: 'When you make HTTP requests in k6 it will automatically upgrade the connection to HTTP/2.0 if the server supports it, just like your web browser would.' +weight: 01 +--- + +# HTTP/2 + +## Overview + +HTTP/2.0 is the latest version of the HTTP protocol. +It improves significantly upon HTTP/1. +Most importantly, it introduces a binary wire protocol with multiplexed streams over a single TCP connection. +This solves a long-standing performance issue with HTTP/1.1: [head-of-line blocking](https://en.wikipedia.org/wiki/Head-of-line_blocking). + +Well, it at least _partially_ solves it. +The TCP congestion control mechanisms can interfere with the intended independent nature of the multiplexed streams in cases of lost/dropped packets and retransmission/reassembly. +The full solution is to run HTTP/2.0 over UDP, as Google implemented with [QUIC](https://en.wikipedia.org/wiki/QUIC). + +## Additional features of HTTP/2.0 + +- Builtin compression of HTTP headers +- Server push +- Pipelining of requests +- Prioritization of requests + +## Load testing HTTP/2 with k6 + +When you make HTTP requests in k6, k6 automatically upgrades the connection to HTTP/2.0 if the server supports it, just like your web browser would. + +To check what protocol was used for a particular request, refer to the `proto` property of the response object. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check, sleep } from 'k6'; + +export default function () { + const res = http.get('https://test-api.k6.io/'); + check(res, { + 'protocol is HTTP/2': (r) => r.proto === 'HTTP/2.0', + }); + sleep(1); +} +``` + +{{< /code >}} + +To see the values that the `r.proto` field can have, refer to the documentation for [k6 HTTP](https://grafana.com/docs/k6//javascript-api/k6-http/response). diff --git a/docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/_index.md b/docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/_index.md new file mode 100644 index 000000000..e741a92dc --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/_index.md @@ -0,0 +1,38 @@ +--- +title: 'SSL/TLS' +description: 'By default and without any special configuration, k6 will connect and talk to servers over TLS. You just need to make sure to specify your request URLs with the https scheme.' +weight: 04 +weight: 04 +--- + +# SSL/TLS + +Transport Layer Security (TLS), the successor of Secure Socket Layer (SSL), is the mechanism through +which encrypted connections can be established between clients and servers on the web and through +which data can flow with integrity intact. + +Simplified, TLS is made possible by using a Public Key Infrastructure (PKI) involving private +cryptographic keys, certificates of ownership (public, signed with private key), and certificate +authorities (CAs, issuing and asserting the validity of issued certificates). + +The entire system depends on the functioning operation of the CAs. +The system trusts that the cryptographic keys that CAs use to sign certificates of ownership to site/domain owners are kept secret. +It also depends on the domain owners' ability to keep their private cryptographic keys secret. + +If they fail to do so, they have to report the leak to the CA that issued the certificate. +After this disclosure, the CA can revoke the certificate and, in doing so, let browsers and other clients know of the changes through +the Online Certificate Status Protocol (OCSP). + +## TLS in k6 + +By default and without any special configuration, k6 connects and talks to servers over TLS. +You just need to make sure that you specify your request URLs with the `https` scheme. + +K6 supports the following TLS functionalities. +Each is worth discussing in more detail: + +- [TLS client certificates](https://grafana.com/docs/k6//using-k6/protocols/ssl-tls/ssl-tls-client-certificates) +- [TLS version and ciphers](https://grafana.com/docs/k6//using-k6/protocols/ssl-tls/ssl-tls-version-and-ciphers) (restricting as + well as checking what was used for a HTTP request by checking response object properties) +- [Online Certificate Status Protocol (OCSP)](https://grafana.com/docs/k6//using-k6/protocols/ssl-tls/online-certificate-status-protocol--ocsp) +- [Response.timings.tls_handshaking](https://grafana.com/docs/k6//javascript-api/k6-http/response) diff --git a/docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/online-certificate-status-protocol-ocsp.md b/docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/online-certificate-status-protocol-ocsp.md new file mode 100644 index 000000000..659b02e9a --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/online-certificate-status-protocol-ocsp.md @@ -0,0 +1,79 @@ +--- +title: 'Online Certificate Status Protocol (OCSP)' +description: 'k6 supports OCSP stapling, receiving and parsing a stapled response as part of +the TLS connection setup.' +aliases: + - ./online-certificate-status-protocol--ocsp/ +--- + +## What is OCSP? + +The Online Certificate Status Protocol (OCSP) lets web browsers and clients check the status of an issued TLS certificate with a Certificate Authority (CA), ensuring that the certificate has not been revoked. + +It exists different ways to check whether the certificate has been revoked. +Each way places the burden on different parties: + +- The browser/client: talk to the CA (or through a CA–entrusted OCSP responder) with OCSP. One downside + with this approach is that the CA's servers need to be available. + +- The browser vendor: maintain an up-to-date list of certificate revocations by talking to + the CAs (or through a CA–entrusted OCSP responder) and distributing this list to the browsers + running on users' machines. +- The server side: the server handles the interaction with the CA (or through a CA–entrusted OCSP + responder), caching the results of the periodic updates and including a "stapled response" + (referred to as OCSP stapling) in the TLS connection setup with the browser/client. + +## OCSP with k6 + +k6 supports OCSP stapling. +The application can receive and parse a stapled response as part of the TLS connection setup. +The OCSP response information is available in the `ocsp.stapled_response` property of the response object. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const res = http.get('https://stackoverflow.com'); + check(res, { + 'is OCSP response good': (r) => r.ocsp.status === http.OCSP_STATUS_GOOD, + }); +} +``` + +{{< /code >}} + +## Properties of an OCSP object + +The OCSP `ocsp` object contains the following properties: + +| Key | Type | Description | +| ------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `status` | string | the status of the certificate, see possible values below | +| `revocation_reason` | string | the reason for revocation of the certificate (if that is the status), see possible values below | +| `produced_at` | number | number of milliseconds elapsed since 1 January 1970 00:00:00 UTC, representing the time when this OCSP stapled response was signed by the CA (or CA–entrusted OCSP responder) | +| `this_update` | number | number of milliseconds elapsed since 1 January 1970 00:00:00 UTC, representing the time when the status being indicated was known to be correct | +| `next_update` | number | number of milliseconds elapsed since 1 January 1970 00:00:00 UTC, representing the time when this OCSP stapled response will be refreshed with CA (or by CA entrusted OCSP responder) | +| `revoked_at` | number | number of milliseconds elapsed since 1 January 1970 00:00:00 UTC, representing the time when this certificate was revoked (if that is the status) | + +### Possible values for `status`: + +- `http.OCSP_STATUS_GOOD` +- `http.OCSP_STATUS_REVOKED` +- `http.OCSP_STATUS_UNKNOWN` +- `http.OCSP_STATUS_SERVER_FAILED` + +### Possible values for `revocation_reason`: + +- `http.OCSP_REASON_UNSPECIFIED` +- `http.OCSP_REASON_KEY_COMPROMISE` +- `http.OCSP_REASON_CA_COMPROMISE` +- `http.OCSP_REASON_AFFILIATION_CHANGED` +- `http.OCSP_REASON_SUPERSEDED` +- `http.OCSP_REASON_CESSATION_OF_OPERATION` +- `http.OCSP_REASON_CERTIFICATE_HOLD` +- `http.OCSP_REASON_REMOVE_FROM_CRL` +- `http.OCSP_REASON_PRIVILEGE_WITHDRAWN` +- `http.OCSP_REASON_AA_COMPROMISE` diff --git a/docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/ssl-tls-client-certificates.md b/docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/ssl-tls-client-certificates.md new file mode 100644 index 000000000..7a8f2e139 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/ssl-tls-client-certificates.md @@ -0,0 +1,90 @@ +--- +title: 'SSL/TLS client certificates' +description: 'To use client certificates, you specify global that tell k6 how to map a public certificate and private key to the domains they are valid for.' +--- + +# SSL/TLS client certificates + +Discussion about TLS certificates is usually about how clients authenticate servers. +However, both TLS and k6 also support the reverse process, in which servers authenticate clients. + +To use client certificates, specify global [configuration options](https://grafana.com/docs/k6//using-k6/k6-options) that tell k6 how to map a public certificate and private key to the domains they are valid for. +You can load the certificate and key from local files or embed them as strings in the script. + +## Loading a certificate and a key from local files + +To load a certificate and a key from local files, use the builtin `open(...)` function: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + tlsAuth: [ + { + domains: ['example.com'], + cert: open('./mycert.pem'), + key: open('./mycert-key.pem'), + }, + ], +}; + +export default function () { + http.get('https://example.com/'); +} +``` + +{{< /code >}} + +## Loading certificate and key from embedded strings + +To load the certificate and key from embedded strings, use this snippet. +Note the use of +[template literals](https://developer.mozilla.org/en-US/Web/JavaScript/Reference/Template_literals) for multi-line strings): + +{{< admonition type="note" >}} + +These are just example keys. + +{{< /admonition >}} + +{{< code >}} + +```javascript +import http from 'k6/http'; + +const CERT = `-----BEGIN CERTIFICATE----- +MIIFgTCCA2kCAQEwDQYJKoZIhvcNAQEFBQAwgYExCzAJBgNVBAYTAlNFMRcwFQYD +VQQIEw5TdG9ja2hvbG1zIExhbjESMBAGA1UEBxMJU3RvY2tob2xtMRcwFQYDVQQK +... +/n5QrTGhP51P9Q1THzRfn6cNCDwzSTMVEJr40QhuTJQWASe3miuFmZoG5ykmGqVm +fWQRiQyM330s9vTwFy14J2Bxe4px6cyy7rVXvYL2LvfA4L0T7/x1nUULw+Mpqun1 +R3XRJWqGDjBKXr5q8VatdQO1QLgr +-----END CERTIFICATE-----`; + +const KEY = `-----BEGIN RSA PRIVATE KEY----- +KsZVVI1FTX+F959vqu1S02T+R1JM29PkIfJILIXapKQfb0FWrALU5xpipdPYBWp7 +j5iSp06/7H8ms87Uz9BrOA6rytoRSE0/wEe5WkWdBBgLLPpfOSWZsAA5RGCB2n+N +... +Dk+frzKuiErHFN7HOHAQannui4eLsY0ehYMByowgJIUGzIJyXR6O19hVhV7Py66u +X7/Jy01JXn83LuWdpaPAKU+B42BLP0IGXt5CocPms07HOdtJ/wm2zwHTyfjn9vu+ +HO/dQr6a7DhRu2lLI9Sc983NwRqDKICZQQ/+gqWk8BgQZ1yI9O4AYkzywzAEk3py +-----END RSA PRIVATE KEY-----`; + +export const options = { + tlsAuth: [ + { + domains: ['example.com'], + cert: CERT, + key: KEY, + }, + ], +}; + +export default function () { + http.get('https://example.com/'); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/ssl-tls-version-and-ciphers.md b/docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/ssl-tls-version-and-ciphers.md new file mode 100644 index 000000000..b7d4033bf --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/protocols/ssl-tls/ssl-tls-version-and-ciphers.md @@ -0,0 +1,137 @@ +--- +title: 'SSL/TLS version and ciphers' +description: 'To support testing specific client configurations, you can set a specific version or range +of versions of SSL/TLS that should be allowed for a connection.' +--- + +# SSL/TLS version and ciphers + +To support testing specific client configurations, you can specify a version or range of versions of SSL/TLS that are allowed for a connection. +You can as also specify which cipher suites are allowed for that connection. + +> #### ⚠️ Reg. ciphers and TLS 1.3 +> +> Due to limitations in the underlying [go implementation](https://github.com/golang/go/issues/29349), changing the ciphers for TLS 1.3 is _not_ supported and will do nothing. + +## Limiting SSL/TLS version + +To limit the k6 to a specific SSL/TLS version, use a global +[configuration option](https://grafana.com/docs/k6//using-k6/k6-options): + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + tlsVersion: http.TLS_1_2, +}; + +export default function () { + http.get('https://badssl.com'); +} +``` + +{{< /code >}} + +You can also accept a range of SSL/TLS versions: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + tlsVersion: { + min: http.SSL_3_0, + max: http.TLS_1_2, + }, +}; + +export default function () { + http.get('https://badssl.com'); +} +``` + +{{< /code >}} + +## Versions available to choose from + +Here's the list of available SSL/TLS versions that you can choose from, ordered from oldest version to latest. + +- `http.SSL_3_0` +- `http.TLS_1_0` +- `http.TLS_1_1` +- `http.TLS_1_2` +- `http.TLS_1_3` + +## Limiting cipher suites + +To limit the cipher suites that k6 is allowed to use, there's a global +[configuration option](https://grafana.com/docs/k6//using-k6/k6-options). +You choose a list of allowed ciphers: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + tlsCipherSuites: ['TLS_RSA_WITH_RC4_128_SHA', 'TLS_RSA_WITH_AES_128_GCM_SHA256'], +}; + +export default function () { + http.get('https://badssl.com'); +} +``` + +{{< /code >}} + +## Checking SSL/TLS version and cipher suite used in requests + +You can also check which SSL/TLS version and ciphers were used. +To do so, look at the `tls_version` and `tls_cipher_suite` response object properties. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check } from 'k6'; + +export default function () { + const res = http.get('https://sha256.badssl.com'); + check(res, { + 'is TLSv1.2': (r) => r.tls_version === http.TLS_1_2, + 'is sha256 cipher suite': (r) => r.tls_cipher_suite === 'TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256', + }); +} +``` + +{{< /code >}} + +## Cipher suites available to choose from + +Here's a list of available SSL/TLS cipher suites: + +- `TLS_RSA_WITH_RC4_128_SHA` +- `TLS_RSA_WITH_3DES_EDE_CBC_SHA` +- `TLS_RSA_WITH_AES_128_CBC_SHA` +- `TLS_RSA_WITH_AES_256_CBC_SHA` +- `TLS_RSA_WITH_AES_128_GCM_SHA256` +- `TLS_RSA_WITH_AES_256_GCM_SHA384` +- `TLS_ECDHE_ECDSA_WITH_RC4_128_SHA` +- `TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA` +- `TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA` +- `TLS_ECDHE_RSA_WITH_RC4_128_SHA` +- `TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA` +- `TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA` +- `TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA` +- `TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256` +- `TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256` +- `TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384` +- `TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384` + +> ### ⚠️ Differences depending on k6 build +> +> This list reflects the available cipher suites in the latest official build. +> If you are using a custom-built k6, the available cipher suites will depend on the Go version you compiled it with, see [https://golang.org/pkg/crypto/tls/#pkg-constants](https://golang.org/pkg/crypto/tls/#pkg-constants). diff --git a/docs/sources/k6/v0.55.x/using-k6/protocols/websockets.md b/docs/sources/k6/v0.55.x/using-k6/protocols/websockets.md new file mode 100644 index 000000000..97dfdcb83 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/protocols/websockets.md @@ -0,0 +1,206 @@ +--- +title: 'WebSockets' +description: 'Comparing HTTP based tests to WebSocket ones, there are some differences in the structure and inner workings of k6.' +weight: 02 +--- + +# WebSockets + +## Overview + +[WebSocket](https://en.wikipedia.org/wiki/WebSocket) is a protocol that provides full-duplex communication channels over a single TCP connection. +It is commonly used by single-page apps (SPAs) and mobile apps, to add server-push based functionality, which usually improves performance in polling-based solutions. + +## Load testing WebSockets with k6 + +{{< admonition type="note" >}} + +[xk6-websockets](https://github.com/grafana/xk6-websockets) is an experimental module with a more standard API than `k6/ws`. It implements [the WebSockets API living standard](https://websockets.spec.whatwg.org/). While the implementation isn't complete, it uses a global event loop instead of local one. + +Currently, it's available as an experimental module [`k6/experimental/websockets`](https://grafana.com/docs/k6//javascript-api/k6-experimental/websockets). It's also likely that it will become part of the core of k6 in the future. + +{{< /admonition >}} + +Comparing HTTP-based tests to WebSocket ones, you'll find differences in both structure and inner workings. +The primary difference is that instead of continuously looping the main function (`export default function() { ... }`) over and over, each VU is now runs an asynchronous event loop. + +The basic structure of a WebSocket test looks like this: + +{{< code >}} + +```javascript +import ws from 'k6/ws'; +import { check } from 'k6'; + +export default function () { + const url = 'ws://echo.websocket.org'; + const params = { tags: { my_tag: 'hello' } }; + + const res = ws.connect(url, params, function (socket) { + socket.on('open', () => console.log('connected')); + socket.on('message', (data) => console.log('Message received: ', data)); + socket.on('close', () => console.log('disconnected')); + }); + + check(res, { 'status is 101': (r) => r && r.status === 101 }); +} +``` + +{{< /code >}} + +In this example, the [connect()](https://grafana.com/docs/k6//javascript-api/k6-ws/connect) method takes a "run" function as its third parameter. +That function should accept a [Socket](https://grafana.com/docs/k6//javascript-api/k6-ws/socket) object as its only parameter. +The run function forms the basis of the asynchronous event loop. + +When the WebSocket connection is created, the run function will be immediately called, all code inside it will be executed (usually code to set up event handlers), and then blocked until the WebSocket connection is closed (by the remote host or by using [socket.close()](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-close)). + +## Error handling + +To catch errors happen during the life of a WebSocket connection, attach a handler to the "error" event: + +{{< code >}} + +```javascript +import ws from 'k6/ws'; +import { check } from 'k6'; + +export default function () { + const url = 'ws://echo.websocket.org'; + const params = { tags: { my_tag: 'hello' } }; + + const res = ws.connect(url, params, function (socket) { + socket.on('open', function open() { + // ... + }); + + socket.on('error', function (e) { + if (e.error() != 'websocket: close sent') { + console.log('An unexpected error occured: ', e.error()); + } + }); + }); + + check(res, { 'status is 101': (r) => r && r.status === 101 }); +} +``` + +{{< /code >}} + +## Timers + +To schedule a recurring action, use the [socket.setInterval](https://grafana.com/docs/k6//javascript-api/k6-ws/socket#section-socketsetinterval) to specify a function to call at a particular interval. + +{{< code >}} + +```javascript +import ws from 'k6/ws'; +import { check } from 'k6'; + +export default function () { + const url = 'ws://echo.websocket.org'; + const params = { tags: { my_tag: 'hello' } }; + + const res = ws.connect(url, params, function (socket) { + socket.on('open', function open() { + console.log('connected'); + + socket.setInterval(function timeout() { + socket.ping(); + console.log('Pinging every 1sec (setInterval test)'); + }, 1000); + }); + + socket.on('ping', () => console.log('PING!')); + socket.on('pong', () => console.log('PONG!')); + socket.on('close', () => console.log('disconnected')); + }); + + check(res, { 'status is 101': (r) => r && r.status === 101 }); +} +``` + +{{< /code >}} + +## Timeouts + +To add a timeout to the WebSocket connection, pass both a handler function and a timeout value (in milliseconds) to the [socket.setTimeout](https://grafana.com/docs/k6//javascript-api/k6-ws/socket/socket-settimeout) function. + +{{< code >}} + +```javascript +import ws from 'k6/ws'; +import { check } from 'k6'; + +export default function () { + const url = 'ws://echo.websocket.org'; + const params = { tags: { my_tag: 'hello' } }; + + const res = ws.connect(url, params, function (socket) { + socket.on('open', () => console.log('connected')); + socket.on('close', () => console.log('disconnected')); + + socket.setTimeout(function () { + console.log('2 seconds passed, closing the socket'); + socket.close(); + }, 2000); + }); + + check(res, { 'status is 101': (r) => r && r.status === 101 }); +} +``` + +{{< /code >}} + +In the preceding example, the timeout will close the WebSocket connection after 2 seconds. + +## Multiple event handlers + +You can attach multiple handler functions to an event: + +{{< code >}} + +```javascript +import ws from 'k6/ws'; +import { check } from 'k6'; + +export default function () { + const url = 'ws://echo.websocket.org'; + const params = { tags: { my_tag: 'hello' } }; + + const response = ws.connect(url, params, function (socket) { + socket.on('open', function open() { + console.log('connected'); + socket.send(Date.now()); + + socket.setInterval(function timeout() { + socket.ping(); + console.log('Pinging every 1sec (setInterval test)'); + }, 1000); + }); + + socket.on('ping', () => console.log('PING!')); + socket.on('pong', () => console.log('PONG!')); + socket.on('pong', () => { + // Multiple event handlers on the same event + console.log('OTHER PONG!'); + }); + + socket.on('close', () => console.log('disconnected')); + + socket.on('error', (e) => { + if (e.error() != 'websocket: close sent') { + console.log('An unexpected error occurred: ', e.error()); + } + }); + + socket.setTimeout(function () { + console.log('2 seconds passed, closing the socket'); + socket.close(); + }, 2000); + }); + + check(response, { 'status is 101': (r) => r && r.status === 101 }); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/using-k6/scenarios/_index.md b/docs/sources/k6/v0.55.x/using-k6/scenarios/_index.md new file mode 100644 index 000000000..7871389b1 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/scenarios/_index.md @@ -0,0 +1,208 @@ +--- +title: Scenarios +description: 'Scenarios allow us to make in-depth configurations to how VUs and iterations are scheduled. This makes it possible to model diverse traffic patterns in load tests.' +weight: 13 +--- + +# Scenarios + +Scenarios configure how VUs and iteration schedules in granular detail. +With scenarios, you can model diverse _workloads_, or traffic patterns in load tests. + +Benefits of using scenarios include: + +- **Easier, more flexible test organization.** You can declare multiple scenarios in the same script, + and each one can independently execute a different JavaScript function. +- **Simulate more realistic traffic.** + Every scenario can use a distinct VU and iteration scheduling pattern, + powered by a purpose-built [executor](#scenario-executors). +- **Parallel or sequential workloads.** Scenarios are independent from each other and run in parallel, though they can be made to appear sequential by setting the `startTime` property of each carefully. +- **Granular results analysis.** Different environment variables and metric tags can be set per scenario. + +## Configure scenarios + +To configure scenarios, use the `scenarios` key in the `options` object. +You can give the scenario any name, as long as each scenario name in the script is unique. + +The scenario name appears in the result summary, tags, and so on. + +{{< code >}} + +```javascript +export const options = { + scenarios: { + example_scenario: { + // name of the executor to use + executor: 'shared-iterations', + + // common scenario configuration + startTime: '10s', + gracefulStop: '5s', + env: { EXAMPLEVAR: 'testing' }, + tags: { example_tag: 'testing' }, + + // executor-specific configuration + vus: 10, + iterations: 200, + maxDuration: '10s', + }, + another_scenario: { + /*...*/ + }, + }, +}; +``` + +{{< /code >}} + +## Scenario executors + +For each k6 scenario, the VU workload is scheduled by an _executor_. +Executors configure how long the test runs, whether traffic stays constant or changes, and whether the workload is modeled by VUs or by arrival rate (that is, [open or closed models](https://grafana.com/docs/k6//using-k6/scenarios/concepts/open-vs-closed)). + +Your scenario object must define the `executor` property with one of the predefined executor names. +Your choice of executor determines how k6 models load. +Choices include: + +- **By number of iterations.** + + - [`shared-iterations`](https://grafana.com/docs/k6//using-k6/scenarios/executors/shared-iterations) shares iterations between VUs. + - [`per-vu-iterations`](https://grafana.com/docs/k6//using-k6/scenarios/executors/per-vu-iterations) has each VU run the configured iterations. + +- **By number of VUs.** + + - [`constant-VUs`](https://grafana.com/docs/k6//using-k6/scenarios/executors/constant-vus) sends VUs at a constant number. + - [`ramping-vus`](https://grafana.com/docs/k6//using-k6/scenarios/executors/ramping-vus) ramps the number of VUs according to your configured stages. + +- **By iteration rate.** + + - [`constant-arrival-rate`](https://grafana.com/docs/k6//using-k6/scenarios/executors/constant-arrival-rate) starts iterations at a constant rate. + - [`ramping-arrival-rate`](https://grafana.com/docs/k6//using-k6/scenarios/executors/ramping-arrival-rate) ramps the iteration rate according to your configured stages. + +Along with the generic scenario options, each executor object has additional options specific to its workload. +For the full list, refer to [Executors](https://grafana.com/docs/k6//using-k6/scenarios/executors). + +## Scenario options {#options} + +| Option | Type | Description | Default | +| ------------------------------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- | +| executor(required) ️ | string | Unique executor name. See the list of possible values in the [executors](https://grafana.com/docs/k6//using-k6/scenarios/executors) section. | - | +| startTime | string | Time offset since the start of the test, at which point this scenario should begin execution. | `"0s"` | +| gracefulStop | string | Time to wait for iterations to finish executing before stopping them forcefully. To learn more, read [Graceful stop](https://grafana.com/docs/k6//using-k6/scenarios/concepts/graceful-stop). | `"30s"` | +| exec | string | Name of exported JS function to execute. | `"default"` | +| env | object | Environment variables specific to this scenario. | `{}` | +| tags | object | [Tags](https://grafana.com/docs/k6//using-k6/tags-and-groups) specific to this scenario. | `{}` | +| options | object | Additional options include [browser options](https://grafana.com/docs/k6//using-k6-browser/options/). | `{}` | + +## Scenario example + +This script combines two scenarios, with sequencing: + +- The `shared_iter_scenario` starts immediately. Ten VUs try to use 100 iterations as quickly as possible (some VUs may use more iterations than others). +- The `per_vu_scenario` starts after 10s. In this case, ten VUs each run ten iterations. + +Which scenario takes longer? +You can run to discover. +You can also add a `maxDuration` property to one or both scenarios. + +```javascript +import http from 'k6/http'; + +export const options = { + scenarios: { + shared_iter_scenario: { + executor: 'shared-iterations', + vus: 10, + iterations: 100, + startTime: '0s', + }, + per_vu_scenario: { + executor: 'per-vu-iterations', + vus: 10, + iterations: 10, + startTime: '10s', + }, + }, +}; + +export default function () { + http.get('https://test.k6.io/'); +} +``` + +If you run a script with scenarios, k6 output includes high-level information about each one. +For example, if you run the preceding script, `k6 run scenario-example.js`, +then k6 reports the scenarios as follows: + +{{< code >}} + +```bash + execution: local + script: scenario-example.js + output: - + + scenarios: (100.00%) 2 scenarios, 20 max VUs, 10m40s max duration (incl. grace +ful stop): + * shared_iter_scenario: 100 iterations shared among 10 VUs (maxDurati +on: 10m0s, gracefulStop: 30s) + * per_vu_scenario: 10 iterations for each of 10 VUs (maxDuration: 10m +0s, startTime: 10s, gracefulStop: 30s) +``` + +{{< /code >}} + +The full output includes the summary metrics, like any default end-of-test summary: + +{{< collapse title="full scenario-example.js output" >}} + +```bash + /\ |‾‾| /‾‾/ /‾‾/ + /\ / \ | |/ / / / + / \/ \ | ( / ‾‾\ + / \ | |\ \ | (‾) | + / __________ \ |__| \__\ \_____/ .io + + execution: local + script: scenario-example.js + output: - + + scenarios: (100.00%) 2 scenarios, 20 max VUs, 10m40s max duration (incl. grace +ful stop): + * shared_iter_scenario: 100 iterations shared among 10 VUs (maxDurati +on: 10m0s, gracefulStop: 30s) + * per_vu_scenario: 10 iterations for each of 10 VUs (maxDuration: 10m +0s, startTime: 10s, gracefulStop: 30s) + + +running (00m12.8s), 00/20 VUs, 200 complete and 0 interrupted iterations +shared_iter_scenario ✓ [ 100% ] 10 VUs 00m02.7s/10m0s 100/100 shared iters +per_vu_scenario ✓ [ 100% ] 10 VUs 00m02.8s/10m0s 100/100 iters, 10 per V + + data_received..................: 2.4 MB 188 kB/s + data_sent......................: 26 kB 2.1 kB/s + http_req_blocked...............: avg=64.26ms min=1.56µs med=8.28µs max +=710.86ms p(90)=57.63ms p(95)=582.36ms + http_req_connecting............: avg=28.6ms min=0s med=0s max +=365.92ms p(90)=20.38ms p(95)=224.44ms + http_req_duration..............: avg=204.25ms min=169.55ms med=204.36ms max +=407.95ms p(90)=205.96ms p(95)=239.32ms + { expected_response:true }...: avg=204.25ms min=169.55ms med=204.36ms max +=407.95ms p(90)=205.96ms p(95)=239.32ms + http_req_failed................: 0.00% ✓ 0 ✗ 200 + http_req_receiving.............: avg=195.54µs min=53.87µs med=162.55µs max +=1.52ms p(90)=260.6µs p(95)=317.89µs + http_req_sending...............: avg=38.16µs min=7.61µs med=37.99µs max +=167.54µs p(90)=50.16µs p(95)=60.18µs + http_req_tls_handshaking.......: avg=24.03ms min=0s med=0s max +=274.05ms p(90)=20.81ms p(95)=212.74ms + http_req_waiting...............: avg=204.01ms min=169.32ms med=204.11ms max +=407.82ms p(90)=205.74ms p(95)=239.01ms + http_reqs......................: 200 15.593759/s + iteration_duration.............: avg=268.83ms min=169.78ms med=204.67ms max +=952.85ms p(90)=444.97ms p(95)=786.52ms + iterations.....................: 200 15.593759/s + vus............................: 10 min=0 max=10 + vus_max........................: 20 min=20 max=20 +``` + +{{< /collapse >}} diff --git a/docs/sources/k6/v0.55.x/using-k6/scenarios/advanced-examples.md b/docs/sources/k6/v0.55.x/using-k6/scenarios/advanced-examples.md new file mode 100644 index 000000000..71e75b89e --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/scenarios/advanced-examples.md @@ -0,0 +1,264 @@ +--- +title: 'Advanced Examples' +description: 'Advanced Examples using the k6 Scenario API - Using multiple scenarios, different environment variables and tags per scenario.' +weight: 02 +--- + +# Advanced Examples + +You can use multiple scenarios in one script, and these scenarios can be run in sequence or in parallel. +Some ways that you can combine scenarios include the following: + +- Have different start times to sequence workloads +- Add per-scenario tags and environment variables +- Make scenario-specific thresholds. +- Use multiple scenarios to run different test logic, so that VUs don't run only the [`default` function](https://grafana.com/docs/k6//using-k6/test-lifecycle). + +## Combine scenarios + +With the `startTime` property, you can configure your script to start some scenarios later than others. +To sequence your scenarios, you can combine `startTime` with the duration options specific to the executor. +(this is easiest to do with executors with set durations, like the arrival-rate executors). + +This script has two scenarios, `contacts` and `news`, which run in sequence: + +1. At the beginning of the test, k6 starts the `contacts` scenario. 50 VUs try to run as many iterations as possible for 30 seconds. +1. After 30 seconds, k6 starts the `news` scenario. 50 VUs each try to run 100 iterations in one minute. + +Along with `startTime`, `duration`, and `maxDuration`, note the different test logic for each scenario. + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + discardResponseBodies: true, + scenarios: { + contacts: { + executor: 'constant-vus', + exec: 'contacts', + vus: 50, + duration: '30s', + }, + news: { + executor: 'per-vu-iterations', + exec: 'news', + vus: 50, + iterations: 100, + startTime: '30s', + maxDuration: '1m', + }, + }, +}; + +export function contacts() { + http.get('https://test.k6.io/contacts.php', { + tags: { my_custom_tag: 'contacts' }, + }); +} + +export function news() { + http.get('https://test.k6.io/news.php', { tags: { my_custom_tag: 'news' } }); +} +``` + +{{< /code >}} + +## Use different environment variables and tags per scenario. + +The previous example sets tags on individual HTTP request metrics. +But, you can also set tags per scenario, which applies them to other +[taggable](https://grafana.com/docs/k6//using-k6/tags-and-groups#tags) objects as well. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { fail } from 'k6'; + +export const options = { + discardResponseBodies: true, + scenarios: { + contacts: { + executor: 'constant-vus', + exec: 'contacts', + vus: 50, + duration: '30s', + tags: { my_custom_tag: 'contacts' }, + env: { MYVAR: 'contacts' }, + }, + news: { + executor: 'per-vu-iterations', + exec: 'news', + vus: 50, + iterations: 100, + startTime: '30s', + maxDuration: '1m', + tags: { my_custom_tag: 'news' }, + env: { MYVAR: 'news' }, + }, + }, +}; + +export function contacts() { + if (__ENV.MYVAR != 'contacts') fail(); + http.get('https://test.k6.io/contacts.php'); +} + +export function news() { + if (__ENV.MYVAR != 'news') fail(); + http.get('https://test.k6.io/news.php'); +} +``` + +{{< /code >}} + +{{< admonition type="note" >}} + +By default, k6 applies a `scenario` tag to all metrics in each scenario, whose value is the scenario name. +You can combine these tags with thresholds, or use them to simplify results filtering. + +To disable scenario tags, use the [`--system-tags` option](https://grafana.com/docs/k6//using-k6/k6-options#system-tags). + +{{< /admonition >}} + +## Run multiple scenario functions, with different thresholds + +You can also set different thresholds for different scenario functions. +To do this: + +1. Set scenario-specific tags +1. Set thresholds for these tags. + +This test has 3 scenarios, each with different `exec` functions, tags and environment variables, and thresholds: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + scenarios: { + my_web_test: { + // some arbitrary scenario name + executor: 'constant-vus', + vus: 50, + duration: '5m', + gracefulStop: '0s', // do not wait for iterations to finish in the end + tags: { test_type: 'website' }, // extra tags for the metrics generated by this scenario + exec: 'webtest', // the function this scenario will execute + }, + my_api_test_1: { + executor: 'constant-arrival-rate', + rate: 90, + timeUnit: '1m', // 90 iterations per minute, i.e. 1.5 RPS + duration: '5m', + preAllocatedVUs: 10, // the size of the VU (i.e. worker) pool for this scenario + tags: { test_type: 'api' }, // different extra metric tags for this scenario + env: { MY_CROC_ID: '1' }, // and we can specify extra environment variables as well! + exec: 'apitest', // this scenario is executing different code than the one above! + }, + my_api_test_2: { + executor: 'ramping-arrival-rate', + startTime: '30s', // the ramping API test starts a little later + startRate: 50, + timeUnit: '1s', // we start at 50 iterations per second + stages: [ + { target: 200, duration: '30s' }, // go from 50 to 200 iters/s in the first 30 seconds + { target: 200, duration: '3m30s' }, // hold at 200 iters/s for 3.5 minutes + { target: 0, duration: '30s' }, // ramp down back to 0 iters/s over the last 30 second + ], + preAllocatedVUs: 50, // how large the initial pool of VUs would be + maxVUs: 100, // if the preAllocatedVUs are not enough, we can initialize more + tags: { test_type: 'api' }, // different extra metric tags for this scenario + env: { MY_CROC_ID: '2' }, // same function, different environment variables + exec: 'apitest', // same function as the scenario above, but with different env vars + }, + }, + discardResponseBodies: true, + thresholds: { + // we can set different thresholds for the different scenarios because + // of the extra metric tags we set! + 'http_req_duration{test_type:api}': ['p(95)<250', 'p(99)<350'], + 'http_req_duration{test_type:website}': ['p(99)<500'], + // we can reference the scenario names as well + 'http_req_duration{scenario:my_api_test_2}': ['p(99)<300'], + }, +}; + +export function webtest() { + http.get('https://test.k6.io/contacts.php'); + sleep(Math.random() * 2); +} + +export function apitest() { + http.get(`https://test-api.k6.io/public/crocodiles/${__ENV.MY_CROC_ID}/`); + // no need for sleep() here, the iteration pacing will be controlled by the + // arrival-rate executors above! +} +``` + +{{< /code >}} + +## Run specific scenario via environment variable + +k6 runs all [scenarios](https://grafana.com/docs/k6//using-k6/scenarios/#scenarios) listed in a test script by default. But, with some small code changes and using [environment variables](https://grafana.com/docs/k6//using-k6/environment-variables/#environment-variables), you can tell k6 to only run a specific scenario via the command-line. + +The following example shows a test script that uses a `SCENARIO` environment variable, if it exists, to choose which scenario to execute: +{{< code >}} + +```javascript +import http from 'k6/http'; + +const scenarios = { + my_web_test: { + executor: 'per-vu-iterations', + vus: 1, + iterations: 1, + maxDuration: '30s', + }, + my_api_test: { + executor: 'per-vu-iterations', + vus: 1, + iterations: 1, + maxDuration: '30s', + }, +}; + +const { SCENARIO } = __ENV; +export const options = { + // if a scenario is passed via a CLI env variable, then run that scenario. Otherwise, run + // using the pre-configured scenarios above. + scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : scenarios, + discardResponseBodies: true, + thresholds: { + http_req_duration: ['p(95)<250', 'p(99)<350'], + }, +}; + +export default function () { + const response = http.get('https://test-api.k6.io/public/crocodiles/'); +} +``` + +{{< /code >}} + +Then from the command line, you could run the test script and only execute the `my_web_test` scenario by running: + +{{< code >}} + +```bash +$ SCENARIO=my_web_test k6 run script.js +``` + +```windows +C:\k6> set "SCENARIO=my_web_test" && k6 run script.js +``` + +```powershell +PS C:\k6> $env:SCENARIO="my_web_test"; k6 run script.js +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/_index.md b/docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/_index.md new file mode 100644 index 000000000..ce2ce5c0f --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/_index.md @@ -0,0 +1,20 @@ +--- +title: 'Concepts' +description: High-level explanations about how your executor configuration can change the test execution and test results +weight: -10 +--- + +# Concepts + +These topics explain the essential concepts of how scenarios and their executors work. + +Different scenario configurations can affect many different aspects of your system, +including the generated load, utilized resources, and emitted metrics. +If you know a bit about how scenarios work, you'll design better tests and interpret test results with more understanding. + +| On this page | Read about | +| ----------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | +| [Open and closed models](https://grafana.com/docs/k6//using-k6/scenarios/concepts/open-vs-closed) | Different ways k6 can schedule VUs, their affects on test results, and how k6 implements the open model in its arrival-rate executors | +| [Graceful Stop](https://grafana.com/docs/k6//using-k6/scenarios/concepts/graceful-stop) | A configurable period for iterations to finish or ramp down after the test reaches its scheduled duration | +| [Arrival-rate VU allocation](https://grafana.com/docs/k6//using-k6/scenarios/concepts/arrival-rate-vu-allocation) | How k6 allocates VUs in arrival-rate executors | +| [Dropped iterations](https://grafana.com/docs/k6//using-k6/scenarios/concepts/dropped-iterations) | Possible reasons k6 might drop a scheduled iteration | diff --git a/docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/arrival-rate-vu-allocation.md b/docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/arrival-rate-vu-allocation.md new file mode 100644 index 000000000..f5a8dea30 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/arrival-rate-vu-allocation.md @@ -0,0 +1,124 @@ +--- +title: Arrival-rate VU allocation +description: How k6 allocates VUs in the open-model, arrival-rate executors +weight: 20 +--- + +# Arrival-rate VU allocation + +In arrival-rate executors, as long as k6 has VUs available, it starts iterations according to your target rate. +The ability to set iteration rate comes with a bit more configuration complexity: you must pre-allocate a sufficient number of VUs. +In other words, before the tests runs, you must both: + +- Configure load (as new iterations per unit of time) +- Ensure that you've allocated enough VUs. + +Read on to learn about how k6 allocates VUs in the arrival-rate executors. + +{{< admonition type="caution" >}} + +In cloud tests, **`preAllocatedVUs` count against your subscription.** + +When planning a test, consider doing a trial initialization on a local machine to ensure you're allocating VUs efficiently. + +{{< /admonition >}} + +## Pre-allocation in arrival-rate executors + +As [open-model](https://grafana.com/docs/k6//using-k6/scenarios/concepts/open-vs-closed#open-model) scenarios, arrival-rate executors start iterations according to a configured rate. +For example, you can configure arrival-rate executors to start 10 iterations each second, or minute, or hour. +This behavior is opposed to the closed-model scenarios, in which VUs wait for one iteration to finish before starting another + +Each iteration needs a VU to run it. +Because k6 VUs are single threaded, like other JavaScript runtimes, a VU can only run a single iteration (and its event loop) at a time. +To ensure you have enough, you must pre-allocate a sufficient number. + +In your arrival-rate configuration, three properties determine the iteration rate: + +- k6 starts `rate` number of iterations evenly across the `timeUnit` (default 1s). +- `preAllocatedVUs` sets the number of VUs to initialize to meet the target iteration rate. + +For example, with a `constant-arrival-rate` executor and `rate: 10`, k6 tries to start a new iteration every 100 milliseconds. If the scenario has `rate: 10, timeUnit: '1m'`, k6 tries to start a new iteration every 6 seconds. +Whether it _can_ start the target iteration rate depends on whether the number of allocated VUs is sufficient. + +```javascript +export const options = { + scenarios: { + constant_load: { + executor: 'constant-arrival-rate', + preAllocatedVUs: 10, + rate: 10, + timeUnit: '1m', + }, + }, +}; +``` + +In practice, determining the right number of VUs to be able to reach the target iteration rate might take some trial and error, +as the necessary VUs entirely depends on what your iteration code looks like and how quickly the system under test can process requests. + +## How k6 uses allocated VUs + +Before an arrival-rate scenario starts, k6 first initializes the number of `preAllocatedVUs`. +When the test runs, +the number of available `preAllocatedVUs` determines how many iterations k6 can start. +k6 tries to reach the target iterations per second, +and one of two things can happen: + +| If the executor | Then.. | +| --------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Has enough VUs | the extra VUs are "idle," ready to be used when needed. | +| Has insufficient VUs. | k6 emits a [`dropped_iterations` metric](https://grafana.com/docs/k6//using-k6/scenarios/concepts/dropped-iterations) for each iteration that it can't run. | + +## Iteration duration affects the necessary allocation + +The necessary allocation depends on the iteration duration: +longer durations need more VUs. + +In a perfect world, you could estimate the number of pre-allocated VUs with this formula: + +``` +preAllocatedVUs = [median_iteration_duration * rate] + constant_for_variance +``` + +In the real world, if you know _exactly_ how long an iteration takes, you likely don't need to run a test. +What's more, as the test goes on, iteration duration likely increases. +If response times slow so much that k6 lacks the VUs to start iterations at the desired rate, +the allocation might be insufficient and k6 will drop iterations. + +To determine your strategy, you can run tests locally and gradually add more pre-allocated VUs. +As dropped iterations can also indicate that the system performance is degrading, this early experimentation can provide useful data on its own. + +## You probably don't need `maxVUs` + +{{< admonition type="caution" >}} + +In cloud tests, the number of `maxVUs` counts against your subscription, +**overriding the number set by `preAllocatedVUs`**. + +{{< /admonition >}} + +The arrival-rate executors also have a `maxVUs` property. +If you set it, k6 runs in this sequence: + +1. Pre-allocate the `preAllocatedVUs`. +1. Run the test, trying to reach the target iteration rate. +1. If the target exceeds the available VUs, allocate another VU. +1. If the target still exceeds available VUs, continue allocating VUs until reaching the number set by `maxVUs`. + +Though it seems convenient, you should avoid using `maxVUs` in most cases. +Allocating VUs has CPU and memory costs, and allocating VUs as the test runs **can overload the load generator and skew results**. +In Cloud tests, the number of `maxVUs` count against your subscription. +This is because k6 must allocate sufficient resources for `maxVUs` to be initialized, even if they never are. +In almost all cases, the best thing to do is to pre-allocate the number of VUs you need beforehand. + +Some of the times it might make sense to use `maxVUs` include: + +- To determine necessary allocation in first-time tests +- To add a little "cushion" to the pre-allocated VUs that you expect the test needs +- In huge, highly distributed tests, in which you need to carefully scale load generators as you increment VUs. + +## You can't guarantee which VU runs an iteration + +As with all executors, you can't predict the specific VU that an arrival-rate executor uses for a specific iteration. +As the test runs on, the executor might use some or all of the allocated VUs, even if it never needs the entire allocated number to reach the iteration rate. diff --git a/docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/dropped-iterations.md b/docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/dropped-iterations.md new file mode 100644 index 000000000..66e80376c --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/dropped-iterations.md @@ -0,0 +1,44 @@ +--- +title: Dropped iterations +description: Explanations about how your scenario configuration or SUT performance can lead to dropped iterations +weight: 30 +--- + +# Dropped iterations + +Sometimes, a scenario can't run the expected number of iterations. +k6 tracks the number of unsent iterations in a counter metric, `dropped_iterations`. +The number of dropped iterations can be valuable data when you debug executors or analyze results. + +Dropped iterations usually happen for one of two reasons: + +- The executor configuration is insufficient. +- The SUT can't handle the configured VU arrival rate. + +### Configuration-related iteration drops + +Dropped iterations happen for different reasons in different types of executors. + +With `shared-iterations` and `per-vu-iterations`, iterations drop if the scenario reaches its `maxDuration` before all iterations finish. +To mitigate this, you likely need to increase the value of the duration. + +With `constant-arrival-rate` and `ramping-arrival-rate`, iterations drop if there are no free VUs. +**If it happens at the beginning of the test, you likely just need to allocate more VUs.** +If this happens later in the test, the dropped iterations might happen because SUT performance is degrading and iterations are taking longer to finish. + +### SUT-related iteration drops + +At a certain point of high latency or longer iteration durations, k6 will no longer have free VUs to start iterations with at the configured rate. +As a result, the executor will drop iterations. + +The reasons for these dropped iterations vary: + +- The SUT response has become so long that k6 starts dropping scheduled iterations from the queue. +- The SUT iteration duration has become so long that k6 needs to schedule more VUs to reach the target arrival rate, exceeding the number of scheduled iterations. + +As the causes vary, dropped iterations might mean different things. +A few dropped iterations might indicate a quick network error. +Many dropped iterations might indicate that your SUT has completely stopped responding. + +When you design your test, consider what an acceptable rate of dropped iterations is (the _error budget_). +To assert that the SUT responds within this error budget, you can use the `dropped_iterations` metric in a [Threshold](https://grafana.com/docs/k6//using-k6/thresholds). diff --git a/docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/graceful-stop.md b/docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/graceful-stop.md new file mode 100644 index 000000000..b828ac939 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/graceful-stop.md @@ -0,0 +1,97 @@ +--- +title: 'Graceful stop' +description: 'This option is available for all executors except externally-controlled and allows the user to specify a duration to wait before forcefully interrupting them.' +weight: 10 +--- + +# Graceful stop + +The `gracefulStop` is a period at the end of the test in which k6 lets iterations in progress finish. + +If a test has a set duration or ramp down, its possible that k6 could interrupt iterations in progress. +These interruptions can lead to skewed metrics and unexpected test results. +To deal with this, k6 scenarios have a `gracefulStop`. +For the `ramping-vus` executor, a related option, `gracefulRampDown`, exists to let VUs finish as their number ramps down. + +## Graceful stop + +The `gracefulStop` option is available for all executors except `externally-controlled`. +It specifies a duration that k6 will wait before forcefully interrupting an iteration. +The default value is `30s`. + +### Graceful stop example + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + discardResponseBodies: true, + scenarios: { + contacts: { + executor: 'constant-vus', + vus: 100, + duration: '10s', + gracefulStop: '3s', + }, + }, +}; + +export default function () { + const delay = Math.floor(Math.random() * 5) + 1; + http.get(`https://httpbin.test.k6.io/delay/${delay}`); +} +``` + +{{< /code >}} + +Running this script would result in something like: + +```bash +running (13.0s), 000/100 VUs, 349 complete and 23 interrupted iterations +contacts ✓ [======================================] 100 VUs 10s +``` + +Notice that even though the total test duration is 10s, the actual execution time was 13s +because of `gracefulStop`, giving the VUs a 3s additional time to complete iterations in progress. 23 +of the iterations currently in progress did not complete within this window and was therefore interrupted. + +## The `gracefulRampDown` + +In addition to `gracefulStop`, the [ramping-vus](https://grafana.com/docs/k6//using-k6/scenarios/executors/ramping-vus) executor also has a `gracefulRampDown`. + +When the target value for a stage is lower than the target for the previous stage, k6 might need to stop some VUs that were started during the previous stages. +The `gracefulRampDown` option controls how long these VUs have to finish currently before k6 interrupts them. + +To get an idea of how `gracefulRampDown` works, you can run the following script with +`k6 run --verbose`. +In this script, the iteration `sleep` time exceeds the `gracefulRampdown` time. +So, as k6 ramps down to reach the target of the second stage, it must forcibly interrupt VUs. +The `--verbose` flag will log to your console when VUs start, enter the grace period, and are forcibly interrupted. + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + discardresponsebodies: true, + scenarios: { + contacts: { + executor: 'ramping-vus', + startvus: 0, + stages: [ + { duration: '10s', target: 10 }, + { duration: '10s', target: 0 }, + ], + gracefulRampDown: '1s', + }, + }, +}; + +export default function () { + http.get('https://test.k6.io/contacts.php'); + // adding sleep beyond so that iterations are longer than rampdown + sleep(5); +} +``` diff --git a/docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/open-vs-closed.md b/docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/open-vs-closed.md new file mode 100644 index 000000000..1fc96de47 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/scenarios/concepts/open-vs-closed.md @@ -0,0 +1,127 @@ +--- +title: 'Open and closed models' +description: 'k6 has two ways to schedule VUs, which can affect test results. k6 implements the open model in its arrival-rate executors.' +weight: 00 +--- + +# Open and closed models + +Different k6 executors have different ways of scheduling VUs. +Some executors use the _closed model_, while the arrival-rate executors use the _open model_. + +In short, in the closed model, VU iterations start only when the last iteration finishes. +In the open model, on the other hand, VUs arrive independently of iteration completion. +Different models suit different test aims. + +## Closed Model + +In a closed model, the execution time of each iteration dictates the +number of iterations executed in your test. +The next iteration doesn't start until the previous one finishes. + +Thus, in a closed model, the start or arrival rate of +new VU iterations is tightly coupled with the iteration duration (that is, time from start +to finish of the VU's `exec` function, by default the `export default function`): + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + scenarios: { + closed_model: { + executor: 'constant-vus', + vus: 1, + duration: '1m', + }, + }, +}; + +export default function () { + // The following request will take roughly 6s to complete, + // resulting in an iteration duration of 6s. + + // As a result, New VU iterations will start at a rate of 1 per 6s, + // and we can expect to get 10 iterations completed in total for the + // full 1m test duration. + + http.get('https://httpbin.test.k6.io/delay/6'); +} +``` + +{{< /code >}} + +Running this script would result in something like: + +```bash + +running (1m01.5s), 0/1 VUs, 10 complete and 0 interrupted iterations +closed_model ✓ [======================================] 1 VUs 1m0s + +``` + +### Drawbacks of using the closed model + +When the duration of the VU iteration is tightly coupled to the start of new VU iterations, +the target system's response time can influence the throughput of the test. +Slower response times means longer iterations and a lower arrival rate of new iterations―and vice versa for faster response times. +In some testing literature, this problem is known as _coordinated omission._ + +In other words, when the target system is stressed and starts to respond more +slowly, a closed model load test will wait, resulting in increased +iteration durations and a tapering off of the arrival rate of new VU iterations. + +This effect is not ideal when the goal is to simulate a certain arrival rate of new VUs, +or more generally throughput (e.g. requests per second). + +## Open model + +Compared to the closed model, the open model decouples VU iterations from +the iteration duration. +The response times of the target system no longer +influence the load on the target system. + +To fix this problem of coordination, you can use an open model, +which decouples the start of new VU iterations from the iteration duration. +This reduces the influence of the target system's response time. + +![Arrival rate closed/open models](/media/docs/k6-oss/arrival-rate-open-closed-model.png) + +k6 implements the open model with two _arrival rate_ executors: +[constant-arrival-rate](https://grafana.com/docs/k6//using-k6/scenarios/executors/constant-arrival-rate) and [ramping-arrival-rate](https://grafana.com/docs/k6//using-k6/scenarios/executors/ramping-arrival-rate): + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + scenarios: { + open_model: { + executor: 'constant-arrival-rate', + rate: 1, + timeUnit: '1s', + duration: '1m', + preAllocatedVUs: 20, + }, + }, +}; + +export default function () { + // With the open model arrival rate executor config above, + // new VU iterations will start at a rate of 1 every second, + // and we can thus expect to get 60 iterations completed + // for the full 1m test duration. + http.get('https://httpbin.test.k6.io/delay/6'); +} +``` + +{{< /code >}} + +Running this script would result in something like: + +```bash +running (1m09.3s), 000/011 VUs, 60 complete and 0 interrupted iterations +open_model ✓ [======================================] 011/011 VUs 1m0s 1 iters/s +``` diff --git a/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/_index.md b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/_index.md new file mode 100644 index 000000000..c3739f267 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/_index.md @@ -0,0 +1,50 @@ +--- +title: 'Executors' +description: 'Executors control how k6 schedules VUs and iterations. Choose the executor to model traffic you want to model to test your services' +weight: 01 +--- + +# Executors + +**Executors** control how k6 schedules VUs and iterations. +The executor that you choose depends on the goals of your test and the type of traffic you want to model. + +Define the executor in `executor` key of the scenario object. +The value is the executor name separated by hyphens. + +```javascript +export const options = { + scenarios: { + arbitrary_scenario_name: { + //Name of executor + executor: 'ramping-vus', + // more configuration here + }, + }, +}; +``` + +## All executors + +The following table lists all k6 executors and links to their documentation. + +| Name | Value | Description | +| -------------------------------------------------------------------------------------------------------------------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| [Shared iterations](https://grafana.com/docs/k6//using-k6/scenarios/executors/shared-iterations) | `shared-iterations` | A fixed amount of iterations are
shared between a number of VUs. | +| [Per VU iterations](https://grafana.com/docs/k6//using-k6/scenarios/executors/per-vu-iterations) | `per-vu-iterations` | Each VU executes an exact number of iterations. | +| [Constant VUs](https://grafana.com/docs/k6//using-k6/scenarios/executors/constant-vus) | `constant-vus` | A fixed number of VUs execute as many
iterations as possible for a specified amount of time. | +| [Ramping VUs](https://grafana.com/docs/k6//using-k6/scenarios/executors/ramping-vus) | `ramping-vus` | A variable number of VUs execute as many
iterations as possible for a specified amount of time. | +| [Constant Arrival Rate](https://grafana.com/docs/k6//using-k6/scenarios/executors/constant-arrival-rate) | `constant-arrival-rate` | A fixed number of iterations are executed
in a specified period of time. | +| [Ramping Arrival Rate](https://grafana.com/docs/k6//using-k6/scenarios/executors/ramping-arrival-rate) | `ramping-arrival-rate` | A variable number of iterations are
executed in a specified period of time. | +| [Externally Controlled](https://grafana.com/docs/k6//using-k6/scenarios/executors/externally-controlled) | `externally-controlled` | Control and scale execution at runtime
via [k6's REST API](https://grafana.com/docs/k6//misc/k6-rest-api) or the [CLI](https://k6.io/blog/how-to-control-a-live-k6-test). | + +{{< admonition type="note" >}} + +For any given scenario, you can't guarantee that a specific VU can run a specific iteration. + +With [`SharedArray`](https://grafana.com/docs/k6//javascript-api/k6-data/sharedarray) and [execution context variables](https://grafana.com/docs/k6//using-k6/execution-context-variables), you can map a specific VU to a specific value in your test data. +So the tenth VU could use the tenth item in your array (or the sixth iteration to the sixth item). + +But, you _cannot_ reliably map, for example, the tenth VU to the tenth iteration. + +{{< /admonition >}} diff --git a/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/constant-arrival-rate.md b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/constant-arrival-rate.md new file mode 100644 index 000000000..c3f42f6eb --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/constant-arrival-rate.md @@ -0,0 +1,108 @@ +--- +title: 'Constant arrival rate' +description: 'A fixed number of iterations are started in a specified period of time.' +weight: 05 +--- + +# Constant arrival rate + +With the `constant-arrival-rate` executor, k6 starts a fixed number of iterations over a specified period of time. +It is an open-model executor, meaning iterations start independently of system response (for details, read +[Open and Closed models](https://grafana.com/docs/k6//using-k6/scenarios/concepts/open-vs-closed)). + +This executor continues to start iterations at the given rate as long as VUs are available. +The time to execute an iteration can vary with test logic or the system-under-test response time. +To compensate for this, the executor starts a varied number of VUs to meet the configured iteration rate. +For explanations of how allocation works, read [Arrival-rate VU allocation](https://grafana.com/docs/k6//using-k6/scenarios/concepts/arrival-rate-vu-allocation). + +{{< admonition type="note" >}} + +**Iteration starts are spaced fractionally.** +Iterations **do not** start at exactly the same time. +At a `rate` of `10` with a `timeUnit` of `1s`, each iteration starts about every tenth of a second (that is, each 100ms). + +{{< /admonition >}} + +## Options + +Besides the [common configuration options](https://grafana.com/docs/k6//using-k6/scenarios#options), +this executor has the following options: + +| Option | Type | Description | Default | +|--------------------------------------|---------|--------------------------------------------------------------------------------|-------------------------------------| +| duration(required) | string | Total scenario duration (excluding `gracefulStop`). | - | +| rate(required) | integer | Number of iterations to start during each `timeUnit` period. | - | +| preAllocatedVUs(required) | integer | Number of VUs to pre-allocate before test start to preserve runtime resources. | - | +| timeUnit | string | Period of time to apply the `rate` value. | `"1s"` | +| maxVUs | integer | Maximum number of VUs to allow during the test run. | If unset, same as `preAllocatedVUs` | + +## When to use + +When you want iterations to remain constant, independent of the performance of the system under test. +This approach is useful for a more accurate representation of RPS, for example. + +{{< admonition type="note" >}} + +**Don't put sleep at the end of an iteration.** + +The arrival-rate executors already pace the iteration rate through the `rate` and `timeUnit` properties. +So it's unnecessary to use a `sleep()` function at the end of the VU code. + +{{< /admonition >}} + +## Example + +This example schedules a constant rate of 30 iterations per second for 30 seconds. +It pre-allocates 2 VUs, and allows k6 to dynamically schedule up to 50 VUs as needed. + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + discardResponseBodies: true, + scenarios: { + contacts: { + executor: 'constant-arrival-rate', + + // How long the test lasts + duration: '30s', + + // How many iterations per timeUnit + rate: 30, + + // Start `rate` iterations per second + timeUnit: '1s', + + // Pre-allocate 2 VUs before starting the test + preAllocatedVUs: 2, + + // Spin up a maximum of 50 VUs to sustain the defined + // constant arrival rate. + maxVUs: 50, + }, + }, +}; + +export default function () { + http.get('https://test.k6.io/contacts.php'); +} +``` + +{{< /code >}} + +## Observations + +The following graph depicts the performance of the [example](#example) script: + +![Ramping VUs](/media/docs/k6-oss/constant-arrival-rate.png) + +Based upon our test scenario inputs and results: + +- The desired rate of 30 iterations started every 1 second is achieved and maintained for the majority of the test. +- The test scenario runs for the specified 30 second duration. +- Having started with 2 VUs (as specified by the `preAllocatedVUs` option), k6 automatically adjusts the number of VUs to achieve the desired rate, up to the `maxVUs`. For this test, this ended up as 17 VUs. +- The number of VUs to achieve the desired rate varies depending on how long each iteration takes to execute. For this test definition, if it would take exactly 1 second, then 30 VUs would be needed. However, as it takes less than 1 second, then less VUs are needed. + +> Using too low of a `preAllocatedVUs` setting will reduce the test duration at the desired rate, as resources need to continually be allocated to achieve the rate. diff --git a/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/constant-vus.md b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/constant-vus.md new file mode 100644 index 000000000..fa84a2aae --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/constant-vus.md @@ -0,0 +1,71 @@ +--- +title: 'Constant VUs' +description: 'A fixed number of VUs execute as many iterations as possible for a specified amount of time.' +weight: 03 +--- + +# Constant VUs + +With the `constant-vus` executor, a fixed number of VUs execute as many iterations as possible for a specified amount of time. + +For a shortcut to this executor, use the [VUs](https://grafana.com/docs/k6//using-k6/k6-options/reference#vus) and [duration](https://grafana.com/docs/k6//using-k6/k6-options/reference#duration) options. + +## Options + +Besides the [common configuration options](https://grafana.com/docs/k6//using-k6/scenarios#options), +this executor has the following options: + +| Option | Type | Description | Default | +| ----------------------------- | ------- | --------------------------------------------------- | ------- | +| duration(required) | string | Total scenario duration (excluding `gracefulStop`). | - | +| vus | integer | Number of VUs to run concurrently. | `1` | + +## When to use + +Use this executor if you need a specific number of VUs to run for a certain amount of time. + +## Example + +This examples schedules 10 VUs to run constantly for a duration 30 seconds. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + discardResponseBodies: true, + scenarios: { + contacts: { + executor: 'constant-vus', + vus: 10, + duration: '30s', + }, + }, +}; + +export default function () { + http.get('https://test.k6.io/contacts.php'); + // Injecting sleep + // Total iteration time is sleep + time to finish request. + sleep(0.5); +} +``` + +{{< /code >}} + +## Observations + +The following graph depicts the performance of the [example](#example) script: + +![Constant VUs](/media/docs/k6-oss/constant-vus.png) + +Based upon our test scenario inputs and results: + +- The number of VUs is fixed at 10, and are initialized before the test begins; +- Overall test duration is fixed at the configured 30 second duration; +- Each _iteration_ of the `default` function is expected to be roughly 515ms, or ~2/s; +- Maximum throughput (highest efficiency) is therefore expected to be ~20 iters/s, `2 iters/s * 10 VUs`; +- We see that the maximum throughput is reached and maintained for the majority of the test; +- Approximately 600 iterations are therefore performed in total, `30 seconds * 20 iters/s`. diff --git a/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/externally-controlled.md b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/externally-controlled.md new file mode 100644 index 000000000..4299f1f0d --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/externally-controlled.md @@ -0,0 +1,68 @@ +--- +title: 'Externally controlled' +description: 'Control and scale execution at runtime via k6 REST API or the CLI.' +weight: 07 +--- + +# Externally controlled + +Control and scale execution at runtime via [k6's REST API](https://grafana.com/docs/k6//misc/k6-rest-api) or +the [CLI](https://k6.io/blog/how-to-control-a-live-k6-test). + +Previously, the `pause`, `resume`, and `scale` CLI commands were used to globally control +k6 execution. This executor does the same job by providing a better API that can be used to +control k6 execution at runtime. + +Note that, passing arguments to the `scale` CLI command for changing the amount of active or +maximum VUs will only affect the externally controlled executor. + +## Options + +**The `externally-controlled` executor has no graceful stop**. + +Besides that, this executor has all the [common configuration options](https://grafana.com/docs/k6//using-k6/scenarios#options), +and these particular ones: + +| Option | Type | Description | Default | +| ----------------------------- | ------- | --------------------------------------------------- | ------- | +| duration(required) | string | Total test duration. | - | +| vus | integer | Number of VUs to run concurrently. | - | +| maxVUs | integer | Maximum number of VUs to allow during the test run. | - | + +## When to use + +If you want to control the number of VUs while the test is running. + +Important: this is the only executor that is not supported in `k6 cloud`, it can only be used +locally with `k6 run`. + +## Example + +In this example, we'll execute a test controllable at runtime, starting with 10 VUs up to +a maximum of 50, and a total duration of 10 minutes. + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + discardResponseBodies: true, + scenarios: { + contacts: { + executor: 'externally-controlled', + vus: 10, + maxVUs: 50, + duration: '10m', + }, + }, +}; + +export default function () { + http.get('https://test.k6.io/contacts.php'); +} +``` + +Once the test has started, it can be externally controlled with the `pause`, `resume`, and `scale` CLI commands. + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/per-vu-iterations.md b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/per-vu-iterations.md new file mode 100644 index 000000000..21ad9c8ef --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/per-vu-iterations.md @@ -0,0 +1,77 @@ +--- +title: 'Per VU iterations' +description: 'Each VU executes an exact number of iterations.' +weight: 02 +--- + +# Per VU iterations + +With the `per-vu-iterations` executor, each VU executes an exact number of iterations. +The total number of completed iterations equals `vus * iterations`. + +## Options + +Besides the [common configuration options](https://grafana.com/docs/k6//using-k6/scenarios#options), +this executor has the following options: + +| Option | Type | Description | Default | +| ----------- | ------- | ---------------------------------------------------------------------------------- | ------- | +| vus | integer | Number of VUs to run concurrently. | `1` | +| iterations | integer | Number of `exec` function iterations to be executed by each VU. | `1` | +| maxDuration | string | Maximum scenario duration before it's forcibly stopped (excluding `gracefulStop`). | `"10m"` | + +## When to use + +Use this executor if you need a specific number of VUs to complete the same number of +iterations. This can be useful when you have fixed sets of test data that you want to +partition between VUs. + +## Example + +The following example schedules 10 VUs to execute 20 iterations _each_. +The test runs 200 total iterations and has a maximum duration of 30 seconds. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + discardResponseBodies: true, + scenarios: { + contacts: { + executor: 'per-vu-iterations', + vus: 10, + iterations: 20, + maxDuration: '30s', + }, + }, +}; + +export default function () { + http.get('https://test.k6.io/contacts.php'); + // Injecting sleep + // Sleep time is 500ms. Total iteration time is sleep + time to finish request. + sleep(0.5); +} +``` + +{{< /code >}} + +## Observations + +The following graph depicts the performance of the [example](#example) script: + +![Per VU Iterations](/media/docs/k6-oss/per-vu-iterations.png) + +Based upon our test scenario inputs and results: + +- The number of VUs is fixed at 10, and are initialized before the test begins; +- Total iterations are fixed at 20 iterations per VU, i.e. 200 iterations, `10 VUs * 20 iters each`; +- Each _iteration_ of the `default` function is expected to be roughly 515ms, or ~2/s; +- Maximum throughput (highest efficiency) is therefore expected to be ~20 iters/s, `2 iters/s * 10 VUs`; +- The maximum throughput is reached, but not maintained; +- Because the distribution of iterations is even among VUs, a _fast_ VU may finish early and be idle for the remainder of the test, thereby lowering _efficiency_; +- Total duration of 9 seconds is slightly longer than [shared iterations](https://grafana.com/docs/k6//using-k6/scenarios/executors/shared-iterations) due to lower efficiency; +- Overall test duration lasts as long as the _slowest_ VU takes to complete 20 requests. diff --git a/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/ramping-arrival-rate.md b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/ramping-arrival-rate.md new file mode 100644 index 000000000..1cd9b07bd --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/ramping-arrival-rate.md @@ -0,0 +1,144 @@ +--- +title: 'Ramping arrival rate' +description: 'A variable number of iterations are started in a specified period of time.' +weight: 06 +--- + +# Ramping arrival rate + +With the `ramping-arrival-rate` executor, k6 starts iterations at a variable rate. +It is an open-model executor, meaning iterations start independently of system response (for details, read +[Open and Closed models](https://grafana.com/docs/k6//using-k6/scenarios/concepts/open-vs-closed)). + +This executor has _stages_ that configure target number of iterations and the time k6 takes to reach or stay at this target. +Unlike the [ramping VUs executor](https://grafana.com/docs/k6//using-k6/scenarios/executors/ramping-vus), which configures VUs, +this executor dynamically changes the number of iterations to start, and starts these iterations as long as the test has enough allocated VUs. +To learn how allocation works, read [Arrival-rate VU allocation](https://grafana.com/docs/k6//using-k6/scenarios/concepts/arrival-rate-vu-allocation). + +{{< admonition type="note" >}} + +**Iteration starts are spaced fractionally.** +Iterations **do not** start at exactly the same time. +At a `rate` of `10` with a `timeUnit` of `1s`, each iteration starts about every tenth of a second (that is, each 100ms). + +{{< /admonition >}} + +## Options + +Besides the [common configuration options](https://grafana.com/docs/k6//using-k6/scenarios#options), +this executor has the following options: + +| Option | Type | Description | Default | +| ------------------------------------ | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------- | +| stages(required) | array | Array of objects that specify the target number of iterations to ramp up or down to. | `[]` | +| preAllocatedVUs(required) | integer | Number of VUs to pre-allocate before test start to preserve runtime resources. | - | +| startRate | integer | Number of iterations to execute each `timeUnit` period at test start. | `0` | +| timeUnit | string | Period of time to apply the `startRate` to the `stages`' `target` value. Its value is constant for the whole duration of the scenario, it is not possible to change it for a specific stage. | `"1s"` | +| maxVUs | integer | Maximum number of VUs to allow during the test run. | If unset, same as `preAllocatedVUs` | + +## When to use + +If you need start iterations independent of system-under-test performance, and +want to ramp the number of iterations up or down during specific periods of time. + +{{< admonition type="note" >}} + +**Don't put sleep at the end of an iteration.** +The arrival-rate executors already pace the iteration rate through the `rate` and `timeUnit` properties. +It's unnecessary to use a `sleep()` function at the end of the VU code. + +{{< /admonition >}} + +## Example + +This is an example of a four-stage test. + +It starts at the defined `startRate`, 300 iterations per minute over a one minute period. +After one minute, the iteration rate ramps to 600 iterations started per minute over the next two minutes, and stays at this rate for four more minutes. +In the last two minutes, it ramps down to a target of 60 iterations per minute. + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + discardResponseBodies: true, + + scenarios: { + contacts: { + executor: 'ramping-arrival-rate', + + // Start iterations per `timeUnit` + startRate: 300, + + // Start `startRate` iterations per minute + timeUnit: '1m', + + // Pre-allocate necessary VUs. + preAllocatedVUs: 50, + + stages: [ + // Start 300 iterations per `timeUnit` for the first minute. + { target: 300, duration: '1m' }, + + // Linearly ramp-up to starting 600 iterations per `timeUnit` over the following two minutes. + { target: 600, duration: '2m' }, + + // Continue starting 600 iterations per `timeUnit` for the following four minutes. + { target: 600, duration: '4m' }, + + // Linearly ramp-down to starting 60 iterations per `timeUnit` over the last two minutes. + { target: 60, duration: '2m' }, + ], + }, + }, +}; + +export default function () { + http.get('https://test.k6.io/contacts.php'); +} +``` + +{{< /code >}} + +## Observations + +The following graph depicts the performance of the [example](#example) script: + +![Ramping Arrival Rate](/media/docs/k6-oss/ramping-arrival-rate.png) + +Based upon our test scenario inputs and results: + +- The configuration defines 4 stages for a total test duration of 9 minutes. +- Stage 1 maintains the `startRate` iteration rate at 300 iterations started per minute for 1 minute. +- Stage 2 ramps _up_ the iteration rate linearly from the _stage 1_ of 300 iterations started per minute, to the target of 600 iterations started per minute over a 2-minute duration. +- Stage 3 maintains the _stage 2_ iteration rate at 600 iterations started per minute over a 4-minute duration. +- Stage 4 ramps _down_ the iteration rate linearly to the target rate of 60 iterations started per minute over the last two minutes duration. +- Changes to the iteration rate are performed by k6, adjusting the number of VUs +- The script waits for a period of time (defined by the `gracefulStop` option) for iterations to finish. It doesn't start new iterations during the `gracefulStop` period. +- Our example performed, 4020 iterations over the course of the test. + +## Get the stage index + +To get the current running stage index, use the `getCurrentStageIndex` helper function from the [k6-jslib-utils](https://grafana.com/docs/k6//javascript-api/jslib/utils) library. It returns a zero-based number equal to the position in the shortcut `stages` array or in the executor's `stages` array. + +```javascript +import { getCurrentStageIndex } from 'https://jslib.k6.io/k6-utils/1.3.0/index.js'; + +export const options = { + stages: [ + { target: 10, duration: '30s' }, + { target: 50, duration: '1m' }, + { target: 10, duration: '30s' }, + ], +}; + +export default function () { + if (getCurrentStageIndex() === 1) { + console.log('Running the second stage where the expected target is 50'); + } +} +``` + +Using this feature, it is possible to automatically tag using the current running stage. Check the [Tagging stages](https://grafana.com/docs/k6//using-k6/tags-and-groups#tagging-stages) section for more details. diff --git a/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/ramping-vus.md b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/ramping-vus.md new file mode 100644 index 000000000..1779c4968 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/ramping-vus.md @@ -0,0 +1,109 @@ +--- +title: 'Ramping VUs' +description: 'A variable number of VUs execute as many iterations as possible for a specified amount of time.' +weight: 04 +--- + +# Ramping VUs + +With the `ramping-vus` executor, a variable number of VUs executes as many iterations as possible for a specified amount of time. + +For a shortcut to this executor, use the [stages](https://grafana.com/docs/k6//using-k6/k6-options#stages) option. + +## Options + +Besides the [common configuration options](https://grafana.com/docs/k6//using-k6/scenarios#options), +this executor has the following options: + +| Option | Type | Description | Default | +| --------------------------- | ------- | ---------------------------------------------------------------------------------------------- | ------- | +| stages(required) | array | Array of objects that specify the target number of VUs to ramp up or down to. | `[]` | +| startVUs | integer | Number of VUs to run at test start. | `1` | +| gracefulRampDown | string | Time to wait for an already started iteration to finish before stopping it during a ramp down. | `"30s"` | + +## When to use + +This executor is a good fit if you need VUs to ramp up or down during specific periods +of time. + +## Example + +This example schedules a two-stage test, ramping up from 0 to 10 VUs over 20 seconds, then down +to 0 VUs over 10 seconds. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + discardResponseBodies: true, + scenarios: { + contacts: { + executor: 'ramping-vus', + startVUs: 0, + stages: [ + { duration: '20s', target: 10 }, + { duration: '10s', target: 0 }, + ], + gracefulRampDown: '0s', + }, + }, +}; + +export default function () { + http.get('https://test.k6.io/contacts.php'); + // Injecting sleep + // Sleep time is 500ms. Total iteration time is sleep + time to finish request. + sleep(0.5); +} +``` + +{{< /code >}} + +{{< admonition type="note" >}} + +With [`gracefulRampDown`](https://grafana.com/docs/k6//using-k6/scenarios/concepts/graceful-stop#the-gracefulrampdown) set to 0 seconds, some iterations might be +interrupted during the ramp down stage. + +{{< /admonition >}} + +## Observations + +The following graph depicts the performance of the [example](#example) script: + +![Ramping VUs](/media/docs/k6-oss/ramping-vus.png) + +Based upon our test scenario inputs and results: + +- The configuration defines 2 stages for a total test duration of 30 seconds; +- Stage 1 ramps _up_ VUs linearly from the `startVUs` of 0 to the target of 10 over a 20 second duration; +- From the 10 VUs at the end of stage 1, stage 2 then ramps _down_ VUs linearly to the target of 0 over a 10 second duration; +- Each _iteration_ of the `default` function is expected to be roughly 515ms, or ~2/s; +- As the number of VUs changes, the iteration rate directly correlates; each addition of a VU increases the rate by about 2 iters/s, whereas each subtraction of a VU reduces by about 2 iters/s; +- The example performed ~300 iterations over the course of the test. + +## Get the stage index + +To get the current running stage index, use the `getCurrentStageIndex` helper function from the [k6-jslib-utils](https://grafana.com/docs/k6//javascript-api/jslib/utils) library. It returns a zero-based number equal to the position in the shortcut `stages` array or in the executor's `stages` array. + +```javascript +import { getCurrentStageIndex } from 'https://jslib.k6.io/k6-utils/1.3.0/index.js'; + +export const options = { + stages: [ + { target: 10, duration: '30s' }, + { target: 50, duration: '1m' }, + { target: 10, duration: '30s' }, + ], +}; + +export default function () { + if (getCurrentStageIndex() === 1) { + console.log('Running the second stage where the expected target is 50'); + } +} +``` + +Using this feature, it is possible to automatically tag using the current running stage. Check the [Tagging stages](https://grafana.com/docs/k6//using-k6/tags-and-groups#tagging-stages) section for more details. diff --git a/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/shared-iterations.md b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/shared-iterations.md new file mode 100644 index 000000000..f90663e99 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/scenarios/executors/shared-iterations.md @@ -0,0 +1,89 @@ +--- +title: 'Shared iterations' +description: 'A fixed number of iterations are "shared" between a number of VUs, and the test ends once all iterations are executed.' +weight: 01 +--- + +# Shared iterations + +The `shared-iterations` executor shares iterations between the number of VUs. +The test ends once k6 executes all iterations. + +For a shortcut to this executor, use the [`vus`](https://grafana.com/docs/k6//using-k6/k6-options/reference#vus) and [`iterations`](https://grafana.com/docs/k6//using-k6/k6-options/reference#iterations) options. + +{{< admonition type="note" >}} + +Iterations **are not guaranteed to be evenly distributed** with this executor. +VU that executes faster will complete more iterations than slower VUs. + +To guarantee that every VU completes a specific, fixed number of iterations, [use the per-VU iterations executor](https://grafana.com/docs/k6//using-k6/scenarios/executors/per-vu-iterations). + +{{< /admonition >}} + +## Options + +Besides the [common configuration options](https://grafana.com/docs/k6//using-k6/scenarios#options), +this executor has the following options: + +| Option | Type | Description | Default | +| ----------- | ------- | ---------------------------------------------------------------------------------- | ------- | +| vus | integer | Number of VUs to run concurrently. | `1` | +| iterations | integer | Total number of script iterations to execute across all VUs. | `1` | +| maxDuration | string | Maximum scenario duration before it's forcibly stopped (excluding `gracefulStop`). | `"10m"` | + +## When to use + +This executor is suitable when you want a specific number of VUs to complete a fixed +number of total iterations, and the amount of iterations per VU is unimportant. +If the **time to complete** a number of test iterations is your concern, this executor should perform best. + +An example use case is for quick performance tests in the developement build cycle. +As developers make changes, they might run the test against the local code to test for performance regressions. +Thus the executor works well with a _shift-left_ policy, where emphasizes testing performance early in the development cycle, when the cost of a fix is lowest. + +## Example + +The following example schedules 200 total iterations shared by 10 VUs with a maximum test duration of 30 seconds. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + discardResponseBodies: true, + scenarios: { + contacts: { + executor: 'shared-iterations', + vus: 10, + iterations: 200, + maxDuration: '30s', + }, + }, +}; + +export default function () { + http.get('https://test.k6.io/contacts.php'); + // Injecting sleep + // Sleep time is 500ms. Total iteration time is sleep + time to finish request. + sleep(0.5); +} +``` + +{{< /code >}} + +## Observations + +The following graph depicts the performance of the [example](#example) script: + +![Shared Iterations](/media/docs/k6-oss/shared-iterations.png) + +Based upon our test scenario inputs and results: + +- Test is limited to a fixed number of 200 iterations of the `default` function; +- The number of VUs is fixed to 10, and are initialized before the test begins; +- Each _iteration_ of the `default` function is expected to be roughly 515ms, or ~2/s; +- Maximum throughput (highest efficiency) is therefore expected to be ~20 iters/s, `2 iters/s * 10 VUs`; +- The maximum throughput is maintained for a larger portion of the test; +- The distribution of iterations may be skewed: one VU may have performed 50 iterations, another only 10. diff --git a/docs/sources/k6/v0.55.x/using-k6/tags-and-groups.md b/docs/sources/k6/v0.55.x/using-k6/tags-and-groups.md new file mode 100644 index 000000000..77f3d96c5 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/tags-and-groups.md @@ -0,0 +1,341 @@ +--- +title: 'Tags and Groups' +description: 'k6 provides the Tags and Groups APIs to help you during the analysis and easily visualize, sort and +filter your test results.' +weight: 08 +--- + +# Tags and Groups + +A load test usually targets a service with different subsystems and resources. +This can make it hard to pinpoint the issues that are degrading performance. + +To help you visualize, sort, and filter your test results, k6 adds the following to your results. + +- _Tags_ categorize your checks, thresholds, custom metrics, and requests for in-depth filtering. +- _Groups_ apply tags to the script's functions. + +Besides these granular tags, you can also use options to set test-wide tags. +You can use these tags to compare results from multiple tests. + +In addition to filtering results, you can also [use tags to limit the operations that your thresholds analyze](https://grafana.com/docs/k6//using-k6/thresholds#thresholds-on-tags). + +## Tags + +Tags are a powerful way to categorize your k6 entities and filter test results. + +k6 provides two types of tags: + +- **System tags.** Tags that k6 automatically assigns. +- **User-defined tags.** Tags that you add when you write your script. + +### System tags + +Currently, k6 automatically creates the following tags by default: + +| Tag | Description | +| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `proto` | the name of the protocol used (e.g. `HTTP/1.1`) | +| `subproto` | the subprotocol name (used by websockets) | +| `status` | the HTTP status code (e.g. `200`, `404`, etc.) | +| `method` | the HTTP method name (e.g. `GET`, `POST`, etc.) or the RPC method name for gRPC | +| `url` | the HTTP request URL | +| `name` | the HTTP [request name](https://grafana.com/docs/k6//using-k6/http-requests#url-grouping) | +| `group` | the full [group](#groups) path, see the preceding explanation for details about its value | +| `check` | the [Check](https://grafana.com/docs/k6//using-k6/checks) name | +| `error` | a string with a non-HTTP error message (e.g. network or DNS error) | +| `error_code` | A number specifying an error types; a list of current error codes can be found at the [Error Codes](https://grafana.com/docs/k6//javascript-api/error-codes) page | +| `tls_version` | the [TLS](https://grafana.com/docs/k6//using-k6/protocols/ssl-tls) version | +| `scenario` | the name of the scenario where the metric was emitted | +| `service` | the RPC service name for gRPC | +| `expected_response` | `true` or `false` based on the [responseCallback](https://grafana.com/docs/k6//javascript-api/k6-http/set-response-callback); by default checks whether the status is 2xx or 3xx | + +To disable some of the preceding tags, use the [`systemTags` option](https://grafana.com/docs/k6//using-k6/k6-options/reference#system-tags). +Note that some data collectors, for example `cloud` runs, may require certain tags. + +The following system tags are optional. Enable them as needed: + +| Tag | Description | +| ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `vu` | the ID of the virtual user that executed the request | +| `iter` | the iteration number | +| `ip` | The IP address of the remote server | +| `ocsp_status` | the [Online Certificate Status Protocol (OCSP)](https://grafana.com/docs/k6//using-k6/protocols/ssl-tls/online-certificate-status-protocol--ocsp) HTTPS status | + +## User-defined tags + +Define your own tags to categorize k6 entities based on your test logic. +You can tag the following entities: + +- requests +- checks +- thresholds +- custom metrics + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { Trend } from 'k6/metrics'; +import { check } from 'k6'; + +const myTrend = new Trend('my_trend'); + +export default function () { + // Add tag to request metric data + const res = http.get('https://httpbin.test.k6.io/', { + tags: { + my_tag: "I'm a tag", + }, + }); + + // Add tag to check + check(res, { 'status is 200': (r) => r.status === 200 }, { my_tag: "I'm a tag" }); + + // Add tag to custom metric + myTrend.add(res.timings.connecting, { my_tag: "I'm a tag" }); +} +``` + +{{< /code >}} + +## Test-wide tags + +Besides attaching tags to requests, checks, and custom metrics, you can set test-wide tags across all metrics. +You can set these tags in two ways: + +- In the CLI, using one or more `--tag NAME=VALUE` flags + +- In the script itself: + + {{< code >}} + + ```javascript + export const options = { + tags: { + name: 'value', + }, + }; + ``` + + {{< /code >}} + +## Code-defined tags + +In the case, a user-defined tag with advanced logic for handling which tag to set is required then it's possible doing it by defining the tag from the code. + +To support advanced tagging workflows, it is also possible to directly set and get them from scripts' code. + +[k6/execution.vu.tags](https://grafana.com/docs/k6//javascript-api/k6-execution/#vu) object's properties can indeed be directly assigned new key/value pairs to define new tags dynamically. This can prove useful, as demonstrated in the following example, to track a container's group from nested groups, and aggregating nested group's sub-metrics. + +```javascript +import http from 'k6/http'; +import exec from 'k6/execution'; +import { group } from 'k6'; + +export const options = { + thresholds: { + 'http_reqs{container_group:main}': ['count==3'], + 'http_req_duration{container_group:main}': ['max<1000'], + }, +}; + +export default function () { + exec.vu.tags.containerGroup = 'main'; + + group('main', function () { + http.get('https://test.k6.io'); + group('sub', function () { + http.get('https://httpbin.test.k6.io/anything'); + }); + http.get('https://test-api.k6.io'); + }); + + delete exec.vu.tags.containerGroup; + + http.get('https://httpbin.test.k6.io/delay/3'); +} +``` + +Using the same API, you can also retrieve any already set user-defined or system-defined tag: + +```javascript +import exec from 'k6/execution'; + +export default function () { + const tag = exec.vu.tags['scenario']; + console.log(tag); // default +} +``` + +## Tagging stages + +Thanks to some helper functions in the [k6-jslib-utils](https://grafana.com/docs/k6//javascript-api/jslib/utils) project, if an executor supports the `stages` option, you can add tags with the current ongoing stage. +Similar to other tags tag, the tag is added to all samples collected during the iteration. + +One way to tag the executed operations is to invoke the `tagWithCurrentStageIndex` function for setting a `stage` tag for identifying the stage that has executed them: + +```javascript +import http from 'k6/http'; +import exec from 'k6/execution'; +import { tagWithCurrentStageIndex } from 'https://jslib.k6.io/k6-utils/1.3.0/index.js'; + +export const options = { + stages: [ + { target: 5, duration: '5s' }, + { target: 10, duration: '10s' }, + ], +}; + +export default function () { + tagWithCurrentStageIndex(); + + // all the requests will have a `stage` tag + // with its value equal to the index of the stage + http.get('https://test.k6.io'); // e.g. {stage: "1"} +} +``` + +Additionally, a profiling function `tagWithCurrentStageProfile` can add a tag with a computed profile of the current running stage: + +```javascript +import http from 'k6/http'; +import exec from 'k6/execution'; +import { tagWithCurrentStageProfile } from 'https://jslib.k6.io/k6-utils/1.3.0/index.js'; + +export const options = { + stages: [{ target: 10, duration: '10s' }], +}; + +export default function () { + tagWithCurrentStageProfile(); + + // all the requests are tagged with a `stage` tag + // with the index of the stage as value + http.get('https://test.k6.io'); // {stage_profile: ramp-up} +} +``` + +The profile value based on the current stage can be one of the following options: + +| Profile | Description | +| ----------- | ----------------------------------------------------------------------- | +| `ramp-up` | The current stage has a target greater than the previous stage's target | +| `steady` | The current stage has a target equal to the previous stage's target | +| `ramp-down` | The current stage has a target less than the previous stage's target | + +## Tags in results output + +{{< code >}} + +```json +{ + "type ": "Point ", + "data ": { + "time ": "2017-05-09T14:34:45.239531499+02:00 ", + "value ": 459.865729, + "tags ": { + "group ": "::my group::json ", + "method ": "GET ", + "status ": "200 ", + "url ": "https://httpbin.test.k6.io/get " + } + }, + "metric ": "http_req_duration " +} +``` + +```json +{ + "type ": "Point ", + "data ": { + "time ": "2017-05-09T14:34:45.625742514+02:00 ", + "value ": 5, + "tags ": null + }, + "metric ": "vus " +} +``` + +{{< /code >}} + +To see how tags affect your test-result output, refer to the [k6 results output syntax](https://grafana.com/docs/k6//results-output/real-time/json). + +## Groups + +For extra organization, use _groups_ to organize a load script by functions. +You can also nest groups for BDD-style testing. + +All metrics emitted in a [group](https://grafana.com/docs/k6//javascript-api/k6/group) have the tag `group` with a value of all wrapping group names separated by `::` (two colons). +The root group uses the name '' (empty string). +If you have a single group named `cool requests`, the actual value of the `group` is `::cool requests`. + +For example, you could use groups to organize multiple requests by page loads or user actions. + +{{< code >}} + +```javascript +import { group } from 'k6'; + +export default function () { + group('visit product listing page', function () { + // ... + }); + group('add several products to the shopping cart', function () { + // ... + }); + group('visit login page', function () { + // ... + }); + group('authenticate', function () { + // ... + }); + group('checkout process', function () { + // ... + }); +} +``` + +{{< /code >}} + +Groups do the following tasks internally: + +- For each `group()` function, k6 emits a [group_duration metric](https://grafana.com/docs/k6//using-k6/metrics), which contains the total time to execute the group function. + +- When a taggable resource—a check, request, or custom metric—runs within a group, k6 sets the tag `group` with the current group name. + For more info, refer to the [Tags section](https://grafana.com/docs/k6//using-k6/tags-and-groups#tags). + +Both options, the `group_duration` metric and `group tagging`, could help you analyze and visualize complex test results. Check out how they work in your [k6 result output](https://grafana.com/docs/k6//misc/integrations#result-store-and-visualization). + +### Discouraged: one group per request + +Wrapping each request within a group might add unnecessary boilerplate. + +{{< code >}} + +```javascript +import { group, check } from 'k6'; +import http from 'k6/http'; + +const id = 5; + +// reconsider this type of code +group('get post', function () { + http.get(`http://example.com/posts/${id}`); +}); +group('list posts', function () { + const res = http.get(`http://example.com/posts`); + check(res, { + 'is status 200': (r) => r.status === 200, + }); +}); +``` + +{{< /code >}} + +If your code looks like the preceding snippet, consider the following strategies to write cleaner code: + +- For dynamic URLs, use the [URL grouping feature](https://grafana.com/docs/k6//using-k6/http-requests#url-grouping). +- To provide a meaningful name to your request, set the value of [tags.name](https://grafana.com/docs/k6//using-k6/http-requests#http-request-tags). +- To reuse common logic or organize your code better, group logic in functions, or create a [local JavaScript module](https://grafana.com/docs/k6//using-k6/modules#local-filesystem-modules) and import it into the test script. +- To model advanced user patterns, check out [Scenarios](https://grafana.com/docs/k6//using-k6/scenarios). diff --git a/docs/sources/k6/v0.55.x/using-k6/test-authoring/_index.md b/docs/sources/k6/v0.55.x/using-k6/test-authoring/_index.md new file mode 100644 index 000000000..43d554462 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/test-authoring/_index.md @@ -0,0 +1,10 @@ +--- +title: Test authoring +weight: 14 +--- + +# Test authoring + + + +{{< section >}} diff --git a/docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/_index.md b/docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/_index.md new file mode 100644 index 000000000..58f7d95e2 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/_index.md @@ -0,0 +1,66 @@ +--- +title: 'Create tests from recordings' +description: 'In load testing, recording usually refers to the process of creating a load test from the recording of a user session.' +weight: 02 +--- + +# Create tests from recordings + +A recording stores the sequence of requests and parameters of a user session or API interaction. +You can use this recording to auto-generate your test logic. + +Testers commonly use recordings to avoid writing complex tests from scratch. +For example, testing advanced scenarios on websites or mobile applications, such as end-to-end (E2E) tests with dozens or hundreds of requests. + +k6 provides three tools that can directly convert a recording into k6 script: + +- [Browser recorder](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings/using-the-browser-recorder) generates a k6 script from a browser session. +- [DevTools recorder](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings/using-the-devtools-recorder) generates a k6 Browser script from user flows recorded in Chrome DevTools. +- [HAR converter](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings/using-the-har-converter) generates a k6 script from the requests included in a HAR file. + +## Steps + +The steps to create a load test from a recording are as follows: + +1. Record a user or API session. +2. Convert the recorded session into a test. +3. Set up the load and test options. +4. Handle [correlation and dynamic data](https://grafana.com/docs/k6//examples/correlation-and-dynamic-data). + +You can then debug or run the load test. + +## Be sure to record realistically + +If you use a browser to simulate a user session and generate its recording, consider the following dos and don'ts. + +It's a good idea to: + +- Browse as a user would. +- Take natural pauses that users would take to consume page content. +- Focus on the most common use cases, rather than all the possible use cases. +- Take note of pages where forms/logins occur. You will need to edit the script to [handle correlation](https://grafana.com/docs/k6//examples/correlation-and-dynamic-data). + +You probably _do not_ want to: + +- Visit every page in one journey. +- Click every possible option. +- Navigate as fast as you can. +- Navigate out of your actual site or application. + +## Consider hybrid approach for load testing websites + +When you start the recording and navigate as a user, the recorder captures every HTTP(s) request loaded into the browser as you click. This includes all the requests for third-party services, ads, images, documents, etc. + +When you finish the recording, the converter generates the k6 script from all the recorded requests and assets. +The script could include **dozens or hundreds of requests for each page visit or interaction**. + +These types of recorded tests are difficult to maintain. As the website changes, these tests must be updated to reflect assets and API changes. + +An alternative approach to load test websites is to run a [hybrid load test](https://grafana.com/docs/k6//testing-guides/load-testing-websites/#hybrid-load-testing) which: + +- Runs a [browser test](https://grafana.com/docs/k6//using-k6-browser/running-browser-tests) to validate the frontend. +- While simultaneously running API tests to inject load to the backend. + +As the browser test automatically handles website assets, these tests require fewer updates. + +To learn more about this approach, check out the [example mixing browser-level and protocol-level tests](https://grafana.com/docs/k6//using-k6-browser/running-browser-tests#run-both-browser-level-and-protocol-level-tests-in-a-single-script). diff --git a/docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/using-the-browser-recorder.md b/docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/using-the-browser-recorder.md new file mode 100644 index 000000000..1198adb52 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/using-the-browser-recorder.md @@ -0,0 +1,48 @@ +--- +title: 'Using the browser recorder' +description: 'The browser recorder allows generating a k6 script based on a web session. It is available as extensions for Chrome and Firefox.' +weight: 01 +--- + +# Using the browser recorder + +The browser recorder lets you generate a k6 script based on a browser session. +It's available as an extension for [Chrome](https://chrome.google.com/webstore/detail/grafana-k6-browser-record/fbanjfonbcedhifbgikmjelkkckhhidl) and [Firefox](https://addons.mozilla.org/en-US/firefox/addon/grafana-k6-browser-recorder/). + +## Before you start + +Before you start, consider the following: + +- [Be sure to record realistically](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings#be-sure-to-record-realistically) +- [A hybrid approach for load testing websites](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings#consider-hybrid-approach-for-load-testing-websites) + +{{< admonition type="note" >}} + +Note that the browser recorders **don't require a cloud account**. For cloud users, refer to the [Grafana Cloud](https://grafana.com/docs/grafana-cloud/testing/k6/author-run/browser-recorder/) instructions. + +{{< /admonition >}} + +## How to record + +1. Install the [Chrome](https://chrome.google.com/webstore/detail/grafana-k6-browser-record/fbanjfonbcedhifbgikmjelkkckhhidl) or [Firefox](https://addons.mozilla.org/en-US/firefox/addon/grafana-k6-browser-recorder/) extension. +1. Open the extension by clicking the k6 logo. +1. Choose where to save the auto-generated script. + - To save it on your local machine, select **I don't want to save tests in the cloud**. + - To save it on any of your Grafana Cloud k6 projects, select **Sign In**. +1. Select **Start recording** to begin recording the current browser tab. +1. When done, select **Stop recording**. +1. Save the recorded script locally or in any of your cloud projects. +1. Edit your script as necessary. Depending on the [type of load test](https://grafana.com/docs/k6//testing-guides/test-types/), you might need to change different aspects of the script. + Typical changes are for [load options](https://grafana.com/docs/k6//using-k6/k6-options) and to handle [correlation and dynamic data](https://grafana.com/docs/k6//examples/correlation-and-dynamic-data). +1. Run the test from the CLI or Grafana Cloud k6. For more about running k6, refer to the [Running k6 guide](https://grafana.com/docs/k6//get-started/running-k6). + +## Troubleshooting. Try the HAR converter + +If you experience problems recording a request, try the [HAR converter](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings/using-the-har-converter). + +The Browser recorder uses the HAR converter under the hood. +Like the Browser Recorder, the HAR converter creates a k6 script from the HTTP requests included in a HAR file. + +The HAR converter can also record other tabs or pop-up windows, which the Browser Recorder cannot. + +If the error persists with the HAR converter, please provide detailed information about the problem [in a new issue](https://github.com/k6io/har-to-k6/issues). diff --git a/docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/using-the-devtools-recorder.md b/docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/using-the-devtools-recorder.md new file mode 100644 index 000000000..23d225f77 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/using-the-devtools-recorder.md @@ -0,0 +1,31 @@ +--- +title: 'Using the k6 DevTools recorder' +description: 'The k6 DevTools recorder allows you to export k6 browser scripts from the recorder panel in Chrome DevTools.' +weight: 01 +--- + +# Using the k6 DevTools recorder + +The k6 DevTools recorder lets you record user journeys using Chrome DevTools and then export them as a k6 script. + +## Before you begin + +To ensure you have a useful test output: + +- Review the [Be sure to record realistically](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings/#be-sure-to-record-realistically) and [Consider hybrid approach for load testing websites](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings/#consider-hybrid-approach-for-load-testing-websites) sections on [Create tests from recordings](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings/). +- Learn the basics of k6 browser tests with [Running browser tests](https://grafana.com/docs/k6//using-k6-browser/running-browser-tests/). + +{{< admonition type="note" >}} + +Using the DevTools recorder _doesn't require a Grafana Cloud account_. + +{{< /admonition >}} + +## Create a script from a recording + +1. Install the [k6 DevTools Recorder Chrome extension](https://chromewebstore.google.com/detail/k6-devtools-recorder/fkajbajcclbdgaoanencnhpfnigfipgc). +1. Record a session using the [Chrome DevTools Recorder](https://developer.chrome.com/docs/devtools/recorder). +1. [Export the user flow](https://developer.chrome.com/docs/devtools/recorder/reference#export-flows) and choose the k6 Browser option. +1. Edit your script as necessary. Depending on the [type of test](https://grafana.com/docs/k6//testing-guides/test-types/), you might need to change different aspects of the script. + Typical changes are for [load options](https://grafana.com/docs/k6//using-k6/k6-options) and to handle [correlation and dynamic data](https://grafana.com/docs/k6//examples/correlation-and-dynamic-data). +1. Run the test from the CLI or Grafana Cloud k6. Refer to [Running k6](https://grafana.com/docs/k6//get-started/running-k6) for more details. diff --git a/docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/using-the-har-converter.md b/docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/using-the-har-converter.md new file mode 100644 index 000000000..fb52f6fd6 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/test-authoring/create-tests-from-recordings/using-the-har-converter.md @@ -0,0 +1,163 @@ +--- +title: 'Using the HAR converter' +description: 'The HAR converter is an alternative to the Browser recorder. It generates a k6 script based on the HTTP requests included on a HAR file.' +weight: 02 +--- + +# Using the HAR converter + +The [har-to-k6 converter](https://github.com/k6io/har-to-k6) is a NodeJS tool that generates a k6 script based on the HTTP requests included in a [HAR file](). +It is an alternative to the [Browser recorder](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings/using-the-browser-recorder). + +{{< admonition type="note" >}} + +HAR is a file format used by all major browsers and various other tools to export recorded HTTP requests. + +{{< /admonition >}} + +## Before you start + +Before you start, consider the following: + +- [Be sure to record realistically](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings#be-sure-to-record-realistically) +- [A hybrid approach for load testing websites](https://grafana.com/docs/k6//using-k6/test-authoring/create-tests-from-recordings#consider-hybrid-approach-for-load-testing-websites) + +You'll need to choose a tool to record your HAR file. +Multiple browsers and tools can export HTTP traffic in a HAR format. +A few popular ones are: + +- [Chrome](https://www.google.com/chrome/) +- [Firefox](https://www.mozilla.org/en-US/firefox/) +- [Microsoft Edge](https://www.microsoft.com/en-us/edge) +- [Charles recording proxy](http://www.charlesproxy.com/)(HTTP proxy/recorder) +- [Fiddler](http://www.telerik.com/fiddler) (HTTP proxy/recorder) + +## 1. Record a HAR file + +Here are the basic steps you need to record in Chrome: + +1. Open a new incognito window in Chrome. (This is optional, but it means you won't send things like cookies, which your browser might have saved). +1. Open up Chrome developer tools (press F12). +1. Select the **Network** tab. +1. Check that the recording button (round button) is activated (red color). +1. If you want to make a recording of several successive page loads, select the **Preserve log** checkbox. +1. Enter the URL of your site and start doing whatever you want your simulated load-test users to do. +1. When done, in Chrome developer tools, right-click the URLs and choose **Save as HAR with content**. + +![Save HAR for load testing](/media/docs/k6-oss/session_recorder_save_as_har.png) + +## 2. Convert with `har-to-k6` + +The [har-to-k6 converter](https://github.com/k6io/har-to-k6) is a NodeJS tool that can convert a HAR file (browser session) into a k6 script. + +1. Make sure that you have installed NodeJS (version >=11.0.0). +1. Install the converter. You can use `npm`: + + ```bash + $ npm install -g har-to-k6 + ``` + + For other installation options, check out the [har-to-k6 installation instructions](https://github.com/k6io/har-to-k6#installation). + +1. Generate a k6 script from a HAR file with the convert command: + + ```bash + $ har-to-k6 myfile.har -o loadtest.js + ``` + + This command auto-generates a k6 script for you. + It reads the HAR file (_myfile.har_) and converts it into a k6 test (_loadtest.js_). + +## 3. Modify the auto-generated k6 script + +In the previous step, the converter created a k6 script for testing. +Now, you should evaluate whether you have to change any part of the k6 script. + +Depending on your use case, you might need to: + +- Configure the load options +- Remove third-party content +- Correlate dynamic data + +### Configure the load options + +Now, k6 has auto-generated a "functional" test. +By default, this test runs with one virtual user and for one iteration. + +It's time for you to configure the load options of your performance tests. +k6 lets you configure this in several ways: + +- As CLI arguments while running the test: + + ```bash + k6 run --vus 10 --duration 30s loadtest.js + ``` + +- As options in the script file. + + ```javascript + export const options = { + vus: 10, + duration: '30s', + }; + ``` + +To learn more about how to configure the load options, read the [Adding more VUs guide](https://grafana.com/docs/k6//get-started/running-k6#adding-more-vus) and the [Options guide](https://grafana.com/docs/k6//using-k6/k6-options). + +### Remove third-party content + +If you are recording a user session of a website, by default, you'll record all the HTTP requests that your website uses. +This includes requests from the third-party tools that your site uses, +e.g. analytics tools, Facebook, Twitter, Support Widgets, CDNs, etc. + +You should remove these third party requests: + +- They will skew the percentiles of your performance results. +- You may be unable to affect the performance of the third-party service. +- The load test may violate the terms-of-service contract that you have with the provider. + +Your k6 script can skip third-party requests in a few ways: + +- Edit the auto-generated k6 script, removing the requests one-by-one +- Download a HAR file with only requests to the selected domains. + +In Chrome, you can use the DevTools Network Filter to select only particular domains. +The Filter input accepts a Regex to match multiple domains. + +```bash +/loadimpact.com|cloudfront.net/ +``` + +![Save HAR filter domain using regex](/media/docs/k6-oss/session_recorder_filter_domain.png) + +After filtering your selected domains, you can download the HAR file as described in the first step of this tutorial. +The HAR file will include only the requests to the selected domains. + +If you don't know all the domains to filter, it helps to use the query language of the Network Filter. +Just input `domain:` in the filter to see all the different domains recorded by the Network Panel. + +![Save HAR filter domain list](/media/docs/k6-oss/session_recorder_filter_domain_list.png) + +### Correlate dynamic data + +In load testing, _correlation_ is when you extract the value from the response of one request and reuse it in a subsequent request. +Often, the correlation would be for a token or ID that is needed to run a sequence of steps in a user journey. + +The recorded HAR file may include dynamic data used on your site - `IDs`, `CSRF tokens`, `VIEWSTATE`, `wpnonce`, and other `dynamic values` - that will be converted into the k6 script. + +To run your load test correctly, you may need to replace some recorded data with dynamic data that k6 gets from previous requests. +For example, tokens expire quickly, and they are one of the most common things that users will correlate from a recorded session. + +[Here](https://grafana.com/docs/k6//examples/correlation-and-dynamic-data) are a few examples using the k6 API to correlate dynamic data. + +## 4. Run the test + +Now, you can run your load test with k6. If you have not installed k6 yet, please, follow the [k6 installation instructions](https://grafana.com/docs/k6//set-up/installation). + +Execute the `k6 run` command to run your k6 script: + +```bash +$ k6 run loadtest.js +``` + +To learn about running k6, check out the [Running k6 tutorial](https://grafana.com/docs/k6//get-started/running-k6). diff --git a/docs/sources/k6/v0.55.x/using-k6/test-authoring/test-builder.md b/docs/sources/k6/v0.55.x/using-k6/test-authoring/test-builder.md new file mode 100644 index 000000000..3abefb399 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/test-authoring/test-builder.md @@ -0,0 +1,21 @@ +--- +title: 'Test builder' +description: 'Use a graphical interface to create a k6 test.' +weight: 01 +--- + +# Test builder + +The k6 Test Builder provides a graphical interface to generate a k6 test script based on your input. Then, you can copy the test script and [run the test from the CLI](https://grafana.com/docs/k6//get-started/running-k6). + +Though we strongly believe that scriptable, code-based tools will help you get the most out of your performance-testing efforts, a GUI-based tool like the test builder could help you: + +- Speed up the test creation. +- Learn the [k6 API](https://grafana.com/docs/k6//javascript-api) quickly. +- Collaborate with non-coders to build tests. + +The k6 Test Builder is **free to use** and is available in: + +- [Grafana Cloud k6](https://grafana.com/docs/grafana-cloud/testing/k6/author-run/test-builder/) + +![k6 Test Builder](/media/docs/k6-oss/grafana-cloud-k6-test-builder.png) diff --git a/docs/sources/k6/v0.55.x/using-k6/test-lifecycle.md b/docs/sources/k6/v0.55.x/using-k6/test-lifecycle.md new file mode 100644 index 000000000..46f612691 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/test-lifecycle.md @@ -0,0 +1,284 @@ +--- +title: 'Test lifecycle' +description: 'The four distinct lifecycle stages in a k6 test are "init", "setup", "VU", and "teardown".' +weight: 06 +--- + +# Test lifecycle + +In the lifecycle of a k6 test, +a script always runs through these stages in the same order: + +1. Code in the `init` context prepares the script, loading files, importing modules, and defining the test _lifecycle functions_. **Required**. +2. The `setup` function runs, setting up the test environment and generating data. _Optional._ +3. VU code runs in the `default` or scenario function, running for as long and as many times as the `options` define. **Required**. +4. The `teardown` function runs, postprocessing data and closing the test environment. _Optional._ + +{{< admonition type="note" >}} + +**Lifecycle functions** + +Except for init code, each stage occurs in a _lifecycle function_, +a function called in a specific sequence in the k6 runtime. + +{{< /admonition >}} + +{{< code >}} + +```javascript +// 1. init code + +export function setup() { + // 2. setup code +} + +export default function (data) { + // 3. VU code +} + +export function teardown(data) { + // 4. teardown code +} +``` + +{{< /code >}} + +## Overview of the lifecycle stages + +For examples and implementation details of each stage, refer to the subsequent sections. + +| Test stage | Purpose | Example | Called | +| --------------- | ------------------------------------------------------------- | --------------------------------------------------------------------------------------- | ------------------------------------------------------------- | +| **1. init** | Load local files, import modules, declare lifecycle functions | Open JSON file, Import module | Once per VU\* | +| **2. Setup** | Set up data for processing, share data among VUs | Call API to start test environment | Once | +| **3. VU code** | Run the test function, usually `default` | Make https requests, validate responses | Once per iteration, as many times as the test options require | +| **4. Teardown** | Process result of setup code, stop test environment | Validate that setup had a certain result, send webhook notifying that test has finished | Once \*\* | + +\* In cloud scripts, init code might be called more often. + +\*\* If the `Setup` function ends abnormally (e.g throws an error), the `teardown()` function isn't called. Consider adding logic to the `setup()` function to handle errors and ensure proper cleanup. + +## The init stage + +**The init stage is required**. +Before the test runs, k6 needs to initialize the test conditions. +To prepare the test, code in the `init` context runs once per VU. + +Some operations that might happen in `init` include the following: + +- Import modules +- Load files from the local file system +- Configure the test for all `options` +- Define lifecycle functions for the VU, `setup`, and `teardown` stages (and for custom or `handleSummary()` functions, too). + +**All code that is outside of a lifecycle function is code in the `init` context**. +Code in the `init` context _always executes first_. + +{{< code >}} + +```javascript +// init context: importing modules +import http from 'k6/http'; +import { Trend } from 'k6/metrics'; + +// init context: define k6 options +export const options = { + vus: 10, + duration: '30s', +}; + +// init context: global variables +const customTrend = new Trend('oneCustomMetric'); + +// init context: define custom function +function myCustomFunction() { + // ... +} +``` + +{{< /code >}} + +Separating the `init` stage from the VU stage removes irrelevant computation from VU code, which improves k6 performance and makes test results more reliable. +One limitation of `init` code is that it **cannot** make HTTP requests. +This limitation ensures that the `init` stage is reproducible across tests (the response from protocol requests is dynamic and unpredictable) + +## The VU stage + +Scripts must contain, at least, a _scenario function_ that defines the logic of the VUs. +The code inside this function is _VU code_. +Typically, VU code is inside the `default` function, but it can also be inside the function defined by a scenario (see subsequent section for an example). + +{{< code >}} + +```javascript +export default function () { + // do things here... +} +``` + +{{< /code >}} + +**VU code runs over and over through the test duration.** +VU code can make HTTP requests, emit metrics, and generally do everything you'd expect a load test to do. +The only exceptions are the jobs that happen in the `init` context. + +- VU code _does not_ load files from your local filesystem. +- VU code _does not_ import any other modules. + +Again, instead of VU code, init code does these jobs. + +### The default function life-cycle + +A VU executes the `default()` function from start to end in sequence. +Once the VU reaches the end of the function, it loops back to the start and executes the code all over. + +As part of this "restart" process, k6 resets the VU. +Cookies are cleared, and TCP connections might be torn down (depending on your test configuration options). + +## Setup and teardown stages + +Like `default`, `setup` and `teardown` functions must be exported functions. +But unlike the `default` function, k6 calls `setup` and `teardown` only once per test. + +- `setup` is called at the beginning of the test, after the init stage but before the VU stage. +- `teardown` is called at the end of a test, after the VU stage (`default` function). + +You can call the full k6 API in the setup and teardown stages, unlike the init stage. +For example, you can make HTTP requests: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export function setup() { + const res = http.get('https://httpbin.test.k6.io/get'); + return { data: res.json() }; +} + +export function teardown(data) { + console.log(JSON.stringify(data)); +} + +export default function (data) { + console.log(JSON.stringify(data)); +} +``` + +{{< /code >}} + +### Skip setup and teardown execution + +You can skip the execution of setup and teardown stages using the options `--no-setup` and +`--no-teardown`. + +{{< code >}} + +```bash +$ k6 run --no-setup --no-teardown ... +``` + +{{< /code >}} + +### Use data from setup in default and teardown + +Again, let's have a look at the basic structure of a k6 test: + +{{< code >}} + +```javascript +// 1. init code + +export function setup() { + // 2. setup code +} + +export default function (data) { + // 3. VU code +} + +export function teardown(data) { + // 4. teardown code +} +``` + +{{< /code >}} + +You might have noticed the function signatures of the `default()` and `teardown()` functions take an argument, referred to here as `data`. + +Here's an example of passing some data from the setup code to the VU and teardown stages: + +{{< code >}} + +```javascript +export function setup() { + return { v: 1 }; +} + +export default function (data) { + console.log(JSON.stringify(data)); +} + +export function teardown(data) { + if (data.v != 1) { + throw new Error('incorrect data: ' + JSON.stringify(data)); + } +} +``` + +{{< /code >}} + +For example, with the data returned by the `setup()` function, you can: + +- Give each VU access to an identical copy of the data +- Postprocess the data in `teardown` code + +However, there are some restrictions. + +- You can pass only data (i.e. JSON) between `setup` and the other stages. + You cannot pass functions. +- If the data returned by the `setup()` function is large, it will consume more memory. +- You cannot manipulate data in the `default()` function, then pass it to the `teardown()` function. + +It's best to think that each stage and each VU has access to a fresh "copy" of whatever data the `setup()` function returns. + +![Diagram showing data getting returned by setup, then used (separately) by default and teardown functions](/media/docs/k6-oss/lifecycle.png) + +It would be extremely complicated and computationally intensive to pass mutable data between all VUs and then to teardown, especially in distributed setups. +This would go against a core k6 goal: the same script should be executable in multiple modes. + +## Additional lifecycle functions + +k6 has a few additional ways to use lifecycle functions: + +- **`handleSummary()`**. If you want to make a custom summary, k6 calls one more lifecycle function at the very end of the test. + + For details, refer to [Custom summary](https://grafana.com/docs/k6//results-output/end-of-test/custom-summary). + +- **Scenario functions**. Instead of the `default` function, you can also run VU code in scenario functions. + + {{< code >}} + + ```javascript + import http from 'k6/http'; + import { sleep } from 'k6'; + + export const options = { + scenarios: { + my_web_test: { + // the function this scenario will execute + exec: 'webtest', + executor: 'constant-vus', + vus: 50, + duration: '1m', + }, + }, + }; + + export function webtest() { + http.get('https://test.k6.io/contacts.php'); + sleep(Math.random() * 2); + } + ``` + + {{< /code >}} diff --git a/docs/sources/k6/v0.55.x/using-k6/thresholds.md b/docs/sources/k6/v0.55.x/using-k6/thresholds.md new file mode 100644 index 000000000..909354670 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/thresholds.md @@ -0,0 +1,530 @@ +--- +title: 'Thresholds' +description: 'Thresholds are a pass/fail criteria used to specify the performance expectations of the system under test.' +weight: 04 +--- + +# Thresholds + +Thresholds are the pass/fail criteria that you define for your test metrics. +If the performance of the system under test (SUT) does not meet the conditions of your threshold, +**the test finishes with a failed status.** + +Often, testers use thresholds to codify their SLOs. +For example, you can create thresholds for any combination of the following expectations: + +- Less than 1% of requests return an error. +- 95% of requests have a response time below 200ms. +- 99% of requests have a response time below 400ms. +- A specific endpoint always responds within 300ms. +- Any conditions for a [custom metric](https://grafana.com/docs/k6//using-k6/metrics/create-custom-metrics). + +Thresholds are also essential for [load-testing automation](https://grafana.com/docs/k6//testing-guides/automated-performance-testing): + +1. Give your test a threshold. +1. Automate your execution +1. Set up alerts for test failures. + +After that, you need to worry about the test only after your SUT fails to meet its performance expectations. + +## Thresholds example for HTTP errors and response duration + +This sample script specifies two thresholds. +One threshold evaluates the rate of HTTP errors (`http_req_failed` metric). +The other evaluates whether 95 percent of responses happen within a certain duration (the `http_req_duration` metric). + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + thresholds: { + http_req_failed: ['rate<0.01'], // http errors should be less than 1% + http_req_duration: ['p(95)<200'], // 95% of requests should be below 200ms + }, +}; + +export default function () { + http.get('https://test-api.k6.io/public/crocodiles/1/'); +} +``` + +{{< /code >}} + +In other words, when you define your threshold, specify an expression for a `pass` criteria. +If that expression evaluates to `false` at the end of the test, k6 considers the whole test a `fail`. + +After executing that script, k6 outputs something similar to this: + +{{< code >}} + +```bash + ✓ http_req_duration..............: avg=151.06ms min=151.06ms med=151.06ms max=151.06ms p(90)=151.06ms p(95)=151.06ms + { expected_response:true }...: avg=151.06ms min=151.06ms med=151.06ms max=151.06ms p(90)=151.06ms p(95)=151.06ms + ✓ http_req_failed................: 0.00% ✓ 0 ✗ 1 +``` + +{{< /code >}} + +In this case, the test met the criteria for both thresholds. +k6 considers this test a `pass` and exits with an exit code `0`. + +If any of the thresholds had failed, the little green checkmark next to the threshold name +(`http_req_failed`, `http_req_duration`) would be a red cross +and k6 would exit with a non-zero exit code. + +## Threshold Syntax + +To use a threshold, follow these steps: + +1. In the `thresholds` property of the `options` object, set a key using the name of the metric you want the threshold for: + + ```javascript + export const options = { + thresholds: { + /* ... */ + }, + }; + ``` + +2. Define at least one threshold expression. You can do this in two ways: + + - The short format puts all threshold expressions as strings in an array. + - The long format puts each threshold in an object, with extra properties to [abort on failure](#abort-a-test-when-a-threshold-is-crossed). + + ```javascript + export const options = { + thresholds: { + //short format + METRIC_NAME1: ['THRESHOLD_EXPRESSION', `...`], + //long format + METRIC_NAME2: [ + { + threshold: 'THRESHOLD_EXPRESSION', + abortOnFail: true, // boolean + delayAbortEval: '10s', // string + }, + ], // full format + }, + }; + ``` + + Note that `METRIC_NAME1` and `THRESHOLD_EXPRESSION` are placeholders. + The real text must be the name of the metric and the threshold expression. + +This declaration configures thresholds for the metrics `metric_name1` and `metric_name2`. +To determine whether the threshold passes or fails, the script evaluates the `'threshold_expression'.` + +### Threshold expression syntax + +A threshold expression evaluates to `true` or `false`. +The threshold expression must be in the following format: + +``` + +``` + +Some examples of threshold expressions are as follows: + +- `avg < 200` // average duration must be less than 200ms +- `count >= 500` // count must be larger than or equal to 500 +- `p(90) < 300` // 90% of samples must be below 300 + +### Aggregation methods by type + +k6 aggregates metrics according to [their type](https://grafana.com/docs/k6//using-k6/metrics). +These aggregation methods form part of the threshold expressions. + +| Metric type | Aggregation methods | +| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| Counter | `count` and `rate` | +| Gauge | `value` | +| Rate | `rate` | +| Trend | `avg`, `min`, `max`, `med` and `p(N)` where `N` specifies the threshold percentile value, expressed as a number between 0.0 and 100. E.g. `p(99.99)` means the 99.99th percentile. The values are in milliseconds. | + +This (slightly contrived) sample script uses all different types of metrics, +setting different types of thresholds for each: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { Trend, Rate, Counter, Gauge } from 'k6/metrics'; +import { sleep } from 'k6'; + +export const TrendRTT = new Trend('RTT'); +export const RateContentOK = new Rate('Content OK'); +export const GaugeContentSize = new Gauge('ContentSize'); +export const CounterErrors = new Counter('Errors'); +export const options = { + thresholds: { + // Count: Incorrect content cannot be returned more than 99 times. + 'Errors': ['count<100'], + // Gauge: returned content must be smaller than 4000 bytes + 'ContentSize': ['value<4000'], + // Rate: content must be OK more than 95 times + 'Content OK': ['rate>0.95'], + // Trend: Percentiles, averages, medians, and minimums + // must be within specified milliseconds. + 'RTT': ['p(99)<300', 'p(70)<250', 'avg<200', 'med<150', 'min<100'], + }, +}; + +export default function () { + const res = http.get('https://test-api.k6.io/public/crocodiles/1/'); + const contentOK = res.json('name') === 'Bert'; + + TrendRTT.add(res.timings.duration); + RateContentOK.add(contentOK); + GaugeContentSize.add(res.body.length); + CounterErrors.add(!contentOK); + + sleep(1); +} +``` + +{{< /code >}} + +{{< admonition type="caution" >}} + +Do not specify multiple thresholds for the same metric by repeating the same object key. + +{{< /admonition >}} + +Since thresholds are defined as the properties of a JavaScript object, you can't specify multiple ones with the same property name. + +{{< code >}} + +```javascript +export const options = { + thresholds: { + // don't use the same metric more than once here + metric_name: ['count<100'], + metric_name: ['rate<50'], + }, +}; +``` + +{{< /code >}} + +The rest will be **silently** ignored. +If you want to set multiple thresholds for a metric, specify them with an [array for the same key](https://grafana.com/docs/k6//using-k6/thresholds#multiple-thresholds-on-a-single-metric). + +## Threshold examples to copy and paste + +The quickest way to start with thresholds is to use the [built-in metrics](https://grafana.com/docs/k6//using-k6/metrics/reference). +Here are a few copy-paste examples that you can start using right away. + +For more specific threshold examples, refer to the [Counter](https://grafana.com/docs/k6//javascript-api/k6-metrics/counter#counter-usage-in-thresholds), [Gauge](https://grafana.com/docs/k6//javascript-api/k6-metrics/gauge#gauge-usage-in-thresholds), [Trend](https://grafana.com/docs/k6//javascript-api/k6-metrics/trend#trend-usage-in-thresholds) and [Rate](https://grafana.com/docs/k6//javascript-api/k6-metrics/rate#rate-usage-in-thresholds) pages. + +### A percentile of requests finishes in a specified duration + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + thresholds: { + // 90% of requests must finish within 400ms. + http_req_duration: ['p(90) < 400'], + }, +}; + +export default function () { + http.get('https://test-api.k6.io/public/crocodiles/1/'); + sleep(1); +} +``` + +{{< /code >}} + +### Error rate is lower than 1 percent + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + thresholds: { + // During the whole test execution, the error rate must be lower than 1%. + http_req_failed: ['rate<0.01'], + }, +}; + +export default function () { + http.get('https://test-api.k6.io/public/crocodiles/1/'); + sleep(1); +} +``` + +{{< /code >}} + +### Multiple thresholds on a single metric + +You can also apply multiple thresholds for one metric. +This threshold has different duration requirements for different request percentiles. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + thresholds: { + // 90% of requests must finish within 400ms, 95% within 800, and 99.9% within 2s. + http_req_duration: ['p(90) < 400', 'p(95) < 800', 'p(99.9) < 2000'], + }, +}; + +export default function () { + const res1 = http.get('https://test-api.k6.io/public/crocodiles/1/'); + sleep(1); +} +``` + +{{< /code >}} + +### Threshold on group duration + +You can set thresholds per [Group](https://grafana.com/docs/k6//using-k6/tags-and-groups#groups). +This code has groups for individual requests and batch requests. +For each group, there are different thresholds. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { group, sleep } from 'k6'; + +export const options = { + thresholds: { + 'group_duration{group:::individualRequests}': ['avg < 400'], + 'group_duration{group:::batchRequests}': ['avg < 200'], + }, + vus: 1, + duration: '10s', +}; + +export default function () { + group('individualRequests', function () { + http.get('https://test-api.k6.io/public/crocodiles/1/'); + http.get('https://test-api.k6.io/public/crocodiles/2/'); + http.get('https://test-api.k6.io/public/crocodiles/3/'); + }); + + group('batchRequests', function () { + http.batch([ + ['GET', `https://test-api.k6.io/public/crocodiles/1/`], + ['GET', `https://test-api.k6.io/public/crocodiles/2/`], + ['GET', `https://test-api.k6.io/public/crocodiles/3/`], + ]); + }); + + sleep(1); +} +``` + +{{< /code >}} + +## Set thresholds for specific tags + +It's often useful to specify thresholds on a single URL or specific tag. +In k6, tagged requests create sub-metrics that you can use in thresholds: + +```javascript +export const options = { + thresholds: { + 'metric_name{tag_name:tag_value}': ['threshold_expression'], + }, +}; +``` + +And here's a full example. + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; +import { Rate } from 'k6/metrics'; + +export const options = { + thresholds: { + 'http_req_duration{type:API}': ['p(95)<500'], // threshold on API requests only + 'http_req_duration{type:staticContent}': ['p(95)<200'], // threshold on static content only + }, +}; + +export default function () { + const res1 = http.get('https://test-api.k6.io/public/crocodiles/1/', { + tags: { type: 'API' }, + }); + const res2 = http.get('https://test-api.k6.io/public/crocodiles/2/', { + tags: { type: 'API' }, + }); + + const responses = http.batch([ + ['GET', 'https://test-api.k6.io/static/favicon.ico', null, { tags: { type: 'staticContent' } }], + [ + 'GET', + 'https://test-api.k6.io/static/css/site.css', + null, + { tags: { type: 'staticContent' } }, + ], + ]); + + sleep(1); +} +``` + +{{< /code >}} + +## Abort a test when a threshold is crossed + +If you want to abort a test as soon as a threshold is crossed, +set the `abortOnFail` property to `true`. +When you set `abortOnFail`, the test run stops _as soon as the threshold fails_. + +Sometimes, though, a test might fail a threshold early and abort before the test generates significant data. +To prevent these cases, you can delay `abortOnFail` with `delayAbortEval`. +In this script, `abortOnFail` is delayed ten seconds. +After ten seconds, the test aborts if it fails the `p(99) < 10` threshold. + +{{< code >}} + +```javascript +export const options = { + thresholds: { + metric_name: [ + { + threshold: 'p(99) < 10', // string + abortOnFail: true, // boolean + delayAbortEval: '10s', // string + /*...*/ + }, + ], + }, +}; +``` + +{{< /code >}} + +The fields are as follows: + +| Name | Type | Description | +| -------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| threshold | string | This is the threshold expression string specifying the threshold condition to evaluate. | +| abortOnFail | boolean | Whether to abort the test if the threshold is evaluated to false before the test has completed. | +| delayAbortEval | string | If you want to delay the evaluation of the threshold to let some metric samples to be collected, you can specify the amount of time to delay using relative time strings like `10s`, `1m` and so on. | + +Here is an example: + +{{< code >}} + +```javascript +import http from 'k6/http'; + +export const options = { + vus: 30, + duration: '2m', + thresholds: { + http_req_duration: [{ threshold: 'p(99) < 10', abortOnFail: true }], + }, +}; + +export default function () { + http.get('https://test-api.k6.io/public/crocodiles/1/'); +} +``` + +{{< /code >}} + +{{< admonition type="caution" >}} + +When k6 runs in the cloud, thresholds are evaluated every 60 seconds. +Therefore, the `abortOnFail` feature may be delayed by up to 60 seconds. + +{{< /admonition >}} + +## Fail a load test using checks + +[Checks](https://grafana.com/docs/k6//using-k6/checks) are nice for codifying assertions, but unlike `thresholds`, `checks` do not affect the exit status of k6. + +If you use only `checks` to verify that things work as expected, you can't fail the whole test run based on the `check` results. + +It's often useful to combine `checks` and `thresholds`, to get the best of both: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check, sleep } from 'k6'; + +export const options = { + vus: 50, + duration: '10s', + thresholds: { + // the rate of successful checks should be higher than 90% + checks: ['rate>0.9'], + }, +}; + +export default function () { + const res = http.get('https://httpbin.test.k6.io'); + + check(res, { + 'status is 500': (r) => r.status == 500, + }); + + sleep(1); +} +``` + +{{< /code >}} + +In this example, the `threshold` is configured on the [checks metric](https://grafana.com/docs/k6//using-k6/metrics/reference), establishing that the rate of successful checks is higher than 90%. + +Additionally, you can use `tags` on checks if you want to define a threshold based on a particular check or group of checks. For example: + +{{< code >}} + +```javascript +import http from 'k6/http'; +import { check, sleep } from 'k6'; + +export const options = { + vus: 50, + duration: '10s', + thresholds: { + 'checks{myTag:hola}': ['rate>0.9'], + }, +}; + +export default function () { + let res; + + res = http.get('https://httpbin.test.k6.io'); + check(res, { + 'status is 500': (r) => r.status == 500, + }); + + res = http.get('https://httpbin.test.k6.io'); + check( + res, + { + 'status is 200': (r) => r.status == 200, + }, + { myTag: 'hola' } + ); + + sleep(1); +} +``` + +{{< /code >}} diff --git a/docs/sources/k6/v0.55.x/using-k6/workaround-iteration-duration.md b/docs/sources/k6/v0.55.x/using-k6/workaround-iteration-duration.md new file mode 100644 index 000000000..afcdec629 --- /dev/null +++ b/docs/sources/k6/v0.55.x/using-k6/workaround-iteration-duration.md @@ -0,0 +1,69 @@ +--- +title: Workaround to calculate iteration_duration +description: 'A threshold can calculate the value of a metric excluding the results of the setup and teardown functions' +_build: + list: false +weight: 20 +--- + +# Workaround to calculate iteration_duration + +A common requested case is to track the `iteration_duration` metric without including time spent for `setup` and `teardown` functions. +This feature is not yet available but a threshold on `iteration_duration` or any pre-existing metrics can be used as a workaround. + +It's based on the concept of creating thresholds for sub-metrics created by tags for the required scope and setting the criteria that always pass. It works with any enabled tags that already works with threshold, for example: + +- `iteration_duration{scenario:default}` generates a sub-metric collecting samples only for the default scenario's iteration. `scenario:default` is used because that's the internal k6 scenario name when we haven't specified `options. `scenarios` explicitly and are just using the execution shortcuts instead. +- `iteration_duration{group:::setup}` or `iteration_duration{group:::teardown}` create sub-metrics collecting the duration only for `setup` and `teardown`. `k6` implicitly creates [groups](https://grafana.com/docs/k6//using-k6/tags-and-groups#groups) for `setup` and `teardown`, and `::` is the group separator. +- `http_req_duration{scenario:default}` can be useful as well for isolating executed long-running requests. + +{{< code >}} + +```javascript +import { sleep } from 'k6'; +import http from 'k6/http'; + +export const options = { + vus: 1, + iterations: 1, + thresholds: { + 'iteration_duration{scenario:default}': [`max>=0`], + 'iteration_duration{group:::setup}': [`max>=0`], + 'iteration_duration{group:::teardown}': [`max>=0`], + 'http_req_duration{scenario:default}': [`max>=0`], + }, +}; + +export function setup() { + http.get('https://httpbin.test.k6.io/delay/5'); +} + +export default function () { + http.get('http://test.k6.io/?where=default'); + sleep(0.5); +} + +export function teardown() { + http.get('https://httpbin.test.k6.io/delay/3'); + sleep(5); +} +``` + +{{< /code >}} + +Dedicated sub-metrics have been generated collecting samples only for the scope defined by thresholds: + +{{< code >}} + +```bash +http_req_duration..............: avg=1.48s min=101.95ms med=148.4ms max=5.22s p(90)=4.21s p(95)=4.71s + { expected_response:true }...: avg=1.48s min=101.95ms med=148.4ms max=5.22s p(90)=4.21s p(95)=4.71s +✓ { scenario:default }.........: avg=148.4ms min=103.1ms med=148.4ms max=193.7ms p(90)=184.64ms p(95)=189.17ms + +iteration_duration.............: avg=5.51s min=1.61s med=6.13s max=8.81s p(90)=8.27s p(95)=8.54s +✓ { group:::setup }............: avg=6.13s min=6.13s med=6.13s max=6.13s p(90)=6.13s p(95)=6.13s +✓ { group:::teardown }.........: avg=8.81s min=8.81s med=8.81s max=8.81s p(90)=8.81s p(95)=8.81s +✓ { scenario:default }.........: avg=1.61s min=1.61s med=1.61s max=1.61s p(90)=1.61s p(95)=1.61s +``` + +{{< /code >}}