Author Archives: Emran Hasan

Optimizing variables cache in Drupal 6

In Drupal 6, a number of caching strategies are incorporated to handle large traffic. One of them is the serialization of the whole variable table. It is being cached in the database and gets extracted into global $conf variable in each invoke.

In one of our production sites, we faced hard time to keep up with the memory requirement of PHP for the unserialization of this variable from the cache. The variables table was so large that we had to assign around 1GB memory to each PHP thread so that the value can be unserialized without memory exhaustion. This made it much harder to scale the application.

So, we decided to do something about it and successfully handled it by doing the following:

1. First of all, we installed the memcache module to move the cache storage from DB to memory

2. We then edited the memcache module’s cache_get and cache_set functions to store/retrieve individual items from the variables array and split/join them when requested.

3. This requires a memcache call for each of the items in the variable array, but consumes a small amount of memory as there is no huge unserialize operation going on.

4. We performed a few tests to see if the site is working as before, and found its working perfectly!

Here is the code in case you are facing similar issue: Continue reading

JSONize your PHP classes using traits

I was having a discussion with a colleague regarding how to add generic JSON based representation to a number of classes without making a big effort. The immediate solution that came in my mind is to use the Traits for it (introduced in PHP 5.4).

So I wrote the following example for him:

JSONized.php

trait JSONized {

    public function toJson()
    {
        $properties = get_object_vars($this);
        return json_encode($properties);
    }

}

User.php

class User
{
    use JSONized;

    public $username;
    public $password;
    public $email;

    public function __construct($username, $password, $email)
    {
        $this->username = $username;
        $this->password = md5($password);
        $this->email = $email;
    }
}

test.php

include_once 'JSONized.php';
include_once 'User.php';

$emran = new User('phpfour', 'emran123', 'phpfour@gmail.com');
echo $emran->toJson();

The result of running the above code will be like this:

{
    "username" : "phpfour",
    "password" : "02c2ec1e3f21e97dd5ac747147a141e0",
    "email" : "phpfour@gmail.com"
}

Using Mockery as a mocking framework with PHPUnit

A few days ago I was explaining mocking to a few colleagues. I came up with the following quick n dirty code to show them the elegance of Mockery as a mocking framework.

The code is straight-forward: it checks the health of a server using a tiny heart beat script.

1. The script residing in the remote server:

/remote/heart.php

$response = array('status' => 'online');

header("Content-type: application/json");
echo json_encode($response);

2. The class that tries to fetch the status from the remote server:

/test/Heartbeat.php

class Heartbeat
{
    private $http;

    public function __construct(Http $http)
    {
        $this->http = $http;
    }
    
    public function checkHealth()
    {
        $url = 'http://localhost/remote/heart.php';
        $response = $this->http->getResponse($url);

        $beat = json_decode($response);
        return (!is_null($beat) && $beat->status == 'online');
    }
}

Continue reading

Node access rebuild using Drush

In one of our Drupal production site, we have around 84K nodes. Periodically we need to rebuild the content access permission, which takes around 6 hours to complete when done through the browser. Being very unhappy with it, today found the following drush use:

Emrans-MacBook-Air ~: drush php-eval 'node_access_rebuild();'

The results were immediate, the whole rebuild now takes only 20 minutes!

A clean way of integrating PHPUnit 3.5.x with CodeIgniter 2.x

Now a days, my default choice of framework is always Zend Framework. However, I have to maintain a couple live projects in CodeIgniter from early days. I feel its very important to have tests around critical applications, so I have attempted a couple times to integrate PHPUnit with CodeIgniter but failed every time – well, until now.

I’ve managed it this time with hooks. It provides a clean way of bootstrapping the framework and then performing tests on the Model layer – for me testing the model layer has been sufficient. The use is very simple as it does not require any change in how a regular CodeIgniter application is built.

Grab the code from github.

Example – /tests/PostTest.php

<?php

class PostTest extends PHPUnit_Framework_TestCase
{
    private $CI;

    public function setUp()
    {
        $this->CI = &get_instance();
        $this->CI->load->database('testing');
    }

    public function testGetsAllPosts()
    {
        $this->CI->load->model('post');
        $posts = $this->CI->post->getAll();
        $this->assertEquals(1, count($posts));
    }
}

Continue reading

Painless Ruby and Rails installation on Ubuntu/Mint

It took me an hour to properly install the latest versions of ruby, rails and its mysql binder. Although its less than a 5 mins task, I had to search in forums, blogs to deal with the various errors. So just posting this so its easy for someone else to get done with this in 5 mins.

1. Open Terminal and install RVM (Ruby Version Manager)

bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer

2. Enter the following in ~/.bash_profile or in ~/.bashrc (replace YOUR USER NAME with the username you’re logged in with):

[[ -s "/home/YOUR USER NAME/.rvm/scripts/rvm" ]] && source "/home/YOUR USER NAME/.rvm/scripts/rvm"  # This loads RVM into a shell session.

3. Install Ruby 1.9.2

rvm install 1.9.2

Continue reading

Setting up central Mercurial server in Ubuntu

Mercurial

It’s been a while since we’ve been thinking about moving to Mercurial from Subversion at our company. We were convinced about the benefits of DVCS and chose Mercurial over Git. However, due to pressure at work and some procrastination, the move was delayed a couple times. Finally, today we had the move and the team is already up to speed with it.

Although it’s quite simple to install it in a central server, the different articles on the web had me confused with too many options or with a different distro than Ubuntu. So, here is a dead simple list of things to do for installing Mercurial centrally for your dev team.

Continue reading

2011

First, a very happy new year to everybody!!! The last year has showed me the ups and downs of life and I believe 2011 will be a great year. Some of my wishes in 2011 would be:

  • Re-shaping my company to become a major OSS based company in Bangladesh
  • Doing something meaningful for my country
  • Learning Ruby and related frameworks (Rails, Cucumber, etc)
  • Spending more time my family
  • Having more fun with my friends
  • Living a healthy life