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