Skip to content

Instantly share code, notes, and snippets.

@gezp
Last active Aug 23, 2021
Embed
What would you like to do?
test cpu/ram usgae for nav2 node
import sys
import time
import psutil
def get_process(name):
for p in psutil.process_iter(['name']):
if p.info['name'] == name:
return p
print("no process with name:",name)
sys.exit()
def get_nav_multi_processes():
localization_nodes = ['map_server', 'amcl']
navigation_nodes = ['controller_server', 'planner_server',
'recoveries_server', 'bt_navigator', 'waypoint_follower']
nodes = localization_nodes + navigation_nodes + ['lifecycle_manager']
processes = []
processes_name = []
for p in psutil.process_iter(['name']):
if p.info['name'] in nodes:
processes.append(p)
processes_name.append(p.name())
return processes, processes_name
def test_multi_node():
node_processes,processes_name = get_nav_multi_processes()
print("node num:",len(node_processes))
while(1):
cpu_percent = 0.0
cpu_percents = []
for p in node_processes:
cur_cpu = p.cpu_percent()
cpu_percent = cpu_percent + cur_cpu
cpu_percents.append(cur_cpu)
memory_percent =0.0
memory_percents = []
for p in node_processes:
cur_mem = p.memory_percent()
memory_percent = memory_percent + cur_mem
memory_percents.append(1.0*int(cur_mem*100)/100)
print(processes_name)
print(cpu_percents)
print(memory_percents)
print("cpu:",cpu_percent,"memory:",memory_percent)
print("------------------------------------------------")
time.sleep(2)
def test_composed_node():
p = get_process("loc_and_nav")
while(1):
print("cpu:",p.cpu_percent(),"memory:",p.memory_percent())
time.sleep(2)
#test_multi_node()
test_composed_node()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment