Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 Upload speed test


I'm making an speedtest on AS3. And got such a problem.
I'm using URLLoader( ) in my test, and for download test it work very good, but for uploading test i got some troubles. I'm sending some binary data to my php-script, and checking the progress_event to get bytesLoaded, to calculate current speed, but the event is not dispatching, only complete_event appears when thi whole file is sent. So how can I determine the speed by bytes-sending process?

P.S. I can't use fileReference, cause I'm using my tests one-by-one and can't make users click in filebrowse() dialog.

Thx.

        public function startme( ):void {

             _startTime = ( new Date( ) ).getTime( );

        var req:URLRequest = new URLRequest();
        req.url = "http://smart.t3a.ru/speedtest/test.php";
        req.contentType = 'application/octet-stream';
        req.method = URLRequestMethod.POST;
        req.data = Obj;

        //req.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );

        _loader = new URLLoader();
        _loader.dataFormat = URLLoaderDataFormat.BINARY;
        _loader.addEventListener(Event.COMPLETE,uploadComplete);
        _loader.addEventListener(ProgressEvent.PROGRESS, uploadProgress );
        _loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onstatus);
        _loader.addEventListener(IOErrorEvent.IO_ERROR, eIOError);
        _loader.load(req);


    }

    private function onstatus(event:HTTPStatusEvent):void
    {
        trace(event);
    }
    private function eIOError(event:IOErrorEvent):void
    {
        trace(event);
    }

    private function uploadComplete( e:Event ):void
    {
        var endTime:Number = ( new Date( ) ).getTime( );
        var totalTime:Number = ( endTime - _startTime ) / 1000;

        var totalKB:Number = Obj.length * 8 / 1024;
        //_speed = totalKB / totalTime;

        if (debug)
        {
            trace( "U total time: " + totalTime + " total KB: " + totalKB + " speed: " + speed + "KBps" );
        }

        dispatchEvent( e );
    }

    private function uploadProgress( e:ProgressEvent ):void
    {
        var endTime:Number = ( new Date( ) ).getTime( );
        var totalTime:Number = ( endTime - _startTime ) / 1000;
        if (totalTime > 1)
        {
            var totalKB:Number = e.bytesLoaded * 8 / 1024;
            _speed = totalKB / totalTime;

            if (debug)
            {
                trace( "u total time: " + totalTime + " total KB: " + totalKB + " speed: " + speed + "KBps" );
            }
            dispatchEvent( e );

        }
        if (totalTime > 7)
        {

            var ec:Event = new Event(Event.COMPLETE);
            _loader.dispatchEvent(ec);
            _loader.close();
        }
    }

UPD My php script:

    <?
    $postdata = file_get_contents("php://input");
    file_put_contents("inp.bin",var_export($postdata,true));
    echo "Done";
    ?>

Yes it's small, but i realy don't need the data, i need to check how fast is it uploaded.

UPD 2

So, after reading all the answers, comments and googling for hours, i probably got the solution, but it is made in some different way, than i wish at the beginning. Anyway TYVM all, i'll post the solution shortly.

like image 771
Den Avatar asked May 25 '26 12:05

Den


2 Answers

Try to add this, and see if you are getting an error. That might be the problem.

_loader.addEventListener("ioError", ldrError);    

function ldrError(evt:*):void
{
    trace("ERROR");
}

It could be that there is a problem in your php script.

like image 153
ThomasM Avatar answered May 30 '26 09:05

ThomasM


Instead of relying on the ProgressEvent firing, try adding an ENTER_FRAME listener and pull the bytesLoaded from the _loader instance. If the ENTER_FRAME works, you could then try replacing it with a repeating TimerEvent on a short delay to reduce the processing load of ENTER_FRAME.

I'm not able to test this right now but this should at least let you know if it's a problem with ProgressEvent not firing or the streaming itself.

like image 27
DNJohnson Avatar answered May 30 '26 10:05

DNJohnson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!