Exemplo n.º 1
0
 public function testSpeedSame()
 {
     Benchmark::compare(['register' => function () use(&$index) {
         $this->_manager->register('item', 'assets:less/styles.less');
     }, 'add' => function () {
         $this->_manager->add('item', 'assets:less/styles.less');
     }], ['count' => 1000]);
 }
Exemplo n.º 2
0
 public function testHash()
 {
     Benchmark::compare(array('md5' => function () {
         $string = str_repeat(mt_rand(0, 9), 1024 * 1024);
         return md5($string);
     }, 'crc32' => function () {
         $string = str_repeat(mt_rand(0, 9), 1024 * 1024);
         return crc32($string);
     }, 'sha1' => function () {
         $string = str_repeat(mt_rand(0, 9), 1024 * 1024);
         return sha1($string);
     }), array('name' => __FUNCTION__, 'count' => 100));
 }
Exemplo n.º 3
0
 public function testForReadme()
 {
     $times = 10000;
     $this->_data = array('prop' => uniqid('', true), 'prop1' => uniqid('', true), 'prop2' => uniqid('', true), 'prop3' => uniqid('', true), 'prop4' => uniqid('', true), 'inner' => array('prop' => uniqid('', true), 'prop1' => uniqid('', true), 'prop2' => uniqid('', true), 'prop3' => uniqid('', true), 'prop4' => uniqid('', true), 'inner' => array('prop' => uniqid('', true), 'prop1' => uniqid('', true), 'prop2' => uniqid('', true), 'prop3' => uniqid('', true), 'prop4' => uniqid('', true))));
     $array = $this->_data;
     $data = new Data($this->_data);
     $arrobj = new \ArrayObject($this->_data);
     Benchmark::compare(array('Array' => function () use($array) {
         $var = $array;
         // for clean experiment
         return $var;
     }, 'ArrayObject' => function () use($array) {
         $var = new \ArrayObject($array);
         return $var;
     }, 'Data' => function () use($array) {
         $var = new Data($array);
         return $var;
     }), array('name' => 'For Readme: Create', 'count' => $times));
     Benchmark::compare(array('Array' => function () use($array) {
         return array_key_exists('prop', $array) ? $array['prop'] : null;
     }, 'ArrayObject' => function () use($arrobj) {
         return $arrobj->offsetGet('prop');
     }, 'Data' => function () use($data) {
         return $data->get('prop');
     }), array('name' => 'For Readme: Get by key', 'count' => $times));
     Benchmark::compare(array('Array' => function () use($array) {
         if (array_key_exists('inner', $array) && array_key_exists('inner', $array['inner']) && array_key_exists('prop', $array['inner']['inner'])) {
             return $array['inner']['inner']['prop'];
         }
         return 42;
     }, 'ArrayObject' => function () use($arrobj) {
         if (array_key_exists('inner', $arrobj) && array_key_exists('inner', $arrobj['inner']) && array_key_exists('prop', $arrobj['inner']['inner'])) {
             return $arrobj['inner']['inner']['prop'];
         }
         return 42;
     }, 'Data' => function () use($data) {
         return $data->find('inner.inner.prop', 42);
     }), array('name' => 'For Readme: Find nested defined var', 'count' => $times));
     Benchmark::compare(array('Array' => function () use($array) {
         if (array_key_exists('inner', $array) && array_key_exists('inner', $array['inner']) && array_key_exists('undefined', $array['inner']['inner'])) {
             return $array['inner']['inner']['prop'];
         }
         return 42;
     }, 'ArrayObject' => function () use($arrobj) {
         if (array_key_exists('inner', $arrobj) && array_key_exists('inner', $arrobj['inner']) && array_key_exists('undefined', $arrobj['inner']['inner'])) {
             return $arrobj['inner']['inner']['undefined'];
         }
         return 42;
     }, 'Data' => function () use($data) {
         return $data->find('inner.inner.undefined', 42);
     }), array('name' => 'For Readme: Find nested undefined var', 'count' => $times));
 }
Exemplo n.º 4
0
 /**
  * @param callable $test
  * @param array    $options
  * @return array
  */
 public static function one($test, $options)
 {
     $options = array_merge(array('name' => 'One test', 'count' => 1000, 'output' => true), $options);
     // Prepare
     $bench = new Benchmark();
     $bench->setCount($options['count']);
     $bench->add('Test', $test);
     declare (ticks=1);
     // Run tests
     $wrapProfiler = new Profiler();
     $wrapProfiler->start(false);
     if ($options['output']) {
         $bench->out(PHP_EOL . '<pre>--------------- Start one bench: ' . $options['name'] . ' ---------------');
         $result = $bench->run(true);
         $bench->out('-------------------- Finish one bench: ' . $options['name'] . ' ---------</pre>' . PHP_EOL);
     } else {
         $result = $bench->run(false);
     }
     $wrapProfiler->stop();
     $result['total'] = array('time' => $wrapProfiler->getTime(), 'time_one' => $wrapProfiler->getTime() / $options['count'], 'memory' => $wrapProfiler->getMemory(), 'formated' => $wrapProfiler->getTotalUsage());
     return $result;
 }
Exemplo n.º 5
0
/**
 * @link http://www.php.net/manual/en/control-structures.declare.php#control-structures.declare.ticks
 *
 * @param array $tests
 * @param array $options
 * @return array
 *
 * @deprecated
 * @throws Exception
 */
function runBench(array $tests, array $options = array())
{
    if (!class_exists('\\JBZoo\\Profiler\\Benchmark')) {
        throw new Exception('jbzoo/profiler required for runBench() function');
    }
    return Benchmark::compare($tests, $options);
}