-
Notifications
You must be signed in to change notification settings - Fork 11
Improve API performance #57
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
Conversation
| _handler = MpicCaaCheckerLambdaHandler() | ||
| return _handler | ||
|
|
||
| if os.environ.get("PYTEST_VERSION") is None: |
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.
Add a newline before and after these if statements (so there's 2 lines between top level commands) for PEP8 compliance (PyCharm give me style warnings here). In general, in the main.py files, have there be 2 newlines between top level commands.
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.
Fixed. Now, to prevent these basic errors in the future, a linting tool like pycodestyle could be introduced to check for some specific coding conventions errors like E305 (https://pycodestyle.pycqa.org/en/latest/intro.html#error-codes).
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.
It's a good thought. I'll add it to our to-dos to look at to toss into the pipeline.
| _handler = MpicDcvCheckerLambdaHandler() | ||
| return _handler | ||
|
|
||
| if os.environ.get("PYTEST_VERSION") is None: |
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 would like to not use a check that looks for PYTEST_VERSION (set by virtue of being in a unit test run)... I prefer a more explicit env variable to drive this.
Recommend this in each of the main.py files instead:
if os.environ.get("INIT_LAMBDA_HANDLER", "true").lower() == "true":
get_handler()
Then, to set the variable accordingly in the unit tests, I would leverage the existing conftest.py and add this:
def pytest_configure():
os.environ["INIT_LAMBDA_HANDLER"] = "false"
at the top, just under the imports and above def setup_logging(). But do preserve 2 spaces between imports and the next command.
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.
To keep things simpler, what do you think of looking for AWS_LAMBDA_FUNCTION_NAME (https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime) to detect code is running in AWS Lambda?
be85d95 to
06b4db4
Compare
|
I spoke with @sciros and the AWS lambda name check is OK since we are in an AWS repo. I am going ahead with merging this in since all comments are addressed. |
This PR improves the cold start time by a around 2 seconds. There are some other options to prevent cold starts, like Provisioned Concurrency, SnapStart or EventBridge Scheduler (as a warmer), but they cannot fully eliminate cold starts. For example, with Provisioned Concurrency, if the number of concurrent requests surpasses the number of provisioned execution environments, cold starts are still going to happen.
So, given that cold starts cannot be totally eliminated, it makes sense to improve cold start first.
With the current implementation, for 5 concurrent requests sent using an script like the following:
The result is:
With the proposed changes:
Note that for obtaining the previous cold starts results, it is necessary to wait around 7 minutes without any requests to AWS to get all the current execution environments to be shut down.
On the other hand, for warm starts some improvements have been observed as well.
Now, about the changes, the pool of clients has been reduced to a single client per region as Lambda execution environments only execute one request at the time. From https://docs.aws.amazon.com/lambda/latest/dg/lambda-concurrency.html:
Additionally, I've increased the memory for the perspectives to 256 MB as I've observed they are running close to the current limit of 128MB.
On the other hand, I've reduced the default memory to 256 MB for the coordinator as the average usage is currently around 160 MB. Doubling the memory increases the cost without noticeable performance improvements.
Finally, in the future, AWS Lambda Power Tuning (see https://aws.amazon.com/pt/blogs/compute/operating-lambda-performance-optimization-part-2/) could possibly be introduced to automatically measure and supervise the memory usage of the functions as changes are being introduced.