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

Format code, add stream #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fastapi_cprofile/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.0.2"
__version__ = "0.0.3"
__author__ = "jijunlx"
58 changes: 31 additions & 27 deletions fastapi_cprofile/profiler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
import sys
from typing import Optional

from starlette.routing import Router
Expand All @@ -12,12 +13,15 @@ def __init__(
self,
app: ASGIApp,
server_app: Optional[Router] = None,
enable : bool = True,
sort_by : str = 'cumulative',
print_each_request : bool = False,
filename : str = None,
strip_dirs : bool = False):

enable: bool = True,
sort_by: str = "cumulative",
print_each_request: bool = False,
filename: str = None,
strip_dirs: bool = False,
stream=None,
):

self.stream = stream or sys.stdout
self.app = app
self.enable = enable
self._server_app = server_app
Expand All @@ -27,32 +31,31 @@ def __init__(
self._print_each_request = print_each_request
self._filename = filename
self._strip_dirs = strip_dirs


async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:

if scope["type"] != "http" or self.enable is False:
await self.app(scope, receive, send)
return

if self._server_app is not None:
self._server_app.add_event_handler("shutdown", self.get_profiler_result)

self._profiler.enable()

request = Request(scope, receive=receive)
method = request.method
path = request.url.path
begin = time.perf_counter()

# Default status code used when the application does not return a valid response
# or an unhandled exception occurs.
status_code = 500

async def wrapped_send(message: Message) -> None:
if message['type'] == 'http.response.start':
if message["type"] == "http.response.start":
nonlocal status_code
status_code = message['status']
status_code = message["status"]
await send(message)

try:
Expand All @@ -61,23 +64,24 @@ async def wrapped_send(message: Message) -> None:
if self._print_each_request:
self._profiler.disable()
end = time.perf_counter()
print(f"Method: {method} ", f"Path: {path} ", f"Duration: {end - begin} ", f"Status: {status_code}")
ps = pstats.Stats(self._profiler).sort_stats(self._sort_by)
print(
f"Method: {method} ",
f"Path: {path} ",
f"Duration: {end - begin} ",
f"Status: {status_code}",
file=self.stream,
)
ps = pstats.Stats(self._profiler, stream=self.stream).sort_stats(
self._sort_by
)
if self._strip_dirs:
ps.strip_dirs()
ps.print_stats()


async def get_profiler_result(self):
self._profiler.disable()
ps = pstats.Stats(self._profiler).sort_stats(self._sort_by)
if self._strip_dirs:
ps.strip_dirs()
if self._filename:
ps.dump_stats(self._filename)






self._profiler.disable()
ps = pstats.Stats(self._profiler, stream=self.stream).sort_stats(self._sort_by)
if self._strip_dirs:
ps.strip_dirs()
if self._filename:
ps.dump_stats(self._filename)