log

<?php

/**
* 
*/
class Timer 
{
    private $aTimes = array();

    function startTime($point)
    {
        $dat = getrusage( );

        $this->aTimes[$point]["start"] = microtime(TRUE);
        $this->aTimes[$point]["start_utime"] = 
            $dat["ru_utime.tv_sec"]*le6 + $dat["ru_utime.tv_usec"];
        $this->aTimes[$point]["start_stime"] = $dat["ru_stime.tv_sec"]*le6 + $dat["ru_stime.tv_usec"];
    }

    function stopTime($point, $comment="") {

        $dat = getrusage( );
        $this->aTimes[$point]["end"] = microtime(TRUE);
        $this->aTimes[$point]["end_utime"] = 
            $dat["ru_utime.tv_sec"]*le6 + $dat["ru_utime.tv_usec"];
        $this->aTimes[$point]["end_stime"] = $dat["ru_stime.tv_sec"]*le6 + $dat["ru_stime.tv_usec"];
        $this->aTimes[$point]["comment"] .= $comment ;
        $this->aTimes[$point]['sum'] += $this->aTimes[$point]["end"] - $this->aTimes[$point]["start"];
        $this->aTimes[$point]["sum_utime"] +=($this->aTimes[$point]["end_utime"] - $this->aTimes[$point]["start_utime"]) /le6;
        $this->aTimes[$point]["sum_stime"] +=($this->aTimes[$point]["end_stime"] - $this->aTimes[$point]["start_stime"]) /le6;
    }

    function logdata( ) {
        $query_logger = DBQueryLog::getInstance("DBQueryLog");
        $data["utime"] = $this->aTimes["Page"]["sum_utime"];
        $data["wtime"] = $this->aTimes["Page"]["sum"];
        $data["stime"] = $this->aTimes["Page"]["sum_stime"];
        $data["mysql_time"] = $this->aTimes["MySQL"]["sum"];
        $data["mysql_count_queries"] = $this->aTimes["MySQL"]["comment"];
        $data["sphinx_time"] = $this->aTimes["Sphinx"]["sum"];
        $query_logger->logProfilingData($data);
    }

    function getInstance( ) {

        static $instance;
        if(!isset($instance)) {


            $instance = new Timer( );
        }
        return ($instance);

    }
}

?>
<?php

class mysqlx extends mysqli {
    function query($query, $resultmode) {



        $timer = Timer::getInstance( );
        $timer->startTime("MySQL");
        $res = parent::query($query, $resultmode);
        $timer->stopTime("MySQL", "Query: $query\n");
        return $res;
    }




}

?>
<?php

class DBQueryLog {
    function logProfilingData($data) {
        $table_name = "logs.performance_log_". @date("ymd");


        $query = "INSERT DELAYED INTO $table_name (ip, page, utime,
            wtime, stime, mysql_time , sphinx_time, mysql_count_queries,
            mysql_queies, user_agent,referer) VALUES (.. data ..)";

        $res = $this->mysqlx->query($query);

        if ((!$res) && ($this->mysqlx->error == 1146)) {
                $res = $this->mysqlx->query(
                        "CREATE TABLE $table_name LIKE logs.performance_log_template"

                    );
                $res = $this->mysqlx->query($query);


        } 




    }
}

?>

<?php

$timer = Timer::getInstance( );
$timer->startTime('Page');

?>

<?php

$timer = Timer::getInstance( );
$timer->stopTime('Page');
$timer->logdata( );

?>

CREATE TABLE cat.performance_log_template (

ip INT UNSIGNED NOT NULL,
page VARCHAR(255) NOT NULL,
utime FLOAT NOT NULL,
wtime FLOAT NOT NULL,
mysql_time FLOAT NOT NULL,
sphinx_time FLOAT NOT NULL,
mysql_count_queries INT UNSIGNED NOT NULL,
mysql_queries TEXT NOT NULL,
stime FLOAT NOT NULL,
logged TIMESTAMP NOT NULL default CURRENT_TIMESTAMP ON update CURRENT_TIMESTAMP,
user_agent VARCHAR(255) NOT NULL,
referer VARCHAR(255) NOT NULL

) ENGINE = ARCHIVE;

你可能感兴趣的:(log)