• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# -*- coding: utf-8 -*-
2# Copyright 2019 The Chromium OS 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"""Tools for recording and reporting timeline of benchmark_run."""
7
8from __future__ import print_function
9
10__author__ = 'yunlian@google.com (Yunlian Jiang)'
11
12import time
13
14
15class Event(object):
16  """One event on the timeline."""
17
18  def __init__(self, name='', cur_time=0):
19    self.name = name
20    self.timestamp = cur_time
21
22
23class Timeline(object):
24  """Use a dict to store the timeline."""
25
26  def __init__(self):
27    self.events = []
28
29  def Record(self, event):
30    for e in self.events:
31      assert e.name != event, (
32          'The event {0} is already recorded.'.format(event))
33    cur_event = Event(name=event, cur_time=time.time())
34    self.events.append(cur_event)
35
36  def GetEvents(self):
37    return ([e.name for e in self.events])
38
39  def GetEventDict(self):
40    tl = {}
41    for e in self.events:
42      tl[e.name] = e.timestamp
43    return tl
44
45  def GetEventTime(self, event):
46    for e in self.events:
47      if e.name == event:
48        return e.timestamp
49    raise IndexError('The event {0} is not recorded'.format(event))
50
51  def GetLastEventTime(self):
52    return self.events[-1].timestamp
53
54  def GetLastEvent(self):
55    return self.events[-1].name
56