User Timing Level 2

Abstract

This specification defines an interface to help web developers measure the performance of their applications by giving them access to high precision timestamps.

Status of This Document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at https://www.w3.org/TR/.

User Timing Level 2 replaces the first version of [USER-TIMING] and includes:

This document was published by the Web Performance Working Group as a First Public Working Draft. This document is intended to become a W3C Recommendation. If you wish to make comments regarding this document, the GitHub repository is preferred for discussion of this specification. There is also a public mailing list public-web-perf@w3.org (archives). When sending e-mail, please use [UserTiming] at the start of your email's subject. All comments are welcome.

Publication as a First Public Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

This document is governed by the 1 September 2015 W3C Process Document.

1. Introduction

This section is non-normative.

Web developers need the ability to assess and understand the performance characteristics of their applications. While JavaScript provides a mechanism to measure application latency (retrieving the current timestamp from the Date.now() method), the precision of this timestamp varies between user agents.

This document defines the PerformanceMark and PerformanceMeasure interfaces, and extensions to the Performance interface, which expose a high precision, monotonically increasing timestamp so they can better measure the performance characteristics of their applications.

The following script shows how a developer can use the interfaces defined in this document to obtain timing data related to developer scripts.

Example 1
<!doctype html>
<html>
  <head>
    <title>User Timing example</title>
  </head>
  <body onload="init()">
    <script>
       function init()
       {
            performance.mark("startTask1");
            doTask1(); // Some developer code
            performance.mark("endTask1");

            performance.mark("startTask2");
            doTask2(); // Some developer code
            performance.mark("endTask2");

            measurePerf();
       }

       function measurePerf()
       {
           var perfEntries = performance.getEntriesByType("mark");
           for (var i = 0; i < perfEntries.length; i++)
           {
                 if (window.console) console.log("Name: "        + perfEntries[i].name      +
                                                 " Entry Type: " + perfEntries[i].entryType +
                                                 " Start Time: " + perfEntries[i].startTime +
                                                 " Duration: "   + perfEntries[i].duration  + "\n");
           }
       }
    </script>
  </body>
</html>
Note

[PERFORMANCE-TIMELINE-2] defines two mechanisms that can be used to retrieve recorded metrics: getEntries() and getEntriesByType() methods, and the PerformanceObserver interface. The former is best suited for cases where you want to retrieve a particular metric by name at a single point in time, and the latter is optimized for cases where you may want to receive notifications of new metrics as they become available.

2. Conformance

As well as sections marked as non-normative, all authoring guidelines, diagrams, examples, and notes in this specification are non-normative. Everything else in this specification is normative.

The key words MAY and MUST are to be interpreted as described in [RFC2119].

Some conformance requirements are phrased as requirements on attributes, methods or objects. Such requirements are to be interpreted as requirements on user agents.

The IDL fragments in this specification MUST be interpreted as required for conforming IDL fragments, as described in the Web IDL specification. [WebIDL]

3. Extensions to the Performance Interface

partial interface Performance {
    void mark(DOMString markName);
    void clearMarks(optional DOMString markName);

    void measure(DOMString measureName,
                 optional DOMString startMark,
                 optional DOMString endMark);
    void clearMeasures(optional DOMString measureName);
};

The mark(markName) method stores a timestamp with the associated name (a "mark"). It MUST run these steps:

  1. If the global object [HTML51] is a Window object and markName uses the same name as a read only attribute in the PerformanceTiming interface [NAVIGATION-TIMING-2], throw a SyntaxError.
  2. Create a new PerformanceMark object.
  3. Set name to markName.
  4. Set entryType to DOMString "mark".
  5. Set startTime to be the value that would be returned by the Performance object's now() method.
  6. Set duration to 0.
  7. Queue the PerformanceMark object.
  8. Add the PerformanceMark object to the performance entry buffer.
  9. Return undefined.

The clearMarks(markName) removes stored timestamp with the associated name. It MUST run these steps:

  1. If markName is omitted, remove all PerformanceMark objects from the performance entry buffer.
  2. Otherwise, remove all PerformanceMark objects listed in the performance entry buffer whose name matchesmarkName.
  3. Return undefined.

