Example #1
0
    def test_stats(self, get_line_mock):
        """
    Tests stats() with all combinations of stat_type arguments.
    """

        # list of all combinations of args with respective return values
        stat_combinations = test.get_all_combinations([
            ('command', 'test_program'),
            ('utime', '0.13'),
            ('stime', '0.14'),
            ('start time', '10.21'),
        ])

        stat_path = '/proc/24062/stat'
        stat = '1 (test_program) 2 3 4 5 6 7 8 9 10 11 12 13.0 14.0 15 16 17 18 19 20 21.0 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43'

        # tests the case where no stat_types are specified

        get_line_mock.side_effect = lambda *params: {
            (stat_path, '24062', 'process '): stat
        }[params]

        self.assertEqual((), proc.stats(24062))

        for stats in stat_combinations:
            # the stats variable is...
            #   [(arg1, resp1), (arg2, resp2)...]
            #
            # but we need...
            #   (arg1, arg2...), (resp1, resp2...).

            args, response = list(zip(*stats))

            get_line_mock.side_effect = lambda *params: {
                (stat_path, '24062', 'process %s' % ', '.join(args)): stat
            }[params]

            self.assertEqual(response, proc.stats(24062, *args))

            # tests the case where pid = 0

            if 'start time' in args:
                response = 10
            else:
                response = ()

                for arg in args:
                    if arg == 'command':
                        response += ('sched', )
                    elif arg == 'utime':
                        response += ('0', )
                    elif arg == 'stime':
                        response += ('0', )

            get_line_mock.side_effect = lambda *params: {
                ('/proc/0/stat', '0', 'process %s' % ', '.join(args)): stat
            }[params]

            self.assertEqual(response, proc.stats(0, *args))
Example #2
0
  def test_stats(self, get_line_mock):
    """
    Tests stats() with all combinations of stat_type arguments.
    """

    # list of all combinations of args with respective return values
    stat_combinations = test.get_all_combinations([
      ('command', 'test_program'),
      ('utime', '0.13'),
      ('stime', '0.14'),
      ('start time', '10.21'),
    ])

    stat_path = '/proc/24062/stat'
    stat = '1 (test_program) 2 3 4 5 6 7 8 9 10 11 12 13.0 14.0 15 16 17 18 19 20 21.0 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43'

    # tests the case where no stat_types are specified

    get_line_mock.side_effect = lambda *params: {
      (stat_path, '24062', 'process '): stat
    }[params]

    self.assertEqual((), proc.stats(24062))

    for stats in stat_combinations:
      # the stats variable is...
      #   [(arg1, resp1), (arg2, resp2)...]
      #
      # but we need...
      #   (arg1, arg2...), (resp1, resp2...).

      args, response = list(zip(*stats))

      get_line_mock.side_effect = lambda *params: {
        (stat_path, '24062', 'process %s' % ', '.join(args)): stat
      }[params]

      self.assertEqual(response, proc.stats(24062, *args))

      # tests the case where pid = 0

      if 'start time' in args:
        response = 10
      else:
        response = ()

        for arg in args:
          if arg == 'command':
            response += ('sched',)
          elif arg == 'utime':
            response += ('0',)
          elif arg == 'stime':
            response += ('0',)

      get_line_mock.side_effect = lambda *params: {
        ('/proc/0/stat', '0', 'process %s' % ', '.join(args)): stat
      }[params]

      self.assertEqual(response, proc.stats(0, *args))
Example #3
0
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)
Example #4
0
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)
Example #5
0
File: proc.py Project: zmchu/stem
  def test_stats(self):
    """
    Checks that stem.util.proc.stats looks somewhat reasonable.
    """

    tor_cmd = test.runner.get_runner().get_tor_command(True)
    tor_pid = test.runner.get_runner().get_pid()
    command, utime, stime, start_time = proc.stats(tor_pid, 'command', 'utime', 'stime', 'start time')

    self.assertEqual(tor_cmd, command)
    self.assertTrue(float(utime) > 0)
    self.assertTrue(float(stime) >= 0)
    self.assertTrue(float(start_time) > proc.system_start_time())
Example #6
0
  def test_stats(self):
    """
    Checks that stem.util.proc.stats looks somewhat reasonable.
    """

    if not proc.is_available():
      test.runner.skip(self, '(proc unavailable)')
      return

    tor_cmd = test.runner.get_runner().get_tor_command(True)
    tor_pid = test.runner.get_runner().get_pid()
    command, utime, stime, start_time = proc.stats(tor_pid, 'command', 'utime', 'stime', 'start time')

    self.assertEqual(tor_cmd, command)
    self.assertTrue(float(utime) > 0)
    self.assertTrue(float(stime) >= 0)
    self.assertTrue(float(start_time) > proc.system_start_time())