HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/kommabasic.nl/vendor/phpbench/phpbench/lib/Benchmark/BaselineManager.php
<?php

/*
 * This file is part of the PHPBench package
 *
 * (c) Daniel Leech <daniel@dantleech.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 */

namespace PhpBench\Benchmark;

/**
 * The baseline manager is responsible for collecting and executing
 * baseline benchmarks.
 *
 * Baseline benchmarks are standard microbenchmarks which can be used to
 * determine the "baseline" performance of the test platform.
 *
 * These measurements can be used to establish a baseline speed for the system,
 * or to provide counterweights to iteration measurements (and so attempt to
 * cancel out any fluctuations of the test platforms performance).
 */
class BaselineManager
{
    /**
     * @var mixed[]
     */
    private $callables;

    /**
     * Add a baseline callable. The callable can be any
     * callable accepted by call_user_func.
     *
     * Throws an invalid argument exception if the name has
     * already been registered.
     *
     *
     * @throws \InvalidArgumentException
     */
    public function addBaselineCallable(string $name, $callable): void
    {
        if (isset($this->callables[$name])) {
            throw new \InvalidArgumentException(sprintf(
                'Baseline callable "%s" has already been registered.',
                $name
            ));
        }

        if (!is_callable($callable)) {
            throw new \InvalidArgumentException(sprintf(
                'Given baseline "%s" callable "%s" is not callable.',
                $name, is_string($callable) ? $callable : gettype($callable)
            ));
        }

        $this->callables[$name] = $callable;
    }

    /**
     * Return mean time taken to execute the named baseline
     * callable in microseconds.
     */
    public function benchmark($name, $revs): float
    {
        if (!isset($this->callables[$name])) {
            throw new \InvalidArgumentException(sprintf(
                'Unknown baseline callable "%s", known baseline callables: "%s"',
                $name, implode('", "', array_keys($this->callables))
            ));
        }

        $start = microtime(true);
        call_user_func($this->callables[$name], $revs);

        return (microtime(true) - $start) / $revs * 1E6;
    }
}