跳转到主要内容

用于跟踪软件内存和时间需求的Python工具。

项目描述

Pypi project Pypi total project downloads

内存时间追踪器是一个简单的Python工具,用于通过自适应日志分辨率跟踪软件在非常短暂(毫秒)和较长(天数)时间以及内存需求。

报告是CSV文件,包含两列,第一列是时间差,第二列是所需的RAM。

向上追踪到崩溃

此包优雅地处理了跟踪的软件由于内存不足或通常崩溃的情况,当正常完成执行时,在它产生的CSV文档的最后一行添加“0,0”,当执行以可检测的异常结束时,添加“-1,-1”。当存在无法通过异常检测的崩溃时,例如由于内存不足而导致的机器冻结、内核崩溃或其他情况,CSV的末尾既不会写入“0,0”,也不会写入“1,1”。

为了帮助区分不同的可能完成状态,我们准备了三种方法

  • has_completed_successfully 检测执行是否顺利完成。

  • has_crashed_gracefully 检测引发“正常”异常的崩溃。

  • has_crashed_ungracefully 检测未引发“正常”异常的崩溃,例如内存不足和核心转储。

更多内容请参阅下面的示例部分。

需求

请注意,此包使用 proc/meminfo,因此它仅与Linux系统严格兼容。

与大多数跟踪系统一样,如果系统中的噪音有限,此系统效果最佳。在运行基准测试时不要运行其他软件,否则结果可能会偏差。

安装包

通常情况下,要从Pypi安装此包,只需运行

pip install memory_time_tracker

用法示例

您可以使用此包跟踪给定方法的执行,方法如下

from memory_time_tracker import Tracker, has_completed_successfully, has_crashed_gracefully, has_crashed_ungracefully, plot_reports
from time import sleep
import numpy as np

def example_function():
    """Small example of function that takes 2 seconds."""
    arrays = []
    for _ in range(10):
        arrays.append(np.zeros((10000, 10000)))
        sleep(0.2)

def example_function_which_crashes():
    """Small example of function that takes 2 seconds and crashes."""
    arrays = []
    for _ in range(20):
        arrays.append(np.zeros((10000, 5000)))
        sleep(0.1)
    raise ValueError("Argh! I'm crashig!")

# The path where we will store the log
path1 = "/tmp/tracker_example.csv"
# The path where we will store the log with the crash
path2 = "/tmp/tracker_example_with_crash.csv"

# Create the tracker context
with Tracker(path1):
    example_function()

# Wait between tracking to allow for memory to free
sleep(20)

# Create the tracker context to handle crashable libraries
try:
    with Tracker(path2, verbose=True):
        example_function_which_crashes()
except Exception:
    pass

print(
    "Successful: ", has_completed_successfully(path1),
    "Crashed gracefully: ", has_crashed_gracefully(path1),
    "Crashed ungracefully: ", has_crashed_ungracefully(path1)
)
# Successful:  True Crashed gracefully:  False Crashed ungracefully:  False

print(
    "Successful: ", has_completed_successfully(path2),
    "Crashed gracefully: ", has_crashed_gracefully(path2),
    "Crashed ungracefully: ", has_crashed_ungracefully(path2)
)
# Successful:  False Crashed gracefully:  True Crashed ungracefully:  False

plot_reports([path1, path2])

您可以在此处运行它

上面的示例应生成如下图片

Alternative text

请注意,由于是在COLAB上执行,RAM和时间测量中存在一些噪声。

作者和许可证

此包由Luca CappellettiTommaso Fontana开发,并按照MIT许可证发布。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

memory_time_tracker-0.0.5.tar.gz (8.5 kB 查看哈希值)

上传时间

由以下支持