Result

class Result

Result contains the test results for a specific method and parameter.

Methods

public __construct(float $duration, int $operations, Method $method, Parameter $parameter = null) No description
public floatgetDuration() Duration in milliseconds.
public intgetOperations() Get the number of iterations.
public floatgetOperationsPerSecond(bool $withUnit = false) Get the operations per second as a float.
public MethodgetMethod() Get the method used to create this result.
public ParametergetParameter() Get the parameter that was used to call the method.
public __toString() No description

Details

at line 44

__construct()

public __construct(float $duration, int $operations, Method $method, Parameter $parameter = null)

Parameters

float$duration
int$operations
Method$method
Parameter$parameter
at line 57

getDuration()

public float getDuration()

Duration in milliseconds.

Return Value

floatMilliseconds
at line 67

getOperations()

public int getOperations()

Get the number of iterations.

Return Value

int
at line 79

getOperationsPerSecond()

public float getOperationsPerSecond(bool $withUnit = false)

Get the operations per second as a float.

Parameters

bool$withUnitOptionally format with abbreviated unit. Default is false.

Return Value

float
at line 93

getMethod()

public Method getMethod()

Get the method used to create this result.

Return Value

Method
at line 103

getParameter()

public Parameter getParameter()

Get the parameter that was used to call the method.

Return Value

Parameter
at line 108

__toString()

public __toString()

Source code

<?php

/*
 * This file is part of nochso/benchmark.
 *
 * @copyright Copyright (c) 2015 Marcel Voigt <mv@noch.so>
 * @license   https://github.com/nochso/benchmark/blob/master/LICENSE ISC
 * @link      https://github.com/nochso/benchmark
 */

namespace nochso\Benchmark;

/**
 * Result contains the test results for a specific method and parameter.
 *
 * @author Marcel Voigt <mv@noch.so>
 * @copyright Copyright (c) 2015 Marcel Voigt <mv@noch.so>
 */
class Result
{
    /**
     * @var float Milliseconds
     */
    private $duration;
    /**
     * @var int
     */
    private $operations;
    /**
     * @var Method
     */
    private $method;
    /**
     * @var Parameter
     */
    private $parameter;

    /**
     * @param float     $duration
     * @param int       $operations
     * @param Method    $method
     * @param Parameter $parameter
     */
    public function __construct($duration, $operations, Method $method, Parameter $parameter = null)
    {
        $this->duration = $duration;
        $this->operations = $operations;
        $this->method = $method;
        $this->parameter = $parameter;
    }

    /**
     * Duration in milliseconds.
     *
     * @return float Milliseconds
     */
    public function getDuration()
    {
        return $this->duration;
    }

    /**
     * Get the number of iterations.
     *
     * @return int
     */
    public function getOperations()
    {
        return $this->operations;
    }

    /**
     * Get the operations per second as a float.
     *
     * @param bool $withUnit Optionally format with abbreviated unit. Default is false.
     *
     * @return float
     */
    public function getOperationsPerSecond($withUnit = false)
    {
        $opsPerSec = ($this->operations / $this->duration) * 1000.0;
        if ($withUnit) {
            return $this->formatNumber($opsPerSec);
        }
        return $opsPerSec;
    }

    /**
     * Get the method used to create this result.
     *
     * @return Method
     */
    public function getMethod()
    {
        return $this->method;
    }

    /**
     * Get the parameter that was used to call the method.
     *
     * @return Parameter
     */
    public function getParameter()
    {
        return $this->parameter;
    }

    public function __toString()
    {
        $ops = $this->formatNumber($this->getOperationsPerSecond());
        return $ops . ' op/sec';
    }

    /**
     * Turns 2000 into 2K, 2,000,000 into 2M, etc.
     *
     * This uses base10. Don't use it for bytes.
     *
     * @param int|float $value
     * @param int       $decimals
     *
     * @return string
     *
     * @link http://stackoverflow.com/a/2510540
     */
    private function formatNumber($value, $decimals = 1)
    {
        $base = log($value, 1000);
        $newValue = pow(1000, $base - floor($base));

        $suffixes = array('', 'K', 'M', 'G', 'T');
        $suffix = $suffixes[intval($base)];

        return number_format($newValue, $decimals) . $suffix;
    }
}