Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Klasa do sprawdzania czasu wykonywania zapytan SQL
Forum PHP.pl > Forum > PHP > Object-oriented programming
rahul
  1. Czesc. Mam taka prosta klase/zbior funkcji.. wykonujace proste zapytania do bazy i mierzace czas metoda microtime. Zmienna result zwraca nieraz ujemny wynik, dlaczego tak ? (funkja handler)
  2.  
  3. class testDatabase_Model{
  4.  
  5. private $db;
  6. private $db_type;
  7. private $names;
  8. private $starttime;
  9. private $endtime;
  10. private $result;
  11. private $sql;
  12.  
  13. public function PDO_Connection() {
  14.  
  15. $this->db = PDO_Database::GetInstance();
  16. $this->db_type = 'PDO';
  17. }
  18.  
  19. public function MySQLi_Connection()
  20. {
  21. $this->db = Mysql_Database::GetInstance();
  22. $this->db_type = 'MySQLi';
  23. }
  24.  
  25. public function getStarttime() {return $this->starttime;}
  26. public function getEndtime() return $this->endtime;}
  27. public function getResult() {return $this->result;}
  28. public function getDb_type() {return $this->db_type; }
  29.  
  30. public function ExecuteQuery($sql)
  31. {
  32. if($this->db_type == "MySQLi")
  33. {
  34. if(! mysqli_query($this->db,$sql))
  35. {
  36. die('mysqli driver '.$sql.'failure');
  37. }
  38. }
  39. if($this->db_type == "PDO")
  40. {
  41. try{
  42. $this->db->query($sql);
  43.  
  44. }catch (Exception $e)
  45. {
  46. die($e);
  47. }
  48. }
  49. }
  50.  
  51. public function insert()
  52. {
  53. $sql = "INSERT INTO speed_test VALUES";
  54. for ($i = 0; $i< 10000; $i ++)
  55. {
  56. $name = $this->getRandomName();
  57. $date = $this->getRandomDate();
  58. $age = $this->getRandomAge();
  59. $sql .= " ('' , '$name' ,'$date' , '$age' ) ,";
  60. }
  61. $sql .= "('' , '' , '' , '' ) ";
  62. $this->sql = $sql;
  63. $this->handler();
  64. }
  65.  
  66. public function select()
  67. {
  68. $this->sql = "SELECT * FROM speed_test";
  69. $this->handler();
  70. }
  71.  
  72. public function update()
  73. {
  74. $this->sql = "UPDATE speed_test set name='Stefan', age=99";
  75.  
  76. $this->handler();
  77. }
  78.  
  79. public function handler()
  80. {
  81. $this->starttime = microtime();
  82. $this->ExecuteQuery($this->sql);
  83. $this->endtime =microtime();
  84. round($this->result = ($this->endtime) - ($this->starttime));
  85. }
  86. }
adbacz
Zmień sobie ciało metody getResutl() na takie coś:
  1. return 'Start: '.$this->starttime.'<br />End: '.$this->endtime.'<br />Result: '.$this->result;

I zobacz, co Ci zwraca microtime() do poszczególnych pól. I w funkcji handler() nie używaj round(), tylko zapisuj czysty wynik.

Ja miałem podobny problem gdy mierzyłem czas wykonywania całego skryptu, ale tylko przez jakiś czas, później samo przeszło więc się nad tym nie zastanawiałem.
droslaw
microtime zwraca czas jako string w formie 'czesc_ulamkowa czesc_calkowita'. Odejmując takie wartości nie można spodziewać się sensownych wyników.
Zamiast
  1. $this->starttime = microtime();
  2. ...
  3. $this->endtime =microtime();
  4. round($this->result = ($this->endtime) - ($this->starttime));

daj
  1. $this->starttime = array_sum(explode(' ',microtime()));
  2. ..
  3. $this->starttime = array_sum(explode(' ',microtime()));
  4. $this->result = ($this->endtime) - ($this->starttime)

Edit:
Zerknąlem do manuala i okazuje się że microtime(true) zwraca czas jako float.
Dobrze będzie:
  1. $this->starttime = microtime(true);
  2. ...
  3. $this->endtime =microtime(true);
  4. $this->result = ($this->endtime) - ($this->starttime);
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.