Make cleanup more robust and minor code cleaning in run benchmark script.
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 10 Aug 2015 23:44:10 +0000 (23:44 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 10 Aug 2015 23:44:10 +0000 (23:44 +0000)
https://bugs.webkit.org/show_bug.cgi?id=147800

Patch by Dewei Zhu <dewei_zhu@apple.com> on 2015-08-10
Reviewed by Ryosuke Niwa.

Use 'finnaly' block to make sure cleanup code is always executed.

* Scripts/webkitpy/benchmark_runner/benchmark_runner.py:
(built_benchmark):
(built_benchmark.__init__):
(built_benchmark.__enter__):
(built_benchmark.__exit__):
(test_environment):
(test_environment.__init__):
(test_environment.__enter__):
(test_environment.__exit__):
(BenchmarkRunner.__init__):
(BenchmarkRunner.execute):
(BenchmarkRunner._dump):
(BenchmarkRunner._wrap):
(BenchmarkRunner): Deleted.
(BenchmarkRunner._cleanup): Deleted.
* Scripts/webkitpy/benchmark_runner/browser_driver/browser_driver.py:
(BrowserDriver.close_browsers):
(BrowserDriver):
(BrowserDriver.restore_env):
* Scripts/webkitpy/benchmark_runner/http_server_driver/simple_http_server_driver.py:
(SimpleHTTPServerDriver.__init__): We do not actually need to know external ip address for now.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@188237 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Tools/ChangeLog
Tools/Scripts/webkitpy/benchmark_runner/benchmark_builder.py
Tools/Scripts/webkitpy/benchmark_runner/benchmark_runner.py
Tools/Scripts/webkitpy/benchmark_runner/browser_driver/browser_driver.py
Tools/Scripts/webkitpy/benchmark_runner/http_server_driver/simple_http_server_driver.py

index d41aa32..8a79765 100644 (file)
@@ -1,3 +1,34 @@
+2015-08-10  Dewei Zhu  <dewei_zhu@apple.com>
+
+        Make cleanup more robust and minor code cleaning in run benchmark script.
+        https://bugs.webkit.org/show_bug.cgi?id=147800
+
+        Reviewed by Ryosuke Niwa.
+
+        Use 'finnaly' block to make sure cleanup code is always executed.
+
+        * Scripts/webkitpy/benchmark_runner/benchmark_runner.py:
+        (built_benchmark):
+        (built_benchmark.__init__):
+        (built_benchmark.__enter__):
+        (built_benchmark.__exit__):
+        (test_environment):
+        (test_environment.__init__):
+        (test_environment.__enter__):
+        (test_environment.__exit__):
+        (BenchmarkRunner.__init__):
+        (BenchmarkRunner.execute):
+        (BenchmarkRunner._dump):
+        (BenchmarkRunner._wrap):
+        (BenchmarkRunner): Deleted.
+        (BenchmarkRunner._cleanup): Deleted.
+        * Scripts/webkitpy/benchmark_runner/browser_driver/browser_driver.py:
+        (BrowserDriver.close_browsers):
+        (BrowserDriver):
+        (BrowserDriver.restore_env):
+        * Scripts/webkitpy/benchmark_runner/http_server_driver/simple_http_server_driver.py:
+        (SimpleHTTPServerDriver.__init__): We do not actually need to know external ip address for now.
+
 2015-08-10  Youenn Fablet  <youenn.fablet@crf.canon.fr>
 
         Compile warning (-Wsign-compare) on 32-bits at WebCore/platform/FileSystem.cpp
index 72b0a2a..e6c3382 100644 (file)
@@ -16,30 +16,36 @@ _log = logging.getLogger(__name__)
 
 
 class BenchmarkBuilder(object):
-    def prepare(self, name, plan):
+    def __init__(self, name, plan):
         self._name = name
+        self._plan = plan
+
+    def __enter__(self):
         self._web_root = tempfile.mkdtemp()
         self._dest = os.path.join(self._web_root, self._name)
-        if 'local_copy' in plan:
-            self._copy_benchmark_to_temp_dir(plan['local_copy'])
-        elif 'remote_archive' in plan:
-            self._fetch_remote_archive(plan['remote_archive'])
-        elif 'svn_source' in plan:
-            self._checkout_with_subversion(plan['svn_source'])
+        if 'local_copy' in self._plan:
+            self._copy_benchmark_to_temp_dir(self._plan['local_copy'])
+        elif 'remote_archive' in self._plan:
+            self._fetch_remote_archive(self._plan['remote_archive'])
+        elif 'svn_source' in self._plan:
+            self._checkout_with_subversion(self._plan['svn_source'])
         else:
             raise Exception('The benchmark location was not specified')
 
         _log.info('Copied the benchmark into: %s' % self._dest)
         try:
-            if 'create_script' in plan:
+            if 'create_script' in self._plan:
                 self._run_create_script(plan['create_script'])
-            if 'benchmark_patch' in plan:
-                self._apply_patch(plan['benchmark_patch'])
+            if 'benchmark_patch' in self._plan:
+                self._apply_patch(self._plan['benchmark_patch'])
             return self._web_root
         except Exception:
-            self.clean()
+            self._clean()
             raise
 
+    def __exit__(self, exc_type, exc_value, traceback):
+        self._clean()
+
     def _run_create_script(self, create_script):
         old_working_directory = os.getcwd()
         os.chdir(self._dest)
@@ -92,7 +98,7 @@ class BenchmarkBuilder(object):
         if error_code:
             raise Exception('Cannot apply patch, will skip current benchmark_path - Error: %s' % error_code)
 
-    def clean(self):
+    def _clean(self):
         _log.info('Cleaning Benchmark')
         if self._web_root:
             force_remove(self._web_root)
index 5d82822..26f883d 100644 (file)
@@ -41,10 +41,10 @@ class BenchmarkRunner(object):
                 self._output_file = output_file
                 self._device_id = device_id
         except IOError as error:
-            _log.error('Can not open plan file: %s - Error %s' % (plan_file, error))
+            _log.error('Can not open plan file: {plan_file} - Error {error}'.format(plan_file=plan_file, error=error))
             raise error
         except ValueError as error:
-            _log.error('Plan file: %s may not follow JSON format - Error %s' % (plan_file, error))
+            _log.error('Plan file: {plan_file} may not follow JSON format - Error {error}'.format(plan_file=plan_file, error=error))
             raise error
 
     def _find_plan_file(self, plan_file):
@@ -58,46 +58,33 @@ class BenchmarkRunner(object):
                 return absPath
         return plan_file
 
-    def execute(self):
-        _log.info('Start to execute the plan')
-        _log.info('Start a new benchmark')
+    def _run_benchmark(self, count, web_root):
         results = []
-        self._benchmark_builder = BenchmarkBuilder()
-
-        web_root = self._benchmark_builder.prepare(self._plan_name, self._plan)
-        for x in xrange(int(self._plan['count'])):
-            _log.info('Start the iteration %d of current benchmark' % (x + 1))
-            self._http_server_driver.serve(web_root)
-            self._browser_driver.prepare_env(self._device_id)
-            url = urlparse.urljoin(self._http_server_driver.base_url(), self._plan_name + '/' + self._plan['entry_point'])
-            self._browser_driver.launch_url(url, self._build_dir)
-            result = None
+        for iteration in xrange(1, count + 1):
+            _log.info('Start the iteration {current_iteration} of current benchmark'.format(current_iteration=iteration))
             try:
+                result = None
+                self._http_server_driver.serve(web_root)
+                self._browser_driver.prepare_env(self._device_id)
+                url = urlparse.urljoin(self._http_server_driver.base_url(), self._plan_name + '/' + self._plan['entry_point'])
+                self._browser_driver.launch_url(url, self._build_dir)
                 with timeout(self._plan['timeout']):
                     result = self._http_server_driver.fetch_result()
-                assert(not self._http_server_driver.get_return_code())
-                assert(result)
-                results.append(json.loads(result))
-            except Exception as error:
-                _log.error('No result or the server crashed. Something went wrong. Will skip current benchmark.\nError: %s, Server return code: %d, result: %s' % (error, self._http_server_driver.get_return_code(), result))
-                self._cleanup()
-                sys.exit(1)
+                    assert(not self._http_server_driver.get_return_code())
+                    assert(result)
+                    results.append(json.loads(result))
             finally:
+                self._browser_driver.restore_env()
                 self._browser_driver.close_browsers()
-                _log.info('End of %d iteration of current benchmark' % (x + 1))
+                self._http_server_driver.kill_server()
+            _log.info('End of {current_iteration} iteration of current benchmark'.format(current_iteration=iteration))
         results = self._wrap(results)
         self._dump(results, self._output_file if self._output_file else self._plan['output_file'])
         self._show_results(results)
-        self._benchmark_builder.clean()
-        sys.exit()
 
-    def _cleanup(self):
-        if self._browser_driver:
-            self._browser_driver.close_browsers()
-        if self._http_server_driver:
-            self._http_server_driver.kill_server()
-        if self._benchmark_builder:
-            self._benchmark_builder.clean()
+    def execute(self):
+        with BenchmarkBuilder(self._plan_name, self._plan) as web_root:
+            self._run_benchmark(int(self._plan['count']), web_root)
 
     @classmethod
     def _dump(cls, results, output_file):
@@ -106,18 +93,18 @@ class BenchmarkRunner(object):
             with open(output_file, 'w') as fp:
                 json.dump(results, fp)
         except IOError as error:
-            _log.error('Cannot open output file: %s - Error: %s' % (output_file, error))
-            _log.error('Results are:\n %s', json.dumps(results))
+            _log.error('Cannot open output file: {output_file} - Error: {error}'.format(output_file=output_file, error=error))
+            _log.error('Results are:\n {result}'.format(json.dumps(results)))
 
     @classmethod
     def _wrap(cls, dicts):
-        _log.debug('Merging following results:\n%s', json.dumps(dicts))
+        _log.debug('Merging following results:\n{results}'.format(results=json.dumps(dicts)))
         if not dicts:
             return None
         ret = {}
         for dic in dicts:
             ret = cls._merge(ret, dic)
-        _log.debug('Results after merging:\n%s', json.dumps(ret))
+        _log.debug('Results after merging:\n{result}'.format(result=json.dumps(ret)))
         return ret
 
     @classmethod
index 95dad01..ab1c553 100644 (file)
@@ -18,3 +18,7 @@ class BrowserDriver(object):
     @abc.abstractmethod
     def close_browsers(self):
         pass
+
+    @abc.abstractmethod
+    def restore_env(self):
+        pass
index 86621da..135990d 100644 (file)
@@ -24,14 +24,7 @@ class SimpleHTTPServerDriver(HTTPServerDriver):
     def __init__(self):
         self._server_process = None
         self._server_port = 0
-        # FIXME: This may not be reliable.
-        _log.info('Finding the IP address of current machine')
-        try:
-            self._ip = [ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][0]
-            _log.info('IP of current machine is: %s' % self._ip)
-        except Exception as error:
-            _log.error('Cannot get the ip address of current machine - Error: %s' % error)
-            raise
+        self._ip = '127.0.0.1'
 
     def serve(self, web_root):
         _log.info('Launching an http server')