Skip to content

Commit

Permalink
[Archive] 2012: php54 version
Browse files Browse the repository at this point in the history
  • Loading branch information
jgauthi committed Sep 13, 2020
1 parent 30b473c commit 14eb832
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 130 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"type": "library",
"homepage": "https://github.com/jgauthi/component_debug",
"require": {
"php": ">=4.0"
"php": ">=5.4",
"ext-mbstring": "*"
},
"autoload": {
"classmap": [ "src/" ]
"psr-4": { "Jgauthi\\Component\\Debug\\": "src/" }
},
"license": "GPL-3.0",
"authors": [
Expand Down
19 changes: 19 additions & 0 deletions example/timer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
use Jgauthi\Component\Debug\Timer;

// In this example, the vendor folder is located in "example/"
require_once __DIR__.'/vendor/autoload.php';

$time = new Timer;

$time->chapitre_debut('readfile Readme');
readfile(__DIR__.'/../readme.md');
//somecode...
$time->chapitre_fin('readfile Readme');

$time->chapitre_debut('Sleep 2s');
sleep(2);
$time->chapitre_fin('Sleep 2s');

$time->stop();
echo $time->outPut('html');
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Component Debug
Debug tools like: varExport* functions, timer, server dump, SQL Beautifier, etc.
Debug tools like: varExport* functions, timer, server dump, debug handler, SQL Beautifier, etc.

* [Class Timer](src/Timer.php) to evaluate time script and specific portion code.

## Prerequisite

Expand Down
241 changes: 241 additions & 0 deletions src/Timer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
<?php
/*******************************************************************************
* @name: Timer
* @note: Stopwatch in milliseconds script time+ the various steps in the code
* @author: Jgauthi <github.com/jgauthi>, created at [2mars2007]
* @version: 1.1
*******************************************************************************/

namespace Jgauthi\Component\Debug;

class Timer
{
protected $debut;
protected $fin;
protected $time = [];
protected $time_chap = [];
public $start = false;
public $header = 0;
public $resultat;
protected $etape;
protected $chapitre;

//-- CONSTRUCTEUR ---------------------------------
public function __construct($header = 0)
{
$this->debut = microtime();
$this->header = $header;
}

//-- ETALONNER LE PARCOURS DU SCRIPT --------------

/**
* @param $nom
* @return self
*/
public function etape($nom)
{
$this->time[] = ['nom' => $nom, 'time' => microtime()];

return $this;
}

/**
* @param $nom
* @return self
*/
public function chapitre_debut($nom)
{
$this->time_chap[$nom] = ['debut' => microtime()];

return $this;
}

/**
* @param $nom
*/
public function chapitre_fin($nom)
{
$this->time_chap[$nom]['fin'] = microtime();
}

//-- METTRE FIN AU COMPTEUR

public function stop()
{
$this->fin = microtime();

// Conversion
$this->debut = $this->_unit($this->debut);
$this->fin = $this->_unit($this->fin);

// Vérifie si le header est correcte
if ($this->header && headers_sent()) {
$this->header = false;
}

// Analyse des données
$this->resultat = $this->_conv($this->fin - $this->debut);

// Analyse des étapes
if (count($this->time) > 0) {
$this->etape = '';
foreach ($this->time as $id => $tmp) {
$calcul = $this->_vir($this->_conv($this->_unit($tmp['time']) - $this->debut));
$this->etape .= "- {$tmp['nom']}: $calcul ms\n";

if ($this->header) {
header("$id-Time: $calcul ms->{$tmp['nom']}");
}
}
}

// Analyse des chapitres
if (count($this->time_chap) > 0) {
$this->chapitre = '';
foreach ($this->time_chap as $id => $temp) {
$calcul = $this->_vir($this->_conv($this->_unit($temp['fin']) - $this->_unit($temp['debut'])));
$this->chapitre .= "- {$id}: $calcul ms\n";

if ($this->header) {
header("$id-Chap: $calcul ms");
}
}
}

// Renvoi des données
if ($this->header) {
header('X-Time: '.$this->_vir($this->resultat).' ms');
}
}

/**
* @param null $type
* @return string
*/
public function OutPut($type = null)
{
$output = '';

// Organiser les données
if (count($this->time) > 0) {
$output .= "Etape:\n".$this->etape;
}
if (count($this->time_chap) > 0) {
$output .= "Chapitre:\n".$this->chapitre;
}
$output .= "\n=>Time: ".$this->_vir($this->resultat).' ms';

// Formater les données
if ('html' === $type) {
$outpub = "<h2>Temps du script:</h2>\n".
str_replace(["\r\n", "\r", "\n"], '<br />', $output);
} elseif ('commentaire' === $type) {
$outpub = "<!- TEMPS ECOULE DU SCRIPT: \n$output\n-->";
}

return $outpub;
}

/**
* @return string
*/
public function temps()
{
return $this->_vir($this->resultat);
}

/**
* @param string $format
*/
public function end($format = 'html')
{
$this->stop();
echo $this->OutPut($format);
}

/**
* @param string $format
*/
public function shutdown($format = 'html')
{
register_shutdown_function([$this, 'end'], 'html');
}

//-- CONVERTISSEUR ---------------------------------

/**
* @param $time
* @return mixed
*/
protected function _unit($time)
{
list($usec, $sec) = explode(' ', $time);

return $usec + $sec;
}

/**
* @param $time
* @return string
*/
protected function _conv($time)
{
return mb_substr($time * 1000, 0, 6);

// Récupère les 6 premiers chiffres
// Convertie en millisecondes
// Remplace . par , (écriture FR)
}

/**
* @param $chiffre
* @return string
*/
protected function _vir($chiffre)
{
return number_format($chiffre, 2, ',', ' ');
}

//-- Outil de test/debug -------------------------------

/**
* @param string $phpcode
* @param int $nb_loop
* @param int $nb
*/
public function testloop($phpcode, $nb_loop = 10, $nb = 100)
{
$chapitre = preg_replace('#[^a-z0-9-_]#i', '', mb_substr($phpcode, 0, 50));

for ($loop = 0; $loop < $nb_loop; ++$loop) {
$this->chapitre_debut($chapitre.'#'.$loop);
for ($i = 0; $i < $nb; ++$i) {
eval($phpcode);
}

$this->chapitre_fin($chapitre.'#'.$loop);
}
}

/**
* Exporte un chapitre au format CSV.
*
* @param string $filename
* @return bool
*/
public function export_chapitre($filename)
{
// Analyse des chapitres
if (empty($this->time_chap)) {
return false;
}

foreach ($this->time_chap as $id => $temp) {
$calcul = $this->_vir($this->_conv($this->_unit($temp['fin']) - $this->_unit($temp['debut'])));
error_log("{$id};{$calcul};ms\n", 3, $filename);
}
}
}

Loading

0 comments on commit 14eb832

Please sign in to comment.