Skip to content

Commit

Permalink
[Archive] 2mars2007: php4 version
Browse files Browse the repository at this point in the history
  • Loading branch information
jgauthi committed Sep 13, 2020
1 parent 6645d45 commit 30b473c
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 2 deletions.
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Debug tools like: varExport* functions, timer, server dump, SQL Beautifier, etc.

## Prerequisite

* PHP 4 (v1.0), PHP 5.4+ (v1.1+) ou PHP 7 & 7.4 (v2)
* PHP 4 (v1.0), PHP 5.4+ (v1.1), PHP 5.6 (v1.2+) or PHP 7.4 (v2)

## Install
Edit your [composer.json](https://getcomposer.org) (launch `composer update` after edit):
Expand All @@ -12,7 +12,7 @@ Edit your [composer.json](https://getcomposer.org) (launch `composer update` aft
"repositories": [
{ "type": "git", "url": "[email protected]:jgauthi/component_debug.git" }
],
"require": {
"require-dev": {
"jgauthi/component_debug": "1.*"
}
}
Expand Down
127 changes: 127 additions & 0 deletions src/timer.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* Timer - Stopwatch in milliseconds script time
+ the various steps in the code
* @author: Jgauthi <github.com/jgauthi>
* @maj 14/03/09
* @version 1.0
*/

class timer
{
var $debut;
var $fin;
var $time = array();
var $start = false;
var $header = 0;
var $resultat;
var $etape;

//-- CONSTRUCTEUR ---------------------------------
FUNCTION timer($header = 0)
{
$this -> debut = microtime();
$this -> header = $header;
}

//-- ETALONNER LE PARCOURS DU SCRIPT --------------
FUNCTION etape($nom)
{
$this -> time[] = array('nom' => $nom, 'time' => microtime());
}

//-- METTRE FIN AU COMPTEUR
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)
{
$etape = '';
foreach($this -> time as $id => $tmp)
{
$temp = $this -> _vir($this -> _conv($this -> _unit($tmp['time']) - $this -> debut));
$this -> etape .= '- '.$tmp['nom'].': '.$temp." ms\n";
if ($this -> header)
header($id.'-Time: '.$temp.' ms -> '.$tmp['nom']);
}
}

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

//--
FUNCTION OutPut($type = 'commentaire')
{
if ($type == 'commentaire')
{
$av = '<!- TEMPS ECOULE DU SCRIPT: ';
$ap = '-->';
}
else
{
$av = '<h2>Temps écoulé du script: </h2>';
$ap = '';
}

if (count($this -> time) == 0)
return ("\n\n".$av.'Time: '.$this -> _vir($this -> resultat).' ms'.$ap);
else
return "\n\n".$av.
(($type != 'commentaire') ?
str_replace("\n","<br />\n", $this -> etape):
$this -> etape).
$ap."\n". '=> Time: '.$this -> _vir($this -> resultat)." ms\n\n";
}

FUNCTION temps ()
{
return $this -> _vir($this -> resultat);
}

FUNCTION end($format = 'html')
{
$this->stop();
echo $this->OutPut($format);
}

FUNCTION shutdown()
{
register_shutdown_function(array($this, 'end'));
}

//-- CONVERTISSEUR ---------------------------------
FUNCTION _unit($time)
{
list($usec,$sec) = explode(' ',$time);
return $usec + $sec;
}

FUNCTION _conv($time)
{
return substr($time*1000, 0, 6);

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

FUNCTION _vir($chiffre)
{
return str_replace('.', ',', $chiffre);
}
}

?>

0 comments on commit 30b473c

Please sign in to comment.