HosipLan

Untitled

Mar 9th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.58 KB | None | 0 0
  1.  
  2.     public function testBenchmark()
  3.     {
  4.         $time = microtime(TRUE);
  5.  
  6.         $container = $this->createContainer('sharding');
  7.         foreach ($container->getByType('Kdyby\Redis\ClientsPool') as $client) {
  8.             $this->getClient($client); // flushdb
  9.         }
  10.  
  11.         $containerFile = Nette\Reflection\ClassType::from($container)->getFileName();
  12.         $containerClass = get_class($container);
  13.  
  14.         $statsFile = TEMP_DIR . '/stats.log';
  15.  
  16.         $this->threadStress(function () use ($containerFile, $containerClass, $statsFile) {
  17.             require_once $containerFile;
  18.             /** @var Nette\DI\Container|\SystemContainer $container */
  19.             $container = new $containerClass();
  20.             $container->initialize();
  21.             Nette\Diagnostics\Debugger::enable(TRUE, TEMP_DIR);
  22.  
  23.             $cache = new \Nette\Caching\Cache($container->getByType('Kdyby\Redis\StorageRouter'));
  24.             $stats = array('miss' => 0);
  25.  
  26.             // randomly read every 1000th key
  27.             for ($i = 1; $i <= 10000; $i += 10) {
  28.                 $key = rand($i, $i + 10);
  29.                 $cache->load($key, function (&$dp) use ($key, $cache, &$stats) {
  30.                     $stats['miss'] += 1;
  31.                     $dp[$cache::TAGS] = array_unique(array(md5($key % 10000), md5($key % 1000), md5($key % 100), md5($key % 10)));
  32.                     return array_fill(0, 30, range('a', 'z'));
  33.                 });
  34.             }
  35.  
  36.             /** @var \Kdyby\Redis\Diagnostics\Panel $panel */
  37.             $panel = $container->getByType('Kdyby\Redis\Diagnostics\Panel');
  38.             $stats['queries'] = $panel->getQueryCount();
  39.             $stats['time'] = sprintf('%0.1f', $panel->getTotalTime()) . ' ms';
  40.             file_put_contents($statsFile, json_encode($stats) . "\n", FILE_APPEND);
  41.  
  42.         }, 200, 5);
  43.  
  44.         var_dump(microtime(TRUE) - $time);
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment