PHP code example of stcer / syar

1. Go to this page and download the library: Download stcer/syar library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stcer / syar example snippets


use syar\Server;
use syar\log\File as FileLog;
use syar\log\Log;

$vendorPath = __Your vendor path__;
/** @var \Composer\Autoload\ClassLoader $loader */
$loader = example\service\Test();
$server->setDispatcher(function(\syar\Token $token, $isDocument) use ($service){
    if(!$isDocument){
        $method = $token->getMethod();
        $params = $token->getArgs();
        $value = call_user_func_array(array($service, $method), $params);
    } else {
        $value = "Yar api document";
    }
    return $value;
});

$server->run(['max_request' => 10000]);


#php server.php


$url = "http://127.0.0.1:5604/test";
$client = new Yar_client($url);
$name = $client->getName("tester");
$age = $client->getAge();

//
echo "<pre>\n";
var_dump($name);
var_dump($age);


function test($type, $times = 5, $limit = 5){
    $timer = new Timer();
    $benchmark = new Benchmark($type);
    $rs[] = $benchmark->simpleTest(); // 2
    $rs[] = $benchmark->dbTest($limit); // 2
    $rs[] = $benchmark->batchTest($times, $limit); // 20
    $rs[] = $benchmark->concurrentTest($times, $limit); // 20
    $stop = $timer->stop();

    // 44 calls, 22 db, 22 normal
    foreach($rs as $v){
        var_dump($v);
    }
    
    return $stop;
}

// start test
$times['syar'] = test('syar');
$times['fpm'] = test('fpm');
var_dump($times);

---------------------------
output: 

array(2) {
  ["syar"]=>
  float(0.01271)
  ["fpm"]=>
  float(0.08602)
}


#example/client_mul.php
$vendorPath = ...;
$loader = new Yar_client($url);

$calls = [
	'age' => [
		'api' => '/test',
		'method' => 'getAge',
		'params' => []
	    ],
	'name' => [
		'api' => '/test',
		'method' => 'getName',
		'params' => [rand(1, 245301)]
	]
];
$rs = $client->calls($calls);

var_dump($rs);