Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple locust performance test script to load test a privatebin installation
from locust import SequentialTaskSet, HttpUser, between, task
from locust.contrib.fasthttp import FastHttpUser
from pbincli.api import PrivateBin
from pbincli.format import Paste
class SequenceOfTasks(SequentialTaskSet):
def on_start(self):
self.payload = "hello world"
self.url = None
self.pasteid = None
self.delete_token = None
self.hash = None
self.headers = {"X-Requested-With": "JSONHttpRequest"}
@task
def create_paste(self):
paste = Paste()
paste.setText(self.payload)
paste.encrypt(
formatter="plaintext",
burnafterreading=False,
discussion=True,
expiration="5min",
)
request = paste.getJSON()
response = self.client.post(
"/", data=request, headers=self.headers, name="create-paste"
)
if response.status_code == 200:
r = response.json()
self.url = r.get("url")
self.pasteid = r.get("id")
self.delete_token = r.get("deletetoken")
self.hash = paste.getHash()
@task
def get_paste(self):
if not self.url:
return
with self.client.get(
self.url, headers=self.headers, name="get-paste", catch_response=True
) as response:
if response.status_code != 200:
response.failure(
f'failed to get paste "{self.pasteid}", {response.text}'
)
return
paste = Paste()
paste.loadJSON(response.json())
paste.setHash(self.hash)
paste.decrypt()
value = paste.getText()
if isinstance(value, bytes):
value = value.decode()
if value != self.payload:
response.failure(f'expected "{self.payload}", got "{value}"')
@task
def delete_paste(self):
if not self.url:
return
r = self.client.post(
self.url,
json={"pasteid": self.pasteid, "deletetoken": self.delete_token},
headers=self.headers,
name="delete-paste",
)
class QuickstartUser(HttpUser):
wait_time = between(0, 0)
tasks = {SequenceOfTasks: 1}
@mvanholsteijn
Copy link
Author

mvanholsteijn commented Jun 11, 2021

Note that this script will each your CPU in the blink of an eye. Use k6 with privatebin extension if you need some reliable numbers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment