Skip to content

Instantly share code, notes, and snippets.

@Kwarf

Kwarf/pirrd.py Secret

Created February 10, 2015 18:22
Show Gist options
  • Save Kwarf/e2f7fcb0feb713fde5be to your computer and use it in GitHub Desktop.
Save Kwarf/e2f7fcb0feb713fde5be to your computer and use it in GitHub Desktop.
Quick hack to benchmark Raspberry Pi 2
#!/usr/bin/python2
# !!! IMPORTANT !!!
# This is just something I threw together when benchmarking my Raspberry Pi 2
# don't expect it to work on anything else.
# Check out my blog post for more info:
# http://blog.kwarf.com/2015/02/arch-and-kodi-on-the-raspberry-pi-2/
# The graphs in the post were generated using the following commands:
# rrdtool graph pinetwork.png -W http://blog.kwarf.com/2015/02/arch-and-kodi-on-the-raspberry-pi-2/ -s end-90s -e `rrdtool last pi.rrd` -w 900 -h 200 DEF:downbytes=pi.rrd:down:AVERAGE CDEF:down=downbytes,8,* AREA:down#00ff00:down DEF:upbytes=pi.rrd:up:AVERAGE CDEF:up=upbytes,8,* AREA:up#ff0000:up
# rrdtool graph picores.png -W http://blog.kwarf.com/2015/02/arch-and-kodi-on-the-raspberry-pi-2/ -s end-90s -e `rrdtool last pi.rrd` -w 900 -h 200 -l 0 -u 100 -r DEF:core0=pi.rrd:core0:AVERAGE LINE:core0#ff0000:core0 DEF:core1=pi.rrd:core1:AVERAGE LINE:core1#00ff00:core1 DEF:core2=pi.rrd:core2:AVERAGE LINE:core2#0000ff:core2 DEF:core3=pi.rrd:core3:AVERAGE LINE:core3#ffff00:core3
# rrdtool graph piperf.png -W http://blog.kwarf.com/2015/02/arch-and-kodi-on-the-raspberry-pi-2/ -s end-90s -e `rrdtool last pi.rrd` -w 900 -h 200 -l 0 -u 100 -r DEF:core0=pi.rrd:core0:AVERAGE DEF:core1=pi.rrd:core1:AVERAGE DEF:core2=pi.rrd:core2:AVERAGE DEF:core3=pi.rrd:core3:AVERAGE CDEF:cpu=core0,core1,+,core2,+,core3,+,4,/ LINE:cpu#00ff00:cpu DEF:mem=pi.rrd:mem:AVERAGE LINE:mem#0000ff:mem DEF:temp=pi.rrd:temp:AVERAGE LINE:temp#ff0000:temp
import os
import psutil
import rrdtool
import threading
FILENAME = "pi.rrd"
def core_temp():
path = "/sys/class/thermal/thermal_zone0/temp"
return int(open(path, "r").read()) / 1000
def tick():
threading.Timer(1, tick).start()
cpu = psutil.cpu_percent(percpu=True)
mem = psutil.phymem_usage().percent
temp = core_temp()
net = psutil.network_io_counters(pernic=True).get("eth0")
down = net.bytes_recv
up = net.bytes_sent
update_string = "N:{}:{}:{}:{}:{}:{}:{}:{}".format(
cpu[0], cpu[1], cpu[2], cpu[3], mem, temp, down, up
)
rrdtool.update(FILENAME, update_string)
# Create RRD database for 1 second log interval, 90 samples (1.5 minute)
rrdtool.create(
FILENAME,
"--step", "1",
"DS:core0:GAUGE:2:0:100",
"DS:core1:GAUGE:2:0:100",
"DS:core2:GAUGE:2:0:100",
"DS:core3:GAUGE:2:0:100",
"DS:mem:GAUGE:2:0:100",
"DS:temp:GAUGE:2:0:100",
"DS:down:COUNTER:2:U:U",
"DS:up:COUNTER:2:U:U",
"RRA:AVERAGE:0.5:1:90"
)
tick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment