public function profile($class, $methodName, array $methodArguments = null, $invocations = 1)
    {
        if (true === is_object($class)) {
            $className = get_class($class);
        } else {
            $className = $class;

            if (false === class_exists($className)) {
                throw new Doozr_Exception(
                    sprintf('%s does not exist.', $className)
                );
            }
        }

        $method   = new \ReflectionMethod($className, $methodName);
        $instance = null;

        if (true === $method->isStatic()) {
            // mark last profiling session as static
            $this->profileStatic = true;
        } elseif (false === $method->isStatic() && !($class instanceof $className)) {
            $class    = new \ReflectionClass($className);
            $instance = $class->newInstance();
        } else {
            $instance = $class;
        }

        $durations = [];

        for ($i = 0; $i < $invocations; ++$i) {
            $start = microseconds();

            if (is_null($methodArguments)) {
                $method->invoke($instance);
            } else {
                $method->invokeArgs($instance, $methodArguments);
            }

            $durations[] = microseconds() - $start;
        }

        $total = round(array_sum($durations), 8);

        return $this->profilingDetails([
            'class'     => $className,
            'method'    => $methodName,
            'arguments' => $methodArguments,
            'duration'  => [
                'microseconds' => [
                    'total'   => $total,
                    'average' => round($total / count($durations), 8),
                    'worst'   => round(max($durations), 8),
                ],
                'seconds' => [
                    'total'   => $total / 1000,
                    'average' => round($total / 1000 / count($durations), 8),
                    'worst'   => round(max($durations) / 1000, 8),
                ],
            ],
            'invocations' => $invocations,
        ]);
    }
public function profile($class, $methodName, array $methodArguments = null, $invocations = 1)
    {
        if (true === is_object($class)) {
            $className = get_class($class);
        } else {
            $className = $class;

            if (false === class_exists($className)) {
                throw new Doozr_Exception(
                    sprintf('%s does not exist.', $className)
                );
            }
        }

        $method   = new \ReflectionMethod($className, $methodName);
        $instance = null;

        if (true === $method->isStatic()) {
            // mark last profiling session as static
            $this->profileStatic = true;
        } elseif (false === $method->isStatic() && !($class instanceof $className)) {
            $class    = new \ReflectionClass($className);
            $instance = $class->newInstance();
        } else {
            $instance = $class;
        }

        $durations = [];

        for ($i = 0; $i < $invocations; ++$i) {
            $start = microseconds();

            if (is_null($methodArguments)) {
                $method->invoke($instance);
            } else {
                $method->invokeArgs($instance, $methodArguments);
            }

            $durations[] = microseconds() - $start;
        }

        $total = round(array_sum($durations), 8);

        return $this->profilingDetails([
            'class'     => $className,
            'method'    => $methodName,
            'arguments' => $methodArguments,
            'duration'  => [
                'microseconds' => [
                    'total'   => $total,
                    'average' => round($total / count($durations), 8),
                    'worst'   => round(max($durations), 8),
                ],
                'seconds' => [
                    'total'   => $total / 1000,
                    'average' => round($total / 1000 / count($durations), 8),
                    'worst'   => round(max($durations) / 1000, 8),
                ],
            ],
            'invocations' => $invocations,
        ]);
    }
public function profile ( $ class , $ methodName , array $ methodArguments = null , $ invocations = 1 ) { if ( true === is_object ( $ class ) ) { $ className = get_class ( $ class ) ; } else { $ className = $ class ; if ( false === class_exists ( $ className ) ) { throw new Doozr_Exception ( sprintf ( '%s does not exist.' , $ className ) ) ; } } $ method = new \ ReflectionMethod ( $ className , $ methodName ) ; $ instance = null ; if ( true === $ method -> isStatic ( ) ) { // mark last profiling session as static $ this -> profileStatic = true ; } elseif ( false === $ method -> isStatic ( ) && ! ( $ class instanceof $ className ) ) { $ class = new \ ReflectionClass ( $ className ) ; $ instance = $ class -> newInstance ( ) ; } else { $ instance = $ class ; } $ durations = [ ] ; for ( $ i = 0 ; $ i < $ invocations ; ++ $ i ) { $ start = microseconds ( ) ; if ( is_null ( $ methodArguments ) ) { $ method -> invoke ( $ instance ) ; } else { $ method -> invokeArgs ( $ instance , $ methodArguments ) ; } $ durations [ ] = microseconds ( ) - $ start ; } $ total = round ( array_sum ( $ durations ) , 8 ) ; return $ this -> profilingDetails ( [ 'class' => $ className , 'method' => $ methodName , 'arguments' => $ methodArguments , 'duration' => [ 'microseconds' => [ 'total' => $ total , 'average' => round ( $ total / count ( $ durations ) , 8 ) , 'worst' => round ( max ( $ durations ) , 8 ) , ] , 'seconds' => [ 'total' => $ total / 1000 , 'average' => round ( $ total / 1000 / count ( $ durations ) , 8 ) , 'worst' => round ( max ( $ durations ) / 1000 , 8 ) , ] , ] , 'invocations' => $ invocations , ] ) ; }
Runs a method with the provided arguments for profiling.

Runs a method with the provided arguments, and returns profilingDetails about how long it took.
Works with instance methods and static methods.

@param mixed      $class           Name of the class to profile or an existing instance
@param string     $methodName      Name of the method to profile
@param array|null $methodArguments Arguments to pass to the function
@param int        $invocations     Number of times to call the method

@author Benjamin Carl <opensource@clickalicious.de>

@return $this Instance for chaining

@throws Doozr_Exception
Runs a method with the provided arguments for profiling .