Skip to content

Commit

Permalink
add memray example
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Fiedler <[email protected]>
  • Loading branch information
fiedlerNr9 committed Nov 8, 2024
1 parent 9af4cac commit 914d047
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
27 changes: 27 additions & 0 deletions examples/memray_plugin/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM python:3.11-slim-bookworm
LABEL org.opencontainers.image.source=https://github.com/flyteorg/flytesnacks

WORKDIR /root
ENV VENV /opt/venv
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV PYTHONPATH /root

WORKDIR /root

ENV VENV /opt/venv
# Virtual environment
RUN python3 -m venv ${VENV}
ENV PATH="${VENV}/bin:$PATH"

# Install Python dependencies
COPY requirements.in /root
RUN pip install -r /root/requirements.in

# Copy the actual code
COPY . /root

# This tag is supplied by the build script and will be used to determine the version
# when registering tasks, workflows, and launch plans
ARG tag
ENV FLYTE_INTERNAL_IMAGE $tag
20 changes: 20 additions & 0 deletions examples/memray_plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(memray_plugin)=

# Memray Profiling

```{eval-rst}
.. tags:: Integration, Profiling, Observability
```

Memray tracks and reports memory allocations, both in python code and in compiled extension modules.
This Memray Profiling plugin enables memory tracking on the Flyte task level and renders a memgraph profiling graph on Flyte Deck.

First, install the Memray plugin:

```bash
pip install flytekitplugins-memray
```

```{auto-examples-toc}
memray_example
```
Empty file.
66 changes: 66 additions & 0 deletions examples/memray_plugin/memray_plugin/memray_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# %% [markdown]
# (memray_example)=
#
# # Memray Profiling Example
# Memray tracks and reports memory allocations, both in python code and in compiled extension modules.
# This Memray Profiling plugin enables memory tracking on the Flyte task level and renders a memgraph profiling graph on Flyte Deck.
# %%
from flytekit import workflow, task, ImageSpec
from flytekitplugins.memray import memray_profiling
import time

# %% [markdown]
# First, we use `ImageSpec` to construct a container that contains the dependencies for the
# tasks, we want to profile:
# %%
image = ImageSpec(
name="memray_demo",
packages=["flytekitplugins_memray"],
registry="<your_cr_registry>",
)


# %% [markdown]
# Next, we define a dummy function that generates data in memory without releasing:
# %%
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


# %% [markdown]
# Example of profiling the memory usage of `generate_data()` via the memray `table` html reporter
# %%
@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"


# %% [markdown]
# Example of profiling the memory leackage of `generate_data()` via the memray `flamegraph` html reporter
# %%


@task(container_image=image, enable_deck=True)
@memray_profiling(trace_python_allocators=True, memray_reporter_args=["--leaks"])
def memory_leakage(n: int) -> str:
generate_data(n=n)

return "Well"


# %% [markdown]
# Put everything together in a workflow.
# %%


@workflow
def wf(n: int = 500):
memory_usage(n=n)
memory_leakage(n=n)
1 change: 1 addition & 0 deletions examples/memray_plugin/requirements.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flytekitplugins-memray

0 comments on commit 914d047

Please sign in to comment.