| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Base classes for a test and validator which upload results | 5 """Base classes for a test and validator which upload results |
| 6 (reference images, error images) to cloud storage.""" | 6 (reference images, error images) to cloud storage.""" |
| 7 | 7 |
| 8 import cv2 |
| 8 import os | 9 import os |
| 9 import re | 10 import re |
| 10 import tempfile | 11 import tempfile |
| 11 | 12 |
| 12 from telemetry import benchmark | 13 from telemetry import benchmark |
| 13 from telemetry.core import bitmap | 14 from telemetry.core import bitmap |
| 14 from telemetry.page import page_test | 15 from telemetry.page import page_test |
| 15 from telemetry.util import cloud_storage | 16 from telemetry.util import cloud_storage |
| 16 | 17 |
| 17 | 18 |
| 18 test_data_dir = os.path.abspath(os.path.join( | 19 test_data_dir = os.path.abspath(os.path.join( |
| 19 os.path.dirname(__file__), '..', '..', 'data', 'gpu')) | 20 os.path.dirname(__file__), '..', '..', 'data', 'gpu')) |
| 20 | 21 |
| 21 default_generated_data_dir = os.path.join(test_data_dir, 'generated') | 22 default_generated_data_dir = os.path.join(test_data_dir, 'generated') |
| 22 | 23 |
| 23 error_image_cloud_storage_bucket = 'chromium-browser-gpu-tests' | 24 error_image_cloud_storage_bucket = 'chromium-browser-gpu-tests' |
| 24 | 25 |
| 25 def _CompareScreenshotSamples(screenshot, expectations, device_pixel_ratio): | 26 def _CompareScreenshotSamples(screenshot, expectations, device_pixel_ratio): |
| 26 for expectation in expectations: | 27 for expectation in expectations: |
| 27 location = expectation["location"] | 28 location = expectation["location"] |
| 28 x = int(location[0] * device_pixel_ratio) | 29 x = int(location[0] * device_pixel_ratio) |
| 29 y = int(location[1] * device_pixel_ratio) | 30 y = int(location[1] * device_pixel_ratio) |
| 30 | 31 |
| 31 if x < 0 or y < 0 or x > screenshot.width or y > screenshot.height: | 32 if x < 0 or y < 0 or x > screenshot.width or y > screenshot.height: |
| 32 raise page_test.Failure( | 33 raise page_test.Failure( |
| 33 'Expected pixel location [%d, %d] is out of range on [%d, %d] image' % | 34 'Expected pixel location [%d, %d] is out of range on [%d, %d] image' % |
| 34 (x, y, screenshot.width, screenshot.height)) | 35 (x, y, screenshot.width, screenshot.height)) |
| 35 | 36 |
| 36 actual_color = screenshot.GetPixelColor(x, y) | 37 actual_color = screenshot.image[y][x] |
| 37 expected_color = bitmap.RgbaColor( | 38 expected_color = (expectation["color"][2], expectation["color"][1], |
| 38 expectation["color"][0], | 39 expectation["color"][0]) |
| 39 expectation["color"][1], | 40 if not bitmap.ColorsAreEqual(actual_color, expected_color, |
| 40 expectation["color"][2]) | 41 expectation["tolerance"]): |
| 41 if not actual_color.IsEqual(expected_color, expectation["tolerance"]): | 42 raise page_test.Failure( |
| 42 raise page_test.Failure('Expected pixel at ' + str(location) + | 43 'Expected pixel at %s to be %s but got [%s, %s, %s]' % |
| 43 ' to be ' + | 44 (location, expectation["color"], actual_color[2], actual_color[1], |
| 44 str(expectation["color"]) + " but got [" + | 45 actual_color[0])) |
| 45 str(actual_color.r) + ", " + | |
| 46 str(actual_color.g) + ", " + | |
| 47 str(actual_color.b) + "]") | |
| 48 | 46 |
| 49 class ValidatorBase(page_test.PageTest): | 47 class ValidatorBase(page_test.PageTest): |
| 50 def __init__(self): | 48 def __init__(self): |
| 51 super(ValidatorBase, self).__init__() | 49 super(ValidatorBase, self).__init__() |
| 52 # Parameters for cloud storage reference images. | 50 # Parameters for cloud storage reference images. |
| 53 self.vendor_id = None | 51 self.vendor_id = None |
| 54 self.device_id = None | 52 self.device_id = None |
| 55 self.vendor_string = None | 53 self.vendor_string = None |
| 56 self.device_string = None | 54 self.device_string = None |
| 57 self.msaa = False | 55 self.msaa = False |
| 58 | 56 |
| 59 ### | 57 ### |
| 60 ### Routines working with the local disk (only used for local | 58 ### Routines working with the local disk (only used for local |
| 61 ### testing without a cloud storage account -- the bots do not use | 59 ### testing without a cloud storage account -- the bots do not use |
| 62 ### this code path). | 60 ### this code path). |
| 63 ### | 61 ### |
| 64 | 62 |
| 65 def _UrlToImageName(self, url): | 63 def _UrlToImageName(self, url): |
| 66 image_name = re.sub(r'^(http|https|file)://(/*)', '', url) | 64 image_name = re.sub(r'^(http|https|file)://(/*)', '', url) |
| 67 image_name = re.sub(r'\.\./', '', image_name) | 65 image_name = re.sub(r'\.\./', '', image_name) |
| 68 image_name = re.sub(r'(\.|/|-)', '_', image_name) | 66 image_name = re.sub(r'(\.|/|-)', '_', image_name) |
| 69 return image_name | 67 return image_name |
| 70 | 68 |
| 71 def _WriteImage(self, image_path, png_image): | 69 def _WriteImage(self, image_path, png_image): |
| 72 output_dir = os.path.dirname(image_path) | 70 output_dir = os.path.dirname(image_path) |
| 73 if not os.path.exists(output_dir): | 71 if not os.path.exists(output_dir): |
| 74 os.makedirs(output_dir) | 72 os.makedirs(output_dir) |
| 75 png_image.WritePngFile(image_path) | 73 cv2.imwrite(image_path, png_image.image) |
| 76 | 74 |
| 77 def _WriteErrorImages(self, img_dir, img_name, screenshot, ref_png): | 75 def _WriteErrorImages(self, img_dir, img_name, screenshot, ref_png): |
| 78 full_image_name = img_name + '_' + str(self.options.build_revision) | 76 full_image_name = img_name + '_' + str(self.options.build_revision) |
| 79 full_image_name = full_image_name + '.png' | 77 full_image_name = full_image_name + '.png' |
| 80 | 78 |
| 81 # Always write the failing image. | 79 # Always write the failing image. |
| 82 self._WriteImage( | 80 self._WriteImage( |
| 83 os.path.join(img_dir, 'FAIL_' + full_image_name), screenshot) | 81 os.path.join(img_dir, 'FAIL_' + full_image_name), screenshot) |
| 84 | 82 |
| 85 if ref_png: | 83 if ref_png: |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 """Downloads the reference image for the given test from cloud | 162 """Downloads the reference image for the given test from cloud |
| 165 storage, returning it as a Telemetry Bitmap object.""" | 163 storage, returning it as a Telemetry Bitmap object.""" |
| 166 # TODO(kbr): there's a race condition between the deletion of the | 164 # TODO(kbr): there's a race condition between the deletion of the |
| 167 # temporary file and gsutil's overwriting it. | 165 # temporary file and gsutil's overwriting it. |
| 168 if not self.options.refimg_cloud_storage_bucket: | 166 if not self.options.refimg_cloud_storage_bucket: |
| 169 raise Exception('--refimg-cloud-storage-bucket argument is required') | 167 raise Exception('--refimg-cloud-storage-bucket argument is required') |
| 170 temp_file = tempfile.NamedTemporaryFile().name | 168 temp_file = tempfile.NamedTemporaryFile().name |
| 171 cloud_storage.Get(self.options.refimg_cloud_storage_bucket, | 169 cloud_storage.Get(self.options.refimg_cloud_storage_bucket, |
| 172 self._FormatReferenceImageName(img_name, page, tab), | 170 self._FormatReferenceImageName(img_name, page, tab), |
| 173 temp_file) | 171 temp_file) |
| 174 return bitmap.Bitmap.FromPngFile(temp_file) | 172 return bitmap.Bitmap.FromImageFile(temp_file) |
| 175 | 173 |
| 176 def _UploadErrorImagesToCloudStorage(self, image_name, screenshot, ref_img): | 174 def _UploadErrorImagesToCloudStorage(self, image_name, screenshot, ref_img): |
| 177 """For a failing run, uploads the failing image, reference image (if | 175 """For a failing run, uploads the failing image, reference image (if |
| 178 supplied), and diff image (if reference image was supplied) to cloud | 176 supplied), and diff image (if reference image was supplied) to cloud |
| 179 storage. This subsumes the functionality of the | 177 storage. This subsumes the functionality of the |
| 180 archive_gpu_pixel_test_results.py script.""" | 178 archive_gpu_pixel_test_results.py script.""" |
| 181 machine_name = re.sub('\W+', '_', self.options.test_machine_name) | 179 machine_name = re.sub('\W+', '_', self.options.test_machine_name) |
| 182 upload_dir = '%s_%s_telemetry' % (self.options.build_revision, machine_name) | 180 upload_dir = '%s_%s_telemetry' % (self.options.build_revision, machine_name) |
| 183 base_bucket = '%s/runs/%s' % (error_image_cloud_storage_bucket, upload_dir) | 181 base_bucket = '%s/runs/%s' % (error_image_cloud_storage_bucket, upload_dir) |
| 184 image_name_with_revision = '%s_%s.png' % ( | 182 image_name_with_revision = '%s_%s.png' % ( |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 default='') | 242 default='') |
| 245 group.add_option('--test-machine-name', | 243 group.add_option('--test-machine-name', |
| 246 help='Name of the test machine. Specifying this argument causes this ' | 244 help='Name of the test machine. Specifying this argument causes this ' |
| 247 'script to upload failure images and diffs to cloud storage directly, ' | 245 'script to upload failure images and diffs to cloud storage directly, ' |
| 248 'instead of relying on the archive_gpu_pixel_test_results.py script.', | 246 'instead of relying on the archive_gpu_pixel_test_results.py script.', |
| 249 default='') | 247 default='') |
| 250 group.add_option('--generated-dir', | 248 group.add_option('--generated-dir', |
| 251 help='Overrides the default on-disk location for generated test images ' | 249 help='Overrides the default on-disk location for generated test images ' |
| 252 '(only used for local testing without a cloud storage account)', | 250 '(only used for local testing without a cloud storage account)', |
| 253 default=default_generated_data_dir) | 251 default=default_generated_data_dir) |
| OLD | NEW |