Skip to content

Instantly share code, notes, and snippets.

@imiric
Created June 3, 2022 15:08
Show Gist options
  • Save imiric/5272bfa134194db53b88a64d34e96890 to your computer and use it in GitHub Desktop.
Save imiric/5272bfa134194db53b88a64d34e96890 to your computer and use it in GitHub Desktop.
xk6-browser memory usage tests
import { sleep } from 'k6';
import launcher from 'k6/x/browser';
export default function() {
const browser = launcher.launch('chromium', {
headless: true,
timeout: '600s',
slowMo: '500ms',
});
const context = browser.newContext();
const page = context.newPage();
sleep(60);
page.close();
browser.close();
}
import { sleep } from 'k6';
import launcher from 'k6/x/browser';
function scroll(page) {
for (let i=0; i<20; i++) {
page.evaluate(() => window.scrollBy({top: 100, behavior: 'smooth'}));
sleep(1);
}
}
export default function() {
const browser = launcher.launch('chromium', {
headless: true,
timeout: '600s',
slowMo: '500ms',
});
const context = browser.newContext();
const page = context.newPage();
// Goto front page, find login link and click it
page.goto('https://k6.io/', { waitUntil: 'networkidle' });
scroll(page);
page.$('//section[starts-with(@class, "integrations-module")]//div[@class="row"]/div[4]/a[1]').click();
scroll(page);
page.close();
browser.close();
}
#!/bin/bash
# mem.sh prints the resident set size (actual physical memory usage) in KiB of
# the k6 process, and of all its children processes recursively, separated by
# comma.
# I.e. if a k6 binary compiled with xk6-browser is run with `-i 5 -u 5`, this
# script will output something like:
# 92060,612152,614236,601856,621348,613620
# where the first column is the k6 RSS, and all following columns are the RSS
# of each child process (e.g. chrome), including their child processes.
# NOTE: This doesn't take into account shared memory between processes, so the
# total RSS amount will be greater than actual physical memory used (as shown
# by `free`, etc.).
K6PID=$(pgrep -f 'k6 run')
if [ -z "$K6PID" ]; then
echo 0
exit 1
fi
# --forest is required to properly group child process memory
ps --no-headers -o pid,ppid,pgid,rss,size,share,comm -u $USER --forest | \
awk '$3 == "'${K6PID}'" # filter only the k6 PGID' | \
gawk 'BEGIN { idx = 0 }
{
if ( $1 == "'${K6PID}'" ) { rss[0] = $4 } # get the k6 rss
else {
# if we reached a child process, increment the index
if ( $2 == "'${K6PID}'" ) { idx += 1 }
# add rss of all child processes
rss[idx] += $4
}
} END {
# iterate over arrays by index in ascending order
PROCINFO["sorted_in"] = "@ind_num_asc"
for (i in rss) print rss[i]
}' | paste -sd,
#!/bin/bash
# mem2.sh prints the physical memory usage in KiB of the k6 process, and of all
# chrome processes _combined_, separated by comma.
# Unlike mem.sh, this script doesn't group memory usage per child process, but
# does print the correct physical memory usage including shared memory.
# I.e. if a k6 binary compiled with xk6-browser is run with `-i 5 -u 5`, this
# script will output something like:
# 92060,612152
# where the first column is the k6 memory usage, and the second column is the
# memory usage of all combined chrome processes.
#
# External dependencies:
# - https://www.selenic.com/smem/
K6PID=$(pgrep -f 'k6 run')
if [ -z "$K6PID" ]; then
echo 0
exit 1
fi
K6MEM=$(smem -H -U $USER -c 'command pss' -P 'k6 run' | grep -v smem | awk '{ print $NF }')
CHROMEMEM=$(smem -H -U $USER -c 'command pss' -P chrome | grep -v smem | awk '{ mem += $NF } END { print mem }')
echo "${K6MEM},${CHROMEMEM}"
import { check } from 'k6';
import launcher from 'k6/x/browser';
export default function() {
const browser = launcher.launch('chromium', {
headless: true,
timeout: '600s',
slowMo: '1s',
});
const context = browser.newContext();
const page = context.newPage();
// Goto front page, find login link and click it
page.goto('https://test.k6.io/', { waitUntil: 'networkidle' });
const elem = page.$('a[href="/my_messages.php"]');
elem.click();
// Enter login credentials and login
page.$('input[name="login"]').type('admin');
page.$('input[name="password"]').type('123');
page.$('input[type="submit"]').click();
// We expect the above form submission to trigger a navigation, so wait for it
// and the page to be loaded.
page.waitForNavigation();
check(page, {
'header': page.$('h2').textContent() == 'Welcome, admin!',
});
page.close();
browser.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment