Lines Matching refs:self

77     def __init__(self):  argument
78 self.log_file = 'test.log'
79 remove(self.log_file)
81 self.log_fh = open(self.log_file, 'a')
82 logging.basicConfig(filename=self.log_file)
84 def writeln(self, s): argument
85 return self.write(s + '\n')
87 def write(self, s): argument
89 self.log_fh.write(s)
91 self.flush()
93 def flush(self): argument
94 self.log_fh.flush()
103 def __init__(self): argument
104 self.script_dir = os.path.abspath(get_script_dir())
105 self.cur_dir = os.getcwd()
106 self.testdata_dir = os.path.join(self.cur_dir, 'testdata')
107 self.test_base_dir = os.path.join(self.cur_dir, 'test_results')
108 self.adb = AdbHelper(enable_switch_to_root=True)
109 self.android_version = self.adb.get_android_version()
110 self.device_features = None
111 self.browser_option = []
112 self.progress_fh = None
114 def testdata_path(self, testdata_name): argument
116 return os.path.join(self.testdata_dir, testdata_name.replace('/', os.sep))
118 def test_dir(self, test_name): argument
120 return os.path.join(self.test_base_dir, test_name)
122 def script_path(self, script_name): argument
124 return os.path.join(self.script_dir, script_name)
126 def get_device_features(self): argument
127 if self.device_features is None:
128 args = [sys.executable, self.script_path(
132 self.device_features = output.split()
133 return self.device_features
135 def is_trace_offcpu_supported(self): argument
136 return 'trace-offcpu' in self.get_device_features()
138 def build_testdata(self): argument
145 if os.path.isdir(self.testdata_dir):
147 os.makedirs(self.testdata_dir)
151 source_dirs = [os.path.join(self.script_dir, x) for x in source_dirs]
157 target = os.path.join(self.testdata_dir, name)
165 def get_32bit_abi(self): argument
166 return self.adb.get_property('ro.product.cpu.abilist32').strip().split(',')[0]
168 def write_progress(self, progress): argument
169 if self.progress_fh:
170 self.progress_fh.write(progress + '\n')
171 self.progress_fh.flush()
178 def setUp(self): argument
180 self.test_dir = TEST_HELPER.test_dir('%s.%s' % (
181 self.__class__.__name__, self._testMethodName))
182 os.makedirs(self.test_dir)
183 self.saved_cwd = os.getcwd()
184 os.chdir(self.test_dir)
185 TEST_LOGGER.writeln('begin test %s.%s' % (self.__class__.__name__, self._testMethodName))
186 self.start_time = time.time()
189 def run(self, result=None): argument
190 ret = super(TestBase, self).run(result)
191 if result.errors and result.errors[-1][0] == self:
194 elif result.failures and result.failures[-1][0] == self:
200 time_taken = time.time() - self.start_time
203 (self.__class__.__name__, self._testMethodName, status, time_taken))
208 os.chdir(self.saved_cwd)
210 shutil.rmtree(self.test_dir)
212 '%s.%s %s' % (self.__class__.__name__, self._testMethodName, status))
215 def run_cmd(self, args, return_output=False, drop_output=True): argument
244 self.assertEqual(returncode, 0, msg="failed to run cmd: %s" % args)
249 def check_strings_in_file(self, filename, strings): argument
250 self.check_exist(filename=filename)
252 self.check_strings_in_content(fh.read(), strings)
254 def check_exist(self, filename=None, dirname=None): argument
256 self.assertTrue(os.path.isfile(filename), filename)
258 self.assertTrue(os.path.isdir(dirname), dirname)
260 def check_strings_in_content(self, content, strings): argument
262 self.check_fulfilled_entries(fulfilled, strings)
264 def check_fulfilled_entries(self, fulfilled, entries): argument
271 self.fail('failed in below entries: %s' % (failed_entries,))
307 def setUp(self): argument
308 super(TestExampleBase, self).setUp()
309 if 'TraceOffCpu' in self.id() and not TEST_HELPER.is_trace_offcpu_supported():
310 self.skipTest('trace-offcpu is not supported on device')
313 if not os.path.isdir(self.testcase_dir):
314 os.makedirs(self.testcase_dir)
315 os.chdir(self.testcase_dir)
316 self.run_app_profiler(compile_java_code=self.use_compiled_java_code)
317 os.chdir(self.test_dir)
319 for name in os.listdir(self.testcase_dir):
320 path = os.path.join(self.testcase_dir, name)
322 shutil.copy(path, self.test_dir)
324 shutil.copytree(path, os.path.join(self.test_dir, name))
326 def run(self, result=None): argument
327 self.__class__.test_result = result
328 super(TestExampleBase, self).run(result)
330 def run_app_profiler(self, record_arg="-g --duration 10", build_binary_cache=True, argument
332 args = ['app_profiler.py', '--app', self.package_name, '-r', record_arg, '-o', 'perf.data']
338 args += ["-a", self.activity_name]
339 args += ["-lib", self.example_path]
340 if not self.adb_root:
342 self.run_cmd(args)
343 self.check_exist(filename="perf.data")
345 self.check_exist(dirname="binary_cache")
347 def check_file_under_dir(self, dirname, filename): argument
348 self.check_exist(dirname=dirname)
353 self.fail("Failed to call check_file_under_dir(dir=%s, file=%s)" % (dirname, filename))
355 def check_annotation_summary(self, summary_file, check_entries): argument
360 self.check_exist(filename=summary_file)
375 self.check_fulfilled_entries(fulfilled, check_entries)
377 def check_inferno_report_html(self, check_entries, filename="report.html"): argument
378 self.check_exist(filename=filename)
391 self.check_fulfilled_entries(fulfilled, check_entries)
393 def common_test_app_profiler(self): argument
394 self.run_cmd(["app_profiler.py", "-h"])
396 self.run_app_profiler(build_binary_cache=False)
397 self.assertFalse(os.path.isdir("binary_cache"))
399 if not self.adb_root:
401 self.run_cmd(args)
402 self.check_exist(dirname="binary_cache")
404 self.run_app_profiler(build_binary_cache=True)
405 self.run_app_profiler()
406 self.run_app_profiler(start_activity=False)
408 def common_test_report(self): argument
409 self.run_cmd(["report.py", "-h"])
410 self.run_cmd(["report.py"])
411 self.run_cmd(["report.py", "-i", "perf.data"])
412 self.run_cmd(["report.py", "-g"])
413 self.run_cmd(["report.py", "--self-kill-for-testing", "-g", "--gui"])
415 def common_test_annotate(self): argument
416 self.run_cmd(["annotate.py", "-h"])
418 self.run_cmd(["annotate.py", "-s", self.example_path])
419 self.check_exist(dirname="annotated_files")
421 def common_test_report_sample(self, check_strings): argument
422 self.run_cmd(["report_sample.py", "-h"])
423 self.run_cmd(["report_sample.py"])
424 output = self.run_cmd(["report_sample.py", "perf.data"], return_output=True)
425 self.check_strings_in_content(output, check_strings)
427 def common_test_pprof_proto_generator(self, check_strings_with_lines, argument
432 self.run_cmd(["pprof_proto_generator.py", "-h"])
433 self.run_cmd(["pprof_proto_generator.py"])
435 self.run_cmd(["pprof_proto_generator.py", "-i", "perf.data", "-o", "pprof.profile"])
436 self.check_exist(filename="pprof.profile")
437 self.run_cmd(["pprof_proto_generator.py", "--show"])
438 output = self.run_cmd(["pprof_proto_generator.py", "--show", "pprof.profile"],
440 self.check_strings_in_content(output, check_strings_with_lines + ["has_line_numbers: True"])
442 self.run_cmd(["pprof_proto_generator.py"])
443 output = self.run_cmd(["pprof_proto_generator.py", "--show", "pprof.profile"],
445 self.check_strings_in_content(output, check_strings_without_lines +
448 def common_test_inferno(self): argument
449 self.run_cmd([INFERNO_SCRIPT, "-h"])
451 append_args = [] if self.adb_root else ["--disable_adb_root"]
452 self.run_cmd([INFERNO_SCRIPT, "-p", self.package_name, "-t", "3"] + append_args)
453 self.check_exist(filename="perf.data")
454 self.run_cmd([INFERNO_SCRIPT, "-p", self.package_name, "-f", "1000", "-du", "-t", "1"] +
456 self.run_cmd([INFERNO_SCRIPT, "-p", self.package_name, "-e", "100000 cpu-cycles",
458 self.run_cmd([INFERNO_SCRIPT, "-sc"])
460 def common_test_report_html(self): argument
461 self.run_cmd(['report_html.py', '-h'])
462 self.run_cmd(['report_html.py'])
463 self.run_cmd(['report_html.py', '--add_source_code', '--source_dirs', 'testdata'])
464 self.run_cmd(['report_html.py', '--add_disassembly'])
467 self.run_app_profiler(record_arg='-g -f 1000 --duration 3 -e task-clock:u')
468 self.run_cmd(['report_html.py', '-i', 'perf.data', 'perf2.data'])
478 def test_app_profiler(self): argument
479 self.common_test_app_profiler()
481 def test_app_profiler_profile_from_launch(self): argument
482 self.run_app_profiler(start_activity=True, build_binary_cache=False)
483 self.run_cmd(["report.py", "-g", "-o", "report.txt"])
484 self.check_strings_in_file("report.txt", [
488 def test_app_profiler_multiprocesses(self): argument
489 self.adb.check_run(['shell', 'am', 'force-stop', self.package_name])
490 self.adb.check_run(['shell', 'am', 'start', '-n',
491 self.package_name + '/.MultiProcessActivity'])
494 self.run_app_profiler(start_activity=False)
495 self.run_cmd(["report.py", "-o", "report.txt"])
496 self.check_strings_in_file("report.txt", ["BusyService", "BusyThread"])
498 def test_app_profiler_with_ctrl_c(self): argument
501 self.adb.check_run(['shell', 'am', 'start', '-n', self.package_name + '/.MainActivity'])
504 "--app", self.package_name, "-r", "--duration 10000", "--disable_adb_root"]
510 self.assertEqual(subproc.returncode, 0)
511 self.run_cmd(["report.py"])
513 def test_app_profiler_stop_after_app_exit(self): argument
514 self.adb.check_run(['shell', 'am', 'start', '-n', self.package_name + '/.MainActivity'])
518 '--app', self.package_name, '-r', '--duration 10000', '--disable_adb_root'])
520 self.adb.check_run(['shell', 'am', 'force-stop', self.package_name])
522 self.assertEqual(subproc.returncode, 0)
523 self.run_cmd(["report.py"])
525 def test_app_profiler_with_ndk_path(self): argument
527 self.run_cmd(['app_profiler.py', '--app', self.package_name, '-a', self.activity_name,
530 def test_report(self): argument
531 self.common_test_report()
532 self.run_cmd(["report.py", "-g", "-o", "report.txt"])
533 self.check_strings_in_file("report.txt", [
537 def test_profile_with_process_id(self): argument
538 self.adb.check_run(['shell', 'am', 'start', '-n', self.package_name + '/.MainActivity'])
540 pid = self.adb.check_run_and_return_output([
542 self.run_app_profiler(start_activity=False, record_arg='-g --duration 10 -p ' + pid)
543 self.run_cmd(["report.py", "-g", "-o", "report.txt"])
544 self.check_strings_in_file("report.txt", [
548 def test_annotate(self): argument
549 self.common_test_annotate()
550 if not self.use_compiled_java_code:
553 self.check_file_under_dir("annotated_files", "MainActivity.java")
555 self.check_annotation_summary(summary_file, [
561 def test_report_sample(self): argument
562 self.common_test_report_sample(
566 def test_pprof_proto_generator(self): argument
568 if self.use_compiled_java_code:
572 self.common_test_pprof_proto_generator(
577 def test_inferno(self): argument
578 self.common_test_inferno()
579 self.run_app_profiler()
580 self.run_cmd([INFERNO_SCRIPT, "-sc"])
581 self.check_inferno_report_html(
583 self.run_cmd([INFERNO_SCRIPT, "-sc", "-o", "report2.html"])
584 self.check_inferno_report_html(
588 def test_inferno_in_another_dir(self): argument
592 self.run_cmd(['app_profiler.py', '--app', self.package_name,
594 self.check_exist(filename="perf.data")
595 self.run_cmd([INFERNO_SCRIPT, "-sc"])
597 def test_report_html(self): argument
598 self.common_test_report_html()
600 def test_run_simpleperf_without_usb_connection(self): argument
601 self.adb.check_run(['shell', 'am', 'start', '-n', self.package_name + '/.MainActivity'])
602 self.run_cmd(['run_simpleperf_without_usb_connection.py', 'start', '-p',
603 self.package_name, '--size_limit', '1M'])
604 self.adb.check_run(['kill-server'])
607 os.chdir(self.saved_cwd)
608 self.adb.check_run(['devices'])
609 os.chdir(self.test_dir)
610 self.run_cmd(['run_simpleperf_without_usb_connection.py', 'stop'])
611 self.check_exist(filename="perf.data")
612 self.run_cmd(["report.py", "-g", "-o", "report.txt"])
623 def test_app_profiler(self): argument
624 self.common_test_app_profiler()
634 def test_smoke(self): argument
635 self.run_app_profiler(record_arg="-g -f 1000 --duration 10 -e cpu-cycles:u --trace-offcpu")
636 self.run_cmd(["report.py", "-g", "-o", "report.txt"])
637 self.check_strings_in_file("report.txt", [
643 self.run_cmd(["annotate.py", "-s", self.example_path])
644 self.check_exist(dirname="annotated_files")
645 if self.use_compiled_java_code:
646 self.check_file_under_dir("annotated_files", "SleepActivity.java")
648 self.check_annotation_summary(summary_file, [
655 self.run_cmd([INFERNO_SCRIPT, "-sc"])
656 self.check_inferno_report_html(
671 def test_app_profiler(self): argument
672 self.common_test_app_profiler()
674 def test_app_profiler_profile_from_launch(self): argument
675 self.run_app_profiler(start_activity=True, build_binary_cache=False)
676 self.run_cmd(["report.py", "-g", "-o", "report.txt"])
677 self.check_strings_in_file("report.txt", ["BusyLoopThread", "__start_thread"])
679 def test_report(self): argument
680 self.common_test_report()
681 self.run_cmd(["report.py", "-g", "-o", "report.txt"])
682 self.check_strings_in_file("report.txt", ["BusyLoopThread", "__start_thread"])
684 def test_annotate(self): argument
685 self.common_test_annotate()
686 self.check_file_under_dir("annotated_files", "native-lib.cpp")
688 self.check_annotation_summary(summary_file, [
693 def test_report_sample(self): argument
694 self.common_test_report_sample(
698 def test_pprof_proto_generator(self): argument
704 self.common_test_pprof_proto_generator(
708 def test_inferno(self): argument
709 self.common_test_inferno()
710 self.run_app_profiler()
711 self.run_cmd([INFERNO_SCRIPT, "-sc"])
712 self.check_inferno_report_html([('BusyLoopThread', 20)])
714 def test_report_html(self): argument
715 self.common_test_report_html()
716 self.run_cmd(['report_html.py', '--add_source_code', '--source_dirs', 'testdata',
728 def test_app_profiler(self): argument
729 self.common_test_app_profiler()
739 def test_smoke(self): argument
740 self.run_app_profiler(record_arg="-g -f 1000 --duration 10 -e cpu-cycles:u --trace-offcpu")
741 self.run_cmd(["report.py", "-g", "--comms", "SleepThread", "-o", "report.txt"])
742 self.check_strings_in_file("report.txt", [
747 self.run_cmd(["annotate.py", "-s", self.example_path, "--comm", "SleepThread"])
748 self.check_exist(dirname="annotated_files")
749 self.check_file_under_dir("annotated_files", "native-lib.cpp")
751 self.check_annotation_summary(summary_file, [
758 self.run_cmd([INFERNO_SCRIPT, "-sc"])
759 self.check_inferno_report_html([('SleepThread', 80),
771 def test_smoke(self): argument
772 self.run_app_profiler()
773 self.run_cmd(["report.py", "-g", "--comms", "BusyThread", "-o", "report.txt"])
774 self.check_strings_in_file("report.txt", [
778 self.run_cmd(["annotate.py", "-s", self.example_path, "--comm", "BusyThread"])
779 self.check_exist(dirname="annotated_files")
780 self.check_file_under_dir("annotated_files", "native-lib.cpp")
782 self.check_annotation_summary(summary_file, [("native-lib.cpp", 5, 0), ("line 40", 5, 0)])
783 if self.use_compiled_java_code:
784 self.check_file_under_dir("annotated_files", "MixActivity.java")
785 self.check_annotation_summary(summary_file, [
792 self.run_cmd([INFERNO_SCRIPT, "-sc"])
830 def test_app_profiler(self): argument
831 self.common_test_app_profiler()
833 def test_app_profiler_profile_from_launch(self): argument
834 self.run_app_profiler(start_activity=True, build_binary_cache=False)
835 self.run_cmd(["report.py", "-g", "-o", "report.txt"])
836 self.check_strings_in_file("report.txt", [
840 def test_report(self): argument
841 self.common_test_report()
842 self.run_cmd(["report.py", "-g", "-o", "report.txt"])
843 self.check_strings_in_file("report.txt", [
847 def test_annotate(self): argument
848 if not self.use_compiled_java_code:
850 self.common_test_annotate()
851 self.check_file_under_dir("annotated_files", "MainActivity.kt")
853 self.check_annotation_summary(summary_file, [
860 def test_report_sample(self): argument
861 self.common_test_report_sample([
865 def test_pprof_proto_generator(self): argument
867 if self.use_compiled_java_code:
871 self.common_test_pprof_proto_generator(
876 def test_inferno(self): argument
877 self.common_test_inferno()
878 self.run_app_profiler()
879 self.run_cmd([INFERNO_SCRIPT, "-sc"])
880 self.check_inferno_report_html([('com.example.simpleperf.simpleperfexampleofkotlin.' +
883 def test_report_html(self): argument
884 self.common_test_report_html()
895 def test_app_profiler(self): argument
896 self.common_test_app_profiler()
906 def test_smoke(self): argument
907 self.run_app_profiler(record_arg="-g -f 1000 --duration 10 -e cpu-cycles:u --trace-offcpu")
908 self.run_cmd(["report.py", "-g", "-o", "report.txt"])
911 self.check_strings_in_file("report.txt", [
916 if self.use_compiled_java_code:
918 self.run_cmd(["annotate.py", "-s", self.example_path])
919 self.check_exist(dirname="annotated_files")
920 self.check_file_under_dir("annotated_files", "SleepActivity.kt")
922 self.check_annotation_summary(summary_file, [
930 self.run_cmd([INFERNO_SCRIPT, "-sc"])
931 self.check_inferno_report_html([
938 def setUp(self): argument
939 super(TestNativeProfiling, self).setUp()
940 self.is_rooted_device = TEST_HELPER.adb.switch_to_root()
942 def test_profile_cmd(self): argument
943 self.run_cmd(["app_profiler.py", "-cmd", "pm -l", "--disable_adb_root"])
944 self.run_cmd(["report.py", "-g", "-o", "report.txt"])
946 def test_profile_native_program(self): argument
947 if not self.is_rooted_device:
949 self.run_cmd(["app_profiler.py", "-np", "surfaceflinger"])
950 self.run_cmd(["report.py", "-g", "-o", "report.txt"])
951 self.run_cmd([INFERNO_SCRIPT, "-sc"])
952 self.run_cmd([INFERNO_SCRIPT, "-np", "surfaceflinger"])
954 def test_profile_pids(self): argument
955 if not self.is_rooted_device:
958 self.run_cmd(['app_profiler.py', '--pid', str(pid), '-r', '--duration 1'])
959 self.run_cmd(['app_profiler.py', '--pid', str(pid), str(pid), '-r', '--duration 1'])
960 self.run_cmd(['app_profiler.py', '--tid', str(pid), '-r', '--duration 1'])
961 self.run_cmd(['app_profiler.py', '--tid', str(pid), str(pid), '-r', '--duration 1'])
962 self.run_cmd([INFERNO_SCRIPT, '--pid', str(pid), '-t', '1'])
964 def test_profile_system_wide(self): argument
965 if not self.is_rooted_device:
967 self.run_cmd(['app_profiler.py', '--system_wide', '-r', '--duration 1'])
971 def setUp(self): argument
972 super(TestReportLib, self).setUp()
973 self.report_lib = ReportLib()
974 self.report_lib.SetRecordFile(TEST_HELPER.testdata_path('perf_with_symbols.data'))
976 def tearDown(self): argument
977 self.report_lib.Close()
978 super(TestReportLib, self).tearDown()
980 def test_build_id(self): argument
981 build_id = self.report_lib.GetBuildIdForPath('/data/t2')
982 self.assertEqual(build_id, '0x70f1fe24500fc8b0d9eb477199ca1ca21acca4de')
984 def test_symbol(self): argument
986 while self.report_lib.GetNextSample():
987 symbol = self.report_lib.GetSymbolOfCurrentSample()
990 self.assertEqual(symbol.symbol_addr, 0x4004ed)
991 self.assertEqual(symbol.symbol_len, 0x14)
992 self.assertTrue(found_func2)
994 def test_sample(self): argument
996 while self.report_lib.GetNextSample():
997 sample = self.report_lib.GetCurrentSample()
1000 self.assertEqual(sample.pid, 15926)
1001 self.assertEqual(sample.tid, 15926)
1002 self.assertEqual(sample.thread_comm, 't2')
1003 self.assertEqual(sample.cpu, 5)
1004 self.assertEqual(sample.period, 694614)
1005 event = self.report_lib.GetEventOfCurrentSample()
1006 self.assertEqual(event.name, 'cpu-cycles')
1007 callchain = self.report_lib.GetCallChainOfCurrentSample()
1008 self.assertEqual(callchain.nr, 0)
1009 self.assertTrue(found_sample)
1011 def test_meta_info(self): argument
1012 self.report_lib.SetRecordFile(TEST_HELPER.testdata_path('perf_with_trace_offcpu.data'))
1013 meta_info = self.report_lib.MetaInfo()
1014 self.assertTrue("simpleperf_version" in meta_info)
1015 self.assertEqual(meta_info["system_wide_collection"], "false")
1016 self.assertEqual(meta_info["trace_offcpu"], "true")
1017 self.assertEqual(meta_info["event_type_info"], "cpu-cycles,0,0\nsched:sched_switch,2,47")
1018 self.assertTrue("product_props" in meta_info)
1020 def test_event_name_from_meta_info(self): argument
1021 self.report_lib.SetRecordFile(TEST_HELPER.testdata_path('perf_with_tracepoint_event.data'))
1023 while self.report_lib.GetNextSample():
1024 event_names.add(self.report_lib.GetEventOfCurrentSample().name)
1025 self.assertTrue('sched:sched_switch' in event_names)
1026 self.assertTrue('cpu-cycles' in event_names)
1028 def test_record_cmd(self): argument
1029 self.report_lib.SetRecordFile(TEST_HELPER.testdata_path('perf_with_trace_offcpu.data'))
1030 self.assertEqual(self.report_lib.GetRecordCmd(),
1034 def test_offcpu(self): argument
1035 self.report_lib.SetRecordFile(TEST_HELPER.testdata_path('perf_with_trace_offcpu.data'))
1039 while self.report_lib.GetNextSample():
1040 sample = self.report_lib.GetCurrentSample()
1042 if self.report_lib.GetSymbolOfCurrentSample().symbol_name == sleep_function_name:
1045 callchain = self.report_lib.GetCallChainOfCurrentSample()
1050 self.assertEqual(self.report_lib.GetEventOfCurrentSample().name, 'cpu-cycles')
1052 self.assertGreater(sleep_percentage, 0.30)
1054 def test_show_art_frames(self): argument
1068 self.assertFalse(has_art_frame(report_lib))
1071 self.assertFalse(has_art_frame(report_lib))
1074 self.assertTrue(has_art_frame(report_lib))
1076 def test_merge_java_methods(self): argument
1091 self.assertEqual(parse_dso_names(report_lib), (False, True))
1095 self.assertEqual(parse_dso_names(report_lib), (False, True))
1099 self.assertEqual(parse_dso_names(report_lib), (True, False))
1101 def test_tracing_data(self): argument
1102 self.report_lib.SetRecordFile(TEST_HELPER.testdata_path('perf_with_tracepoint_event.data'))
1104 while self.report_lib.GetNextSample():
1105 event = self.report_lib.GetEventOfCurrentSample()
1106 tracing_data = self.report_lib.GetTracingDataOfCurrentSample()
1108 self.assertIsNotNone(tracing_data)
1109 self.assertIn('prev_pid', tracing_data)
1110 self.assertIn('next_comm', tracing_data)
1114 self.assertIsNone(tracing_data)
1115 self.assertTrue(has_tracing_data)
1119 def test_smoke(self): argument
1120 self.run_cmd(['run_simpleperf_on_device.py', 'list', '--show-features'])
1124 def test_addr2nearestline(self): argument
1125 self.run_addr2nearestline_test(True)
1126 self.run_addr2nearestline_test(False)
1128 def run_addr2nearestline_test(self, with_function_name): argument
1206 self.assertIsNotNone(dso, dso_path)
1218 self.assertEqual(len(expected_files), len(expected_functions))
1227 self.assertIsNotNone(actual_source, 'for %s:0x%x' %
1234 self.assertEqual(actual_source, expected_source,
1238 def test_objdump(self): argument
1282 self.assertIsNotNone(dso_info, dso_path)
1284 self.assertTrue(disassemble_code, dso_path)
1286 self.assertIn(
1290 def test_readelf(self): argument
1319 self.assertEqual(dso_info['arch'], readelf.get_arch(path))
1321 self.assertEqual(dso_info['build_id'], readelf.get_build_id(path), dso_path)
1323 self.assertEqual(dso_info['sections'], readelf.get_sections(path), dso_path)
1324 self.assertEqual(readelf.get_arch('not_exist_file'), 'unknown')
1325 self.assertEqual(readelf.get_build_id('not_exist_file'), '')
1326 self.assertEqual(readelf.get_sections('not_exist_file'), [])
1328 def test_source_file_searcher(self): argument
1335 self.assertEqual(
1339 self.assertEqual(
1343 self.assertEqual(
1348 self.assertEqual(
1353 def test_is_elf_file(self): argument
1354 self.assertTrue(is_elf_file(TEST_HELPER.testdata_path(
1359 self.assertFalse(is_elf_file('not_elf'))
1365 def setUp(self): argument
1366 super(TestNativeLibDownloader, self).setUp()
1367 self.adb = TEST_HELPER.adb
1368 self.adb.check_run(['shell', 'rm', '-rf', '/data/local/tmp/native_libs'])
1370 def tearDown(self): argument
1371 self.adb.check_run(['shell', 'rm', '-rf', '/data/local/tmp/native_libs'])
1372 super(TestNativeLibDownloader, self).tearDown()
1374 def list_lib_on_device(self, path): argument
1375 result, output = self.adb.run_and_return_output(
1379 def test_smoke(self): argument
1381 downloader = NativeLibDownloader(None, 'arm64', self.adb)
1384 self.assertEqual(len(downloader.host_build_id_map), 2)
1386 self.assertEqual(entry.score, 3)
1388 self.assertEqual(len(downloader.device_build_id_map), 0)
1398 self.assertEqual(len(downloader.device_build_id_map), sync_count)
1403 self.assertTrue(build_id in downloader.device_build_id_map)
1404 self.assertEqual(name, downloader.device_build_id_map[build_id])
1405 self.assertTrue(self.list_lib_on_device(downloader.dir_on_device + name))
1407 self.assertTrue(build_id not in downloader.device_build_id_map)
1408 self.assertFalse(self.list_lib_on_device(downloader.dir_on_device + name))
1410 self.adb.run(['pull', '/data/local/tmp/native_libs/build_id_list',
1413 self.assertEqual(bytes_to_str(fh.read()),
1417 def test_handle_wrong_build_id_list(self): argument
1420 self.adb.check_run(['shell', 'mkdir', '-p', '/data/local/tmp/native_libs'])
1421 self.adb.check_run(['push', 'build_id_list', '/data/local/tmp/native_libs'])
1423 downloader = NativeLibDownloader(None, 'arm64', self.adb)
1425 self.assertEqual(len(downloader.device_build_id_map), 0)
1427 def test_download_file_without_build_id(self): argument
1428 downloader = NativeLibDownloader(None, 'x86_64', self.adb)
1433 self.assertIn(name, downloader.no_build_id_file_map)
1437 target_file_stat = self.list_lib_on_device(target_file)
1438 self.assertTrue(target_file_stat)
1442 self.assertEqual(target_file_stat, self.list_lib_on_device(target_file))
1445 self.adb.check_run(['shell', 'truncate', '-s', '0', target_file])
1446 target_file_stat = self.list_lib_on_device(target_file)
1448 self.assertNotEqual(target_file_stat, self.list_lib_on_device(target_file))
1452 def test_long_callchain(self): argument
1453 self.run_cmd(['report_html.py', '-i',
1456 def test_aggregated_by_thread_name(self): argument
1460 self.run_cmd(['report_html.py', '--min_func_percent', '0', '-i',
1463 record_data = self._load_record_data_in_html('report.html')
1471 self.run_cmd(['report_html.py', '--aggregate-by-thread-name',
1475 record_data = self._load_record_data_in_html('report.html')
1481 self.assertEqual(thread['eventCount'],
1484 self.assertEqual(hit_count, len(event_count_for_thread_name))
1486 def test_no_empty_process(self): argument
1489 self.run_cmd(['report_html.py', '-i', perf_data])
1490 record_data = self._load_record_data_in_html('report.html')
1492 self.assertEqual(len(processes), 2)
1496 self.run_cmd(['report_html.py', '-i', perf_data, '--min_func_percent', '20'])
1497 record_data = self._load_record_data_in_html('report.html')
1499 self.assertEqual(len(processes), 1)
1501 def _load_record_data_in_html(self, html_file): argument
1507 self.assertNotEqual(start_pos, -1)
1509 self.assertNotEqual(start_pos, -1)
1512 self.assertNotEqual(end_pos, -1)
1518 def test_copy_binaries_from_symfs_dirs(self): argument
1521 self.assertIsNotNone(strip)
1522 symfs_dir = os.path.join(self.test_dir, 'symfs_dir')
1535 self.run_cmd([strip, '--strip-all', '-o', source_file, origin_file])
1537 self.assertTrue(filecmp.cmp(target_file, source_file))
1540 self.run_cmd([strip, '--strip-debug', '-o', source_file, origin_file])
1542 self.assertTrue(filecmp.cmp(target_file, source_file))
1547 self.assertTrue(filecmp.cmp(target_file, source_file))
1549 def test_copy_elf_without_build_id_from_symfs_dir(self): argument
1556 self.assertTrue(filecmp.cmp(target_file, source_file))
1558 self.assertTrue(filecmp.cmp(target_file, source_file))
1562 def run_api_test(self, package_name, apk_name, expected_reports, min_android_version): argument
1568 self.run_cmd(['api_profiler.py', 'prepare'])
1585 self.run_cmd(['api_profiler.py', 'collect', '-p', package_name, '-o', 'simpleperf_data'])
1588 self.assertGreater(len(names), 0)
1592 self.run_cmd(['report.py', '-g', '-o', 'report.txt', '-i', path])
1593 self.check_strings_in_file('report.txt', expected_reports)
1597 def run_cpp_api_test(self, apk_name, min_android_version): argument
1598 self.run_api_test('simpleperf.demo.cpp_api', apk_name, ['BusyThreadFunc'],
1601 def test_cpp_api_on_a_debuggable_app_targeting_prev_q(self): argument
1604 self.run_cpp_api_test('cpp_api-debug_prev_Q.apk', 'N')
1606 def test_cpp_api_on_a_debuggable_app_targeting_q(self): argument
1607 self.run_cpp_api_test('cpp_api-debug_Q.apk', 'N')
1609 def test_cpp_api_on_a_profileable_app_targeting_prev_q(self): argument
1611 self.run_cpp_api_test('cpp_api-profile_prev_Q.apk', 'Q')
1613 def test_cpp_api_on_a_profileable_app_targeting_q(self): argument
1614 self.run_cpp_api_test('cpp_api-profile_Q.apk', 'Q')
1616 def run_java_api_test(self, apk_name, min_android_version): argument
1617 self.run_api_test('simpleperf.demo.java_api', apk_name,
1621 def test_java_api_on_a_debuggable_app_targeting_prev_q(self): argument
1624 self.run_java_api_test('java_api-debug_prev_Q.apk', 'P')
1626 def test_java_api_on_a_debuggable_app_targeting_q(self): argument
1627 self.run_java_api_test('java_api-debug_Q.apk', 'P')
1629 def test_java_api_on_a_profileable_app_targeting_prev_q(self): argument
1631 self.run_java_api_test('java_api-profile_prev_Q.apk', 'Q')
1633 def test_java_api_on_a_profileable_app_targeting_q(self): argument
1634 self.run_java_api_test('java_api-profile_Q.apk', 'Q')
1638 def setUp(self): argument
1639 super(TestPprofProtoGenerator, self).setUp()
1644 def run_generator(self, options=None, testdata_file='perf_with_interpreter_frames.data'): argument
1647 self.run_cmd(['pprof_proto_generator.py', '-i', testdata_path] + options)
1648 return self.run_cmd(['pprof_proto_generator.py', '--show'], return_output=True)
1650 def generate_profile(self, options, testdata_files): argument
1653 self.run_cmd(['pprof_proto_generator.py', '-i'] + testdata_paths + options)
1656 def test_show_art_frames(self): argument
1659 self.assertNotIn(art_frame_str, self.run_generator())
1661 self.assertIn(art_frame_str, self.run_generator(['--show_art_frames']))
1663 def test_pid_filter(self): argument
1665 self.assertIn(key, self.run_generator())
1666 self.assertIn(key, self.run_generator(['--pid', '10419']))
1667 self.assertIn(key, self.run_generator(['--pid', '10419', '10416']))
1668 self.assertNotIn(key, self.run_generator(['--pid', '10416']))
1670 def test_tid_filter(self): argument
1674 output = self.run_generator(options)
1675 self.assertIn(key1, output)
1676 self.assertIn(key2, output)
1677 output = self.run_generator(['--tid', '10459'])
1678 self.assertIn(key1, output)
1679 self.assertNotIn(key2, output)
1680 output = self.run_generator(['--tid', '10463'])
1681 self.assertNotIn(key1, output)
1682 self.assertIn(key2, output)
1684 def test_comm_filter(self): argument
1688 output = self.run_generator(options)
1689 self.assertIn(key1, output)
1690 self.assertIn(key2, output)
1691 output = self.run_generator(['--comm', 'Profile Saver'])
1692 self.assertIn(key1, output)
1693 self.assertNotIn(key2, output)
1694 output = self.run_generator(['--comm', 'e.sample.tunnel'])
1695 self.assertNotIn(key1, output)
1696 self.assertIn(key2, output)
1698 def test_build_id(self): argument
1700 self.assertIn('build_id: e3e938cc9e40de2cfe1a5ac7595897de(', self.run_generator())
1702 def test_location_address(self): argument
1706 profile = self.generate_profile(None, ['perf_with_interpreter_frames.data'])
1710 self.assertLessEqual(mapping.memory_start, location.address)
1711 self.assertGreaterEqual(mapping.memory_limit, location.address)
1713 def test_multiple_perf_data(self): argument
1715 profile1 = self.generate_profile(None, ['aggregatable_perf1.data'])
1716 profile2 = self.generate_profile(None, ['aggregatable_perf2.data'])
1717 profile_both = self.generate_profile(
1720 self.assertGreater(len(profile_both.sample), len(profile1.sample))
1721 self.assertGreater(len(profile_both.sample), len(profile2.sample))
1725 def setUp(self): argument
1726 super(TestRecordingRealApps, self).setUp()
1727 self.adb = TEST_HELPER.adb
1728 self.installed_packages = []
1730 def tearDown(self): argument
1731 for package in self.installed_packages:
1732 self.adb.run(['shell', 'pm', 'uninstall', package])
1733 super(TestRecordingRealApps, self).tearDown()
1735 def install_apk(self, apk_path, package_name): argument
1736 self.adb.run(['uninstall', package_name])
1737 self.adb.run(['install', '-t', apk_path])
1738 self.installed_packages.append(package_name)
1740 def start_app(self, start_cmd): argument
1741 subprocess.Popen(self.adb.adb_path + ' ' + start_cmd, shell=True,
1744 def record_data(self, package_name, record_arg): argument
1745 self.run_cmd(['app_profiler.py', '--app', package_name, '-r', record_arg])
1747 def check_symbol_in_record_file(self, symbol_name): argument
1748 self.run_cmd(['report.py', '--children', '-o', 'report.txt'])
1749 self.check_strings_in_file('report.txt', [symbol_name])
1751 def test_recording_displaybitmaps(self): argument
1752 self.install_apk(TEST_HELPER.testdata_path('DisplayBitmaps.apk'),
1754 self.install_apk(TEST_HELPER.testdata_path('DisplayBitmapsTest.apk'),
1756 self.start_app('shell am instrument -w -r -e debug false -e class ' +
1760 self.record_data('com.example.android.displayingbitmaps', '-e cpu-clock -g --duration 10')
1762 self.check_symbol_in_record_file('androidx.test.espresso')
1764 def test_recording_endless_tunnel(self): argument
1765 self.install_apk(TEST_HELPER.testdata_path(
1767 self.start_app('shell am start -n com.google.sample.tunnel/android.app.NativeActivity -a ' +
1769 self.record_data('com.google.sample.tunnel', '-e cpu-clock -g --duration 10')
1770 self.check_symbol_in_record_file('PlayScene::DoFrame')