Code Monkey home page Code Monkey logo

phpbenchtime's Introduction

PHPBenchTime v2.1.0

Latest Stable Version Total Downloads Monthly Downloads License Build Status

A light benchmark timer class for PHP. PHPBenchTime is quite simple to use and is loaded with functionality - including detailed summary data, easily readable source, a robust lap system and pause/unpause functionality.

Also, please check out my Python version of this package: PyBenchTime Python Package

On Packagist

https://packagist.org/packages/nsa-yoda/phpbenchtime

Methods

public start()
public end()
public reset()
public lap()
public summary()
public pause()
public unPause()
private endLap()
private getCurrentTime()

Properties

private startTime
private endTime
private pauseTime
private laps
private lapCount

Usage

You should see the Usage.php file in source for the best how to documentation. However, here's an overview:

Load and initiate the PHPBenchTime Timer:

require('PHPBenchTime.php');
use PHPBenchTime\Timer;
$T = new Timer;

That was easy! Now lets start a new timer:

$T->start();

Then lets just sleep for 3 seconds:

sleep(3);

Now, lets end the timer, and put results in $time:

$time = $T->end();

When we end a timer, we receive an array back, containing the start time, end time and difference between start and end times:

Array (
    [running] => false
    [start] => 1406146951.9998
    [end] => 1406146952.0638
    [total] => 0.0019998550415039
    [paused] => 0
    [laps] => Array (
        [0] => Array (
            [name] => start
            [start] => 1406146951.9998
            [end] => 1406146952.0018
            [total] => 0.0019998550415039
        )
    )
)

Advanced Usage : Laps

PHPBenchTime also allows you to set laps between code execution, which allows you to determine what part of your code is causing a bottleneck.

Let's sleep for a couple of seconds between laps:

sleep(1);
$T->lap();
sleep(2);
$T->lap();

Now, let's end the timer:

$time = $T->end();

Let's see the results:

Array (
    [running] => false
    [start] => 1406146951.9998
    [end] => 1406146952.0638
    [total] => 0.063999891281128
    [paused] => 0.041000127792358
    [laps] => Array (
        [0] => Array (
            [name] => start
            [start] => 1406146951.9998
            [end] => 1406146952.0018
            [total] => 0.0019998550415039
        )
        [1] => Array (
            [name] => 1
            [start] => 1406146952.0018
            [end] => 1406146952.0028
            [total] => 0.0010001659393311
        )
        [2] => Array (
            [name] => 2
            [start] => 1406146952.0028
            [end] => 1406146952.0128
            [total] => 0.0099999904632568
        )
    )
)

Advanced Usage

PHPBenchTime allows you to do named laps, as well as to pause and unpause the timer (say you want to make a network call or a call out to the database, but don't want to include that time in your benchmark - pause and then unpause after you receive the network/database data).

HISTORY

  • v1.0.0: Static Birth!
  • v1.1.0: Static Namespaces!
  • v1.2.0: Non-Static Namespaces!
  • v1.3.0: Laps! Laps! Laps!
  • v2.0.0: Complete rewrite, adds pause, unpause, central lap system and more detailed summary
  • v2.1.0: Performance enhancements, unit tests, etc

phpbenchtime's People

Contributors

andyfleming avatar josepgv avatar mjaschen avatar nsa-yoda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mjaschen felideo

phpbenchtime's Issues

Allow multiple pauses

You don't allow multiple pauses (it will give a faulty output). In your unpause methoed:

$this->totalPauseTime = $this->getCurrentTime() - $this->pauseTime;

should probably be

$this->totalPauseTime += $this->getCurrentTime() - $this->pauseTime;

You decieded to not end a lap if you pause. this is correct. But, it gives some peculiar output:

$t->lap('test');
sleep(1);
$t->pause();
sleep(1);
$t->unpause();
sleep(1);
$t->endlap();
The lap has no knowledge of a pause.

End does too much

In the endlap, you calculate the current position of the array pointer. There is afunction for that: key So:

$lapCount = key($this->laps);

another option here would be to use a pointer reference instead of accessing the laps array multiple time. But this should only be done if it reads easier:

$lastLap = &current($this->laps);
$lastLap['end'] = $this->getCurrentTime();
$lastLap['total'] = $lastLap['end'] - $lastLap['start'];

setRunningPaused can be upgraded for better performance

private function setRunningPaused( $running, $paused ) {
$this->isRunning = !!$running ;
$this->isPaused = !!$paused ;
}

-or- use constants and a state:

const RUNNING = 1;
const PAUSED  = 0;
const STOPPED = -1;
protected $state

$this->state = Timer::RUNNING

In Lap method, variable can be removed

You are creating a $lapTime variable, but you aren't really using it. Only to pass it to the array. This will do the job aswell:

$this->laps[] = array(
"start" => $this->getCurrentTime()
);

Start does not take a name

$t = new Timer();
$t->start();
//I need to call lap now because I can't pass in a name in the start method
$t->lap('database');
//do some DB stuff
$t->lap();
//do some random stuff
$t->lap('database');
//do some more db stuff
$t->end();

We now have lap called start that added some execution time, but nothing happened. It is only there because we wanted to pass in a different name then start.

Missing argument 1 for PHPBenchTime\Timer::Start()

I get the following error when trying to use this class as per the instructions:

  [ErrorException]                                                                                                     
  Missing argument 1 for PHPBenchTime\Timer::Start(), called in /opt/bitnami/apache2/htdocs/vendor/jsanc623/phpbencht  
  ime/src/PHPBenchTime.php on line 71 and defined  

Unused variables.

You return a totaltime in your summary. It is even a property of the class. but it is only used inside summary() method. So simply remove itfrom the class and make it a variable of the method.

Ideal Output - Extended lap information

Ideally end() should end the timer and return a summary() method.

And summary() should return the following:

Array
(
    [running]   => false
    [start]     => 1353195346.6762
    [end]       => 1353195349.6765
    [total]     => 3.0003
    [laps] =>
        [0] => 
            [name]  => Lap 1
            [start] => 1353195346.6762
            [end]   =>1353195347.6763
            [total] => 1.0001
        [1] => 
            [name]  => Some query, or something
            [start] => 1353195347.6763
            [end]   =>1353195349.6764
            [total] => 1.0001
        [2] => 1353195349.6764
            [name]  => Last lap!
            [start] => 1353195349.6764
            [end]   =>1353195349.6765
            [total] => 1.0001
)

$timer->summary(); could be called before the time is done as well.

Array
(
    [running]   => true
    [start]     => 1353195346.6762
    [laps] =>
        [0] => 
            [name]  => Lap 1
            [start] => 1353195346.6762
            [end]   =>1353195347.6763
            [total] => 1.0001
        [1] => 
            [name]  => Some query, or something
            [start] => 1353195347.6763
)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.