~johannes-s/php-mysqlnd/cvssync

« back to all changes in this revision

Viewing changes to tests/ext/pdo_mysql/bench/micro_benches/pdo_insert_ps_vs_native.php

  • Committer: Johannes Schlueter
  • Date: 2008-10-28 14:21:29 UTC
  • Revision ID: johannes@mysql.com-20081028142129-lf9hvgkvnu51zuci
these files aren't in CVS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?PHP
2
 
$runs   = array(100);
3
 
$rows   = array(10, 50, 200, 500);
4
 
 
5
 
$times = array('overall emulated' => 0, 'overall native' => 0);
6
 
$description = 'Connect, INSERT exec() vs. prepare()/bind()/execute()';
7
 
$errors = array();
8
 
 
9
 
function pdo_insert_select_check($db, $num_rows, $label) {
10
 
 
11
 
        $stmt = $db->query('SELECT id, label FROM test ORDER BY id ASC');
12
 
        for ($i = 0; $i < $num_rows; $i++) {
13
 
                $row = $stmt->fetch(PDO::FETCH_ASSOC);
14
 
                if (($row['id'] != $i) || ($row['label'] != $label))
15
 
                        return false;
16
 
        }
17
 
 
18
 
        return true;
19
 
}
20
 
 
21
 
function pdo_insert_select($db, $num_runs, $num_rows, $emulated) {
22
 
        global $times, $errors, $engine;
23
 
 
24
 
        $label = str_repeat('a', 255);
25
 
 
26
 
        $start = microtime(true);
27
 
        for ($i = 0; $i < $num_runs; $i++) {
28
 
 
29
 
                $db->exec('DROP TABLE IF EXISTS test');
30
 
                $db->exec('CREATE TABLE test(id INT, label VARCHAR(255)) ENGINE=' . $engine);
31
 
 
32
 
                for ($j = 0; $j < $num_rows; $j++) {
33
 
                        $db->exec('INSERT INTO test(id, label) VALUES (' . $j . ', "' . $label . '")');
34
 
                }
35
 
        }
36
 
 
37
 
        $time_label = sprintf("%10s - rows = %d, runs = %d exec() single", ($emulated) ? 'emulated' : 'native',
38
 
                $num_rows, $num_runs);
39
 
        $times[$time_label] = microtime(true) - $start;
40
 
 
41
 
        if (!pdo_insert_select_check($db, $num_rows, $label)) {
42
 
                $errors[] = 'Single INSERT has failed';
43
 
                return false;
44
 
        }
45
 
 
46
 
        $start = microtime(true);
47
 
 
48
 
        $start = microtime(true);
49
 
        for ($i = 0; $i < $num_runs; $i++) {
50
 
 
51
 
                $db->exec('DROP TABLE IF EXISTS test');
52
 
                $db->exec('CREATE TABLE test(id INT, label VARCHAR(255)) ENGINE=' . $engine);
53
 
 
54
 
                $sql = 'INSERT INTO test(id, label) VALUES';
55
 
                for ($j = 0; $j < $num_rows; $j++) {
56
 
                        $sql .= '(' . $j . ', "' . $label . '"), ';
57
 
                }
58
 
                $db->exec(substr($sql, 0, -2));
59
 
        }
60
 
 
61
 
        $time_label = sprintf("%10s - rows = %d, runs = %d exec() multiple", ($emulated) ? 'emulated' : 'native',
62
 
                $num_rows, $num_runs);
63
 
        $times[$time_label] = microtime(true) - $start;
64
 
 
65
 
        if (!pdo_insert_select_check($db, $num_rows, $label)) {
66
 
                $errors[] = 'Multiple INSERT has failed';
67
 
                return false;
68
 
        }
69
 
 
70
 
        $start = microtime(true);
71
 
 
72
 
 
73
 
        for ($i = 0; $i < $num_runs; $i++) {
74
 
 
75
 
                $db->exec('DROP TABLE IF EXISTS test');
76
 
                $db->exec('CREATE TABLE test(id INT, label VARCHAR(255)) ENGINE=' . $engine);
77
 
 
78
 
                $stmt = $db->prepare('INSERT INTO test(id, label) VALUES (?, ?)');
79
 
                $stmt->bindParam(1, $j);
80
 
                $stmt->bindParam(2, $label);
81
 
                for ($j = 0; $j < $num_rows; $j++) {
82
 
                        $stmt->execute();
83
 
                }
84
 
        }
85
 
 
86
 
        $time_label = sprintf("%10s - rows = %d, runs = %d bind()", ($emulated) ? 'emulated' : 'native',
87
 
                $num_rows, $num_runs);
88
 
 
89
 
        $times[$time_label] = microtime(true) - $start;
90
 
 
91
 
        if (!pdo_insert_select_check($db, $num_rows, $label)) {
92
 
                $errors[] = 'bind() INSERT has failed';
93
 
                return false;
94
 
        }
95
 
 
96
 
        return true;
97
 
}
98
 
 
99
 
do {
100
 
 
101
 
        if (is_string($attr) && strlen($attr))
102
 
                $attr = unserialize($attr);
103
 
        else
104
 
                $attr = null;
105
 
 
106
 
        if ($user === false)
107
 
                $user = NULL;
108
 
 
109
 
        if ($passwd === false)
110
 
                $passwd = NULL;
111
 
 
112
 
        $db = new PDO($dsn, $user, $passwd, $attr);
113
 
        if (!$db) {
114
 
                $errors[] = sprintf('Failed to connect DSN="%s", user="%s", password="%s", attributes="%s"',
115
 
                        $dsn, $user, $passwd, $attr);
116
 
                break;
117
 
        }
118
 
 
119
 
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
120
 
        $db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
121
 
        $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);
122
 
 
123
 
        $times['overall'] = microtime(true);
124
 
 
125
 
        foreach ($runs as $k => $num_runs) {
126
 
                foreach ($rows as $k => $num_rows) {
127
 
 
128
 
                        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
129
 
                        $start = microtime(true);
130
 
                        if (!pdo_insert_select($db, $num_runs, $num_rows, true))
131
 
                                break 3;
132
 
                        $times['overall emulated'] += microtime(true) - $start;
133
 
 
134
 
                        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
135
 
                        $start = microtime(true);
136
 
                        if (!pdo_insert_select($db, $num_runs, $num_rows, false))
137
 
                                break 3;
138
 
                        $times['overall native'] += microtime(true) - $start;
139
 
 
140
 
                }
141
 
        }
142
 
 
143
 
        $times['overall'] = microtime(true) - $times['overall'];
144
 
 
145
 
        $db->exec('DROP TABLE IF EXISTS test');
146
 
 
147
 
} while (false);
148
 
?>
 
 
b'\\ No newline at end of file'