Monitor Computer Board Performance with our Streamer

on November 25 | by

device healthEver wanted to see how much of your little single board computer’s resources are being eaten up by your new robot?  Want to know which method is best for your board?  Is it time for your computer’s annual checkup?
Well, thanks to an amazing python library and Initial State’s data streamer, you can look at what your device is doing and when exactly it did it.
Python’s psutil advertises support for Linux, Windows, OS X, FreeBSD, Sun Solaris, and PyPy on 32-bit and 64-bit architectures.  This means that what we’re about to show you will work on any device that can run Python, has an OS listed above, and has access to the internet.  Just to give you an idea, that means your laptop, a Raspberry Pi, a BeagleBone Black, a PandaBoard, a MinnowBoard, a UDOO…the possibilities are endless*!
*not actually but you get my point
In order to use psutil, you need to download it first.  Just go into your terminal and enter:
sudo pip install psutil
It shouldn’t take but a minute or two, and now you’re ready to write your script!  If you haven’t already imported the Initial State streamer, click below to see a quick how-to!
data streaming icon
Now for the script.  If you’d like to see the whole thing for copy-pasting, click below!
software icon, code icon
We start by importing what we need: psutil, time, and the ISStreamer:
## Import psutil (which we just installed)
import psutil
## Import time for delays
import time
## Import the ISStreamer module
from ISStreamer.Streamer import Streamer
Next, we designate what we want our bucket to be called and where the data is going.  The buffer is an optional parameter that tells the API how many logs to collect before shipping to Initial State.  Setting it to 20 ensures that they ship at the same time. (This does not affect their timestamps, only if they appear in your visualization at the same time as each other.)
## Streamer constructor, this will create a bucket called Computer Performance
## you'll be able to see this name in your list of logs on
## your access_key is a secret and is specific to you, don't share it!
streamer =Streamer(bucket_name="Device Performance",access_key="[Your Access Key Here]",buffer_size=20)
This next part sets the sample rate and how many times we want it to run.  The streamer statement lets us know which sample it’s on.
## We will run this systems check 1000 times with 0.1 seconds between each loop
## so it will take a little less than 2 minutes to run 

for x in range(1000):

	streamer.log("sample", x)
Then we start collecting information using psutil.  We assign names to psutil functions and then stream those values.  The first object, cpu_percent, only has one value associated with it, so the call to stream it looks like our normal format.  Once we get to cpu_percentsmemoryswap, and network, however, their psutil objects start having multiple values associated with them like memory used and memory free.  We can stream all of the values associated with a single object with a single line:  streamer.log_object([object]).  The key_prefix is optional, but allows you to give your own prefix to the various output values instead of using psutil’s default object name.
	# Get total CPU usage
	cpu_percent = psutil.cpu_percent()
	streamer.log("cpu_total", cpu_percent)

	# Get individual CPU usage
	cpu_percents = psutil.cpu_percent(percpu=True)
	streamer.log_object(cpu_percents, key_prefix="cpu")
	# Get the virtual memory usage
	memory = psutil.virtual_memory()
	streamer.log_object(memory, key_prefix="virtual_mem")
	# Get the swap memory usage
	swap = psutil.swap_memory()
	streamer.log_object(swap, key_prefix="swap_mem")

	# Get the network usage
	network = psutil.net_io_counters()
	streamer.log_object(network, key_prefix="net_io")

We can’t forget some sleep so the board doesn’t use all of its resources trying to run the loop!
Finally, safely close the streamer.
When I ran my new code on my Raspberry Pi, I got a graph that looked like this:
Screen Shot 2014-11-24 at 4.57.25 PM
As you can tell, some parameters are more interesting than others!  Did you know that you can hide parameters in Initial State by clicking on the name and pressing ‘delete’ on your keyboard?  Now that is much better!
 Screen Shot 2014-11-24 at 5.03.31 PM
Check out what my Mac looked like:
Mac Performance
Now go try this for yourself!

4 Responses

  1. […] layout! If you’d like an example script to type on your newly formatted keyboard, try our Performance Monitor – you can see how hard you’re making your little board work! » Raspberry […]

  2. […] at boot (though the Pi can’t handle too much!).  An interesting application would be running our monitoring script in the background while you use the Pi and seeing how much processing power your project […]

  3. […] looking at CPU performance while running a simple script.  You may have seen my previous post on monitoring the computer performance – I simply ran this script for a little bit before starting my “stress test” […]

  4. […] looking at CPU performance while running a simple script.  You may have seen my previous post on monitoring the computer performance – I simply ran this script for a little bit before starting my “stress test” […]

Leave a Reply

Your email address will not be published. Required fields are marked *

« »

Initial State Technologies, Inc.
Scroll to top
Initial State