Path: blob/trunk/third_party/closure/goog/labs/net/webchannel/requeststats.js
1865 views
// Copyright 2013 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Static utilities for collecting stats associated with16* ChannelRequest.17*18* @visibility {:internal}19* @visibility {:legacy_users}20*/212223goog.provide('goog.labs.net.webChannel.requestStats');24goog.provide('goog.labs.net.webChannel.requestStats.Event');25goog.provide('goog.labs.net.webChannel.requestStats.ServerReachability');26goog.provide('goog.labs.net.webChannel.requestStats.ServerReachabilityEvent');27goog.provide('goog.labs.net.webChannel.requestStats.Stat');28goog.provide('goog.labs.net.webChannel.requestStats.StatEvent');29goog.provide('goog.labs.net.webChannel.requestStats.TimingEvent');3031goog.require('goog.events.Event');32goog.require('goog.events.EventTarget');333435goog.scope(function() {36var requestStats = goog.labs.net.webChannel.requestStats;373839/**40* Events fired.41* @const42*/43requestStats.Event = {};444546/**47* Singleton event target for firing stat events48* @type {goog.events.EventTarget}49* @private50*/51requestStats.statEventTarget_ = new goog.events.EventTarget();525354/**55* The type of event that occurs every time some information about how reachable56* the server is is discovered.57*/58requestStats.Event.SERVER_REACHABILITY_EVENT = 'serverreachability';596061/**62* Types of events which reveal information about the reachability of the63* server.64* @enum {number}65*/66requestStats.ServerReachability = {67REQUEST_MADE: 1,68REQUEST_SUCCEEDED: 2,69REQUEST_FAILED: 3,70BACK_CHANNEL_ACTIVITY: 471};72737475/**76* Event class for SERVER_REACHABILITY_EVENT.77*78* @param {goog.events.EventTarget} target The stat event target for79the channel.80* @param {requestStats.ServerReachability} reachabilityType81* The reachability event type.82* @constructor83* @extends {goog.events.Event}84*/85requestStats.ServerReachabilityEvent = function(target, reachabilityType) {86goog.events.Event.call(87this, requestStats.Event.SERVER_REACHABILITY_EVENT, target);8889/**90* @type {requestStats.ServerReachability}91*/92this.reachabilityType = reachabilityType;93};94goog.inherits(requestStats.ServerReachabilityEvent, goog.events.Event);959697/**98* Notify the channel that a particular fine grained network event has occurred.99* Should be considered package-private.100* @param {requestStats.ServerReachability} reachabilityType101* The reachability event type.102*/103requestStats.notifyServerReachabilityEvent = function(reachabilityType) {104var target = requestStats.statEventTarget_;105target.dispatchEvent(106new requestStats.ServerReachabilityEvent(target, reachabilityType));107};108109110/**111* Stat Event that fires when things of interest happen that may be useful for112* applications to know about for stats or debugging purposes.113*/114requestStats.Event.STAT_EVENT = 'statevent';115116117/**118* Enum that identifies events for statistics that are interesting to track.119* @enum {number}120*/121requestStats.Stat = {122/** Event indicating a new connection attempt. */123CONNECT_ATTEMPT: 0,124125/** Event indicating a connection error due to a general network problem. */126ERROR_NETWORK: 1,127128/**129* Event indicating a connection error that isn't due to a general network130* problem.131*/132ERROR_OTHER: 2,133134/** Event indicating the start of test stage one. */135TEST_STAGE_ONE_START: 3,136137/** Event indicating the start of test stage two. */138TEST_STAGE_TWO_START: 4,139140/** Event indicating the first piece of test data was received. */141TEST_STAGE_TWO_DATA_ONE: 5,142143/**144* Event indicating that the second piece of test data was received and it was145* received separately from the first.146*/147TEST_STAGE_TWO_DATA_TWO: 6,148149/** Event indicating both pieces of test data were received simultaneously. */150TEST_STAGE_TWO_DATA_BOTH: 7,151152/** Event indicating stage one of the test request failed. */153TEST_STAGE_ONE_FAILED: 8,154155/** Event indicating stage two of the test request failed. */156TEST_STAGE_TWO_FAILED: 9,157158/**159* Event indicating that a buffering proxy is likely between the client and160* the server.161*/162PROXY: 10,163164/**165* Event indicating that no buffering proxy is likely between the client and166* the server.167*/168NOPROXY: 11,169170/** Event indicating an unknown SID error. */171REQUEST_UNKNOWN_SESSION_ID: 12,172173/** Event indicating a bad status code was received. */174REQUEST_BAD_STATUS: 13,175176/** Event indicating incomplete data was received */177REQUEST_INCOMPLETE_DATA: 14,178179/** Event indicating bad data was received */180REQUEST_BAD_DATA: 15,181182/** Event indicating no data was received when data was expected. */183REQUEST_NO_DATA: 16,184185/** Event indicating a request timeout. */186REQUEST_TIMEOUT: 17,187188/**189* Event indicating that the server never received our hanging GET and so it190* is being retried.191*/192BACKCHANNEL_MISSING: 18,193194/**195* Event indicating that we have determined that our hanging GET is not196* receiving data when it should be. Thus it is dead dead and will be retried.197*/198BACKCHANNEL_DEAD: 19,199200/**201* The browser declared itself offline during the lifetime of a request, or202* was offline when a request was initially made.203*/204BROWSER_OFFLINE: 20205};206207208209/**210* Event class for STAT_EVENT.211*212* @param {goog.events.EventTarget} eventTarget The stat event target for213the channel.214* @param {requestStats.Stat} stat The stat.215* @constructor216* @extends {goog.events.Event}217*/218requestStats.StatEvent = function(eventTarget, stat) {219goog.events.Event.call(this, requestStats.Event.STAT_EVENT, eventTarget);220221/**222* The stat223* @type {requestStats.Stat}224*/225this.stat = stat;226227};228goog.inherits(requestStats.StatEvent, goog.events.Event);229230231/**232* Returns the singleton event target for stat events.233* @return {goog.events.EventTarget} The event target for stat events.234*/235requestStats.getStatEventTarget = function() {236return requestStats.statEventTarget_;237};238239240/**241* Helper function to call the stat event callback.242* @param {requestStats.Stat} stat The stat.243*/244requestStats.notifyStatEvent = function(stat) {245var target = requestStats.statEventTarget_;246target.dispatchEvent(new requestStats.StatEvent(target, stat));247};248249250/**251* An event that fires when POST requests complete successfully, indicating252* the size of the POST and the round trip time.253*/254requestStats.Event.TIMING_EVENT = 'timingevent';255256257258/**259* Event class for requestStats.Event.TIMING_EVENT260*261* @param {goog.events.EventTarget} target The stat event target for262the channel.263* @param {number} size The number of characters in the POST data.264* @param {number} rtt The total round trip time from POST to response in MS.265* @param {number} retries The number of times the POST had to be retried.266* @constructor267* @extends {goog.events.Event}268*/269requestStats.TimingEvent = function(target, size, rtt, retries) {270goog.events.Event.call(this, requestStats.Event.TIMING_EVENT, target);271272/**273* @type {number}274*/275this.size = size;276277/**278* @type {number}279*/280this.rtt = rtt;281282/**283* @type {number}284*/285this.retries = retries;286287};288goog.inherits(requestStats.TimingEvent, goog.events.Event);289290291/**292* Helper function to notify listeners about POST request performance.293*294* @param {number} size Number of characters in the POST data.295* @param {number} rtt The amount of time from POST start to response.296* @param {number} retries The number of times the POST had to be retried.297*/298requestStats.notifyTimingEvent = function(size, rtt, retries) {299var target = requestStats.statEventTarget_;300target.dispatchEvent(301new requestStats.TimingEvent(target, size, rtt, retries));302};303304305/**306* Allows the application to set an execution hooks for when a channel307* starts processing requests. This is useful to track timing or logging308* special information. The function takes no parameters and return void.309* @param {Function} startHook The function for the start hook.310*/311requestStats.setStartThreadExecutionHook = function(startHook) {312requestStats.startExecutionHook_ = startHook;313};314315316/**317* Allows the application to set an execution hooks for when a channel318* stops processing requests. This is useful to track timing or logging319* special information. The function takes no parameters and return void.320* @param {Function} endHook The function for the end hook.321*/322requestStats.setEndThreadExecutionHook = function(endHook) {323requestStats.endExecutionHook_ = endHook;324};325326327/**328* Application provided execution hook for the start hook.329*330* @type {Function}331* @private332*/333requestStats.startExecutionHook_ = function() {};334335336/**337* Application provided execution hook for the end hook.338*339* @type {Function}340* @private341*/342requestStats.endExecutionHook_ = function() {};343344345/**346* Helper function to call the start hook347*/348requestStats.onStartExecution = function() {349requestStats.startExecutionHook_();350};351352353/**354* Helper function to call the end hook355*/356requestStats.onEndExecution = function() {357requestStats.endExecutionHook_();358};359360361/**362* Wrapper around SafeTimeout which calls the start and end execution hooks363* with a try...finally block.364* @param {Function} fn The callback function.365* @param {number} ms The time in MS for the timer.366* @return {number} The ID of the timer.367*/368requestStats.setTimeout = function(fn, ms) {369if (!goog.isFunction(fn)) {370throw Error('Fn must not be null and must be a function');371}372return goog.global.setTimeout(function() {373requestStats.onStartExecution();374try {375fn();376} finally {377requestStats.onEndExecution();378}379}, ms);380};381}); // goog.scope382383384