Skip to content

Commit

Permalink
change how the connection is opened
Browse files Browse the repository at this point in the history
  • Loading branch information
guel-codes committed Aug 21, 2024
1 parent 70ddd14 commit b43a0c7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
4 changes: 1 addition & 3 deletions examples/postgres/locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ class UserTasks(TaskSet):
@task
def run_select_query(self):
self.client.execute_query(
self.user.conn_string,
"SELECT * FROM loadtesting.invoice WHERE amount > 500",
)

@task(3)
def run_update_query(self):
random_amount = random.randint(1, 12)
self.client.execute_query(
self.user.conn_string,
f"UPDATE loadtesting.invoice SET amount={random_amount} WHERE amount < 10",
)

Expand All @@ -31,7 +29,7 @@ class PostgresLocust(PostgresUser):
# Use environment variables or default values
PGHOST = os.getenv("PGHOST", "localhost")
PGPORT = os.getenv("PGPORT", "5432")
PGDATABASE = os.getenv("PGDATABASE", "loadtesting_db")
PGDATABASE = os.getenv("PGDATABASE", "postgres")
PGUSER = os.getenv("PGUSER", "postgres")
PGPASSWORD = os.getenv("PGPASSWORD", "postgres")

Expand Down
63 changes: 33 additions & 30 deletions locust/contrib/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,44 @@ def create_conn(conn_string):
return psycopg.connect(conn_string)


def execute_query(conn_string, query):
db_conn = create_conn(conn_string)
return db_conn.cursor().execute(query)


class PostgresClient:
def __getattr__(self, name):
def request_handler(*args, **kwargs):
start_time = time.time()
try:
execute_query(*args, **kwargs)
response_time = int((time.time() - start_time) * 1000)
events.request.fire(
request_type="postgres_success",
name=name,
response_time=response_time,
response_length=0,
)
except Exception as e:
response_time = int((time.time() - start_time) * 1000)
events.request.fire(
request_type="postgres_failure",
name=name,
response_time=response_time,
response_length=0,
exception=e,
)
print(f"error: {e}")

return request_handler
def __init__(self, conn_string):
self.conn_string = conn_string
self.connection = create_conn(conn_string)

def execute_query(self, query):
start_time = time.time()
try:
cursor = self.connection.cursor()
cursor.execute(query)
response_time = int((time.time() - start_time) * 1000)
events.request.fire(
request_type="postgres_success",
name="execute_query",
response_time=response_time,
response_length=0,
)
except Exception as e:
response_time = int((time.time() - start_time) * 1000)
events.request.fire(
request_type="postgres_failure",
name="execute_query",
response_time=response_time,
response_length=0,
exception=e,
)
print(f"error: {e}")

def close(self):
self.connection.close()


class PostgresUser(User):
abstract = True

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.client = PostgresClient()
self.client = PostgresClient(conn_string=self.conn_string)

def on_stop(self):
self.client.close()

0 comments on commit b43a0c7

Please sign in to comment.