-
Notifications
You must be signed in to change notification settings - Fork 0
/
locustfile.py
53 lines (41 loc) · 1.37 KB
/
locustfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
from collections import deque
import os
import json
from faker import Faker
from locust import HttpLocust, TaskSet, task
from minikin.utils import generate_slug
PWD = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(PWD, 'slugs')) as fo:
slugs = deque([
row.split('|')[0].strip()
for row in fo.readlines()[2:-2]]) # ommit header and footer
def gen_url():
fake = Faker()
uri, slug = fake.uri().rstrip('/'), fake.slug()
return f'{uri}#{slug}'
class Tasks(TaskSet):
@task(1)
def shorten_url(self):
rsp = self.client.post("/shorten_url", json.dumps({"url": gen_url()}))
if rsp.ok:
slug = rsp.json()['shortened_url'].split('/')[-1]
slugs.append(slug)
@task(1)
def not_found(self):
slug = generate_slug(gen_url(), 7)
with self.client.get(f'/{slug}',
name='/[not-found]',
catch_response=True,
allow_redirects=False) as response:
if response.status_code == 404:
response.success()
@task(8)
def found(self):
slug = slugs.popleft().strip()
self.client.get(f'/{slug}', name='/[found]', allow_redirects=False)
slugs.append(slug)
class User(HttpLocust):
task_set = Tasks
min_wait = 100
max_wait = 100