Best Python code snippet using ATX
cpu_tracing_agent_unittest.py
Source:cpu_tracing_agent_unittest.py
1# Copyright 2016 The Chromium Authors. All rights reserved.2# Use of this source code is governed by a BSD-style license that can be3# found in the LICENSE file.4import json5import sys6import time7import unittest8from telemetry import decorators9from telemetry.internal.platform.tracing_agent import cpu_tracing_agent10from telemetry.internal.platform import tracing_agent11from telemetry.internal.platform import linux_platform_backend12from telemetry.internal.platform import mac_platform_backend13from telemetry.internal.platform import win_platform_backend14from telemetry.timeline import tracing_config15from tracing.trace_data import trace_data16SNAPSHOT_KEYS = ['pid', 'ppid', 'name', 'pCpu', 'pMem']17TRACE_EVENT_KEYS = ['name', 'tid', 'pid', 'ph', 'args', 'local', 'id', 'ts']18class FakeAndroidPlatformBackend(object):19 def __init__(self):20 self.device = 'fake_device'21 def GetOSName(self):22 return 'android'23class CpuTracingAgentTest(unittest.TestCase):24 def setUp(self):25 self._config = tracing_config.TracingConfig()26 self._config.enable_cpu_trace = True27 if sys.platform.startswith('win'):28 self._desktop_backend = win_platform_backend.WinPlatformBackend()29 elif sys.platform.startswith('darwin'):30 self._desktop_backend = mac_platform_backend.MacPlatformBackend()31 else:32 self._desktop_backend = linux_platform_backend.LinuxPlatformBackend()33 self._agent = cpu_tracing_agent.CpuTracingAgent(self._desktop_backend)34 @decorators.Enabled('linux', 'mac', 'win')35 def testInit(self):36 self.assertTrue(isinstance(self._agent,37 tracing_agent.TracingAgent))38 self.assertFalse(self._agent._snapshots)39 self.assertFalse(self._agent._snapshot_ongoing)40 @decorators.Enabled('linux', 'mac', 'win')41 def testIsSupported(self):42 self.assertTrue(cpu_tracing_agent.CpuTracingAgent.IsSupported(43 self._desktop_backend))44 self.assertFalse(cpu_tracing_agent.CpuTracingAgent.IsSupported(45 FakeAndroidPlatformBackend()))46 @decorators.Enabled('linux', 'mac', 'win')47 def testStartAgentTracing(self):48 self.assertFalse(self._agent._snapshot_ongoing)49 self.assertFalse(self._agent._snapshots)50 self.assertTrue(self._agent.StartAgentTracing(self._config, 0))51 self.assertTrue(self._agent._snapshot_ongoing)52 time.sleep(2)53 self.assertTrue(self._agent._snapshots)54 self._agent.StopAgentTracing()55 @decorators.Enabled('linux', 'mac', 'win')56 def testStartAgentTracingNotEnabled(self):57 self._config.enable_cpu_trace = False58 self.assertFalse(self._agent._snapshot_ongoing)59 self.assertFalse(self._agent.StartAgentTracing(self._config, 0))60 self.assertFalse(self._agent._snapshot_ongoing)61 self.assertFalse(self._agent._snapshots)62 time.sleep(2)63 self.assertFalse(self._agent._snapshots)64 @decorators.Enabled('linux', 'mac', 'win')65 def testStopAgentTracingBeforeStart(self):66 self.assertRaises(AssertionError, self._agent.StopAgentTracing)67 @decorators.Enabled('linux', 'mac', 'win')68 def testStopAgentTracing(self):69 self._agent.StartAgentTracing(self._config, 0)70 self._agent.StopAgentTracing()71 self.assertFalse(self._agent._snapshot_ongoing)72 @decorators.Enabled('linux', 'mac', 'win')73 def testCollectAgentTraceDataBeforeStop(self):74 self._agent.StartAgentTracing(self._config, 0)75 self.assertRaises(AssertionError, self._agent.CollectAgentTraceData,76 trace_data.TraceDataBuilder())77 self._agent.StopAgentTracing()78 @decorators.Enabled('linux', 'mac', 'win')79 def testCollectAgentTraceData(self):80 builder = trace_data.TraceDataBuilder()81 self._agent.StartAgentTracing(self._config, 0)82 self._agent.StopAgentTracing()83 self._agent.CollectAgentTraceData(builder)84 self.assertFalse(self._agent._snapshot_ongoing)85 builder = builder.AsData()86 self.assertTrue(builder.HasTracesFor(trace_data.CPU_TRACE_DATA))87 @decorators.Enabled('linux', 'mac', 'win')88 def testCollectAgentTraceDataFormat(self):89 builder = trace_data.TraceDataBuilder()90 self._agent.StartAgentTracing(self._config, 0)91 time.sleep(2)92 self._agent.StopAgentTracing()93 self._agent.CollectAgentTraceData(builder)94 builder = builder.AsData()95 data = json.loads(builder.GetTracesFor(trace_data.CPU_TRACE_DATA)[0])96 self.assertTrue(data)97 self.assertEquals(set(data[0].keys()), set(TRACE_EVENT_KEYS))98 self.assertEquals(set(data[0]['args']['snapshot'].keys()),99 set(['processes']))100 self.assertTrue(data[0]['args']['snapshot']['processes'])101 self.assertEquals(set(data[0]['args']['snapshot']['processes'][0].keys()),102 set(SNAPSHOT_KEYS))103 @decorators.Enabled('linux', 'mac', 'win')104 def testContainsRealProcesses(self):105 builder = trace_data.TraceDataBuilder()106 self._agent.StartAgentTracing(self._config, 0)107 time.sleep(2)108 self._agent.StopAgentTracing()109 self._agent.CollectAgentTraceData(builder)110 builder = builder.AsData()111 data = json.loads(builder.GetTracesFor(trace_data.CPU_TRACE_DATA)[0])112 self.assertTrue(data)113 for snapshot in data:114 found_unittest_process = False115 processes = snapshot['args']['snapshot']['processes']116 for process in processes:117 if 'run_tests' in process['name']:118 found_unittest_process = True119 self.assertTrue(found_unittest_process)120 @decorators.Enabled('win')121 def testWindowsCanHandleProcessesWithSpaces(self):122 proc_collector = cpu_tracing_agent.WindowsProcessCollector()123 proc_collector.Init()124 proc = proc_collector._ParseProcessString(125 '0 1 Multi Word Process 50 75')126 self.assertEquals(proc['ppid'], 0)127 self.assertEquals(proc['pid'], 1)128 self.assertEquals(proc['name'], 'Multi Word Process')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!