test_user_timing_mark.htm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>window.performance User Timing mark() method is working properly</title>
  6. <link rel="author" title="Microsoft" href="http://www.microsoft.com/" />
  7. <link rel="help" href="http://w3c-test.org/webperf/specs/UserTiming/#dom-performance-mark"/>
  8. <script src="testharness.js"></script>
  9. <script src="testharnessreport.js"></script>
  10. <script src="webperftestharness.js"></script>
  11. <script src="../src/usertiming.js"></script>
  12. <script type="text/javascript">
  13. // test data
  14. var markTestDelay = 200;
  15. var testThreshold = 50;
  16. var marks;
  17. var TEST_MARKS =
  18. [
  19. {
  20. name: "mark1",
  21. expectedStartTime: undefined,
  22. entryMatch: undefined
  23. },
  24. {
  25. name: "mark1",
  26. expectedStartTime: undefined,
  27. entryMatch: undefined
  28. }
  29. ];
  30. setup({timeout:1000, explicit_done: true});
  31. test_namespace();
  32. function onload_test()
  33. {
  34. // test for existance of User Timing and Performance Timeline interface
  35. if (window.performance.mark == undefined ||
  36. window.performance.clearMarks == undefined ||
  37. window.performance.measure == undefined ||
  38. window.performance.clearMeasures == undefined ||
  39. window.performance.getEntriesByName == undefined ||
  40. window.performance.getEntriesByType == undefined ||
  41. window.performance.getEntries == undefined)
  42. {
  43. test_true(false,
  44. "The User Timing and Performance Timeline interfaces, which are required for this test, " +
  45. "are defined.");
  46. done();
  47. }
  48. else
  49. {
  50. // create first mark
  51. window.performance.mark(TEST_MARKS[0].name);
  52. // record the time that this mark is created; this should correspond to the mark's startTime
  53. TEST_MARKS[0].expectedStartTime = (new Date()) - window.performance.timing.navigationStart;
  54. // create the duplicate mark using the test delay; the duplicate mark's value should be equivalent to
  55. // the loadEventStart navigation timing attribute plus the test delay
  56. setTimeout(mark_test_cb, markTestDelay);
  57. }
  58. }
  59. function mark_test_cb()
  60. {
  61. var getByNameScenarios = new Array();
  62. // create second, duplicate mark
  63. window.performance.mark(TEST_MARKS[1].name);
  64. // record the time that this mark is created; this should correspond to the mark's startTime
  65. TEST_MARKS[1].expectedStartTime = (new Date()) - window.performance.timing.navigationStart;
  66. // test the test marks are returned by getEntriesByName
  67. entries = window.performance.getEntriesByName(TEST_MARKS[0].name);
  68. test_mark(entries[0],
  69. "window.performance.getEntriesByName(\"" + TEST_MARKS[0].name + "\")[0]",
  70. TEST_MARKS[0].name,
  71. TEST_MARKS[0].expectedStartTime);
  72. TEST_MARKS[0].entryMatch = entries[0];
  73. test_mark(entries[1],
  74. "window.performance.getEntriesByName(\"" + TEST_MARKS[1].name + "\")[1]",
  75. TEST_MARKS[1].name,
  76. TEST_MARKS[1].expectedStartTime);
  77. TEST_MARKS[1].entryMatch = entries[1];
  78. // test the test marks are returned by getEntriesByName with the entryType parameter provided
  79. entries = window.performance.getEntriesByName(TEST_MARKS[0].name, "mark");
  80. test_equals(entries[0].name, TEST_MARKS[0].name,
  81. "window.performance.getEntriesByName(\"" + TEST_MARKS[0].name + "\", \"mark\") returns an " +
  82. "object containing the \"" + TEST_MARKS[0].name + "\" mark in the correct order");
  83. test_equals(entries[1].name, TEST_MARKS[1].name,
  84. "window.performance.getEntriesByName(\"" + TEST_MARKS[1].name + "\", \"mark\") returns an " +
  85. "object containing the duplicate \"" + TEST_MARKS[1].name + "\" mark in the correct order");
  86. test_true(match_entries(entries[0], TEST_MARKS[0].entryMatch),
  87. "The \"" + TEST_MARKS[0].name + "\" mark returned by " +
  88. "window.performance.getEntriesByName(\"" + TEST_MARKS[0].name + "\", \"mark\") matches the " +
  89. "the \"" + TEST_MARKS[0].name + "\" mark returned by " +
  90. "window.performance.getEntriesByName(\"" + TEST_MARKS[0].name + "\")");
  91. test_true(match_entries(entries[1], TEST_MARKS[1].entryMatch),
  92. "The duplicate \"" + TEST_MARKS[1].name + "\" mark returned by " +
  93. "window.performance.getEntriesByName(\"" + TEST_MARKS[1].name + "\", \"mark\") matches the " +
  94. "the duplicate \"" + TEST_MARKS[1].name + "\" mark returned by " +
  95. "window.performance.getEntriesByName(\"" + TEST_MARKS[1].name + "\")");
  96. // test the test marks are returned by getEntries
  97. entries = get_test_entries(window.performance.getEntries(), "mark");
  98. test_equals(entries[0].name, TEST_MARKS[0].name,
  99. "window.performance.getEntries() returns an object containing the original \"" +
  100. TEST_MARKS[0].name + "\" mark in the correct order");
  101. test_equals(entries[1].name, TEST_MARKS[1].name,
  102. "window.performance.getEntries() returns an object containing the duplicate \"" +
  103. TEST_MARKS[1].name + "\" mark in the correct order");
  104. test_true(match_entries(entries[0], TEST_MARKS[0].entryMatch),
  105. "The \"" + TEST_MARKS[0].name + "\" mark returned by " +
  106. "window.performance.getEntries() matches the the \"" + TEST_MARKS[0].name + "\" mark returned " +
  107. "by window.performance.getEntriesByName(\"" + TEST_MARKS[0].name + "\")");
  108. test_true(match_entries(entries[1], TEST_MARKS[1].entryMatch),
  109. "The \"" + TEST_MARKS[1].name + "\" mark returned by " +
  110. "window.performance.getEntries() matches the the duplicate \"" + TEST_MARKS[1].name + "\" mark " +
  111. "returned by window.performance.getEntriesByName(\"" + TEST_MARKS[1].name + "\")");
  112. // test the test marks are returned by getEntriesByType
  113. entries = window.performance.getEntriesByType("mark");
  114. test_equals(entries[0].name, TEST_MARKS[0].name,
  115. "window.performance.getEntriesByType(\"mark\") returns an object containing the original \"" +
  116. TEST_MARKS[0].name + "\" mark in the correct order");
  117. test_equals(entries[1].name, TEST_MARKS[1].name,
  118. "window.performance.getEntriesByType(\"mark\") returns an object containing the duplicate \"" +
  119. TEST_MARKS[1].name + "\" mark in the correct order");
  120. test_true(match_entries(entries[0], TEST_MARKS[0].entryMatch),
  121. "The \"" + TEST_MARKS[0].name + "\" mark returned by " +
  122. "window.performance.getEntriesByType(\"mark\") matches the the \"" + TEST_MARKS[0].name +
  123. "\" mark returned by window.performance.getEntriesByName(\"" + TEST_MARKS[0].name + "\")");
  124. test_true(match_entries(entries[1], TEST_MARKS[1].entryMatch),
  125. "The \"" + TEST_MARKS[1].name + "\" mark returned by " +
  126. "window.performance.getEntriesByType(\"mark\") matches the the duplicate \"" +
  127. TEST_MARKS[1].name + "\" mark returned by window.performance.getEntriesByName(\"" +
  128. TEST_MARKS[1].name + "\")");
  129. done();
  130. }
  131. function match_entries(entry1, entry2)
  132. {
  133. var pass = true;
  134. // match name
  135. pass = pass && (entry1.name == entry2.name);
  136. // match startTime
  137. pass = pass && (entry1.startTime == entry2.startTime);
  138. // match entryType
  139. pass = pass && (entry1.entryType == entry2.entryType);
  140. // match duration
  141. pass = pass && (entry1.duration == entry2.duration);
  142. return pass;
  143. }
  144. function test_mark(markEntry, markEntryCommand, expectedName, expectedStartTime)
  145. {
  146. // test name
  147. test_equals(markEntry.name, expectedName, markEntryCommand + ".name == \"" + expectedName + "\"");
  148. // test startTime, allow for an acceptable threshold in the difference between the startTime and the
  149. // expected value for the startTime (loadEventStart + markTestDelay)
  150. test_true(Math.abs(markEntry.startTime - expectedStartTime) <= testThreshold,
  151. markEntryCommand + ".startTime ~== " + expectedStartTime + " (up to " + testThreshold +
  152. "ms difference allowed)");
  153. console.log("mes: " + markEntry.startTime);
  154. console.log("es: " + expectedStartTime);
  155. console.log("d: " + (markEntry.startTime - expectedStartTime));
  156. // verify entryType
  157. test_equals(markEntry.entryType, "mark", markEntryCommand + ".entryType == \"mark\"");
  158. // verify duration
  159. test_equals(markEntry.duration, 0, markEntryCommand + ".duration == 0");
  160. }
  161. function get_test_entries(entryList, entryType)
  162. {
  163. var testEntries = new Array();
  164. // filter entryList
  165. for (var i in entryList)
  166. {
  167. if (entryList[i].entryType == entryType)
  168. {
  169. testEntries.push(entryList[i]);
  170. }
  171. }
  172. return testEntries;
  173. }
  174. </script>
  175. </head>
  176. <body onload="onload_test();">
  177. <h1>Description</h1>
  178. <p>This test validates that the performance.mark() method is working properly. This test creates the
  179. following marks to test this method:
  180. <ul>
  181. <li>"mark1": created using a normal mark() call</li>
  182. <li>"mark1": duplicate of the first mark, used to confirm names can be re-used</li>
  183. </ul>
  184. After creating each mark, the existence of these marks is validated by calling
  185. performance.getEntriesByName() (both with and without the entryType parameter provided),
  186. performance.getEntriesByType(), and performance.getEntries()
  187. </p>
  188. <div id="log"></div>
  189. </body>
  190. </html>