OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 import json |
| 5 import sys |
| 6 import time |
| 7 import unittest |
| 8 |
| 9 from telemetry import decorators |
| 10 from telemetry.internal.platform.tracing_agent import cpu_tracing_agent |
| 11 from telemetry.internal.platform import tracing_agent |
| 12 from telemetry.internal.platform import linux_platform_backend |
| 13 from telemetry.internal.platform import mac_platform_backend |
| 14 from telemetry.internal.platform import win_platform_backend |
| 15 from telemetry.timeline import trace_data |
| 16 from telemetry.timeline import tracing_config |
| 17 |
| 18 SNAPSHOT_KEYS = ['pid', 'command', 'pCpu', 'pMem'] |
| 19 TRACE_EVENT_KEYS = ['name', 'tid', 'pid', 'ph', 'args', 'local', 'id', 'ts'] |
| 20 |
| 21 |
| 22 class FakeAndroidPlatformBackend(object): |
| 23 def __init__(self): |
| 24 self.device = 'fake_device' |
| 25 |
| 26 def GetOSName(self): |
| 27 return 'android' |
| 28 |
| 29 |
| 30 class CpuTracingAgentTest(unittest.TestCase): |
| 31 |
| 32 def setUp(self): |
| 33 self._config = tracing_config.TracingConfig() |
| 34 self._config.enable_cpu_trace = True |
| 35 if sys.platform.startswith('win'): |
| 36 self._desktop_backend = win_platform_backend.WinPlatformBackend() |
| 37 elif sys.platform.startswith('darwin'): |
| 38 self._desktop_backend = mac_platform_backend.MacPlatformBackend() |
| 39 else: |
| 40 self._desktop_backend = linux_platform_backend.LinuxPlatformBackend() |
| 41 self._agent = cpu_tracing_agent.CpuTracingAgent(self._desktop_backend) |
| 42 |
| 43 @decorators.Enabled('linux', 'mac', 'win') |
| 44 def testInit(self): |
| 45 self.assertTrue(isinstance(self._agent, |
| 46 tracing_agent.TracingAgent)) |
| 47 self.assertFalse(self._agent._snapshots) |
| 48 self.assertFalse(self._agent._snapshot_ongoing) |
| 49 |
| 50 @decorators.Enabled('linux', 'mac', 'win') |
| 51 def testIsSupported(self): |
| 52 self.assertTrue(cpu_tracing_agent.CpuTracingAgent.IsSupported( |
| 53 self._desktop_backend)) |
| 54 self.assertFalse(cpu_tracing_agent.CpuTracingAgent.IsSupported( |
| 55 FakeAndroidPlatformBackend())) |
| 56 |
| 57 @decorators.Enabled('linux', 'mac', 'win') |
| 58 def testStartAgentTracing(self): |
| 59 self.assertFalse(self._agent._snapshot_ongoing) |
| 60 self.assertFalse(self._agent._snapshots) |
| 61 self.assertTrue(self._agent.StartAgentTracing(self._config, 0)) |
| 62 self.assertTrue(self._agent._snapshot_ongoing) |
| 63 time.sleep(2) |
| 64 self.assertTrue(self._agent._snapshots) |
| 65 self._agent.StopAgentTracing() |
| 66 |
| 67 @decorators.Enabled('linux', 'mac', 'win') |
| 68 def testStartAgentTracingNotEnabled(self): |
| 69 self._config.enable_cpu_trace = False |
| 70 self.assertFalse(self._agent._snapshot_ongoing) |
| 71 self.assertFalse(self._agent.StartAgentTracing(self._config, 0)) |
| 72 self.assertFalse(self._agent._snapshot_ongoing) |
| 73 self.assertFalse(self._agent._snapshots) |
| 74 time.sleep(2) |
| 75 self.assertFalse(self._agent._snapshots) |
| 76 |
| 77 @decorators.Enabled('linux', 'mac', 'win') |
| 78 def testStopAgentTracingBeforeStart(self): |
| 79 self.assertRaises(AssertionError, self._agent.StopAgentTracing) |
| 80 |
| 81 @decorators.Enabled('linux', 'mac', 'win') |
| 82 def testStopAgentTracing(self): |
| 83 self._agent.StartAgentTracing(self._config, 0) |
| 84 self._agent.StopAgentTracing() |
| 85 self.assertFalse(self._agent._snapshot_ongoing) |
| 86 |
| 87 @decorators.Enabled('linux', 'mac', 'win') |
| 88 def testCollectAgentTraceDataBeforeStop(self): |
| 89 self._agent.StartAgentTracing(self._config, 0) |
| 90 self.assertRaises(AssertionError, self._agent.CollectAgentTraceData, |
| 91 trace_data.TraceDataBuilder()) |
| 92 self._agent.StopAgentTracing() |
| 93 |
| 94 @decorators.Enabled('linux', 'mac', 'win') |
| 95 def testCollectAgentTraceData(self): |
| 96 builder = trace_data.TraceDataBuilder() |
| 97 self._agent.StartAgentTracing(self._config, 0) |
| 98 self._agent.StopAgentTracing() |
| 99 self._agent.CollectAgentTraceData(builder) |
| 100 self.assertFalse(self._agent._snapshot_ongoing) |
| 101 builder = builder.AsData() |
| 102 self.assertTrue(builder.HasTraceFor(trace_data.CPU_TRACE_DATA)) |
| 103 |
| 104 @decorators.Enabled('linux', 'mac', 'win') |
| 105 def testCollectAgentTraceDataFormat(self): |
| 106 builder = trace_data.TraceDataBuilder() |
| 107 self._agent.StartAgentTracing(self._config, 0) |
| 108 time.sleep(2) |
| 109 self._agent.StopAgentTracing() |
| 110 self._agent.CollectAgentTraceData(builder) |
| 111 builder = builder.AsData() |
| 112 data = json.loads(builder.GetTraceFor(trace_data.CPU_TRACE_DATA)) |
| 113 self.assertTrue(data) |
| 114 self.assertEquals(set(data[0].keys()), set(TRACE_EVENT_KEYS)) |
| 115 self.assertEquals(set(data[0]['args'].keys()), set(['processes'])) |
| 116 self.assertTrue(data[0]['args']['processes']) |
| 117 self.assertEquals(set(data[0]['args']['processes'][0].keys()), |
| 118 set(SNAPSHOT_KEYS)) |
| 119 |
| 120 @decorators.Enabled('linux', 'mac', 'win') |
| 121 def testMinimumCpuThreshold(self): |
| 122 builder = trace_data.TraceDataBuilder() |
| 123 self._agent.StartAgentTracing(self._config, 0) |
| 124 time.sleep(2) |
| 125 self._agent.StopAgentTracing() |
| 126 self._agent.CollectAgentTraceData(builder) |
| 127 builder = builder.AsData() |
| 128 data = json.loads(builder.GetTraceFor(trace_data.CPU_TRACE_DATA)) |
| 129 self.assertTrue(data) |
| 130 for snapshot in data: |
| 131 for process in snapshot['args']['processes']: |
| 132 self.assertTrue(process['pCpu'] >= cpu_tracing_agent.DEFAULT_MIN_PCPU) |
| 133 |
| 134 @decorators.Enabled('linux', 'mac') |
| 135 def testParseLine(self): |
| 136 collector = self._agent._collector |
| 137 invalid_inputs = ['', '1000 chrome', '1000 chrome 1.0 1.0 1.0'] |
| 138 for invalid_input in invalid_inputs: |
| 139 self.assertFalse(collector._ParseLine(invalid_input)) |
| 140 valid_input = '1000 chrome 20.0 10.0 ' |
| 141 output = collector._ParseLine(valid_input) |
| 142 self.assertTrue(output['pCpu'] == '20.0' and |
| 143 output['pMem'] == '10.0'and |
| 144 output['pid'] == '1000'and |
| 145 output['command'] == 'chrome') |
OLD | NEW |