Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Klasa do sprawdzania czasu wykonywania zapytan SQL
rahul
post
Post #1





Grupa: Zarejestrowani
Postów: 71
Pomógł: 0
Dołączył: 5.03.2011

Ostrzeżenie: (0%)
-----


  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. }


Ten post edytował rahul 7.01.2012, 01:44:05
Go to the top of the page
+Quote Post
adbacz
post
Post #2





Grupa: Zarejestrowani
Postów: 532
Pomógł: 24
Dołączył: 15.04.2011
Skąd: Kalisz

Ostrzeżenie: (0%)
-----


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.
Go to the top of the page
+Quote Post
droslaw
post
Post #3





Grupa: Zarejestrowani
Postów: 98
Pomógł: 33
Dołączył: 10.05.2011
Skąd: Krak

Ostrzeżenie: (0%)
-----


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);


Ten post edytował droslaw 8.01.2012, 16:45:39
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.08.2025 - 09:27