Представляю вашему вниманию простой инструмент, который предоставляет возможность тестировать функции - чтобы измерить время их исполнения/потребляемую память. Так же существует возможность определять собственную функцию для измерения, однако она, разумеется, должна возвращать числовые значения.
CODE (php):
скопировать код в буфер обмена
скопировать код в буфер обмена
- namespace Benchmark;
- final class Measure
- {
- const MEMORY_VALUE = 0;
- const BENCHMARK_VALUE = 1;
- const BENCHMARK_AVERAGE = 2;
- const BENCHMARK_COUNT = 3;
- private $max, $memory;
- public function memoryTick()
- {
- $this->max = $this->memory>$this->max?$this->memory:$this->max;
- }
- /**
- *
- * @param callable $function A function to be measured
- * @param array $args Parameters to be passed for measured function
- * @return array Result currently contains one value: used memory space
- */
- {
- declare(ticks=1);
- $this->max = 0;
- $this->measureFunction($function, $args, 1);
- return [
- self::MEMORY_VALUE => $this->max
- ];
- }
- /**
- *
- * @param callable $function A function to be measured
- * @param array $args Parameters to be passed for measured function
- * @param int $count Count of measurements
- * @return array Result currently contains: total time, average time and measurements count
- * @throws \InvalidArgumentException If measurements count is invalid
- */
- {
- return $this->benchmarkCustom('microtime', $function, [1], $args, $count);
- }
- /**
- *
- * @param callable $benchmark Function which will do measurements
- * @param callable $function A function to be measured
- * @param array $benchmarkArgs Parameters to be passed for measurement function
- * @param array $functionArgs Parameters to be passed for measured function
- * @param int $count Count of measurements
- * @return array Result currently contains: total value, average value and measurements count
- * @throws \InvalidArgumentException If measurements count is invalid
- * @throws \LogicException If measurement function did not returned numeric value
- */
- {
- {
- throw new \InvalidArgumentException('Count of measure times must be positive integer');
- }
- {
- throw new \LogicException('Benchmark function must return valid numeric value');
- }
- $this->measureFunction($function, $functionArgs, $count);
- return [
- self::BENCHMARK_VALUE => $end - $init,
- self::BENCHMARK_AVERAGE => ($end - $init) / $count,
- self::BENCHMARK_COUNT => $count
- ];
- }
- private function measureFunction($function, $args, $count)
- {
- for($i=0; $i<$count; $i++)
- {
- }
- }
- }