1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| import time import matplotlib.pyplot as plt import numpy as np
def get_memory(t): "Simulate a function that returns system memory" return 100 * (0.5 + 0.5 * np.sin(0.5 * np.pi * t))
def get_cpu(t): "Simulate a function that returns cpu usage" return 100 * (0.5 + 0.5 * np.sin(0.2 * np.pi * (t - 0.25)))
def get_net(t): "Simulate a function that returns network bandwidth" return 100 * (0.5 + 0.5 * np.sin(0.7 * np.pi * (t - 0.1)))
def get_stats(t): return get_memory(t), get_cpu(t), get_net(t)
fig, ax = plt.subplots() ind = np.arange(1, 4)
plt.show(block=False)
pm, pc, pn = plt.bar(ind, get_stats(0)) pm.set_facecolor('r') pc.set_facecolor('g') pn.set_facecolor('b') ax.set_xticks(ind) ax.set_xticklabels(['Memory', 'CPU', 'Bandwidth']) ax.set_ylim([0, 100]) ax.set_ylabel('Percent usage') ax.set_title('System Monitor')
start = time.time() for i in range(200): m, c, n = get_stats(i / 10.0)
pm.set_height(m) pc.set_height(c) pn.set_height(n)
fig.canvas.draw_idle() try: fig.canvas.flush_events() except NotImplementedError: pass
stop = time.time() print("{fps:.1f} frames per second".format(fps=200 / (stop - start)))
|