2017 © Pedro Peláez
 

library phpunit-xhprof

XHProf / PHPUnit integration via PHP Trait for test cases

image

namelesscoder/phpunit-xhprof

XHProf / PHPUnit integration via PHP Trait for test cases

  • Wednesday, September 9, 2015
  • by NamelessCoder
  • Repository
  • 1 Watchers
  • 1 Stars
  • 424 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

PHPUnit XHProfile integration

Build Status Coverage Status, (*1)

Provides a single Trait that can be used in your test cases to analyse profiling data and make assertions based on it in your test. In short, allows calling any amount of code and analysing the result to know for example how many times a certain method or class was used, the total number of classes and functions used, CPU and memory usage, calls from class A to class B and more., (*2)

You can view this much like an extended version of the $this->at($index) and $this->exactly() and other expectation methods of PHPUnit: it allows you to assert much the same but does so not by using mocks but by profiling. It can therefore cover any amount and complexity of logic and still be able to, for example, tell how many total times class A called method X on class B., (*3)

In addition to this comes the ability to support CPU and memory usage profiling. There is, however, a warning along with this., (*4)

Caveat

Note: you can include both CPU time and memory use in the profiling results, however, when your tests are expected to execute on multiple platforms of varying potency you should probably avoid this and stick to number of methods called (or a relative measure, e.g. percent of total profiled time, but even so platform differences may cause skew/failure)., (*5)

Usage

public function testSomething() {
    // Profile only a specific scope plus a single
    // related function "myfunction". The result
    // will return everything that happens inside
    // any class of the defined scope, plus every
    // call to "myfunction" regardless of where the
    // function is called. You can find the syntax
    // of profile keys in the xhprof documentation.
    $methods = array(
        '/^Vendor\\Namespace\\Scope\\.+/',
        '/.+myfunction$'
    );
    $closure = function() use $foo, $bar {
        // do something that gets profiled. We have
        // already created and prepped our instances
        // so we don't profile that part of the code.
        $foo->doThatWith($bar);
    }
    $profile = $this->profileClosure($closure, $methods);
    $this->assertLessThan(
        10,
        $profile['Vendor\\Class::funcA==>Vendor\\Class::funcB']['ct'],
        'Method funcB was called from funcA more than expected 10 times'
    );
}

And that's about it. You can consult the xhprof documentation for possible flags and options, as well as examples of the structure of the $profile array., (*6)

The Versions

09/09 2015

dev-master

9999999-dev

XHProf / PHPUnit integration via PHP Trait for test cases

  Sources   Download

The Requires

 

09/09 2015

1.0.0

1.0.0.0

XHProf / PHPUnit integration via PHP Trait for test cases

  Sources   Download

The Requires