Minimizing Bootstrapping.

brad.curnow's picture

Hi Everyone!

I've written a PHP page that sits inside my Drupal install and is used for split second traffic processing and directing. Pending certain conditions (carried as POST data in the url) the script progressively bootstraps Drupal.

For instance we first look to see if we can direct the user to a cached page. If not, we bootstrap to DATABASE and run a couple of simple queries to see if we need to direct them to somewhere else.

Depending on what the results here are, I'll need to load probably a full Drupal page, and also trigger a Rule. The details are still formalising, but there may be opportunity to give the user a cached message whilst just triggering a Rule, and avoiding a full bootstrap.

SO!

Two questions:

a) does anyone know off the top of their head which phase would be sufficient to execute a Rules invoke event in?

and

b) if I bootstrap to to DATABASE, and then later call bootstrap again for a later phase, will it re-do all the bootstrapping it did for the database phase, or can it just build on top of the previous call?

Thanks!

Hope all your projects are going well :)

Comments

does anyone know off the top

dalin's picture

does anyone know off the top of their head which phase would be sufficient to execute a Rules invoke event in?

You'll need a full bootstrap. But if you've got enough dev skills to get this far, you might consider not using Rules and just do it in code. I avoid Rules where possible so that I can add documentation about why things are happening.

if I bootstrap to to DATABASE, and then later call bootstrap again for a later phase, will it re-do all the bootstrapping it did for the database phase

Nope. There's a static in there that keeps track of which bootstrap steps are already complete.

--
Dave Hansen-Lange
Technical Manager
Advomatic LLC
Great White North Office
Canada

Indeed. Thanks!

brad.curnow's picture

you might consider not using Rules and just do it in code.

Absolutely - my approach at the moment is to get everything up as quickly as possible so mostly using Rules, then replacing with custom code as time/skills permit.

Nope. There's a static in there that keeps track of which bootstrap steps are already complete.

That is very cool! Great to know, thanks.

Avoid module_implements before full bootstrap

znerol's picture

Regrettably it is not save to invoke anything calling module_implements() before DRUPAL_BOOTSTRAP_FULL. Doing so may lead to very weird and hard to track errors because you risk to corrupt the module_implements entry in the cache_bootstrap when not all modules are loaded.

You can use bootstrap_invoke_all instead (if you control all of the code which is invoked from there).

Interesting consideration,

brad.curnow's picture

Interesting consideration, and duly noted - thanks!

BC

Page cache without database

ben.bunk's picture

It seems like your looking for this setting:

<?php
$conf['page_cache_without_database'] = FALSE;

This tells Drupal to pull the entire page out of a cache and skip the database bootstrap altogether obviously you'll want to avoid storing your page cache in the Drupal database for this :-)

For a cache_page miss Drupal will progressively access it's cached resources and invoke hooks to build the page and return as early and as fast as it can. The trick is to make sure everything Drupal needs to build a page is cached as much as possible (lists of modules, hook implementors, etc) which is a fairly long list but most of it is done for you out of the box unless you have less optimized modules hooking into your page build or your own custom code isn't doing it.

Cheers,
Ben

Thanks mate! I'll look into

brad.curnow's picture

Thanks mate! I'll look into the ramifications of that.

Right now I'm hosting on Pantheon so not sure what does and doesn't work, but everything deserves investigating.

explicitly load the code you need???

tloudon's picture

If you look at the bootstrap code, you can generally run all the levels very quickly up to FULL bootstrap. (I'm sure this varies by server and config, but eg just loading a bootstrap level and echoing 'hello world' can easily be done w/in 100ms--that includes network time, etc so from your server to your browser).

The real hitch in the giddyup seems to be line 5127 in includes/common.inc -- the call to module_load_all(). Again depending on your setup, you can expect to pay a 1ms-5ms penalty per enabled module. EG, if you have 120 modules enabled you might see 300-500ms here. (In my initial testing, an opcode cache like APC helps quite a bit here--Pantheon should already have technology like this enabled tho)

The timings here are just for demonstrative purposes--you should definitely confirm on your own environment--however, I think that realtime processing becomes very difficult if you are spending half a second just loading files. If your use-case is small enough, you might be able to include only the files you need--and for ultimate speed directly include the file over drupal_load which will call the db or scan the filesystem. Definitely a running w/ scissors option--caveat emptor :)

Skipping Rules altogether and using some simple logic + meta data/config--like an ini file--might also work.

Cheers