aboutsummaryrefslogtreecommitdiff
path: root/cros_utils/timeline.py
blob: cce0b05c20f189e568fa23cd7b1c3c09852dd1f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
# Copyright 2019 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tools for recording and reporting timeline of benchmark_run."""

from __future__ import print_function

__author__ = 'yunlian@google.com (Yunlian Jiang)'

import time


class Event(object):
  """One event on the timeline."""

  def __init__(self, name='', cur_time=0):
    self.name = name
    self.timestamp = cur_time


class Timeline(object):
  """Use a dict to store the timeline."""

  def __init__(self):
    self.events = []

  def Record(self, event):
    for e in self.events:
      assert e.name != event, (
          'The event {0} is already recorded.'.format(event))
    cur_event = Event(name=event, cur_time=time.time())
    self.events.append(cur_event)

  def GetEvents(self):
    return ([e.name for e in self.events])

  def GetEventDict(self):
    tl = {}
    for e in self.events:
      tl[e.name] = e.timestamp
    return tl

  def GetEventTime(self, event):
    for e in self.events:
      if e.name == event:
        return e.timestamp
    raise IndexError('The event {0} is not recorded'.format(event))

  def GetLastEventTime(self):
    return self.events[-1].timestamp

  def GetLastEvent(self):
    return self.events[-1].name