
またはコードに直接移動します:
1 2 3 4 5 6 | <?php $http = new swoole_http_server( "0.0.0.0" , 9501); $http ->on('request', function ( $request , $response ){
$swoole_mysql1 = new Swoole\Coroutine\MySQL(); $swoole_mysql2 = new Swoole\Coroutine\MySQL(); $swoole_mysql1 ->connect([ 'host' => '127.0.0.1', 'port' => 3306, 'user' => 'root', 'password' => 'root', 'database' => 'swoole',
]); $swoole_mysql2 ->connect([ 'host' => '127.0.0.1', 'port' => 3306, 'user' => 'root', 'password' => 'root', 'database' => 'swoole',
]); $res1 = $swoole_mysql1 ->query('SELECT * FROM data1'); $res2 = $swoole_mysql2 ->query('SELECT * FROM data2'); $response ->header( "Content-Type" , "text/html; charset=utf-8" ); $response -> end ( "<h1>Hello Swoole. #" . count ( $res1 ). count ( $res2 ). "</h1>" );
}); $http ->start();
|
ログイン後にコピー
推奨 (無料): swoole
## を使用しますサーバーアクセスを参照します。 http://ip:9501
非同期 MySQL では、2 番目のクエリを実行する前に、最初のクエリが完了するまで待つ必要がありません。その効果は、異なるサーバー、異なるデータベース、および異なるテーブルにアクセスするときにより顕著になります。
同期 MySQL クエリ コードを比較します。
1 2 3 | <?php $http = new swoole_http_server( "0.0.0.0" , 9501); $http ->on('request', function ( $request , $response ){ $swoole_mysql1 = mysqli_connect('127.0.0.1', 'root', 'root', 'swoole', 3306); $swoole_mysql2 = mysqli_connect('127.0.0.1', 'root', 'root', 'swoole', 3306); $res1 = $swoole_mysql1 ->query('SELECT * FROM data1'); $res2 = $swoole_mysql2 ->query('SELECT * FROM data2'); $response ->header( "Content-Type" , "text/html; charset=utf-8" ); $response -> end ( "<h1>Hello Swoole. #" . $res1 ->num_rows. $res2 ->num_rows. "</h1>" );
}); $http ->start();
|
ログイン後にコピー
同期コードは、PHP ネイティブ メソッドを使用してデータをクエリします。
2 つのクエリ メソッドに対して ab を使用してパフォーマンス テストを実行します。
ab -c 100 -n 1000 http://127.0.0.1:9501/
非同期クエリ:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Server Software: swoole-http-server
Server Hostname: 127.0.0.1Server Port: 9501Document Path: /
Document Length: 30 bytesConcurrency Level: 100Time taken for tests: 1.477 secondsComplete requests: 1000Failed requests: 0Write errors: 0Total transferred: 193000 bytesHTML transferred: 30000 bytesRequests per second: 676.82 [#/sec] (mean)Time per request: 147.749 [ms] (mean)
Time per request: 1.477 [ms] (mean, across all concurrent requests)
Transfer rate: 127.57 [Kbytes/sec] received
Connection Times (ms) min mean[+/-sd] median maxConnect: 0 1 1.8 0 7Processing: 4 140 24.0 145 156Waiting: 0 140 24.1 145 156Total: 7 140 22.6 145 160Percentage of the requests served within a certain time (ms) 50% 145
66% 146
75% 148
80% 148
90% 150
95% 152
98% 153
99% 154
100% 160 (longest request)
|
ログイン後にコピー
同期クエリ:
rree
以上がSwoole HTTPサーバーの非同期MySQLについて説明するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。