3
$rows = array(10, 50, 200, 500);
5
$times = array('overall emulated' => 0, 'overall native' => 0);
6
$description = 'Connect, INSERT exec() vs. prepare()/bind()/execute()';
9
function pdo_insert_select_check($db, $num_rows, $label) {
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))
21
function pdo_insert_select($db, $num_runs, $num_rows, $emulated) {
22
global $times, $errors, $engine;
24
$label = str_repeat('a', 255);
26
$start = microtime(true);
27
for ($i = 0; $i < $num_runs; $i++) {
29
$db->exec('DROP TABLE IF EXISTS test');
30
$db->exec('CREATE TABLE test(id INT, label VARCHAR(255)) ENGINE=' . $engine);
32
for ($j = 0; $j < $num_rows; $j++) {
33
$db->exec('INSERT INTO test(id, label) VALUES (' . $j . ', "' . $label . '")');
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;
41
if (!pdo_insert_select_check($db, $num_rows, $label)) {
42
$errors[] = 'Single INSERT has failed';
46
$start = microtime(true);
48
$start = microtime(true);
49
for ($i = 0; $i < $num_runs; $i++) {
51
$db->exec('DROP TABLE IF EXISTS test');
52
$db->exec('CREATE TABLE test(id INT, label VARCHAR(255)) ENGINE=' . $engine);
54
$sql = 'INSERT INTO test(id, label) VALUES';
55
for ($j = 0; $j < $num_rows; $j++) {
56
$sql .= '(' . $j . ', "' . $label . '"), ';
58
$db->exec(substr($sql, 0, -2));
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;
65
if (!pdo_insert_select_check($db, $num_rows, $label)) {
66
$errors[] = 'Multiple INSERT has failed';
70
$start = microtime(true);
73
for ($i = 0; $i < $num_runs; $i++) {
75
$db->exec('DROP TABLE IF EXISTS test');
76
$db->exec('CREATE TABLE test(id INT, label VARCHAR(255)) ENGINE=' . $engine);
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++) {
86
$time_label = sprintf("%10s - rows = %d, runs = %d bind()", ($emulated) ? 'emulated' : 'native',
87
$num_rows, $num_runs);
89
$times[$time_label] = microtime(true) - $start;
91
if (!pdo_insert_select_check($db, $num_rows, $label)) {
92
$errors[] = 'bind() INSERT has failed';
101
if (is_string($attr) && strlen($attr))
102
$attr = unserialize($attr);
109
if ($passwd === false)
112
$db = new PDO($dsn, $user, $passwd, $attr);
114
$errors[] = sprintf('Failed to connect DSN="%s", user="%s", password="%s", attributes="%s"',
115
$dsn, $user, $passwd, $attr);
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);
123
$times['overall'] = microtime(true);
125
foreach ($runs as $k => $num_runs) {
126
foreach ($rows as $k => $num_rows) {
128
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
129
$start = microtime(true);
130
if (!pdo_insert_select($db, $num_runs, $num_rows, true))
132
$times['overall emulated'] += microtime(true) - $start;
134
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
135
$start = microtime(true);
136
if (!pdo_insert_select($db, $num_runs, $num_rows, false))
138
$times['overall native'] += microtime(true) - $start;
143
$times['overall'] = microtime(true) - $times['overall'];
145
$db->exec('DROP TABLE IF EXISTS test');
b'\\ No newline at end of file'