Page 1 of 1

Bandwidth/compression versus server load, when and why?

Posted: Mon Jan 19, 2009 12:32 am
by JAB Creations
Compressing anything puts some amount of load on the server so my main question is what is the range (going from modest to minimal) that using ob_gzhandler starts taxing the server more then it's worth to save bandwidth for both the client and the server?

Here is an example of the file that handles the AJAX message requests for the chat room I've been working on. I've tested some results so first the code, results, then my own comments...

Code: Select all

<?php
session_name("member");
session_set_cookie_params(0);
session_start();
 
function user_date_mysql($date, $format) {return date ($format, strtotime($date));}
 
if (isset($_SESSION['member']))
{
 include("_0_header_02_mysql.php");
 mysql_query("SET time_zone = '-5:00';");
 
 if (isset($_GET['id']))
 {
  $id = mysql_real_escape_string($_GET['id']);;
 
  $query1 = "SELECT * FROM (SELECT ch.id, ch.comments, ch.date, ua.user_username FROM chat AS ch LEFT JOIN user_accounts AS ua ON (ch.id_member = ua.user_id) ORDER BY ch.id DESC LIMIT 0, 10) AS b WHERE b.id>'".$id."' ORDER BY b.id ASC";
  $result1 = mysql_query($query1);
 
  if (mysql_num_rows($result1) > '0')
  {
   ob_start("ob_gzhandler");
   while($row1 = mysql_fetch_assoc($result1))
   {
    echo $row1['id'].'::<span class="color1">'.$row1['user_username'].'</span> <span class="color5">(</span>'.user_date_mysql($row1['date'], "l h:i:sA").'<span class="color5">): </span>'.$row1['comments'].';;';
   }
   ob_end_flush();
  }
 }
 else
 {
  $result2 = mysql_query("SELECT * FROM (SELECT ch.id, ch.comments, ch.date, ua.user_username FROM chat AS ch LEFT JOIN user_accounts AS ua ON (ch.id_member = ua.user_id) ORDER BY ch.id DESC LIMIT 0, 10) AS b ORDER BY b.id ASC");
  $row2 = mysql_fetch_assoc($result2);
  //ob_start("ob_gzhandler");
  while($row2 = mysql_fetch_assoc($result2))
  {
   echo $row2['id'].'::<span class="color1">'.$row2['user_username'].'</span> <span class="color5">(</span>'.user_date_mysql($row2['date'], "l F jS, Y, h:iA").'<span class="color5">): </span>'.$row2['comments'].';;';
  }
  //ob_end_flush();
 };
}
?>
The first half of the file handles messages almost real-time. If there are no new messages no bandwidth is burned unless I add the compression in which case it will unnecessarily burn 26 bytes. The second part of the file handles the initial 10 messages when you first enter the chat room (so you can see a little bit of the conversation that happened before you entered the chat room).

Example results..
1.) First half no compression with new message: 120 bytes.
2.) First half with compression with new message: 99 bytes.
3.) Second half no compression loading 10 messages: 1237 bytes.
4.) Second half with compression loading 10 messages: 212 bytes.

One thing to keep in consideration is that the first half of this file can handle any given number of simultaneous messages so for example if there were a hundred people who simultaneously entered a message (however unlikely) it would be possible to have this file then read 100 messages from the database in a single read.

Any way so ultimately I'm curious as to roughly the range of where compressing client output becomes worth the server load. I think dialup at 36K (I never got more then 4.7KB sustained which is about 37.6K) wouldn't matter with these transfers. I'm going to pretty much ignore anyone who starts talking about jQuery and the likes, this is all made by me and is intended to be lightweight and non-third party dependent with support (size/compression) for folks on dialup. It's an aim above the mark to hit the mark sort of thing for me. :)

So let me reiterate my question, with all the variables roughly at what size does compressing the client output create an undesirable amount of load on the server?