public static function writeToDatabase() { $START_EXEC_CURRENT_TIME = microtime(); global $DB, $APPLICATION; $application = \Bitrix\Main\Application::getInstance(); $connection = $application->getConnection(); $connection->stopTracker(); $DB->ShowSqlStat = false; $arQueryDebug = $connection->getTracker()->getQueries(); $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 \Bitrix\Main\Diag\CacheTracker $arCacheDebug */ $arCacheDebug = null; if ($cache_log) { $arCacheDebug = \Bitrix\Main\Diag\CacheTracker::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); } } }
public static function OnAfterAfterEpilog() { if(defined("PERFMON_STARTED")) { $START_EXEC_CURRENT_TIME = microtime(); global $DB, $APPLICATION; $DB->ShowSqlStat = false; $sql_log = COption::GetOptionString("perfmon", "sql_log") === "Y"? "Y": "N"; $slow_sql_log = COption::GetOptionString("perfmon", "slow_sql_log") === "Y"? "Y": "N"; $slow_sql_time = floatval(COption::GetOptionString("perfmon", "slow_sql_time")); $bHasSQL2Write = false; $cquery_count = 0; $cquery_time = 0; if(($sql_log === "Y") && ($slow_sql_log === "Y") && is_array($DB->arQueryDebug)) { foreach($DB->arQueryDebug as $i => $arQueryInfo) if($arQueryInfo["TIME"] < $slow_sql_time) unset($DB->arQueryDebug[$i]); else { $bHasSQL2Write = true; $cquery_count++; $cquery_time += $arQueryInfo["TIME"]; } } $comps_count = 0; $comps_time = 0.0; if($cquery_count > 0) { $query_count = $cquery_count; $query_time = $cquery_time; } else { $query_count = intval($DB->cntQuery); $query_time = $DB->timeQuery > 0? $DB->timeQuery: 0; } foreach($APPLICATION->arIncludeDebug as $i => $ar) { if($slow_sql_log === "Y") { $cquery_count = 0; $cquery_time = 0; foreach($ar["QUERIES"] as $N => $arQueryInfo) { if($arQueryInfo["TIME"] < $slow_sql_time) unset($APPLICATION->arIncludeDebug[$i]["QUERIES"][$N]); else { $bHasSQL2Write = true; $cquery_count++; $cquery_time += $arQueryInfo["TIME"]; } } if($cquery_count == 0) unset($APPLICATION->arIncludeDebug[$i]); else { $query_count += $cquery_count; $query_time += $cquery_time; $comps_count++; $comps_time += $ar["TIME"]; } } else { $query_count += $ar["QUERY_COUNT"]; $query_time += $ar["QUERY_TIME"]; $comps_count++; $comps_time += $ar["TIME"]; } } 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"]), "QUERIES" => $query_count, "~QUERIES_TIME" => $query_time, "SQL_LOG" => $sql_log, "COMPONENTS" => $comps_count, "~COMPONENTS_TIME" => $comps_time, "~MENU_RECALC" => $APPLICATION->_menu_recalc_counter, ); CPerfomanceKeeper::SetPageTimes($START_EXEC_CURRENT_TIME, $arFields); if($slow_sql_log !== "Y" || $bHasSQL2Write) $HIT_ID = $DB->Add("b_perf_hit", $arFields); else $HIT_ID = false; if($HIT_ID && ($sql_log === "Y") && is_array($DB->arQueryDebug)) { $NN = 0; foreach($DB->arQueryDebug as $i => $arQueryInfo) { $module_id = false; $comp_id = false; foreach($arQueryInfo["TRACE"] as $i => $arCallInfo) { if(array_key_exists("file", $arCallInfo)) { $file = strtolower(str_replace("\\", "/", $arCallInfo["file"])); if(!$module_id && substr($file, -12) != "database.php") { if(preg_match("#.*/bitrix/modules/(.+?)/#", $file, $match)) { $module_id = $match[1]; } } if(!$comp_id && preg_match("#.*/bitrix/components/(.+?)/(.+?)/#", $file, $match)) { $comp_id = $match[1].":".$match[2]; } if($module_id && $comp_id) break; } } $arFields = array( "HIT_ID" => $HIT_ID, "NN" => ++$NN, "QUERY_TIME" => $arQueryInfo["TIME"], "MODULE_NAME" => $module_id, "COMPONENT_NAME" => $comp_id, "SQL_TEXT" => $arQueryInfo["QUERY"], ); $SQL_ID = $DB->Add("b_perf_sql", $arFields, array("SQL_TEXT")); if($SQL_ID && COption::GetOptionString("perfmon", "sql_backtrace") === "Y") { $pl = strlen(rtrim($_SERVER["DOCUMENT_ROOT"], "/")); foreach($arQueryInfo["TRACE"] as $i => $arCallInfo) { $DB->Add("b_perf_sql_backtrace", array( "ID" => 1, "SQL_ID" => $SQL_ID, "NN" => $i, "FILE_NAME" => substr($arCallInfo["file"], $pl), "LINE_NO" => $arCallInfo["line"], "CLASS_NAME" => $arCallInfo["class"], "FUNCTION_NAME" => $arCallInfo["function"], )); } } } foreach($APPLICATION->arIncludeDebug as $ii => $ar) { $arFields = array( "HIT_ID" => $HIT_ID, "NN" => $ii, "CACHE_TYPE" => $ar["CACHE_TYPE"], "CACHE_SIZE" => intval($ar["CACHE_SIZE"]), "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); foreach($ar["QUERIES"] as $N => $arQueryInfo) { $module_id = false; $comp_id = false; foreach($arQueryInfo["TRACE"] as $i => $arCallInfo) { if(array_key_exists("file", $arCallInfo)) { $file = strtolower(str_replace("\\", "/", $arCallInfo["file"])); if(!$module_id && substr($file, -12) != "database.php") { if(preg_match("#.*/bitrix/modules/(.+?)/#", $file, $match)) { $module_id = $match[1]; } } if(!$comp_id && preg_match("#.*/bitrix/components/(.+?)/(.+?)/#", $file, $match)) { $comp_id = $match[1].":".$match[2]; } if($module_id && $comp_id) break; } } $arFields = array( "HIT_ID" => $HIT_ID, "COMPONENT_ID" => $COMP_ID, "NN" => ++$NN, "QUERY_TIME" => $arQueryInfo["TIME"], "MODULE_NAME" => $module_id, "COMPONENT_NAME" => $comp_id, "SQL_TEXT" => $arQueryInfo["QUERY"], ); $SQL_ID = $DB->Add("b_perf_sql", $arFields, array("SQL_TEXT")); $pl = strlen(rtrim($_SERVER["DOCUMENT_ROOT"], "/")); if($SQL_ID && COption::GetOptionString("perfmon", "sql_backtrace") === "Y") { foreach($arQueryInfo["TRACE"] as $i => $arCallInfo) { $DB->Add("b_perf_sql_backtrace", array( "ID" => 1, "SQL_ID" => $SQL_ID, "NN" => $i, "FILE_NAME" => substr($arCallInfo["file"], $pl), "LINE_NO" => $arCallInfo["line"], "CLASS_NAME" => $arCallInfo["class"], "FUNCTION_NAME" => $arCallInfo["function"], )); } } } } } global $perfmonErrors; if($HIT_ID && (count($perfmonErrors) > 0)) { foreach($perfmonErrors as $arError) { $arError["HIT_ID"] = $HIT_ID; $ERR_ID = $DB->Add("b_perf_error", $arError); } } $DB->ShowSqlStat = constant("PERFMON_STARTED"); } }