I found this forum when I was having the same problem measuring my functions, requisitions, instantiations and everything. Seeking to find a simple function or class saw in the answers above good ideas, and from them, I could make some improvements.
So I created a class that follows the idea of jquery Quinit, is very simple, but meets the minimum of measure runtime, the number of interactions and a assert true and false.
If you are familiar with the Qunit javascript tool, you will have no difficulty using this php class.
<?php
#Tester.php
header ( 'Content-Type: text/html; charset=utf-8' );
error_reporting ( E_ALL ^ E_NOTICE );
error_reporting ( E_ALL );
ini_set ( 'error_reporting', E_ALL );
ini_set ( 'display_startup_errors', TRUE );
ini_set ( "display_errors", TRUE );
ini_set ( "default_charset", TRUE );
class Tester
{
private static $offset = 0.000007;
private static $success = array ( "#0A0", "rgba(0,190,0,0.1)" );
private static $error = array ( "#C00", "rgba(190,0,0,0.1)" );
private static $status = false;
private static $name = null;
private static $msg = null;
private static $repeat = 1;
private static $timeOfTest = 0;
private static $timeOfEachTest = 0;
private static function reset ( )
{
self::$status = false;
self::$name = null;
self::$msg = null;
self::$repeat = 1;
self::$timeOfTest = 0;
self::$timeOfEachTest = 0;
}
private static function assert ( )
{
return "Tester";
}
public static function ok ( bool $status = false, string $msg = null ): bool
{
self::$msg = $msg;
return self::$status = $status;
}
private static function inner ( )
{
$set = ( self::$status !== false ) ? self::$success : self::$error;
$title = '<span><b>'.self::$name.' </b><small>'.round ( self::$timeOfEachTest, 6 ).'ms <sub>( x'.self::$repeat.' )</sub></small> </span>';
$time = '<span style="float: right;text-align: right; rigth: 0;"><b>Tempo total: '.round ( ( self::$timeOfTest / 1000 ), 2 ).'s <small> ( '. round ( self::$timeOfTest, 4 ).'ms )</b></small></span>';
$msg = '<div style="font-size: 10pt;">'.self::$msg.'</div>';
echo '<div style="font-family: verdana, sans-serif;font-size: 10pt; display: block; border: solid 1px '.$set [ 0 ].'; background-color: '.$set [ 1 ].'; padding: 5px; margin: 2px 0;"><div style="display: block; margin-bottom: 4px; border-bottom: solid 1px '.$set [ 1 ].'; padding-bottom: 3px; width:100%;">'.$title.$time.'</div>'.$msg.'</div>';
}
private static function sum ( array $array = null ): float
{
return array_reduce ( $array, function ( $previous, $item ) {
return $previous += $item;
} );
}
public static function on ( string $name = null, Closure $fn = null, int $repeat = 1 )
{
self::reset ( );
if ( is_string ( $name ) && $fn instanceof Closure && is_numeric ( self::$repeat ) ) {
self::$name = $name;
self::$repeat = $repeat;
$timeOfFunctions = array ( );
$init = microtime ( 1 );
for ( $i = 0; $i < self::$repeat; $i++ ) {
$time = microtime ( 1 );
$fn ( self::assert ( ) );
array_push ( $timeOfFunctions, ( ( microtime ( 1 ) - $time ) * 1000 ) );
};
$timeOfExec = ( ( ( microtime ( 1 ) - $init ) - self::$offset ) * 1000 );
$timeOfEachExec = ( $timeOfExec / self::$repeat );
$totalOfFunctions = self::sum ( $timeOfFunctions );
$timeOfEachFunction = ( $totalOfFunctions / count ( $timeOfFunctions ) );
self::$timeOfTest = ( ( $timeOfExec + $totalOfFunctions ) / 2 );
self::$timeOfEachTest = ( ( $timeOfEachExec + $timeOfEachFunction ) / 2 );
} else {
self::$name = ( empty ( self::$name ) ) ? "Error!" : self::$name;
self::$msg = " Erro de Sintaxe. Favor verificar os argumentos de entrada do teste.";
};
self::inner ( );
}
}
#Tester::on ( "test 1", function ( $assert ) { $assert::ok ( true, "msg" ); }, 1000 );
The Use is simple, because I chose to start the whole class with static methods, I avoided creating the concrete instance of the class in order not to interfere in the performance, in case there are many tests, but this is optional if using the instantiation of the class in the Singleton standard. Follow a Usage Template below.
Tester::on ( "nome do teste", function ( $assert ) {
$assert::ok ( true, "descrição do teste" );
}, 1 );
My recommendations for use are:
- The class is simple, which justifies its use in simple cases.
- The right way to use the class is to create a unit test file, never use the class in the software files.
- The file of this class must be the first to be included in the test file as it contains the header function, which can cause errors if it does not do so
- The static method ::on ( ) is responsible for initiating the test, which makes it mandatory.
- The method ::on ( ) follows the following convention:
::on ( string $nomeDoTeste, function $funcaoDeTeste, number $numeroDeIterações )
. So the first argument is a string which specifies the test name, the second argument is a anonymous function which validates the test and the third argument is number the same test is run.
- The anonymous function receives an "assert" parameter which is the class Tester, this in turn makes available the static aid ::okay ( ).
The static method ::okay ( ) is responsible for validating the test and describing the action, so it gets two arguments, the first must be of the bool type, which certifies that the test has passed and the second of the string type which describes the action tested as below.
$assert::ok ( true, "descrição do teste" );
I suggest Xhprof! http://br1.php.net/xhprof Take a look here too: http://xhprof.io/
– Marcelo Da Silva Santos