|
1 | 1 | #!/usr/bin/env php |
2 | 2 | <?php |
3 | | -require 'vendor/autoload.php'; |
4 | | - |
5 | | -use JmesPath\Runtime\RuntimeInterface; |
6 | | - |
7 | | -$runtime = \JmesPath\Env::createRuntime(); |
8 | | - |
9 | | -if (!isset($_SERVER['CACHE'])) { |
10 | | - $_SERVER['CACHE'] = false; |
11 | | -} |
12 | | - |
13 | | -if (isset($argv[1])) { |
14 | | - $dir = $argv[1]; |
15 | | -} else { |
16 | | - $dir = __DIR__ . '/../tests/compliance/perf'; |
17 | | -} |
| 3 | +require __DIR__ . '/../vendor/autoload.php'; |
18 | 4 |
|
| 5 | +$dir = isset($argv[1]) ? $argv[1] : __DIR__ . '/../tests/compliance/perf'; |
19 | 6 | is_dir($dir) or die('Dir not found: ' . $dir); |
20 | | - |
21 | 7 | // Warm up the runner |
22 | | -$runtime->search('abcdefg', array()); |
| 8 | +\JmesPath\Env::createRuntime()->search('foo', []); |
| 9 | +// Run the test suites. |
| 10 | +array_map('runSuite', glob($dir . '/*.json')); |
23 | 11 |
|
24 | | -$total = 0; |
25 | | -foreach (glob($dir . '/*.json') as $file) { |
26 | | - if (!strpos($file, 'syntax')) { |
27 | | - $total += runSuite($file, $runtime); |
28 | | - } |
29 | | -} |
30 | | - |
31 | | -echo "\nTotal time: {$total}ms\n"; |
32 | | - |
33 | | -function runSuite($file, RuntimeInterface $runtime) |
| 12 | +function runSuite($file) |
34 | 13 | { |
35 | 14 | $contents = file_get_contents($file); |
36 | 15 | $json = json_decode($contents, true); |
37 | | - $total = 0; |
38 | 16 | foreach ($json as $suite) { |
39 | 17 | foreach ($suite['cases'] as $case) { |
40 | | - $total += runCase( |
| 18 | + runCase( |
41 | 19 | str_replace(getcwd(), '.', $file), |
42 | 20 | $suite['given'], |
43 | | - $case['expression'], |
44 | | - $runtime |
| 21 | + $case['expression'] |
45 | 22 | ); |
46 | 23 | } |
47 | 24 | } |
48 | | - |
49 | | - return $total; |
50 | 25 | } |
51 | 26 |
|
52 | | -function runCase( |
53 | | - $file, |
54 | | - $given, |
55 | | - $expression, |
56 | | - RuntimeInterface $runtime |
57 | | -) { |
| 27 | +function runCase($file, $given, $expression) |
| 28 | +{ |
58 | 29 | $best = 99999; |
| 30 | + $runtime = \JmesPath\Env::createRuntime(); |
59 | 31 |
|
60 | 32 | for ($i = 0; $i < 1000; $i++) { |
61 | | - if (!$_SERVER['CACHE']) { |
62 | | - $runtime->clearCache(); |
63 | | - } |
64 | | - try { |
65 | | - $t = microtime(true); |
66 | | - $runtime->search($expression, $given); |
67 | | - $tryTime = (microtime(true) - $t) * 1000; |
68 | | - } catch (\Exception $e) { |
69 | | - // Failure test cases shouldn't be tested |
70 | | - return 0; |
71 | | - } |
| 33 | + $t = microtime(true); |
| 34 | + $runtime->search($expression, $given); |
| 35 | + $tryTime = (microtime(true) - $t) * 1000; |
72 | 36 | if ($tryTime < $best) { |
73 | 37 | $best = $tryTime; |
74 | 38 | } |
| 39 | + if (!getenv('CACHE')) { |
| 40 | + $runtime = \JmesPath\Env::createRuntime(); |
| 41 | + // Delete compiled scripts if not caching. |
| 42 | + if ($runtime instanceof \JmesPath\CompilerRuntime) { |
| 43 | + array_map('unlink', glob(sys_get_temp_dir() . '/jmespath_*.php')); |
| 44 | + } |
| 45 | + } |
75 | 46 | } |
76 | 47 |
|
77 | | - $template = "time: %fms, description: %s, name: %s\n"; |
| 48 | + $template = "time: %fms, %s: %s\n"; |
78 | 49 | $expression = str_replace("\n", '\n', $expression); |
79 | | - printf($template, $best, $file, $expression); |
80 | | - |
81 | | - return $best; |
| 50 | + printf($template, $best, basename($file), substr($expression, 0, 50)); |
82 | 51 | } |
0 commit comments