More Related Content
PDF
PDF
[東京] JapanSharePointGroup 勉強会 #2 PDF
PDF
PDF
「スピード」と「品質」を実現するPHP開発チームの取り組み~AngularJS+FuelPHP+AspectMock~ PDF
PDF
Yapc -asia 2012 lt @studio3104 PDF
What's hot
PPTX
PPTX
CakePHP+Smartyハイブリッドによるラクラク開発 PDF
KEY
PDF
PDF
KEY
Mojoliciousをウェブ制作現場で使ってみてる PDF
GMO TECHNOLOGY BOOT CAMP2015(PHP編) PDF
PDF
3時間濃縮CakePHP2.1 in PHPカンファレンス北海道2012 PDF
PDF
PDF
PDF
PDF
Chiba.pm #1 lt @studio3104 PDF
PDF
G*workshop 2011/11/22 Geb+Betamax PDF
JavaScript/CSS 2015 Autumn Similar to swooleを試してみた
PPTX
PPT
第7回社内勉強会「Code Sucks - 人の振り見て我が振り直せ」 PDF
Webアプリケーションの パフォーマンス向上のコツ 実践編 KEY
PDF
Perl暦およそ10年(?)の僕がデータベースを使えるようになるまでの昔話 PDF
PDF
「Html sql」で図書館hpにアクセスしてみよう PPTX
PDF
20091030cakephphandson 02 More from Yukihiro Katsumi
PDF
PDF
PPTX
PDF
エンタープライズアプリケーションアーキテクチャパターン PDF
PPTX
PDF
PPTX
Virtualbox+vagrant+docker PPTX
PPTX
PDF
swooleを試してみた
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
こんな感じの
テーブルレコードに
CREATE TABLE `users`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`mail` varchar(255) NOT NULL,
`pass` varchar(255) NOT NULL,
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
データ準備(通常)
• 大体15秒程度。
mysql> SELECT* FROM `users` ORDER BY `update_time` DESC LIMIT 1;
+-------+------------------+---------------+---------------------+
| id | mail | pass | update_time |
+-------+------------------+---------------+---------------------+
| 10000 | 99 mail@mail.com | 99 abcdefghij | 2019-05-19 11:17:57 |
+-------+------------------+---------------+---------------------+
1 row in set (0.01 sec)
mysql> SELECT * FROM `users` ORDER BY `update_time` ASC LIMIT 1;
+----+-----------------+--------------+---------------------+
| id | mail | pass | update_time |
+----+-----------------+--------------+---------------------+
| 1 | 0 mail@mail.com | 0 abcdefghij | 2019-05-19 11:17:42 |
+----+-----------------+--------------+---------------------+
1 row in set (0.00 sec)
- 17.
データ準備(swoole)private function insertUserSwoole($count)
{
Log::info('insertUser'.'_start');
for($i= 0;$i < $count;$i++){
go(function () use ($i, $count) {
$mysql = $this->getMysql();
for($j = 0;$j < $count;$j++){
$statement = $mysql->prepare("insert into users (mail, pass) VALUES (‘$i $j mail@mail.com', ‘$i $j
abcdefghij');");
$result = $statement->execute();
}
});
}
Log::info('insertUser'.'_end');
}
• さっきのと同じ条件で実行。
• go()で囲まれているとこが非同期に実行される(んだと思う)。
private function getMysql()
{
$mysql = new SwooleCoroutineMySQL();
$mysql->connect([
'host' => env('DB_HOST'),
'port' => 3306,
'user' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'database' => env('DB_DATABASE'),
'timeout' => 4, // 設定しといた方がよい
]);
return $mysql;
}
- 18.
• 大体4秒程度。
mysql> SELECT* FROM `users` ORDER BY `update_time` DESC LIMIT 1;
+------+---------------------+------------------+---------------------+
| id | mail | pass | update_time |
+------+---------------------+------------------+---------------------+
| 9329 | 44 94 mail@mail.com | 44 94 abcdefghij | 2019-05-19 11:29:37 |
+------+---------------------+------------------+---------------------+
1 row in set (0.01 sec)
mysql> SELECT * FROM `users` ORDER BY `update_time` ASC LIMIT 1;
+----+-------------------+----------------+---------------------+
| id | mail | pass | update_time |
+----+-------------------+----------------+---------------------+
| 1 | 3 0 mail@mail.com | 3 0 abcdefghij | 2019-05-19 11:29:33 |
+----+-------------------+----------------+---------------------+
1 row in set (0.01 sec)
データ準備(swoole)
- 19.
- 20.
- 21.
- 22.
- 23.
• 大体9分17秒程度。
mysql> SELECT* FROM `users` ORDER BY `update_time` DESC LIMIT 1;
+-------+--------------------+--------------------------------------------------------------+---------------------+
| id | mail | pass | update_time |
+-------+--------------------+--------------------------------------------------------------+---------------------+
| 10000 | 0 99 mail@mail.com | $2y$10$oFmEp9d6qtK5WrvUtQ4PRevs8BtFK5CcLEF04qD4Vl0TdQaAY2yMa | 2019-05-19 12:14:41 |
+-------+--------------------+--------------------------------------------------------------+---------------------+
1 row in set (0.01 sec)
mysql> SELECT * FROM `users` ORDER BY `update_time` ASC LIMIT 1;
+----+--------------------+--------------------------------------------------------------+---------------------+
| id | mail | pass | update_time |
+----+--------------------+--------------------------------------------------------------+---------------------+
| 1 | 68 0 mail@mail.com | $2y$10$JdBPfgEMn2kAJm2sotn9NObS7hHzuoZwfamx0VrrtQHuhBlxMu5rm | 2019-05-19 12:05:24 |
+----+--------------------+--------------------------------------------------------------+---------------------+
1 row in set (0.01 sec)
ハッシュ(通常)
- 24.
ハッシュ(swoole)
private function passHashSwoole($count)
{
Log::info('passHashSwoole'.'_start');
$coefficient= 2;
$parent = $count / $coefficient;
$child = $count * $coefficient;
for($i = 0;$i < $parent;$i++){
$sql = DB::table('users')->limit($child);
if ($i > 0) {
$sql->offset($i * $child);
}
$datas = $sql->get();
go(function () use ($datas) {
$mysql = $this->getMysql();
foreach($datas as $data){
$pass = Hash::make($data->pass);
$id = $data->id;
$data = $mysql->query("update users set pass = '$pass' where id = $id;");
}
});
Log::info('passHashSwoole_child'.'_end');
}
Log::info('passHashSwoole'.'_end');
}
• 色々あって何とか辿りついたソース。
- 25.
ハッシュ(swoole)
mysql> SELECT *FROM `users` ORDER BY `update_time` DESC LIMIT 1;
+------+---------------------+--------------------------------------------------------------+---------------------+
| id | mail | pass | update_time |
+------+---------------------+--------------------------------------------------------------+---------------------+
| 8794 | 64 97 mail@mail.com | $2y$10$dJEFSl9CikhSv.E4ICumMO3uOCqbVvK2E0uTzZWON8tPAejrIm5wq | 2019-05-19 14:54:31 |
+------+---------------------+--------------------------------------------------------------+---------------------+
1 row in set (0.01 sec)
mysql> SELECT * FROM `users` ORDER BY `update_time` ASC LIMIT 1;
+----+-------------------+--------------------------------------------------------------+---------------------+
| id | mail | pass | update_time |
+----+-------------------+--------------------------------------------------------------+---------------------+
| 1 | 5 0 mail@mail.com | $2y$10$PFUJN/mKBE3uVNtX/GpE6Oaz5I.VjEFkRxWvh2wiFStQySkdSQJcy | 2019-05-19 14:45:58 |
+----+-------------------+--------------------------------------------------------------+---------------------+
1 row in set (0.01 sec)
- 26.
- 27.
• 大体8分33秒程度。
mysql> SELECT* FROM `users` ORDER BY `update_time` DESC LIMIT 1;
+------+---------------------+--------------------------------------------------------------+---------------------+
| id | mail | pass | update_time |
+------+---------------------+--------------------------------------------------------------+---------------------+
| 8794 | 64 97 mail@mail.com | $2y$10$dJEFSl9CikhSv.E4ICumMO3uOCqbVvK2E0uTzZWON8tPAejrIm5wq | 2019-05-19 14:54:31 |
+------+---------------------+--------------------------------------------------------------+---------------------+
1 row in set (0.01 sec)
mysql> SELECT * FROM `users` ORDER BY `update_time` ASC LIMIT 1;
+----+-------------------+--------------------------------------------------------------+---------------------+
| id | mail | pass | update_time |
+----+-------------------+--------------------------------------------------------------+---------------------+
| 1 | 5 0 mail@mail.com | $2y$10$PFUJN/mKBE3uVNtX/GpE6Oaz5I.VjEFkRxWvh2wiFStQySkdSQJcy | 2019-05-19 14:45:58 |
+----+-------------------+--------------------------------------------------------------+---------------------+
1 row in set (0.01 sec)
ハッシュ(swoole)
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
• Cpuを使い切れてない様子。
• ローカルでも2つCpuが使えれば、せめて倍くらいの速度は出
せるはず。
top- 21:41:33 up 9:04, 0 users, load average: 0.79, 0.40, 0.22
Tasks: 15 total, 2 running, 13 sleeping, 0 stopped, 0 zombie
Cpu0 : 1.0%us, 2.3%sy, 0.0%ni, 96.3%id, 0.3%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu1 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
実行サーバ
- 36.
- 37.
- 38.
ハッシュ(複数プロセス)private function passHashSwooleProcess()
{
Log::info('passHashSwooleProcess'.'_start');
$workers= [];
$worker_num = 2;
$data_count = DB::table('users')->where(DB::raw('length(pass)'), '<', 50)->count();
for($i = 0; $i < $worker_num; $i++){
$sql = DB::table('users')->where(DB::raw('length(pass)'), '<', 50)->limit($data_count / 2);
if ($i > 0) {
$sql->offset($i * $data_count / 2);
}
$userss = $sql->get();
$process = new SwooleProcess(function($process) use ($userss){
go(function () use ($process, $userss) {
$mysql = $this->getMysql();
foreach($userss as $users){
$pass = Hash::make($users->pass);
$id = $users->id;
$data = $mysql->query("update users set pass = '$pass' where id = $id;");
}
Log::info('passHashSwooleProcess_child'.'_end');
$process->exit(0);
});
});
$pid = $process->start();
$workers[$pid] = $process;
}
Log::info('passHashSwooleProcess'.'_end');
}
- 39.
private function passHashSwooleProcess()
{
Log::info('passHashSwooleProcess'.'_start');
$workers= [];
$worker_num = 2;
$data_count = DB::table('users')->where(DB::raw('length(pass)'), '<', 50)->count();
for($i = 0; $i < $worker_num; $i++){
$sql = DB::table('users')->where(DB::raw('length(pass)'), '<', 50)->limit($data_count / 2);
if ($i > 0) {
$sql->offset($i * $data_count / 2);
}
$userss = $sql->get();
$process = new SwooleProcess(function($process) use ($userss){
go(function () use ($process, $userss) {
$mysql = $this->getMysql();
foreach($userss as $users){
$pass = Hash::make($users->pass);
$id = $users->id;
$data = $mysql->query("update users set pass = '$pass' where id = $id;");
}
Log::info('passHashSwooleProcess_child'.'_end');
$process->exit(0);
});
});
$pid = $process->start();
$workers[$pid] = $process;
}
Log::info('passHashSwooleProcess'.'_end');
}
ハッシュ(複数プロセス)
プロセスを作って
- 40.
ハッシュ(複数プロセス)private function passHashSwooleProcess()
{
Log::info('passHashSwooleProcess'.'_start');
$workers= [];
$worker_num = 2;
$data_count = DB::table('users')->where(DB::raw('length(pass)'), '<', 50)->count();
for($i = 0; $i < $worker_num; $i++){
$sql = DB::table('users')->where(DB::raw('length(pass)'), '<', 50)->limit($data_count / 2);
if ($i > 0) {
$sql->offset($i * $data_count / 2);
}
$userss = $sql->get();
$process = new SwooleProcess(function($process) use ($userss){
go(function () use ($process, $userss) {
$mysql = $this->getMysql();
foreach($userss as $users){
$pass = Hash::make($users->pass);
$id = $users->id;
$data = $mysql->query("update users set pass = '$pass' where id = $id;");
}
Log::info('passHashSwooleProcess_child'.'_end');
$process->exit(0);
});
});
$pid = $process->start();
$workers[$pid] = $process;
}
Log::info('passHashSwooleProcess'.'_end');
}
プロセス動かす。
- 41.
private function passHashSwooleProcess()
{
Log::info('passHashSwooleProcess'.'_start');
$workers= [];
$worker_num = 2;
$data_count = DB::table('users')->where(DB::raw('length(pass)'), '<', 50)->count();
for($i = 0; $i < $worker_num; $i++){
$sql = DB::table('users')->where(DB::raw('length(pass)'), '<', 50)->limit($data_count / 2);
if ($i > 0) {
$sql->offset($i * $data_count / 2);
}
$userss = $sql->get();
$process = new SwooleProcess(function($process) use ($userss){
go(function () use ($process, $userss) {
$mysql = $this->getMysql();
foreach($userss as $users){
$pass = Hash::make($users->pass);
$id = $users->id;
$data = $mysql->query("update users set pass = '$pass' where id = $id;");
}
Log::info('passHashSwooleProcess_child'.'_end');
$process->exit(0);
});
});
$pid = $process->start();
$workers[$pid] = $process;
}
Log::info('passHashSwooleProcess'.'_end');
}
ハッシュ(複数プロセス)
終わったら殺す。
- 42.
- 43.
いけてそう!!!!
top - 21:34:34up 1 day, 14:42, 0 users, load average: 0.54, 0.51, 0.58
Tasks: 5 total, 3 running, 2 sleeping, 0 stopped, 0 zombie
Cpu0 : 94.0%us, 3.0%sy, 0.0%ni, 2.3%id, 0.0%wa, 0.0%hi, 0.7%si, 0.0%st
Cpu1 : 93.0%us, 4.3%sy, 0.0%ni, 0.0%id, 2.0%wa, 0.0%hi, 0.7%si, 0.0%st
実行サーバでTOP
- 44.
- 45.
• 大体4分39秒程度。
mysql> SELECT* FROM `users` ORDER BY `update_time` DESC LIMIT 1;
+-------+---------------------+--------------------------------------------------------------+---------------------+
| id | mail | pass | update_time |
+-------+---------------------+--------------------------------------------------------------+---------------------+
| 10000 | 91 99 mail@mail.com | $2y$10$60bx7lqjuNUys/tHUC3pOOkooM6AyN2/KabeP3flsmGWXfAn7HoRG | 2019-05-19 15:23:22 |
+-------+---------------------+--------------------------------------------------------------+---------------------+
1 row in set (0.01 sec)
mysql> SELECT * FROM `users` ORDER BY `update_time` ASC LIMIT 1;
+----+-------------------+--------------------------------------------------------------+---------------------+
| id | mail | pass | update_time |
+----+-------------------+--------------------------------------------------------------+---------------------+
| 1 | 0 0 mail@mail.com | $2y$10$h6ziNoQHFSTsMbe8qOKJ8O/FDfM80vmy21/9nt7Q6H76aV9lcC1VW | 2019-05-19 15:18:43 |
+----+-------------------+--------------------------------------------------------------+---------------------+
1 row in set (0.01 sec)
ハッシュ(swoole)
- 46.