Run multiple load test scripts with K6

k6 run test1.js ; k6 run test2.js ; …
// signIn.js
import { config, globalChecks } from ‘../common/index.js’
import http from ‘k6/http’
import { sleep } from ‘k6’
import { Trend, Rate, Counter } from ‘k6/metrics’
export let SignInDuration = new Trend(‘Sign in Duration’)
export let SignInFailRate = new Rate(‘Sign in Fail Rate’)
export let SignInReqs = new Counter(‘Sign in Requests’)
let duration = 500
let rate = 0.1
export let options = {
thresholds: {
‘Sign in Duration’: [`p(95)<${duration}`],
‘Sign in Fail Rate’: [`rate<${rate}`]
}
}
const users = JSON.parse(open(‘../common/users.json’))export default function () {
for (let user of users) {
let res = http.post(__ENV.HOST + config.api.signIn, {
“email”: user.email, “password”: user.password
})
globalChecks(res, duration) || SignInFailRate.add(1)
SignInDuration.add(res.timings.duration)
SignInReqs.add(1)
}
sleep(1)
}
// signUp.jsimport { config, globalChecks } from ‘../common/index.js’
import http from ‘k6/http’
import { sleep } from ‘k6’
import { Trend, Rate, Counter } from ‘k6/metrics’
import faker from ‘cdnjs.com/libraries/Faker’
export let SignUpDuration = new Trend(‘Sign up Duration’)
export let SignUpFailRate = new Rate(‘Sign up Fail Rate’)
export let SignUpReqs = new Counter(‘Sign up Requests’)
let duration = 1000
let rate = 0.1
export let options = {
thresholds: {
‘Sign up Duration’: [`p(95)<${duration}`],
‘Sign up Fail Rate’: [`rate<${rate}`]
}
}
export default function () {
let res = http.post(__ENV.HOST + config.api.signUp, {
“email”: ‘QA_TECH_’ + faker.internet.email(),
“password”: faker.internet.password(),
“language”: “vn”, “gender”: “M”
})
globalChecks(res, duration) || SignUpFailRate.add(1)
SignUpDuration.add(res.timings.duration)
SignUpReqs.add(1)
sleep(1)
}
// signOut.jsimport { config, globalChecks } from ‘../common/index.js’
import * as helper from ‘../common/helper.js’
import http from ‘k6/http’
import { sleep } from ‘k6’
import { Trend, Rate, Counter } from ‘k6/metrics’
export let SignOutDuration = new Trend(‘Sign out Duration’)
export let SignOutFailRate = new Rate(‘Sign out Fail Rate’)
export let SignOutReqs = new Counter(‘Sign out Requests’)
let duration = 500
let rate = 0.1
export let options = {
thresholds: {
‘Sign out Duration’: [`p(95)<${duration}`],
‘Sign out Fail Rate’: [`rate<${rate}`]
}
}
export function setup() {
let userCookies = helper.getCookies()
return { cookies: userCookies }
}
export default function (data) {
let jar = http.cookieJar()
jar.set(__ENV.HOST, ‘leflair.connect.sid’, JSON.parse(data.cookies)[‘leflair.connect.sid’][0].value)
let res = http.get(__ENV.HOST + config.api.signOut)globalChecks(res, duration) || SignOutFailRate.add(1)
SignOutDuration.add(res.timings.duration)
SignOutReqs.add(1)
sleep(1)
}
import * as helper from ‘./common/helper.js’
import { group, sleep } from ‘k6’
import SignInTest from ‘./tests/signIn.js’
import { options as SignInOptions } from ‘./tests/signIn.js’
import SignOutTest from ‘./tests/signOut.js’
import { options as SignOutOptions } from ‘./tests/signOut.js’
import SignUpTest from ‘./tests/signUp.js’
import { options as SignUpOptions } from ‘./tests/signUp.js’
export function setup() {
let userCookies = helper.getCookies()
return {
cookies: userCookies
}
}
let optionsSum = {} // here we use the imported options to create a sum of options to run all tests
Object.entries(SignInOptions.thresholds).forEach(item => optionsSum[item[0]] = item[1])
Object.entries(SignOutOptions.thresholds).forEach(item => optionsSum[item[0]] = item[1])
Object.entries(SignUpOptions.thresholds).forEach(item => optionsSum[item[0]] = item[1])
export let options = {
thresholds: optionsSum
}
export default (data) => {
group(‘Sign in’, () => {
SignInTest()
})
group(‘Sign out’, () => {
SignOutTest(data)
})
group(‘Sign up’, () => {
SignUpTest()
})
sleep(1)
}

--

--

--

From translator -> Manual QC Engineer -> Automation QA Engineer -> NodeJS Developer -> yearning to learn more

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hung Tran Nguyen

Hung Tran Nguyen

From translator -> Manual QC Engineer -> Automation QA Engineer -> NodeJS Developer -> yearning to learn more

More from Medium

Use of dynamic access token for multiple requests in Jmeter

Running Playwright Mocha tests in BuildWise CT Server

Mocking backend API and automating end-to-end tests for mobile native apps

What is Playwright? Cross-browser automation library for end-to-end testing