Linux GNU time 評估程序性能表現(xiàn)

在開發(fā)完程序后可能需要對程序進行性能評估,比方說運行速度和內(nèi)存占用在不同輸入和參數(shù)情況下的表現(xiàn)。在 Linux 使用 GNU time 可以很輕松地測量到這些指標(biāo),為什么說 GNU time 呢?因為系統(tǒng)可能有 2 個 time 命令,一個是 shell 內(nèi)置的,一個是 GNU time 命令,后者路徑往往是 /usr/bin/time 可通過路徑調(diào)用。

# shell 內(nèi)置 time 
$ type time
time is a shell keyword

# GNU time
$ which time
/usr/bin/time

兩個 time 命令的輸入格式都是:

time [option ...] command [argument ...]

time 命令本身 + time 命令參數(shù) + 要測量的命令(程序)及其參數(shù)。

直接調(diào)用 time 命令時可能會調(diào)用到 shell 內(nèi)置的,建議使用 /usr/bin/time 路徑調(diào)用。

# shell 內(nèi)置 time 
$ time echo "just test"
just test

real    0m0.000s
user    0m0.000s
sys 0m0.000s

# GNU time
$ /usr/bin/time echo "just test"
just test
0.00user 0.00system 0:00.00elapsed 50%CPU (0avgtext+0avgdata 1792maxresident)k
0inputs+0outputs (0major+80minor)pagefaults 0swaps

GNU time 默認(rèn)輸出格式為:

%Uuser %Ssystem %Eelapsed %PCPU (%Xtext+%Ddata %Mmax)k
%Iinputs+%Ooutputs (%Fmajor+%Rminor)pagefaults %Wswaps

可通過 -f 參數(shù)修改輸出格式,如:

$ /usr/bin/time -f "%e elapsed %E user" echo "just test"
just test
0.00 elapsed 0:00.00 user

使用 -v 參數(shù)報告所有統(tǒng)計信息,我一般是這么用的,因為一般 benchmark 都是要進行大量不同條件的測試,而且要有重復(fù)。不如輸出所有統(tǒng)計保存到文件,再寫個腳本去匯總統(tǒng)計。

$ /usr/bin/time -v echo "just test"
just test
    Command being timed: "echo just test"
    User time (seconds): 0.00
    System time (seconds): 0.00
    Percent of CPU this job got: 100%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 1792
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 81
    Voluntary context switches: 1
    Involuntary context switches: 0
    Swaps: 0
    File system inputs: 0
    File system outputs: 0
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0

其中 "Maximum resident set size" 指命令最大內(nèi)存占用;"Elapsed (wall clock) time" 是命令現(xiàn)實的耗時;"User time" 和 "System time" 加起來是 CPU time。

內(nèi)存占用多進程問題
如果使用多進程要注意最大內(nèi)存占用是否是所有進程的加總,下面用一個測試腳本說明可能的問題,用 GNU time 測量結(jié)果與 psutil 測量結(jié)果進行比較。

將下面代碼寫入 MemoryTest.py 文件,代碼定義一個 use_memory 函數(shù)會創(chuàng)建一個包含 1000000 隨機浮點數(shù)的列表。然后分別測試使用和不使用多進程時最大內(nèi)存占用。

import multiprocessing
import random
import time

def use_memory(i):
    random.seed(1001)
    random_list = [random.random() for _ in range(1000000)]
    time.sleep(10)
    return None

# 非多進程測試時使用
use_memory(0)

# 多進程測試時使用
with multiprocessing.Pool(4) as poool:
    poool.map(use_memory, range(4))

使用 time 命令測試不使用多進程的結(jié)果如下:

$ /usr/bin/time -f %M python MemoryTest.py 
403584

默認(rèn)單位為 Kbytes。

使用多進程(4 進程)的結(jié)果:

$ /usr/bin/time -f %M python MemoryTest.py 
401632

兩個結(jié)果相近,這不符合預(yù)期,莫非 time 只是測量一個子進程,沒有給所有的加總處理?

作為對比,寫一個 GetMemory.py 腳本,腳本里使用 psutil 每隔 1 秒對命令測量一次內(nèi)存占用,并輸出最大的占用值。

import psutil
import subprocess
import time

def peak_memory(process: psutil.Process) -> int:
    rss = process.memory_info().rss
    for child_process in process.children():
        rss += child_process.memory_info().rss
    return rss

if __name__ == "__main__":
    run_cmd = subprocess.Popen(["python", "MemoryTest.py"])
    process = psutil.Process(run_cmd.pid)

    max_memory = 0
    # 只要 MemoryTest.py 沒結(jié)束就每隔 1 秒鐘測量一次內(nèi)存占用
    while run_cmd.poll() is None:
        each_memory = peak_memory(process)
        if each_memory > max_memory:
            max_memory = each_memory
        time.sleep(1)
    
    print(f"Peak Memory (Kbytes): {int(max_memory / 1024)}")

腳本使用 poll 函數(shù)檢測命令是否完成,不使用 psutilis_running 函數(shù),因為在 while 循環(huán)使用后者,會導(dǎo)致命令完成后由于主程序未接受退出狀態(tài)并處理,命令變成 zombie 狀態(tài)。由于 psutil.Process 是跟 pid 綁定的,而系統(tǒng) pid 是會被重復(fù)使用的,所以用這個模塊要注意操作。

使用 psutil 測量時,不使用多進程結(jié)果:

$ python GetMemory.py 
Peak Memory (Kbytes): 403712

跟之前使用 time 測量結(jié)果相近。

使用多進程結(jié)果:

$ python GetMemory.py 
Peak Memory (Kbytes): 1620840

大小約是不使用多進程的 4 倍,這也是設(shè)置的進程數(shù),看起來是合理的。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容