Skip to content

Instantly share code, notes, and snippets.

@mvanholsteijn
Last active Jun 12, 2021
Embed
What would you like to do?
simple k6 performance test script for privatebin installations
import http from 'k6/http';
import {
sleep,
check
} from 'k6';
import {Trend} from 'k6/metrics';
import privatebin from 'k6/x/privatebin';
let createMetric = new Trend('01 - create-paste');
let getMetric = new Trend('02 - get-paste');
let deleteMetric = new Trend('03 - delete-paste');
export default function () {
let test = {
createPaste: function () {
this.paste = privatebin.encrypt("hello world!", "5min");
this.pasteInfo = null;
let res = http.post(this.host, this.paste.body, {headers: {'X-Requested-With': 'JSONHttpRequest'}});
createMetric.add(res.timings.duration);
if (check(res, {'create-paste': (r) => this.ok(r)})) {
this.pasteInfo = res.json();
}
},
getPaste: function () {
if (this.pasteInfo != null) {
let res = http.get(this.host + '/?' + this.pasteInfo.id, {headers: {'X-Requested-With': 'JSONHttpRequest'}});
getMetric.add(res.timings.duration);
check(res, {'get-paste': (r) => this.ok(r)});
}
},
deletePaste: function () {
if (this.pasteInfo != null) {
let res = http.post(this.host, JSON.stringify({
'pasteid': this.pasteInfo.id,
'deletetoken': this.pasteInfo.token
}), {headers: {'X-Requested-With': 'JSONHttpRequest'}});
deleteMetric.add(res.timings.duration);
check(res, {'delete-paste': (r) => this.ok(r)});
this.pasteInfo = null;
}
},
run: function (host) {
this.host = host;
this.createPaste();
this.getPaste();
this.deletePaste();
sleep(0.5);
},
ok: function (res) {
let result = res.status === 200 &&
res.headers['Content-Type'].startsWith('application/json');
if (result) {
try {
result = res.json().status === 0;
} catch (err) {
console.log(err);
result = false;
}
}
if (!result) {
console.log(res.body);
}
return result;
},
host: null,
paste: null,
pasteInfo: null,
}
test.run("https://privatebin.net");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment