Skip to content
Permalink
Browse files

Performance tweaks

  • Loading branch information...
sergejey committed Jun 5, 2019
1 parent 19a6beb commit 324e80b23168fa6f076a853d97fb21cbf8603774
263 index.php
@@ -1,130 +1,133 @@
<?php
/**
* Main project script
*
* @package MajorDoMo
* @author Serge Dzheigalo <jey@tut.by>
* @url http://smartliving.ru
* @version 1.2
*/
include_once("./lib/perfmonitor.class.php");
startMeasure('TOTAL');
include_once("./config.php");
include_once("./lib/loader.php");
// start calculation of execution time
include_once(DIR_MODULES . "application.class.php");
$session = new session("prj");
include_once("./load_settings.php");
$use_caching = 0;
$cache_expire = 60 * 60; // 60 minutes cache expiration time
$cached_result = '';
$req_url = $_SERVER['REQUEST_URI'];
if ($req_url == '/')
$req_url = '/index.html';
if ($use_caching && preg_match('/^\/([\/\w_-]+)\.html$/', $req_url, $matches) && $_SERVER['REQUEST_METHOD'] != 'POST')
{
$cache_filename = preg_replace('/\W/', '_', $matches[1]) . '.html';
if (file_exists(ROOT . 'cms/cached/' . $cache_filename))
{
if ((time() - filemtime(ROOT . 'cms/cached/' . $cache_filename)) <= $cache_expire)
{
$cached_result = LoadFile(ROOT . 'cms/cached/' . $cache_filename);
}
else
{
unlink(ROOT . 'cms/cached/' . $cache_filename);
}
}
}
if ($cached_result == '')
{
if (!file_exists(DIR_MODULES . 'control_modules/installed'))
{
include_once(DIR_MODULES . "control_modules/control_modules.class.php");
$ctl = new control_modules();
}
$app = new application();
if ($md != $app->name)
$app->restoreParams();
else
$app->getParams();
if ($app->action != '' && $app->action != 'docs')
$fake_doc = '';
$result = $app->run();
$result = str_replace("nf.php", "index.php", $result);
}
else
{
// show cached result
$result = $cached_result;
}
require(ROOT.'lib/utils/postprocess_result.inc.php');
/**
* Echo large text
* @param mixed $string Text
* @param mixed $bufferSize Buffer size
* @return void
*/
function echobig($string, $bufferSize = 8192)
{
$chars = strlen($string) - 1;
for ($start = 0; $start <= $chars; $start += $bufferSize)
{
echo substr($string,$start,$bufferSize);
}
}
startMeasure('final_echo');
if (!headers_sent()) {
header("HTTP/1.0: 200 OK\n");
header('Content-Type: text/html; charset=utf-8');
header('Access-Control-Allow-Origin: *');
if (!ob_get_length()) {
if(!ob_start("ob_gzhandler")) ob_start();
}
}
echobig($result);
endMeasure('final_echo', 1);
if ($cache_filename != '' && $cached_result == '')
{
SaveFile(ROOT . 'cms/cached/' . $cache_filename, $result);
}
$session->save();
if (isset($wsClient) && $wsClient) {
$wsClient->disconnect();
}
// end calculation of execution time
endMeasure('TOTAL');
// print performance report
performanceReport();
ob_end_flush();
<?php
/**
* Main project script
*
* @package MajorDoMo
* @author Serge Dzheigalo <jey@tut.by>
* @url http://smartliving.ru
* @version 1.2
*/
include_once("./lib/perfmonitor.class.php");
startMeasure('TOTAL');
include_once("./config.php");
include_once("./lib/loader.php");
// start calculation of execution time
startMeasure('prepare');
include_once(DIR_MODULES . "application.class.php");
$session = new session("prj");
startMeasure('load_settings');
include_once("./load_settings.php");
endMeasure('load_settings');
$use_caching = 0;
$cache_expire = 60 * 60; // 60 minutes cache expiration time
$cached_result = '';
$req_url = $_SERVER['REQUEST_URI'];
if ($req_url == '/')
$req_url = '/index.html';
if ($use_caching && preg_match('/^\/([\/\w_-]+)\.html$/', $req_url, $matches) && $_SERVER['REQUEST_METHOD'] != 'POST')
{
$cache_filename = preg_replace('/\W/', '_', $matches[1]) . '.html';
if (file_exists(ROOT . 'cms/cached/' . $cache_filename))
{
if ((time() - filemtime(ROOT . 'cms/cached/' . $cache_filename)) <= $cache_expire)
{
$cached_result = LoadFile(ROOT . 'cms/cached/' . $cache_filename);
}
else
{
unlink(ROOT . 'cms/cached/' . $cache_filename);
}
}
}
endMeasure('prepare');
if ($cached_result == '')
{
if (!file_exists(DIR_MODULES . 'control_modules/installed'))
{
include_once(DIR_MODULES . "control_modules/control_modules.class.php");
$ctl = new control_modules();
}
$app = new application();
if ($md != $app->name)
$app->restoreParams();
else
$app->getParams();
if ($app->action != '' && $app->action != 'docs')
$fake_doc = '';
$result = $app->run();
$result = str_replace("nf.php", "index.php", $result);
}
else
{
// show cached result
$result = $cached_result;
}
require(ROOT.'lib/utils/postprocess_result.inc.php');
/**
* Echo large text
* @param mixed $string Text
* @param mixed $bufferSize Buffer size
* @return void
*/
function echobig($string, $bufferSize = 8192)
{
$chars = strlen($string) - 1;
for ($start = 0; $start <= $chars; $start += $bufferSize)
{
echo substr($string,$start,$bufferSize);
}
}
startMeasure('final_echo');
if (!headers_sent()) {
header("HTTP/1.0: 200 OK\n");
header('Content-Type: text/html; charset=utf-8');
header('Access-Control-Allow-Origin: *');
if (!ob_get_length()) {
if(!ob_start("ob_gzhandler")) ob_start();
}
}
echobig($result);
endMeasure('final_echo', 1);
if ($cache_filename != '' && $cached_result == '')
{
SaveFile(ROOT . 'cms/cached/' . $cache_filename, $result);
}
$session->save();
if (isset($wsClient) && $wsClient) {
$wsClient->disconnect();
}
// end calculation of execution time
endMeasure('TOTAL');
// print performance report
performanceReport();
ob_end_flush();
@@ -407,8 +407,10 @@ function deleteScheduledJob($id)
*/
function setTimeOut($title, $commands, $timeout)
{
startMeasure('setTimeout');
clearTimeOut($title);
$res = addScheduledJob($title, $commands, time() + $timeout);
endMeasure('setTimeout');
return $res;
}
@@ -739,6 +741,7 @@ function getURLBackground($url, $cache = 0, $username = '', $password = '')
*/
function getURL($url, $cache = 0, $username = '', $password = '', $background = false)
{
startMeasure('getURL');
// DebMes($url,'urls');
$filename_part = preg_replace('/\W/is', '_', str_replace('http://', '', $url));
if (strlen($filename_part) > 200) {
@@ -750,8 +753,7 @@ function getURL($url, $cache = 0, $username = '', $password = '', $background =
try {
//DebMes('Geturl started for '.$url. ' Source: ' .debug_backtrace()[1]['function'], 'geturl');
$startTime = getmicrotime();
startMeasure('curl_prepare');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0');
@@ -807,17 +809,22 @@ function getURL($url, $cache = 0, $username = '', $password = '', $background =
curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfname);
curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfname);
endMeasure('curl_prepare');
startMeasure('curl_exec');
$result = curl_exec($ch);
endMeasure('curl_exec');
if (curl_errno($ch) && !$background) {
startMeasure('curl_post');
if (!$background && curl_errno($ch)) {
$errorInfo = curl_error($ch);
$info = curl_getinfo($ch);
$backtrace = debug_backtrace();
$callSource = $backtrace[1]['function'];
DebMes("GetURL to $url (source " . $callSource . ") finished with error: \n" . $errorInfo . "\n" . json_encode($info),'geturl_error');
}
curl_close($ch);
endMeasure('curl_post');
} catch (Exception $e) {
@@ -832,6 +839,9 @@ function getURL($url, $cache = 0, $username = '', $password = '', $background =
$result = LoadFile($cache_file);
}
endMeasure('getURL');
return $result;
}
@@ -951,9 +961,28 @@ function isOnline($host)
*/
function checkAccess($object_type, $object_id)
{
global $access_rules_cached;
startMeasure('checkAccess');
if (!isset($access_rules_cached)) {
$all_rules=SQLSelect("SELECT OBJECT_TYPE, OBJECT_ID FROM security_rules");
foreach($all_rules as $rule) {
$access_rules_cached[$rule['OBJECT_TYPE'].$rule['OBJECT_ID']]=1;
}
}
if (!isset($access_rules_cached[$object_type.$object_id])) {
endMeasure('checkAccess');
return true;
}
include_once(DIR_MODULES . 'security_rules/security_rules.class.php');
$sc = new security_rules();
return $sc->checkAccess($object_type, $object_id);
$result = $sc->checkAccess($object_type, $object_id);
endMeasure('checkAccess');
return $result;
}
/**

0 comments on commit 324e80b

Please sign in to comment.
You can’t perform that action at this time.