2017 © Pedro Peláez
 

package async-mysql-php

php mysql async client

image

jenner/async-mysql-php

php mysql async client

  • Thursday, December 3, 2015
  • by huyanping
  • Repository
  • 6 Watchers
  • 38 Stars
  • 132 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 10 Forks
  • 0 Open issues
  • 3 Versions
  • 7 % Grown

The README.md

async-mysql-php

async mysql client
中文README
Doc:async-mysql-php, (*1)

Import

composer require jenner/async-mysql-php
Or
require /path/to/async-mysql-php/autoload.php, (*2)

Details

  • based on mysqli::poll
  • throw RuntimeException when mysql connection or sql is error
  • the return value's order is same to the order that you call attach method
  • every attach method will return a Promise object, you can call Process::then method to defer the data processing.

Interface

  • attach(),submit async mysql task
  • isDone(),check all the task whether complete
  • execute(), get the result

History

  • add isDone method to check whether complete. it will check every thousand microsecond.
  • add react/promise package for asynchronous processing data.

Notice

The async object will send the request to when you call the attach method. mysql server. Actually the async object will just check all the task whether complete and return result, when you call execute method.So you can do anything you want between attach and execute, for example:request a url and call execute after the request is done., (*3)

example:, (*4)

try{
    $async_mysql = new \Jenner\Mysql\Async();
    $async_mysql->attach(
        ['host' => '127.0.0.1', 'user' => 'root', 'password' => '', 'database' => 'test', 'port'=>3306],
        'select * from stu'
    );
    $async_mysql->attach(
        ['host' => '127.0.0.1', 'user' => 'root', 'password' => '', 'database' => 'test', 'port'=>3306],
        'select * from stu limit 0, 3'
    );
    $result = $async_mysql->execute();
    print_r($result);
}catch (Exception $e){
    echo $e->getMessage();
}

use promise:, (*5)

try {
    $async_mysql = new \Jenner\Mysql\Async();
    $promise_1 = $async_mysql->attach(
        ['host' => '127.0.0.1', 'user' => 'root', 'password' => '', 'database' => 'test'],
        'select * from stu'
    );
    $promise_1->then(
        function ($data) {
            echo 'sucess:' . var_export($data, true) . PHP_EOL;
        },
        function ($info) {
            echo "error:" . var_export($info, true);
        }
    );
    $promise_2 = $async_mysql->attach(
        ['host' => '127.0.0.1', 'user' => 'root', 'password' => '', 'database' => 'test'],
        'select * from stu limit 0, 3'
    );
    $promise_2->then(
        function ($data) {
            echo 'sucess:' . var_export($data, true) . PHP_EOL;
        },
        function ($info) {
            echo "error:" . var_export($info, true);
        }
    );
    $async_mysql->execute();
} catch (Exception $e) {
    echo $e->getMessage();
}

performance tests

Summary:async-mysql is really faster than sync-mysql.
Based on the performance test result, async-mysql can improve performance. How much it can improve is depend on what your code want to do. In general case, the execution time is depend on the most complex sql. The memory used will be improved because the async-mysql will get all the result at the same time., (*6)

# 同步
[root@iZ942077c78Z async-mysql-php]# php tests/performance_sync.php 
------------------------------------------
mark:[total diff]
time:4.2648551464081s
memory_real:18944KB
memory_emalloc:18377.171875KB
memory_peak_real:28416KB
memory_peak_emalloc:27560.3828125KB
[root@iZ942077c78Z async-mysql-php]# php tests/performance_sync.php 
------------------------------------------
mark:[total diff]
time:4.2285549640656s
memory_real:18944KB
memory_emalloc:18377.171875KB
memory_peak_real:28416KB
memory_peak_emalloc:27560.3828125KB
[root@iZ942077c78Z async-mysql-php]# php tests/performance_async.php  
------------------------------------------
mark:[total diff]
time:1.455677986145s
memory_real:38144KB
memory_emalloc:32574.015625KB
memory_peak_real:66816KB
memory_peak_emalloc:65709.7734375KB
# 异步
[root@iZ942077c78Z async-mysql-php]# php tests/performance_async.php 
------------------------------------------
mark:[total diff]
time:1.8936941623688s
memory_real:38144KB
memory_emalloc:32574.015625KB
memory_peak_real:66816KB
memory_peak_emalloc:65709.7734375KB
[root@iZ942077c78Z async-mysql-php]# php tests/performance_async.php 
------------------------------------------
mark:[total diff]
time:1.5208158493042s
memory_real:38144KB
memory_emalloc:32574.015625KB
memory_peak_real:66816KB
memory_peak_emalloc:65709.7734375KB

The Versions

03/12 2015

dev-master

9999999-dev

php mysql async client

  Sources   Download

MIT

The Requires

 

The Development Requires

php async mysql client

03/12 2015

v0.2

0.2.0.0

php mysql async client

  Sources   Download

MIT

The Requires

 

The Development Requires

php async mysql client

23/07 2015

v0.1

0.1.0.0

php mysql async client

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • ext-mysqli *

 

The Development Requires

php async mysql client