1#!/usr/bin/env python 2 3# Copyright (c) 2015 The Chromium Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7import contextlib 8import logging 9import os 10import unittest 11 12from systrace import decorators 13from systrace import run_systrace 14from systrace import util 15from systrace.tracing_agents import atrace_agent 16 17from devil.android import device_utils 18from devil.android.sdk import intent 19 20 21DEVICE_SERIAL = 'AG8404EC0444AGC' 22ATRACE_ARGS = ['atrace', '-z', '-t', '10', '-b', '4096'] 23CATEGORIES = ['sched', 'gfx', 'view', 'wm'] 24ADB_SHELL = ['adb', '-s', DEVICE_SERIAL, 'shell'] 25 26SYSTRACE_CMD = ['./run_systrace.py', '--time', '10', '-o', 'out.html', '-e', 27 DEVICE_SERIAL] + CATEGORIES 28TRACE_ARGS = (ATRACE_ARGS + CATEGORIES) 29 30TEST_DIR = os.path.join(os.path.dirname(__file__), os.pardir, 'test_data') 31ATRACE_DATA = os.path.join(TEST_DIR, 'atrace_data') 32ATRACE_DATA_RAW = os.path.join(TEST_DIR, 'atrace_data_raw') 33ATRACE_DATA_STRIPPED = os.path.join(TEST_DIR, 'atrace_data_stripped') 34ATRACE_PROCFS_DUMP = os.path.join(TEST_DIR, 'atrace_procfs_dump') 35ATRACE_EXTRACTED_TGIDS = os.path.join(TEST_DIR, 'atrace_extracted_tgids') 36ATRACE_MISSING_TGIDS = os.path.join(TEST_DIR, 'atrace_missing_tgids') 37ATRACE_FIXED_TGIDS = os.path.join(TEST_DIR, 'atrace_fixed_tgids') 38 39 40class AtraceAgentTest(unittest.TestCase): 41 42 # TODO(washingtonp): These end-to-end tests do not work on the Trybot server 43 # because adb cannot be found on the Trybot servers. Figure out what the 44 # issue is and update this test. 45 @decorators.Disabled 46 def test_tracing(self): 47 TRACE_BUFFER_SIZE = '16384' 48 TRACE_TIME = '5' 49 50 devices = device_utils.DeviceUtils.HealthyDevices() 51 package_info = util.get_supported_browsers()['stable'] 52 device = devices[0] 53 output_file_name = util.generate_random_filename_for_test() 54 55 try: 56 # Launch the browser before tracing. 57 device.StartActivity( 58 intent.Intent(activity=package_info.activity, 59 package=package_info.package, 60 data='about:blank', 61 extras={'create_new_tab': True}), 62 blocking=True, force_stop=True) 63 64 # Run atrace agent. 65 run_systrace.main_impl(['./run_systrace.py', 66 '-b', 67 TRACE_BUFFER_SIZE, 68 '-t', 69 TRACE_TIME, 70 '-o', 71 output_file_name, 72 '-e', 73 str(device), 74 '--atrace-categories=gfx,input,view']) 75 76 # Verify results. 77 with open(output_file_name, 'r') as f: 78 full_trace = f.read() 79 self.assertTrue('CPU#'in full_trace) 80 except: 81 raise 82 finally: 83 if os.path.exists(output_file_name): 84 os.remove(output_file_name) 85 86 @decorators.HostOnlyTest 87 def test_construct_atrace_args(self): 88 options, categories = run_systrace.parse_options(SYSTRACE_CMD) 89 options.atrace_categories = categories 90 tracer_args = atrace_agent._construct_atrace_args(options, categories) 91 self.assertEqual(' '.join(TRACE_ARGS), ' '.join(tracer_args)) 92 93 @decorators.HostOnlyTest 94 def test_strip_and_decompress_trace(self): 95 with contextlib.nested(open(ATRACE_DATA_RAW, 'r'), 96 open(ATRACE_DATA_STRIPPED, 'r')) as (f1, f2): 97 atrace_data_raw = f1.read() 98 atrace_data_stripped = f2.read() 99 100 trace_data = atrace_agent.strip_and_decompress_trace(atrace_data_raw) 101 self.assertEqual(atrace_data_stripped, trace_data) 102 103 @decorators.HostOnlyTest 104 def test_extract_tgids(self): 105 with contextlib.nested(open(ATRACE_PROCFS_DUMP, 'r'), 106 open(ATRACE_EXTRACTED_TGIDS, 'r')) as (f1, f2): 107 108 atrace_procfs_dump = f1.read() 109 atrace_procfs_extracted = f2.read() 110 111 tgids = eval(atrace_procfs_extracted) 112 result = atrace_agent.extract_tgids(atrace_procfs_dump.splitlines()) 113 114 self.assertEqual(result, tgids) 115 116 @decorators.HostOnlyTest 117 def test_fix_missing_tgids(self): 118 with contextlib.nested(open(ATRACE_EXTRACTED_TGIDS, 'r'), 119 open(ATRACE_MISSING_TGIDS, 'r'), 120 open(ATRACE_FIXED_TGIDS, 'r')) as (f1, f2, f3): 121 122 atrace_data = f2.read() 123 tgid_map = eval(f1.read()) 124 fixed = f3.read() 125 126 res = atrace_agent.fix_missing_tgids(atrace_data, tgid_map) 127 self.assertEqual(res, fixed) 128 129 130if __name__ == "__main__": 131 logging.getLogger().setLevel(logging.DEBUG) 132 unittest.main(verbosity=2) 133