• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# -*- coding: utf-8 -*-
2# Copyright 2019 The ChromiumOS Authors
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
8
9__author__ = "yunlian@google.com (Yunlian Jiang)"
10
11import time
12
13
14class Event(object):
15    """One event on the timeline."""
16
17    def __init__(self, name="", cur_time=0):
18        self.name = name
19        self.timestamp = cur_time
20
21
22class Timeline(object):
23    """Use a dict to store the timeline."""
24
25    def __init__(self):
26        self.events = []
27
28    def Record(self, event):
29        for e in self.events:
30            assert e.name != event, "The event {0} is already recorded.".format(
31                event
32            )
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