-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres_test.py
29 lines (23 loc) · 906 Bytes
/
postgres_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
from locust import TaskSet, between, task
from locust.contrib.postgres import PostgresUser
import os
import random
class PostgresLocust(PostgresUser):
@task
def run_select_query(self):
self.client.execute_query(
"SELECT * FROM loadtesting.invoice WHERE amount > 500",
)
@task
def run_update_query(self):
random_amount = random.randint(1, 12)
self.client.execute_query(
f"UPDATE loadtesting.invoice SET amount={random_amount} WHERE amount < 10",
)
# Use environment variables or default values
PGHOST = os.getenv("PGHOST", "localhost")
PGPORT = os.getenv("PGPORT", "5432")
PGDATABASE = os.getenv("PGDATABASE", "test_db")
PGUSER = os.getenv("PGUSER", "postgres")
PGPASSWORD = os.getenv("PGPASSWORD", "postgres")
conn_string = f"postgresql://{PGUSER}:{PGPASSWORD}@{PGHOST}:{PGPORT}/{PGDATABASE}"