After about an year, since we launched Asianet News Portal the home page with all the complications had started to show a dead slow performance, and by using our Query Profiler, I could identify the bottleneck as the number of queries, and wordpress itself was telling the status as 48 queries in 6.344 seconds. Now this was a whopping value, and when the latency of 2 to 3 seconds is added to this, the initial html load would be in 9 seconds. Making the total site complete load in about 53 seconds. One can imagine the agony I was feeling, after all the work I had done to get backlinks, and if users who are visiting the site is just running away without waiting for the site to load, then I better not run the site atall.
Continue reading “Speeding up WordPress home page”
During the past couple of months, since we are maintaining a heavily loaded wordpress implementation, Kerala News Portal for Asianet, distributed across two servers, and having a daily visit of 50K, I was on the hunt for better caching and optimizing techniques. On the move, I happened to read the article by Jeff Starr, written some 15 months ago, and all the associated comments. It was sad to see that the file based cache was removed from wordpress, due to several reasons though.
After reading through this, I saw the official Function Reference for WP Cache, and the eAccelerator for WordPress by NeoSmart Technologies. Reading through the code written by Computer Guru of NeoSmart Technologies, tickled me and I just modified some parts, by pulling out the eaccelerator specific functions, and switching to a file system based store. I am not adding the code as pretty php code. Drop the file into wp-content, create a cache folder in wp-content and chmod folder to 755.
Recently only I found that this has been outdated with new version of wordpress demanding more of the wrapper functions and capabilities. And this has been brought into the system. Functionally more or less the same, but will work on shared hosting for just object caching.
A PHP accelerator is an extension designed to boost the performance of software applications written using the PHP programming language. Most PHP accelerators work by caching the compiled bytecode of PHP scripts to avoid the overhead of parsing and compiling source code on each request (some or all of which may never even be executed). For best performance, caching is to shared memory with direct execution from the shared memory and the minimum of memory copying at runtime. A PHP accelerator typically reduces server load and increases the speed of PHP code anywhere from 2-10 times, depending on factors such as the inherent execution time of the PHP application and the percentage of source code actually executed on a given request. While a code optimizer may even slow down overall performance when used in isolation, it can provide an additional performance boost when coupled with a code cache as the optimization effort is performed just once.
Continue reading “PHP Opcode Caches, switching between”
Tweaking the wordpress 404 template such that error pages for images and other linked items do not go the full blown 404 error page.
Today on the server logs I found about 2900 404 errors triggered from a single IP, the logs are similar as
[IP REMOVED] keralaonline.com - [27/Apr/2009:11:38:34 +0530] "GET /wp-content/uploads/2009/04/photo993.jpg HTTP/1.0" 404 18013 "https://keralaonline.com/wp-content/uploads/2009/04/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
[IP REMOVED] keralaonline.com - [27/Apr/2009:11:38:38 +0530] "GET /wp-content/uploads/2009/04/photo997.jpg HTTP/1.0" 404 18013 "https://keralaonline.com/wp-content/uploads/2009/04/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
[IP REMOVED] keralaonline.com - [27/Apr/2009:11:38:39 +0530] "GET /wp-content/uploads/2009/04/photo999.jpg HTTP/1.0" 404 18013 "https://keralaonline.com/wp-content/uploads/2009/04/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
one can see that the request is scripted and at a random interval, seems to be a script kiddie trying to leech images, but I got worried that even for this wordpress would go and output my pretty 404 page, which was as you could see is a whopping 17Kb, and takes about half a second to build. So just gave a twist and modified the top area of my 404 template Continue reading “Tunning (tuning) WordPress”
What was that ?
Yes.. I mean use variables in SQL. This can be quite interesting, though there may be a lot of people who might comment, that this kills readability, or does not induce even a ray of benefit, well I am not into an argument. And the method I am testing only with MySQL. In several situations, it has proven to be more faster, and as of recent times, we have started to send the whole of the code as a single operation letting mysql to handle the query splitting.
Suppose we need to insert first into a login table, to generate the user_id, which is auto-increment, and then into a set of tables, normally, we would go around insert into the table, then use mysql_insert_id(), to get the last inserted id, to process the rest of the tables. Now think if we used
insert into [table1] (fieldnames) values (values);
select last_insert_id() into @ii;
insert into [table2] (fkId,fieldnames) values (@ii,values);
insert into [table3] (fkId,fieldnames) values (@ii,values);
There are several other example uses for selecting into a variable, but the limitations are that you can select only a single field and single row.
Any comments :
- If a method can be static, declare it static. Speed improvement is by a factor of 4.
- Avoid magic like __get, __set, __autoload
- require_once() is expensive
- Use full paths in includes and requires, less time spent on resolving the OS paths.
- If you need to find out the time when the script started executing, $_SERVER[â€™REQUEST_TIMEâ€™] is preferred to time()
- See if you can use strncasecmp, strpbrk and stripos instead of regex
- preg_replace is faster than str_replace, but strtr is faster than preg_replace by a factor of 4
- If the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments.
- Error suppression with @ is very slow.
- $row[‘id’] is 7 times faster than $row[id]
- Error messages are expensive
- Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time.
As blogged by alexmoskalyuk
Our team was having a debate on why we should put a ‘ (single quote) when referencing arrays in php. Well what is the problem if we use $_SESSION[key], instead of $_SESSION[‘key’].
Well when you think that when encountering non functions or keywords, php tries to evaluate it as a declared constant. So referencing $_SESSION[‘key’] would be better against $_SESSION[key], unless we have defined the constant key and want to use the value of key.
I recommend using ‘ (single quotes) when programming with PHP; Always use ‘ (single quotes) unless you need the features of ” (double quotes). You might think it’s much easier to write code as:
echo "Today is the $day of $month";
However, using single quotes forces variables to be outside the quotes; instead when you use the ” (double quotes), forces php to evaluate the string, where as with ‘ (single quotes), the string content is taken as such.
PHP pages have a reputation of being more difficult (or at least different) to SEO than static HTML pages. Here is an overview of the major issues encountered when trying to optimize PHP script for search engines. While this focuses on PHP much of it is still relevant to SEO ing dynamic pages in general.
While page size does affect load time, spiders run on servers connected to high bandwidth networks, so download time is less important than the latency of the PHP script execution time. If a search engine spider follows a link on a site and is forced to wait too long for the server to process the PHP code behind that page, it may label your page as unresponsive.
The biggest delays in a PHP script typically are the database and loop code. Avoid making SELECT * calls, instead explicitly name all the columns you want to retrieve, and if you are using MySQL, test your queries using the EXPLAIN statement. To optimize loops consider using duplicated code instead of loops that don’t repeat very many times, also use as many static values, such as count($array) values inside the loop as you can, generating their values before the loop once.
Use of Compile Cache
The execution times can be improved by implementing compile caches like Turk MMCache (https://turck-mmcache.sourceforge.net) or APC as a php module. Though I do agree, that these would not be sufficient where contents are changing dynamically or across time.
Continue reading “Search Engine Optimizing PHP Scripts”