-
Notifications
You must be signed in to change notification settings - Fork 728
test: add memory test and benchmark utilities for Python #5461
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
269a658
start package
wjones127 e4069fc
get tests passing
wjones127 b33e321
cleanup
wjones127 9ebdb37
simplify
wjones127 8259074
wip
wjones127 03b419b
try this
wjones127 9d6b55e
python test
wjones127 fd9c04c
fix missing API
wjones127 131beaf
wip
wjones127 57410be
better benchmark
wjones127 c4bdea3
upload
wjones127 12906e5
cleanup
wjones127 787f9c0
this is fixed
wjones127 260891c
cleanup
wjones127 ee9e68d
fix installation
wjones127 a0bee28
fix tests
wjones127 1ed1dcb
set shell
wjones127 fe513cf
pr feedback
wjones127 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,19 @@ | ||
| # Rust | ||
| target/ | ||
| Cargo.lock | ||
|
|
||
| # Python | ||
| __pycache__/ | ||
| *.py[cod] | ||
| *$py.class | ||
| *.so | ||
| .pytest_cache/ | ||
| *.egg-info/ | ||
| dist/ | ||
| build/ | ||
|
|
||
| # IDE | ||
| .vscode/ | ||
| .idea/ | ||
| *.swp | ||
| *.swo |
This file contains hidden or 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,23 @@ | ||
| [workspace] | ||
|
|
||
| [package] | ||
| name = "lance-memtest" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| authors = ["Lance Developers"] | ||
| description = "Memory allocation testing utilities for Python" | ||
| license = "Apache-2.0" | ||
|
|
||
| [lints.clippy] | ||
| arithmetic_side_effects = "deny" | ||
|
|
||
| [lib] | ||
| name = "memtest" | ||
| crate-type = ["cdylib", "rlib"] | ||
|
|
||
| [dependencies] | ||
| libc = "0.2" | ||
|
|
||
| [profile.release] | ||
| lto = true | ||
| codegen-units = 1 |
This file contains hidden or 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,29 @@ | ||
| .PHONY: build test lint format clean | ||
|
|
||
| build: | ||
| cargo build | ||
| cp target/debug/libmemtest.so python/memtest/ | ||
| pip install -e . | ||
|
|
||
| build-release: | ||
| cargo build --release | ||
| cp target/release/libmemtest.so python/memtest/ | ||
| pip install -e . | ||
|
|
||
| test: | ||
| LD_PRELOAD=./python/memtest/libmemtest.so pytest python/tests/ -v | ||
|
|
||
| lint: | ||
| cargo clippy -- -D warnings | ||
| ruff check python/ | ||
|
|
||
| format: | ||
| cargo fmt | ||
| ruff format python/ | ||
|
|
||
| clean: | ||
| cargo clean | ||
| rm -rf target/ | ||
| find . -type d -name __pycache__ -exec rm -rf {} + | ||
| find . -type f -name "*.pyc" -delete | ||
| find . -type f -name "*.so" -delete |
This file contains hidden or 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,35 @@ | ||
| # lance-memtest | ||
|
|
||
| Memory allocation testing utilities for Python test suites. This package provides tools to track memory allocations made by the Python interpreter and any Python libraries during test execution. | ||
|
|
||
| ## Usage | ||
|
|
||
| Install with: | ||
|
|
||
| ```shell | ||
| make build-release | ||
| ``` | ||
|
|
||
| To activate the memory tracking, you need to set the `LD_PRELOAD` environment variable: | ||
|
|
||
| ```shell | ||
| export LD_PRELOAD=$(lance-memtest) | ||
| ``` | ||
|
|
||
| Then you can write Python code that tracks memory allocations: | ||
|
|
||
| ```python | ||
| import memtest | ||
|
|
||
| def test_memory(): | ||
| with memtest.track() as get_stats: | ||
| # Your code that allocates memory | ||
| data = [0] * 1000000 | ||
|
|
||
| stats = get_stats() | ||
| assert stats['peak_bytes'] < 10**7 # Assert peak memory usage | ||
| ``` | ||
|
|
||
| ## How this works | ||
|
|
||
| The library uses dynamic linking to intercept memory allocation calls (like `malloc`, `free`, etc.) made by the Python interpreter and its extensions. It keeps track of the total number of allocations, deallocations, and the peak memory usage during the execution of your code. |
This file contains hidden or 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,37 @@ | ||
| [build-system] | ||
| requires = ["setuptools>=61.0", "wheel"] | ||
| build-backend = "setuptools.build_meta" | ||
|
|
||
| [project] | ||
| name = "lance-memtest" | ||
| version = "0.1.0" | ||
| description = "Memory allocation testing utilities for Python test suites" | ||
| readme = "README.md" | ||
| requires-python = ">=3.9" | ||
| license = { text = "Apache-2.0" } | ||
| authors = [ | ||
| { name = "Lance Developers" } | ||
| ] | ||
| classifiers = [ | ||
| "Development Status :: 3 - Alpha", | ||
| "Intended Audience :: Developers", | ||
| "License :: OSI Approved :: Apache Software License", | ||
| "Programming Language :: Python :: 3", | ||
| "Programming Language :: Python :: 3.9", | ||
| "Programming Language :: Python :: 3.10", | ||
| "Programming Language :: Python :: 3.11", | ||
| "Programming Language :: Python :: 3.12", | ||
| "Programming Language :: Rust", | ||
| ] | ||
|
|
||
| [project.scripts] | ||
| lance-memtest = "memtest.__main__:main" | ||
|
|
||
| [tool.setuptools] | ||
| packages = ["memtest"] | ||
|
|
||
| [tool.setuptools.package-dir] | ||
| memtest = "python/memtest" | ||
|
|
||
| [tool.setuptools.package-data] | ||
| memtest = ["*.so", "*.dylib", "*.dll"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.