在Ubuntu上進行Python性能測試,可以使用多種工具和方法。以下是一些常用的工具和步驟:
pip install py-spy
py-spy top --pid <pid>
py-spy record -o profile.txt --pid <pid>
py-spy top --pid <pid> --threads
pip3 install locust
locustfile.py
文件,定義用戶行為和請求。locust -f locustfile.py
http://localhost:8089
查看和管理測試。import cProfile
def func1():
# 這里放置你要測試的代碼
pass
cProfile.run('func1()', 'profile.txt')
import timeit
def test_function():
# 這里放置你要測試的代碼
pass
elapsed_time = timeit.timeit(test_function, number=1000)
print(f"函數執行時間: {elapsed_time} 秒")
通過這些工具和方法,你可以在Ubuntu上對Python程序進行全面的性能測試和分析,從而優化代碼性能。