【问题标题】:Comparing memory usage of shell script and Python script for same task比较同一任务的 shell 脚本和 Python 脚本的内存使用情况
【发布时间】:2017-09-14 08:24:54
【问题描述】:

假设有两个脚本:(a) 一个调用 JAVA jar 的 bash shell 脚本(以下称为 my_shell_script),以及 (b) 一个从其他 Python 包导入函数但确实如此的 Python 脚本不要调用任何非 Python 包或软件(以下称为my_Python_script)。这两个脚本的目的相同:它们采用相同的输入(以下称为testinput)并生成大致相同的输出。

我想测量和比较两个脚本的内存使用情况,作为它们执行时的时间函数。

为此,我使用massif(将time_unit 设置为毫秒)通过valgrind 执行每个脚本,然后是通过ms_print 输出的massif 的摘要。

INF=testinput

# Testing Shell script
valgrind --tool=massif --pages-as-heap=yes --time-unit=ms --massif-out-file=${INF}_shell.out bash my_shell_script -f $INF -j my_java_jar
ms_print --threshold=50.0 ${INF}_shell.out > ${INF}_shell.summary

# Testing Python script
valgrind --tool=massif --pages-as-heap=yes --time-unit=ms --massif-out-file=${INF}_Python.out python2 my_python_script $INF
ms_print --threshold=50.0 ${INF}_Python.out > ${INF}_Python.summary

虽然 valgrind/massif 记录了 my_python_script 的内存使用情况,这与我通过 htop 看到的大致一致,但 my_shell_script 的情况并非如此。 htop 的统计数据表明在执行 my_shell_script 期间使用了 GB 的内存,而 valgrind/massif 记录仅使用了几十 MB 的内存。

因此,我怀疑 valgrind/massif 记录了执行 bash 代码的内存使用情况,但没有记录 bash 代码正在调用的 JAVA jar。

如何正确测量my_shell_script 的内存使用量随时间的变化?

【问题讨论】:

    标签: python shell memory valgrind performance-testing


    【解决方案1】:

    选项 --trace-children=yes|no 表示跟踪(或不跟踪)孩子。默认值为 no,表示执行脚本的 shell 将 在 valgrind 下,但不在启动的 python 或 java 进程下。

    所以,指定 --trace-children=yes。

    请务必使用例如massif out 文件参数中的 %p,否则所有 进程(例如 shell 及其子进程)将写入同一个文件,这将无法正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 2013-05-01
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多