Skip to content

Commit 4d0ab76

Browse files
Add useful_scripts/cpu_load.py
1 parent fe24d6f commit 4d0ab76

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

useful_scripts/cpu_load.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
3+
# This file is part of eRCaGuy_dotfiles: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
4+
5+
# STATUS: WORKS! TODO: UPDATE THE COMMENTS below and clean up the code.
6+
7+
# Print out basic stats on cpu load.
8+
9+
# INSTALLATION INSTRUCTIONS:#########3
10+
# 1. Install RipGrep: https://github.com/BurntSushi/ripgrep#installation
11+
# 2. Create a symlink in ~/bin to this script so you can run it from anywhere.
12+
# cd /path/to/here
13+
# mkdir -p ~/bin
14+
# ln -si "${PWD}/cpu_load.py" ~/bin/cpu_load # required
15+
# ln -si "${PWD}/cpu_load.py" ~/bin/gs_cpu_load # optional; replace "gs" with your initials
16+
# 3. Now you can use this command directly anywhere you like in any of these ways:
17+
# 1. `rgr`
18+
# 2. `rg_replace`
19+
# 1. `gs_rgr`
20+
# 3. `gs_rg_replace`
21+
22+
# References:
23+
# 1.
24+
25+
# Test commands:
26+
# eRCaGuy_dotfiles$ rgr foo -R boo
27+
# eRCaGuy_dotfiles$ rgr foo -R boo --stats "git & Linux cmds, help, tips & tricks - Gabriel.txt"
28+
29+
30+
##### https://unix.stackexchange.com/a/295608/114401 <========
31+
32+
import psutil
33+
34+
# ============= this works but lets do it differently ==============
35+
# MOVE THIS TO MY HELLO_WORLD REPO!
36+
# cpu_percent_total = psutil.cpu_percent(interval=2)
37+
# cpu_percent_cores = psutil.cpu_percent(percpu=True)
38+
# cpu_percent_total_str = ('%.2f' % cpu_percent_total) + "%"
39+
# cpu_percent_cores_str = [('%.2f' % x) + "%" for x in cpu_percent_cores]
40+
# print('Total: {}'.format(cpu_percent_total_str))
41+
# print('Individual CPUs: {}'.format(' '.join(cpu_percent_cores_str)))
42+
43+
# # Sanity check
44+
# avg = sum(cpu_percent_cores)/len(cpu_percent_cores)
45+
# print("DEBUG PRINT: avg = {:.2f}%".format(avg))
46+
# ==================================================================
47+
48+
t_measurement_sec = 2
49+
print("Measuring CPU load for {} seconds...".format(t_measurement_sec))
50+
cpu_percent_cores = psutil.cpu_percent(interval=t_measurement_sec, percpu=True)
51+
avg = sum(cpu_percent_cores)/len(cpu_percent_cores)
52+
cpu_percent_overall_str = ('%.2f' % avg) + '%'
53+
cpu_percent_cores_str = [('%.2f' % x) + '%' for x in cpu_percent_cores]
54+
print('Overall: {}'.format(cpu_percent_overall_str))
55+
print('Individual CPUs: {}'.format(' '.join(cpu_percent_cores_str)))
56+

0 commit comments

Comments
 (0)