Home | History | Annotate | Download | only in brillo_StorageWriteSpeedTest
      1 # Copyright 2015 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import logging
      6 import os
      7 import re
      8 
      9 import common
     10 from autotest_lib.client.common_lib import error
     11 from autotest_lib.server import test
     12 
     13 
     14 _DEFAULT_BLOCK_SIZE = 4096
     15 _DEFAULT_NUM_BLOCKS = 20000
     16 _DEFAULT_MIN_SPEED = 30 * 1024 * 1024
     17 
     18 
     19 class brillo_StorageWriteSpeedTest(test.test):
     20     """Verify that writing to a Brillo device data storage is fast enough."""
     21     version = 1
     22 
     23     def run_once(self, host=None, block_size=_DEFAULT_BLOCK_SIZE,
     24                  num_blocks=_DEFAULT_NUM_BLOCKS, min_speed=_DEFAULT_MIN_SPEED):
     25         """Runs the test.
     26 
     27         @param host: A host object representing the DUT.
     28         @param block_size: The size of blocks to write in bytes.
     29         @param num_blocks: The number of blocks to write.
     30         @param min_speed: Minimum required write speed in bytes/sec.
     31 
     32         @raise TestError: Something went wrong while trying to execute the test.
     33         @raise TestFail: The test failed.
     34         """
     35         try:
     36             tmp_file = os.path.join(host.get_tmp_dir(), 'testfile')
     37             result = host.run_output(
     38                     'dd if=/dev/zero of=%s bs=%s count=%s 2>&1' %
     39                     (tmp_file, block_size, num_blocks))
     40             actual_speed = None
     41             for line in result.splitlines():
     42                 match = re.match('.*\(([0-9]+) bytes/sec\)$', line)
     43                 if match:
     44                     actual_speed = int(match.group(1))
     45                     break
     46             if actual_speed is None:
     47                 raise error.TestError('Error finding storage write speed')
     48             logging.info('Actual write speed is %d bytes/sec', actual_speed)
     49             if actual_speed < int(min_speed):
     50                 logging.error('Write speed (%s bytes/sec) is lower than '
     51                               'required (%s bytes/sec)',
     52                               actual_speed, min_speed)
     53                 raise error.TestFail(
     54                         'Storage write speed is lower than required')
     55         except error.GenericHostRunError:
     56             raise error.TestFail('Error writing to device data partition')
     57