performance.php 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. require '../lib/SqlFormatter.php';
  3. //this is the default value
  4. //set to '0' to disable caching
  5. //a value between 10 and 20 seems to give the best result
  6. SqlFormatter::$max_cachekey_size = 15;
  7. $contents = file('sql.sql');
  8. //track time and memory usage
  9. $start = microtime(true);
  10. $ustart = memory_get_usage(true);
  11. //track number of queries and size of queries
  12. $queries = 0;
  13. $bytes = 0;
  14. //format each query 3 times
  15. for ($i =0; $i<3; $i++) {
  16. foreach ($contents as $query) {
  17. //this tries to mix up the queries so we aren't just running the same thing a bunch of times
  18. $query = str_replace('tablename', rand(1, 10000), $query);
  19. //do formatting and highlighting
  20. SqlFormatter::format($query);
  21. $queries++;
  22. $bytes += strlen($query);
  23. }
  24. }
  25. $uend = memory_get_usage(true);
  26. $end = microtime(true);
  27. echo "<p>Formatted $queries queries.</p>";
  28. echo "<p>Average query length of ".number_format($bytes/$queries,5)." characters</p>";
  29. echo "<p>Took ".number_format($end-$start,5)." seconds total, ".number_format(($end-$start)/$queries,5)." seconds per query, ".number_format(1000*($end-$start)/$bytes,5)." seconds per 1000 characters</p>";
  30. echo "<p>Used ".number_format($uend-$ustart)." bytes of memory</p>";
  31. echo "<h3>Cache Stats</h3><pre>".print_r(SqlFormatter::getCacheStats(),true)."</pre>";