tests for egw_cache_provider and not using APC for cli, if apc.enable_cli is not set

Also did some benchmarking on my Macbook (with a SSD and memcached running on localhost):
Checking egw_cache_memcache:
0 checks failed, 100 iterations took 0.480 sec

Checking egw_cache_apc:
0 checks failed, 100 iterations took 0.025 sec

Checking egw_cache_files:
0 checks failed, 100 iterations took 0.826 sec
--> APC is by a factor of 20 faster then memcached, which is double as fast compared to files on a SSD
This commit is contained in:
Ralf Becker 2012-04-13 10:22:38 +00:00
parent a492f2a426
commit db729eac41
4 changed files with 107 additions and 13 deletions

View File

@ -1,13 +1,13 @@
<?php <?php
/** /**
* eGroupWare API: Caching data * EGroupware API: Caching data
* *
* @link http://www.egroupware.org * @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api * @package api
* @subpackage cache * @subpackage cache
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de> * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @copyright (c) 2009/10 by Ralf Becker <RalfBecker-AT-outdoor-training.de> * @copyright (c) 2009-12 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @version $Id$ * @version $Id$
*/ */
@ -518,10 +518,11 @@ class egw_cache
} }
} }
// setting apc as default provide, if apc_fetch function exists // setting apc as default provide, if apc_fetch function exists AND not cli or apc enabled for cli
if (is_null(egw_cache::$default_provider)) if (is_null(egw_cache::$default_provider))
{ {
egw_cache::$default_provider = function_exists('apc_fetch') ? 'egw_cache_apc' : 'egw_cache_files'; egw_cache::$default_provider = function_exists('apc_fetch') && (PHP_SAPI != 'cli' || ini_get('apc.enable_cli')) ?
'egw_cache_apc' : 'egw_cache_files';
} }
/** /**
@ -567,3 +568,92 @@ interface egw_cache_provider
*/ */
function delete(array $keys); function delete(array $keys);
} }
abstract class egw_cache_provider_check implements egw_cache_provider
{
/**
* Run several checks on a caching provider
*
* @param boolean $verbose=false true: echo failed checks
* @return int number of failed checks
*/
function check($verbose=false)
{
$failed = 0;
foreach(array(
egw_cache::TREE => 'tree',
egw_cache::INSTANCE => 'instance',
) as $level => $label)
{
foreach(array('string',123,true,false,null,array(),array(1,2,3)) as $data)
{
$location = md5(microtime(true).$label.serialize($data));
$get_before_set = $this->get(array($level,__CLASS__,$location));
if (!is_null($get_before_set))
{
if ($verbose) echo "$label: get_before_set=".array2string($get_before_set)." != NULL\n";
++$failed;
}
if (($set = $this->set(array($level,__CLASS__,$location), $data, 10)) !== true)
{
if ($verbose) echo "$label: set returned ".array2string($set)." !== TRUE\n";
++$failed;
}
$get_after_set = $this->get(array($level,__CLASS__,$location));
if ($get_after_set !== $data)
{
if ($verbose) echo "$label: get_after_set=".array2string($get_after_set)." !== ".array2string($data)."\n";
++$failed;
}
if (($delete = $this->delete(array($level,__CLASS__,$location))) !== true)
{
if ($verbose) echo "$label: delete returned ".array2string($delete)." !== TRUE\n";
++$failed;
}
$get_after_delete = $this->get(array($level,__CLASS__,$location));
if (!is_null($get_after_delete))
{
if ($verbose) echo "$label: get_after_delete=".array2string($get_after_delete)." != NULL\n";
++$failed;
}
}
}
return $failed;
}
}
// some testcode, if this file is called via it's URL
// can be run on command-line: sudo php -d apc.enable_cli=1 -f phpgwapi/inc/class.egw_cache.inc.php
/*if (isset($_SERVER['SCRIPT_FILENAME']) && realpath($_SERVER['SCRIPT_FILENAME']) == __FILE__)
{
if (!isset($_SERVER['HTTP_HOST']))
{
chdir(dirname(__FILE__)); // to enable our relative pathes to work
}
$GLOBALS['egw_info'] = array(
'flags' => array(
'noapi' => true,
),
);
include_once '../../header.inc.php';
if (isset($_SERVER['HTTP_HOST'])) echo "<pre>\n";
foreach(array(
'egw_cache_memcache' => array('localhost'),
'egw_cache_apc' => array(),
'egw_cache_files' => array('/tmp'),
) as $class => $param)
{
echo "Checking $class:\n";
$start = microtime(true);
$provider = new $class($param);
$n = 100;
for($i=1; $i <= $n; ++$i)
{
$failed = $provider->check($i == 1);
}
printf("$failed checks failed, $n iterations took %5.3f sec\n\n", microtime(true)-$start);
}
}*/

