| 1 | #!/usr/bin/env php |
|---|
| 2 | <?php |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | require_once 'MyBench.php'; |
|---|
| 13 | |
|---|
| 14 | $opt_map = getopt('n:r:d:u:p:P:h:H:'); |
|---|
| 15 | |
|---|
| 16 | $num_kids = array_key_exists('n', $opt_map) ? $opt_map{n} : 10; |
|---|
| 17 | $num_runs = array_key_exists('r', $opt_map) ? $opt_map{r} : 100; |
|---|
| 18 | $db = array_key_exists('d', $opt_map) ? $opt_map{d} : 'mysql'; |
|---|
| 19 | $user = array_key_exists('u', $opt_map) ? $opt_map{u} : 'root'; |
|---|
| 20 | $pass = array_key_exists('p', $opt_map) ? $opt_map{p} : ''; |
|---|
| 21 | $port = array_key_exists('P', $opt_map) ? $opt_map{P} : 3306; |
|---|
| 22 | $host_map = Array( |
|---|
| 23 | master => array_key_exists('h', $opt_map) ? $opt_map{h} : 'localhost', |
|---|
| 24 | ); |
|---|
| 25 | if (array_key_exists('H', $opt_map)) { |
|---|
| 26 | if (is_array($opt_map{H})) { |
|---|
| 27 | foreach ($opt_map{H} as $slave) { |
|---|
| 28 | $host_map[slave][] = $slave; |
|---|
| 29 | } |
|---|
| 30 | } |
|---|
| 31 | else { |
|---|
| 32 | $host_map[slave][] = $opt_map{H}; |
|---|
| 33 | } |
|---|
| 34 | } |
|---|
| 35 | $dsn_map = Array( |
|---|
| 36 | master => Array($host_map[master].":".$port, $user, $pass, $db), |
|---|
| 37 | ); |
|---|
| 38 | if (is_array($host_map[slave])) { |
|---|
| 39 | foreach ($host_map{slave} as $host_slave) { |
|---|
| 40 | $dsn_map[slave][] |
|---|
| 41 | = Array($host_slave.":".$port, $user, $pass, $db); |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | function callback($id) { |
|---|
| 47 | global $num_runs; |
|---|
| 48 | global $dsn_map; |
|---|
| 49 | |
|---|
| 50 | $db_map = Array(); |
|---|
| 51 | $db_map[master] = mysql_connect($dsn_map[master][0], $dsn_map[master][1], $dsn_map[master][2]); |
|---|
| 52 | mysql_select_db($dsn_map[master][3], $db_map[master]); |
|---|
| 53 | |
|---|
| 54 | if (is_array($dsn_map[slave])) { |
|---|
| 55 | foreach ($dsn_map[slave] as $dsn_slave) { |
|---|
| 56 | $db_slave = mysql_connect($dsn_slave[0], $dsn_slave[1], $dsn_slave[2]); |
|---|
| 57 | mysql_select_db($dsn_slave[3], $db_slave); |
|---|
| 58 | $db_map[slave][] = $db_slave; |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | $cnt = 0; |
|---|
| 63 | $times = Array(); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | pcntl_signal(SIGHUP, create_function('','return 1;')); |
|---|
| 67 | sleep(600); |
|---|
| 68 | |
|---|
| 69 | while ($cnt < $num_runs) { |
|---|
| 70 | $t0 = microtime(true); |
|---|
| 71 | do_task($db_map); |
|---|
| 72 | $t1 = microtime(true) - $t0; |
|---|
| 73 | $times[] = $t1; |
|---|
| 74 | $cnt++; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | mysql_close($db_map[master]); |
|---|
| 79 | if (is_array($db_map[slave])) { |
|---|
| 80 | foreach ($db_map[slave] as $db_slave) { |
|---|
| 81 | mysql_close($db_slave); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | $num = count($times); |
|---|
| 86 | $tot = array_sum($times); |
|---|
| 87 | $avg = $tot / $num; |
|---|
| 88 | $r = Array($id, $num, min($times), max($times), $avg, $tot); |
|---|
| 89 | return $r; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | function do_task($db_map) { |
|---|
| 94 | $db_master = $db_map[master]; |
|---|
| 95 | if (is_array($db_map[slave])) { |
|---|
| 96 | $offset = rand() % sizeof($db_map[slave]); |
|---|
| 97 | $db_slave = $db_map[slave][$offset]; |
|---|
| 98 | } |
|---|
| 99 | else { |
|---|
| 100 | $db_slave = $db_map[master]; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | mysql_query('select * from user', $db_slave); |
|---|
| 105 | mysql_query('select * from user where user = "root"', $db_slave); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | $results = MyBench::fork_and_work($num_kids, 'callback'); |
|---|
| 110 | MyBench::complete_results('test', $results); |
|---|
| 111 | |
|---|
| 112 | exit(0); |
|---|