Last active
May 28, 2023 13:45
-
-
Save jrfk/2236f6a1cac910fb92b426092a274f86 to your computer and use it in GitHub Desktop.
cpu_stats_by_async
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""This code is for monitoring CPU usage. It performs 3 minutes of polling and 3 minutes of monitoring. | |
During the 3 minutes of polling, it acquires CPU usage every second and adds it to a list. | |
During the 3 minutes of monitoring, it acquires CPU usage every second and adds it to a list. | |
Finally, it calculates and displays the average value and median value. | |
$ pip install statistics psutil | |
$ python cpu_stats_by_asyncio.py | |
Average CPU usage: 9.531666666666666% | |
Median CPU usage: 7.75% | |
""" | |
import asyncio | |
import psutil | |
import time | |
import statistics | |
POOL_TIME=180 # 3分間のポーリング 3分間のモニタリング | |
async def poll(): | |
end_time = time.time() + POOL_TIME # 3分間のポーリング | |
while time.time() < end_time: | |
await asyncio.sleep(1) | |
async def monitor_cpu_usage(cpu_percentages: list): | |
end_time = time.time() + POOL_TIME # 3分間のモニタリング | |
while time.time() < end_time: | |
cpu_percent = psutil.cpu_percent() | |
cpu_percentages.append(cpu_percent) | |
await asyncio.sleep(1) | |
async def main(): | |
cpu_percentages = [] | |
await asyncio.gather( | |
poll(), | |
monitor_cpu_usage(cpu_percentages), | |
) | |
avg = statistics.mean(cpu_percentages) | |
median = statistics.median(cpu_percentages) | |
print(f'Average CPU usage: {avg}%') | |
print(f'Median CPU usage: {median}%') | |
if __name__ == "__main__": | |
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""This code is for monitoring CPU usage. It performs 3 minutes of polling and 3 minutes of monitoring. | |
During the 3 minutes of polling, it acquires CPU usage every second and adds it to a list. | |
During the 3 minutes of monitoring, it acquires CPU usage every second and adds it to a list. | |
Finally, it calculates and displays the average value and median value. | |
$ pip install statistics psutil uvloop | |
$ python cpu_stats_by_asyncio_with_uvloop.py | |
Average CPU usage: 5.962777777777778% | |
Median CPU usage: 4.9% | |
""" | |
import asyncio | |
import uvloop | |
import psutil | |
import time | |
import statistics | |
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) | |
POOL_TIME=180 # 3分間のポーリング 3分間のモニタリング | |
async def poll(): | |
end_time = time.time() + POOL_TIME | |
while time.time() < end_time: | |
await asyncio.sleep(1) | |
async def monitor_cpu_usage(cpu_percentages: list): | |
end_time = time.time() + POOL_TIME | |
while time.time() < end_time: | |
cpu_percent = psutil.cpu_percent() | |
cpu_percentages.append(cpu_percent) | |
await asyncio.sleep(1) | |
async def main(): | |
cpu_percentages = [] | |
await asyncio.gather( | |
poll(), | |
monitor_cpu_usage(cpu_percentages), | |
) | |
avg = statistics.mean(cpu_percentages) | |
median = statistics.median(cpu_percentages) | |
print(f'Average CPU usage: {avg}%') | |
print(f'Median CPU usage: {median}%') | |
if __name__ == "__main__": | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment