<commit_before><?php
/**
 * https://github.com/jimrubenstein/php-profiler
 */
class Profiler
{
    /**
     * Used to insure that the {@link init} method is only called once.
     *
     * @see Profiler::init()
     * @var bool
     */
    protected static $init = false;

    /**
     * Used to identify when the profiler has been enabled.
     *
     * If <em>false</em> no profiling data is stored, in order to reduce the overhead of running the profiler
     *
     * @var bool
     */
    protected static $enabled = false;

    /**
     * Time the profiler was included
     *
     * This is used to calculate time-from-start values for all methods
     * as well as total running time.
     */
    protected static $globalStartTime = 0;
    protected static $globalStartMemory = 0;

    /**
     * Time the profiler 'ends'
     *
     * This is populated just before rendering output (see {@link Profiler::render()})
     */
    protected static $globalEndTime = 0;
    protected static $globalEndMemory = 0;

    /**
     * Total time script took to run
     */
    protected static $globalDuration = 0;

    /**
     * Used to identify when some methods are accessed internally
     * versus when they're used externally (as an api or so)
     *
     * @var string
     */
    protected static $profilerKey = null;

    /** @var array */
    protected static $p = array();

    /**
     * @var \Doctrine\DBAL\Logging\SQLLogger
     */
    protected static $dbLogger = null;

    /**
     * @var \Symfony\Component\HttpKernel\KernelInterface
     */
    protected static $kernel = null;

    /**
     * Create a constructor that basically says "don't construct me!"
     */
    public function __construct()
    {
        throw new \Exception('The Profiler class is a static class. Do not instantiate it, access all member methods statically.');
    }

    /**
     * Initialize the profiler
     */
    public static function init()
    {
        if (self::$init) {
            return;
        }

        self::$globalStartTime   = defined('START_TIME')   ? START_TIME   : microtime(true);
        self::$globalStartMemory = defined('START_MEMORY') ? START_MEMORY : memory_get_usage();
        self::$profilerKey       = md5(rand(1, 1000) . time());
        self::$init              = true;
    }

    /**
     * Check to see if the profiler is enabled
     *
     * @see profiler::enabled
     *
     * @return bool true if profiler is enabled, false if disabled
     */
    public static function isEnabled()
    {
        return self::$enabled;
    }

    /**
     * Enable the profiler
     *
     * @see profiler::enabled
     */
    public static function enable()
    {
        self::$enabled = true;
    }

    /**
     * Disable the profiler
     *
     * @see profiler::enabled
     */
    public static function disable()
    {
        self::$enabled = false;
    }

    /**
     * @param \Symfony\Component\HttpKernel\KernelInterface $kernel
     */
    public static function setKernel($kernel)
    {
        self::$kernel = $kernel;
        // @todo имя sql логгера.
        if ($kernel->getEnvironment() === 'prod' and $kernel->getContainer()->has('smart_simple_profiler.db.logger')) {
            self::$dbLogger = $kernel->getContainer()->get('smart_simple_profiler.db.logger');
        }
    }

    /**
     * Get the global memory usage in KB
     *
     * @param string unit a metric prefix to force the unit of bytes used (B, K, M, G)
     */
    public static function getMemUsage($unit = '')
    {
        $usage = memory_get_usage();

        if ($usage < 1e3 || $unit == 'B') {
            $unit = '';
        } elseif ($usage < 9e5 || $unit == 'K') {
            $usage = round($usage / 1e3, 2);
            $unit = 'K';
        } elseif ($usage < 9e8 || $unit == 'M') {
            $usage = round($usage / 1e6, 2);
            $unit = 'M';
        } elseif ($usage < 9e11 || $unit = 'G') {
            $usage = round($usage / 1e9, 2);
            $unit = 'G';
        } else {
            $usage = round($usage / 1e12, 2);
            $unit = 'T';
        }

        return array(
            'num' => $usage,
            'unit' => $unit,
        );
    }

    /**
     * Start a new step
     *
     * This is the most-called method of the profiler.  It initializes and returns a new step node.
     *
     * @param string $name name/identifier for your step. is used later in the output to identify this step
     */
	public static function start($name, $tag = 'all')
    {
        if (!self::isEnabled()) {
            return;
        }

        self::$p[$tag][$name]['start_memory'] = memory_get_usage();
        self::$p[$tag][$name]['start_time'] = microtime(true);

        if (!is_null(self::$dbLogger) and is_object(self::$dbLogger)) {
            self::$p[$tag][$name]['start_db_count'] = self::$dbLogger->currentQuery;
        }
    }

