AMAZON SQS (a) PHP Producer
Last Update:2016-06-13
Source: Internet
Author: User
AMAZON SQS (1) PHP Producer
AMAZON SQS (1) PHP Producer
1. Some Basic Information
Price:1 million 2$
Send Throughput:
Receive Throughput:
Message latency:500 ms
Message size:256 KB
Batch size:10 Message
2. Env Set up
Install composer
> Curl-ss Https://getcomposer.org/installer | Php
Install the latest package
> PHP Composer.phar require aws/aws-sdk-php
This would install all the dependencies
> PHP Composer.phar Install
This is my composer.json which would identify the dependencies.
{
"Require": {
"aws/aws-sdk-php": "3.0.6",
"Predis/predis": "1.0.1"
}
}
and command below'll do the magic, something maven or ivy
>php Composer.phar Install
Some import classes would be as follow,
Sqsconnector.inc
Require ' vendor/autoload.php ';
Use aws\sqs\sqsclient;
Class Sqsconnector {
Const QUEUEURL = "Https://sqs.us-east-1.amazonaws.com/xx_test";
protected $client = null;
function __construct () {
try {
$this->client = sqsclient::factory (Array (
' Region ' = ' us-east-1 ',
' Version ' = ' latest '
));
echo "Successfully connected to sqs\n";
} catch (Exception $e) {
echo "couldn ' t connected to SQS";
echo $e->getmessage ();
}
}
}
A time Track class which can provide us more detail about the time consuming during the process, Timetracker.inc
Class Timetracker implements countable {
protected $count = 0;
protected $distribution;
protected $distributionPrecision = 0;
protected $startTime;
protected $totalTime;
protected $name;
Public function __construct ($name, $distributionPrecision = 2) {
$this->name = $name;
$this->distribution = Array ();
$this->distributionprecision = $distributionPrecision;
$this->start ();
}
Public function count () {
return $this->count;
}
Public Function Start ($time = null) {
return $this->starttime = ($time = = = null)? Microtime (true): $time;
}
Public Function Stop ($count = 1, $time = null) {
$interval = ($time = = = null)? Microtime (True): $time)-$this->starttime;
$this->totaltime + = $interval;
$key = (string) round ($interval, $this->distributionprecision);
if (!array_key_exists ($key, $this->distribution)) $this->distribution[$key] = 0;
$this->distribution[$key]++;
$this->count + = $count;
return $interval;
}
Public Function __tostring () {
Ksort ($this->distribution);
Return "{$this->name}:". Print_r ($this->distribution, True).
Return "{$this->name}: {$this->count}m". Round ($this->totaltime,2). ' s '.
Round ($this->count/$this->totaltime,1). "M/s\n";
}
}
Async Send the message via PHP in sqssender.php
#!/usr/bin/php
Require_once ' Timetracker.inc ';
Require_once ' Sqsconnector.inc ';
Use guzzlehttp\promise;
Define (' Msgnum_request ', 10);
Define (' Maxsize_request ', 262144);
Class Sqssender extends Sqsconnector {
function SendMessage ($job) {
$timer = new Timetracker (' sendMessage ');
$promise _array = Array ();
if (strlen ($job) >= maxsize_request)
{
echo "The size of message is larger than 256kb\n";
Var_dump ($job);
Return
}
$promise = $this->client->sendmessageasync (Array (
' Queueurl ' = Self::queueurl,
' MessageBody ' = $job,
));
$promise->wait ();
$timer->stop ();
Print $timer;
}
function Sendmessagebatch ($jobs, $timer) {
$promise _array = Array ();
$id = 1;
$message = ";
$msg _num = 0;
foreach ($jobs as $job)
{
$maxsize _msg = (int) (maxsize_request/msgnum_request);
$tmp _msg = $message. $job;
if (strlen ($job) >= $maxsize _msg)
{
$this->sendmessage ($job);
}
else if (strlen ($tmp _msg) >= $maxsize _msg)
{
$entries [] = Array (
' Id ' = $id,
' MessageBody ' = $message
);
$id + +;
$message = $job;
if ($id > 10) {
$promise = $this->client->sendmessagebatchasync (Array (
' Queueurl ' = Self::queueurl,
' Entries ' = $entries,
));
$entries = Array ();
$id = 1;
$promise _array[' key '. Count ($timer)] = $promise;
/*if (Count ($promise _array)% 50 = = 0) {
Promise\unwrap ($promise _array); Unwrap the Promise list and wait for all promise complete
$promise _array = Array ();
}*/
$timer->stop ($msg _num);
$msg _num = 0;
$timer->start ();
}
$msg _num++;
}
Else
{
$message = $tmp _msg;
$msg _num++;
}
}
$entries [] = Array (
' Id ' = $id,
' MessageBody ' = $message
);
$promise = $this->client->sendmessagebatchasync (Array (
' Queueurl ' = Self::queueurl,
' Entries ' = $entries,
));
$promise _array[' key '. Count ($timer)] = $promise;
Promise\unwrap ($promise _array); Unwrap the Promise list and wait for all promise complete
$promise _array = Array ();
$timer->stop ($msg _num);
Print $timer;
$timer->start ();
}
}
/* $sender = new Sqssender;
$promise = $sender->send (500);
if (count ($promise) > 0) {
Promise\unwrap ($promises);
}*/
?>
Before we run the test, the AMAZON require us to configure the key here>
Carl-mac:.aws carl$ pwd
/users/carl/.aws
Carl-mac:.aws carl$ Ls-l
Total 8
-rw-r--r--1 Carl Staff 17:00 Jul 6 credentials
Carl-mac:.aws carl$
The content would be as follow:
[Default]
aws_access_key_id = xxxxxxxx
Aws_secret_access_key = xxxxxxxxxxxxx
References:
http://colby.id.au/benchmarking-sqs/
http://www.warski.org/blog/2014/06/benchmarking-sqs/
http://nsono.net/amazon-sqs-vs-rabbitmq/
Kafka
Http://engineering.linkedin.com/kafka/benchmarking-apache-kafka-2-million-writes-second-three-cheap-machines
Sqs
Http://docs.aws.amazon.com/aws-sdk-php/guide/latest/service-sqs.html
http://aws.amazon.com/documentation/sqs/
Http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/promises.html
Http://docs.aws.amazon.com/aws-sdk-php/v3/api/index.html
Scala queue
Https://github.com/adamw/mqperf/tree/master/src/main/scala/com/softwaremill/mqperf/mq
SQS Auto Scaling
https://aws.amazon.com/articles/Amazon-SQS/1464