RequestTimingView.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 2010 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @extends {WebInspector.View}
  33. * @param {WebInspector.NetworkRequest} request
  34. */
  35. WebInspector.RequestTimingView = function(request)
  36. {
  37. WebInspector.View.call(this);
  38. this.element.addStyleClass("resource-timing-view");
  39. this._request = request;
  40. }
  41. WebInspector.RequestTimingView.prototype = {
  42. wasShown: function()
  43. {
  44. this._request.addEventListener(WebInspector.NetworkRequest.Events.TimingChanged, this._refresh, this);
  45. if (!this._request.timing) {
  46. if (!this._emptyView) {
  47. this._emptyView = new WebInspector.EmptyView(WebInspector.UIString("This request has no detailed timing info."));
  48. this._emptyView.show(this.element);
  49. this.innerView = this._emptyView;
  50. }
  51. return;
  52. }
  53. if (this._emptyView) {
  54. this._emptyView.detach();
  55. delete this._emptyView;
  56. }
  57. this._refresh();
  58. },
  59. willHide: function()
  60. {
  61. this._request.removeEventListener(WebInspector.NetworkRequest.Events.TimingChanged, this._refresh, this);
  62. },
  63. _refresh: function()
  64. {
  65. if (this._tableElement)
  66. this._tableElement.remove();
  67. this._tableElement = WebInspector.RequestTimingView.createTimingTable(this._request);
  68. this.element.appendChild(this._tableElement);
  69. },
  70. __proto__: WebInspector.View.prototype
  71. }
  72. WebInspector.RequestTimingView.createTimingTable = function(request)
  73. {
  74. var tableElement = document.createElement("table");
  75. var rows = [];
  76. function addRow(title, className, start, end)
  77. {
  78. var row = {};
  79. row.title = title;
  80. row.className = className;
  81. row.start = start;
  82. row.end = end;
  83. rows.push(row);
  84. }
  85. if (request.timing.proxyStart !== -1)
  86. addRow(WebInspector.UIString("Proxy"), "proxy", request.timing.proxyStart, request.timing.proxyEnd);
  87. if (request.timing.dnsStart !== -1)
  88. addRow(WebInspector.UIString("DNS Lookup"), "dns", request.timing.dnsStart, request.timing.dnsEnd);
  89. if (request.timing.connectStart !== -1) {
  90. var label = request.connectionReused ? WebInspector.UIString("Blocking") : WebInspector.UIString("Connecting");
  91. addRow(label, "connecting", request.timing.connectStart, request.timing.connectEnd);
  92. }
  93. if (request.timing.sslStart !== -1)
  94. addRow(WebInspector.UIString("SSL"), "ssl", request.timing.sslStart, request.timing.sslEnd);
  95. addRow(WebInspector.UIString("Sending"), "sending", request.timing.sendStart, request.timing.sendEnd);
  96. addRow(WebInspector.UIString("Waiting"), "waiting", request.timing.sendEnd, request.timing.receiveHeadersEnd);
  97. addRow(WebInspector.UIString("Receiving"), "receiving", (request.responseReceivedTime - request.timing.requestTime) * 1000, (request.endTime - request.timing.requestTime) * 1000);
  98. const chartWidth = 200;
  99. var total = (request.endTime - request.timing.requestTime) * 1000;
  100. var scale = chartWidth / total;
  101. for (var i = 0; i < rows.length; ++i) {
  102. var tr = document.createElement("tr");
  103. tableElement.appendChild(tr);
  104. var td = document.createElement("td");
  105. td.textContent = rows[i].title;
  106. tr.appendChild(td);
  107. td = document.createElement("td");
  108. td.width = chartWidth + "px";
  109. var row = document.createElement("div");
  110. row.className = "network-timing-row";
  111. td.appendChild(row);
  112. var bar = document.createElement("span");
  113. bar.className = "network-timing-bar " + rows[i].className;
  114. bar.style.left = scale * rows[i].start + "px";
  115. bar.style.right = scale * (total - rows[i].end) + "px";
  116. bar.style.backgroundColor = rows[i].color;
  117. bar.textContent = "\u200B"; // Important for 0-time items to have 0 width.
  118. row.appendChild(bar);
  119. var title = document.createElement("span");
  120. title.className = "network-timing-bar-title";
  121. if (total - rows[i].end < rows[i].start)
  122. title.style.right = (scale * (total - rows[i].end) + 3) + "px";
  123. else
  124. title.style.left = (scale * rows[i].start + 3) + "px";
  125. title.textContent = Number.secondsToString((rows[i].end - rows[i].start) / 1000);
  126. row.appendChild(title);
  127. tr.appendChild(td);
  128. }
  129. return tableElement;
  130. }