11#!/usr/bin/env php
22<?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 ' ;
184
5+ $ dir = isset ($ argv [1 ]) ? $ argv [1 ] : __DIR__ . '/../tests/compliance/perf ' ;
196is_dir ($ dir ) or die ('Dir not found: ' . $ dir );
20-
217// Warm up the runner
22- $ runtime -> search ('abcdefg ' , array () );
8+ \ JmesPath \Env:: search ('foo ' , [] );
239
2410$ total = 0 ;
2511foreach (glob ($ dir . '/*.json ' ) as $ file ) {
26- if (!strpos ($ file , 'syntax ' )) {
27- $ total += runSuite ($ file , $ runtime );
28- }
12+ $ total += runSuite ($ file );
2913}
14+ echo "\nTotal time: {$ total }\n" ;
3015
31- echo "\nTotal time: {$ total }ms \n" ;
32-
33- function runSuite ($ file , RuntimeInterface $ runtime )
16+ function runSuite ($ file )
3417{
3518 $ contents = file_get_contents ($ file );
3619 $ json = json_decode ($ contents , true );
@@ -40,43 +23,37 @@ function runSuite($file, RuntimeInterface $runtime)
4023 $ total += runCase (
4124 str_replace (getcwd (), '. ' , $ file ),
4225 $ suite ['given ' ],
43- $ case ['expression ' ],
44- $ runtime
26+ $ case ['expression ' ]
4527 );
4628 }
4729 }
48-
4930 return $ total ;
5031}
5132
52- function runCase (
53- $ file ,
54- $ given ,
55- $ expression ,
56- RuntimeInterface $ runtime
57- ) {
33+ function runCase ($ file , $ given , $ expression )
34+ {
5835 $ best = 99999 ;
36+ $ runtime = \JmesPath \Env::createRuntime ();
5937
6038 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- }
39+ $ t = microtime (true );
40+ $ runtime ($ expression , $ given );
41+ $ tryTime = (microtime (true ) - $ t ) * 1000 ;
7242 if ($ tryTime < $ best ) {
7343 $ best = $ tryTime ;
7444 }
45+ if (!getenv ('CACHE ' )) {
46+ $ runtime = \JmesPath \Env::createRuntime ();
47+ // Delete compiled scripts if not caching.
48+ if ($ runtime instanceof \JmesPath \CompilerRuntime) {
49+ array_map ('unlink ' , glob (sys_get_temp_dir () . '/jmespath_*.php ' ));
50+ }
51+ }
7552 }
7653
77- $ template = "time: %fms, description: %s, name : %s \n" ;
54+ $ template = "time: %fms, %s : %s \n" ;
7855 $ expression = str_replace ("\n" , '\n ' , $ expression );
79- printf ($ template , $ best , $ file, $ expression );
56+ printf ($ template , $ best , basename ( $ file), substr ( $ expression, 0 , 50 ) );
8057
8158 return $ best ;
8259}
0 commit comments