-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PostgresUser to examples #2836
Conversation
|
Scratch that, I can see that psycopg3 has built in support for gevent! Awesome! |
Can you split this into two files? One that contains a base Also, maybe you can read connection settings from class variables of the final User class defined in the actual locustfile? Another option is to just use the standard postgres env vars (PGHOST, PGUSER etc) |
@cyberw I broke these files out and nested them inside of their own |
locust/contrib/postgres.py
Outdated
|
||
|
||
class PostgresClient: | ||
def __getattr__(self, name): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think you need the whole __getattr__
song and dance :)
Just define the execute_query method directly on the PostgresClient class.
locust/contrib/postgres.py
Outdated
|
||
|
||
def execute_query(conn_string, query): | ||
db_conn = create_conn(conn_string) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this create a new connection on each query? That sounds very wasteful. Probably you wanna do that only when the client object is created.
locust/contrib/postgres.py
Outdated
response_length=0, | ||
exception=e, | ||
) | ||
print(f"error: {e}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think we should print the exception :)
locust/contrib/postgres.py
Outdated
import psycopg | ||
|
||
|
||
def create_conn(conn_string): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this method is too small to make sense! The code is easier to understand if you just call .connect directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, it looks a lot cleaner calling the psycopg.connect
directly
locust/contrib/postgres.py
Outdated
def execute_query(self, query): | ||
start_time = time.time() | ||
try: | ||
cursor = self.connection.cursor() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
psycopg3 can do connection.execute(), no need to have the intermediate step of creating a cursor.
examples/postgres/locustfile.py
Outdated
|
||
class PostgresLocust(PostgresUser): | ||
tasks = [UserTasks] | ||
min_wait = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove the wait_time stuff and just leave it as the default, to simplify the example.
You can skip the taskset and put the tasks directly under the user too.
Real close now, just a few last simplifications. |
@cyberw I made the updates for this. I think it is A LOT better than when it started. Thanks for all your patience as I made changes 🙏🏽 |
Overview
This PR creates a new
database/
sub-dir inside of examples where database related files can be stored. Currently this only supports PostgreSQL. Ideally in the future we can have support for Mongo, MySQL, Redis, etc.Changes
examples/database/README.md
examples/database/postgres_test.py