Last active
April 20, 2017 08:00
-
-
Save endofline/33be538787426db1fa91af9dbca05d1e to your computer and use it in GitHub Desktop.
hangoutsbot cpu usage logger
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
import asyncio | |
import logging | |
import time | |
import os | |
logger = logging.getLogger(__name__) | |
import plugins | |
INTERVAL = 300 | |
def _initialise(bot): | |
plugins.start_asyncio_task(tick) | |
plugins.register_admin_command(["cpuusage"]) | |
def cpuusage(bot, event, *args): | |
process_start = time.process_time() | |
monotonic_start = time.monotonic() | |
yield from asyncio.sleep(10) | |
process_diff = time.process_time() - process_start | |
monotonic_diff = time.monotonic() - monotonic_start | |
cpu = process_diff / monotonic_diff * 100 # percent | |
logger.info("[{}] cpu = {}%".format(os.getpid(), round(cpu,2))) | |
@asyncio.coroutine | |
def tick(bot): | |
monotonic_last = 0 | |
while True: | |
if time.monotonic() - monotonic_last > INTERVAL: | |
yield from cpuusage(bot, False, False) | |
monotonic_last = time.monotonic() | |
yield from asyncio.sleep(30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment