Skip to content

Commit

Permalink
Added profiling code around library init
Browse files Browse the repository at this point in the history
  • Loading branch information
jimleroyer committed Oct 2, 2024
1 parent ad149b4 commit 24fd2cd
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
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
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
Expand Down Expand Up @@ -43,10 +58,22 @@
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
newrelic.agent.register_application(timeout=20.0)
Expand Down

0 comments on commit 24fd2cd

Please sign in to comment.