Bugy自制代码用于在Bash中对内存和时间进行基准测试

时间:2018-03-18 17:50:40

标签: bash if-statement memory-management resources ps

Bash代码

我编写了这个小代码来测量进程内存使用的时间和峰值。

# Get arguments
MaxMemory="$1"
MaxTime="$2"
Command="$3"
for (( i=4 ; i<="$#"; i++)); do
    Command="${Command} ${!i}"
done

echo -e "MaxMemory = ${MaxMemory}\nMaxTime = ${MaxTime}\nCommand = ${Command}"


#### run the given Command in the background
${Command} &


#### Get pid
pid=$!
echo "pid = ${pid}"


#### Monitor resources
MemoryPeak=0
timeBefore=$(date +"%s")
while true;do
        # Get memory
        mem=$(ps -o rss,pid | grep ${pid} | awk '{print $1}')

        # Break if the process has stopped running
        if [[ ${mem} == "" ]]; then
                #echo "process has stopped"
                break
        fi

        # Set the MemoryPeak of memory
        if [ "${mem}" -gt "${MemoryPeak}" ]; then
                MemoryPeak=$mem
        fi

        # If it consumed too much memory, then kill
        if [ "${MemoryPeak}" -gt "${MaxMemory}" ];then
                #echo "process consumed too much memory"
                kill ${pid}
                break
        fi

        # If it consumed too much CPU time, then kill
        timeAfter=$(date +"%s")
        timeUsage=$((timeAfter - timeBefore))
        if [ "${timeUsage}" -gt "${MaxTime}" ];then
                #echo "process consumed too much time"
                kill ${pid}
                break
        fi

        # sleep
        sleep 0.1
done

timeAfter=$(date +"%s")
timeUsage=$((timeAfter - timeBefore))

echo "MEM ${MemoryPeak} TIME ${timeUsage}"

名为memAndTime的命令与

一样
memAndTime ${MaxMemory} ${MaxTime} ./myProcess arg1 arg2 arg3

问题

我不是来讨论我没有使用timeoutgtimeoutmemusgulimit的原因。

代码有时会发送以下一系列错误消息

264: integer expression expected
/usr/bin/memAndTime: line 36: [: 473268
644
912
372: integer expression expected
/usr/bin/memAndTime: line 36: [: 705108
652: integer expression expected
/usr/bin/memAndTime: line 36: [: 842760
384: integer expression expected

第36行

if [ "${mem}" -gt "${MemoryPeak}" ]; then

感觉mem中内置的对象mem=$(ps -o rss,pid | grep ${pid} | awk '{print $1}')有时会以列开头。

我无法在使用此命令之外重现错误消息。此外,错误消息非常罕见,难以重现。我不明白是什么导致了它。你能帮忙找出这个错误信息的来源以及如何修复它吗?

1 个答案:

答案 0 :(得分:1)

在shell中尝试此命令:

[ s -gt 1 ]

输出应该是这样的:

-bash: [: s: integer expression expected

即:一行错误消息

我们在您的输出中看到了什么?

/usr/bin/memAndTime: line 36: [: 705108
652: integer expression expected

这是2行!怎么会这样?

此命令后mem中有多少行?

mem=$(ps -o rss,pid | grep ${pid} | awk '{print $1}')

你可能认为它是一个,但它可以更多。这是一种表面的方式,以确保它始终包含至多一个值:

mem=$(ps -o rss,pid | grep ${pid} | awk '{print $1; exit}')

这是一种更好,更强大的方式:

mem=$(ps -o rss= -p $pid)

=参数中的-o可用于指定自定义标头。只是附加=表示空标题。如果所有列的标题都为空,则输出中将省略标题,因此需要其他进程(如tail或其他进程)将其删除。