root/lang/php/phpmybench/trunk/bench_example_adodb.php

Revision 13070, 3.1 kB (checked in by hansode, 4 years ago)

Added examples

  • Property svn:executable set to *
Line 
1#!/usr/bin/env php
2<?php
3/**
4 * phpmybench
5 *
6 * Copyright (C) 2006-2008 axsh Co., LTD.
7 * License: GPL v2 or (at your option) any later version
8 * Author: Masahito Yoshida <masahito@axsh.net>
9 * Blog:  http://blog.hansode.org/
10 */
11
12require_once 'MyBench.php';
13require_once 'adodb/adodb.inc.php';
14
15$opt_map = getopt('n:r:d:u:p:P:h:H:');
16
17$num_kids = array_key_exists('n', $opt_map) ? $opt_map{n} : 10;
18$num_runs = array_key_exists('r', $opt_map) ? $opt_map{r} : 100;
19$db       = array_key_exists('d', $opt_map) ? $opt_map{d} : 'mysql';
20$user     = array_key_exists('u', $opt_map) ? $opt_map{u} : 'root';
21$pass     = array_key_exists('p', $opt_map) ? $opt_map{p} : '';
22$port     = array_key_exists('P', $opt_map) ? $opt_map{P} : 3306;
23$host_map = Array(
24    master => array_key_exists('h'$opt_map) ? $opt_map{h}  : 'localhost',
25    );
26if (array_key_exists('H', $opt_map)) {
27    if (is_array($opt_map{H})) {
28        foreach ($opt_map{H} as $slave) {
29            $host_map[slave][] = $slave;
30        }
31    }
32    else {
33        $host_map[slave][] = $opt_map{H};
34    }
35}
36$dsn_map = Array(
37    master => sprintf("mysql://%s:%s@%s:%s/%s",
38                      $user, $pass, $host_map[master],  $port, $db),
39    );
40if (is_array($host_map[slave])) {
41    foreach ($host_map{slave} as $host_slave) {
42        $dsn_map[slave][]
43            = sprintf("mysql://%s:%s@%s:%s/%s",
44                      $user, $pass, $host_slave$port, $db);
45    }
46}
47
48//------------------------------
49function callback($id) {
50    global $num_runs;
51    global $dsn_map;
52
53    $db_map = Array();
54    $db_map[master] =& ADONewConnection($dsn_map[master]);
55    if (is_array($dsn_map[slave])) {
56        foreach ($dsn_map[slave] as $dsn_slave) {
57            $db_slave =& ADONewConnection($dsn_slave);
58            $db_map[slave][] = $db_slave;
59        }
60    }
61
62    $cnt   = 0;
63    $times = Array();
64
65    // wait for the parent to HUP me
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    // cleanup
78    $db_map[master]->Disconnect();
79    if (is_array($db_map[slave])) {
80        foreach ($db_map[slave] as $db_slave) {
81            $db_slave->Disconnect();
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//------------------------------
93function 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    // SQLs
104    $res = $db_slave->Execute('select * from user');
105
106    $sth = $db_slave->Prepare('select * from user where user = ?');
107    $res = $db_slave->Execute($sth, Array('root'));
108}
109
110//------------------------------
111$results = MyBench::fork_and_work($num_kids, 'callback');
112MyBench::complete_results('test', $results);
113
114exit(0);
Note: See TracBrowser for help on using the browser.