As I stated in the section that you just read, the first benchmarking example that I’ll set up will be aimed at determining how long it takes to fetch ten rows from a sample “USERS” MySQL database table. They will be displayed on the browser after the retrieval process. In this case, all these tasks will be performed without using HTTP compression when the data is transferred back to the client.
Assuming that the sample database table has been populated with the following data,
here is the corresponding code sample along with the respective timing results:
// example without using HTTP compression
// instantiate 'Timer' class
// start timer
// connect to MySQL
$result=$db->query('SELECT * FROM users');
echo 'ID: '.$row['id'].' Name: '.$row['name'].' Email: '.$row['email'].'<br />';
// display elapsed time
echo 'Time spent in fetching database rows was '.$elapsedTime.' seconds';
/* displays the following:
ID: 1 Name: user1 Email: firstname.lastname@example.org
ID: 2 Name: user2 Email: email@example.com
ID: 3 Name: user3 Email: firstname.lastname@example.org
ID: 4 Name: user4 Email: email@example.com
ID: 5 Name: user5 Email: firstname.lastname@example.org
ID: 6 Name: user6 Email: email@example.com
ID: 7 Name: user7 Email: firstname.lastname@example.org
ID: 8 Name: user8 Email: email@example.com
ID: 9 Name: user9 Email: firstname.lastname@example.org
ID: 10 Name: user10 Email: email@example.com
Time spent in fetching database rows was 0.0053 seconds
As you can see, the benchmarking script shown above is very simple to grasp. Basically, all it does is time the retrieval of the rows from MySQL, including the connection to the server. Obviously, this process is performed very quickly, since only a trivial query is executed against the sample database table. But in this case, I opted to display the data without using HTTP compression.
At this point, I’m pretty sure that you understand how to correctly use the “Timer” class that I defined a few lines above. Therefore, it’s time to leap forward and set up a similar example, but this time the database rows will be fetched and sent to the client using HTTP compression.
Go ahead and read the following section.
blog comments powered by Disqus