Does K6 Tool Support sending Requests using Following Browser Supported Javascript or AJAX APIs
a) Fetch API,
b) XMLHttpRequest API and
c) JQuery APIs
Hi @srikr !
Welcome to the community forums!
The answer to this question depends on the context in which k6 should execute these requests.
For instance, if you intend to make such calls using the k6’s script, the answer is probably no since k6 uses its own API for HTTP calls k6/http | Grafana k6 documentation
There is also ongoing work to introduce the new version of the API: k6/docs/design/018-new-http-api.md at master · grafana/k6 · GitHub. But currently, it’s just a plan.
But if you are asking that calls of such APIs continue to work for the k6-browser, then the answer is yes, they are supported, with the remark that in that case browser (Chrome) will make them.
Let me know if that helps.
Cheers!
Thanks for the Welcome @olegbespalov.
But if you are asking that calls of such APIs continue to work for the k6-browser, then the answer is yes, they are supported, with the remark that in that case browser (Chrome) will make them
Wanted to understand more on the comment. Are you saying we have to simulate fetch/xmlhttprequest/jquery calls using some browser to hit to k6 browser then later k6 browser or k6 Tool can leverage it and sent it across. Can you please share a example scripts which I can use to simulate fetch/xmlhttprequest/jquery calls at k6 from a Browsers like Chrome.
@srikr If you make a request with k6-browser, the way a user would use the application in a browser, k6-browser will automagically generate all those calls.
Try this example script: Using k6 browser | Grafana k6 documentation
You can observe what is being sent in the console (or intercept with a proxy).
ok @richmarshall I see so if I understand that statement correctly, Say I have a web page in my server which uses fetch, XMLHTTPRequest APIs. If I send GET request on to that web page(which has POST request being called by Fetch and XMLHTTPRequest APIs) that is lying in the web server, will those fetch and XMLHTTPRequest POST APIs be executed by the k6-browser like chrome/firefox browser or how can I do something similar with k6
@srikr Yes it runs all of those requests within the browser. You will also see the JS, CSS, image resources etc. Just try the browser module example:
try {
await page.goto('your URL');
...
...
Run the browser command as follows:
$ K6_BROWSER_HEADLESS=false K6_BROWSER_TIMEOUT='60s' k6 run script.js
Thanks @richmarshall I will test this and confirm by tommorow.
Hi @richmarshall,
I did try test using following javascript code:
import { browser } from 'k6/experimental/browser';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
}
export default async function() {
const page = browser.newPage();
try {
await page.goto('https://<server_ip>/fetch_test.html');
} finally {
page.close();
}
}
I see chrome browser opens up and immediately closes based on the capture I could figure out that it is because of certificate unknown
I tried following options nothing worked
a) Uploaded the Server Certificate CA in the Chrome
b) While running k6 command included these options nothing worked
(i) K6_BROWSER_HEADLESS=false K6_BROWSER_TIMEOUT=‘60s’ K6_INSECURE_SKIP_TLS_VERIFY=true k6 run http.js
(ii) K6_BROWSER_HEADLESS=false K6_BROWSER_TIMEOUT=‘60s’ k6 run --insecure-skip-tls-verify http.js
(iii) K6_BROWSER_HEADLESS=false K6_BROWSER_TIMEOUT=‘60s’ K6_INSECURE_SKIP_TLS_VERIFY=true k6 run --insecure-skip-tls-verify http.js
Do we have a way to ignore certificate verification in k6 browser or how should I fix the certificate unknown issue.
@srikr please refer to this other solution which I believe this is the syntax:
Thanks a lot @richmarshall that worked. As part of those changes I tried to increase vus and iterations in the java script and wanted the test to run for maximum 5 minutes but browser is crashing with this error:
browser \u2717 [======>----------] 10 VUs 2m47.7s/5m0s 3069/7000 iters, 700 per VU
ERRO[0168] error building browser on IterStart: launching browser: browser process shutdown unexpectedly before establishing a connection: read |0: file already closed at file:///home/aviuser/K6JS/http.js:17:15(0)
Below is the modified JS Script(http.js) and Headless Mode Command used to run this
import { browser } from 'k6/experimental/browser';
export const options = {
scenarios: {
browser: {
executor: 'per-vu-iterations',
vus: 10,
iterations: '700',
maxDuration: '300s',
options: {
browser: {
type: 'chromium',
},
},
},
},
}
export default async function() {
const page = browser.newPage();
try {
await page.goto('https://<server_ip>/fetch_test.html');
} finally {
page.close();
}
}
Command that is run:
K6_BROWSER_HEADLESS=true K6_BROWSER_TIMEOUT='300s' K6_BROWSER_ARGS="ignore-certificate-errors" k6 run http.js
Below is the question I have to increase based on the above observation:
1. How to increase the iterations and vus without browser crash - Do I need to increase CPU and RAM of the VM. I am currently running this test in VM having CPU(8 Core) and RAM(8GB)
2. How to continue the same tests even after browser Crashes by reopening the new browser
3. I am fine any solution w.r.t scale both headless and headfull Browser mode. All I need is I want to run the traffic test like 1 or 2 days and so on without stopping the test.
Note: The k6 Version and Ubuntu Version I am using is as follows:
k6 version
k6 v0.49.0 (commit/b5328aa782, go1.21.6, linux/amd64)
lsb_release -a
Distributor ID: Ubuntu
Description: Ubuntu 16.04.6 LTS
Release: 16.04
Codename: xenial
Hi @srikr that is good news. FYI that I am an end user also. I have been pleasantly surprised by the k6 browser scalability on a powerful load generator. So far I’m using a standalone AWS host, it is an m5a.4xlarge running Windows. It can reliably run 30 parallel headless browsers (using a 150 second ramp-up time) for 4 hours and stay near 80% average CPU. My AUT is an Angular SPA with multiple microservices on every page. A few of the API calls are chunky in response size but they run fairly quickly. I noticed the script allowed running more parallel browsers when I added steps. As of now I have a somewhat elaborate user journey scripted (around 10 pages) using a sleep between the steps (random 3-13 seconds). So once a browser is instantiated for a VU iteration, it remains open for a while, limiting the overhead of terminating the browser and starting a new one. The browser is incognito so there is also the heavier effect of a brand-new visitor. Try adding some pages or at least some wait time for realism.
I am also redirecting the log to a file instead of having it lost in the console, this likely reduces overhead. Some of these logs have been huge after 4 hours but I needed to search them for any errors. Here’s my command…
set "K6_BROWSER_HEADLESS=true" && set "K6_BROWSER_TIMEOUT=60s" && k6 run --log-output=file="<path>\<logfile>.log" <path>\<script>.js
For the scenario config I have this, possibly suboptimal but it has been working so far:
export const options = {
scenarios: {
<scenarioname>: {
executor: 'ramping-vus',
exec: '<execname>',
"startVUs": 0,
"stages": [
{ "target": 30, "duration": "150s" },
{ "target": 30, "duration": "14400s" },
{ "target": 0, "duration": "10s" }
],
"gracefulRampDown": "5s",
"gracefulStop": "5s",
options: {
browser: {
type: 'chromium',
},
},
},
},
thresholds: {
'browser_web_vital_lcp': ['max < 3000'],
'checks': ["rate==1.0"]
}
}
Thanks @richmarshall that configuration works. I will play with those configs and see what max stress I can put on to my Server using k6 browsers.
Hi @srikr you may want to check out the just-released version 50. From the release notes, it states:
Browser Context Isolation browser#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.
I installed it but have not run any heavy tests yet. Functionally it is working.
Thanks @richmarshall for the update. Sure for now I am in k6 version 49 it works fine… I will try to upgrade to version 50 ASAP