The measure(measureName, startMark, endMark) method stores the DOMHighResTimeStamp duration between two marks along with the associated name (a "measure"). It MUST run these steps:

  1. Let end time be 0.
  2. If endMark is omitted, let end time be the value that would be returned by the Performance object's now() method. Otherwise let end time be the value of the startTime attribute from the most recent occurence PerformanceMark object in the performance entry buffer whose name matches value endMark.
  3. If startMark is omitted, let start time be 0. Otherwise let start time be the value of the startTime attribute from the most recent occurence PerformanceMark object in the performance entry buffer whose name matches value startMark.
  4. Create a new PerformanceMeasure object.
  5. Set name to measureName.
  6. Set entryType to DOMString "measure".
  7. Set startTime to start time.
  8. Set duration to the duration from start time to end time. The resulting duration value MAY be negative.
  9. Queue the PerformanceMeasure object.
  10. Add the PerformanceMeasure object to the performance entry buffer.
  11. Return undefined.

The clearMeasures(measureName) removes stored timestamp with the associated name. It MUST run these steps:

  1. If measureName is omitted, remove all PerformanceMeasure objects in the performance entry buffer.
  2. Otherwise remove all PerformanceMeasure objects listed in the performance entry buffer whose name matches measureName.
  3. Return undefined.

4. The PerformanceMark Interface

The PerformanceMark interface also exposes marks created via the Performance.mark method to the Performance Timeline [PERFORMANCE-TIMELINE-2].

[Exposed=(Window,Worker)]
interface PerformanceMark : PerformanceEntry {
};

The PerformanceMark interface extends the following attributes of the PerformanceEntry interface:

The name attribute will return the mark's name.

The entryType attribute will return the DOMString "mark".

The startTime attribute will return a DOMHighResTimeStamp with the mark's time value [HR-TIME-2].

The duration attribute will return a DOMHighResTimeStamp of value 0.

5. The PerformanceMeasure Interface

The PerformanceMeasure interface also exposes measures created via the Performance.measure method to the Performance Timeline [PERFORMANCE-TIMELINE-2].

[Exposed=(Window,Worker)]
interface PerformanceMeasure : PerformanceEntry {
};

The PerformanceMeasure interface extends the following attributes of the PerformanceEntry interface:

The name attribute will return the measure's name.

The entryType attribute will return the DOMString "measure".

The startTime attribute will return a DOMHighResTimeStamp with the measure's start mark [HR-TIME-2].

The duration attribute will return a DOMHighResTimeStamp with the duration of the measure.

6. Privacy and Security

This section is non-normative.

The interfaces defined in this specification expose potentially sensitive timing information on specific JavaScript activity of a page. Please refer to [HR-TIME-2] for privacy and security considerations of exposing high-resolution timing information.

Because the web platform has been designed with the invariant that any script included on a page has the same access as any other script included on the same page, regardless of the origin of either scripts, the interfaces defined by this specification do not place any restrictions on recording or retrieval of recorded timing information - i.e. a user timing mark or measure recorded by any script included on the page can be read by any other script running on the same page, regardless of origin.

A. Acknowledgments

Thanks to James Simonsen, Jason Weber, Nic Jansma, Philippe Le Hegaret, Karen Anderson, Steve Souders, Sigbjorn Vik, Todd Reifsteck, and Tony Gentilcore for their contributions to this work.

B. References

B.1 Normative references

[HR-TIME-2]
Ilya Grigorik; James Simonsen; Jatinder Mann. W3C. High Resolution Time Level 2. 25 October 2016. W3C Working Draft. URL: https://www.w3.org/TR/hr-time-2/
[HTML51]
Steve Faulkner; Arron Eicholz; Travis Leithead; Alex Danilo. W3C. HTML 5.1. 15 September 2016. W3C Proposed Recommendation. URL: https://www.w3.org/TR/html51/
[NAVIGATION-TIMING-2]
Tobin Titus; Jatinder Mann; Arvind Jain. W3C. Navigation Timing Level 2. 22 April 2016. W3C Working Draft. URL: https://www.w3.org/TR/navigation-timing-2/
[PERFORMANCE-TIMELINE-2]
Ilya Grigorik. W3C. Performance Timeline Level 2. 27 October 2016. W3C Working Draft. URL: https://www.w3.org/TR/performance-timeline-2/
[RFC2119]
S. Bradner. IETF. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://tools.ietf.org/html/rfc2119
[WORKERS]
Ian Hickson. W3C. Web Workers. 24 September 2015. W3C Working Draft. URL: https://www.w3.org/TR/workers/
[WebIDL]
Cameron McCormack; Boris Zbarsky; Tobie Langel. W3C. Web IDL. 15 September 2016. W3C Working Draft. URL: https://www.w3.org/TR/WebIDL-1/

B.2 Informative references

[USER-TIMING]
Jatinder Mann; Zhiheng Wang; Anderson Quach. W3C. User Timing. 12 December 2013. W3C Recommendation. URL: https://www.w3.org/TR/user-timing/