def test_memory_usage(self, get_lines_mock): """ Tests the memory_usage function with a given pid. """ get_lines_mock.side_effect = lambda *params: { ('/proc/1111/status', ('VmRSS:', 'VmSize:'), 'memory usage'): {'VmRSS:': 'VmRSS: 100 kB', 'VmSize:': 'VmSize: 1800 kB'} }[params] self.assertEqual((0, 0), proc.memory_usage(0)) self.assertEqual((100 * 1024, 1800 * 1024), proc.memory_usage(1111))
def _resources_via_proc(pid): """ Fetches resource usage information about a given process via proc. This returns a tuple of the form... (total_cpu_time, uptime, memory_in_bytes, memory_in_percent) :param int pid: process to be queried :returns: **tuple** with the resource usage information :raises: **IOError** if unsuccessful """ utime, stime, start_time = proc.stats( pid, proc.Stat.CPU_UTIME, proc.Stat.CPU_STIME, proc.Stat.START_TIME, ) total_cpu_time = float(utime) + float(stime) memory_in_bytes = proc.memory_usage(pid)[0] total_memory = proc.physical_memory() uptime = time.time() - float(start_time) memory_in_percent = float(memory_in_bytes) / total_memory return (total_cpu_time, uptime, memory_in_bytes, memory_in_percent)
def test_memory_usage(self): """ Checks that stem.util.proc.memory_usage looks somewhat reasonable. """ tor_pid = test.runner.get_runner().get_pid() res_size, vir_size = proc.memory_usage(tor_pid) # checks that they're larger than a kilobyte self.assertTrue(res_size > 1024) self.assertTrue(vir_size > 1024)
def test_memory_usage(self): """ Checks that stem.util.proc.memory_usage looks somewhat reasonable. """ if not proc.is_available(): test.runner.skip(self, '(proc unavailable)') return tor_pid = test.runner.get_runner().get_pid() res_size, vir_size = proc.memory_usage(tor_pid) # checks that they're larger than a kilobyte self.assertTrue(res_size > 1024) self.assertTrue(vir_size > 1024)