perf.php 1.5 KB

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