-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide elapsed and timestamp info to filename_format
This provides the `elapsed` and `time` values to the `ProfileMiddleware(filename_format=...)` function. Prior to this change, one could not replicate the format string format, much less modify how it would render the timestamp or elapsed time values. These new values can be found under the `werkzeug.profiler` key in the WSGI environ dict passed into the `filename_format()` function. Addresses #2775
- Loading branch information
1 parent
599993d
commit 4820d8c
Showing
3 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import datetime | ||
import os | ||
from unittest.mock import ANY | ||
from unittest.mock import MagicMock | ||
from unittest.mock import patch | ||
|
||
from werkzeug.middleware.profiler import Profile | ||
from werkzeug.middleware.profiler import ProfilerMiddleware | ||
from werkzeug.test import Client | ||
|
||
|
||
def dummy_application(environ, start_response): | ||
start_response("200 OK", [("Content-Type", "text/plain")]) | ||
return [b"Foo"] | ||
|
||
|
||
def test_filename_format_function(): | ||
# This should be called once with the generated file name | ||
mock_capture_name = MagicMock() | ||
|
||
def filename_format(env): | ||
now = datetime.datetime.fromtimestamp(env["werkzeug.profiler"]["time"]) | ||
timestamp = now.strftime("%Y-%m-%d:%H:%M:%S") | ||
path = ( | ||
"_".join(token for token in env["PATH_INFO"].split("/") if token) or "ROOT" | ||
) | ||
elapsed = env["werkzeug.profiler"]["elapsed"] | ||
name = f"{timestamp}.{env['REQUEST_METHOD']}.{path}.{elapsed:.0f}ms.prof" | ||
mock_capture_name(name=name) | ||
return name | ||
|
||
client = Client( | ||
ProfilerMiddleware( | ||
dummy_application, | ||
stream=None, | ||
profile_dir="profiles", | ||
filename_format=filename_format, | ||
) | ||
) | ||
|
||
# Replace the Profile class with a function that simulates an __init__() | ||
# call and returns our mock instance. | ||
mock_profile = MagicMock(wraps=Profile()) | ||
mock_profile.dump_stats = MagicMock() | ||
with patch("werkzeug.middleware.profiler.Profile", lambda: mock_profile): | ||
client.get("/foo/bar") | ||
|
||
mock_capture_name.assert_called_once_with(name=ANY) | ||
name = mock_capture_name.mock_calls[0].kwargs["name"] | ||
mock_profile.dump_stats.assert_called_once_with(os.path.join("profiles", name)) |