PHP code example of codon / profiler

1. Go to this page and download the library: Download codon/profiler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codon / profiler example snippets



$data = [/* ... */];
$profiler = new \Codon\Profiler();
$profiler->set('showOutput', false)
    ->add([
        'name' => 'Count in loop', 'iterations' => 100,
        'function' => function() use ($data, &$profiler) {
            // This is the code to benchmark:
            for($i = 0; $i <= count($data); $i++) {
                echo $data[$i] . "\n";
            }
        }
    ])->run()->showResults();


$profiler->set($name, $value);



$test = [
    'name' => 'Benchmark name',
    'iterations' => # of times to run
    'function' => closure of the test to run
];

$profiler->add($test);


$test = [
    'name' => 'Sample',
    'iterations' => 100
    'function' => function() use (&$profiler) {

        $profiler->checkpoint('Started!');

        // some code

        $profiler->startTimer('Subsection');
        // Subsection of code
        $profiler->endTimer('Subsection');
    }
];

$profiler->add($test);

# You can chain these too
$profiler->add($test1)->add($test2);


$profiler->run();


class Test {

    public $_data = [];

    protected $_profiler;

    public function __construct() {
        $this->_profiler = new \Codon\Profiler();
    }

    public function runMethodProfiler() {
        $this->_profiler->add([
            'name' => 'testFunction', 'iterations' => 1,
            'function' => function($class) use (&$this->_profiler) {
                $class->testFunction($class->_data);
            }
        ])->run($this);
    }

    public function testFunction($data = '') {

        // Benchmark something in here

    }
}


$profiler
 ->add([ 'name' => 'Count in loop', 'iterations' => 1,
     'function' => function() use ($data, &$profiler) {

        $profiler->startTimer('Inside loop');
        for($i = 0; $i < count($data); $i++) {
            echo $data[$i] . "\n";
        }
        $profiler->endTimer('Inside loop');

        // Or this way:

        $timer_outside = $profiler->startTimer('Outside loop');
        $count = count($data);
        for($i = 0; $i < $count; $i++) {
            echo $data[$i] . "\n";
        }
        $profiler->endTimer($timer_outside);
     })->run();


$profiler
->clearAll()
 ->add([
     'name' => 'Count in loop',
     'iterations' => 100,
     'function' => function() use ($data, &$profiler) {

        $profiler->markMemoryUsage('Before loop');
        for($i = 0; $i < count($data); $i++) {
            echo $data[$i] . "\n";
        }
        $profiler->markMemoryUsage('After loop');

     })->run();


$data = [];
for($i = 0; $i < 1000; $i++) {
	$data[] = $i;
}

# You can pass options to the constructor
$profiler = new \Codon\Profiler([
	'showOutput' => false
]);

$profiler->markMemoryUsage('start');
$profiler->startTimer('Count in loop');

for($i = 0; $i < count($data); $i++) {
	echo $data[$i] . "\n";
}

$profiler->endTimer('Count in loop');
$profiler->markMemoryUsage('end');

$profiler->showResults();


$profiler
	->clearAll()
    ->add([
        'name' => 'Count in loop',
        'iterations' => 100,
        'function' => function() use ($data, &$profiler) {

			$profiler->startTimer('Inside loop');
            for($i = 0; $i < count($data); $i++) {
                echo $data[$i] . "\n";
            }
			$profiler->endTimer('Inside loop');


			$profiler->startTimer('Outside loop');
			$count = count($data);
			for($i = 0; $i < $count; $i++) {
				echo $data[$i] . "\n";
			}
			$profiler->endTimer('Outside loop');
        }
    ])
	->run()
	->showResults();


$profiler = new \Codon\Profiler();
$profiler
	->set('showOutput', false)
    ->add([
        'name' => 'Count in loop',
        'iterations' => 100,
        'function' => function() use ($data, &$profiler) {

            // This is the code we are profiling

			$profiler->markMemoryUsage('Start of loop');
			$profiler->startTimer('Loop only');
            for($i = 0; $i < count($data); $i++) {

				if($i === 500) {
					$profiler->checkpoint('halfway');
					$profiler->markMemoryUsage('halfway');
				}

                echo $data[$i] . "\n";
            }
			$profiler->endTimer('Loop only');
        }
    ])
	->run()
	->showResults();


$profiler->add(/*...*/)->run();
// something
$profiler->clear()->run(); # Re-run the tests


$profiler->add(/*...*/)->run();
$profiler->clearAll();
$profiler->add(/*...*/)->run();