-
Notifications
You must be signed in to change notification settings - Fork 3
/
load_test.py
97 lines (72 loc) · 2.65 KB
/
load_test.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import signal
import time
import random
import gevent
import gevent.monkey
import logging
gevent.monkey.patch_all()
from locust import HttpUser, task, between, constant
from locust.env import Environment
from locust.log import setup_logging
from locust.stats import stats_printer, stats_history
from input import text
setup_logging("INFO", None)
logger = logging.getLogger("pytest")
class Backend:
def __init__(self):
import subprocess
self.process = subprocess.Popen(["python3", "../backend/main.py"])
time.sleep(25)
def kill(self):
os.killpg(os.getpgid(self.process.pid), signal.SIGHUP)
def get_random_text() -> str:
output = ""
for i in range(random.randint(2, 15)):
output += random.choice(text) + " "
return output + "."
class BackendUser(HttpUser):
# wait_time = constant(1)
host = "http://localhost:8000"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# def on_start(self):
# logger.info("Starting the backend")
# self.backend = Backend()
# def on_stop(self):
# logger.info("Killing the backend")
# self.backend.kill()
# time.sleep(5)
@task
def generate_task(self):
data = {"text": get_random_text()}
self.client.post("/generate", json=data)
def test_load(users=5, spawn_rate=10, time_s=200):
# setup Environment and Runner
env = Environment(user_classes=[BackendUser])
env.create_local_runner()
# start a greenlet that periodically outputs the current stats
gevent.spawn(stats_printer(env.stats))
# start a greenlet that save current stats to history
gevent.spawn(stats_history, env.runner)
# start the test
env.runner.start(users, spawn_rate=spawn_rate)
# in 60 seconds stop the runner
gevent.spawn_later(time_s, lambda: env.runner.quit())
# wait for the greenlets
env.runner.greenlet.join()
logger.info("Average Response Time: " + str(env.stats.total.avg_response_time))
logger.info("Number of Failures: " + str(env.stats.total.num_failures))
logger.info("Response time percentile<(0.95): " + str(env.stats.total.get_response_time_percentile(0.95)))
assert env.stats.total.avg_response_time < 60
assert env.stats.total.num_failures == 0
assert env.stats.total.get_response_time_percentile(0.95) < 100
# backend.kill()
# todo start the backend
# todo kill the backend
if __name__ == '__main__':
# test_load(users=1, spawn_rate=5, time_s=15)
print(get_random_text())
import requests
resp = requests.post("http://localhost:8000/generate", json={"text": str("huhhu iij")})
print(resp)