request-timings-measurer.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const EventEmitter = require('events').EventEmitter;
  4. class RequestTimgingsMeasurer extends EventEmitter {
  5. constructor(options) {
  6. super();
  7. this.e = null;
  8. this.timings = {
  9. // start: 0,
  10. // lookup: -1,
  11. // connect: -1,
  12. // ready: -1,
  13. // waiting: -1,
  14. // download: -1,
  15. // end: -1
  16. };
  17. this.e = null;
  18. this.enable = options.enable === true;
  19. this.timerStarted = false;
  20. this.intervalId = null;
  21. this.timeoutId = null;
  22. this.waitingTime = options.waitingTime || 1000;
  23. this.interval = options.interval || 200;
  24. }
  25. static new(options) {
  26. return new RequestTimgingsMeasurer(options);
  27. }
  28. /* istanbul ignore next */
  29. measure(clientRequest) {
  30. if (!this.enable) {
  31. return;
  32. }
  33. this.startTimer();
  34. const timings = this.timings;
  35. timings.start = Date.now();
  36. clientRequest
  37. .once('response', message => {
  38. timings.response = Date.now();
  39. timings.waiting = Date.now() - timings.start;
  40. message.once('end', () => {
  41. timings.socket = timings.socket || 0;
  42. // timings.lookup = timings.lookup || timings.socket
  43. // timings.connect = timings.connect || timings.lookup
  44. timings.download = Date.now() - timings.response;
  45. timings.end = Date.now() - timings.start;
  46. this.stopTimer('end');
  47. });
  48. })
  49. .once('socket', socket => {
  50. timings.socket = Date.now() - timings.start;
  51. const onlookup = () => {
  52. this.timings.lookup = Date.now() - this.timings.start;
  53. };
  54. const onconnect = () => {
  55. this.timings.connect = Date.now() - this.timings.start;
  56. };
  57. const onready = () => {
  58. this.timings.ready = Date.now() - this.timings.start;
  59. };
  60. if (socket.connecting) {
  61. socket.once('lookup', onlookup);
  62. socket.once('connect', onconnect);
  63. socket.once('ready', onready);
  64. socket.once('error', e => {
  65. socket.off('lookup', onlookup);
  66. socket.off('connect', onconnect);
  67. socket.off('ready', onready);
  68. this.e = e;
  69. this.timings.error = Date.now() - this.timings.start;
  70. this.stopTimer(`ee:${e.message}`);
  71. });
  72. }
  73. else {
  74. this.timings.lookup = -1;
  75. this.timings.connect = -1;
  76. this.timings.ready = -1;
  77. }
  78. // socket.once('data', () => {})
  79. // socket.once('drain', () => {})
  80. // socket.once('end', () => {
  81. // this.stopTimer('end')
  82. // })
  83. // socket.once('timeout', () => {
  84. // this.timings.timeout = Date.now() - this.timings.start
  85. // })
  86. })
  87. .on('error', (e) => {
  88. this.stopTimer(`ee:${e.message}`);
  89. });
  90. }
  91. /* istanbul ignore next */
  92. startTimer() {
  93. if (!this.enable) {
  94. return;
  95. }
  96. if (this.timerStarted) {
  97. return;
  98. }
  99. this.timerStarted = true;
  100. this.intervalId = null;
  101. this.timeoutId = setTimeout(() => {
  102. this.process('inprogress');
  103. this.intervalId = setInterval(() => {
  104. this.process('inprogress');
  105. }, this.interval);
  106. }, this.waitingTime);
  107. }
  108. /* istanbul ignore next */
  109. stopTimer(reason) {
  110. // if (!this.enable) {
  111. // return
  112. // }
  113. // if (!this.timerStarted) {
  114. // return
  115. // }
  116. this.timerStarted = false;
  117. clearTimeout(this.timeoutId);
  118. clearInterval(this.intervalId);
  119. this.process(reason);
  120. }
  121. /* istanbul ignore next */
  122. process(reason) {
  123. this.emit('progress', Object.assign({}, this.timings), reason);
  124. }
  125. }
  126. exports.RequestTimgingsMeasurer = RequestTimgingsMeasurer;