@@ -39,9 +39,10 @@ process.nextTick(function() {
39
39
let bodySize = 0 ;
40
40
const maxBodySize = 1024 * 1024 ;
41
41
const timeStart = Date . now ( ) ;
42
- let timeEnd = 0 ;
42
+ let timeLookup = timeStart ;
43
+ let timeConnect = timeStart ;
43
44
let timeResponse = 0 ;
44
- // var timeCurr = timeStart ;
45
+ let timeEnd = 0 ;
45
46
let remoteAddress = '' ;
46
47
let remotePort = '' ;
47
48
let localAddress = '' ;
@@ -122,10 +123,10 @@ process.nextTick(function() {
122
123
GotRequestHeaders : new Date ( timeStart ) ,
123
124
ClientDoneRequest : new Date ( timeStart ) ,
124
125
GatewayTime : 0 ,
125
- DNSTime : 0 ,
126
- TCPConnectTime : 0 ,
126
+ DNSTime : timeLookup - timeStart ,
127
+ TCPConnectTime : timeConnect - timeStart ,
127
128
HTTPSHandshakeTime : 0 ,
128
- ServerConnected : new Date ( timeStart ) ,
129
+ ServerConnected : new Date ( timeConnect ) ,
129
130
FiddlerBeginRequest : new Date ( timeStart ) ,
130
131
ServerGotRequest : new Date ( timeStart ) ,
131
132
ServerBeginResponse : new Date ( timeResponse ) ,
@@ -139,6 +140,51 @@ process.nextTick(function() {
139
140
logJson . ajax . push ( curr ) ;
140
141
} ;
141
142
143
+
144
+ const finish = function ( maybeResponse ) {
145
+ if ( timeEnd ) {
146
+ return ;
147
+ }
148
+
149
+ timeEnd = new Date ( ) . getTime ( ) ;
150
+
151
+ if ( captureBody ) {
152
+ buffer = Buffer . concat ( result ) ;
153
+ result = [ ] ;
154
+ }
155
+
156
+ // 上报
157
+ if ( captureBody ) {
158
+ report ( maybeResponse ) ;
159
+ }
160
+ } ;
161
+
162
+ request . once ( 'socket' , function ( socket ) {
163
+ socket . once ( 'lookup' , ( err , address , family , host ) => {
164
+ timeLookup = Date . now ( ) ;
165
+ if ( err ) {
166
+ logger . error ( logPre + err . stack ) ;
167
+ finish ( ) ;
168
+ return ;
169
+ }
170
+ const cost = timeLookup - timeStart ;
171
+ logger . debug ( `${ logPre } dns lookup ${ host } -> ${ address } , cost ${ cost } ms` ) ;
172
+ } ) ;
173
+
174
+ socket . once ( 'connect' , function ( ) {
175
+ timeConnect = Date . now ( ) ;
176
+ const cost = timeConnect - timeStart ;
177
+ remoteAddress = this . remoteAddress ;
178
+ remotePort = this . remotePort ;
179
+ logger . debug ( `${ logPre } connect ${ remoteAddress } :${ remotePort } , cost ${ cost } ms` ) ;
180
+ } ) ;
181
+ } ) ;
182
+
183
+ request . once ( 'error' , function ( err ) {
184
+ logger . error ( err . stack ) ;
185
+ finish ( ) ;
186
+ } ) ;
187
+
142
188
request . once ( 'response' , ( response ) => {
143
189
timeResponse = Date . now ( ) ;
144
190
@@ -163,35 +209,10 @@ process.nextTick(function() {
163
209
164
210
const done = function ( ) {
165
211
this . removeListener ( 'data' , data ) ;
166
-
167
- if ( timeEnd ) {
168
- return ;
169
- }
170
-
171
- timeEnd = new Date ( ) . getTime ( ) ;
172
-
173
- if ( captureBody ) {
174
- buffer = Buffer . concat ( result ) ;
175
- result = [ ] ;
176
- }
177
-
178
- // 上报
179
- if ( captureBody ) {
180
- report ( response ) ;
181
- }
212
+ finish ( response ) ;
182
213
} ;
183
214
184
215
const data = function ( chunk ) {
185
- // var cost = Date.now() - timeCurr;
186
-
187
- // timeCurr = Date.now();
188
-
189
- // logger.debug('${logPre}receive data: ${size},\tcost: ${cost}ms',{
190
- // logPre: logPre,
191
- // cost: cost,
192
- // size: chunk.length
193
- // });
194
-
195
216
bodySize += chunk . length ;
196
217
197
218
if ( captureBody && bodySize <= maxBodySize ) {
0 commit comments