Danack

MySQL/MySQLi/PDO crap speed test

Aug 11th, 2013
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1.  
  2. 100 mysql-buffered 0.00051
  3. 100 mysql-unbuffered 0.00050
  4. 100 mysqli-prepared 0.00150
  5. 100 mysqli-buffered 0.00191
  6. 100 mysqli-unbuffered 0.00187
  7. 100 pdo-buffered 0.00169
  8. 100 pdo-unbuffered 0.00170
  9. 500 mysql-buffered 0.00133
  10. 500 mysql-unbuffered 0.00172
  11. 500 mysqli-prepared 0.00132
  12. 500 mysqli-buffered 0.00271
  13. 500 mysqli-unbuffered 0.00281
  14. 500 pdo-buffered 0.00220
  15. 500 pdo-unbuffered 0.00290
  16. 1000 mysql-buffered 0.00227
  17. 1000 mysql-unbuffered 0.00328
  18. 1000 mysqli-prepared 0.00172
  19. 1000 mysqli-buffered 0.00308
  20. 1000 mysqli-unbuffered 0.00391
  21. 1000 pdo-buffered 0.00423
  22. 1000 pdo-unbuffered 0.00466
  23. 5000 mysql-buffered 1.51175
  24. 5000 mysql-unbuffered 0.01328
  25. 5000 mysqli-prepared 0.00209
  26. 5000 mysqli-buffered 3.01241
  27. 5000 mysqli-unbuffered 1.51294
  28. 5000 pdo-buffered 3.01339
  29. 5000 pdo-unbuffered 3.01633
  30. 10000 mysql-buffered 1.52150
  31. 10000 mysql-unbuffered 0.02688
  32. 10000 mysqli-prepared 0.00140
  33. 10000 mysqli-buffered 4.51979
  34. 10000 mysqli-unbuffered 7.52161
  35. 10000 pdo-buffered 4.52521
  36. 10000 pdo-unbuffered 6.02893
  37.  
  38.  
  39.  
  40.  
  41. <?php
  42.  
  43.  
  44. $table = 'test1.test2';
  45.  
  46. benchmark($table, 100);
  47. benchmark($table, 500);
  48. benchmark($table, 1000);
  49. benchmark($table, 5000);
  50. benchmark($table, 10000);
  51. //benchmark($table, 50000);
  52. //benchmark($table, 100000);
  53.  
  54.  
  55.  
  56.  
  57.  
  58. function benchmark($table, $size) {
  59. //$ipAddress = '127.0.0.1';
  60. $ipAddress = '10.0.2.2';
  61.  
  62. $password = "password";
  63. $runPDO = true;
  64.  
  65.  
  66. $start = microtime(true);
  67. mysql_connect($ipAddress, 'root', $password);
  68. echo "Connect ".number_format(microtime(true)-$start, 5)."\n";
  69.  
  70. $start = microtime(true);
  71.  
  72. mysql_query('drop table if exists '.$table);
  73. mysql_query("CREATE TABLE $table (id int(11) AUTO_INCREMENT,
  74. str1 varchar(255), str2 varchar(255), PRIMARY KEY (id)) ENGINE=INNODB");
  75. mysql_query("begin");
  76. for ($i=0; $i<$size; $i++) {
  77. mysql_query("insert into $table values(null, 'hello$i', 'world$i')");
  78. }
  79. mysql_query("commit");
  80.  
  81. echo "table create ".number_format(microtime(true)-$start, 5)."\n";
  82.  
  83.  
  84. if ($runPDO) {
  85. // warm up mysql cache
  86. $db = new PDO('mysql:host='.$ipAddress, 'root', $password);
  87. foreach ($db->query('select * from '.$table) as $vals){
  88. $test = $vals;
  89. }
  90. }
  91.  
  92. $start = microtime(true);
  93. mysql_connect($ipAddress, 'root', $password);
  94. $result = mysql_query('select * from '.$table);
  95. if (!$result) {
  96. echo "Mysql error: ".mysql_error();
  97.  
  98. exit(0);
  99. }
  100.  
  101. while ($row = mysql_fetch_assoc($result)){
  102. $test = $row;
  103. }
  104. echo $size.' mysql-buffered '.number_format(microtime(true)-$start, 5)."\n";
  105.  
  106. $start = microtime(true);
  107. mysql_connect($ipAddress, 'root', $password);
  108. $result = mysql_unbuffered_query('select * from '.$table);
  109. while ($row = mysql_fetch_assoc($result)) $test = $row;
  110. echo $size.' mysql-unbuffered '.number_format(microtime(true)-$start, 5)."\n";
  111.  
  112.  
  113. $start = microtime(true);
  114.  
  115.  
  116. $mysqli = new MySQLi($ipAddress, "root", $password);
  117. $query = 'select str1, str2 , id from '.$table;
  118. $statement = $mysqli->prepare($query);
  119. $statement->bind_result($str1, $str2, $id);
  120. while ($statement->fetch()) {
  121. }
  122. $statement->close();
  123. //$mysqli->close();
  124.  
  125.  
  126. echo $size.' mysqli-prepared '.number_format(microtime(true)-$start, 5)."\n";
  127.  
  128.  
  129. $start = microtime(true);
  130. $db = mysqli_connect($ipAddress, 'root', $password);
  131. foreach (mysqli_query($db, 'select * from '.$table) as $row) $test = $row;
  132. echo $size.' mysqli-buffered '.number_format(microtime(true)-$start, 5)."\n";
  133.  
  134. $start = microtime(true);
  135. $db = mysqli_connect($ipAddress, 'root', $password);
  136. foreach (mysqli_query($db, 'select * from '.$table, MYSQLI_USE_RESULT)
  137. as $row) $test = $row;
  138. echo $size.' mysqli-unbuffered '.number_format(microtime(true)-$start, 5)."\n";
  139.  
  140. if ($runPDO) {
  141. $start = microtime(true);
  142. $db = new PDO('mysql:host='.$ipAddress, 'root', $password);
  143. foreach ($db->query('select * from '.$table) as $vals) $test = $vals;
  144. echo $size.' pdo-buffered '.number_format(microtime(true)-$start, 5)."\n";
  145.  
  146. $start = microtime(true);
  147. $db = new PDO('mysql:host='.$ipAddress, 'root', $password,
  148. array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false));
  149. foreach ($db->query('select * from '.$table) as $vals) $test = $vals;
  150. echo $size.' pdo-unbuffered '.number_format(microtime(true)-$start, 5)."\n";
  151. }
  152.  
  153. }
  154.  
  155.  
  156.  
  157. ?>
Advertisement
Add Comment
Please, Sign In to add comment