Debian Codegraph @

lib/3rdParty/composer/mtdowling/jmespath.php/bin/perf.php

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