Skip to content

Commit a5fd0a4

Browse files
committed
Timeout
1 parent 557328c commit a5fd0a4

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

lib/Extension/Core/CoreExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PhpBench\Pipeline\Extension\Core\Stage\Aggregator\DescribeAggregator;
1515
use PhpBench\Pipeline\Extension\Core\Stage\Parameter\CounterParameter;
1616
use PhpBench\Pipeline\Extension\Core\Stage\Valve\DelayValve;
17+
use PhpBench\Pipeline\Extension\Core\Stage\Valve\TimeoutValve;
1718

1819
class CoreExtension implements PipelineExtension
1920
{
@@ -35,6 +36,7 @@ public function __construct()
3536
'sampler/curl' => new CurlSampler(),
3637
'valve/delay' => new DelayValve(),
3738
'valve/take' => new TakeValve(),
39+
'valve/timeout' => new TimeoutValve(),
3840
];
3941
}
4042

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace PhpBench\Pipeline\Extension\Core\Stage\Valve;
4+
5+
use PhpBench\Pipeline\Core\Stage;
6+
use Generator;
7+
use PhpBench\Pipeline\Core\Schema;
8+
9+
class TimeoutValve implements Stage
10+
{
11+
public function __invoke(): Generator
12+
{
13+
list($config, $data) = yield;
14+
15+
$runningTime = 0;
16+
$start = microtime(true);
17+
while ($runningTime < $config['time']) {
18+
list($config, $data) = yield $data;
19+
$runningTime = (microtime(true) - $start) * 1E6;
20+
}
21+
}
22+
23+
public function configure(Schema $schema)
24+
{
25+
$schema->setDefaults([
26+
'time' => 1E6,
27+
]);
28+
}
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace PhpBench\Pipeline\Tests\Unit\Extension\Core\Stage\Valve;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use PhpBench\Pipeline\Tests\Unit\Extension\Core\CoreTestCase;
7+
8+
class TimeoutValveTest extends CoreTestCase
9+
{
10+
public function testTerminatesAfterAGivenNumberOfMicroseconds()
11+
{
12+
$start = microtime(true);
13+
$this->pipeline()
14+
->stage('valve/timeout', [ 'time' => 100000 ])
15+
->run();
16+
17+
$time = (microtime(true) - $start) * 1E6;
18+
19+
$this->assertGreaterThanOrEqual(100000, $time);
20+
}
21+
}

0 commit comments

Comments
 (0)