-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96d8640
commit c6a6a36
Showing
9 changed files
with
181 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/** | ||
* @license MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Heavyrain\Executor; | ||
|
||
use Closure; | ||
use Heavyrain\Contracts\CancellationTokenInterface; | ||
use Heavyrain\Contracts\ExecutorInterface; | ||
use Heavyrain\HttpClient\ClientFactory; | ||
use Heavyrain\HttpClient\HttpProfiler; | ||
use Heavyrain\HttpClient\RequestException; | ||
use Throwable; | ||
|
||
use function Amp\async; | ||
|
||
/** | ||
* Executes asynchronized with amphp | ||
*/ | ||
class AmphpExecutor implements ExecutorInterface | ||
{ | ||
public function __construct( | ||
private readonly Closure $scenarioFunction, | ||
private readonly ClientFactory $factory, | ||
private readonly HttpProfiler $profiler, | ||
private readonly int $userCount, | ||
) { | ||
} | ||
|
||
public function execute(CancellationTokenInterface $token): iterable | ||
{ | ||
$scenarioFunc = $this->scenarioFunction; | ||
$factory = $this->factory; | ||
$profiler = $this->profiler; | ||
|
||
for ($i = 0; $i < $this->userCount; $i++) { | ||
// TODO: ramp-up users | ||
yield async(static function () use ($token, $scenarioFunc, $factory, $profiler): void { | ||
while (true) { | ||
try { | ||
$scenarioFunc($factory->create($token)); | ||
} catch (RequestException $e) { | ||
// do nothing because Heavyrain\HttpClient\RequestException was handled in AmphpClient | ||
} catch (Throwable $e) { | ||
$profiler->profileUncaughtException($e); | ||
} | ||
\sleep(1); // sleep 1 second to prevent CPU overload | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/** | ||
* @license MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Heavyrain\Executor; | ||
|
||
use Closure; | ||
use Heavyrain\Contracts\CancellationTokenInterface; | ||
use Heavyrain\Contracts\ExecutorInterface; | ||
use Heavyrain\HttpClient\ClientFactory; | ||
use Heavyrain\HttpClient\HttpProfiler; | ||
use Heavyrain\HttpClient\RequestException; | ||
use Throwable; | ||
|
||
/** | ||
* Simply executes synchronized once | ||
*/ | ||
class OnceExecutor implements ExecutorInterface | ||
{ | ||
public function __construct( | ||
private readonly Closure $scenarioFunction, | ||
private readonly ClientFactory $factory, | ||
private readonly HttpProfiler $profiler, | ||
) { | ||
} | ||
|
||
public function execute(CancellationTokenInterface $token): iterable | ||
{ | ||
try { | ||
yield ($this->scenarioFunction)($this->factory->create($token)); | ||
} catch (RequestException $e) { | ||
// do nothing because Heavyrain\HttpClient\RequestException was handled in AmphpClient | ||
} catch (Throwable $e) { | ||
$this->profiler->profileUncaughtException($e); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.