Skip to content

Commit faa84a1

Browse files
committed
First working version
1 parent a96efd5 commit faa84a1

16 files changed

+586
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/composer.lock
2+
/vendor
3+
.phpunit.result.cache
4+
.idea

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "integer-net/deployer-timer",
3+
"description": "Collect duration of deployer tasks",
4+
"minimum-stability": "stable",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Fabian Schmengler",
9+
"email": "fs@integer-net.de"
10+
}
11+
],
12+
"require": {
13+
"roave/security-advisories": "dev-master",
14+
"deployer/deployer": "v6.4.3"
15+
},
16+
"require-dev": {
17+
"phpunit/phpunit": "8.0.5"
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"IntegerNet\\DeployerTimer\\": "src/"
22+
}
23+
}
24+
}

recipe/timer.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Deployer;
5+
6+
use Deployer\Task\Task;
7+
use IntegerNet\DeployerTimer\DecorateAllTasks;
8+
use IntegerNet\DeployerTimer\TimerTaskDecorator;
9+
10+
function timerCsv(string $fileName)
11+
{
12+
$deployer = Deployer::get();
13+
$decorateAllTasks = new DecorateAllTasks($deployer);
14+
$timer = new TimerTaskDecorator();
15+
$decorateAllTasks->with($timer);
16+
$tasks = $deployer->tasks->toArray();
17+
/** @var Task $lastTask */
18+
$lastTask = array_pop($tasks);
19+
$collectResultTask = uniqid('timer_result-', true);
20+
task(
21+
$collectResultTask, function() use ($timer, $fileName) {
22+
file_put_contents($fileName, $timer->resultsAsCsv());
23+
});
24+
after($lastTask->getName(), $collectResultTask);
25+
}

src/Clock.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\DeployerTimer;
5+
6+
interface Clock
7+
{
8+
public function microtime(): float;
9+
}

src/DecorateAllTasks.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\DeployerTimer;
5+
6+
use Deployer\Deployer;
7+
use Deployer\Task\Task;
8+
9+
class DecorateAllTasks
10+
{
11+
/**
12+
* @var Deployer
13+
*/
14+
private $deployer;
15+
16+
public function __construct(Deployer $deployer)
17+
{
18+
$this->deployer = $deployer;
19+
}
20+
21+
public function with(TaskDecorator $decorator): void
22+
{
23+
foreach ($this->deployer->tasks as $taskName => $task) {
24+
$beforeName = $taskName . '.' . $decorator->name() . '.before';
25+
$afterName = $taskName . '.' . $decorator->name() . '.after';
26+
$this->deployer->tasks->set($beforeName, (new Task($beforeName, $decorator->callbackBefore($taskName)))->shallow());
27+
$this->deployer->tasks->set($afterName, (new Task($afterName, $decorator->callbackAfter($taskName)))->shallow());
28+
if (!in_array($task->getName(), [$beforeName, $afterName], true)) {
29+
$task->addBefore($beforeName);
30+
$task->addAfter($afterName);
31+
}
32+
}
33+
}
34+
}

src/FakeClock.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\DeployerTimer;
5+
6+
class FakeClock implements Clock
7+
{
8+
/**
9+
* @var float
10+
*/
11+
private $timestamp;
12+
13+
public function __construct(float $timestamp)
14+
{
15+
$this->timestamp = $timestamp;
16+
}
17+
18+
public function microtime(): float
19+
{
20+
return $this->timestamp;
21+
}
22+
23+
public function advanceMs(int $ms): void
24+
{
25+
$this->timestamp += ($ms * 0.001);
26+
}
27+
28+
}

src/SystemClock.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\DeployerTimer;
5+
6+
class SystemClock implements Clock
7+
{
8+
public function microtime(): float
9+
{
10+
return \microtime(true);
11+
}
12+
}

src/TaskDecorator.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\DeployerTimer;
5+
6+
interface TaskDecorator
7+
{
8+
public function name(): string;
9+
10+
public function callbackBefore(string $taskName): callable;
11+
12+
public function callbackAfter(string $taskName): callable;
13+
}

src/TimerEvent.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\DeployerTimer;
5+
6+
class TimerEvent
7+
{
8+
public const TYPE_BEGIN = 'BEGIN';
9+
public const TYPE_END = 'END';
10+
/**
11+
* @var string
12+
*/
13+
private $type;
14+
/**
15+
* @var string
16+
*/
17+
private $taskName;
18+
/**
19+
* @var float
20+
*/
21+
private $timestamp;
22+
/**
23+
* @var float
24+
*/
25+
private $duration;
26+
27+
public function __construct(string $type, string $taskName, float $timestamp, float $duration)
28+
{
29+
$this->type = $type;
30+
$this->taskName = $taskName;
31+
$this->timestamp = $timestamp;
32+
$this->duration = $duration;
33+
}
34+
35+
private function asArray()
36+
{
37+
return [$this->type, $this->taskName, round($this->timestamp, 3), round($this->duration, 3)];
38+
}
39+
40+
public function asCsvLine(): string
41+
{
42+
$f = fopen('php://memory', 'rb+');
43+
if (fputcsv($f, $this->asArray()) === false) {
44+
throw new \RuntimeException('Cannot format TimerEvent as CSV');
45+
}
46+
rewind($f);
47+
$line = stream_get_contents($f);
48+
fclose($f);
49+
return rtrim($line);
50+
}
51+
}

src/TimerEvents.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\DeployerTimer;
5+
6+
class TimerEvents
7+
{
8+
/**
9+
* @var TimerEvent[]
10+
*/
11+
private $items = [];
12+
13+
public function append(TimerEvent $event)
14+
{
15+
$this->items[] = $event;
16+
}
17+
18+
public function asCsv(): string
19+
{
20+
$lines = [];
21+
foreach ($this->items as $item) {
22+
$lines[] = $item->asCsvLine();
23+
}
24+
return implode("\n", $lines);
25+
}
26+
}

0 commit comments

Comments
 (0)