1# Copyright (c) 2014 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 5import datetime 6import math 7import random 8 9from autotest_lib.client.cros.video import method_logger 10 11 12@method_logger.log 13def generate_interval_sequence(start, stop, interval_in_s): 14 """ 15 Generates a list of timestamps sequence given the configuration. 16 17 If specified upper limit would coincide as the last value it will be 18 included in the list. 19 20 We use this method when we are capturing golden images for a particular 21 video. We would typically specify an interval of a second and then store 22 obtained those golden images in a server. 23 24 @param start: timedelta, start time. 25 @param stop: timedelta, stop_time. 26 @param interval_in_s: Time gap between two successive timestamps. 27 28 @returns a list of timedelta values specifying points in time to take a 29 screenshot. 30 31 """ 32 33 start_total_ms = int(start.total_seconds() * 1000) 34 stop_total_ms = int(stop.total_seconds() * 1000) 35 interval_total_ms = interval_in_s * 1000 36 duration_total_ms = stop_total_ms - start_total_ms 37 38 if interval_total_ms > duration_total_ms: 39 raise ValueError('Interval too large. Duration = %ms, interval = %ms', 40 duration_total_ms, interval_total_ms) 41 42 # xrange is exclusive of upper limit, add 1 second so that we include 43 # the stop time 44 return [datetime.timedelta(milliseconds=x) for x in 45 xrange(start_total_ms, 46 stop_total_ms + 1, 47 interval_total_ms)] 48 49 50@method_logger.log 51def generate_random_sequence(start, stop, samples_per_min): 52 """ 53 Generates a list of random values per given configuration. 54 55 @param start: timedelta, start time. 56 @param stop: timedelta, stop time. 57 @param samples_per_min: int, number of values to get per minute. 58 59 This method exists because we need to capture images at random time 60 instances. We need to do that so as to maximize the chance of catching a 61 glitch that we wouldn't otherwise catch if we captured in a wrong interval. 62 63 @returns a list of random values between start and stop as per 64 'samples_per_min' configuration. 65 66 """ 67 start_total_s = int(start.total_seconds()) 68 69 stop_total_s = int(stop.total_seconds()) 70 71 duration = stop_total_s - start_total_s 72 73 # Round up the total number of samples. e.g: 8.5 becomes 9 74 75 total_num_samples = int( 76 math.ceil((samples_per_min * duration) / 60.0)) 77 78 # xrange is exclusive of upper limit, add 1 second so that we include 79 # the stop time 80 randomized_time_seq = random.sample(xrange(start_total_s, stop_total_s + 1), 81 total_num_samples) 82 83 return [datetime.timedelta(seconds=t) for t in randomized_time_seq] 84