Swoole 协程性能测试
// 开启协程化,文件操作,sleep,Mysqli,PDO,streams等都变成异步IO Co::set(['hook_flags'=> SWOOLE_HOOK_ALL]); $s = microtime(true); // 创建协程容器 Co\run(function() { // 开启100个协程执行usleep for ($c = 100; $c--;) { go(function () { for ($n = 100; $n--;) { usleep(1000); } }); } // 开启100个协程执行100次文件读写 for ($c = 100; $c--;) { go(function () use ($c) { $tmp_filename = "/tmp/test-{$c}.php"; for ($n = 100; $n--;) { $self = file_get_contents(__FILE__); file_put_contents($tmp_filename, $self); assert(file_get_contents($tmp_filename) === $self); } unlink($tmp_filename); }); } // 开启50个协程执行100次数据库查询 for ($c = 50; $c--;) { go(function