Home » Php » PHP: Simulate multiple MySQL connections to same database

PHP: Simulate multiple MySQL connections to same database

Posted by: admin July 12, 2020 Leave a comment


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.

How to&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: Test 100 concurrent connections by running this ab command:

ab.exe -n 100 -c 100

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


Are you using PHP5.3 ?

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

Try this :

$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)) {
    foreach ($links as $link) {
        if ($result = $link->reap_async_query()) {
} while ($processed < count($all_links));


Since PHP is usually triggered via an HTTP request you need someone/something to issue many HTTP requests.

I’ve been using http://jmeter.apache.org/ for years and it’s very valuable for these scenarios.


Since you want to test the connections from PHP you should run tests with PHP.

Also you want to test the load from MySQL scope, so preferably you should run the PHP script from the same machine that MySQL runs (if applicable).

One last note about the “simultaneously” thing, you should probably use threads. Personally I have never used threads in PHP, but google came up with this php class.

I hope this helped.