APC bewirkt nicht viel - falsch konfiguriert?

para_noid

hirnrissig
Moin,

ich hab mal APC auf meinem Ubuntuserver installiert und mit einem Testscript die Dauer bei aktiviertem und deaktiviertem APC verglichen. Leider sind die Unterschiede nur marginal. Ich würde jetzt gerne wissen ob es dem Script oder einer falschen Einstellung geschuldet ist.

Script:
PHP:
/*
##########################################################################
#                      PHP Benchmark Performance Script                  #
#                         © 2010 Code24 BV                               # 
#                                                                        #
#  Author      : Alessandro Torrisi                                      #
#  Company     : Code24 BV, The Netherlands                              #
#  Date        : July 31, 2010                                           #
#  version     : 1.0                                                     #
#  License     : Creative Commons CC-BY license                          #
#  Website     : http://www.php-benchmark-script.com                     #	
#                                                                        #
##########################################################################
*/

	function test_Math($count = 140000) {
		$time_start = microtime(true);
		$mathFunctions = array("abs", "acos", "asin", "atan", "bindec", "floor", "exp", "sin", "tan", "pi", "is_finite", "is_nan", "sqrt");
		foreach ($mathFunctions as $key => $function) {
			if (!function_exists($function)) unset($mathFunctions[$key]);
		}
		for ($i=0; $i < $count; $i++) {
			foreach ($mathFunctions as $function) {
				$r = call_user_func_array($function, array($i));
			}
		}
		return number_format(microtime(true) - $time_start, 3);
	}
	
	
	function test_StringManipulation($count = 130000) {
		$time_start = microtime(true);
		$stringFunctions = array("addslashes", "chunk_split", "metaphone", "strip_tags", "md5", "sha1", "strtoupper", "strtolower", "strrev", "strlen", "soundex", "ord");
		foreach ($stringFunctions as $key => $function) {
			if (!function_exists($function)) unset($stringFunctions[$key]);
		}
		$string = "the quick brown fox jumps over the lazy dog";
		for ($i=0; $i < $count; $i++) {
			foreach ($stringFunctions as $function) {
				$r = call_user_func_array($function, array($string));
			}
		}
		return number_format(microtime(true) - $time_start, 3);
	}


	function test_Loops($count = 19000000) {
		$time_start = microtime(true);
		for($i = 0; $i < $count; ++$i);
		$i = 0; while($i < $count) ++$i;
		return number_format(microtime(true) - $time_start, 3);
	}

	
	function test_IfElse($count = 9000000) {
		$time_start = microtime(true);
		for ($i=0; $i < $count; $i++) {
			if ($i == -1) {
			} elseif ($i == -2) {
			} else if ($i == -3) {
			}
		}
		return number_format(microtime(true) - $time_start, 3);
	}	
	
	
	$total = 0;
	$functions = get_defined_functions();
	$line = str_pad("-",38,"-");
	echo "<pre>$line\n|".str_pad("PHP BENCHMARK SCRIPT",36," ",STR_PAD_BOTH)."|\n$line\nStart : ".date("Y-m-d H:i:s")."\nServer : {$_SERVER['SERVER_NAME']}@{$_SERVER['SERVER_ADDR']}\nPHP version : ".PHP_VERSION."\nPlatform : ".PHP_OS. "\n$line\n";
	foreach ($functions['user'] as $user) {
		if (preg_match('/^test_/', $user)) {
			$total += $result = $user();
            echo str_pad($user, 25) . " : " . $result ." sec.\n";
        }
	}
	echo str_pad("-", 38, "-") . "\n" . str_pad("Total time:", 25) . " : " . $total ." sec.</pre>";

APC-config:
Code:
Version 	3.1.3p1
MMAP Support 	Enabled
MMAP File Mask 	no value
Locking type 	pthread mutex Locks
Revision 	$Revision: 286798 $
Build Date 	Apr 18 2010 06:56:17 
apc.cache_by_default	On	On
apc.canonicalize	On	On
apc.coredump_unmap	Off	Off
apc.enable_cli	Off	Off
apc.enabled	Off	Off
apc.file_md5	Off	Off
apc.file_update_protection	2	2
apc.filters	no value	no value
apc.gc_ttl	3600	3600
apc.include_once_override	Off	Off
apc.lazy_classes	Off	Off
apc.lazy_functions	Off	Off
apc.max_file_size	1M	1M
apc.mmap_file_mask	no value	no value
apc.num_files_hint	1000	1000
apc.preload_path	no value	no value
apc.report_autofilter	Off	Off
apc.rfc1867	Off	Off
apc.rfc1867_freq	0	0
apc.rfc1867_name	APC_UPLOAD_PROGRESS	APC_UPLOAD_PROGRESS
apc.rfc1867_prefix	upload_	upload_
apc.rfc1867_ttl	3600	3600
apc.shm_segments	1	1
apc.shm_size	32M	32M
apc.stat	On	On
apc.stat_ctime	Off	Off
apc.ttl	0	0
apc.use_request_time	On	On
apc.user_entries_hint	4096	4096
apc.user_ttl	0	0
apc.write_lock	On	On

Der Wert bei apc.shm_size ist genau passend für den Arbeitsspeicher, daran dürfte es also nicht liegen.

Bei deaktiviertem APC
Total time: : 6.39 sec.

Bei aktiviertem APC
Total time: : 6.277 sec.

Jemand eine Idee?
 
Du solltest dir darüber im Klaren sein, was APC eigentlich macht. Er cached den Bytecode, der beim "kompilieren" der PHP-Scripts enstanden ist, so das beim nächsten Aufruf das "Kompilieren" nicht notwendig ist. Das macht sich vor allem bei großen Scripts oder bei sehr vielen Includes und damit verteilten Code bemerkbar.

Das du bei deinem Test-Script keine oder kaum Unterschiede feststellst, ist also nicht verwunderlich.

Du kannst aber mal folgendermaßen testen: Verwende ab (Apache-Benchmark) um das Script quasi gleichzeitig sehr oft hintereinander aufzurufen und vergleiche dann mit und ohne APC. Das Tool ab dürfte bei deiner Apache-Installation im "bin" Ordner liegen.
 
Zurück