

GitHub - bloomberg/pytest-memray: Memray is a memory profiler for Python
source link: https://github.com/bloomberg/pytest-memray
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

pytest-memray
pytest-memray is a pytest plugin for easy integration of memray
.
Installation
pytest-memray requires Python 3.7+ and can be easily installed using most common Python packaging tools. We recommend installing the latest stable release from PyPI with pip:
pip install pytest-memray
Documentation
You can find the latest documentation available here.
Usage
To use the plugin in a pytest run, simply add --memray
to the command line invocation:
pytest --memray tests/
Would produce a report like:
python3 -m pytest tests --memray
=============================================================================================================================== test session starts ================================================================================================================================
platform linux -- Python 3.8.10, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /mypackage, configfile: pytest.ini
plugins: cov-2.12.0, memray-0.1.0
collected 21 items
tests/test_package.py ..................... [100%]
================================================================================================================================= MEMRAY REPORT ==================================================================================================================================
Allocations results for tests/test_package.py::some_test_that_allocates
📦 Total memory allocated: 24.4MiB
📏 Total allocations: 33929
📊 Histogram of allocation sizes: |▂ █ |
🥇 Biggest allocating functions:
- parse:/opt/bb/lib/python3.8/ast.py:47 -> 3.0MiB
- parse:/opt/bb/lib/python3.8/ast.py:47 -> 2.3MiB
- _visit:/opt/bb/lib/python3.8/site-packages/astroid/transforms.py:62 -> 576.0KiB
- parse:/opt/bb/lib/python3.8/ast.py:47 -> 517.6KiB
- __init__:/opt/bb/lib/python3.8/site-packages/astroid/node_classes.py:1353 -> 512.0KiB
Configuration
This plugin provides a clean minimal set of command line options that are added to pytest.
--memray
: Activate memray tracking.--most-allocations=MOST_ALLOCATIONS
: Show the N tests that allocate most memory (N=0 for all).--hide-memray-summary
: Hide the memray summary at the end of the execution.
Markers
There are some builtin markers and fixtures in pytest-memray
:
limit_memory
When this marker is applied to a test, it will cause the test to fail if the execution of the test allocates more memory than allowed. It takes a single argument with a string indicating the maximum memory that the test can allocate.
The format for the string is <NUMBER> ([KMGTP]B|B)
. The marker will raise
ValueError if the string format cannot be parsed correctly.
Example of usage:
@pytest.mark.limit_memory("24 MB") def test_foobar(): # do some stuff that allocates memory
License
pytest-memray is Apache-2.0 licensed, as found in the LICENSE file.
Code of Conduct
This project has adopted a Code of Conduct. If you have any concerns about the Code, or behavior which you have experienced in the project, please contact us at [email protected].
Security Policy
If you believe you have identified a security vulnerability in this project, please send email to the project team at [email protected], detailing the suspected issue and any methods you've found to reproduce it.
Please do NOT open an issue in the GitHub repository, as we'd prefer to keep vulnerability reports private until we've had an opportunity to review and address them.
Contributing
We welcome your contributions to help us improve and extend this project!
Below you will find some basic steps required to be able to contribute to the project. If you have any questions about this process or any other aspect of contributing to a Bloomberg open source project, feel free to send an email to [email protected] and we'll get your questions answered as quickly as we can.
Contribution Licensing
Since this project is distributed under the terms of an open source license, contributions that you make are licensed under the same terms. In order for us to be able to accept your contributions, we will need explicit confirmation from you that you are able and willing to provide them under these terms, and the mechanism we use to do this is called a Developer's Certificate of Origin (DCO). This is very similar to the process used by the Linux(R) kernel, Samba, and many other major open source projects.
To participate under these terms, all that you must do is include a line like the following as the last line of the commit message for each commit in your contribution:
Signed-Off-By: Random J. Developer <[email protected]>
The simplest way to accomplish this is to add -s
or --signoff
to your git commit
command.
You must use your real name (sorry, no pseudonyms, and no anonymous contributions).
Steps
- Create an Issue, selecting 'Feature Request', and explain the proposed change.
- Follow the guidelines in the issue template presented to you.
- Submit the Issue.
- Submit a Pull Request and link it to the Issue by including "#" in the Pull Request summary.
Recommend
-
36
emacs-python-pytest - run pytest inside emacs
-
38
README.md
-
12
Profiler week 5: Mac support, experiments profiling memory allocations Hello! Week 5 of profiler work is over. as a reminder – what I’ve been doing is building a new sampling CPU profiler for Ruby! It’s called rbs...
-
14
scalene: a high-performance CPU, GPU and memory profiler for Python by Emery Berger, Sam Stern, and Juan Altmaye...
-
12
bloomberg/memray: Memray is a memory profiler for Python README.md ...
-
6
Memray首页、文档和下载 - Python 内存分析器 - OSCHINA - 中文开源技术交流社区 ...
-
4
memray: Python的内存分析器 Memray 是 Python 的内存分析器。它可以跟踪 Python 代码、本机扩展模块和 Python 解释器本身中的内存分配。它可以生成几种不同类型的报告来帮助您分析捕获的内存...
-
8
memray Contents Menu Expand Light mode Dark mode Auto light/dark mode Hide navigation sidebar Hide table of conten...
-
8
畫 Python 下記憶體使用情況的 Flamegraph:Memray 前幾天的 Hacker News Daily 上看到的東西,是由 Bloomberg 開發出來的工具
-
5
Memray: The endgame Python memory profiler Episode #425, published Fri, Aug 4,...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK