Skip to content

Instantly share code, notes, and snippets.

@liangway
Created February 6, 2021 07:00
Show Gist options
  • Save liangway/ddc4c4d30a657c4f76c99be70e4c4b03 to your computer and use it in GitHub Desktop.
Save liangway/ddc4c4d30a657c4f76c99be70e4c4b03 to your computer and use it in GitHub Desktop.
Code for Run performance test with k6 on medium
// Example of http get
// 1_http_get.js
import http from 'k6/http';
import { sleep } from 'k6';
export default function() {
http.get('http://test.k6.io');
sleep(1);
}
// Example of http get
// 2_http_post.js
import http from 'k6/http';
export default function () {
var url = 'http://test.k6.io/login';
var payload = JSON.stringify({
email: 'aaa',
password: 'bbb',
});
var params = {
headers: {
'Content-Type': 'application/json',
},
};
http.post(url, payload, params);
}
// Example of check
//
// Checks are like asserts but differ in that they don't halt the execution,
// instead, they just store the result of the check, pass or fail,
// and let the script execution continue.
// Ref. https://k6.io/docs/using-k6/checks
//
// 3_http_check.js
import { check } from 'k6';
import http from 'k6/http';
export default function () {
let res = http.get('http://test.k6.io/');
check(res, {
'is status 500': (r) => r.status === 500,
});
}
// Example of threshold
// Thresholds are a pass/fail criteria used to specify the performance expectations of
// the system under test.
// Ref. https://k6.io/docs/using-k6/thresholds
// 4_http_threadshold.js
import http from 'k6/http';
import { Rate } from 'k6/metrics';
const myFailRate = new Rate('failed requests');
export let options = {
thresholds: {
'failed requests': ['rate<0.1'], // threshold on a custom metric
http_req_duration: ['p(95)<500'], // threshold on a standard metric
},
};
export default function () {
let res = http.get('https://test-api.k6.io/public/crocodiles/1/');
myFailRate.add(res.status !== 200);
}
// Example of option
// Options allow you to configure how k6 will behave during test execution.
// Ref. https://k6.io/docs/using-k6/options
// 5_http_options.js
import http from 'k6/http';
import { sleep } from 'k6';
export let options = {
vus: 10,
iterations: 100,
};
export default function() {
http.get('http://test.k6.io');
sleep(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment