swoole协程socket高性能并发测试代码

代码语言:phpcli

所属分类:通讯

代码描述:swoole协程socket高性能并发测试代码

代码标签: 高性能 并发 测试

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<?php
//请切换至php7 运行
  //此行代码后,文件操作,sleep,Mysqli,PDO,streams等都变成异步IO,见文档"一键协程化"章节
    Swoole\Runtime::enableCoroutine();
    // UDP server & client with 12.8k requests in single process
    Co\run(function() {
        go(function () {
            $socket = new Swoole\Coroutine\Socket(AF_INET, SOCK_DGRAM, 0);
            $socket->bind('127.0.0.1', 9506);
            $client_map = [];
            for ($c = 128; $c--;) {
                for ($n = 0; $n < 100; $n++) {
                    $recv = $socket->recvfrom($peer);
                    $client_uid = "{$peer['address']}:{$peer['port']}";
                    $id = $client_map[$client_uid] = ($client_map[$client_uid] ?? -1) + 1;
                    echo $recv."\n";
                    assert($recv === "Client: Hello #{$id}!");
                    $socket->sendto($peer['address'], $peer['port'], "Server: H.........完整代码请登录后点击上方下载按钮下载查看

网友评论0