Reputation: 412
I am using python 3.10 and here is my locust file.
from locust import HttpUser, task, between
import string
import random
import time
import datetime
WAIT_TIME_MIN = 1
WAIT_TIME_MAX = 5
h = {
"Content-Type": "application/json"
}
random.seed()
class LoadTest(HttpUser):
wait_time = between(WAIT_TIME_MIN, WAIT_TIME_MAX)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.path = None
def generate_random_string(self, min_name_size=2, max_name_size=20) -> str:
letters = string.ascii_lowercase
string_size = random.randint(min_name_size, max_name_size)
generated_string = ''.join(random.choice(letters) for i in range(string_size))
return generated_string
def generate_random_dob(self) -> str:
d = random.randint(1, int(time.time()))
return datetime.date.fromtimestamp(d).strftime('%Y-%m-%d')
@task(2)
def get_all(self):
self.client.get(url=self.path)
@task(8)
def post_request(self):
self.client.post(url=self.path, json=self._generate_post_data(), headers=h)
class TeacherProcess(LoadTest):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.path = "/api/v1/teacher"
def _generate_post_data(self):
request_data = {
"teacherName": str,
"teacherEmail": str,
"teacherDOB": str
}
request_data["teacherName"] = self.generate_random_string()
request_data["teacherEmail"] = f"{self.generate_random_string()}@{self.generate_random_string()}.{self.generate_random_string()}"
request_data["teacherDOB"] = self.generate_random_dob()
return request_data
class StudentProcess(LoadTest):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.path = "/api/v1/student"
def _generate_post_data(self):
request_data = {
"studentName": str,
"studentEmail": str,
"studentDOB": str
}
request_data["studentName"] = self.generate_random_string()
request_data["studentEmail"] = f"{self.generate_random_string()}@{self.generate_random_string()}.{self.generate_random_string()}"
request_data["studentDOB"] = self.generate_random_dob()
return request_data
I don't know how but somehow I am able to use _generate_post_data
inside LoadTest
class. I am telling it's working because locust output as below:
Type Name # reqs # fails | Avg Min Max Med | req/s failures/s
--------|----------------------------------------------------------------------------|-------|-------------|-------|-------|-------|-------|--------|-----------
GET /api/v1/student 126 0(0.00%) | 36 2 117 7 | 12.78 0.00
POST /api/v1/student 499 0(0.00%) | 66 1 276 6 | 50.61 0.00
GET /api/v1/teacher 135 0(0.00%) | 53 2 233 8 | 13.69 0.00
POST /api/v1/teacher 502 0(0.00%) | 60 2 238 6 | 50.92 0.00
--------|----------------------------------------------------------------------------|-------|-------------|-------|-------|-------|-------|--------|-----------
Aggregated 1262 0(0.00%) | 59 1 276 7 | 128.00 0.00
Response time percentiles (approximated)
Type Name 50% 66% 75% 80% 90% 95% 98% 99% 99.9% 99.99% 100% # reqs
--------|--------------------------------------------------------------------------------|--------|------|------|------|------|------|------|------|------|------|------|------
GET /api/v1/student 7 10 110 110 110 110 120 120 120 120 120 126
POST /api/v1/student 6 10 200 220 250 260 260 270 280 280 280 499
GET /api/v1/teacher 8 10 110 140 230 230 230 230 230 230 230 135
POST /api/v1/teacher 6 9 180 200 220 230 230 230 240 240 240 502
--------|--------------------------------------------------------------------------------|--------|------|------|------|------|------|------|------|------|------|------|------
Aggregated 7 10 110 190 230 240 260 260 270 280 280 1262
As you can see there is no failure. My question is how I am able to access _generate_post_data
while I am inside the LoadTest
class? The second one is related to below error:
[2023-01-25 12:31:38,147] pop-os/ERROR/locust.user.task: 'LoadTest' object has no attribute '_generate_post_data'
Traceback (most recent call last):
File "/home/ak/.local/lib/python3.10/site-packages/locust/user/task.py", line 347, in run
self.execute_next_task()
File "/home/ak/.local/lib/python3.10/site-packages/locust/user/task.py", line 372, in execute_next_task
self.execute_task(self._task_queue.pop(0))
File "/home/ak/.local/lib/python3.10/site-packages/locust/user/task.py", line 493, in execute_task
task(self.user)
File "/home/ak/Desktop/my-projects/spring-boot-app/performans-testing/locust.py", line 38, in post_request
self.client.post(url=self.path, json=self._generate_post_data(), headers=h)
AttributeError: 'LoadTest' object has no attribute '_generate_post_data'
I am quite confused, is that a locust bug, or am I doing something wrong? While locust doesn't show any failure, while I am having this error. If anyone can explain that, I would be appreciated
Upvotes: 0
Views: 1340
Reputation: 11426
You need to inform Locust not to instantiate users based on the parent LoadTest class.
class LoadTest(HttpUser):
abstract = True
...
Upvotes: 1