detect internet speed with php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yang Li Ke

    #1

    detect internet speed with php

    Hi guys,
    Is it possible to know the internet speed of the visitors with php?

    Thanx
    --
    Yang


  • Randy Webb

    #2
    Re: detect internet speed with php

    Yang Li Ke wrote:
    [color=blue]
    > Hi guys,
    > Is it possible to know the internet speed of the visitors with php?[/color]


    No. There are too many things that have to happen to try to determine
    the speed of the visitor. And most of those things, PHP has no access to.

    --
    Randy

    Comment

    • Chung Leong

      #3
      Re: detect internet speed with php

      Something like this might given you a sense of how fast the connection is:

      function microtime_diff( $a, $b) {
      list($a_dec, $a_sec) = explode(" ", $a);
      list($b_dec, $b_sec) = explode(" ", $b);
      return $b_sec - $a_sec + $b_dec - $a_dec;
      }

      function test_speed($tes t_size) {
      flush();
      $start_time = microtime();
      $comment = "<!--O-->";
      $len = strlen($comment );
      for($i = 0; $i < $test_size; $i += $len) {
      echo $comment;
      }
      flush();
      $duration = microtime_diff( $start_time, microtime());
      if($duration != 0) {
      return $test_size / $duration / 1024;
      }
      else {
      return log(0);
      }
      }

      $speed = test_speed(1024 );
      if($speed > 50) { // a fast connection, send more byte for more accuracy
      $speed = test_speed(1024 0);
      if($speed > 500) { // a really fast connection, send even more byte for
      more accuracy
      $speed = test_speed(1024 00);
      }
      }
      echo sprintf("Downlo ad speed is %0.3f kb/s", $speed);

      I have the code set up here: http://www.conradish.net/speed.php. On my cable
      connection I get around 300 kb/s, which is about right.

      Uzytkownik "Yang Li Ke" <yanglike@sympa tico.ca> napisal w wiadomosci
      news:v7fOb.2709 $XZ.403071@news 20.bellglobal.c om...[color=blue]
      > Hi guys,
      > Is it possible to know the internet speed of the visitors with php?
      >
      > Thanx
      > --
      > Yang
      >
      >[/color]


      Comment

      • CountScubula

        #4
        Re: detect internet speed with php

        "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
        news:_fqdnU6Pye ESf5TdRVn-sA@comcast.com. ..[color=blue]
        > Something like this might given you a sense of how fast the connection is:
        >
        > function microtime_diff( $a, $b) {
        > list($a_dec, $a_sec) = explode(" ", $a);
        > list($b_dec, $b_sec) = explode(" ", $b);
        > return $b_sec - $a_sec + $b_dec - $a_dec;
        > }
        >
        > function test_speed($tes t_size) {
        > flush();
        > $start_time = microtime();
        > $comment = "<!--O-->";
        > $len = strlen($comment );
        > for($i = 0; $i < $test_size; $i += $len) {
        > echo $comment;
        > }
        > flush();
        > $duration = microtime_diff( $start_time, microtime());
        > if($duration != 0) {
        > return $test_size / $duration / 1024;
        > }
        > else {
        > return log(0);
        > }
        > }
        >
        > $speed = test_speed(1024 );
        > if($speed > 50) { // a fast connection, send more byte for more accuracy
        > $speed = test_speed(1024 0);
        > if($speed > 500) { // a really fast connection, send even more byte for
        > more accuracy
        > $speed = test_speed(1024 00);
        > }
        > }
        > echo sprintf("Downlo ad speed is %0.3f kb/s", $speed);
        >
        > I have the code set up here: http://www.conradish.net/speed.php. On my[/color]
        cable[color=blue]
        > connection I get around 300 kb/s, which is about right.
        >
        > Uzytkownik "Yang Li Ke" <yanglike@sympa tico.ca> napisal w wiadomosci
        > news:v7fOb.2709 $XZ.403071@news 20.bellglobal.c om...[color=green]
        > > Hi guys,
        > > Is it possible to know the internet speed of the visitors with php?
        > >
        > > Thanx
        > > --
        > > Yang
        > >
        > >[/color]
        >
        >[/color]

        Interesting concept,

        when I test your link, I average 240, but I am on 3 Mbit

        when I run it localy with my test server, GigaBit from my desktop to 1
        switch to server, I get 190

        hmm, I think it is rather testing execution speed.


        --
        Mike Bradley
        http://www.gzentools.com -- free online php tools


        Comment

        • Chung Leong

          #5
          Re: detect internet speed with php

          I wonder. According to the PHP manual, flush() is supposed to flush the
          server's buffer as well. Presumably the function wouldn't return until it
          has received an acknowledgement for the last packet sent.

          I tried adding ob_start("ob_gz handler") to the beginning of the file. The
          measured speed increased nearly ten-fold. So I don't think it's testing the
          execution speed.

          Uzytkownik "CountScubu la" <me@scantek.hot mail.com> napisal w wiadomosci
          news:UzmOb.1226 6$eI6.3040@news svr25.news.prod igy.com...[color=blue]
          > Interesting concept,
          >
          > when I test your link, I average 240, but I am on 3 Mbit
          >
          > when I run it localy with my test server, GigaBit from my desktop to 1
          > switch to server, I get 190
          >
          > hmm, I think it is rather testing execution speed.
          >
          >
          > --
          > Mike Bradley
          > http://www.gzentools.com -- free online php tools
          >
          >[/color]


          Comment

          • CountScubula

            #6
            Re: detect internet speed with php

            "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
            news:NJGdnWOAdK l-sZfdRVn-vg@comcast.com. ..[color=blue]
            > I wonder. According to the PHP manual, flush() is supposed to flush the
            > server's buffer as well. Presumably the function wouldn't return until it
            > has received an acknowledgement for the last packet sent.
            >
            > I tried adding ob_start("ob_gz handler") to the beginning of the file. The
            > measured speed increased nearly ten-fold. So I don't think it's testing[/color]
            the[color=blue]
            > execution speed.
            >[/color]


            Hmm, here is an idea, havent done any code, I am going to sleep, diving in
            the morning.

            what if you were to send a header with a timestamp:

            fist call to page:
            Location:
            http://www.blablabla.com/test.php?action=loop&step=1&ts[1]=microtime

            next call to page
            Location:
            http://www.blablabla.com/test.php?action=loop&step=2&ts[1]=microtime&ts[2]=m
            icrotime

            loop a couple times of times, average out the time stamps for an average
            page redirct.

            this will be used a reference to delays, overhead etc...

            then do this header
            Location: http://www.blablabla.com/test.php?ac...avg=_avg_stamp

            then for that page, send a lot of header data:
            for ($i=0; $i < 100; $i++)
            {
            $head = "X-SpeedTest: --- speed test $i---";

            $headerSize += strlen($head) + 2;
            header($head);
            }

            header("Locatio n:

            $headerSize


            then the last call to the page:

            $timeElapsed = (microtime() - $_GET['ts']) - $_GET['avg'];
            $dataSize = $_GET['ds'];

            hmm, then I think we would have a size of data sent, and time to send?

            just a thought.....Wha t do you think?



            --
            Mike Bradley
            http://www.gzentools.com -- free online php tools


            Comment

            • CountScubula

              #7
              Re: detect internet speed with php

              Ok, I just got back, and tried to code it, let me know if this is any good.

              you can test it here:



              and the source is here:



              --
              Mike Bradley
              http://www.gzentools.com -- free online php tools

              "CountScubu la" <me@scantek.hot mail.com> wrote in message
              news:ZxrOb.1233 1$0s1.2109@news svr25.news.prod igy.com...[color=blue]
              > "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
              > news:NJGdnWOAdK l-sZfdRVn-vg@comcast.com. ..[color=green]
              > > I wonder. According to the PHP manual, flush() is supposed to flush the
              > > server's buffer as well. Presumably the function wouldn't return until[/color][/color]
              it[color=blue][color=green]
              > > has received an acknowledgement for the last packet sent.
              > >
              > > I tried adding ob_start("ob_gz handler") to the beginning of the file.[/color][/color]
              The[color=blue][color=green]
              > > measured speed increased nearly ten-fold. So I don't think it's testing[/color]
              > the[color=green]
              > > execution speed.
              > >[/color]
              >
              >
              > Hmm, here is an idea, havent done any code, I am going to sleep, diving in
              > the morning.
              >
              > what if you were to send a header with a timestamp:
              >
              > fist call to page:
              > Location:
              > http://www.blablabla.com/test.php?action=loop&step=1&ts[1]=microtime
              >
              > next call to page
              > Location:
              >[/color]
              http://www.blablabla.com/test.php?action=loop&step=2&ts[1]=microtime&ts[2]=m[color=blue]
              > icrotime
              >
              > loop a couple times of times, average out the time stamps for an average
              > page redirct.
              >
              > this will be used a reference to delays, overhead etc...
              >
              > then do this header
              > Location: http://www.blablabla.com/test.php?ac...avg=_avg_stamp
              >
              > then for that page, send a lot of header data:
              > for ($i=0; $i < 100; $i++)
              > {
              > $head = "X-SpeedTest: --- speed test $i---";
              >
              > $headerSize += strlen($head) + 2;
              > header($head);
              > }
              >
              > header("Locatio n:
              >[/color]
              http://www.blablabla.com/test.php?ac...=mictotime&ds=[color=blue]
              > $headerSize
              >
              >
              > then the last call to the page:
              >
              > $timeElapsed = (microtime() - $_GET['ts']) - $_GET['avg'];
              > $dataSize = $_GET['ds'];
              >
              > hmm, then I think we would have a size of data sent, and time to send?
              >
              > just a thought.....Wha t do you think?
              >
              >
              >
              > --
              > Mike Bradley
              > http://www.gzentools.com -- free online php tools
              >
              >[/color]


              Comment

              • Chung Leong

                #8
                Re: detect internet speed with php

                Hmmm, using the header might be a better idea than sending the test bytes as
                HTML comment, as it seems the browser would save the comment tags into the
                DOM (thus eat up memory). A bit cleaner too to redirect to a separate page.

                Tried your page. It's reporting that my 3 mbit connection is capable of
                downloading 1.8 k per second. Perhaps a larger amount of test data is
                needed?

                Uzytkownik "CountScubu la" <me@scantek.hot mail.com> napisal w wiadomosci
                news:ZxrOb.1233 1$0s1.2109@news svr25.news.prod igy.com...[color=blue]
                > "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
                > news:NJGdnWOAdK l-sZfdRVn-vg@comcast.com. ..[color=green]
                > > I wonder. According to the PHP manual, flush() is supposed to flush the
                > > server's buffer as well. Presumably the function wouldn't return until[/color][/color]
                it[color=blue][color=green]
                > > has received an acknowledgement for the last packet sent.
                > >
                > > I tried adding ob_start("ob_gz handler") to the beginning of the file.[/color][/color]
                The[color=blue][color=green]
                > > measured speed increased nearly ten-fold. So I don't think it's testing[/color]
                > the[color=green]
                > > execution speed.
                > >[/color]
                >
                >
                > Hmm, here is an idea, havent done any code, I am going to sleep, diving in
                > the morning.
                >
                > what if you were to send a header with a timestamp:
                >
                > fist call to page:
                > Location:
                > http://www.blablabla.com/test.php?action=loop&step=1&ts[1]=microtime
                >
                > next call to page
                > Location:
                >[/color]
                http://www.blablabla.com/test.php?action=loop&step=2&ts[1]=microtime&ts[2]=m[color=blue]
                > icrotime
                >
                > loop a couple times of times, average out the time stamps for an average
                > page redirct.
                >
                > this will be used a reference to delays, overhead etc...
                >
                > then do this header
                > Location: http://www.blablabla.com/test.php?ac...avg=_avg_stamp
                >
                > then for that page, send a lot of header data:
                > for ($i=0; $i < 100; $i++)
                > {
                > $head = "X-SpeedTest: --- speed test $i---";
                >
                > $headerSize += strlen($head) + 2;
                > header($head);
                > }
                >
                > header("Locatio n:
                >[/color]
                http://www.blablabla.com/test.php?ac...=mictotime&ds=[color=blue]
                > $headerSize
                >
                >
                > then the last call to the page:
                >
                > $timeElapsed = (microtime() - $_GET['ts']) - $_GET['avg'];
                > $dataSize = $_GET['ds'];
                >
                > hmm, then I think we would have a size of data sent, and time to send?
                >
                > just a thought.....Wha t do you think?
                >
                >
                >
                > --
                > Mike Bradley
                > http://www.gzentools.com -- free online php tools
                >
                >[/color]


                Comment

                • CountScubula

                  #9
                  Re: detect internet speed with php

                  Ok, now try it at:



                  I think it may be off by a factor of 9 to 10, I arrive at this nubmer by
                  doing a local test, and it shows my DL speed as 5 Mbit/s, and when I do a
                  ftp download of a large file from the same server, I get about 51 Mbit/s

                  let me know what everyone thinks, I think we can make this happen!

                  --
                  Mike Bradley
                  http://www.gzentools.com -- free online php tools

                  "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
                  news:KoWdnQNVoe up_pHdRVn-sQ@comcast.com. ..[color=blue]
                  > Hmmm, using the header might be a better idea than sending the test bytes[/color]
                  as[color=blue]
                  > HTML comment, as it seems the browser would save the comment tags into the
                  > DOM (thus eat up memory). A bit cleaner too to redirect to a separate[/color]
                  page.[color=blue]
                  >
                  > Tried your page. It's reporting that my 3 mbit connection is capable of
                  > downloading 1.8 k per second. Perhaps a larger amount of test data is
                  > needed?
                  >[/color]



                  Comment

                  • Larry Jaques

                    #10
                    Re: detect internet speed with php

                    On Mon, 19 Jan 2004 23:30:16 GMT, "CountScubu la"
                    <me@scantek.hot mail.com> brought forth from the murky depths:
                    [color=blue]
                    >Ok, now try it at:
                    >
                    >http://www.gzentools.com/test/speedtest.php[/color]

                    I get "too many" redirection errors with NN7, Mike.

                    "Your line speed is around 11.788 Mbit/s" for Stargate
                    sat modem via IE6.

                    -
                    Every day above ground is a Good Day(tm).
                    -----------
                    http://diversify.com Website Application Programming

                    Comment

                    • CountScubula

                      #11
                      Re: detect internet speed with php

                      "Larry Jaques" <novalidaddress @di\/ersify.com> wrote in message
                      news:n2pr00lumg cfva0v4nj7jbi86 953mbn8to@4ax.c om...[color=blue]
                      > On Mon, 19 Jan 2004 23:30:16 GMT, "CountScubu la"
                      > <me@scantek.hot mail.com> brought forth from the murky depths:
                      >[color=green]
                      > >Ok, now try it at:
                      > >
                      > >http://www.gzentools.com/test/speedtest.php[/color]
                      >
                      > I get "too many" redirection errors with NN7, Mike.
                      >
                      > "Your line speed is around 11.788 Mbit/s" for Stargate
                      > sat modem via IE6.
                      >
                      > -
                      > Every day above ground is a Good Day(tm).
                      > -----------
                      > http://diversify.com Website Application Programming[/color]



                      OK, I changed the redirects to a lower number, how is it now?

                      btw, was 11.788 to high or too low?





                      --
                      Mike Bradley
                      http://www.gzentools.com -- free online php tools


                      Comment

                      • Larry Jaques

                        #12
                        Re: detect internet speed with php

                        On Wed, 21 Jan 2004 02:44:03 GMT, "CountScubu la"
                        <me@scantek.hot mail.com> brought forth from the murky depths:
                        [color=blue]
                        >OK, I changed the redirects to a lower number, how is it now?[/color]

                        NN works now: Your line speed is around 933.794 Kbit/s
                        Your line speed is around 1.030 Mbit/s
                        Your line speed is around 7.401 Mbit/s
                        Your line speed is around 1.062 Mbit/s


                        IE: Your line speed is around 2.025 Mbit/s
                        Your line speed is around 447.926 Kbit/s
                        Your line speed is around 2.340 Mbit/s
                        Your line speed is around 754.559 Kbit/s
                        [color=blue]
                        >btw, was 11.788 to high or too low?[/color]

                        Good question. Satellites connections are iffy. Sometimes it
                        takes half an hour to upload 40 files for a site where it
                        would have taken 5 minutes via my 56k modem. Sometimes I get
                        2MB download speeds, other times 7MB speeds. The variations
                        in your program are probably due to the sat latencies.

                        Are others with true hardwired connections getting more closely
                        matched results, or widely varying results like mine?


                        ----------------------------------------------------------------
                        "Let's sing praise to Aphrodite || www.diversify.com
                        She may seem a little flighty, || Full Service Websites
                        but she wears a green gauze nighty, || PHP Applications
                        And she's good enough for me." || SQL Database Development

                        Comment

                        • CountScubula

                          #13
                          Re: detect internet speed with php

                          "Larry Jaques" <novalidaddress @di\/ersify.com> wrote in message
                          news:ianu00hm93 tldv37l87ueci3j b0n0pbbkp@4ax.c om...> On Wed, 21 Jan 2004
                          02:44:03 GMT, > >btw, was 11.788 to high or too low?[color=blue]
                          >
                          > Good question. Satellites connections are iffy. Sometimes it
                          > takes half an hour to upload 40 files for a site where it
                          > would have taken 5 minutes via my 56k modem. Sometimes I get
                          > 2MB download speeds, other times 7MB speeds. The variations
                          > in your program are probably due to the sat latencies.
                          >
                          > Are others with true hardwired connections getting more closely
                          > matched results, or widely varying results like mine?
                          >
                          >
                          > ----------------------------------------------------------------
                          > "Let's sing praise to Aphrodite || www.diversify.com[/color]


                          Hmm, I do not know, c'mon people post a little, please.

                          Btw, I did an ftp download localy, and tweeked script to match that. I
                          know, rudimentry baseline.


                          --
                          Mike Bradley
                          http://www.gzentools.com -- free online php tools


                          Comment

                          • Stoodder
                            New Member
                            • Jun 2006
                            • 1

                            #14
                            k so i know that this is pretty old but if i were you i would look into using javascript and implement ajax(to be able to load a page behind the scenes) my theory behiond this is you shoul dbe able to open a page in the background then test to see how fast it opened(which your able to do with the readystate of an XMLHttpRequestO bject)

                            I dunno i havent actually tried and it sjust an idea im tossing out there. because the way it seems you gusy are oging about it now is your testing the servers speed(how fast the server can compile your page) because the filesize of a page changes (for example in forums when more posts are added)

                            To branch off of the Ajax idea, heres how i think you could go about it:

                            at the end of your php script set $_SESSION["microtime_ 1"]=microtime(); then flush out your html

                            then load an image (in the HTML document)(make the file bigger in size) and say <img onLoad="runTime Test()">

                            in run test run an XMLHttpRequest that opens another php page behind the scenes to set $_SESSION["microtime_ 2"]=microtime();

                            and it will also say something like echo"speed = " . $approximateFil eSize/($_SESSION["microtime_ 2"] - $_SESSION["microtime_ 1"] );

                            $approximateFil eSize = (this will be the file size of the two pages and the image added togethor)

                            anyways, i knwo this isnt really that clear and i probobly jumped around alot but i think this might work fo ryou. anyways hope someonee will give this a try(im too lazy lol) that is of course if anyone else is even interested still

                            Comment

                            Working...