perf.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env php
  2. <?php
  3. if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
  4. require __DIR__ . '/../vendor/autoload.php';
  5. } elseif (file_exists(__DIR__ . '/../../../autoload.php')) {
  6. require __DIR__ . '/../../../autoload.php';
  7. } else {
  8. throw new RuntimeException('Unable to locate autoload.php file.');
  9. }
  10. $xdebug = new \Composer\XdebugHandler\XdebugHandler('perf.php');
  11. $xdebug->check();
  12. unset($xdebug);
  13. $dir = isset($argv[1]) ? $argv[1] : __DIR__ . '/../tests/compliance/perf';
  14. is_dir($dir) or die('Dir not found: ' . $dir);
  15. // Warm up the runner
  16. \JmesPath\Env::search('foo', []);
  17. $total = 0;
  18. foreach (glob($dir . '/*.json') as $file) {
  19. $total += runSuite($file);
  20. }
  21. echo "\nTotal time: {$total}\n";
  22. function runSuite($file)
  23. {
  24. $contents = file_get_contents($file);
  25. $json = json_decode($contents, true);
  26. $total = 0;
  27. foreach ($json as $suite) {
  28. foreach ($suite['cases'] as $case) {
  29. $total += runCase(
  30. $suite['given'],
  31. $case['expression'],
  32. $case['name']
  33. );
  34. }
  35. }
  36. return $total;
  37. }
  38. function runCase($given, $expression, $name)
  39. {
  40. $best = 99999;
  41. $runtime = \JmesPath\Env::createRuntime();
  42. for ($i = 0; $i < 100; $i++) {
  43. $t = microtime(true);
  44. $runtime($expression, $given);
  45. $tryTime = (microtime(true) - $t) * 1000;
  46. if ($tryTime < $best) {
  47. $best = $tryTime;
  48. }
  49. if (!getenv('CACHE')) {
  50. $runtime = \JmesPath\Env::createRuntime();
  51. // Delete compiled scripts if not caching.
  52. if ($runtime instanceof \JmesPath\CompilerRuntime) {
  53. array_map('unlink', glob(sys_get_temp_dir() . '/jmespath_*.php'));
  54. }
  55. }
  56. }
  57. printf("time: %07.4fms name: %s\n", $best, $name);
  58. return $best;
  59. }