AMAZONSQS (1) PHPProducer

Source: Internet
Author: User
AMAZONSQS (1) PHPProducerAMAZONSQS (1) protocol: 1million2 $ SendThroughput: & nbsp; ReceiveThroughput: MessageLatency: & nbsp; 500 msMessageS 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 will install all the dependencies
> Php composer. phar install

Here is my composer. json which will identify the dependencies.
{
"Require ":{
"Aws/aws-sdk-php": "3.0.6 ",
"Predis/predis": "1.0.1"
}
}

And command below will do the magic, something maven or ivy
> Php composer. phar install

Some import classes will 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 (
'Queuurl' => 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 (
'Queuurl' => 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 (
'Queuurl' => 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 -- 1 carl staff 116 Jul 6 credentials
Carl-mac:. aws carl $

The content will 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.