Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / tools / perf / metrics / timeline_interaction_record.py
1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import re
6
7
8 def IsTimelineInteractionRecord(event_name):
9   return event_name.startswith('Interaction.')
10
11
12 class TimelineInteractionRecord(object):
13   """Represents an interaction that took place during a timeline recording.
14
15   As a page runs, typically a number of different (simulated) user interactions
16   take place. For instance, a user might click a button in a mail app causing a
17   popup to animate in. Then they might press another button that sends data to a
18   server and simultaneously closes the popup without an animation. These are two
19   interactions.
20
21   From the point of view of the page, each interaction might have a different
22   logical name: ClickComposeButton and SendEmail, for instance. From the point
23   of view of the benchmarking harness, the names aren't so interesting as what
24   the performance expectations are for that interaction: was it loading
25   resources from the network? was there an animation?
26
27   Determining these things is hard to do, simply by observing the state given to
28   a page from javascript. There are hints, for instance if network requests are
29   sent, or if a CSS animation is pending. But this is by no means a complete
30   story.
31
32   Instead, we expect pages to mark up the timeline what they are doing, with
33   logical names, and flags indicating the semantics of that interaction. This
34   is currently done by pushing markers into the console.time/timeEnd API: this
35   for instance can be issued in JS:
36
37      var str = 'Interaction.SendEmail/is_smooth,is_loading_resources';
38      console.time(str);
39      setTimeout(function() {
40        console.timeEnd(str);
41      }, 1000);
42
43   When run with perf.measurements.timeline_based_measurement running, this will
44   then cause a TimelineInteractionRecord to be created for this range and both
45   smoothness and network metrics to be reported for the marked up 1000ms
46   time-range.
47
48   """
49   def __init__(self, logical_name, start, end):
50     assert logical_name
51     self.logical_name = logical_name
52     self.start = start
53     self.end = end
54     self.is_smooth = False
55     self.is_loading_resources = False
56
57   @staticmethod
58   def FromEvent(event):
59     m = re.match('Interaction\.(.+)\/(.+)', event.name)
60     if m:
61       logical_name = m.group(1)
62       if m.group(1) != '':
63         flags = m.group(2).split(',')
64       else:
65         flags = []
66     else:
67       m = re.match('Interaction\.(.+)', event.name)
68       assert m
69       logical_name = m.group(1)
70       flags = []
71
72     record = TimelineInteractionRecord(logical_name, event.start, event.end)
73     for f in flags:
74       if not f in ('is_smooth', 'is_loading_resources'):
75         raise Exception(
76           'Unrecognized flag in timeline Interaction record: %s' % f)
77     record.is_smooth = 'is_smooth' in flags
78     record.is_loading_resources = 'is_loading_resources' in flags
79     return record
80
81   def GetResultNameFor(self, result_name):
82     return "%s-%s" % (self.logical_name, result_name)