    /**
     * End a step
     *
     * End a step by name, or end all steps in the current tree.
     *
     * @param string $nodeName ends the first-found step with this name. (Note: a warning is generated if it's not the current step, because this is probably unintentional!)
     */
    public static function end($name, $tag = 'all')
    {
        if (!self::isEnabled()) return;

        if (isset(self::$p[$tag][$name]['start_memory'])) {
            self::$p[$tag][$name]['memory'] = memory_get_usage() - self::$p[$tag][$name]['start_memory'];
            self::$p[$tag][$name]['time'] = round(microtime(true) - self::$p[$tag][$name]['start_time'], 3) * 1000;
            unset(
                self::$p[$tag][$name]['start_memory'],
                self::$p[$tag][$name]['start_time']
            );
        }

        if (!is_null(self::$dbLogger) and is_object(self::$dbLogger) and isset(self::$p[$tag][$name]['start_db_count'])) {
            self::$p[$tag][$name]['db_query_count'] = self::$dbLogger->currentQuery - self::$p[$tag][$name]['start_db_count'];
            unset(
                self::$p[$tag][$name]['start_db_count']
                // @todo логгирование запросов.
                //self::$p[$tag][$name]['start_db_query_log']
            );
        }
    }

    /**
     * Render the profiler output.
     *
     * @param bool $isDetailed
     */
	public static function render($isDetailed = false)
    {
        if (!self::isEnabled()) {
            return null;
        }

        $precision = 3;

        $exec_time = microtime(true) - self::$globalStartTime;

        echo "<div style='background-color: #6a6a6a; color: #f3f3f3; font-family: Arial;margin: 10px;'>";

        echo 'Time: <b>', round($exec_time, $precision) * 1000 , '</b> ms',
        '. Memory <b>', round((memory_get_usage() - self::$globalStartMemory) / 1024 / 1024, 2), '</b> MB (<b>',
        round((memory_get_peak_usage(true) - self::$globalStartMemory) / 1024 / 1024, 2), '</b> peak).',
            ' Files: <b>' . count(get_included_files()) . "</b>.\n";

        if (!is_null(self::$dbLogger) and is_object(self::$dbLogger)) {
            echo 'DB: <b>' . self::$dbLogger->currentQuery . '</b>';

            $queries_time = 0;
            foreach (self::$dbLogger->queries as $value) {
                $queries_time += $value['executionMS'];
            }

            $delta = round($queries_time * 100 / $exec_time, 2);
            echo ' (<b>' . round($queries_time, $precision) * 1000 . "</b> ms, $delta %)." . "\n";
        }

        if (!empty(self::$p) and $isDetailed) {
            $summary_time = 0;<commit_msg>Remove files count from profiler.<commit_after><?php
/**
 * https://github.com/jimrubenstein/php-profiler
 */
class Profiler
{
    /**
     * Used to insure that the {@link init} method is only called once.
     *
     * @see Profiler::init()
     * @var bool
     */
    protected static $init = false;

    /**
     * Used to identify when the profiler has been enabled.
     *
     * If <em>false</em> no profiling data is stored, in order to reduce the overhead of running the profiler
     *
     * @var bool
     */
    protected static $enabled = false;

    /**
     * Time the profiler was included
     *
     * This is used to calculate time-from-start values for all methods
     * as well as total running time.
     */
    protected static $globalStartTime = 0;
    protected static $globalStartMemory = 0;

    /**
     * Time the profiler 'ends'
     *
     * This is populated just before rendering output (see {@link Profiler::render()})
     */
    protected static $globalEndTime = 0;
    protected static $globalEndMemory = 0;

    /**
     * Total time script took to run
     */
    protected static $globalDuration = 0;

    /**
     * Used to identify when some methods are accessed internally
     * versus when they're used externally (as an api or so)
     *
     * @var string
     */
    protected static $profilerKey = null;

    /** @var array */
    protected static $p = array();

    /**
     * @var \Doctrine\DBAL\Logging\SQLLogger
     */
    protected static $dbLogger = null;

    /**
     * @var \Symfony\Component\HttpKernel\KernelInterface
     */
    protected static $kernel = null;

    /**
     * Create a constructor that basically says "don't construct me!"
     */
    public function __construct()
    {
        throw new \Exception('The Profiler class is a static class. Do not instantiate it, access all member methods statically.');
    }

    /**
     * Initialize the profiler
     */
    public static function init()
    {
        if (self::$init) {
            return;
        }

        self::$globalStartTime   = defined('START_TIME')   ? START_TIME   : microtime(true);
        self::$globalStartMemory = defined('START_MEMORY') ? START_MEMORY : memory_get_usage();
        self::$profilerKey       = md5(rand(1, 1000) . time());
        self::$init              = true;
    }

    /**
     * Check to see if the profiler is enabled
     *
     * @see profiler::enabled
     *
     * @return bool true if profiler is enabled, false if disabled
     */
    public static function isEnabled()
    {
        return self::$enabled;
    }

    /**
     * Enable the profiler
     *
     * @see profiler::enabled
     */
    public static function enable()
    {
        self::$enabled = true;
    }

    /**
     * Disable the profiler
     *
     * @see profiler::enabled
     */
    public static function disable()
    {
        self::$enabled = false;
    }

    /**
     * @param \Symfony\Component\HttpKernel\KernelInterface $kernel
     */
    public static function setKernel($kernel)
    {
        self::$kernel = $kernel;
        // @todo имя sql логгера.
        if ($kernel->getEnvironment() === 'prod' and $kernel->getContainer()->has('smart_simple_profiler.db.logger')) {
            self::$dbLogger = $kernel->getContainer()->get('smart_simple_profiler.db.logger');
        }
    }

    /**
     * Get the global memory usage in KB
     *
     * @param string unit a metric prefix to force the unit of bytes used (B, K, M, G)
     */
    public static function getMemUsage($unit = '')
    {
        $usage = memory_get_usage();

        if ($usage < 1e3 || $unit == 'B') {
            $unit = '';
        } elseif ($usage < 9e5 || $unit == 'K') {
            $usage = round($usage / 1e3, 2);
            $unit = 'K';
        } elseif ($usage < 9e8 || $unit == 'M') {
            $usage = round($usage / 1e6, 2);
            $unit = 'M';
        } elseif ($usage < 9e11 || $unit = 'G') {
            $usage = round($usage / 1e9, 2);
            $unit = 'G';
        } else {
            $usage = round($usage / 1e12, 2);
            $unit = 'T';
        }

        return array(
            'num' => $usage,
            'unit' => $unit,
        );
    }

    /**
     * Start a new step
     *
     * This is the most-called method of the profiler.  It initializes and returns a new step node.
     *
     * @param string $name name/identifier for your step. is used later in the output to identify this step
     */
	public static function start($name, $tag = 'all')
    {
        if (!self::isEnabled()) {
            return;
        }

        self::$p[$tag][$name]['start_memory'] = memory_get_usage();
        self::$p[$tag][$name]['start_time'] = microtime(true);

        if (!is_null(self::$dbLogger) and is_object(self::$dbLogger)) {
            self::$p[$tag][$name]['start_db_count'] = self::$dbLogger->currentQuery;
        }
    }

    /**
     * End a step
     *
     * End a step by name, or end all steps in the current tree.
     *
     * @param string $nodeName ends the first-found step with this name. (Note: a warning is generated if it's not the current step, because this is probably unintentional!)
     */
    public static function end($name, $tag = 'all')
    {
        if (!self::isEnabled()) return;

        if (isset(self::$p[$tag][$name]['start_memory'])) {
            self::$p[$tag][$name]['memory'] = memory_get_usage() - self::$p[$tag][$name]['start_memory'];
            self::$p[$tag][$name]['time'] = round(microtime(true) - self::$p[$tag][$name]['start_time'], 3) * 1000;
            unset(
                self::$p[$tag][$name]['start_memory'],
                self::$p[$tag][$name]['start_time']
            );
        }

        if (!is_null(self::$dbLogger) and is_object(self::$dbLogger) and isset(self::$p[$tag][$name]['start_db_count'])) {
            self::$p[$tag][$name]['db_query_count'] = self::$dbLogger->currentQuery - self::$p[$tag][$name]['start_db_count'];
            unset(
                self::$p[$tag][$name]['start_db_count']
                // @todo логгирование запросов.
                //self::$p[$tag][$name]['start_db_query_log']
            );
        }
    }

    /**
     * Render the profiler output.
     *
     * @param bool $isDetailed
     */
	public static function render($isDetailed = false)
    {
        if (!self::isEnabled()) {
            return null;
        }

        $precision = 3;

        $exec_time = microtime(true) - self::$globalStartTime;

        echo "<div style='background-color: #6a6a6a; color: #f3f3f3; font-family: Arial;margin: 10px;'>";

        echo 'Time: <b>', round($exec_time, $precision) * 1000 , '</b> ms',
        '. Memory <b>', round((memory_get_usage() - self::$globalStartMemory) / 1024 / 1024, 2), '</b> MB (<b>',
        round((memory_get_peak_usage(true) - self::$globalStartMemory) / 1024 / 1024, 2), '</b> peak).';
            //' Files: <b>' . count(get_included_files()) . "</b>.\n";

        if (!is_null(self::$dbLogger) and is_object(self::$dbLogger)) {
            echo ' DB: <b>' . self::$dbLogger->currentQuery . '</b>';

            $queries_time = 0;
            foreach (self::$dbLogger->queries as $value) {
                $queries_time += $value['executionMS'];
            }

            $delta = round($queries_time * 100 / $exec_time, 2);
            echo ' (<b>' . round($queries_time, $precision) * 1000 . "</b> ms, $delta %)." . "\n";
        }

        if (!empty(self::$p) and $isDetailed) {
            $summary_time = 0;
{'fraction_non_alphanumeric': 0.15352421444230385, 'fraction_numerical': 0.010496246595363051, 'mean_word_length': 2.069113149847095, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 55, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3014992', 'n_tokens_mistral': 4687, 'n_tokens_neox': 4561, 'n_words': 1193}