SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member
    Join Date
    Feb 2001
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript & Unexpected Results

    Sorry for the vague title to the thread, but I couldnt think of a simple way of putting the problem that I have come up agaisnt.

    I have been tasked with putting together a simple bandwidth test tool that can run through a browser with the least amount of user interaction. To do this I have created a javascript that downloads an image, times this download, reloads the page passing the values from the previous test - my current script iterates ten times.

    The problem that I have encountered is that when I view the summary results at the end of the test run, alot of the timings are either "infinite" or "0" and these are randomly placed in the results, i.e. if I re-run the script I will get infinite or 0 on different sets.

    The code I am using is as follows :

    Code:
    <!--
    //set vars
    var Request = new QSHandler();
    var imageBase = Request.QueryString("img");
    var recurCount = Request.QueryString("cnt");
    var filesize = Request.QueryString("fls");
    var timings = Request.QueryString("tim"); 
    var throughputs = Request.QueryString("thr"); 
    var imageName = imageBase + recurCount + ".bmp";
    var startTime;
    var endTime; 
    	
    //display image & start timer
    if (recurCount != 11)
    {  
     document.write("<center><img height=150 width=150 onload=endTimer() src="+imageName+"></center>");
     document.write("<center><font face=verdana size=2>"+imageName+" / " + filesize + " bytes</center>");
     startTime = new Date();
    }
    else
    {
     document.location = "downloadrecurse_summary.htm?&img=" + imageBase +"&thr=" + throughputs + "&tim=" + timings;
    }
    //set functions
    function endTimer()
    {
     var endTime = new Date();
     var totalTime = ( endTime.getTime() - startTime.getTime() ) / 1000; //seconds
     var throughput = Math.round(((filesize / totalTime)  / 1024) * 100)/100; //kbytes/sec
     //see http://javascriptkit.com/javatutors/round.shtml for rounding description
     if (recurCount == 1)
     {
      timings = totalTime;
      throughputs = throughput;
     }
     else
     {
      timings += ":" + totalTime;
      throughputs += ":" + throughput;
     }
      
     recurCount++;
     document.location = "downloadrecurse.htm?img=" + imageBase + "&fls=" + filesize + "&cnt=" + recurCount + "&thr=" + throughputs + "&tim=" + timings;
    }
    function QSHandler()
    {
     var qs = location.search.substr(1).split("&");
     this.data = [];
     for(var i = 0; i < qs.length; i++) this.data[qs[i].split("=")[0]] = qs[i].split("=")[1];
     
     this.QueryString = function(x)
     {
      return this.data[x];
     };
    }
    // -->
    When the script gets to the 11 iteration it dives out to another page that simply displays the results of the test run, for the sake of post size I havent included the code here as all it is is a simple script that takes the command line args splits them up by ":" and pumps the results into a table.

    A typical results table looks like this :

    Run / Time (secs) / throughput (kbs)
    Run 1 - 0 - Infinity
    Run 2 - 0 - Infinity
    Run 3 - 0.016 - 612.18
    Run 4 - 0 - Infinity
    ........

    As somebody who has about 24 hours experience in Javascript I am sure that I am missing something here that is blatently obvious to most if not all, I wont admit that I have failed, rather acknowledge that I have found a 1000 ways that it doesnt work :-)

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's my version of a javascript bandwidth test. You'll have to clear your cache to run it more than once, though.

    http://www.peterbailey.net/test/bandwidth.htm
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    Non-Member
    Join Date
    Feb 2001
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mind if I "steal" your sources and play around with them.

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Go right ahead. Copyright notices are included where I wish them to be [img]images/smilies/biggrin.gif[/img]

    Oh, and I changed to the cache header on the image, so clearing your cache isn't necessary to re-run the test.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    Non-Member
    Join Date
    Feb 2001
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    Go right ahead. Copyright notices are included where I wish them to be [img]images/smilies/biggrin.gif[/img]

    Oh, and I changed to the cache header on the image, so clearing your cache isn't necessary to re-run the test.
    Thanks alot, I will obviously leave all the copyright info etc intact.

    Using your code and variations of mine I have actually got this thing working it still reports infinity & 0 from time to time but no where near as often as the orignal version.

    One final thing that I am curious about is the image file you use in your code :

    http://www.peterbailey.net/test/image.php

    You mentioned that you have changed the cache header on the image to prevent cache issues, how did you achieve this? and is it possible to do without php?

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not possible without PHP or some other server-side language - they are the only way to send additional headers.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •