PHP Backend Application(5)Redis Advanced and PHP Memory and HTTP Client

PHP Backend Application(5)Redis Advanced and PHP Memory and HTTP Client

1 Trouble Shooting PHP Memory and TimeZone Issue
Error Message:
1) SegmentServiceTest::testGetCampaignConfig
date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.

Solution:
http://stackoverflow.com/questions/16765158/date-it-is-not-safe-to-rely-on-the-systems-timezone-settings

I met this issue when I install Symfony long time ago
http://sillycat.iteye.com/blog/2149513

> cat /etc/php.ini
; http://php.net/date.timezone
date.timezone = America/North_Dakota/Center
display_errors = On
memory_limit = 512M

Since I manually install the php myself, so my php.ini file actually is in this place.
> ls -l /etc/ | grep php
lrwxrwxrwx  1 root root      20 Jun 17 02:43 php.ini -> /opt/php/lib/php.ini

Memory Issues
> phpunit --bootstrap vendor/autoload.php tests/JobConsumerPHP/SegmentServiceTest
PHPUnit 5.4.6 by Sebastian Bergmann and contributors.

..
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 3248 bytes) in /home/ec2-user/users/carl/jobs-consumerphp/src/JobConsumerPHP/SegmentService.php on line 166

Solution:
give more memory limit in php.ini as follow:
memory_limit = 512M

2 HTTP Client
I plan to use this client library.
http://docs.guzzlephp.org/en/latest/request-options.html#json
https://github.com/guzzle/guzzle

Set up the configuration in composer.json
"guzzlehttp/guzzle": "^6.2"


Try to initiate the client in base class and reuse them.
<?php

namespace JobConsumerPHP;

require __DIR__.'/../../vendor/autoload.php';

use \GuzzleHttp\Client;
use \GuzzleHttp\Psr7\Request;

class WebHttpClient
{

    protected $classifierClient = null;

    protected $predictionClient = null;

    protected $ioc = null;

    public function __construct($ioc)
    {
        $this->ioc = $ioc;

        $logger = $this->ioc->getService("logger");
        $config = $this->ioc->getService("config");

        $logger->info("==============WebClient config start ==============");
        $classifierURL = $config['classifierURL'];
        $classifierKey = $config['classifierKey'];

        $predictionURL = $config['predictionURL'];
        $predictionKey = $config['predictionKey'];

        $gatewayKey = $config['gatewayKey'];
        $httpTimeout = $config['httpTimeout'];

        $logger->info("classifierURL = {$classifierURL}");
        $logger->info("classifierKey = {$classifierKey}");
        $logger->info("predictionURL = {$predictionURL}");
        $logger->info("predictionKey = {$predictionKey}");
        $logger->info("predictionKey = {$predictionKey}");
        $logger->info("httpTimeout = {$httpTimeout}");
        $logger->info("=============================================");

        try
        {
            $this->classifierClient = new Client([
                'base_uri' => $classifierURL,
                'timeout' => $httpTimeout,
                'connect_timeout'=> $httpTimeout,
            ]);
            $this->predictionClient = new Client([
                    'base_uri' => $predictionURL,
                    'timeout' => $httpTimeout,
                    'connect_timeout' => $httpTimeout,
                    'headers' => [
                            'Content-Type' => 'application/json',
                            'x-api-key' => $predictionKey,
                            'api-gateway-key' => $gatewayKey,
                    ],
            ]);
        } catch (Exception $e) {
            $logger->error("Couldn't init the HTTP Client.");
            $logger->error($e->getMessage());
        }
    }

    /**
     * post params to classifier
     * @param string $path
     * @param array $params, format will be ['key1'=>'value1', 'key2'=>'value2',]
     * @return response
     */
    public function post2Classifier($path, $params)
    {
        $logger = $this->ioc->getService("logger");

        try{
            $response = $this->classifierClient->request('POST', $path, [
                    'form_params' => $params
                ]
            );
            return $response;
        }catch(RequestException $e){
            $logger->error(\GuzzleHttp\Psr7\str($e->getRequest()));
            if ($e->hasResponse()) {
                $logger->error(\GuzzleHttp\Psr7\str($e->getResponse()));
            }
        }
    }

    /**
     * post json params to prediction
     * @param string $path
     * @param array $params, format will be ['key1' => 'value1', 'key2' => 'value2',]
     * @return response
     */
    public function post2Prediction($path, $params)
    {
        $logger = $this->ioc->getService("logger");

        try{
            $response = $this->predictionClient->request('POST', $path, [
                    'json' => $params
            ]);
            return $response;
        }catch(RequestException $e){
            $logger->error(\GuzzleHttp\Psr7\str($e->getRequest()));
            if ($e->hasResponse()) {
                $logger->error(\GuzzleHttp\Psr7\str($e->getResponse()));
            }
        }
    }

}

?>

Test class to cover all the functions in WebHttpClientTest.php.
<?php

use \JobConsumerPHP\IOCUtil;
use function GuzzleHttp\json_decode;

class WebHttpClientTest extends PHPUnit_Framework_TestCase{


protected $webHttpClient;


protected function setUp()
{
$ioc = new IOCUtil();

$this->webHttpClient = $ioc->getService("webHttpClient");

}


public function testDummy()
{
$this->assertTrue(true);

}


public function testPost2Classifier()
{
$path = '/get/job/industry/predictions';

$params = [

'title'=>'senior engineer',



'description'=>'java, scala, python, php, nodejs',



];

$response = $this->webHttpClient->post2Classifier($path, $params);

$this->assertNotEmpty($response);

$this->assertEquals(200, $response->getStatusCode());

$this->assertNotEmpty($response->getBody());

$result = json_decode($response->getBody(), true);

$this->assertNotEmpty($result);

$this->assertTrue(count($result) > 0 );

$this->assertNotEmpty($result[0]);

$this->assertNotEmpty($result[0]['majorCategory']);

}


public function testPost2Prediction()
{
$path = '/v0.9/campaigns/3275/jobPrediction?accountID=0';

$params = [

'applicationName' => 'Sample Application',



'title' => 'Senior Engineer',



'description' => 'Java, Scala, Python, Perl, NodeJS, Groovy, PHP',



'jobCompanyName' => 'Sample Company',



'locations' => [ array("postalCode" => "78729", "postalCode" => "78749") ],



];

$response = $this->webHttpClient->post2Prediction($path, $params);

$this->assertNotEmpty($response);

$this->assertEquals(200, $response->getStatusCode());

$this->assertNotEmpty($response->getBody());





$result = json_decode($response->getBody(), true);

$this->assertNotEmpty($result);

$this->assertTrue(count($result) > 0);

$this->assertNotEmpty($result['jobPredictions']);

$this->assertNotEmpty($result['jobPredictions'][0]);

$this->assertNotEmpty($result['jobPredictions'][0]['suggestedBudget']);

}


}

?>

References:
Redis Command Doc
http://redis.io/commands#sorted_set
Redis PHP Doc
https://github.com/phpredis/phpredis#zrange

https://www.neontsunami.com/posts/how-to-solve-phpunit-exiting-without-errors

猜你喜欢

转载自sillycat.iteye.com/blog/2306459