Skip to content

Commit

Permalink
StatisticsListener (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 authored and mrook committed Nov 3, 2017
1 parent 09e33dc commit 3e1511b
Show file tree
Hide file tree
Showing 16 changed files with 1,041 additions and 0 deletions.
150 changes: 150 additions & 0 deletions classes/phing/listener/StatisticsListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

include_once 'phing/SubBuildListener.php';
include_once 'phing/listener/statistics/StatisticsReport.php';
require_once 'phing/listener/statistics/DefaultClock.php';
require_once 'phing/listener/statistics/ProjectTimerMap.php';

/**
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
* @package phing.listener
*/
class StatisticsListener implements SubBuildListener
{
private static $BUILDEVENT_PROJECT_NAME_HAS_NULL_VALUE = true;

/** @var ProjectTimerMap $projectTimerMap */
protected $projectTimerMap;

/** @var Clock $clock */
private $clock;

/** @var StatisticsReport $statisticsReport */
private $statisticsReport;

public function __construct(Clock $clock = null)
{
$this->projectTimerMap = new ProjectTimerMap();
$this->statisticsReport = new StatisticsReport();
if ($clock === null) {
$this->clock = new DefaultClock();
} else {
$this->clock = $clock;
}
}

public function buildStarted(BuildEvent $buildEvent)
{
if (self::$BUILDEVENT_PROJECT_NAME_HAS_NULL_VALUE) {
$this->findInitialProjectTimer()->start();
}
}

public function buildFinished(BuildEvent $buildEvent)
{
$projectTimer = $this->findProjectTimer($buildEvent);
$this->updateDurationWithInitialProjectTimer($projectTimer);
$this->buildFinishedTimer($projectTimer);
$this->statisticsReport->write();
}

public function targetStarted(BuildEvent $buildEvent)
{
$this->findTargetTimer($buildEvent)->start();
}

public function targetFinished(BuildEvent $buildEvent)
{
$this->findTargetTimer($buildEvent)->finish();
}

public function taskStarted(BuildEvent $buildEvent)
{
$this->findTaskTimer($buildEvent)->start();
}

public function taskFinished(BuildEvent $buildEvent)
{
$this->findTaskTimer($buildEvent)->finish();
}

public function messageLogged(BuildEvent $buildEvent)
{
}

public function subBuildStarted(BuildEvent $buildEvent)
{
$this->findProjectTimer($buildEvent)->start();
}

public function subBuildFinished(BuildEvent $buildEvent)
{
$projectTimer = $this->findProjectTimer($buildEvent);
$this->buildFinishedTimer($projectTimer);
}

private function findProjectTimer(BuildEvent $buildEvent)
{
$project = $buildEvent->getProject();
return $this->projectTimerMap->find($project, $this->clock);
}

protected function findInitialProjectTimer()
{
return $this->projectTimerMap->find('', $this->clock);
}

/**
* @param BuildEvent $buildEvent
* @return SeriesTimer
*/
private function findTargetTimer(BuildEvent $buildEvent)
{
$projectTimer = $this->findProjectTimer($buildEvent);
$target = $buildEvent->getTarget();
$name = $target->getName();
return $projectTimer->getTargetTimer($name);
}

/**
* @param BuildEvent $buildEvent
* @return SeriesTimer
*/
private function findTaskTimer(BuildEvent $buildEvent)
{
$projectTimer = $this->findProjectTimer($buildEvent);
$task = $buildEvent->getTask();
$name = $task->getTaskName();
return $projectTimer->getTaskTimer($name);
}

private function buildFinishedTimer(ProjectTimer $projectTimer)
{
$projectTimer->finish();
$this->statisticsReport->push($projectTimer);
}

private function updateDurationWithInitialProjectTimer(ProjectTimer $projectTimer)
{
$rootProjectTimer = $this->findInitialProjectTimer();
$duration = $rootProjectTimer->getSeries()->current();
$projectTimer->getSeries()->add($duration);
}
}
30 changes: 30 additions & 0 deletions classes/phing/listener/statistics/Clock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

/**
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
* @package phing.listener.statistics
*/
interface Clock
{
/**
* @return int
*/
public function getCurrentTime();
}
35 changes: 35 additions & 0 deletions classes/phing/listener/statistics/DefaultClock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

include_once 'phing/listener/statistics/Clock.php';

/**
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
* @package phing.listener.statistics
*/
class DefaultClock implements Clock
{
/**
* @return int
*/
public function getCurrentTime()
{
return microtime(true);
}
}
44 changes: 44 additions & 0 deletions classes/phing/listener/statistics/Duration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

/**
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
* @package phing.listener.statistics
*/
class Duration
{
private $startTime;

private $finishTime;

public function setFinishTime($finishTime)
{
$this->finishTime = $finishTime;
}

public function setStartTime($startTime)
{
$this->startTime = $startTime;
}

public function getTime()
{
return $this->finishTime - $this->startTime;
}
}
61 changes: 61 additions & 0 deletions classes/phing/listener/statistics/ProjectTimer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

include_once 'phing/listener/statistics/StatsTimer.php';
include_once 'phing/listener/statistics/TimerMap.php';

/**
* A logger which logs nothing but build failure and what task might output.
*
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
* @package phing.listener.statistics
*/
class ProjectTimer extends StatsTimer
{
private $targetMap;

private $taskMap;

public function __construct($name, Clock $clock)
{
parent::__construct($name, $clock);
$this->targetMap = new TimerMap();
$this->taskMap = new TimerMap();
}

public function getTargetTimer($name)
{
return $this->targetMap->find($name, $this->clock);
}

public function getTaskTimer($name)
{
return $this->taskMap->find($name, $this->clock);
}

public function toTargetSeriesMap()
{
return $this->targetMap->toSeriesMap();
}

public function toTaskSeriesMap()
{
return $this->taskMap->toSeriesMap();
}
}
44 changes: 44 additions & 0 deletions classes/phing/listener/statistics/ProjectTimerMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/

require_once 'phing/listener/statistics/ProjectTimer.php';

/**
* @author Siad Ardroumli <siad.ardroumli@gmail.com>
* @package phing.listener.statistics
*/
class ProjectTimerMap extends TimerMap
{
public function get($project)
{
return parent::get($project->getName());
}

public function find($project, Clock $clock)
{
$name = $project instanceof Project ? $project->getName() : '';

return parent::find($name, $clock);
}

protected function createTimer($name, Clock $clock)
{
return new ProjectTimer($name, $clock);
}
}
Loading

0 comments on commit 3e1511b

Please sign in to comment.