I am trying to embed a function on the site that controls the sql queries and the time spent on them. Naryl in the internet is an interesting solution. Work works, but it produces a 1 sql query all the time. I feel that the dog is buried in the line $ sql_time + = do_query ("SELECT * FROM SOME_TABLE"); but nothing works. Tell me, please, what's the matter?

//Засекаем время старта $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $tstart = $mtime; //Коннектимся к базе: include 'connect.php'; //Объявляем переменные $result=0; $qnum=0; //Объявляем нашу функцию function do_query($query){ global $result; global $qnum; $qnum++; $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $tstart = $mtime; $result = MYSQL_QUERY($query); $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $tend = $mtime; $tpassed = ($tend - $tstart); return($tpassed); } //Далее тело скрипта $sql_time+=do_query("SELECT * FROM SOME_TABLE"); //Обрабатываем данные while($row = mysql_fetch_array($result)){ print($row['Text']); } //Засекаем время окончания $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $tend = $mtime; $total = ($tend - $tstart); //Выдаем время: printf("SQL запросов: $qnum, время mysql: %f, всего затрачено: %f секунд !", $sql_time, $total); //Вычисляем процент времени: $sqlpercent = ($sql_time*100)/$total; print('Процент времени на MySQL: '. round($sqlpercent, 2) . '%'); 

    3 answers 3

    Something namudri. Notice the time, fulfilled the request, looked at the time again. Added query counter.

     // инициализировали счётчики $sqlCount = 0; $sqlTime = 0; // для каждого SQL запроса: $timeStart = microtime( TRUE); // выполняем SQL запрос ... $sqlTime += microtime( TRUE) - $timeStart; $sqlCount++; // Итоги printf( '%d запросов за %01.2f секунд.', $sqlCount, $sqlTime); 

      In general, the task is laughable easy. But you are cruelly covenant, you must be rehabilitated! Below is the class that you need to declare at the beginning of the start of the application, and at the end of the application output information about the requests. To connect to mySQL, use PDO. I also note that I did not check the code for performance, but in fact it is correct. First, we measure the time to connect to the database, then we measure the time of the request and the sample time.

       <?php class GetSQLTime extends PDO { private $db, $sqlNum, $allTime = 0; /** * connect */ function __construct () { $start = getmicrotime(); // parent::__construct($dsn, $username, $password, $driver); // $stop = getmicrotime(); // $this->allTime += $stop-$start; } /** * query */ function query($qu) { $this->sqlNum++; // $start = getmicrotime(); // $return = $db->query($qu); // $stop = getmicrotime(); // $this->allTime += $stop-$start; // return $return; } function fetch($qu) { // $start = getmicrotime(); // $return = $db->fetch($qu); // $stop = getmicrotime(); // $this->allTime += $stop-$start; // return $return; } /** * stop app */ function end() { return "sql запросов: ".$this->sqlNum."; Затрачено времени: ".$this->allTime.".\n<br>"; } /** * getmicrotime */ function getmicrotime() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } } $time = new GetSQLTime(); $query = $time->query('1234'); while($fetch = $query->fetch()) { var_dump($fetch); } $time->end(); ?> 
      • Thanks for the answer, but the problem is that I do not understand the OO PDO better than in ballet, but I don’t understand anything in ballet. I didn't understand anything in the code. 95% of the writing I see for the first time (this is a dark forest). "I did not check for performance, but in fact it is correct." This is of course great. But my code is “essentially correct”, and not only in essence, but also in workability. The problem is that I can not embed it in my script. - Garik Pokrovskij
      • Yes, there is nothing special to understand and you just need to try it! This is not the PLO, it is just a class. A class that contains functions. We simply declare a class, and then simply call the functions that this class contains. Try it! - lampa
      • one
        why nobody uses microtime( TRUE) , all the returned strings? =) Bad heredity :) - Sergiks
      • @sergiks I didn’t get into the code at all, but just made a handy tool :-) - lampa

      The easiest way:

       $start_time = microtime(true); $result = mysql_query($sql_string, $connection); $query_time = (microtime(true)-$start_time)/1000; echo "Запрос выполнялся $query_time миллисекунд"; 

      Something like this.