foreword
In the process of testing a job-level application, it is inevitable to test some performance of the service, such as the amount of cpu usage, the size of the memory used, etc. The simpler way is to create a parallel thread in the service, each Print the size of these concerns every once in a while, and then sort the printed values in chronological order, and draw a picture to see the rules
Here to calculate these quantities, there is a library in python that is psutil
psutil
Install
pip install can
pip install psutil
doc:https://psutil.readthedocs.io/en/latest/#
Introduction to common methods
1. cpu_percent
doc:https://psutil.readthedocs.io/en/latest/#psutil.cpu_percent
cpu usage, the return is a number from 0-100, the unit is %
interval: seconds percpu: True/False True for each cpu, False for total cpu
Pay special attention here when the interval is 0, the first call returns 0
print(psutil.cpu_percent(interval=30))
print(psutil.cpu_percent(interval=10, percpu=True))
result:
8.3
[31.2, 0.8, 27.0, 0.7, 22.2, 0.8, 18.9, 0.8, 16.8, 0.8, 13.5, 0.9]
There are 12 cpus shown here, if it is a mac, execute
sysctl machdep.cpu
You can see the information of the cpu
# 总核数 = 物理CPU个数 X 每颗物理CPU的核数
# 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数
core_count: 物理cpu的核数
thread_count: 逻辑cpu的数量
psutil.cpu_count
print(psutil.cpu_count(logical=False)) #6
print(psutil.cpu_count(logical=True)) #12
The default logical is True, returns the number of logical cpus, if it is False, returns the number of physical cores
The official website writes more clearly:
Return the number of logical CPUs in the system (same as os.cpu_count in Python 3.4). “logical CPUs” means the number of physical cores multiplied by the number of threads that can run on each core (this is known as Hyper Threading). If logical is False return the number of physical cores only.
3. psutil.virtual_memory
returns many values
s = psutil.virtual_memory()
print(s)
This command is similar to the free command in linux: https://blog.csdn.net/lijinzhou2017/article/details/103008827
total The total available physical memory size of the system
available The physical memory size that can also be used by the application
used The physical memory size that has been used
free How much physical memory is available
percent (total-available)/total
The units are all bytes, so if you want to display G when displaying, you need to divide by units = 1024✖️1024✖️1024
If you just want to know how much physical memory is left in a cross-platform way, just rely on available and percent
reference
https://www.cnblogs.com/feibazhf/p/12858792.html
Recommended reading:
My 2022 Internet School Recruitment Sharing
Talking about the difference between algorithm post and development post
Internet school recruitment research and development salary summary
The 2022 Internet job hunting status, gold 9 silver 10 will soon become copper 9 iron 10! !
Public number: AI snail car
Stay humble, stay disciplined, keep improving
Send [Snail] to get a copy of "Hands-on AI Project" (written by AI Snail Car)
Send [1222] to get a good leetcode brushing notes
Send [AI Four Classics] Get four classic AI e-books