According to the concepts that I expressed in the previous section, the second benchmarking example that I plan to develop will be similar to the first one. However, the main difference will involve displaying the respective database rows using HTTP compression. This means that if everything goes as expected, there should be a small reduction in the time it takes to display these rows on the browser. Having explained how the benchmarking test in question will be performed, what I’ll do next is define an additional class, called “DataCompressor,” that will be tasked with compressing any data inputted into its constructor via the GZIP algorithm. That said, here is the signature for this brand new class: // define 'DataCompressor' class class DataCompressor{ private $data; public function __construct($data){ if(!is_string($data)){ throw new Exception('Data must be a string!'); } $this->data=$data; } public function compressData(){ // remove white spaces from (X)HTML code $this->data=preg_replace(array("/r/","/n/"),'',$this->data); // check if browser supports gzip encoding if(strstr($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')){ // start output buffer ob_start(); // echo page contents to output buffer echo $this->data; // compress data with gzip $this->data=gzencode(ob_get_clean(),9); // send content encoding http header header('Content-Encoding: gzip'); } return $this->data; } } As shown above, the “DataCompressor” class compresses any string passed as an argument to the respective constructor via its “compressData()” method and returns this data to calling code. As you’ll realize, the logic implemented by this class is pretty easy to grasp. Therefore, pay attention to the following example, which fetches the same ten rows from the previous “USERS” MySQL database table and displays them on the browser, but this time using HTTP compression: // example using HTTP compression try{ // instantiate 'Timer' class $timer=new Timer(); // start timer $timer->start(); // connect to MySQL $db=new MySQL(array $result=$db->query('SELECT * FROM users'); $data=''; while($row=$result->fetchRow()){ $data.='ID: '.$row['id'].' Name: '.$row['name'].' Email: '.$row['email'].'<br />'; } $data.='Time spent in fetching database rows was '.$timer->stop().' seconds'; // use 'DataCompressor' object $dataComp=new DataCompressor($data); echo $dataComp->compressData(); } catch(Exception $e){ echo $e->getMessage(); exit(); } /* displays the following ID: 1 Name: user1 Email: user1@domain.com ID: 2 Name: user2 Email: user2@domain.com ID: 3 Name: user3 Email: user3@domain.com ID: 4 Name: user4 Email: user4@domain.com ID: 5 Name: user5 Email: user5@domain.com ID: 6 Name: user6 Email: user6@domain.com ID: 7 Name: user7 Email: user7@domain.com ID: 8 Name: user8 Email: user8@domain.com ID: 9 Name: user9 Email: user9@domain.com ID: 10 Name: user10 Email: user10@domain.com Time spent in fetching database rows was 0.0049 seconds */ As you can see, the previous example shows a small, almost negligible difference when compared to the first case, because database rows were sent to the client compressed. However, you must remember that the two examples were tested with a local server. And even considering this condition, the overall result was slightly faster when HTTP compression was applied to the respective data. On one hand, you learned how to define a timer class with PHP 5, while on the other, you saw two concrete cases where this class was used with pretty good results. Of course, my main recommendation here is that you test the previous timer class with your own PHP applications to evaluate their real performance. Final thoughts Finally, we’ve come to the end of this article. In this second tutorial of the series, I developed a couple of hands-on examples with the purpose of using a timer class to benchmark their respective performances. I hope this experience was pretty instructive for you. In the last part of the series, I’ll show you how to use the same timer class, but this time to evaluate the difference between fetching rows from a database table and retrieving content from a text file. See you in the last article!
blog comments powered by Disqus |
|
|
|
|
|
|
|