Source: lib/debug/timer.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.timer');
  18. goog.require('shaka.log');
  19. /**
  20. * @namespace shaka.timer
  21. * @summary A performance timing framework.
  22. * Used in both debugging and production builds.
  23. */
  24. /**
  25. * Begins a timer.
  26. *
  27. * @param {string} name
  28. */
  29. shaka.timer.begin = function(name) {
  30. shaka.timer.timers_[name] = {
  31. begin: shaka.timer.now_(),
  32. end: NaN
  33. };
  34. };
  35. /**
  36. * End a timer and log (debug) the elapsed time.
  37. * Does nothing if the timer has not begun.
  38. *
  39. * @param {string} name
  40. */
  41. shaka.timer.end = function(name) {
  42. var record = shaka.timer.timers_[name];
  43. if (!record) {
  44. return;
  45. }
  46. record.end = shaka.timer.now_();
  47. var diff = record.end - record.begin;
  48. shaka.log.debug(name + ': ' + diff.toFixed(3) + 'ms');
  49. };
  50. /**
  51. * Log (debug) the diff between two or more completed timers and return it.
  52. * Does nothing if not all of the timers have begun.
  53. *
  54. * @param {string} name1
  55. * @param {string} name2
  56. * @param {...string} var_args
  57. * @return {number} The diff between the timers, or NaN if they have not all
  58. * completed.
  59. */
  60. shaka.timer.diff = function(name1, name2, var_args) {
  61. var t1 = shaka.timer.get(name1);
  62. var t2 = shaka.timer.get(name2);
  63. if (!t1 || !t2) {
  64. return NaN;
  65. }
  66. var diff = t1 - t2;
  67. var name = name1 + ' - ' + name2;
  68. for (var i = 2; i < arguments.length; ++i) {
  69. var name3 = arguments[i];
  70. var t3 = shaka.timer.get(name3);
  71. if (!t3) {
  72. return NaN;
  73. }
  74. diff -= t3;
  75. name += ' - ' + name3;
  76. }
  77. shaka.log.debug(name + ': ' + diff.toFixed(3) + 'ms');
  78. return diff;
  79. };
  80. /**
  81. * Query a timer.
  82. *
  83. * @param {string} name
  84. * @return {number} The elapsed time in milliseconds, if the timer is complete.
  85. * Returns NaN if the timer doesn't exist or hasn't ended yet.
  86. */
  87. shaka.timer.get = function(name) {
  88. var record = shaka.timer.timers_[name];
  89. if (!record || !record.end) {
  90. return NaN;
  91. }
  92. return record.end - record.begin;
  93. };
  94. /** @private {function():number} */
  95. shaka.timer.now_ = window.performance && window.performance.now ?
  96. window.performance.now.bind(window.performance) :
  97. Date.now;
  98. /** @private {!Object.<string, {begin: number, end: number}>} */
  99. shaka.timer.timers_ = {};