1# Copyright 2013 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
5from metrics import Metric
6from telemetry.value import scalar
7
8class LoadingMetric(Metric):
9  """A metric for page loading time based entirely on window.performance"""
10
11  def Start(self, page, tab):
12    raise NotImplementedError()
13
14  def Stop(self, page, tab):
15    raise NotImplementedError()
16
17  def AddResults(self, tab, results):
18    load_timings = tab.EvaluateJavaScript('window.performance.timing')
19
20    # NavigationStart relative markers in milliseconds.
21    load_start = (
22      float(load_timings['loadEventStart']) - load_timings['navigationStart'])
23    results.AddValue(scalar.ScalarValue(
24        results.current_page, 'load_start', 'ms', load_start))
25
26    dom_content_loaded_start = (
27      float(load_timings['domContentLoadedEventStart']) -
28      load_timings['navigationStart'])
29    results.AddValue(scalar.ScalarValue(
30        results.current_page, 'dom_content_loaded_start', 'ms',
31        dom_content_loaded_start))
32
33    fetch_start = (
34        float(load_timings['fetchStart']) - load_timings['navigationStart'])
35    results.AddValue(scalar.ScalarValue(
36        results.current_page, 'fetch_start', 'ms', fetch_start,
37        important=False))
38
39    request_start = (
40        float(load_timings['requestStart']) - load_timings['navigationStart'])
41    results.AddValue(scalar.ScalarValue(
42        results.current_page, 'request_start', 'ms', request_start,
43        important=False))
44
45    # Phase measurements in milliseconds.
46    domain_lookup_duration = (
47        float(load_timings['domainLookupEnd']) -
48        load_timings['domainLookupStart'])
49    results.AddValue(scalar.ScalarValue(
50        results.current_page, 'domain_lookup_duration', 'ms',
51        domain_lookup_duration, important=False))
52
53    connect_duration = (
54        float(load_timings['connectEnd']) - load_timings['connectStart'])
55    results.AddValue(scalar.ScalarValue(
56        results.current_page, 'connect_duration', 'ms', connect_duration,
57        important=False))
58
59    request_duration = (
60        float(load_timings['responseStart']) - load_timings['requestStart'])
61    results.AddValue(scalar.ScalarValue(
62        results.current_page, 'request_duration', 'ms', request_duration,
63        important=False))
64
65    response_duration = (
66        float(load_timings['responseEnd']) - load_timings['responseStart'])
67    results.AddValue(scalar.ScalarValue(
68        results.current_page, 'response_duration', 'ms', response_duration,
69        important=False))
70