| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import logging | 5 import logging |
| 6 import os | 6 import os |
| 7 import unittest | 7 import unittest |
| 8 | 8 |
| 9 from telemetry.core import util | 9 from telemetry.core import util |
| 10 from telemetry.internal.platform import linux_based_platform_backend | 10 from telemetry.internal.platform import linux_based_platform_backend |
| 11 import mock | 11 import mock |
| 12 | 12 |
| 13 | 13 |
| 14 class TestBackend(linux_based_platform_backend.LinuxBasedPlatformBackend): | 14 class TestLinuxBackend(linux_based_platform_backend.LinuxBasedPlatformBackend): |
| 15 | 15 |
| 16 # pylint: disable=abstract-method | 16 # pylint: disable=abstract-method |
| 17 | 17 |
| 18 def __init__(self): | 18 def __init__(self): |
| 19 super(TestBackend, self).__init__() | 19 super(TestLinuxBackend, self).__init__() |
| 20 self._mock_files = {} | 20 self._mock_files = {} |
| 21 | 21 |
| 22 def SetMockFile(self, filename, output): | 22 def SetMockFile(self, filename, output): |
| 23 self._mock_files[filename] = output | 23 self._mock_files[filename] = output |
| 24 | 24 |
| 25 def GetFileContents(self, filename): | 25 def GetFileContents(self, filename): |
| 26 return self._mock_files[filename] | 26 return self._mock_files[filename] |
| 27 | 27 |
| 28 def GetClockTicks(self): | 28 def GetClockTicks(self): |
| 29 return 41 | 29 return 41 |
| 30 | 30 |
| 31 | 31 |
| 32 class LinuxBasedPlatformBackendTest(unittest.TestCase): | 32 class LinuxBasedPlatformBackendTest(unittest.TestCase): |
| 33 | 33 |
| 34 def SetMockFileInBackend(self, backend, real_file, mock_file): | 34 def SetMockFileInBackend(self, backend, real_file, mock_file): |
| 35 with open(os.path.join(util.GetUnittestDataDir(), real_file)) as f: | 35 with open(os.path.join(util.GetUnittestDataDir(), real_file)) as f: |
| 36 backend.SetMockFile(mock_file, f.read()) | 36 backend.SetMockFile(mock_file, f.read()) |
| 37 | 37 |
| 38 def testGetSystemCommitCharge(self): | 38 def testGetSystemCommitCharge(self): |
| 39 if not linux_based_platform_backend.resource: | 39 if not linux_based_platform_backend.resource: |
| 40 logging.warning('Test not supported') | 40 logging.warning('Test not supported') |
| 41 return | 41 return |
| 42 | 42 |
| 43 backend = TestBackend() | 43 backend = TestLinuxBackend() |
| 44 self.SetMockFileInBackend(backend, 'proc_meminfo', '/proc/meminfo') | 44 self.SetMockFileInBackend(backend, 'proc_meminfo', '/proc/meminfo') |
| 45 result = backend.GetSystemCommitCharge() | 45 result = backend.GetSystemCommitCharge() |
| 46 # 25252140 == MemTotal - MemFree - Buffers - Cached (in kB) | 46 # 25252140 == MemTotal - MemFree - Buffers - Cached (in kB) |
| 47 self.assertEquals(result, 25252140) | 47 self.assertEquals(result, 25252140) |
| 48 | 48 |
| 49 def testGetSystemTotalPhysicalMemory(self): | 49 def testGetSystemTotalPhysicalMemory(self): |
| 50 if not linux_based_platform_backend.resource: | 50 if not linux_based_platform_backend.resource: |
| 51 logging.warning('Test not supported') | 51 logging.warning('Test not supported') |
| 52 return | 52 return |
| 53 | 53 |
| 54 backend = TestBackend() | 54 backend = TestLinuxBackend() |
| 55 self.SetMockFileInBackend(backend, 'proc_meminfo', '/proc/meminfo') | 55 self.SetMockFileInBackend(backend, 'proc_meminfo', '/proc/meminfo') |
| 56 result = backend.GetSystemTotalPhysicalMemory() | 56 result = backend.GetSystemTotalPhysicalMemory() |
| 57 # 67479191552 == MemTotal * 1024 | 57 # 67479191552 == MemTotal * 1024 |
| 58 self.assertEquals(result, 67479191552) | 58 self.assertEquals(result, 67479191552) |
| 59 | 59 |
| 60 def testGetCpuStatsBasic(self): | 60 def testGetCpuStatsBasic(self): |
| 61 if not linux_based_platform_backend.resource: | 61 if not linux_based_platform_backend.resource: |
| 62 logging.warning('Test not supported') | 62 logging.warning('Test not supported') |
| 63 return | 63 return |
| 64 | 64 |
| 65 backend = TestBackend() | 65 backend = TestLinuxBackend() |
| 66 self.SetMockFileInBackend(backend, 'stat', '/proc/1/stat') | 66 self.SetMockFileInBackend(backend, 'stat', '/proc/1/stat') |
| 67 result = backend.GetCpuStats(1) | 67 result = backend.GetCpuStats(1) |
| 68 self.assertEquals(result, {'CpuProcessTime': 22.0}) | 68 self.assertEquals(result, {'CpuProcessTime': 22.0}) |
| 69 | 69 |
| 70 def testGetCpuTimestampBasic(self): | 70 def testGetCpuTimestampBasic(self): |
| 71 if not linux_based_platform_backend.resource: | 71 if not linux_based_platform_backend.resource: |
| 72 logging.warning('Test not supported') | 72 logging.warning('Test not supported') |
| 73 return | 73 return |
| 74 jiffies_grep_string = """ | 74 jiffies_grep_string = """ |
| 75 jiffies | 75 jiffies |
| (...skipping 10 matching lines...) Expand all Loading... |
| 86 result = backend.GetCpuTimestamp() | 86 result = backend.GetCpuTimestamp() |
| 87 self.assertEquals(result, {'TotalTime': 105054633.0}) | 87 self.assertEquals(result, {'TotalTime': 105054633.0}) |
| 88 mock_method.assert_call_once_with( | 88 mock_method.assert_call_once_with( |
| 89 ['grep', '-m', '1', 'jiffies:', '/proc/timer_list']) | 89 ['grep', '-m', '1', 'jiffies:', '/proc/timer_list']) |
| 90 | 90 |
| 91 def testGetMemoryStatsBasic(self): | 91 def testGetMemoryStatsBasic(self): |
| 92 if not linux_based_platform_backend.resource: | 92 if not linux_based_platform_backend.resource: |
| 93 logging.warning('Test not supported') | 93 logging.warning('Test not supported') |
| 94 return | 94 return |
| 95 | 95 |
| 96 backend = TestBackend() | 96 backend = TestLinuxBackend() |
| 97 self.SetMockFileInBackend(backend, 'stat', '/proc/1/stat') | 97 self.SetMockFileInBackend(backend, 'stat', '/proc/1/stat') |
| 98 self.SetMockFileInBackend(backend, 'status', '/proc/1/status') | 98 self.SetMockFileInBackend(backend, 'status', '/proc/1/status') |
| 99 self.SetMockFileInBackend(backend, 'smaps', '/proc/1/smaps') | 99 self.SetMockFileInBackend(backend, 'smaps', '/proc/1/smaps') |
| 100 result = backend.GetMemoryStats(1) | 100 result = backend.GetMemoryStats(1) |
| 101 self.assertEquals(result, {'PrivateDirty': 5324800, | 101 self.assertEquals(result, {'PrivateDirty': 5324800, |
| 102 'VM': 1025978368, | 102 'VM': 1025978368, |
| 103 'VMPeak': 1050099712, | 103 'VMPeak': 1050099712, |
| 104 'WorkingSetSize': 84000768, | 104 'WorkingSetSize': 84000768, |
| 105 'WorkingSetSizePeak': 144547840}) | 105 'WorkingSetSizePeak': 144547840}) |
| 106 | 106 |
| 107 def testGetMemoryStatsNoHWM(self): | 107 def testGetMemoryStatsNoHWM(self): |
| 108 if not linux_based_platform_backend.resource: | 108 if not linux_based_platform_backend.resource: |
| 109 logging.warning('Test not supported') | 109 logging.warning('Test not supported') |
| 110 return | 110 return |
| 111 | 111 |
| 112 backend = TestBackend() | 112 backend = TestLinuxBackend() |
| 113 self.SetMockFileInBackend(backend, 'stat', '/proc/1/stat') | 113 self.SetMockFileInBackend(backend, 'stat', '/proc/1/stat') |
| 114 self.SetMockFileInBackend(backend, 'status_nohwm', '/proc/1/status') | 114 self.SetMockFileInBackend(backend, 'status_nohwm', '/proc/1/status') |
| 115 self.SetMockFileInBackend(backend, 'smaps', '/proc/1/smaps') | 115 self.SetMockFileInBackend(backend, 'smaps', '/proc/1/smaps') |
| 116 result = backend.GetMemoryStats(1) | 116 result = backend.GetMemoryStats(1) |
| 117 self.assertEquals(result, {'PrivateDirty': 5324800, | 117 self.assertEquals(result, {'PrivateDirty': 5324800, |
| 118 'VM': 1025978368, | 118 'VM': 1025978368, |
| 119 'VMPeak': 1025978368, | 119 'VMPeak': 1025978368, |
| 120 'WorkingSetSize': 84000768, | 120 'WorkingSetSize': 84000768, |
| 121 'WorkingSetSizePeak': 84000768}) | 121 'WorkingSetSizePeak': 84000768}) |
| OLD | NEW |