Пример #1
0
 def cpu_used(self) -> _CpuUsed:
     end = self.proc.cpu_times()
     unprivileged_cpu_end = communication.TotalServerCpuTime()
     unprivileged_sys_end = communication.TotalServerSysTime()
     return _CpuUsed((end.user - self.cpu_start.user +
                      unprivileged_cpu_end - self.unprivileged_cpu_start),
                     (end.system - self.cpu_start.system +
                      unprivileged_sys_end - self.unprivileged_sys_start))
Пример #2
0
  def testTotalServerCpuSysTime_usesPsutilProcess(self):
    _FakeCpuTimes = collections.namedtuple("FakeCpuTimes", ["user", "system"])

    class _FakeProcess:

      def __init__(self, pid):
        pass

      def cpu_times(self):  # pylint: disable=invalid-name
        return _FakeCpuTimes(42.0, 43.0)

    with mock.patch.object(psutil, "Process", _FakeProcess):
      init_cpu_time = communication.TotalServerCpuTime()
      init_sys_time = communication.TotalServerSysTime()

      with communication.SubprocessServer(_MakeArgs):
        pass

      self.assertAlmostEqual(communication.TotalServerCpuTime() - init_cpu_time,
                             42.0)
      self.assertAlmostEqual(communication.TotalServerSysTime() - init_sys_time,
                             43.0)
Пример #3
0
  def testTotalServerCpuSysTime_usesWin32Process(self):

    def _MockGetProcessTimes(handle):
      del handle  # Unused.
      return {
          "UserTime": 42 * 10 * 1000 * 1000,
          "KernelTime": 43 * 10 * 1000 * 1000
      }

    # pytype: disable=import-error
    import win32process  # pylint: disable=g-import-not-at-top
    # pytype: enable=import-error
    with mock.patch.object(win32process, "GetProcessTimes",
                           _MockGetProcessTimes):
      init_cpu_time = communication.TotalServerCpuTime()
      init_sys_time = communication.TotalServerSysTime()

      with communication.SubprocessServer(_MakeArgs):
        pass

      self.assertAlmostEqual(communication.TotalServerCpuTime() - init_cpu_time,
                             42.0)
      self.assertAlmostEqual(communication.TotalServerSysTime() - init_sys_time,
                             43.0)
Пример #4
0
  def testCpuSysTime_addsUpMultipleProcesses(self):

    class _SubprocessSeverWithFakeCpuTime(communication.SubprocessServer):

      def __init__(self, *args):
        super().__init__(*args)
        self.fake_cpu_time = 0.0
        self.fake_sys_time = 0.0

      @property
      def cpu_time(self):
        return self.fake_cpu_time

      @property
      def sys_time(self):
        return self.fake_sys_time

    init_cpu_time = communication.TotalServerCpuTime()
    init_sys_time = communication.TotalServerSysTime()

    server1 = _SubprocessSeverWithFakeCpuTime(_MakeArgs)
    server1.Start()

    server2 = _SubprocessSeverWithFakeCpuTime(_MakeArgs)
    server2.Start()

    server1.fake_cpu_time = 1.0
    server2.fake_cpu_time = 2.0

    server1.fake_sys_time = 3.0
    server2.fake_sys_time = 4.0

    self.assertAlmostEqual(communication.TotalServerCpuTime() - init_cpu_time,
                           1.0 + 2.0)
    self.assertAlmostEqual(communication.TotalServerSysTime() - init_sys_time,
                           3.0 + 4.0)

    server1.fake_cpu_time = 5.0
    server2.fake_cpu_time = 6.0

    server1.fake_sys_time = 7.0
    server2.fake_sys_time = 8.0

    self.assertAlmostEqual(communication.TotalServerCpuTime() - init_cpu_time,
                           5.0 + 6.0)
    self.assertAlmostEqual(communication.TotalServerSysTime() - init_sys_time,
                           7.0 + 8.0)

    server1.Stop()
    server2.Stop()

    # These updates shouldn't be taken into account.

    server1.fake_cpu_time = 9.0
    server2.fake_cpu_time = 9.0

    server1.fake_sys_time = 9.0
    server2.fake_sys_time = 9.0

    self.assertAlmostEqual(communication.TotalServerCpuTime() - init_cpu_time,
                           5.0 + 6.0)
    self.assertAlmostEqual(communication.TotalServerSysTime() - init_sys_time,
                           7.0 + 8.0)
Пример #5
0
 def __init__(self):
     self.proc = psutil.Process()
     self.cpu_start = self.proc.cpu_times()
     self.unprivileged_cpu_start = communication.TotalServerCpuTime()
     self.unprivileged_sys_start = communication.TotalServerSysTime()