-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBandwidthTester.as
More file actions
111 lines (92 loc) · 2.7 KB
/
Copy pathBandwidthTester.as
File metadata and controls
111 lines (92 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package nativeClasses.player
{
import flash.net.*;
import flash.utils.*;
import flash.events.*;
public class BandwidthTester extends EventDispatcher
{
public static const BAND_TESTED:String = 'tested';
public static const TEST:String = 'test';
private var bandwidth:* = 0; //final average bandwidth
private var peak_bandwidth:* = 0; //peak bandwidth
private var curr_bandwidth:* = 0; //current take bandwidth
private var testfile:* = '';
private var l:*; //loader
private var tm:*; //timer
private var last_bytes:* = 0; //bytes loaded last time
private var bands:*; //recorded byte speeds
private var _latency:* = 1; //network utilization approximation
public function BandwidthTester(latency:* = 0,URL_Path:String="")
{
tm = new Timer(1000, 3);
testfile = URL_Path;
tm.addEventListener(TimerEvent.TIMER, get_band);
tm.addEventListener(TimerEvent.TIMER_COMPLETE, timer_complete);
bands = new Array();
_latency = 1 - latency;
}
public function start():void
{
l = new URLLoader();
l.addEventListener(Event.OPEN, start_timer);
l.addEventListener(Event.COMPLETE, end_download);
l.load(new URLRequest(testfile));
}
private function get_band(e:TimerEvent):void
{
curr_bandwidth = Math.floor(((l.bytesLoaded - last_bytes) / 125) * _latency);
bands.push(curr_bandwidth);
last_bytes = l.bytesLoaded;
dispatchEvent(new Event(BandwidthTester.TEST));
}
public function start_timer(e:Event):void
{
tm.start();
}
private function timer_complete(e:TimerEvent):void
{
l.close();
bands.sort(Array.NUMERIC | Array.DESCENDING);
peak_bandwidth = bands[0];
bandwidth = calc_avg_bandwidth();
dispatchEvent(new Event(BandwidthTester.BAND_TESTED));
}
private function end_download(e:*):void
{
tm.removeEventListener(TimerEvent.TIMER, get_band);
tm.removeEventListener(TimerEvent.TIMER_COMPLETE, timer_complete);
tm.stop();
l.close();
bands.sort(Array.NUMERIC | Array.DESCENDING);
bandwidth = 10000;
peak_bandwidth = (bands[0]) ? bands[0] : bandwidth;
dispatchEvent(new Event(BandwidthTester.BAND_TESTED));
}
private function calc_avg_bandwidth():Number
{
var total:* = 0;
var len:int = bands.length;
while (len--)
{
total += bands[len];
}
return Math.round(total / bands.length);
}
public function set latency(prc:*):void
{
this._latency = 1 - prc;
}
public function getBandwidth():*
{
return bandwidth;
}
public function getPeak():*
{
return peak_bandwidth;
}
public function last_speed():*
{
return curr_bandwidth;
}
}
}