vitoresis / extend-benchmark
此包已被弃用且不再维护。没有推荐替代包。
适用于 PHP 8.2+ 的简单基准测试
1.0.5
2024-01-02 15:53 UTC
Requires
- php: >=8.2
- vitorsreis/extend-caller: ^1.0
Requires (Dev)
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^3.8
README
使用这个简单优雅的基准测试,您可以一次创建多个测试,并实时输出测试结果。单元测试已通过版本:8.2
和 8.3
安装
composer require vitorsreis/extend-benchmark
用法
require_once __DIR__ . '/vendor/autoload.php'; use VSR\Extend\Benchmark; // 1. Benchmark agent $agent = new Benchmark( 'My benchmark tests', // [optional] 'My Comment', // [optional] Printer, if not informed, will be used the default printers ( Printer/Console | Printer/Html ) ); // 2. Create benchmarks $benchmark1 = $agent ->createBenchmark( 'My Benchmark 1', // [optional] 'Test 1 Comment', // [optional] <iterations>, force number of interactions for this test ); // 3. Add tests $benchmark1->addTest( 'Test 1', [ 'return' => 'RESULT1-OK' ], // Expected result "RESULT1-OK" fn() => 'RESULT1-OK', // Callback 1 ); $benchmark1->addTest( 'Test 2', [ 'return' => 'RESULT2-OK' ], // Expected result "RESULT1-OK" fn() => 'RESULT2', // Callback 1 - returns "RESULT2" fn($__partial) => "$__partial[return]-OK", // Callback 2 - Merge with callback 1, returns "RESULT2-OK" ); // 4. Execute benchmarks and tests $agent->execute( // [optional, default 1] <iterations>, number of interactions for all tests );
• 预期测试类型
• null // Use null to any result • array( // or array of expected results, if omitted, the test will be considered successful "skipped" => string, // [optional] custom skipped message print "type" => string, // [optional] output|throw|skipped or return value type "return" => mixed, // [optional] accept callback return value "output" => null, // [optional] not accept callback output "output" => string, // [optional] accept callback output "throw" => null, // [optional] not accept callback throw "throw" => string, // [optional] accept callback throw class "throw" => array( // [optional] accept callback throw by class, message, code, file and line "class" => string, # [optional] accept callback throw class "message" => string, # [optional] accept callback throw message "code" => int, # [optional] accept callback throw code "file" => string, # [optional] accept callback throw file "line" => int # [optional] accept callback throw line ) )
• 当检测到调用时,自动填充两个魔法参数。
// Current interaction number $__interaction = int // Partial test returns $__partial = array( "type" => string, // pending|output|throw|skipped or return value type "return" => mixed, "output" => null|string, "throw" => null|array( "class" => string, "message" => string, "code" => int, "line" => int, "file" => string ) )
如果需要,可以将参数添加到映射中,以调用 "__construct" 和方法。智能填充会递归地查找所需参数并填充它们,无论位置如何,因此不会出现参数溢出。
$benchmark = $agent->createBenchmark('Test 1'); // by methods $benchmark->argsByMethods([ 'arg1' => 'value1', 'arg2' => 'value2', 'arg3' => 'value3' // ignored ]); $benchmark->addTest( '<title>', '<expected result>', // Expected result fn ($arg1, $__interaction, $arg2, $__partial) => ..., // arrow function )
支持的回调
$benchmark->test( '<title>', '<expected result>', # Supported callbacks ↓↓↓ "strlen", // native function name "myFunction", // function name function () { ... }, // anonymous function fn () => ..., // arrow function "MyClass::myMethod", // class method, "__construct" will be called before the method [ MyClass::class, "myMethod" ], # class method, "__construct" will be called before the method "MyClass::myStaticMethod", // class static method [ MyClass::class, "myStaticMethod" ], # class static method [ new MyClass(), "myMethod" ], // object, myMethod [ $myObject, "myMethod" ], # object, myMethod new MyClass(), # object, __invoke $myObject, # object, __invoke [ new MyClass(), "myStaticMethod" ], // object, myStaticMethod [ $myObject, "myStaticMethod" ], # object, myStaticMethod );