View previous topic :: View next topic |
Author |
Message |
hinrichs Smarty Rookie
Joined: 24 Apr 2003 Posts: 28 Location: San Francisco, CA
|
Posted: Fri Apr 25, 2003 4:02 pm Post subject: $smarty ->display(tpl,cacheid,compress) |
|
|
Before caching the html, or maybe before rendering a noncached page (not sure about overhead there), it would be very cool if smarty could run a simple ereg that would compress the html in the template for output. This can greatly cut down on the bandwidth needed to serve pages on a popular site, especially these days when site pages are so table-heavy. It is great when developers can make pages (templates) without having to worry about adding extra spaces, tabs, etc. to make it easier to maitain, in its effect on bandwidth. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Apr 25, 2003 9:24 pm Post subject: |
|
|
That is not a function of Smarty. If you want to compress the output, use fetch() and then compress it in your application. The best solution IMHO is to use mod_gzip with Apache. This is 100% transparent to your applications, and this will cover static content as well as dynamic (PHP, Perl, etc.), it will handle quirkyness of buggy/old browsers, it will cache .gz files, it is extremely efficient since it is done at the apache layer, etc.
Monte
Last edited by mohrt on Fri Apr 25, 2003 9:38 pm; edited 2 times in total |
|
Back to top |
|
toma Smarty Regular
Joined: 25 Apr 2003 Posts: 62
|
|
Back to top |
|
hinrichs Smarty Rookie
Joined: 24 Apr 2003 Posts: 28 Location: San Francisco, CA
|
Posted: Fri Apr 25, 2003 10:57 pm Post subject: |
|
|
The documentation on this page states "Also note that using ini.zlib.output_compression is preferred over ob_gzhandler()."
I have not yet looked into doing this on the apache level (which sounds attractive). But how do I test to see if it is working? When I add the ob_gzhandler line or set this transparent compression value in the php.ini, then access the page, everything looks exactly the same as before. If compression is working, this is probably good. However I can't find any suggestions on how to test to see if it is working and how to test the results. Also I am concerned about side-effects, such as whether the browser will need to spend time "decompressing", and compatibility among browser types. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Apr 25, 2003 11:20 pm Post subject: |
|
|
yeah, it is compression over the wire, so the browser has to be able to accept gzip--which almost all do. I'm not sure what benefit you'd get out of compression without a decompression stage at the client. Otherwise it seems rather gratuitus (and costly) to compress a file for storage only to uncompress it prior to transmission. |
|
Back to top |
|
hinrichs Smarty Rookie
Joined: 24 Apr 2003 Posts: 28 Location: San Francisco, CA
|
Posted: Sat Apr 26, 2003 12:05 am Post subject: |
|
|
Thanks for your reply.
It looks from the documentation that php 4.3 can allow one to change the ini zlib settings at the page level, and that using the zlib.output_compression is the recommended method.
But, again, does anyone out there know how I can examine the results of turning this on? I cannot even tell if it is working or not. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sat Apr 26, 2003 12:11 am Post subject: |
|
|
i'd use "ngrep '' port 80" and open the page with a browser. if i can read the stuff that is dumped to the console, then zlib-compression was definately off.
ngrep can be found at http://www.packetfactory.net/Projects/ngrep/ but i'm sure that there are "smarter" ways to analyze this. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Apr 26, 2003 12:36 am Post subject: |
|
|
This doesn't require ngrep.
Use telnet:
C:\>telnet localhost 80
Use localhost or whatever name (or address) of the host you want to test.
NOTE: you get a blank screen... your keypresses won't be echoed, so don't make any spelling errrs
Now type the following, changing it to match the path to your page:
get /yoursite/yourfile.php
Hit enter and you should see scrambled toast if gzip is on. That's because telnet doesn't support gzip. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sat Apr 26, 2003 12:45 am Post subject: |
|
|
hmm. first of all you should prefer nc over telnet wherever possible, of course
second: http is simple, but not that simple. you have to pretend that you accept gzip-compressed encoding via the correct headers. otherwise it would only be transparent to the clients that know it at all. that wouldn't be backwards compatible. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Apr 26, 2003 1:23 am Post subject: |
|
|
Nothing escapes your eagle eye, messju!
Sure telnet may be the bad girl in your toolbox, but sometimes, when onsite, she can be your only friend.
In my last example, once you are connected to your site, instead of typing the get, modify the following as appropriate, paste it into the pastebuffer, right-click in the command window (to paste) and hit enter.
Code: | get /yoursite/yourfile.php HTTP/1.1
Accept: */*
Accept-Language: en-us
Host: localhost
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
Accept-Encoding: gzip |
The output will look something like:
Quote: | HTTP/1.1 200 OK
Date: Sat, 26 Apr 2003 01:21:08 GMT
Server: Apache/1.3.27 (Win32) PHP/4.3.0
X-Powered-By: PHP/4.3.0
Content-Encoding: gzip
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html
276
▼♥∞T█j█@►}_╚?êàB☻i$ ...... |
Notice the Content-Encoding in the returned header and that the data returned is assuredly non text. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Apr 26, 2003 1:30 am Post subject: |
|
|
oh yeah, I was intending this mainly for windows (C:/>) which has a dearth of tools available
edit:
I should note that PHP 4.3+ allows you to read the headers of an open stream, so you could write a cli script to do this, too. Hmm.
update: just tested PHP 4.3 and stream_get_meta_data() doesn't return all headers. Oh well. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sat Apr 26, 2003 10:29 am Post subject: |
|
|
thats what i did some time ago for another purpose:
(i just added the accept-encoding-header)
Code: |
<?php
$url='http://test.com/test';
$u = parse_url($url);
if (!isset($u['port'])) $u['port']=80;
if (!isset($u['path'])) $u['path'] = '/';
if (isset($u['query'])) $u['path'] .= '?' . $u['query'];
if ($s=fsockopen($u['host'],$u['port'])) {
fputs($s, "GET ".$u['path']." HTTP/1.1\r\n");
fputs($s, "Host: ".$u['host']."\r\n");
fputs($s, "Accept-Encoding: gzip\r\n");
fputs($s, "Connection: close\r\n");
fputs($s, "\r\n");
fflush($s);
// read headers
while (false!==($line=fgets($s, 65536))) {
$line = chop($line);
if (strlen($line)==0) break;
echo $line, "\n";
}
// read body here
// ,,,,
fclose($s);
} // else connection refused
?>
|
this is very similar to your suggestion via telnet of course |
|
Back to top |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Sat Apr 26, 2003 12:22 pm Post subject: |
|
|
unfortunately, ob_gzhandler is broken in 4.3.1 :/ |
|
Back to top |
|
dthought Smarty Regular
Joined: 21 Apr 2003 Posts: 55 Location: Melbourne, Australia
|
Posted: Sun Apr 27, 2003 10:30 am Post subject: Re: $smarty ->display(tpl,cacheid,compress) |
|
|
hinrichs wrote: | It is great when developers can make pages (templates) without having to worry about adding extra spaces, tabs, etc. to make it easier to maitain, in its effect on bandwidth. |
That sounds like you also want to strip whitespace before you send the page... |
|
Back to top |
|
hinrichs Smarty Rookie
Joined: 24 Apr 2003 Posts: 28 Location: San Francisco, CA
|
Posted: Mon Apr 28, 2003 4:53 pm Post subject: stripping whitespace |
|
|
This was actually what I was originally talking about, as I was unaware that the gzip option was an option whose time has come--I thought it might alienate some users. Sounds however like it has developed to the point it will work for almost everyone.
I did discover the trimwhitespace output filter. This is very cool and a no-brainer for cached pages. I just put this in my smarty include:
Code: | if ($GLOBALS['COMPRESS']){
$this->load_filter('output','trimwhitespace');
}
|
However if the page happens not to be cached, I suppose you might need to weigh the overhead of trimming the whitespace from the page versus the overhead of sending the original whitespace over the network. This seems like the realm of black art and I'm not sure I want to embark on such tests. |
|
Back to top |
|
|