Request quote

How to increase speed and performance of your Elgg website

Posted on: May 10th, 2012 by Mohammed Aqeel 3 Comments

Hello people, through this tutorial we will help yoou in increasing the performance and speed of your elgg website by some simple tweaks. Check these and let us know your feedback. These are all based up on our inferences on upgrading our home site to Elgg 1.8.X

Click the below image to see our site speed result with all parameters ranking as A grade! that too on a shared server :)

webpage speed test

Areas where we can have some easier optimizations

  • Ajaxification of some areas
  • Caching the html / images and other contents
  • Code optimizations
  • CSS and JS Compression
  • Compress Images
  • Optimize the Database tables
  • Use only necessary plugins
  • Avoid reloading of the engine.
  • Avoid 404

Ajaxification of Elgg

We found that the major loading time needed for elgg is in pagination and reload of the page. So we started ajaxifying pagination in elgg. We have added ajaxified pagination for our portfolio section, advanced plugin search functionality etc. We also cached these ajax outputs so that it avoids repeated search query on the same contents. We used ajax GET in maximum possible areas with an expiry date in future. This avoided repeated database query and made the search in elgg faster. If you are a newbie have a look at this elgg plugin which will avoid pagination and load contents via ajax.

Caching of contents

As Team Webgalli site is mainly an informative site, we have set future expiry date for all contents like html, images, css, jss etc.. We added a version tag to the theme and this will be passed to all these contents. So only if we change the version tag, these contents will be re downloaded from the server. Otherwise the browser will provide you with the cached contents. This save a lot of bandwidth ,queries and increases site speed tremendously. We created html caches for the some of the search contents (like the latest list of plugins, the latest blog list which is fetched from our wordpress site, the latest tweets), pages etc.. So the system wont make a query to the DB, instead it will load contents from the html cache only. You can use the following code to create such a cache. Add the following code to your start.php file

function webgalli_cache_exists($interval = 604800, $filename){
    if(!$filename){
        $filename = $_SERVER["REQUEST_URI"];
    }
    $cache_filename = "path_to_cache/".basename( rtrim( $filename, '/' ) ).".cache";
    if (file_exists($cache_filename)){
        if ((time() - $interval) < filemtime( $cache_filename ) ) {
            return $cache_filename;
        } else {
            unlink ($cache_filename);
        }
    } else {
        ob_start();
    }
    return false;
}
function create_webgalli_cache($filename){
    if(!$filename){
        $filename = $_SERVER["REQUEST_URI"];
    }
    $cache_filename = "path_to_cache/".basename( rtrim( $filename, '/' ) ).".cache";
    $buff = ob_get_contents();
    $file = fopen( $cache_filename, "w" );
    fwrite( $file, $buff );
    fclose( $file );
    ob_end_flush();
}

Now inside your views add the following code (this is just reference only, you need to use it wisely)

<?php
    $cache_filename = webgalli_cache_exists(86400,'view_name');
    if(!$cache_filename){
?>
    <!-- load your html here-->
<?php
    create_webgalli_cache('view_name');
} else {
        readfile($cache_filename);
}
?>

Code optimizations

Elgg 1.8 is much heavier than its parent 1.7. So we did some basic code optimizations through out our plugins. Instead of getting the site URL, the logged in users and the plugin settings etc.. we made these values predefined on the system boot itself. This reduced around 20% calls to the server for loading a single page. We are still planning for more optimizations and will keep you all updated on these methods.

CSS Compression

We compressed the complete Elgg css with help of css compressor. This reduced the total size of the CSS file from 90kbs to just 52Kbs. The javascripts are compressed with the help of the minify plugin (We integrated this functionality with our theme, so that we can avoid the use of one extra plugin). The javascripts are compressed 47.1%. Before compression the size was 61.3Kbs and after compression it becomes 28.9Kbs. Take a look at the source of our home page and compare the js and css with yours.

Compress Images

Compress, compress, compress the maximum possible. Use png images maximum possible. We used the tool PNGOUT Win to compress the maximum possible images. This helped us to reduce the total size by more than 500Kb.

mySQL Table optimizations

We also did many optimizations in the mySQL tables. We scheduled daily cron jobs for database cleaning. The elgg’s garbage collector wont remove all the orphaned metadatas. We noticed that sometimes when a metadata is deleted, the corresponding metastrings are not getting deleted. So we added specific SQL commands to to run with the daily cron, so that it will remove all orphan metadata and meta strings. As the system logs table are not useful for us, we used cron to truncate it daily.

Use only necessary plugins

We are not using a lot of plugins, in our site. We are using only 18 plugins (including the theme). The more the number of plugins you are having in your mod directory, the slower will be your Elgg site. So to increase the elgg site’s speed remove all those unnecessary plugins. Keep the engine slim. We merged the functionality of garbage collector, members, twitter etc.. with our theme. This avoided loading three start.php file’s during the loading of elgg engine.

Avoid reloading of the engine.

We processed all the page / search / requests through page handlers. This avoided the necessity of reloading the elgg engine again and the difference is visible when you switch between the two.

Avoid 404’s

In Elgg 1.8 for every 404 page, the system has to load once. So cleanup your broken links etc.. You can use the following code to track your broken links.

    $current_url = current_page_url();
    $referer = $_SERVER['HTTP_REFERER'];
    $file = 'PATH-TO-404-TRACK/404.TXT';
    $fp = fopen($file, "a");
    $time = date("F jS - g:i a");
    fwrite($fp, $referer ." | ". $current_url . " | " . $time."n");
    fclose($fp);

We will add more of our inferences here. But remember that these all can help you to make the site faster by around 30-40%. The remaining 60-70% depends on your server configurations. If you are on cheap shared servers then the effect of these optimizations will be very minimal because Elgg is a resource hog. It needs quality servers for its performance.

Tags: , , , , , , ,

3 Responses

  1. jimmy says:

    great article, I have one question about “Avoid reloading of the engine.”

    How to processed all the page / search / requests through page handlers?
    If I never call engine/start.php , how can I use the elgg classes and libs?

  2. Webgalli says:

    Hi Jimmy,

    You have to route everything via elgg page handlers.