2021-04-12 10:55:31 -04:00
|
|
|
from locust import task, between, SequentialTaskSet
|
|
|
|
from locust.contrib.fasthttp import FastHttpUser
|
|
|
|
from locust import events
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
@events.quitting.add_listener
|
|
|
|
def _(environment, **kw):
|
2021-04-13 04:56:50 -04:00
|
|
|
totalStats = environment.stats.total
|
|
|
|
if totalStats.fail_ratio > 0.20:
|
|
|
|
logging.error("Test failed due to failure ratio " + totalStats.fail_ratio + " > 20%")
|
2021-04-12 10:55:31 -04:00
|
|
|
environment.process_exit_code = 1
|
2021-04-13 04:56:50 -04:00
|
|
|
elif totalStats.get_response_time_percentile(0.80) > 800:
|
|
|
|
logging.error("Test failed due to 80th percentile response time > 800 ms")
|
2021-04-12 10:55:31 -04:00
|
|
|
environment.process_exit_code = 1
|
|
|
|
else:
|
|
|
|
environment.process_exit_code = 0
|
|
|
|
|
|
|
|
class QuickStartUser(SequentialTaskSet):
|
|
|
|
wait_time = between(0.1,0.2)
|
|
|
|
|
|
|
|
@task
|
|
|
|
def captcha(self):
|
|
|
|
captcha_params = {"level":"debug","media":"image/png","input_type":"text"}
|
|
|
|
|
2021-04-12 23:32:11 -04:00
|
|
|
with self.client.post(path="/v1/captcha", json=captcha_params, name="/captcha", catch_response = True) as resp:
|
|
|
|
if resp.status_code != 200:
|
|
|
|
resp.failure("Status was not 200: " + resp.text)
|
|
|
|
captchaJson = resp.json()
|
|
|
|
uuid = captchaJson.get("id")
|
|
|
|
if not uuid:
|
|
|
|
resp.failure("uuid not returned on /captcha endpoint: " + resp.text)
|
|
|
|
|
|
|
|
with self.client.get(path="/v1/media?id=%s" % uuid, name="/media", stream=True, catch_response = True) as resp:
|
|
|
|
if resp.status_code != 200:
|
|
|
|
resp.failure("Status was not 200: " + resp.text)
|
2021-04-12 10:55:31 -04:00
|
|
|
|
2021-04-12 23:32:11 -04:00
|
|
|
media = resp.content
|
2021-04-12 10:55:31 -04:00
|
|
|
|
2021-04-12 23:32:11 -04:00
|
|
|
ocrAnswer = self.solve(uuid, media)
|
2021-04-12 10:55:31 -04:00
|
|
|
|
|
|
|
answerBody = {"answer": ocrAnswer,"id": uuid}
|
|
|
|
with self.client.post(path='/v1/answer', json=answerBody, name="/answer", catch_response=True) as resp:
|
|
|
|
if resp.status_code != 200:
|
2021-04-12 23:32:11 -04:00
|
|
|
resp.failure("Status was not 200: " + resp.text)
|
2021-04-12 10:55:31 -04:00
|
|
|
else:
|
|
|
|
if resp.json().get("result") != "True":
|
2021-04-13 04:58:14 -04:00
|
|
|
resp.failure("Answer was not accepted: " + ocrAnswer)
|
2021-04-12 10:55:31 -04:00
|
|
|
|
2021-04-12 23:32:11 -04:00
|
|
|
def solve(self, uuid, media):
|
|
|
|
mediaFileName = "tests/test-%s.png" % uuid
|
|
|
|
with open(mediaFileName, "wb") as f:
|
|
|
|
f.write(media)
|
2021-04-13 04:12:34 -04:00
|
|
|
#ocrResult = subprocess.Popen("gocr %s" % mediaFileName, shell=True, stdout=subprocess.PIPE)
|
|
|
|
ocrResult = subprocess.Popen("tesseract %s stdout -l eng" % mediaFileName, shell=True, stdout=subprocess.PIPE)
|
2021-04-12 23:32:11 -04:00
|
|
|
ocrAnswer = ocrResult.stdout.readlines()[0].strip().decode()
|
|
|
|
return ocrAnswer
|
|
|
|
|
|
|
|
|
2021-04-12 10:55:31 -04:00
|
|
|
|
|
|
|
class User(FastHttpUser):
|
|
|
|
wait_time = between(0.1,0.2)
|
|
|
|
tasks = [QuickStartUser]
|
|
|
|
host = "http://localhost:8888"
|