Gzip using all of my CPU?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm compressing all of my PHP scripts using gzip. To my knowledge,
it's working correctly, although I'm having a significant problem with
my site running slow during peak hours.

In looking for the leak, I found this in my CPU Usage list (in Web
Host Manager):

Top Process %CPU 92.8 gzip -f -

It's over 90% almost every day since I began using compression. Does
this mean that I'm not compressing correctly, or what?

Each page has 3 files: header, footer, and the main page. The header
and footer is included on the main page via "include". The file
header.php has this at the top:

// everything else

And then the last line of the footer is:


if (headers_sent()) $encoding = false;
elseif (strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false) $encoding =
elseif (strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false) $encoding =
else $encoding = false;

if ($encoding) {
  $contents = ob_get_contents();
  header('Content-Encoding: '.$encoding);
  $size = strlen($contents);
  $contents = gzcompress($contents, 9);
  $contents = substr($contents, 0, $size);

else ob_end_flush();

Re: Gzip using all of my CPU?

Quoted text here. Click to load it

While we're on the subject, is the compression code I posted earlier
better, worse, or the same as simply putting ob_start("ob_gzhandler");
at the top?

Re: Gzip using all of my CPU?

Jason Carlton wrote:
Quoted text here. Click to load it

You shouldn't need to use the ob_xxx() functions - they are placing an
additional load on your server and can hide real problems in your code.
  Additionally, your web server can do compression much more efficiently
than your PHP code.

Gzipping your code like this will lower your bandwidth requirements, but
will require more cpu (but much less than the web server will do).

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Gzip using all of my CPU?

.oO(Jerry Stuckle)

Quoted text here. Click to load it

In addition: Where is $HTTP_ACCEPT_ENCODING coming from? Is it what I
think it is?


Re: Gzip using all of my CPU?

El 30/01/2010 9:16, Jason Carlton escribió:
Quoted text here. Click to load it

A few remarks:

- Compression saves bandwidth and transfer time, not CPU or memory. You
invest on a resource to save another one.

- A 93% usage is not necessarily a problem. It's not 100% and it doesn't
really say if there're delays due to lack of resources. You should pay
more attention to the figures of server load, which indicate the number
of queued processes.

- Make you sure aren't blinding gzipping everything. There's no gain in
compressing pictures and similar stuff.

- If you use PHP to compress static files (CSS, JS...) you might be
getting the standard do-not-cache-this headers that are the default for
dynamic pages. Check that: you don't want to compress the same style
sheet 20 times per visit.

-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com

Re: Gzip using all of my CPU?

Álvaro G. Vicario wrote:
Quoted text here. Click to load it

Also 93% is a good thing. Why not use everything you have and then set
the priority (nice) on how importand it is for you.
I would rather ask why it ONLY uses 93% and not more. The answer most
likely is that the 93% is an average and between files it goes back to

Reminds me of people who say that program X uses so much memory where
that is a GOOD thing. Please use the memory as it is bought and payed

houghi        http://houghi.org http://www.plainfaqs.org/linux /
Quoted text here. Click to load it

Re: Gzip using all of my CPU?

Quoted text here. Click to load it

No you aren't - you appear to be conmpresing the output of your

Quoted text here. Click to load it

If PHP is having to invoke an external process to handle the
compression, then its not surprising.

Quoted text here. Click to load it

The right way to do it is to use mod_deflate / mod_compress on the
webserver - this works as an in-process library - no forking/startup

IME, using mod_gzip improvse performance significantly by offloading
work from the server faster. The net result was a decrease in load
with no discernible impact on CPU usage.

However it may be that you don't have access to reconfigure your
webserver - did you check that it would not compress content before
implementing your fix?

Maybe it really is just overloaded. CPU usage is an indicator of
performance but not an accurate measure. Load is better - whats
happenned to the load since you implemented this? What's happenend to
your php generation times since you implemented this? Presumably you
had some objective in mind by doing this - did you test the throughput
of your site before and after?

Quoted text here. Click to load it

You really should disable register_globals.

Quoted text here. Click to load it

Have you tried using the ob_gzhandler intead of gzcompress? Or


Site Timeline