Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    65
    Thanks
    4
    Thanked 0 Times in 0 Posts

    How to effectively measure bandwidth used by a user

    Hello,

    I have a downloading website, where i measure the bandwidth used by every member.

    For example the member is downloading a 1GB file, i want to measure how amny MB the user has downloaded in.

    I'm currently using this code, it works but it creates huge load on the my server

    Code:
    	while (!feof($fp) && (connection_status()==0)) {
    		$recv = @stream_get_line($fp, 1048577);
    		@print $recv;
    		@flush();
    		@ob_flush();
    
    		$query = mysql_query("UPDATE userband SET today = today + 1  WHERE username='$username'"); 
    		$query = mysql_query("UPDATE userband SET month = month + 1 WHERE username='$username'"); 
    		$query = mysql_query("UPDATE userband SET total = total + 1 WHERE username='$username'");
    
    	}
    is there a more efficient way to do this? i keep track of bandwidth used in month, yeat and total. as you can see for every 1MB of chunk data 3 SQL statements are executed but the download happens fast so many many sql statements are executed in one second

    Advice is appreciated, thank you

  • #2
    New Coder
    Join Date
    Sep 2007
    Posts
    98
    Thanks
    25
    Thanked 4 Times in 4 Posts
    Might be easier if you just create a 'logs' table with fields like username:action:mb:stamp:url:uip

    After each action you could insert a record populating this - just make a function so you can perform it easily.
    InsertStat($username,"downloaded $filename",$mb,time(),$url,$uip);

    Then make a page which takes this data and spits it out on a page for you filtered user by user for the current month.

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,312
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    For a start, you're running 3 queries there all on the same table. Why don't you just set those 3 columns to their new values in one query?

    Also, you're doing it on every pass of the loop. Why not just do it at the end of the script execution? You don't need those numbers in real time do you? (and even if you did, updating 3 columns in one query is still 1/3 of the workload on the sql server).
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    New Coder
    Join Date
    Sep 2010
    Posts
    65
    Thanks
    4
    Thanked 0 Times in 0 Posts
    You are absolutely right in this, I don't know why i have done this. I have also though to add "LIMIT 1" at the end of the sql query to avoid it going through all of the records

    But i do want it in real time, but in case i don't want and the user terminates the download in middle how can i execute a php command when the downloader disconnect?

    Quote Originally Posted by tangoforce View Post
    For a start, you're running 3 queries there all on the same table. Why don't you just set those 3 columns to their new values in one query?

    Also, you're doing it on every pass of the loop. Why not just do it at the end of the script execution? You don't need those numbers in real time do you? (and even if you did, updating 3 columns in one query is still 1/3 of the workload on the sql server).

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,312
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    register_shutdown_function()

    That will allow you to run a function when the script ends, user disconnects etc.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    TheCracker (01-03-2013)

  • #6
    New Coder
    Join Date
    Sep 2010
    Posts
    65
    Thanks
    4
    Thanked 0 Times in 0 Posts
    i think that is what i'm looking for

    Thank you


  •  

    Posting Permissions

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