-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jan Fiedler <[email protected]>
- Loading branch information
1 parent
916687e
commit 5bb70ed
Showing
3 changed files
with
110 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,55 @@ | ||
# Memray | ||
# Memray Profiling Plugin | ||
|
||
Memray tracks and reports memory allocations, both in python code and in compilled extension modules. | ||
This Memray Profiling plugin enables memory tracking on the Flyte task level and renders a memgraph profiling graph on Flyte Deck. | ||
|
||
To install the plugin, run the following command: | ||
|
||
```bash | ||
pip install flytekitplugins-memray | ||
``` | ||
|
||
Example | ||
```python | ||
from flytekit import workflow, task, ImageSpec | ||
from flytekitplugins.memray import memray_profiling | ||
import time | ||
|
||
|
||
image = ImageSpec( | ||
name="memray_demo", | ||
packages=["flytekitplugins_memray"], | ||
env={"PYTHONMALLOC": "malloc"}, | ||
registry="<your_cr_registry>", | ||
) | ||
|
||
|
||
def generate_data(n: int): | ||
leak_list = [] | ||
for _ in range(n): # Arbitrary large number for demonstration | ||
large_data = " " * 10**6 # 1 MB string | ||
leak_list.append(large_data) # Keeps appending without releasing | ||
time.sleep(0.1) # Slow down the loop to observe memory changes | ||
|
||
|
||
@task(container_image=image, enable_deck=True) | ||
@memray_profiling(memray_html_reporter="table") | ||
def memory_usage(n: int) -> str: | ||
generate_data(n=n) | ||
|
||
return "Well" | ||
|
||
|
||
@task(container_image=image, enable_deck=True) | ||
@memray_profiling(memray_reporter_args=["--leaks"]) | ||
def memory_leakage(n: int) -> str: | ||
generate_data(n=n) | ||
|
||
return "Well" | ||
|
||
|
||
@workflow | ||
def wf(n: int = 500): | ||
memory_usage(n=n) | ||
memory_leakage(n=n) | ||
``` |
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
39 changes: 39 additions & 0 deletions
39
plugins/flytekit-memray/flytekitplugins/memray/tests/test_memray_profiling.py
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,39 @@ | ||
from unittest.mock import Mock, patch | ||
import pytest | ||
from flytekit import task, current_context | ||
from flytekitplugins.memray import memray_profiling | ||
|
||
|
||
@task(enable_deck=True) | ||
@memray_profiling | ||
def heavy_compute(i: int) -> int: | ||
return i + 1 | ||
|
||
|
||
def test_local_exec(): | ||
heavy_compute(i=7) | ||
assert ( | ||
len(current_context().decks) == 6 | ||
) # memray flamegraph, timeline, input, and output, source code, dependencies | ||
|
||
|
||
def test_errors(): | ||
reporter = "summary" | ||
with pytest.raises( | ||
ValueError, match=f"{reporter} is not a supported html reporter." | ||
): | ||
memray_profiling(memray_html_reporter=reporter) | ||
|
||
reporter = "flamegraph" | ||
with pytest.raises( | ||
ValueError, | ||
match=f"unrecognized arguments for {reporter} reporter. Please check https://bloomberg.github.io/memray/{reporter}.html", | ||
): | ||
memray_profiling(memray_reporter_args=["--leaks", "trash"]) | ||
|
||
reporter = "flamegraph" | ||
with pytest.raises( | ||
ValueError, | ||
match=f"unrecognized arguments for {reporter} reporter. Please check https://bloomberg.github.io/memray/{reporter}.html", | ||
): | ||
memray_profiling(memray_reporter_args=[0, 1, 2]) |