PHP Classes

File: src/example/concurent-clients.php

Recommend this page to a friend!
  Classes of LAGGOUNE Walid   GeoPulse   src/example/concurent-clients.php   Download  
File: src/example/concurent-clients.php
Role: Example script
Content type: text/plain
Description: Example script
Class: GeoPulse
Server application to track devices' location
Author: By
Last change: Imporved performance, added connections pool
Date: 27 days ago
Size: 1,118 bytes
 

Contents

Class file image Download
<?php

use Swoole\Coroutine;
use
Swoole\Coroutine\Client;

use function
Swoole\Coroutine\run;

$numberOfClients = 5000;

$sendInterval = 100;

$data = ['appId' => '123', 'clientId' => '22f8e456-93f2-4173-8f2d-8a010abcceb1', 'data' => ['type' => 'Point', 'coordinates' => [1, 1]]];
$jsonData = msgpack_pack($data);

function
simulateClient($host, $port, $jsonData, $sendInterval)
{
   
$client = new Client(SWOOLE_SOCK_UDP);

    if (!
$client->connect($host, $port, 0.5)) {
        echo
"Connect failed. Error: {$client->errCode}\n";

        return;
    }
    while (
true) {
       
$client->send($jsonData);
       
Coroutine::sleep($sendInterval / 1000);
    }

   
$client->close();
}

run(function () use ($numberOfClients, $jsonData, $sendInterval) {
   
$host = '192.168.1.12';
   
$port = 9505;
    for (
$i = 0; $i < $numberOfClients; $i++) {
       
go(function () use ($host, $port, $jsonData, $sendInterval) {
           
simulateClient($host, $port, $jsonData, $sendInterval);
        });
    }

    echo
"Load test with {$numberOfClients} clients sending messages every {$sendInterval}ms started.\n";
});