1  /**
     2   * com.sekati.net.Bandwidth
     3   * @version 1.0.0
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   */
     8   
     9  import com.sekati.core.CoreObject;
    10  import com.sekati.time.StopWatch;
    11  import com.sekati.utils.Delegate;
    12  
    13  /**
    14   * Simple bandwidth throughput test
    15   * {@code Usage:
    16   * function bandwidthResult(speed:Number, testsize:Number, ms:Number){
    17   * 	trace("bandwidth speed: "+speed+"kbps, test filesize: "+testsize+", test time: "+ms+"ms");
    18   * }
    19   * var bandwidthTest = new com.sekati.net.Bandwidth("assets/bandwidth_data/50k", bandwidthResult);
    20   * } 
    21   */
    22  class com.sekati.net.Bandwidth extends CoreObject {
    23  
    24  	private var _timer:StopWatch;
    25  	private var _cb:Function;
    26  	private var _con:LoadVars;	
    27  
    28  	/**
    29  	 * constructor
    30  	 * @param uri (String) uri to bandwidth test file (should be non tcp/ip compressable random "junk data" see deploy/assets/bandwidth_data) 
    31  	 * @param cb (Function) callback function for test to return speed, filesize, ms results.
    32  	 */
    33  	public function Bandwidth(uri:String, cb:Function) {
    34  		super( );
    35  		_cb = cb;
    36  		_timer = new StopWatch( true );
    37  		_con = new LoadVars( );
    38  		_con.onLoad = Delegate.create( this, testLoaded );
    39  		_con.load( uri + "?" + Math.random( ) );		
    40  	}
    41  
    42  	private function testLoaded(success:Boolean):Void {
    43  		if (success) {
    44  			var ms:Number = _timer.stop( );
    45  			var filesize:Number = _con.getBytesTotal( );
    46  			var speed:Number = Math.round( (filesize / 1024 * 8) / (ms / 1000) );
    47  			_cb( speed, filesize, ms );	
    48  		} else {
    49  			throw new Error( "@@@ " + this.toString( ) + " Error: data file loading failed." );
    50  		}
    51  	}
    52  }