Skip to content
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

Profiling NewRelic 8.11.0 #2317

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
33 changes: 33 additions & 0 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@
from __future__ import print_function

import os
import time

# Check if profiling should be enabled
enable_profiling = os.getenv('NOTIFY_PROFILE') is not None

if enable_profiling:
print("Profiling enabled")
import cProfile
import pstats

Check notice

Code scanning / CodeQL

Module is imported with 'import' and 'import from' Note

Module 'pstats' is imported with both 'import' and 'import from'.
from pstats import SortKey

# Create a cProfile.Profile object
profiler = cProfile.Profile()
# Start profiling
profiler.enable()

# Timer start for initialization.
start_time = time.time()

import newrelic.agent # See https://bit.ly/2xBVKBH
from apig_wsgi import make_lambda_handler
Expand Down Expand Up @@ -40,6 +58,21 @@
print("========================================================")
print("")

# Timer end for initialization.
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Elapsed time: {elapsed_time:.2f}s")

if enable_profiling:
# Stop profiling
profiler.disable()
# Dump profiling results to a file
profiler.dump_stats('profile_results.prof')
# Analyze profiling results
with open('profile_report.txt', 'w') as f:
stats = pstats.Stats('profile_results.prof', stream=f)
stats.sort_stats(SortKey.CUMULATIVE)
stats.print_stats()

def handler(event, context):
newrelic.agent.initialize() # noqa: E402
Expand Down
2 changes: 1 addition & 1 deletion gunicorn_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

newrelic.agent.initialize() # noqa: E402

workers = 4
workers = 1
worker_class = "gevent"
worker_connections = 256
bind = "0.0.0.0:{}".format(os.getenv("PORT"))
Expand Down
34 changes: 17 additions & 17 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ PyYAML = "6.0.1"

cachelib = "0.12.0"
SQLAlchemy = "1.4.52"
newrelic = "8.10.0"
newrelic = "8.11.0"
notifications-python-client = "6.4.1"
python-dotenv = "1.0.1"
pwnedpasswords = "2.0.0"
Expand Down
Loading