Skip to content

Commit

Permalink
Merge branch 'feature/run_information' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanhoekelen committed Jun 28, 2017
2 parents 50c416e + 9082674 commit 01074e7
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 5 deletions.
Binary file added assets/raw/config-run-information.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions examples/10_config_run_information.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* Require Performance
*/

require_once('../vendor/autoload.php');
use Performance\Performance;
use Performance\Config;

/*
* Set config item
*/
Config::setConsoleLive(true);
Config::setRunInformation(true);

/*
* One simply performance check
*/
Performance::point();

// Run task A
for($x = 0; $x < 100; $x++)
{
echo ".";
usleep(3000);
}

// Finish all tasks and show test results
Performance::results();
13 changes: 13 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ public static function setPointLabelNice($status)
self::$config->setPointLabelNice($status);
}

/*
* Set config point label nice
* @param bool $status
* return void
*/
public static function setRunInformation($status)
{
if( ! self::enableTool())
return;

self::$config->setRunInformation($status);
}

/*
* Reset
*/
Expand Down
20 changes: 20 additions & 0 deletions src/Lib/Handlers/ConfigHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ConfigHandler implements ExportInterface
private $pointLabelLTrim = false;
private $pointLabelRTrim = false;
private $pointLabelNice = false;
private $runInformation = false;
private $presenter;

/*
Expand Down Expand Up @@ -238,6 +239,25 @@ public function setPointLabelNice($pointLabelNice)
$this->pointLabelNice = (bool) $pointLabelNice;
}

/**
* Set run information
* @param bool $status
*/
public function setRunInformation($status)
{
$this->runInformation = (bool) $status;
}

/**
* @return bool
*/
public function isRunInformation()
{
return $this->runInformation;
}





}
66 changes: 66 additions & 0 deletions src/Lib/Holders/InformationHolder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php namespace Performance\Lib\Holders;


use Performance\Lib\Handlers\ConfigHandler;

class InformationHolder
{
// Config
private $config;

// Run information holder
private $currentUser;
private $currentProcessId;

public function __construct(ConfigHandler $config)
{
$this->config = $config;

// Set information
$this->activateConfig();
}

/**
* @return mixed
*/
public function getCurrentUser()
{
return $this->currentUser;
}

/**
* @return mixed
*/
public function getCurrentProcessId()
{
return $this->currentProcessId;
}

//
// Private
//

private function activateConfig()
{
if($this->config->isRunInformation())
$this->setRunInformation();
}

private function setRunInformation()
{
// Set unknown
$this->currentUser = '?';
$this->currentProcessId = '?';


// Set current user
try{
$this->currentUser = get_current_user();
}catch (\ErrorException $exception) {}

// Set current user
try{
$this->currentProcessId = getmypid();
}catch (\ErrorException $exception) {}
}
}
9 changes: 8 additions & 1 deletion src/Lib/Presenters/ConsolePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ private function printStartUp()
. " " . terminal_style(' PHP PERFORMANCE TOOL ', null, 'gray') . $queryLogIndication . $liveIndication . PHP_EOL
. " Created by B. van Hoekelen version " . PerformanceHandler::VERSION . " PHP v" . phpversion() . PHP_EOL
. " Max memory " . ini_get("memory_limit") . ", max execution time " . $textExecutionTime . " on " . date('Y-m-d H:i:s') . PHP_EOL
. PHP_EOL);
);

// Print run information
if($this->config->isRunInformation())
$this->liveOrStack(" Run by user " . $this->information->getCurrentUser() . " on process id " . $this->information->getCurrentProcessId() . PHP_EOL);

// Set new line
$this->liveOrStack(PHP_EOL);

// Print head
$this->printHeadLine();
Expand Down
4 changes: 3 additions & 1 deletion src/Lib/Presenters/Presenter.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Performance\Lib\Presenters;

use Performance\Lib\Handlers\ConfigHandler;
use Performance\Lib\Holders\InformationHolder;
use Performance\Lib\Point;

abstract class Presenter {
Expand All @@ -14,13 +15,14 @@ abstract class Presenter {
protected $formatter;
protected $calculate;
protected $pointStack;
protected $information;

public function __construct(ConfigHandler $config)
{
$this->config = $config;
$this->formatter = new Formatter();
$this->calculate = new Calculate();

$this->information = new InformationHolder($config);

// Choose display format
$this->bootstrap();
Expand Down
9 changes: 6 additions & 3 deletions src/Lib/Presenters/WebPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@ private function displayForWebAsHtml()

echo '<div class="table-more-info">Performance v' . PerformanceHandler::VERSION
. ' PHP v' . phpversion() . ' on ' . date('Y-m-d H:i:s')
. '<br>Calibrate point: '
. $this->formatter->timeToHuman($calibratePoint->getDifferenceTime())
. '</div>';
. '<br>Calibrate point: ' . $this->formatter->timeToHuman($calibratePoint->getDifferenceTime());

if($this->config->isRunInformation())
echo "<br> Run by user " . $this->information->getCurrentUser() . " on process id " . $this->information->getCurrentProcessId();

echo '</div>';
echo '</div>';
}
else
Expand Down
43 changes: 43 additions & 0 deletions tests/Unit/T10A_ConfigRunInformationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php namespace Tests\Unit;

use Performance\Performance;
use Performance\Config;

class T10A_ConfigRunInformationTest extends \PHPUnit_Framework_TestCase
{

protected function setTestUp()
{
Performance::instanceReset();
}

public function testConfigRunInformation()
{
$this->setTestUp();

// Set config
Config::setRunInformation(true);

// Run test tasks
$this->synchronizeTaskARun();

// Finish all tasks and show test results
Performance::results();
}

// Create task

private function synchronizeTaskARun()
{
// Set point Task A
Performance::point(__FUNCTION__);

//
// Run code
usleep(2000);
//

// Finish point Task C
Performance::finish();
}
}
13 changes: 13 additions & 0 deletions tests/Unit/T10B_ConfigRunInformationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php namespace Tests\Unit;

use Performance\Config;
use Performance\Performance;

class T10B_ConfigRunInformationTest extends T10A_ConfigRunInformationTest
{
protected function setTestUp()
{
Performance::instanceReset();
Config::setPresenter('web');
}
}

0 comments on commit 01074e7

Please sign in to comment.