I have a LAMP website up and going. It runs fairly fast, but would like to improve the speed. I recognize you do not have the specific PHP script, SQL queries, whether caching is enabled, server configuration, etc, and not asking questions at that level. Instead, I am asking whether there is a general best-practice approach and the order of the the steps to identify bottlenecks so that website speed could be improved.
Asked
Active
Viewed 52 times
0
-
There aren't really any general answers to questions like this. It's just a matter of looking at all the diagnostics, and noticing values that seem excessive. – Barmar Jan 31 '15 at 15:37
-
@Barmar Any thoughts on where you would start first? – user1032531 Jan 31 '15 at 15:38
-
2Step 1: profile the execution. How much of the response time is spent in the user's browser, traversing the network, parsing the request, getting the necessary data, processing, assembling the reply, traversing the net back, and displaying the results in the user's browser? Apply recursively to the critical steps -- that's where specific techniques are used. But first, profile. – mpez0 Jan 31 '15 at 15:38
-
@mpez0 Would http://stackoverflow.com/questions/21133/simplest-way-to-profile-a-php-script provide a good approach to profile the execution? If not, any recommendations? – user1032531 Jan 31 '15 at 15:41
-
@user1032531 I would start by asking on the correct site, maybe serverfault.com. – Barmar Jan 31 '15 at 15:42
-
good tool http://gtmetrix.com/ – Simone Nigro Jan 31 '15 at 15:43
-
@Barmar I expect it will be considered off topic at serverfault even more so than here. – user1032531 Jan 31 '15 at 15:43
-
@barmar This is on topic for stackoverflow, but is not a specific question. If it was phrased "In general, how do I optimize a multi-tiered application's performance?", would you feel better? – mpez0 Jan 31 '15 at 15:47
-
@user1032531: that's a fine start. Anything from a stopwatch on down is a good way to get going. The important thing is to understand how much effect your current efforts *can* have relative to the overall application performance. – mpez0 Jan 31 '15 at 15:50
1 Answers
1
I don't know if there is a general way to optimise for speed but personally every time I write a new section of code, I always know how long that code takes to execute simply by measuring it.
If you are retrospectively looking at a lot of code that's already been written, just break it down into sections and measure the time taken by each section.
I permanently have these lines wrapped around the main body of my code;
<?php
$start_time = microtime(true); // Note the start time.
$html = get_html(); // Main body of code.
// Calculate peak memory usage and time taken.
$mem = number_format(memory_get_peak_usage(true) / 1024 / 1024, 1).'mb';
$tmr = number_format(microtime(true) - $start_time, 2).'s';
echo "{$html}\n<!-- Peak memory usage: {$mem}. Response time: {$tmr} -->";
?>
That will show how long get_html()
is taking. If you compare that to the waiting time reported by the browser, you can tell how much of the overall response time is consumed by your code and how much is consumed by things other than your code.
Hope that helps.

Nigel Alderton
- 2,265
- 2
- 24
- 55
-
Thanks Nigel, Doing so only measures PHP and associated MySQL, but not network/apache/multiple requests, etc. – user1032531 Jan 31 '15 at 20:02
-
Indeed, but if you know how much time your own code is taking and you know the total time taken then obviously the network/apache time is the difference between those two. I'm not aware of a way to directly measure the network/apache time sorry. btw I don't think your question is too broad as is being suggested by the moderators. Slightly 'meta' questions are good. – Nigel Alderton Feb 01 '15 at 13:48
-
Thanks Nigel, I would have thought someone would have written a book on the subject. – user1032531 Feb 01 '15 at 16:11