run_page_cycler.py revision 71716223201a01a4f4b6fe182ad97720b7d06124
1ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project#!/usr/bin/python
2ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
3ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project"""Run page cycler tests using Android instrumentation.
4ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
5ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  First, you need to get an SD card or sdcard image that has page cycler tests.
6ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
7ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  Usage:
8ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    Run a single page cycler test:
971716223201a01a4f4b6fe182ad97720b7d06124Guang Zhu      run_page_cycler.py "file:///sdcard/webkit/page_cycler/moz/start.html\?auto=1\&iterations=10"
10ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project"""
11ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
12ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Projectimport logging
13ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Projectimport optparse
14ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Projectimport os
15ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Projectimport subprocess
16ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Projectimport sys
17ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Projectimport time
18ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
19ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
20ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
21ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Projectdef main(options, args):
22ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  """Run the tests. Will call sys.exit when complete.
235dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu
24ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  """
25ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
26ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  # Set up logging format.
27ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  log_level = logging.INFO
28ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  if options.verbose:
29ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    log_level = logging.DEBUG
30ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  logging.basicConfig(level=log_level,
31ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project                      format='%(message)s')
32ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
33ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  # Include all tests if none are specified.
34ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  if not args:
3571716223201a01a4f4b6fe182ad97720b7d06124Guang Zhu    print "need a URL, e.g. file:///sdcard/webkit/page_cycler/moz/start.html\?auto=1\&iterations=10"
36ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    sys.exit(1)
37ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  else:
38ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    path = ' '.join(args);
39ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
40ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  adb_cmd = "adb ";
41ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  if options.adb_options:
42ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    adb_cmd += options.adb_options
43ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
44ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  logging.info("Running the test ...")
45ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
46ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  # Count crashed tests.
47ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  crashed_tests = []
48ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
49ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  timeout_ms = '0'
50ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  if options.time_out_ms:
51ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    timeout_ms = options.time_out_ms
52ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
53ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  # Run test until it's done
54ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
55ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  run_load_test_cmd_prefix = adb_cmd + " shell am instrument"
56ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  run_load_test_cmd_postfix = " -w com.android.dumprendertree/.LayoutTestsAutoRunner"
57ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
58ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  # Call LoadTestsAutoTest::runTest.
595dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu  run_load_test_cmd = run_load_test_cmd_prefix + " -e class com.android.dumprendertree.LoadTestsAutoTest#runPageCyclerTest -e path \"" + path + "\" -e timeout " + timeout_ms
605dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu
615dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu  if options.drawtime:
625dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu    run_load_test_cmd += " -e drawtime true "
635dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu
645dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu  if options.save_image:
655dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu    run_load_test_cmd += " -e saveimage \"%s\"" % options.save_image
665dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu
675dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu  run_load_test_cmd += run_load_test_cmd_postfix
68ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
69ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  (adb_output, adb_error) = subprocess.Popen(run_load_test_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
7060ef21a5daff22dca1c562da8d131c67c8587353Guang Zhu  fail_flag = False
7160ef21a5daff22dca1c562da8d131c67c8587353Guang Zhu  for line in adb_output.splitlines():
7260ef21a5daff22dca1c562da8d131c67c8587353Guang Zhu    line = line.strip()
7360ef21a5daff22dca1c562da8d131c67c8587353Guang Zhu    if line.find('INSTRUMENTATION_CODE') == 0:
7460ef21a5daff22dca1c562da8d131c67c8587353Guang Zhu      if not line[22:] == '-1':
7560ef21a5daff22dca1c562da8d131c67c8587353Guang Zhu        fail_flag = True
7660ef21a5daff22dca1c562da8d131c67c8587353Guang Zhu        break
7760ef21a5daff22dca1c562da8d131c67c8587353Guang Zhu    if (line.find('INSTRUMENTATION_FAILED') != -1 or
7860ef21a5daff22dca1c562da8d131c67c8587353Guang Zhu        line.find('Process crashed.') != -1):
7960ef21a5daff22dca1c562da8d131c67c8587353Guang Zhu      fail_flag = True
8060ef21a5daff22dca1c562da8d131c67c8587353Guang Zhu      break
8160ef21a5daff22dca1c562da8d131c67c8587353Guang Zhu  if fail_flag:
82ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    logging.error("Error happened : " + adb_output)
83ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    sys.exit(1)
84ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
85ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  logging.info(adb_output);
86ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  logging.info(adb_error);
87ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  logging.info("Done\n");
88ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
89ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  # Pull results from /sdcard/load_test_result.txt
90ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  results_dir = options.results_directory
91ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  if not os.path.exists(results_dir):
92ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    os.makedirs(results_dir)
93ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  if not os.path.isdir(results_dir):
94ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    logging.error("Cannot create results dir: " + results_dir)
95ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project    sys.exit(1)
96ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
97ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  result_file = "/sdcard/load_test_result.txt"
98ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  shell_cmd_str = adb_cmd + " pull " + result_file + " " + results_dir
99ce8d9d1672e8321466aab745083233239422557dGuang Zhu  (adb_output, err) = subprocess.Popen(
100ce8d9d1672e8321466aab745083233239422557dGuang Zhu      shell_cmd_str, shell=True,
101ce8d9d1672e8321466aab745083233239422557dGuang Zhu      stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
102ce8d9d1672e8321466aab745083233239422557dGuang Zhu  if not os.path.isfile(os.path.join(results_dir, "load_test_result.txt")):
103ce8d9d1672e8321466aab745083233239422557dGuang Zhu    logging.error("Failed to pull result file.")
104ce8d9d1672e8321466aab745083233239422557dGuang Zhu    logging.error("adb stdout:")
105ce8d9d1672e8321466aab745083233239422557dGuang Zhu    logging.error(adb_output)
106ce8d9d1672e8321466aab745083233239422557dGuang Zhu    logging.error("adb stderr:")
107ce8d9d1672e8321466aab745083233239422557dGuang Zhu    logging.error(err)
108ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  logging.info("Results are stored under: " + results_dir + "/load_test_result.txt\n")
109ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
110ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Projectif '__main__' == __name__:
111ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  option_parser = optparse.OptionParser()
1125dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu  option_parser.add_option("-t", "--time-out-ms",
113ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project                           default=None,
114ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project                           help="set the timeout for each test")
1155dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu  option_parser.add_option("-v", "--verbose", action="store_true",
116ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project                           default=False,
117ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project                           help="include debug-level logging")
1185dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu  option_parser.add_option("-a", "--adb-options",
119ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project                           default=None,
120ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project                           help="pass options to adb, such as -d -e, etc");
1215dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu  option_parser.add_option("-r", "--results-directory",
122ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project                           default="layout-test-results",
123ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project                           help="directory which results are stored.")
124ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project
1255dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu  option_parser.add_option("-d", "--drawtime", action="store_true",
1265dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu                           default=False,
1275dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu                           help="log draw time for each page rendered.")
1285dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu
1295dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu  option_parser.add_option("-s", "--save-image",
1305dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu                           default=None,
1315dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu                           help="stores rendered page to a location on device.")
1325dc4f21ab6360b45f464c1451f8d403dd4df3c63Guang Zhu
133ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  options, args = option_parser.parse_args();
134ba87e3e6c985e7175152993b5efcc7dd2f0e1c93The Android Open Source Project  main(options, args)
135