1# Lint as: python2, python3 2# Copyright 2017 The Chromium Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6""" 7A test which monitors the camera HAL3 performance metrics: 8 1. Time to open device 9 2. Time to start preview 10 3. Time to capture still image 11 4. Shot to shot time 12""" 13 14from __future__ import absolute_import 15from __future__ import division 16from __future__ import print_function 17import os, logging 18from autotest_lib.client.bin import test, utils 19from autotest_lib.client.common_lib import error 20from autotest_lib.client.cros import service_stopper 21from autotest_lib.client.cros.camera import camera_utils 22from six.moves import map 23 24 25class camera_HAL3Perf(test.test): 26 """ 27 This test monitors several performance metrics reported by the test binary 28 cros_camera_test. 29 """ 30 31 version = 1 32 test_binary = 'cros_camera_test' 33 dep = 'camera_hal3' 34 timeout = 60 35 test_name = 'camera_HAL3Perf' 36 test_log_suffix = 'test.log' 37 cros_camera_service = 'cros-camera' 38 39 def _logperf(self, name, key, value, units, higher_is_better=False): 40 description = '%s.%s' % (name, key) 41 self.output_perf_value( 42 description=description, 43 value=value, 44 units=units, 45 higher_is_better=higher_is_better) 46 47 def _analyze_log(self, log_file): 48 """ 49 Analyzes the performance metrics extracted from the log file. 50 51 @param log_file: path of the log file. Sample lines of the log file are 52 as below. 53 Camera: front 54 device_open: 5020 us 55 preview_start: 353285 us 56 still_image_capture: 308166 us 57 """ 58 try: 59 with open(log_file, 'r') as f: 60 camera_id = None 61 for line in f: 62 key, value = line.split(':') 63 if key == 'Camera': 64 camera_id = value.strip() 65 elif camera_id is not None: 66 self._logperf(self.test_name, 67 'camera_%s_%s' % (camera_id, key), 68 value.strip().split(' ')[0], 69 value.strip().split(' ')[1]) 70 else: 71 msg = 'Error in parsing the log file (%s)' % log_file 72 raise error.TestFail(msg) 73 except IOError as err: 74 msg = 'Error in reading the log file (%s): %s' % (log_file, err) 75 raise error.TestFail(msg) 76 77 def setup(self): 78 """ 79 Run common setup steps. 80 """ 81 self.dep_dir = os.path.join(self.autodir, 'deps', self.dep) 82 self.job.setup_dep([self.dep]) 83 logging.debug('mydep is at %s', self.dep_dir) 84 85 def run_once(self): 86 """ 87 Entry point of this test. 88 """ 89 self.job.install_pkg(self.dep, 'dep', self.dep_dir) 90 binary_path = os.path.join(self.dep_dir, 'bin', self.test_binary) 91 test_log_file = os.path.join(self.resultsdir, '%s_%s' % 92 (self.test_name, self.test_log_suffix)) 93 94 with service_stopper.ServiceStopper([self.cros_camera_service]): 95 for hal in camera_utils.get_camera_hal_paths_for_test(): 96 cmd = [ 97 binary_path, 98 '--camera_hal_path=%s' % hal, 99 ('--gtest_filter=Camera3StillCaptureTest/' 100 'Camera3SimpleStillCaptureTest.PerformanceTest/*'), 101 '--output_log=%s' % test_log_file 102 ] 103 cmd = ' '.join(map(utils.sh_quote_word, cmd)) 104 105 ret = utils.system( 106 cmd, timeout=self.timeout, ignore_status=True) 107 self._analyze_log(test_log_file) 108 if ret != 0: 109 raise error.TestFail('Failed to execute command: %s' % cmd) 110