Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Simulate multiple MySQL connections to same database

Tags:

php

mysql

testing

An insert query is constantly getting logged in my MySQL slow query log.
I want to see how much time the INSERT query is taking with 100 simultaneous insert operations(to the same table).

So I need to simulate the follwoing.
500 different simultaneous connections from PHP to the same database on a mysql server, all of which are inserting a row(simultaneously) to the same table.

DO I need to use any load testing tool? Or Can I simply write a PHP script to do this?

Any thoughts?

PS: I am using apache on windows.

like image 887
Varun Avatar asked Dec 31 '10 09:12

Varun


2 Answers

If you've installed apache on windows, you should have the apache benchmark tool (ab.exe). Create your php script to insert a row and make it accessible via: http://127.0.0.1/insert.php. Test 100 concurrent connections by running this ab command:

ab.exe -n 100 -c 100 http://127.0.0.1/insert.php

That should run those 100 inserts simultaneously via your php script.

like image 91
lzhang Avatar answered Nov 10 '22 12:11

lzhang


Are you using PHP5.3 ?

The new MySQL driver (mysqlnd) can handle parallel queries.

Try this :

<?php
$query = "SELECT 'test'";
$all_links = array();

for($i=0; $i<100; $i++) {
    $link = mysqli_connect();
    $link->query($query, MYSQLI_ASYNC);
    $all_links[] = $link;
}

$processed = 0;
do {
    $links = $errors = $reject = array();
    foreach ($all_links as $link) {
        $links[] = $errors[] = $reject[] = $link;
    }
    if (!mysqli_poll($links, $errors, $reject, 1)) {
        continue;
    }
    foreach ($links as $link) {
        if ($result = $link->reap_async_query()) {
            print_r($result->fetch_row());
            mysqli_free_result($result);
            $processed++;
        }
    }
} while ($processed < count($all_links));
like image 22
Xavier Barbosa Avatar answered Nov 10 '22 12:11

Xavier Barbosa