OSDN Git Service

simpleperf: allow inferno to run from any dir.
authorYabin Cui <yabinc@google.com>
Wed, 20 Sep 2017 23:20:04 +0000 (16:20 -0700)
committerYabin Cui <yabinc@google.com>
Wed, 20 Sep 2017 23:20:04 +0000 (16:20 -0700)
Previously inferno.sh/.bat can only run from scripts/, this CL
removes the restriction.

Also add -o option to select report path.

Bug: http://b/32834638
Test: run test.py.
Change-Id: Ib1651dcd1beedac5be00249150e0b74fea906924

simpleperf/scripts/inferno.bat
simpleperf/scripts/inferno.sh
simpleperf/scripts/inferno/inferno.py
simpleperf/scripts/test.py

index a2d18a7..5818f98 100644 (file)
@@ -1 +1,2 @@
+set PYTHONPATH=%PYTHONPATH%;%~dp0
 python -m inferno.inferno %*
index 8ba5097..d30ee31 100755 (executable)
@@ -1,2 +1,4 @@
 #!/bin/bash
+SCRIPTPATH=$(dirname "$0")
+export PYTHONPATH=$SCRIPTPATH:$PYTHONPATH
 python -m inferno.inferno "$@"
index a7c60fa..5060a35 100644 (file)
@@ -36,19 +36,17 @@ import subprocess
 import sys
 import webbrowser
 
-try:
-    from simpleperf_report_lib import ReportLib
-    from utils import log_exit, log_info, AdbHelper
-except:
-    print("Please go to the parent directory, and run inferno.sh or inferno.bat.")
-    sys.exit(1)
+scripts_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
+sys.path.append(scripts_path)
+from simpleperf_report_lib import ReportLib
+from utils import log_exit, log_info, AdbHelper
 
 from data_types import *
 from svg_renderer import *
 
 
 def collect_data(args):
-    app_profiler_args = [sys.executable, "app_profiler.py", "-nb"]
+    app_profiler_args = [sys.executable, os.path.join(scripts_path, "app_profiler.py"), "-nb"]
     if args.app:
         app_profiler_args += ["-p", args.app]
     elif args.native_program:
@@ -141,7 +139,7 @@ def output_report(process, args):
     :param process: Process object
     :return: str, absolute path to the file
     """
-    f = open('report.html', 'w')
+    f = open(args.report_path, 'w')
     filepath = os.path.realpath(f.name)
     f.write("<html>")
     f.write("<head>")
@@ -274,6 +272,7 @@ def main():
                         default="")
     parser.add_argument('--disable_adb_root', action='store_true', help="""Force adb to run in non
                         root mode.""")
+    parser.add_argument('-o', '--report_path', default='report.html', help="Set report path.")
     args = parser.parse_args()
     process = Process("", 0)
 
index cbdd27b..e75a328 100644 (file)
@@ -51,7 +51,7 @@ try:
 except:
     has_google_protobuf = False
 
-inferno_script = "inferno.bat" if is_windows() else "./inferno.sh"
+inferno_script = os.path.join(get_script_dir(), "inferno.bat" if is_windows() else "./inferno.sh")
 
 support_trace_offcpu = None
 
@@ -225,8 +225,7 @@ class TestExampleBase(TestBase):
                             fulfilled[i] = True
         self.assertEqual(len(fulfilled), sum([int(x) for x in fulfilled]), fulfilled)
 
-    def check_inferno_report_html(self, check_entries):
-        file = "report.html"
+    def check_inferno_report_html(self, check_entries, file="report.html"):
         self.check_exist(file=file)
         with open(file, 'r') as fh:
             data = fh.read()
@@ -403,6 +402,21 @@ class TestExamplePureJava(TestExampleBase):
         self.run_cmd([inferno_script, "-sc"])
         self.check_inferno_report_html(
             [('com.example.simpleperf.simpleperfexamplepurejava.MainActivity$1.run()', 80)])
+        self.run_cmd([inferno_script, "-sc", "-o", "report2.html"])
+        self.check_inferno_report_html(
+            [('com.example.simpleperf.simpleperfexamplepurejava.MainActivity$1.run()', 80)],
+            "report2.html")
+        remove("report2.html")
+
+    def test_inferno_in_another_dir(self):
+        test_dir = 'inferno_testdir'
+        saved_dir = os.getcwd()
+        remove(test_dir)
+        os.mkdir(test_dir)
+        os.chdir(test_dir)
+        self.run_cmd([inferno_script])
+        os.chdir(saved_dir)
+        remove(test_dir)
 
 
 class TestExamplePureJavaRoot(TestExampleBase):