Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / web_perf / metrics / fast_metric.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 logging
6
7 from telemetry.timeline import bounds
8 from telemetry.value import scalar
9 from telemetry.web_perf import timeline_interaction_record as tir_module
10 from telemetry.web_perf.metrics import timeline_based_metric
11
12
13 class FastMetric(timeline_based_metric.TimelineBasedMetric):
14   def __init__(self):
15     super(FastMetric, self).__init__()
16
17   def AddResults(self, model, renderer_thread, interaction_records, results):
18     """Add three results: duration, cpu_time, and idle_time.
19
20     duration is the total wall time for |interaction_records|.
21     cpu_time is the renderer thread time that intersects |interaction_records|.
22     idle time is wall time for |interaction_records| for which renderer slices
23         do not overlap. Note that unscheduled renderer thread time is not
24         counted. Idle time is time for which there was nothing to do.
25
26     Args:
27       model: a TimelineModule instance
28       renderer_thread: a telemetry.timeline.thread.Thread() instance
29       interaction_records: an iterable of TimelineInteractionRecord instances
30       results: an instance of page.PageTestResults
31     """
32     self.VerifyNonOverlappedRecords(interaction_records)
33
34     duration = sum(r.end - r.start for r in interaction_records)
35     results.AddValue(scalar.ScalarValue(
36         results.current_page, 'fast-duration', 'ms', duration))
37
38     try:
39       cpu_time = sum(
40           r.GetOverlappedThreadTimeForSlice(s)
41           for r in interaction_records
42           for s in renderer_thread.toplevel_slices)
43     except tir_module.NoThreadTimeDataException:
44       logging.warning(
45           'Main thread cpu_time cannot be computed for records %s since '
46           'trace does not contain thread time data.',
47           repr(interaction_records))
48     else:
49       results.AddValue(scalar.ScalarValue(
50           results.current_page, 'fast-cpu_time', 'ms', cpu_time))
51
52     idle_time = duration - sum(
53         bounds.Bounds.GetOverlap(r.start, r.end, s.start, s.end)
54         for r in interaction_records
55         for s in renderer_thread.toplevel_slices)
56     results.AddValue(scalar.ScalarValue(
57         results.current_page, 'fast-idle_time', 'ms', idle_time))