Merge pull request #6659 from stanley-cheung/php-fix-style

PHP: ran php-cs-fixer again
pull/6764/merge
Jan Tattermusch 9 years ago committed by GitHub
commit a9e3ea3815
  1. 26
      examples/php/greeter_client.php
  2. 160
      examples/php/helloworld.php
  3. 768
      examples/php/route_guide/route_guide.php
  4. 266
      examples/php/route_guide/route_guide_client.php
  5. 3
      src/php/tests/interop/interop_client.php
  6. 6
      src/php/tests/interop/metrics_client.php
  7. 103
      src/php/tests/interop/stress_client.php

@ -32,19 +32,21 @@
*
*/
require dirname(__FILE__) . '/vendor/autoload.php';
require dirname(__FILE__) . '/helloworld.php';
require dirname(__FILE__).'/vendor/autoload.php';
require dirname(__FILE__).'/helloworld.php';
function greet($name) {
$client = new helloworld\GreeterClient('localhost:50051', [
'credentials' => Grpc\ChannelCredentials::createInsecure()
]);
$request = new helloworld\HelloRequest();
$request->setName($name);
list($reply, $status) = $client->SayHello($request)->wait();
$message = $reply->getMessage();
return $message;
function greet($name)
{
$client = new helloworld\GreeterClient('localhost:50051', [
'credentials' => Grpc\ChannelCredentials::createInsecure(),
]);
$request = new helloworld\HelloRequest();
$request->setName($name);
list($reply, $status) = $client->SayHello($request)->wait();
$message = $reply->getMessage();
return $message;
}
$name = !empty($argv[1]) ? $argv[1] : 'world';
print(greet($name)."\n");
echo greet($name)."\n";

@ -5,154 +5,164 @@
namespace helloworld {
class HelloRequest extends \DrSlump\Protobuf\Message {
/** @var string */
class HelloRequest extends \DrSlump\Protobuf\Message
{
/** @var string */
public $name = null;
/** @var \Closure[] */
protected static $__extensions = array();
public static function descriptor()
{
$descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'helloworld.HelloRequest');
public static function descriptor()
{
$descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'helloworld.HelloRequest');
// OPTIONAL STRING name = 1
$f = new \DrSlump\Protobuf\Field();
$f->number = 1;
$f->name = "name";
$f->type = \DrSlump\Protobuf::TYPE_STRING;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$descriptor->addField($f);
foreach (self::$__extensions as $cb) {
$descriptor->addField($cb(), true);
}
$f->number = 1;
$f->name = 'name';
$f->type = \DrSlump\Protobuf::TYPE_STRING;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$descriptor->addField($f);
return $descriptor;
}
foreach (self::$__extensions as $cb) {
$descriptor->addField($cb(), true);
}
return $descriptor;
}
/**
* Check if <name> has a value
* Check if <name> has a value.
*
* @return boolean
* @return bool
*/
public function hasName(){
return $this->_has(1);
public function hasName()
{
return $this->_has(1);
}
/**
* Clear <name> value
* Clear <name> value.
*
* @return \helloworld\HelloRequest
*/
public function clearName(){
return $this->_clear(1);
public function clearName()
{
return $this->_clear(1);
}
/**
* Get <name> value
* Get <name> value.
*
* @return string
*/
public function getName(){
return $this->_get(1);
public function getName()
{
return $this->_get(1);
}
/**
* Set <name> value
* Set <name> value.
*
* @param string $value
*
* @return \helloworld\HelloRequest
*/
public function setName( $value){
return $this->_set(1, $value);
public function setName($value)
{
return $this->_set(1, $value);
}
}
}
namespace helloworld {
class HelloReply extends \DrSlump\Protobuf\Message {
/** @var string */
class HelloReply extends \DrSlump\Protobuf\Message
{
/** @var string */
public $message = null;
/** @var \Closure[] */
protected static $__extensions = array();
public static function descriptor()
{
$descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'helloworld.HelloReply');
public static function descriptor()
{
$descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'helloworld.HelloReply');
// OPTIONAL STRING message = 1
$f = new \DrSlump\Protobuf\Field();
$f->number = 1;
$f->name = "message";
$f->type = \DrSlump\Protobuf::TYPE_STRING;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$descriptor->addField($f);
foreach (self::$__extensions as $cb) {
$descriptor->addField($cb(), true);
}
$f->number = 1;
$f->name = 'message';
$f->type = \DrSlump\Protobuf::TYPE_STRING;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$descriptor->addField($f);
return $descriptor;
}
foreach (self::$__extensions as $cb) {
$descriptor->addField($cb(), true);
}
return $descriptor;
}
/**
* Check if <message> has a value
* Check if <message> has a value.
*
* @return boolean
* @return bool
*/
public function hasMessage(){
return $this->_has(1);
public function hasMessage()
{
return $this->_has(1);
}
/**
* Clear <message> value
* Clear <message> value.
*
* @return \helloworld\HelloReply
*/
public function clearMessage(){
return $this->_clear(1);
public function clearMessage()
{
return $this->_clear(1);
}
/**
* Get <message> value
* Get <message> value.
*
* @return string
*/
public function getMessage(){
return $this->_get(1);
public function getMessage()
{
return $this->_get(1);
}
/**
* Set <message> value
* Set <message> value.
*
* @param string $value
*
* @return \helloworld\HelloReply
*/
public function setMessage( $value){
return $this->_set(1, $value);
public function setMessage($value)
{
return $this->_set(1, $value);
}
}
}
namespace helloworld {
class GreeterClient extends \Grpc\BaseStub {
public function __construct($hostname, $opts) {
parent::__construct($hostname, $opts);
}
class GreeterClient extends \Grpc\BaseStub
{
public function __construct($hostname, $opts)
{
parent::__construct($hostname, $opts);
}
/**
* @param helloworld\HelloRequest $input
*/
public function SayHello(\helloworld\HelloRequest $argument, $metadata = array(), $options = array()) {
return $this->_simpleRequest('/helloworld.Greeter/SayHello', $argument, '\helloworld\HelloReply::deserialize', $metadata, $options);
public function SayHello(\helloworld\HelloRequest $argument, $metadata = array(), $options = array())
{
return $this->_simpleRequest('/helloworld.Greeter/SayHello', $argument, '\helloworld\HelloReply::deserialize', $metadata, $options);
}
}
}

File diff suppressed because it is too large Load Diff

@ -32,48 +32,50 @@
*
*/
require dirname(__FILE__) . '/../vendor/autoload.php';
require dirname(__FILE__) . '/route_guide.php';
require dirname(__FILE__).'/../vendor/autoload.php';
require dirname(__FILE__).'/route_guide.php';
define('COORD_FACTOR', 1e7);
$client = new routeguide\RouteGuideClient('localhost:50051', [
'credentials' => Grpc\ChannelCredentials::createInsecure()
'credentials' => Grpc\ChannelCredentials::createInsecure(),
]);
function printFeature($feature) {
$name = $feature->getName();
if (!$name) {
$name_str = "no feature";
} else {
$name_str = "feature called $name";
}
print sprintf("Found %s \n at %f, %f\n", $name_str,
$feature->getLocation()->getLatitude() / COORD_FACTOR,
$feature->getLocation()->getLongitude() / COORD_FACTOR);
function printFeature($feature)
{
$name = $feature->getName();
if (!$name) {
$name_str = 'no feature';
} else {
$name_str = "feature called $name";
}
echo sprintf("Found %s \n at %f, %f\n", $name_str,
$feature->getLocation()->getLatitude() / COORD_FACTOR,
$feature->getLocation()->getLongitude() / COORD_FACTOR);
}
/**
* Run the getFeature demo. Calls getFeature with a point known to have a
* feature and a point known not to have a feature.
*/
function runGetFeature() {
print "Running GetFeature...\n";
global $client;
$point = new routeguide\Point();
$points = array(
array(409146138, -746188906),
array(0, 0),
);
foreach ($points as $p) {
$point->setLatitude($p[0]);
$point->setLongitude($p[1]);
// make a unary grpc call
list($feature, $status) = $client->GetFeature($point)->wait();
printFeature($feature);
}
function runGetFeature()
{
echo "Running GetFeature...\n";
global $client;
$point = new routeguide\Point();
$points = array(
array(409146138, -746188906),
array(0, 0),
);
foreach ($points as $p) {
$point->setLatitude($p[0]);
$point->setLongitude($p[1]);
// make a unary grpc call
list($feature, $status) = $client->GetFeature($point)->wait();
printFeature($feature);
}
}
/**
@ -81,29 +83,30 @@ function runGetFeature() {
* containing all of the features in the pre-generated
* database. Prints each response as it comes in.
*/
function runListFeatures() {
print "Running ListFeatures...\n";
global $client;
$lo_point = new routeguide\Point();
$hi_point = new routeguide\Point();
$lo_point->setLatitude(400000000);
$lo_point->setLongitude(-750000000);
$hi_point->setLatitude(420000000);
$hi_point->setLongitude(-730000000);
$rectangle = new routeguide\Rectangle();
$rectangle->setLo($lo_point);
$rectangle->setHi($hi_point);
// start the server streaming call
$call = $client->ListFeatures($rectangle);
// an iterator over the server streaming responses
$features = $call->responses();
foreach ($features as $feature) {
printFeature($feature);
}
function runListFeatures()
{
echo "Running ListFeatures...\n";
global $client;
$lo_point = new routeguide\Point();
$hi_point = new routeguide\Point();
$lo_point->setLatitude(400000000);
$lo_point->setLongitude(-750000000);
$hi_point->setLatitude(420000000);
$hi_point->setLongitude(-730000000);
$rectangle = new routeguide\Rectangle();
$rectangle->setLo($lo_point);
$rectangle->setHi($hi_point);
// start the server streaming call
$call = $client->ListFeatures($rectangle);
// an iterator over the server streaming responses
$features = $call->responses();
foreach ($features as $feature) {
printFeature($feature);
}
}
/**
@ -111,96 +114,99 @@ function runListFeatures() {
* pre-generated feature database with a variable delay in between. Prints
* the statistics when they are sent from the server.
*/
function runRecordRoute() {
print "Running RecordRoute...\n";
global $client, $argv;
// start the client streaming call
$call = $client->RecordRoute();
$db = json_decode(file_get_contents($argv[1]), true);
$num_points_in_db = count($db);
$num_points = 10;
for ($i = 0; $i < $num_points; $i++) {
$point = new routeguide\Point();
$index = rand(0, $num_points_in_db - 1);
$lat = $db[$index]['location']['latitude'];
$long = $db[$index]['location']['longitude'];
$feature_name = $db[$index]['name'];
$point->setLatitude($lat);
$point->setLongitude($long);
print sprintf("Visiting point %f, %f,\n with feature name: %s\n",
$lat / COORD_FACTOR, $long / COORD_FACTOR,
$feature_name ? $feature_name : '<empty>');
usleep(rand(300000, 800000));
$call->write($point);
}
list($route_summary, $status) = $call->wait();
print sprintf("Finished trip with %d points\nPassed %d features\n".
"Travelled %d meters\nIt took %d seconds\n",
$route_summary->getPointCount(),
$route_summary->getFeatureCount(),
$route_summary->getDistance(),
$route_summary->getElapsedTime());
function runRecordRoute()
{
echo "Running RecordRoute...\n";
global $client, $argv;
// start the client streaming call
$call = $client->RecordRoute();
$db = json_decode(file_get_contents($argv[1]), true);
$num_points_in_db = count($db);
$num_points = 10;
for ($i = 0; $i < $num_points; ++$i) {
$point = new routeguide\Point();
$index = rand(0, $num_points_in_db - 1);
$lat = $db[$index]['location']['latitude'];
$long = $db[$index]['location']['longitude'];
$feature_name = $db[$index]['name'];
$point->setLatitude($lat);
$point->setLongitude($long);
echo sprintf("Visiting point %f, %f,\n with feature name: %s\n",
$lat / COORD_FACTOR, $long / COORD_FACTOR,
$feature_name ? $feature_name : '<empty>');
usleep(rand(300000, 800000));
$call->write($point);
}
list($route_summary, $status) = $call->wait();
echo sprintf("Finished trip with %d points\nPassed %d features\n".
"Travelled %d meters\nIt took %d seconds\n",
$route_summary->getPointCount(),
$route_summary->getFeatureCount(),
$route_summary->getDistance(),
$route_summary->getElapsedTime());
}
/**
* Run the routeChat demo. Send some chat messages, and print any chat
* messages that are sent from the server.
*/
function runRouteChat() {
print "Running RouteChat...\n";
global $client;
// start the bidirectional streaming call
$call = $client->RouteChat();
$notes = array(
array(1, 1, 'first message'),
array(1, 2, 'second message'),
array(2, 1, 'third message'),
array(1, 1, 'fourth message'),
array(1, 1, 'fifth message'),
);
foreach ($notes as $n) {
$point = new routeguide\Point();
$point->setLatitude($lat = $n[0]);
$point->setLongitude($long = $n[1]);
$route_note = new routeguide\RouteNote();
$route_note->setLocation($point);
$route_note->setMessage($message = $n[2]);
print sprintf("Sending message: '%s' at (%d, %d)\n",
$message, $lat, $long);
// send a bunch of messages to the server
$call->write($route_note);
}
$call->writesDone();
// read from the server until there's no more
while ($route_note_reply = $call->read()) {
print sprintf("Previous left message at (%d, %d): '%s'\n",
$route_note_reply->getLocation()->getLatitude(),
$route_note_reply->getLocation()->getLongitude(),
$route_note_reply->getMessage());
}
function runRouteChat()
{
echo "Running RouteChat...\n";
global $client;
// start the bidirectional streaming call
$call = $client->RouteChat();
$notes = array(
array(1, 1, 'first message'),
array(1, 2, 'second message'),
array(2, 1, 'third message'),
array(1, 1, 'fourth message'),
array(1, 1, 'fifth message'),
);
foreach ($notes as $n) {
$point = new routeguide\Point();
$point->setLatitude($lat = $n[0]);
$point->setLongitude($long = $n[1]);
$route_note = new routeguide\RouteNote();
$route_note->setLocation($point);
$route_note->setMessage($message = $n[2]);
echo sprintf("Sending message: '%s' at (%d, %d)\n",
$message, $lat, $long);
// send a bunch of messages to the server
$call->write($route_note);
}
$call->writesDone();
// read from the server until there's no more
while ($route_note_reply = $call->read()) {
echo sprintf("Previous left message at (%d, %d): '%s'\n",
$route_note_reply->getLocation()->getLatitude(),
$route_note_reply->getLocation()->getLongitude(),
$route_note_reply->getMessage());
}
}
/**
* Run all of the demos in order
* Run all of the demos in order.
*/
function main() {
runGetFeature();
runListFeatures();
runRecordRoute();
runRouteChat();
function main()
{
runGetFeature();
runListFeatures();
runRecordRoute();
runRouteChat();
}
if (empty($argv[1])) {
print "Usage: php -d extension=grpc.so route_guide_client.php " .
echo 'Usage: php -d extension=grpc.so route_guide_client.php '.
"<path to route_guide_db.json>\n";
exit(1);
exit(1);
}
main();

@ -477,7 +477,8 @@ function _makeStub($args)
return $stub;
}
function interop_main($args, $stub = false) {
function interop_main($args, $stub = false)
{
if (!$stub) {
$stub = _makeStub($args);
}

@ -39,11 +39,11 @@ $server_port = (count($parts) == 2) ? $parts[1] : '';
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
if (@!socket_connect($socket, $server_host, $server_port)) {
echo "Cannot connect to merics server...\n";
exit(1);
echo "Cannot connect to merics server...\n";
exit(1);
}
socket_write($socket, 'qps');
while ($out = socket_read($socket, 1024)) {
echo "$out\n";
echo "$out\n";
}
socket_close($socket);

@ -32,50 +32,52 @@
*
*/
include_once('interop_client.php');
include_once 'interop_client.php';
function stress_main($args) {
mt_srand();
set_time_limit(0);
function stress_main($args)
{
mt_srand();
set_time_limit(0);
// open socket to listen as metrics server
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
if (@!socket_bind($socket, 'localhost', $args['metrics_port'])) {
echo "Cannot create socket for metrics server...\n";
exit(1);
}
socket_listen($socket);
socket_set_nonblock($socket);
// open socket to listen as metrics server
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
if (@!socket_bind($socket, 'localhost', $args['metrics_port'])) {
echo "Cannot create socket for metrics server...\n";
exit(1);
}
socket_listen($socket);
socket_set_nonblock($socket);
$start_time = microtime(true);
$count = 0;
$deadline = $args['test_duration_secs'] ?
($start_time + $args['test_duration_secs']) : false;
$num_test_cases = count($args['test_cases']);
$stub = false;
$start_time = microtime(true);
$count = 0;
$deadline = $args['test_duration_secs'] ?
($start_time + $args['test_duration_secs']) : false;
$num_test_cases = count($args['test_cases']);
$stub = false;
while (true) {
$current_time = microtime(true);
if ($deadline && $current_time > $deadline) {
break;
}
if ($client_connection = socket_accept($socket)) {
// there is an incoming request, respond with qps metrics
$input = socket_read($client_connection, 1024);
$qps = round($count / ($current_time - $start_time));
socket_write($client_connection, "qps: $qps");
socket_close($client_connection);
} else {
// do actual work, run one interop test case
$args['test_case'] =
$args['test_cases'][mt_rand(0, $num_test_cases - 1)];
$stub = @interop_main($args, $stub);
$count++;
while (true) {
$current_time = microtime(true);
if ($deadline && $current_time > $deadline) {
break;
}
if ($client_connection = socket_accept($socket)) {
// there is an incoming request, respond with qps metrics
$input = socket_read($client_connection, 1024);
$qps = round($count / ($current_time - $start_time));
socket_write($client_connection, "qps: $qps");
socket_close($client_connection);
} else {
// do actual work, run one interop test case
$args['test_case'] =
$args['test_cases'][mt_rand(0, $num_test_cases - 1)];
$stub = @interop_main($args, $stub);
++$count;
}
}
}
socket_close($socket);
echo "Number of interop tests run in $args[test_duration_secs] seconds: $count.\n";
socket_close($socket);
echo "Number of interop tests run in $args[test_duration_secs] ".
"seconds: $count.\n";
}
// process command line arguments
@ -85,31 +87,32 @@ $raw_args = getopt('',
'metrics_port::',
'test_duration_secs::',
'num_channels_per_server::',
'num_stubs_per_channel::']);
'num_stubs_per_channel::',
]);
$args = [];
if (empty($raw_args['server_addresses'])) {
$args['server_host'] = 'localhost';
$args['server_port'] = '8080';
$args['server_host'] = 'localhost';
$args['server_port'] = '8080';
} else {
$parts = explode(':', $raw_args['server_addresses']);
$args['server_host'] = $parts[0];
$args['server_port'] = (count($parts) == 2) ? $parts[1] : '';
$parts = explode(':', $raw_args['server_addresses']);
$args['server_host'] = $parts[0];
$args['server_port'] = (count($parts) == 2) ? $parts[1] : '';
}
$args['metrics_port'] = empty($raw_args['metrics_port']) ?
'8081' : $args['metrics_port'];
'8081' : $args['metrics_port'];
$args['test_duration_secs'] = empty($raw_args['test_duration_secs']) ||
$raw_args['test_duration_secs'] == -1 ?
false : $raw_args['test_duration_secs'];
$raw_args['test_duration_secs'] == -1 ?
false : $raw_args['test_duration_secs'];
$test_cases = [];
$test_case_strs = explode(',', $raw_args['test_cases']);
foreach ($test_case_strs as $test_case_str) {
$parts = explode(':', $test_case_str);
$test_cases = array_merge($test_cases, array_fill(0, $parts[1], $parts[0]));
$parts = explode(':', $test_case_str);
$test_cases = array_merge($test_cases, array_fill(0, $parts[1], $parts[0]));
}
$args['test_cases'] = $test_cases;

Loading…
Cancel
Save