📜 ⬆️ ⬇️

Testing code performance on multiple versions of PHP

Good day to all. The other day it took to check the performance of 4 variants of code on different versions of PHP (in the amount of about 20). And initially it was clear that the case will not be limited to 4 copies - in the future there will be more such tests. Manually doing all this is very tedious, so 2 scripts were written that I would like to share with you.

The whole workflow with them is as follows. You download in one folder all the necessary versions of PHP, unpack them and collect. It is necessary to get the following structure:
/ some_dir /.
/some_dir/php-5.1.6
/some_dir/php-5.2.17
/some_dir/php-5.3.1
/ some_dir / ...

Where php-5.1.6, php-5.2.17, etc. are directories with the corresponding versions of the interpreter. Further there put 2 scripts. The first is run.php
<?php # ,     . #       test.php. define('TEST_DONE_STR', 'TEST DONE'); define('TEST_PATH', dirname(__FILE__)); define('CLI_PATH', 'sapi/cli/php'); #   PHP    $dirs = array(); foreach(scandir('./') as $item) { if(preg_match("#^php-#", $item) AND is_dir($item)) { if(file_exists($item . "/sapi/cli/php")) $dirs[] = $item; else print "$item - php-cli not found\n"; } } #      test.php foreach($dirs as $dir) { $output = array(); exec(TEST_PATH . "/$dir/" . CLI_PATH . " " . TEST_PATH . "/test.php", $output); print "$dir - " . (implode("", $output) == TEST_DONE_STR ? "OK" : "FAILED") . "\n"; } 

As can be seen from the sources, it first detects the PHP directories adjacent to it by the “php-” signature, and then with the help of cli / php, each of them executes the test.php script. If, after executing the test script, the line from the constant “TEST_DONE_STR” falls into stdout - the test is passed and run.php displays “php - *. *. * - OK”. Otherwise - “php - *. *. * - FAILED”.
test.php should contain the code you need to check. It can be in any form. The main thing is that in case of successful completion the above described line is displayed. For example, I post the contents of one of my tests:
 <?php # ,     . #       run.php. define('TEST_DONE_STR', 'TEST DONE'); #   ob_start(); include("/etc/passwd/../passwd"); $result = ob_get_contents(); ob_end_clean(); #      if(substr_count($result, 'root:')) print TEST_DONE_STR; 

Example output run.php:
user @ comp: ~ / php $ php run.php
php-4.3.1 - php-cli not found
php-5.2.16 - php-cli not found
php-5.2.17-suhosin - php-cli not found
php-4.3.10 - OK
php-4.4.9 - OK
php-5.0.5 - OK
php-5.1.6 - OK
php-5.1.6-suhosin - FAILED
php-5.2.0 - OK
php-5.2.17 - OK
php-5.2.2 - OK
php-5.2.3 - OK
php-5.2.4 - OK
php-5.2.9 - OK
php-5.3.0 - FAILED
php-5.3.1 - FAILED
php-5.3.10 - FAILED
php-5.3.20 - FAILED
php-5.3.5 - FAILED


I hope they will be useful to you. I have already saved a lot of time :)

')

Source: https://habr.com/ru/post/169311/


All Articles