Skip to content

Instantly share code, notes, and snippets.

@atdt
Last active December 13, 2019 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atdt/f9ae8f7d1eedbe8783fc728e018965e8 to your computer and use it in GitHub Desktop.
Save atdt/f9ae8f7d1eedbe8783fc728e018965e8 to your computer and use it in GitHub Desktop.
loading i10n messages from opcache vs apc
<?php
// apcu vs. opcache benchmark
// loads mediawiki en messages from cache 1000 times
// you need languages/i18n/en.json in the same directory as this script.
//
// ini settings you need:
// zend_extension=opcache.so
// opcache.enable=1
// opcache.enable_cli=1
// extension=apcu.so
// apc.enabled = 1
// apc.enable_cli = 1
if ($argc !== 2 || ($argv[1] !== "apcu" && $argv[1] !== "opcache")) {
die("Usage: " . __FILE__ . " [apcu|opcache]\n");
}
// opcache: check file mtime *every time*. set this to a positive value to
// check script modifications every n seconds.
// To count stat() calls:
// $ strace -e trace=%stat php apcu_vs_opcache.php opcache 2>&1 | grep -c stat
ini_set("opcache.revalidate_freq", 0) || die("ini_set()\n");
if (!file_exists('en.php')) {
$arr = json_decode(file_get_contents('en.json'), true);
file_put_contents("en.php", "<?php\nreturn " . var_export($arr, true) . ";\n") || die();
}
// Warm up the caches
opcache_compile_file('en.php') || die("opcache_compile_file()\n");
$value = include('en.php');
apcu_store('en', $value);
opcache_is_script_cached('en.php') || die("opcache_is_script_cached()\n");
apcu_exists('en') || die("apcu_exists()\n");
$n = 1000;
$dummy = 0;
if ($argv[1] === "opcache") {
echo "opcache\n";
$start = microtime(true);
for ($i = 0; $i < $n; $i++) {
$arr = include('en.php');
$dummy += is_array($arr);
}
$end = microtime(true);
} else {
echo "apcu\n";
$mtime = filemtime('en.php');
$start = microtime(true);
for ($i = 0; $i < $n; $i++) {
$arr = apcu_fetch('en');
$dummy += is_array($arr);
clearstatcache(); // don't cache mtimes
if (filemtime('en.php') != $mtime) die("en.php mtime changed\n");
}
$end = microtime(true);
}
printf("%s: %.2fms\n", $argv[1], (($end-$start)*1000));
if ($dummy !== $n) die("error");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment