• Модуль: perfmon
  • Путь к файлу: ~/bitrix/modules/perfmon/classes/general/keeper.php
  • Класс: CPerfomanceKeeper
  • Вызов: CPerfomanceKeeper::writeToDatabase
static function writeToDatabase()
{
	$START_EXEC_CURRENT_TIME = microtime();

	global $DB, $APPLICATION;
	$application = BitrixMainApplication::getInstance();
	$connection  = $application->getConnection();

	$connection->stopTracker();
	$DB->ShowSqlStat = false;
	if ($connection->getTracker())
		$arQueryDebug = $connection->getTracker()->getQueries();
	else
		$arQueryDebug = array();
	$arIncludeDebug = $APPLICATION->arIncludeDebug;

	$cache_log = COption::GetOptionString("perfmon", "cache_log") === "Y";
	$large_cache_log = COption::GetOptionString("perfmon", "large_cache_log") === "Y";
	$large_cache_size = floatval(COption::GetOptionString("perfmon", "large_cache_size")) * 1024;
	$sql_log = COption::GetOptionString("perfmon", "sql_log") === "Y";
	$slow_sql_log = COption::GetOptionString("perfmon", "slow_sql_log") === "Y";
	$slow_sql_time = floatval(COption::GetOptionString("perfmon", "slow_sql_time"));

	if ($slow_sql_log)
	{
		self::removeQueries($arQueryDebug, $arIncludeDebug, $slow_sql_time, $cache_log);
	}

	$query_count = 0;
	$query_time = 0.0;
	if ($sql_log)
	{
		self::countQueries($query_count, $query_time, $arQueryDebug, $arIncludeDebug);
	}

	$comps_count = 0;
	$comps_time = 0.0;
	if ($sql_log || $cache_log)
	{
		self::countComponents($comps_count, $comps_time, $arIncludeDebug);
	}

	$cache_count = array();
	/** @var BitrixMainDiagCacheTracker $arCacheDebug */
	$arCacheDebug = null;
	if ($cache_log)
	{
		$arCacheDebug = BitrixMainDiagCacheTracker::getCacheTracking();

		if ($large_cache_log)
			self::removeCaches($large_cache_size, $arCacheDebug, $arIncludeDebug);

		self::countCache($arCacheDebug, $cache_count);
		foreach ($arIncludeDebug as $ar)
		{
			if (array_key_exists("REL_PATH", $ar))
				self::countCache($ar["CACHE"], $cache_count);
		}
	}

	if ($_SERVER["SCRIPT_NAME"] == "/bitrix/urlrewrite.php" && isset($_SERVER["REAL_FILE_PATH"]))
		$SCRIPT_NAME = $_SERVER["REAL_FILE_PATH"];
	elseif ($_SERVER["SCRIPT_NAME"] == "/404.php" && isset($_SERVER["REAL_FILE_PATH"]))
		$SCRIPT_NAME = $_SERVER["REAL_FILE_PATH"];
	else
		$SCRIPT_NAME = $_SERVER["SCRIPT_NAME"];

	$arFields = array(
		"~DATE_HIT" => $DB->GetNowFunction(),
		"IS_ADMIN" => defined("ADMIN_SECTION")? "Y": "N",
		"REQUEST_METHOD" => $_SERVER["REQUEST_METHOD"],
		"SERVER_NAME" => $_SERVER["SERVER_NAME"],
		"SERVER_PORT" => $_SERVER["SERVER_PORT"],
		"SCRIPT_NAME" => $SCRIPT_NAME,
		"REQUEST_URI" => $_SERVER["REQUEST_URI"],
		"INCLUDED_FILES" => function_exists("get_included_files")? count(get_included_files()): false,
		"MEMORY_PEAK_USAGE" => function_exists("memory_get_peak_usage")? memory_get_peak_usage(): false,
		"CACHE_TYPE" => COption::GetOptionString("main", "component_cache_on", "Y") == "Y"? "Y": "N",
		"~CACHE_SIZE" => intval($GLOBALS["CACHE_STAT_BYTES"]),
		"~CACHE_COUNT_R" => intval($cache_count["R"]),
		"~CACHE_COUNT_W" => intval($cache_count["W"]),
		"~CACHE_COUNT_C" => intval($cache_count["C"]),
		"QUERIES" => $query_count,
		"~QUERIES_TIME" => $query_time,
		"SQL_LOG" => $sql_log? "Y": "N",
		"COMPONENTS" => $comps_count,
		"~COMPONENTS_TIME" => $comps_time,
		"~MENU_RECALC" => $APPLICATION->_menu_recalc_counter,
	);
	CPerfomanceKeeper::SetPageTimes($START_EXEC_CURRENT_TIME, $arFields);

	if ($query_count || $comps_count || $cache_count)
		$HIT_ID = $DB->Add("b_perf_hit", $arFields);
	else
		$HIT_ID = false;

	$NN = 0;
	if ($HIT_ID && $cache_log)
	{
		self::saveCaches($HIT_ID, false, $arCacheDebug, $NN);
	}

	$MM = 0;
	if ($HIT_ID && $sql_log)
	{
		if (is_array($arQueryDebug))
			self::saveQueries($HIT_ID, false, $arQueryDebug, $MM);
	}

	if ($HIT_ID && ($sql_log || $cache_log))
	{
		foreach ($arIncludeDebug as $ii => $ar)
		{
			if (!array_key_exists("REL_PATH", $ar))
				continue;

			$cache_count = array();
			if ($cache_log)
				self::countCache($ar["CACHE"], $cache_count);

			$arFields = array(
				"HIT_ID" => $HIT_ID,
				"NN" => $ii,
				"CACHE_TYPE" => $ar["CACHE_TYPE"],
				"~CACHE_SIZE" => intval($ar["CACHE_SIZE"]),
				"~CACHE_COUNT_R" => intval($cache_count["R"]),
				"~CACHE_COUNT_W" => intval($cache_count["W"]),
				"~CACHE_COUNT_C" => intval($cache_count["C"]),
				"COMPONENT_TIME" => $ar["TIME"],
				"QUERIES" => $ar["QUERY_COUNT"],
				"QUERIES_TIME" => $ar["QUERY_TIME"],
				"COMPONENT_NAME" => $ar["REL_PATH"],
			);
			$COMP_ID = $DB->Add("b_perf_component", $arFields);

			if ($sql_log && is_array($ar["QUERIES"]))
				self::saveQueries($HIT_ID, $COMP_ID, $ar["QUERIES"], $MM);

			if ($cache_log && is_array($ar["CACHE"]))
				self::saveCaches($HIT_ID, $COMP_ID, $ar["CACHE"], $NN);
		}
	}

	global $perfmonErrors;
	if ($HIT_ID && (count($perfmonErrors) > 0))
	{
		foreach ($perfmonErrors as $arError)
		{
			$arError["HIT_ID"] = $HIT_ID;
			$DB->Add("b_perf_error", $arError);
		}
	}
}