Пример #1
0
<?php

$opt = getopt("c:n:k:");
print_r($opt);
if (empty($opt['c']) || empty($opt['n'])) {
    echo "examples:  php client.php -c 100 -n 10000" . PHP_EOL;
    return;
}
$clients = $opt['c'];
$count = $opt['n'];
$size = empty($opt['k']) ? 0 : $opt['k'];
require __DIR__ . "/WebSocketClient.php";
$host = '127.0.0.1';
$prot = 9501;
$client = new WebSocketClient($host, $prot);
$data = $client->connect();
//echo $data;
$data = "data";
if (!empty($size)) {
    $data = str_repeat("A", $size * 1024);
}
for ($i = 0; $i < $count; $i++) {
    $client->send("hello swoole, number:" . $i . " data:" . $data);
    $recvData = "";
    //while(1) {
    $tmp = $client->recv();
    if (empty($tmp)) {
        break;
    }
    $recvData .= $tmp;
    //}
Пример #2
0
function websocket(Swoole_Benchmark $bc)
{
    static $client = null;
    static $i;
    $start = microtime(true);
    if (empty($client)) {
        $client = new WebSocketClient($bc->server_config['host'], $bc->server_config['port']);
        if (!$client->connect()) {
            echo "connect failed\n";
            return false;
        }
        $end = microtime(true);
        $conn_use = $end - $start;
        $bc->max_conn_time = $conn_use;
        $i = 0;
        $start = $end;
    }
    /*--------写入Sokcet-------*/
    if (!$client->send($bc->send_data)) {
        echo "send failed\n";
        return false;
    }
    $end = microtime(true);
    $write_use = $end - $start;
    if ($write_use > $bc->max_write_time) {
        $bc->max_write_time = $write_use;
    }
    $start = $end;
    /*--------读取Sokcet-------*/
    $ret = $client->recv();
    //var_dump($ret);
    $i++;
    if (empty($ret)) {
        echo $bc->pid, "#{$i}@", " is lost\n";
        return false;
    }
    $end = microtime(true);
    $read_use = $end - $start;
    if ($read_use > $bc->max_read_time) {
        $bc->max_read_time = $read_use;
    }
    return true;
}