Asynchronous Web server + asynchronous Redis Client for PHP

Source: Internet
Author: User
Tags download redis php server php web server

PHP's asynchronous parallel swoole extension has an HTTP server built into the 1.7.7, which makes it easy to implement an asynchronous PHP Web server with Swoole_http_server that is several times more powerful than a synchronous, blocked server such as Php-fpm/apache.

Swoole official also provides Redis-async, an asynchronous io+ connection pool for Redis clients. These 2 features combine to create a Web application that is concurrent with request tens of thousands of.

How to use 1. Download and install the swoole extension

You can use pecl to install or download the latest stable version of Swoole from GitHub.

PECL Install Swoole

Modify PHP.ini Join extension=swoole.so

2. Download Redis-async code
git clone https://github.com/swoole/redis-async.git

3. Write Server code server.php
$http = new Swoole_http_server ("127.0.0.1", 9501), $http->set ([' worker_num ' = 8]); Require __dir__. ' /src/swoole/async/redisclient.php '; $redis = new Swoole\async\redisclient (' 127.0.0.1 '); $http->on (' request '), function ($request, $response) use ($redis) {$redis->get (' Key1 ', function ($result) use ($response) {$respons E->end ("

Run the server.php program, where a total of 8 processes are started. Note that because it is an asynchronous, non-blocking server program, you do not need to turn on the hundreds of process like apache/php-fpm. There is absolutely no waiting, all event-driven. When a request arrives to initiate a Redis request, the redis-server response triggers the corresponding event, renders the page, and sends the HTML page to the browser via the $response->end interface.

PHP server.php

The HTTP server starts listening on port 9501 and the http://127.0.0.1:9501 access page can be opened in the browser. The logic of this program is simple, just take a data from Redis and render the page.

4. Use AB Tools for stress testing
Ab-c 200-n 100000-k http://127.0.0.1:9501/

Machine environment is: Inter CoreI5 4 core cpu+8g memory, Ubuntu 14.04

Results of pressure measurement:

Server software:        swoole-http-serverserver hostname:         127.0.0.1Server Port:             9501Document Path:           /document length:        40 bytesconcurrency  level:      200time taken for tests:   2.853  secondsComplete requests:      100000Failed requests:         0Keep-Alive requests:    100000Total  transferred:      16800000 byteshtml transferred:        4000000 bytesRequests per second:    35049.02  [#/sec]  (mean) time p er request:       5.706 [ms]  (mean) Time per  request:       0.029 [ms]  (mean, across all  concurrent requests) transfer rate:           5750.23 [kbytes/sec] received

Can reach 35,000 QPS, the performance is amazing, just use a common PC machine, hardware performance generally. If you are in a server hardware environment, performance can be better.


Asynchronous Web server + asynchronous Redis Client for PHP

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.