diff --git a/README.md b/README.md index 5dd1b53..88ae998 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,9 @@ Or manually update `require-dev` block of `composer.json` and run `composer upda ### Quick Start ```php -use DragonCode\Benchmark\Benchmark; +use function DragonCode\Benchmark\bench; -new Benchmark() +bench() ->compare( static fn () => true, static fn () => true @@ -49,6 +49,56 @@ new Benchmark() ### How To Use +You can use both the `bench()` function and the `Benchmark` class. + +```php +use DragonCode\Benchmark\Benchmark; + +use function DragonCode\Benchmark\bench; + +bench()->compare([ + fn () => /* some code */, + fn () => /* some code */, +])->toConsole(); + +new Benchmark()->compare([ + fn () => /* some code */, + fn () => /* some code */, +])->toConsole(); +``` + +#### As a function + +```php +use function DragonCode\Benchmark\bench; + +// Array without named keys +bench()->compare([ + fn () => /* some code */, + fn () => /* some code */, +])->toConsole(); + +// Array with named keys +bench()->compare([ + 'foo' => fn () => /* some code */, + 'bar' => fn () => /* some code */, +])->toConsole(); + +// Callbacks without named parameters +bench()->compare( + fn () => /* some code */, + fn () => /* some code */, +)->toConsole(); + +// Callbacks with named parameters +bench()->compare( + foo: fn () => /* some code */, + bar: fn () => /* some code */, +)->toConsole(); +``` + +#### As a class + ```php use DragonCode\Benchmark\Benchmark; diff --git a/composer.json b/composer.json index 5bc55da..712eb61 100644 --- a/composer.json +++ b/composer.json @@ -49,7 +49,8 @@ "autoload": { "psr-4": { "DragonCode\\Benchmark\\": "src/" - } + }, + "files": ["src/helpers.php"] }, "autoload-dev": { "psr-4": { diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 0000000..4d8e19a --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,12 @@ +toBeInstanceOf(Benchmark::class); }); @@ -15,5 +17,5 @@ }); test('helper', function () { - expect(benchmark())->toBeInstanceOf(Benchmark::class); + expect(bench())->toBeInstanceOf(Benchmark::class); }); diff --git a/tests/Unit/Result/HelperTest.php b/tests/Unit/Result/HelperTest.php new file mode 100644 index 0000000..c08a80c --- /dev/null +++ b/tests/Unit/Result/HelperTest.php @@ -0,0 +1,22 @@ +compare(fn () => true) + ->toData(); + + expectOutputToMatchSnapshot(); +}); + +test('deviations', function () { + bench() + ->deviations() + ->compare(fn () => true) + ->toData(); + + expectOutputToMatchSnapshot(); +});