Scripts

PHPbench

Op het forum worden er nogal wat vragen gesteld over snelheden van bepaalde code's. Wat is sneller ... of ...? En dat soort dingen. Nu ben ik vaak de gene geweest die dan een testje maakte en de code testte. Het probleem is alleen dat andere niet weten wat je test en dat ik de hele tijd alle code opnieuw schreef. Tijd om een PHPbench script te maken. Hiermee kan iedereen heel simpel scripts testen en daarmee de tijden vergelijken. Op dit forum plaats ik alleen de classes, deze zul je dan zelf moeten integreren. Ik heb de volledige libary geplaatst op github. Kijk voor de nieuwste en volledige code altijd op github . Wegens problemen met GitHub pages kan ik de documentatie nog niet in website vorm weergeven. De documentatie moet dus helaas uitgesteld worden, gelukkig bevatten alle 3 de classes uitgebreide comments.

timer.php
<?php

/**
 * Timer
 * 
 * The basic timer function
 */
class Timer
{
	/**
	 * All markers with the times
	 *
	 * @access protected
	 * @var Array
	 * @static
	 */
	protected static $markers = Array();

	/**
	 * A shortcut for start marker
	 *
	 * @return void
	 * @see Timer::setMarker()
	 * @static
	 */
	public static function start()
	{
		self::setMarker('start');
	}

	/**
	 * A shortcut for end marker
	 *
	 * @return void
	 * @see Timer::setMarker()
	 * @static
	 */
	public static function end()
	{
		self::setMarker('end');
	}

	/**
	 * Sets a time marker in the script
	 *
	 * @param string $name The name of the marker
	 * @return void
	 * @static
	 */
	public static function setMarker( $name )
	{
		self::$markers[$name] = microtime(true);
	}

	/**
	 * A shortcut for getDiff( 'start', 'end' )
	 *
	 * @return int The difference between start and end
	 * @see Timer::getDiff()
	 * @static
	 */
	public static function getResult()
	{
		return self::getDiff('end', 'start');
	}

	/**
	 * Get the difference between 2 markers
	 *
	 * @param string $marker1 The name of the first marker
	 * @param string $marker2 The name of the second marker
	 * @return int The difference between the times
	 * @static
	 */
	public static function getDiff( $marker1, $marker2 )
	{
		if( !isset(self::$markers[$marker1]) )
			throw new InvalidArgumentException('The Marker('.$marker1.' has not been set');
		if( !isset(self::$markers[$marker2]) )
			throw new InvalidArgumentException('The Marker('.$marker2.' has not been set');


		$diff = (self::$markers[$marker1] - self::$markers[$marker2]);

		if( $diff < 0 )
			$diff *= -1;

		return $diff;
	}
}
research.php
<?php

/**
 * Research Object
 *
 * This object contains one research and it runs some tests.
 *
 * @author Wouter J
 * @since Version 1.0
 */
class Research
{
	/**
	 * All tests in this research
	 *
	 * @access protected
	 * @var Array
	 */
	protected $tests = Array();

	/**
	 * The name of the research
	 *
	 * @access protected
	 * @var String
	 */
	protected $name;

	/**
	 * The times results
	 *
	 * @access protected
	 * @var Array
	 */
	protected $times = Array();

	/**
	 * The percentage results
	 *
	 * @access protected
	 * @var Array
	 */
	protected $percentages = Array();

	/**
	 * How may times the test would repeated. Default: 1000
	 *
	 * @access protected
	 * @var Int
	 */
	protected $repeat = 1000;

	/**
	 * The constructor
	 *
	 * Set up the name of the research.
	 *
	 * @param string $name The name of the research
	 * @return void
	 */
	public function __construct( $name )
	{
		$this->name = (string) $name;
	}

	/**
	 * addTest
	 *
	 * With this method you can add tests to the research
	 *
	 * @param object $test The test object
	 * @return void
	 */
	public function addTest( Test $test )
	{
		$this->tests[$test->getName()] = $test;
	}

	/**
	 * setRepeat
	 *
	 * Set a number how much the test would repeated
	 *
	 * @param int $repeat The repeat count
	 * @return void
	 */
	public function setRepeat( $repeat )
	{
		$this->repeat = $repeat;
	}

	/**
	 * runTests
	 *
	 * Run all tests
	 *
	 * @return void
	 */
	public function runTests()
	{
		foreach( $this->tests as $name => $test )
		{
			ob_start();
			Timer::start();
			for( $i=0; $i < $this->repeat; $i++ )
			{
				Timer::setMarker('Test-'.$i.'-start');
				$test->run();
				Timer::setMarker('Test-'.$i.'-end');
			}
			Timer::end();
			$this->times[$name] = Timer::getResult();

			unset($time);
			ob_end_clean();
		}

		$this->countPercentages();
	}

	/**
	 * count percentages
	 *
	 * Count all percentages
	 *
	 * @return void
	 */
	protected function countPercentages()
	{
		$key = 100 / min($this->times);

		foreach( $this->times as $name => $time )
		{
			$this->percentages[$name] = round($key * $time);
		}
	}

	/**
	 * getResults
	 *
	 * Get the results
	 *
	 * @return array $result The times and percentages
	 */
	public function getResults()
	{
		$result = Array(
			'times' => Array(),
			'percentages' => Array()
		);

		foreach( $this->times as $name => $time )
		{
			$result['times'][$name] = $time / $this->repeat;
			$result['percentages'][$name] = $this->percentages[$name];
		}

		return $result;
	}

	/**
	 * getName
	 *
	 * Get the research name
	 * @retrurn string $name The name of the research
	 */
	public function getName()
	{
		return $this->name;
	}
}
test.php
<?php

/**
 * Test object
 *
 * This objects hold one single test and can run that test
 *
 * @author Wouter J
 * @since Version 1.0
 */
class Test
{
	/**
	 * The test that will run
	 *
	 * @access protected
	 * @var Function
	 */
	protected $test;

	/**
	 * The name of the test
	 *
	 * @access protected
	 * @var String
	 */
	protected $name;

	/**
	 * The constructor
	 *
	 * Set up a new Test. With a name and the function
	 *
	 * @param string $name The name of the test
	 * @param mixed $test The actual test
	 * @return void
	 */
	public function __construct( $name, $test )
	{
		$this->name = preg_replace_callback('/\s(\w)/', function( $matches ) {
			return strtoupper($matches[1]);
		}, (string) $name);

		if( is_callable($test) )
			$this->test = $test;
		else
		{
			$this->test = function() {
				$test;
			};
		}
	}

	/**
	 * GetName
	 *
	 * Get the name of the test
	 *
	 * @return string $name The name
	 */
	public function getName()
	{
		return $this->name;
	}

	/**
	 * Run
	 *
	 * Run the test
	 *
	 * @return void
	 */
	public function run()
	{
		$test = $this->test;
		return $test();
	}
}

Reacties

0
Nog geen reacties.