-
-
Notifications
You must be signed in to change notification settings - Fork 20
Add benchmarks, improve performance of Message::parse()
#132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b5467b0
0a198a0
8af5cb6
def8f5b
622d80e
48d34a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "runner.bootstrap": "vendor/autoload.php", | ||
| "runner.path": "tests/Benchmark", | ||
| "runner.revs": 1000, | ||
| "runner.iterations": 10, | ||
| "runner.retry_threshold": 2 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -206,6 +206,10 @@ private function parse(string|Stringable $message, array $context): string | |
| { | ||
| $message = (string) $message; | ||
|
|
||
| if (!str_contains($message, '{')) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there are no placeholders at all, which is quite common, do not run
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Has
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. |
||
| return $message; | ||
| } | ||
|
|
||
| /** @var string */ | ||
| return preg_replace_callback( | ||
| '/\{([\w\.\\\\_]+)\}/', | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Log\Tests\Benchmark; | ||
|
|
||
| use Psr\Log\LogLevel; | ||
| use Yiisoft\Log\Logger; | ||
| use Yiisoft\Log\Target; | ||
|
|
||
| final class LoggerBench | ||
| { | ||
| private Logger $logger; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->logger = new Logger([new class () extends Target { | ||
| protected function export(): void | ||
| { | ||
| // noop | ||
| } | ||
| }]); | ||
| } | ||
|
|
||
| public function benchLogSimple(): void | ||
| { | ||
| $this->logger->log(LogLevel::INFO, 'simple message'); | ||
| } | ||
|
|
||
| public function benchLogWithPlaceholder(): void | ||
| { | ||
| $this->logger->log(LogLevel::INFO, 'has {foo} placeholder', ['foo' => 'some']); | ||
| } | ||
|
|
||
| public function benchLogWithMultiplePlaceholders(): void | ||
| { | ||
| $this->logger->log( | ||
| LogLevel::INFO, | ||
| 'Placeholder 1: {p1} - Placeholder 2: {p2}', | ||
| ['p1' => 'hello', 'p2' => 'world'], | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Log\Tests\Benchmark; | ||
|
|
||
| use Psr\Log\LogLevel; | ||
| use Yiisoft\Log\Message; | ||
|
|
||
| final class MessageBench | ||
| { | ||
| public function benchNoPlaceholder(): void | ||
| { | ||
| new Message(LogLevel::INFO, 'no placeholder', ['foo' => 'some']); | ||
| } | ||
|
|
||
| public function benchSinglePlaceholderScalar(): void | ||
| { | ||
| new Message(LogLevel::INFO, 'has {foo} placeholder', ['foo' => 'some']); | ||
| } | ||
|
|
||
| public function benchPlaceholderMissing(): void | ||
| { | ||
| new Message(LogLevel::INFO, 'has {foo} placeholder', []); | ||
| } | ||
|
|
||
| public function benchPlaceholderNull(): void | ||
| { | ||
| new Message(LogLevel::INFO, 'has "{foo}" placeholder', ['foo' => null]); | ||
| } | ||
|
|
||
| public function benchPlaceholderArray(): void | ||
| { | ||
| new Message(LogLevel::INFO, 'has "{foo}" placeholder', ['foo' => ['bar' => 7]]); | ||
| } | ||
|
|
||
| public function benchNestedPlaceholder(): void | ||
| { | ||
| new Message(LogLevel::INFO, 'has "{foo.bar}" placeholder', ['foo' => ['bar' => 7]]); | ||
| } | ||
|
|
||
| public function benchDeeplyNestedPlaceholder(): void | ||
| { | ||
| new Message(LogLevel::INFO, 'has "{foo.bar.baz}" placeholder', ['foo' => ['bar' => ['baz' => 7]]]); | ||
| } | ||
|
|
||
| public function benchMultiplePlaceholders(): void | ||
| { | ||
| new Message( | ||
| LogLevel::INFO, | ||
| 'Placeholder 1: {p1} - Placeholder 2: {p2}', | ||
| ['p1' => 'hello', 'p2' => 'world'], | ||
| ); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.