예제 #1
0
def get_data_from_csv(csv_reader):
  """Creates a list of StatEntry objects based on data in CSV data.

  Input CSV data must be in the format:
    Description,timestamp,num_batches,time mean value,time sd

  Args:
    csv_reader: csv.reader instance.

  Returns:
    A tuple of datetime timestamp and list of benchmark_util.StatEntry objects.

  Raises:
    ValueError: if CSV is invalid.
  """
  timestamp = None
  stat_entries = []

  for row in csv_reader:
    if len(row) != 5:
      raise ValueError('Expected 5 entries per line in the input CSV file, '
                       'but found %d entries.' % len(row))
    if '' in row:
      raise ValueError('Found empty entries in row: %s' % row)

    # Set timestamp based on the first line in CSV file.
    if timestamp is None:
      # Example of time formatting: 2017-06-26 02:59:29.325579
      timestamp = datetime.strptime(row[1], "%Y-%m-%d %H:%M:%S.%f")
    stat_entries.append(
        benchmark_util.StatEntry(row[0], float(row[3]), 1))
  return timestamp, stat_entries
    def testStoreDataWithEntries(self):
        with tempfile.TemporaryFile() as temp_file:
            timing_entries = [benchmark_util.StatEntry('test', 0.1, 1)]
            benchmark_util.store_data_in_json(timing_entries,
                                              datetime.date(2017, 1, 1),
                                              temp_file.name)
            json_output = json.loads(open(temp_file.name, 'r').read())

            self.assertEquals(1, len(json_output['entries']['entry']))
            self.assertEquals('test',
                              json_output['entries']['entry'][0]['name'])
            self.assertEquals(0.1,
                              json_output['entries']['entry'][0]['wallTime'])
            self.assertEquals(u'1',
                              json_output['entries']['entry'][0]['iters'])
            self.assertEquals(u'1483228800', json_output['startTime'])
            self.assertEquals('TestBenchmark', json_output['name'])