在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程序进行全面的性能测试和分析,从而优化代码性能。