iOS 12 Simulator Release WK2 frequently timing out while running layout tests
[WebKit-https.git] / Tools / Scripts / webkitpy / port / image_diff.py
1 # Copyright (C) 2010 Google Inc. All rights reserved.
2 # Copyright (C) 2010 Gabor Rapcsanyi <rgabor@inf.u-szeged.hu>, University of Szeged
3 # Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
8 #
9 #     * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #     * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
14 # distribution.
15 #     * Neither the Google name nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 """WebKit implementations of the Port interface."""
32
33 import logging
34 import re
35 import time
36
37 from webkitpy.port import server_process
38
39
40 _log = logging.getLogger(__name__)
41
42
43 class ImageDiffer(object):
44     def __init__(self, port):
45         self._port = port
46         self._tolerance = None
47         self._process = None
48
49     def diff_image(self, expected_contents, actual_contents, tolerance):
50         if tolerance != self._tolerance or (self._process and self._process.has_available_stdout()):
51             self.stop()
52         try:
53             assert(expected_contents)
54             assert(actual_contents)
55             assert(tolerance is not None)
56
57             if not self._process:
58                 self._start(tolerance)
59             # Note that although we are handed 'old', 'new', ImageDiff wants 'new', 'old'.
60             self._process.write('Content-Length: %d\n%sContent-Length: %d\n%s' % (
61                 len(actual_contents), actual_contents,
62                 len(expected_contents), expected_contents))
63             return self._read()
64         except IOError as exception:
65             return (None, 0, "Failed to compute an image diff: %s" % str(exception))
66
67     def _start(self, tolerance):
68         command = [self._port._path_to_image_diff(), '--tolerance', str(tolerance)]
69         if self._port._should_use_jhbuild():
70             command = self._port._jhbuild_wrapper + command
71         environment = self._port.setup_environ_for_server('ImageDiff')
72         self._process = self._port._server_process_constructor(self._port, 'ImageDiff', command, environment)
73         self._process.start()
74         self._tolerance = tolerance
75
76     def _read(self):
77         deadline = time.time() + 2.0
78         output = None
79         output_image = ""
80
81         while not self._process.timed_out and not self._process.has_crashed():
82             output = self._process.read_stdout_line(deadline)
83             if self._process.timed_out or self._process.has_crashed() or not output:
84                 break
85
86             if output.startswith('diff'):  # This is the last line ImageDiff prints.
87                 break
88
89             if output.startswith('Content-Length'):
90                 m = re.match('Content-Length: (\d+)', output)
91                 content_length = int(m.group(1))
92                 output_image = self._process.read_stdout(deadline, content_length)
93                 output = self._process.read_stdout_line(deadline)
94                 break
95
96         stderr = self._process.pop_all_buffered_stderr()
97         err_str = ''
98         if stderr:
99             err_str += "ImageDiff produced stderr output:\n" + stderr
100         if self._process.timed_out:
101             err_str += "ImageDiff timed out\n"
102         if self._process.has_crashed():
103             err_str += "ImageDiff crashed\n"
104
105         diff_percent = 0
106         if output and output.startswith('diff'):
107             m = re.match('diff: (.+)% (passed|failed)', output)
108             if m.group(2) == 'passed':
109                 return (None, 0, None)
110             diff_percent = float(m.group(1))
111
112         return (output_image, diff_percent, err_str or None)
113
114     def stop(self):
115         if self._process:
116             self._process.stop()
117             self._process = None