Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / cli / runperf.php
1 <?php
2
3 chdir( '..' );
4
5 require 'testcontainer/rb.phar';
6
7 //load core classes
8 require 'RedUNIT.php';
9 require 'RedUNIT/Base.php';
10 require 'RedUNIT/Base/Performance.php';
11
12 error_reporting( E_ALL );
13
14 //Load configuration file
15 if ( file_exists( 'config/test.ini' ) ) {
16         $ini = parse_ini_file( "config/test.ini", TRUE );
17 } else {
18         die( 'Cant find configuration file.' );
19 }
20
21 //Configure the databases
22 if ( isset( $ini['mysql'] ) ) {
23         $dsn = "mysql:host={$ini['mysql']['host']};dbname={$ini['mysql']['schema']}";
24
25         R::addDatabase( 'mysql', $dsn, $ini['mysql']['user'], $ini['mysql']['pass'], FALSE );
26
27         R::selectDatabase( 'mysql' );
28
29         R::exec( ' SET GLOBAL sql_mode="" ' );
30 }
31
32 if ( isset( $ini['pgsql'] ) ) {
33         $dsn = "pgsql:host={$ini['pgsql']['host']};dbname={$ini['pgsql']['schema']}";
34
35         R::addDatabase( 'pgsql', $dsn, $ini['pgsql']['user'], $ini['pgsql']['pass'], FALSE );
36 }
37
38 if ( isset( $ini['sqlite'] ) ) {
39         R::addDatabase( 'sqlite', 'sqlite:' . $ini['sqlite']['file'], NULL, NULL, FALSE );
40 }
41
42 R::selectDatabase( 'sqlite' );
43
44 // Function to activate a driver
45 function activate_driver( $d )
46 {
47         R::selectDatabase( $d );
48 }
49 $test = new \RedUNIT\Base\Performance();
50
51 $drivers       = $test->getTargetDrivers();
52
53 foreach ( $drivers as $driver ) {
54
55                         if ( !isset( $ini[$driver] ) ) continue;
56                         if ( !isset( $_SERVER['argv'][1])) die('Missing parameter. Usage: php runperf.php <testname> <TIMES> ');
57
58                         $method = $_SERVER['argv'][1];
59
60                         if ($method === 'setup') {
61
62                                 echo 'Setup...'.PHP_EOL;
63
64                                 $test->$method();
65
66                                 echo 'READY'.PHP_EOL;
67
68                         } else {
69
70                                 $times = 100;
71                                 if (isset($_SERVER['argv'][2])) {
72                                         $times = (int) $_SERVER['argv'][2];
73                                 }
74
75                                 echo "Performing test: $method with driver $driver ".PHP_EOL;
76
77                                 for ($j=0; $j<$times; $j++) {
78
79                                         $t1 = microtime( TRUE );
80
81                                         $test->$method();
82
83                                         $t2 = microtime( TRUE );
84
85                                         $d[] = ($t2 - $t1);
86
87                                 }
88
89                                 $s = array_sum($d);
90                                 $a = ($s / $times);
91                                 $mx = max($d);
92                                 $mn = min($d);
93
94                                 echo PHP_EOL."AVG: $a, MAX: $mx, MIN: $mn, TOTAL: $s, TIMES: $times ".PHP_EOL;
95                         }
96 }