Skip to content

Instantly share code, notes, and snippets.

@alechko
Last active December 17, 2015 20:48
Show Gist options
  • Save alechko/5669723 to your computer and use it in GitHub Desktop.
Save alechko/5669723 to your computer and use it in GitHub Desktop.
drupal cache benchmark sample
<?
# intended to execute in "devel execute php"
# to use outside devel, replace dsm() with print()
#
# this piece will measure cache set /get time for any method implemented in drupal.
# by default, it'll be the database caching, and you will be able to see the table
# 'cache' filling with entries.
# I'm using this to measure the difference of the default db method vs memcached.
# So for the first run, I use no extra cache config / modules, and I'm executing
# snippet 3 times.
# For the second run, I turn on memcached module and use:
# $conf['cache_class_cache'] = 'MemCacheDrupal';
# to point $bin = 'cache' to memcached. Also executing 3 times.
# see results at the bottom
$data = user_load(1); # use user 1 as sample object
$key = uniqid(); # set unique cache key
$start = microtime(true); # start measuring set
cache_set($key, $data, 'cache', time() + 60*60);
$time = microtime(true) - $start; # end measuring set
dsm($time, 'set');
$start = microtime(true); # start measuring get
cache_get($key, $bin = 'cache');
$time = microtime(true) - $start; # stop measuring get
dsm($time, 'get');
# results:
#
# db:
# set => 0.0042819976806641
# get => 0.0010731220245361
#
# set => 0.0051300525665283
# get => 0.00091385841369629
#
# set => 0.0050358772277832
# get => 0.0010919570922852
#
# memc:
# set => 0.00078082084655762
# get => 0.00049304962158203
#
# set => 0.0006718635559082
# get => 0.00067496299743652
#
# set => 0.00086498260498047
# get => 0.00060892105102539
# GODDAMN, memcached is so much faster...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment