Skip to content

Instantly share code, notes, and snippets.

@mvanholsteijn
Last active February 16, 2023 12:38
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mvanholsteijn/b7c9fd4987d847561aa8aff4dc7a146e to your computer and use it in GitHub Desktop.
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