1import os 2import time 3 4import stem.control 5import stem.util.proc 6import stem.util.str_tools 7 8start_time = time.time() 9samplings = [] 10last_sample = None 11 12with stem.control.Controller.from_port() as controller: 13 controller.authenticate() 14 controller.add_event_listener(lambda *args: None, 'DEBUG') 15 16 while True: 17 utime, stime = stem.util.proc.stats(os.getpid(), stem.util.proc.Stat.CPU_UTIME, stem.util.proc.Stat.CPU_STIME) 18 total_cpu_time = float(utime) + float(stime) 19 20 if last_sample: 21 samplings.append(total_cpu_time - last_sample) 22 print '%0.1f%% (%s)' % (sum(samplings) / len(samplings) * 100, stem.util.str_tools.time_label(time.time() - start_time)) 23 24 last_sample = total_cpu_time 25 time.sleep(1) 26