1 2<!DOCTYPE html> 3<html> 4 <head> 5 <meta charset="UTF-8" /> 6 <title>window.performance User Timing clearMeasures() method is working properly with navigation timing 7 attributes</title> 8 <link rel="author" title="Microsoft" href="http://www.microsoft.com/" /> 9 <link rel="help" href="https://w3c.github.io/user-timing/#dom-performance-measure"/> 10 <script src="/resources/testharness.js"></script> 11 <script src="/resources/testharnessreport.js"></script> 12 <script src="/common/performance-timeline-utils.js"></script> 13 <script src="resources/webperftestharness.js"></script> 14 15 <script> 16 // test data 17 var startMarkName = "mark_start"; 18 var startMarkValue; 19 var endMarkName = "mark_end"; 20 var endMarkValue; 21 var measures; 22 var testThreshold = 20; 23 24 // test measures 25 measureTestDelay = 200; 26 var TEST_MEASURES = 27 [ 28 { 29 name: "measure_nav_start_no_end", 30 startMark: "navigationStart", 31 endMark: undefined, 32 exceptionTestMessage: "window.performance.measure(\"measure_nav_start_no_end\", " + 33 "\"navigationStart\") ran without throwing any exceptions.", 34 expectedStartTime: undefined, 35 expectedDuration: undefined, 36 entryMatch: undefined 37 }, 38 { 39 name: "measure_nav_start_mark_end", 40 startMark: "navigationStart", 41 endMark: "mark_end", 42 exceptionTestMessage: "window.performance.measure(\"measure_nav_start_end\", \"navigationStart\", " + 43 "\"mark_end\") ran without throwing any exceptions.", 44 expectedStartTime: undefined, 45 expectedDuration: undefined, 46 entryMatch: undefined 47 }, 48 { 49 name: "measure_mark_start_nav_end", 50 startMark: "mark_start", 51 endMark: "responseEnd", 52 exceptionTestMessage: "window.performance.measure(\"measure_start_nav_end\", \"mark_start\", " + 53 "\"responseEnd\") ran without throwing any exceptions.", 54 expectedStartTime: undefined, 55 expectedDuration: undefined, 56 entryMatch: undefined 57 }, 58 { 59 name: "measure_nav_start_nav_end", 60 startMark: "navigationStart", 61 endMark: "responseEnd", 62 exceptionTestMessage: "window.performance.measure(\"measure_nav_start_nav_end\", " + 63 "\"navigationStart\", \"responseEnd\") ran without throwing any exceptions.", 64 expectedStartTime: undefined, 65 expectedDuration: undefined, 66 entryMatch: undefined 67 } 68 ]; 69 70 setup({explicit_done: true}); 71 72 test_namespace(); 73 74 function onload_test() 75 { 76 // test for existence of User Timing and Performance Timeline interface 77 if (!has_required_interfaces()) 78 { 79 test_true(false, 80 "The User Timing and Performance Timeline interfaces, which are required for this test, " + 81 "are defined."); 82 83 done(); 84 } 85 else 86 { 87 // create the start mark for the test measures 88 window.performance.mark(startMarkName); 89 90 // get the start mark's value 91 startMarkValue = window.performance.getEntriesByName(startMarkName)[0].startTime; 92 93 // create the test end mark using the test delay; this will allow for a significant difference between 94 // the mark values that should be represented in the duration of measures using these marks 95 step_timeout(measure_test_cb, measureTestDelay); 96 } 97 } 98 99 function measure_test_cb() 100 { 101 // create the end mark for the test measures 102 window.performance.mark(endMarkName); 103 104 // get the end mark's value 105 endMarkValue = window.performance.getEntriesByName(endMarkName)[0].startTime; 106 107 // loop through measure scenarios 108 for (var i in TEST_MEASURES) 109 { 110 var scenario = TEST_MEASURES[i]; 111 112 if (scenario.startMark != undefined && scenario.endMark == undefined) 113 { 114 // only startMark is defined, provide startMark and don't provide endMark 115 window.performance.measure(scenario.name, scenario.startMark); 116 117 // when startMark is provided to the measure() call, the value of the mark or navigation 118 // timing attribute whose name is provided is used for the startMark 119 scenario.expectedStartTime = (timingAttributes.indexOf(scenario.startMark) != -1 ? 120 window.performance.timing[scenario.startMark] - 121 window.performance.timing.navigationStart : 122 startMarkValue); 123 124 // when endMark isn't provided to the measure() call, a DOMHighResTimeStamp corresponding to 125 // the current time with a timebase of the navigationStart attribute is used 126 scenario.expectedDuration = ((new Date()) - window.performance.timing.navigationStart) - 127 scenario.expectedStartTime; 128 } 129 else if (scenario.startMark != undefined && scenario.endMark != undefined) 130 { 131 // both startMark and endMark are defined, provide both parameters 132 window.performance.measure(scenario.name, scenario.startMark, scenario.endMark); 133 134 // when startMark is provided to the measure() call, the value of the mark or navigation 135 // timing attribute whose name is provided is used for the startMark 136 scenario.expectedStartTime = (timingAttributes.indexOf(scenario.startMark) != -1 ? 137 window.performance.timing[scenario.startMark] - 138 window.performance.timing.navigationStart : 139 startMarkValue); 140 141 // when endMark is provided to the measure() call, the value of the mark whose name is 142 // provided is used for the startMark 143 scenario.expectedDuration = (timingAttributes.indexOf(scenario.endMark) != -1 ? 144 window.performance.timing[scenario.endMark] - 145 window.performance.timing.navigationStart : 146 endMarkValue) - scenario.expectedStartTime; 147 } 148 } 149 150 // test the test measures are returned by getEntriesByName 151 for (var i in TEST_MEASURES) 152 { 153 entries = window.performance.getEntriesByName(TEST_MEASURES[i].name); 154 test_measure(entries[0], 155 "window.performance.getEntriesByName(\"" + TEST_MEASURES[i].name + "\")[0]", 156 TEST_MEASURES[i].name, 157 TEST_MEASURES[i].expectedStartTime, 158 TEST_MEASURES[i].expectedDuration); 159 TEST_MEASURES[i].entryMatch = entries[0]; 160 } 161 162 done(); 163 } 164 165 function test_measure(measureEntry, measureEntryCommand, expectedName, expectedStartTime, expectedDuration) 166 { 167 // test name 168 test_true(measureEntry.name == expectedName, measureEntryCommand + ".name == \"" + expectedName + "\""); 169 170 // test startTime; since for a mark, the startTime is always equal to a mark's value or the value of a 171 // navigation timing attribute, the actual startTime should match the expected value exactly 172 test_true(Math.abs(measureEntry.startTime - expectedStartTime) == 0, 173 measureEntryCommand + ".startTime is correct"); 174 175 // test entryType 176 test_true(measureEntry.entryType == "measure", measureEntryCommand + ".entryType == \"measure\""); 177 178 // test duration, allow for an acceptable threshold in the difference between the actual duration and the 179 // expected value for the duration 180 test_true(Math.abs(measureEntry.duration - expectedDuration) <= testThreshold, measureEntryCommand + 181 ".duration is approximately correct (up to " + testThreshold + "ms difference allowed)"); 182 } 183 </script> 184 </head> 185 <body onload="onload_test();"> 186 <h1>Description</h1> 187 <p>This test validates that the performance.measure() method is working properly when navigation timing 188 attributes are used in place of mark names. This test creates the following measures to test this method: 189 <ul> 190 <li>"measure_nav_start_no_end": created using a measure() call with a navigation timing attribute 191 provided as the startMark and nothing provided as the endMark</li> 192 <li>"measure_nav_start_mark_end": created using a measure() call with a navigation timing attribute 193 provided as the startMark and a mark name provided as the endMark</li> 194 <li>"measure_mark_start_nav_end": created using a measure() call with a mark name provided as the 195 startMark and a navigation timing attribute provided as the endMark</li> 196 <li>"measure_nav_start_nav_end":created using a measure() call with a navigation timing attribute 197 provided as both the startMark and endMark</li> 198 </ul> 199 After creating each measure, the existence of these measures is validated by calling 200 performance.getEntriesByName() with each measure name 201 </p> 202 203 <div id="log"></div> 204 </body> 205</html> 206