| 1 |
// Copyright 2013 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Static utilities for collecting stats associated with |
| 17 |
* ChannelRequest. |
| 18 |
* |
| 19 |
* @visibility {:internal} |
| 20 |
* @visibility {:legacy_users} |
| 21 |
*/ |
| 22 |
|
| 23 |
|
| 24 |
goog.provide('goog.labs.net.webChannel.requestStats'); |
| 25 |
goog.provide('goog.labs.net.webChannel.requestStats.Event'); |
| 26 |
goog.provide('goog.labs.net.webChannel.requestStats.ServerReachability'); |
| 27 |
goog.provide('goog.labs.net.webChannel.requestStats.ServerReachabilityEvent'); |
| 28 |
goog.provide('goog.labs.net.webChannel.requestStats.Stat'); |
| 29 |
goog.provide('goog.labs.net.webChannel.requestStats.StatEvent'); |
| 30 |
goog.provide('goog.labs.net.webChannel.requestStats.TimingEvent'); |
| 31 |
|
| 32 |
goog.require('goog.events.Event'); |
| 33 |
goog.require('goog.events.EventTarget'); |
| 34 |
|
| 35 |
|
| 36 |
goog.scope(function() { |
| 37 |
var requestStats = goog.labs.net.webChannel.requestStats; |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Events fired. |
| 42 |
* @const |
| 43 |
*/ |
| 44 |
requestStats.Event = {}; |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Singleton event target for firing stat events |
| 49 |
* @type {goog.events.EventTarget} |
| 50 |
* @private |
| 51 |
*/ |
| 52 |
requestStats.statEventTarget_ = new goog.events.EventTarget(); |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* The type of event that occurs every time some information about how reachable |
| 57 |
* the server is is discovered. |
| 58 |
*/ |
| 59 |
requestStats.Event.SERVER_REACHABILITY_EVENT = 'serverreachability'; |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* Types of events which reveal information about the reachability of the |
| 64 |
* server. |
| 65 |
* @enum {number} |
| 66 |
*/ |
| 67 |
requestStats.ServerReachability = { |
| 68 |
REQUEST_MADE: 1, |
| 69 |
REQUEST_SUCCEEDED: 2, |
| 70 |
REQUEST_FAILED: 3, |
| 71 |
BACK_CHANNEL_ACTIVITY: 4 |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* Event class for SERVER_REACHABILITY_EVENT. |
| 78 |
* |
| 79 |
* @param {goog.events.EventTarget} target The stat event target for |
| 80 |
the channel. |
| 81 |
* @param {requestStats.ServerReachability} reachabilityType |
| 82 |
* The reachability event type. |
| 83 |
* @constructor |
| 84 |
* @extends {goog.events.Event} |
| 85 |
*/ |
| 86 |
requestStats.ServerReachabilityEvent = function(target, reachabilityType) { |
| 87 |
goog.events.Event.call( |
| 88 |
this, requestStats.Event.SERVER_REACHABILITY_EVENT, target); |
| 89 |
|
| 90 |
/** |
| 91 |
* @type {requestStats.ServerReachability} |
| 92 |
*/ |
| 93 |
this.reachabilityType = reachabilityType; |
| 94 |
}; |
| 95 |
goog.inherits(requestStats.ServerReachabilityEvent, goog.events.Event); |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* Notify the channel that a particular fine grained network event has occurred. |
| 100 |
* Should be considered package-private. |
| 101 |
* @param {requestStats.ServerReachability} reachabilityType |
| 102 |
* The reachability event type. |
| 103 |
*/ |
| 104 |
requestStats.notifyServerReachabilityEvent = function(reachabilityType) { |
| 105 |
var target = requestStats.statEventTarget_; |
| 106 |
target.dispatchEvent( |
| 107 |
new requestStats.ServerReachabilityEvent(target, reachabilityType)); |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* Stat Event that fires when things of interest happen that may be useful for |
| 113 |
* applications to know about for stats or debugging purposes. |
| 114 |
*/ |
| 115 |
requestStats.Event.STAT_EVENT = 'statevent'; |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* Enum that identifies events for statistics that are interesting to track. |
| 120 |
* @enum {number} |
| 121 |
*/ |
| 122 |
requestStats.Stat = { |
| 123 |
/** Event indicating a new connection attempt. */ |
| 124 |
CONNECT_ATTEMPT: 0, |
| 125 |
|
| 126 |
/** Event indicating a connection error due to a general network problem. */ |
| 127 |
ERROR_NETWORK: 1, |
| 128 |
|
| 129 |
/** |
| 130 |
* Event indicating a connection error that isn't due to a general network |
| 131 |
* problem. |
| 132 |
*/ |
| 133 |
ERROR_OTHER: 2, |
| 134 |
|
| 135 |
/** Event indicating the start of test stage one. */ |
| 136 |
TEST_STAGE_ONE_START: 3, |
| 137 |
|
| 138 |
/** Event indicating the start of test stage two. */ |
| 139 |
TEST_STAGE_TWO_START: 4, |
| 140 |
|
| 141 |
/** Event indicating the first piece of test data was received. */ |
| 142 |
TEST_STAGE_TWO_DATA_ONE: 5, |
| 143 |
|
| 144 |
/** |
| 145 |
* Event indicating that the second piece of test data was received and it was |
| 146 |
* received separately from the first. |
| 147 |
*/ |
| 148 |
TEST_STAGE_TWO_DATA_TWO: 6, |
| 149 |
|
| 150 |
/** Event indicating both pieces of test data were received simultaneously. */ |
| 151 |
TEST_STAGE_TWO_DATA_BOTH: 7, |
| 152 |
|
| 153 |
/** Event indicating stage one of the test request failed. */ |
| 154 |
TEST_STAGE_ONE_FAILED: 8, |
| 155 |
|
| 156 |
/** Event indicating stage two of the test request failed. */ |
| 157 |
TEST_STAGE_TWO_FAILED: 9, |
| 158 |
|
| 159 |
/** |
| 160 |
* Event indicating that a buffering proxy is likely between the client and |
| 161 |
* the server. |
| 162 |
*/ |
| 163 |
PROXY: 10, |
| 164 |
|
| 165 |
/** |
| 166 |
* Event indicating that no buffering proxy is likely between the client and |
| 167 |
* the server. |
| 168 |
*/ |
| 169 |
NOPROXY: 11, |
| 170 |
|
| 171 |
/** Event indicating an unknown SID error. */ |
| 172 |
REQUEST_UNKNOWN_SESSION_ID: 12, |
| 173 |
|
| 174 |
/** Event indicating a bad status code was received. */ |
| 175 |
REQUEST_BAD_STATUS: 13, |
| 176 |
|
| 177 |
/** Event indicating incomplete data was received */ |
| 178 |
REQUEST_INCOMPLETE_DATA: 14, |
| 179 |
|
| 180 |
/** Event indicating bad data was received */ |
| 181 |
REQUEST_BAD_DATA: 15, |
| 182 |
|
| 183 |
/** Event indicating no data was received when data was expected. */ |
| 184 |
REQUEST_NO_DATA: 16, |
| 185 |
|
| 186 |
/** Event indicating a request timeout. */ |
| 187 |
REQUEST_TIMEOUT: 17, |
| 188 |
|
| 189 |
/** |
| 190 |
* Event indicating that the server never received our hanging GET and so it |
| 191 |
* is being retried. |
| 192 |
*/ |
| 193 |
BACKCHANNEL_MISSING: 18, |
| 194 |
|
| 195 |
/** |
| 196 |
* Event indicating that we have determined that our hanging GET is not |
| 197 |
* receiving data when it should be. Thus it is dead dead and will be retried. |
| 198 |
*/ |
| 199 |
BACKCHANNEL_DEAD: 19, |
| 200 |
|
| 201 |
/** |
| 202 |
* The browser declared itself offline during the lifetime of a request, or |
| 203 |
* was offline when a request was initially made. |
| 204 |
*/ |
| 205 |
BROWSER_OFFLINE: 20 |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
/** |
| 211 |
* Event class for STAT_EVENT. |
| 212 |
* |
| 213 |
* @param {goog.events.EventTarget} eventTarget The stat event target for |
| 214 |
the channel. |
| 215 |
* @param {requestStats.Stat} stat The stat. |
| 216 |
* @constructor |
| 217 |
* @extends {goog.events.Event} |
| 218 |
*/ |
| 219 |
requestStats.StatEvent = function(eventTarget, stat) { |
| 220 |
goog.events.Event.call(this, requestStats.Event.STAT_EVENT, eventTarget); |
| 221 |
|
| 222 |
/** |
| 223 |
* The stat |
| 224 |
* @type {requestStats.Stat} |
| 225 |
*/ |
| 226 |
this.stat = stat; |
| 227 |
|
| 228 |
}; |
| 229 |
goog.inherits(requestStats.StatEvent, goog.events.Event); |
| 230 |
|
| 231 |
|
| 232 |
/** |
| 233 |
* Returns the singleton event target for stat events. |
| 234 |
* @return {goog.events.EventTarget} The event target for stat events. |
| 235 |
*/ |
| 236 |
requestStats.getStatEventTarget = function() { |
| 237 |
return requestStats.statEventTarget_; |
| 238 |
}; |
| 239 |
|
| 240 |
|
| 241 |
/** |
| 242 |
* Helper function to call the stat event callback. |
| 243 |
* @param {requestStats.Stat} stat The stat. |
| 244 |
*/ |
| 245 |
requestStats.notifyStatEvent = function(stat) { |
| 246 |
var target = requestStats.statEventTarget_; |
| 247 |
target.dispatchEvent(new requestStats.StatEvent(target, stat)); |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
/** |
| 252 |
* An event that fires when POST requests complete successfully, indicating |
| 253 |
* the size of the POST and the round trip time. |
| 254 |
*/ |
| 255 |
requestStats.Event.TIMING_EVENT = 'timingevent'; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
/** |
| 260 |
* Event class for requestStats.Event.TIMING_EVENT |
| 261 |
* |
| 262 |
* @param {goog.events.EventTarget} target The stat event target for |
| 263 |
the channel. |
| 264 |
* @param {number} size The number of characters in the POST data. |
| 265 |
* @param {number} rtt The total round trip time from POST to response in MS. |
| 266 |
* @param {number} retries The number of times the POST had to be retried. |
| 267 |
* @constructor |
| 268 |
* @extends {goog.events.Event} |
| 269 |
*/ |
| 270 |
requestStats.TimingEvent = function(target, size, rtt, retries) { |
| 271 |
goog.events.Event.call(this, requestStats.Event.TIMING_EVENT, target); |
| 272 |
|
| 273 |
/** |
| 274 |
* @type {number} |
| 275 |
*/ |
| 276 |
this.size = size; |
| 277 |
|
| 278 |
/** |
| 279 |
* @type {number} |
| 280 |
*/ |
| 281 |
this.rtt = rtt; |
| 282 |
|
| 283 |
/** |
| 284 |
* @type {number} |
| 285 |
*/ |
| 286 |
this.retries = retries; |
| 287 |
|
| 288 |
}; |
| 289 |
goog.inherits(requestStats.TimingEvent, goog.events.Event); |
| 290 |
|
| 291 |
|
| 292 |
/** |
| 293 |
* Helper function to notify listeners about POST request performance. |
| 294 |
* |
| 295 |
* @param {number} size Number of characters in the POST data. |
| 296 |
* @param {number} rtt The amount of time from POST start to response. |
| 297 |
* @param {number} retries The number of times the POST had to be retried. |
| 298 |
*/ |
| 299 |
requestStats.notifyTimingEvent = function(size, rtt, retries) { |
| 300 |
var target = requestStats.statEventTarget_; |
| 301 |
target.dispatchEvent( |
| 302 |
new requestStats.TimingEvent(target, size, rtt, retries)); |
| 303 |
}; |
| 304 |
|
| 305 |
|
| 306 |
/** |
| 307 |
* Allows the application to set an execution hooks for when a channel |
| 308 |
* starts processing requests. This is useful to track timing or logging |
| 309 |
* special information. The function takes no parameters and return void. |
| 310 |
* @param {Function} startHook The function for the start hook. |
| 311 |
*/ |
| 312 |
requestStats.setStartThreadExecutionHook = function(startHook) { |
| 313 |
requestStats.startExecutionHook_ = startHook; |
| 314 |
}; |
| 315 |
|
| 316 |
|
| 317 |
/** |
| 318 |
* Allows the application to set an execution hooks for when a channel |
| 319 |
* stops processing requests. This is useful to track timing or logging |
| 320 |
* special information. The function takes no parameters and return void. |
| 321 |
* @param {Function} endHook The function for the end hook. |
| 322 |
*/ |
| 323 |
requestStats.setEndThreadExecutionHook = function(endHook) { |
| 324 |
requestStats.endExecutionHook_ = endHook; |
| 325 |
}; |
| 326 |
|
| 327 |
|
| 328 |
/** |
| 329 |
* Application provided execution hook for the start hook. |
| 330 |
* |
| 331 |
* @type {Function} |
| 332 |
* @private |
| 333 |
*/ |
| 334 |
requestStats.startExecutionHook_ = function() {}; |
| 335 |
|
| 336 |
|
| 337 |
/** |
| 338 |
* Application provided execution hook for the end hook. |
| 339 |
* |
| 340 |
* @type {Function} |
| 341 |
* @private |
| 342 |
*/ |
| 343 |
requestStats.endExecutionHook_ = function() {}; |
| 344 |
|
| 345 |
|
| 346 |
/** |
| 347 |
* Helper function to call the start hook |
| 348 |
*/ |
| 349 |
requestStats.onStartExecution = function() { |
| 350 |
requestStats.startExecutionHook_(); |
| 351 |
}; |
| 352 |
|
| 353 |
|
| 354 |
/** |
| 355 |
* Helper function to call the end hook |
| 356 |
*/ |
| 357 |
requestStats.onEndExecution = function() { |
| 358 |
requestStats.endExecutionHook_(); |
| 359 |
}; |
| 360 |
|
| 361 |
|
| 362 |
/** |
| 363 |
* Wrapper around SafeTimeout which calls the start and end execution hooks |
| 364 |
* with a try...finally block. |
| 365 |
* @param {Function} fn The callback function. |
| 366 |
* @param {number} ms The time in MS for the timer. |
| 367 |
* @return {number} The ID of the timer. |
| 368 |
*/ |
| 369 |
requestStats.setTimeout = function(fn, ms) { |
| 370 |
if (!goog.isFunction(fn)) { |
| 371 |
throw new Error('Fn must not be null and must be a function'); |
| 372 |
} |
| 373 |
return goog.global.setTimeout(function() { |
| 374 |
requestStats.onStartExecution(); |
| 375 |
try { |
| 376 |
fn(); |
| 377 |
} finally { |
| 378 |
requestStats.onEndExecution(); |
| 379 |
} |
| 380 |
}, ms); |
| 381 |
}; |
| 382 |
}); // goog.scope |