开发者

PHP: Simulate multiple MySQL connections to same database

开发者 https://www.devze.com 2023-02-01 17:52 出处:网络
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

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.


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.


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));


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消