Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ $factory->lowLevelPool(); // Returns a low level pool that will scale infinitely
$factory->limitedPool(12); // Returns a limited pool with a maximum number of threads specified by you
```

## Metrics

This package supports metrics through [`wyrihaximus/metrics`](https://github.com/wyrihaximus/php-metrics):

```php
use React\EventLoop\Factory as EventLoopFactory;
use ReactParallel\Factory;
use ReactParallel\Metrics;
use WyriHaximus\Metrics\Configuration;
use WyriHaximus\Metrics\InMemory\Registry;

$loop = EventLoopFactory::create();
$registry = new Registry(Configuration::create());
$factory = (new Factory($loop))->withMetrics(Metrics::create($registry));
```

## License ##

Copyright 2020 [Cees-Jan Kiewiet](http://wyrihaximus.net/)
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
"php": "^7.4",
"ext-parallel": "*",
"react-parallel/contracts": "^1.0",
"react-parallel/event-loop": "^1.0",
"react-parallel/infinite-pool": "^2.0",
"react-parallel/event-loop": "^1.1",
"react-parallel/infinite-pool": "^2.1",
"react-parallel/limited-pool": "^1.0",
"react-parallel/runtime": "^2.0",
"react-parallel/streams": "^1.0",
"react/event-loop": "^1.1",
"react/promise": "^2.8",
"wyrihaximus/constants": "^1.5"
"wyrihaximus/constants": "^1.5",
"wyrihaximus/metrics": "^1"
},
"require-dev": {
"moneyphp/money": "^3.3",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"text": "infection.log"
},
"mutators": {
"@default": true
"@default": true,
"CloneRemoval": false,
"InstanceOf_": false
}
}
15 changes: 15 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
final class Factory
{
private LoopInterface $loop;
private ?Metrics $metrics = null;
private ?EventLoopBridge $eventLoopBridge = null;
private ?LowLevelPoolInterface $infinitePool = null;
private ?StreamsFactory $streamsFactory = null;
Expand All @@ -27,6 +28,14 @@ public function __construct(LoopInterface $loop)
$this->loop = $loop;
}

public function withMetrics(Metrics $metrics): self
{
$self = clone $this;
$self->metrics = $metrics;

return $self;
}

public function loop(): LoopInterface
{
return $this->loop;
Expand All @@ -36,6 +45,9 @@ public function eventLoopBridge(): EventLoopBridge
{
if ($this->eventLoopBridge === null) {
$this->eventLoopBridge = new EventLoopBridge($this->loop);
if ($this->metrics instanceof Metrics) {
$this->eventLoopBridge = $this->eventLoopBridge->withMetrics($this->metrics->eventLoop());
}
}

return $this->eventLoopBridge;
Expand All @@ -62,6 +74,9 @@ public function lowLevelPool(): LowLevelPoolInterface
{
if ($this->infinitePool === null) {
$this->infinitePool = new Infinite($this->loop, $this->eventLoopBridge(), ONE);
if ($this->metrics instanceof Metrics) {
$this->infinitePool = $this->infinitePool->withMetrics($this->metrics->infinitePool());
}
}

return $this->infinitePool;
Expand Down
39 changes: 39 additions & 0 deletions src/Metrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace ReactParallel;

use ReactParallel\EventLoop\Metrics as EventLoopMetrics;
use ReactParallel\Pool\Infinite\Metrics as InfinitePoolMetrics;
use WyriHaximus\Metrics\Registry;

final class Metrics
{
private EventLoopMetrics $eventLoop;
private InfinitePoolMetrics $infinitePool;

public function __construct(EventLoopMetrics $eventLoop, InfinitePoolMetrics $infinitePool)
{
$this->eventLoop = $eventLoop;
$this->infinitePool = $infinitePool;
}

public static function create(Registry $registry): self
{
return new self(
EventLoopMetrics::create($registry),
InfinitePoolMetrics::create($registry),
);
}

public function eventLoop(): EventLoopMetrics
{
return $this->eventLoop;
}

public function infinitePool(): InfinitePoolMetrics
{
return $this->infinitePool;
}
}
12 changes: 7 additions & 5 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use parallel\Future;
use React\EventLoop\Factory as EventLoopFactory;
use ReactParallel\Factory;
use ReactParallel\Metrics;
use WyriHaximus\AsyncTestUtilities\AsyncTestCase;
use WyriHaximus\Metrics\Factory as MetricsFactory;
use function parallel\run;

final class FactoryTest extends AsyncTestCase
Expand All @@ -16,7 +18,7 @@ final class FactoryTest extends AsyncTestCase
*/
public function eventLoopBridge(): void
{
$factory = new Factory(EventLoopFactory::create());
$factory = (new Factory(EventLoopFactory::create()))->withMetrics(Metrics::create(MetricsFactory::create()));

$future = run(function (int $a, int $b): int {
return $a * $b;
Expand All @@ -31,7 +33,7 @@ public function eventLoopBridge(): void
*/
public function streams(): void
{
$factory = new Factory(EventLoopFactory::create());
$factory = (new Factory(EventLoopFactory::create()))->withMetrics(Metrics::create(MetricsFactory::create()));

$time = time();
$channel = new Channel(Channel::Infinite);
Expand All @@ -45,7 +47,7 @@ public function streams(): void
*/
public function call(): void
{
$factory = new Factory(EventLoopFactory::create());
$factory = (new Factory(EventLoopFactory::create()))->withMetrics(Metrics::create(MetricsFactory::create()));

self::assertSame(666, $this->await($factory->call(function (int $a, int $b): int {
return $a * $b;
Expand All @@ -57,7 +59,7 @@ public function call(): void
*/
public function lowLevelPool(): void
{
$factory = new Factory(EventLoopFactory::create());
$factory = (new Factory(EventLoopFactory::create()))->withMetrics(Metrics::create(MetricsFactory::create()));

$pool = $factory->lowLevelPool();
self::assertSame(666, $this->await($pool->run(function (int $a, int $b): int {
Expand All @@ -71,7 +73,7 @@ public function lowLevelPool(): void
*/
public function limitedPool(): void
{
$factory = new Factory(EventLoopFactory::create());
$factory = (new Factory(EventLoopFactory::create()))->withMetrics(Metrics::create(MetricsFactory::create()));

$pool = $factory->limitedPool(1);
self::assertSame(666, $this->await($pool->run(function (int $a, int $b): int {
Expand Down