Beispiel #1
0
 public function testReport()
 {
     Timer::$defaultMinDuration = 100;
     $report = new Report('Test report', 'Example benchmark.');
     $unit = new Unit('String concatenation');
     $unit->addClosure(function ($n) {
         while ($n--) {
             $x = "foobar {$n}";
         }
     }, '$x = "foobar $n"');
     $unit->addClosure(function ($n) {
         while ($n--) {
             $x = 'foobar ' . $n;
         }
     }, '$x = \'foobar \' . $n');
     $report->unitList->add($unit);
     $unit2 = new Unit('String concatenation with varying length');
     foreach ($unit->getMethods() as $method) {
         $unit2->addMethod($method);
     }
     $unit2->addParam(new Parameter('x', '$p = \'x\''));
     $unit2->addParam(new Parameter('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', '$p = \'xxxxx..\''));
     $report->unitList->add($unit2);
     $report->run();
 }
Beispiel #2
0
 public function testUnit()
 {
     Timer::$defaultMinDuration = 100;
     $unit = new Unit('test');
     $unit->addMethod(new Method(function ($n, $cost) {
         if ($cost < 10) {
             while ($n--) {
                 $x = $n * 2;
             }
         } else {
             while ($n--) {
                 $x = $n * 2;
                 $x = number_format($x);
             }
         }
     }, 'calc', 'calculate stuff'));
     $unit->addClosure(function ($n, $cost) {
         while ($n--) {
             $x = password_hash('xx', PASSWORD_DEFAULT, array('cost' => $cost));
         }
     }, 'hash', 'password_hash');
     $unit->addParam(new Parameter(5, 'easy'));
     $unit->addParam(new Parameter(10, 'hard'));
     $results = $unit->run();
 }
Beispiel #3
0
    }
};
$isset = function ($n, $p) {
    $map = $p['map'];
    $needle = $p['needle'];
    while ($n--) {
        $found = isset($map[$needle]);
    }
};
// This is a shortcut to create a Method object and add it to the unit
$unit->addClosure($foreachExact, 'Strict `foreach $v === $n`', 'Strict comparison in a foreach loop');
$unit->addClosure($foreachEquals, 'Loose `foreach $v == $n`', 'Simple comparison in a foreach loop');
$unit->addClosure($arraySearch, '`array_search($n, $l)`', 'Search for value in list');
$unit->addClosure($arrayKeyExists, '`array_key_exists($n, $map)`', 'Check for string key in a map');
// Here's the verbose way:
$unit->addMethod(new \nochso\Benchmark\Method($isset, '`isset($map[$n])`', 'Check if string key is set'));
// $unit->addClosure($isset, 'isset($map[$n]');
// Create the list to be searched by each method
$list = array();
for ($i = 0; $i < 1000; $i++) {
    $list[] = $i . 'There is an art to flying, or rather a knack. Its knack lies in learning to throw yourself at the ground and miss.';
}
// Also supply an associative version of the list
$map = array_flip($list);
$params = array('list' => $list, 'map' => $map, 'needle' => $list[0]);
$unit->addParam(new Parameter($params, 'First of 1000'));
// Add different parameters by changing the needle of the haystack
$params['needle'] = $list[count($list) - 1];
$unit->addParam(new Parameter($params, 'Last of 1000'));
$params['needle'] = '404 Not found';
$unit->addParam(new Parameter($params, 'Not found'));