Reduced Drupal Bootstrap for Better Performance

2012-11-02

Jitesh Doshi

Reduced Drupal Bootstrap for Better Performance

How to reduce Drupal bootstrap level for improved performance in custom PHP scripts.

Did you know you can use Drupal in your custom PHP scripts, outside of modules? But did you know that you can make your scripts much faster by reducing Drupal’s bootstrap level?

The following script reads the first 20 nids from your Drupal database:

define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']);
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

But in our script since we are interested only in reading from the database and not running complete Drupal modules, we can get away with bootstrapping Drupal partially. Change the bootstrap level from DRUPAL_BOOTSTRAP_FULL to DRUPAL_BOOTSTRAP_DATABASE.

Performance test results using Apache Benchmark (ab -n 50):

  • DRUPAL_BOOTSTRAP_FULL: 206ms average request time
  • DRUPAL_BOOTSTRAP_DATABASE: 27ms average request time

That’s how significant! You can read more about Drupal bootstrap levels here.