View File

@ -1,13 +1,13 @@
<?php <?php
/** /**
* eGroupWare API: Caching provider storing data in PHP's APC * EGroupware API: Caching provider storing data in PHP's APC
* *
* @link http://www.egroupware.org * @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api * @package api
* @subpackage cache * @subpackage cache
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de> * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @copyright (c) 2010 by Ralf Becker <RalfBecker-AT-outdoor-training.de> * @copyright (c) 2010-12 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @version $Id$ * @version $Id$
*/ */
@ -20,7 +20,7 @@
* $GLOBALS['egw_info']['server']['cache_provider_instance'] = array('egw_cache_apc'); * $GLOBALS['egw_info']['server']['cache_provider_instance'] = array('egw_cache_apc');
* and optional also $GLOBALS['egw_info']['server']['cache_provider_tree'] (defaults to instance) * and optional also $GLOBALS['egw_info']['server']['cache_provider_tree'] (defaults to instance)
*/ */
class egw_cache_apc implements egw_cache_provider class egw_cache_apc extends egw_cache_provider_check implements egw_cache_provider
{ {
/** /**
* Constructor, eg. opens the connection to the backend * Constructor, eg. opens the connection to the backend
@ -34,6 +34,10 @@ class egw_cache_apc implements egw_cache_provider
{ {
throw new Exception (__METHOD__.'('.array2string($params).") No function apc_fetch()!"); throw new Exception (__METHOD__.'('.array2string($params).") No function apc_fetch()!");
} }
if (PHP_SAPI == 'cli' && !ini_get('apc.enable_cli'))
{
throw new Exception (__METHOD__.'('.array2string($params).") APC NOT enabled for cli, check apc.enable_cli!");
}
} }
/** /**

View File

@ -1,13 +1,13 @@
<?php <?php
/** /**
* eGroupWare API: Caching provider storing data to files * EGroupware API: Caching provider storing data to files
* *
* @link http://www.egroupware.org * @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api * @package api
* @subpackage cache * @subpackage cache
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de> * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @copyright (c) 2009 by Ralf Becker <RalfBecker-AT-outdoor-training.de> * @copyright (c) 2009-12 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @version $Id$ * @version $Id$
*/ */
@ -17,7 +17,7 @@
* The provider creates subdirs under a given path * The provider creates subdirs under a given path
* for each values in $key * for each values in $key
*/ */
class egw_cache_files implements egw_cache_provider class egw_cache_files extends egw_cache_provider_check implements egw_cache_provider
{ {
/** /**
* Extension of file used to store expiration > 0 * Extension of file used to store expiration > 0

View File

@ -1,13 +1,13 @@
<?php <?php
/** /**
* eGroupWare API: Caching provider storing data in memcached via PHP's memcache extension * EGroupware API: Caching provider storing data in memcached via PHP's memcache extension
* *
* @link http://www.egroupware.org * @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api * @package api
* @subpackage cache * @subpackage cache
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de> * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @copyright (c) 2009 by Ralf Becker <RalfBecker-AT-outdoor-training.de> * @copyright (c) 2009-12 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @version $Id$ * @version $Id$
*/ */
@ -22,7 +22,7 @@
* *
* You can set more then one server and specify a port, if it's not the default one 11211. * You can set more then one server and specify a port, if it's not the default one 11211.
*/ */
class egw_cache_memcache implements egw_cache_provider class egw_cache_memcache extends egw_cache_provider_check implements egw_cache_provider
{ {
/** /**
* Instance of Memcache * Instance of Memcache