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

代码语言:phpcli

所属分类:通讯

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

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

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

<?php
//请切换至php7 运行
Swoole\Runtime::enableCoroutine();
// php_stream tcp server & client with 12.8k requests in single process
function tcp_pack(string $data): string
{
    return pack('n', strlen($data)) . $data;
}

function tcp_length(string $head): int
{
    return unpack('n', $head)[1];
}

Co\run(function() {
    go(function () {
        $ctx = stream_context_create(['socket' => ['so_reuseaddr' => true, 'backlog' => 128]]);
        $socket = stream_socket_server(
            'tcp://0.0.0.0:9507',
            $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $ctx
        );
        if (!$socket) {
            echo "$errstr ($errno)\n";
        } else {
            $i = 0;
            while ($conn = stream_socket_accept($socket, 1)) {
                stream_set_timeout($conn, 5);
                for ($n = 100; $n--;) {
                    $data = fread($conn, tcp_length(fread($con.........完整代码请登录后点击上方下载按钮下载查看

网友评论0