gjerokrsteski/reactphp-pimf

该包最新版本(v0.1.0)没有提供许可信息。

使用 ReactPHP 和 PIMF PHP 微框架的响应式 RESTful API

v0.1.0 2017-01-05 14:45 UTC

This package is not auto-updated.

Last update: 2024-09-20 20:00:44 UTC


README

这是一个响应式 RESTful API,它使用 ReactPHP 和 PIMF 微框架。

Build Status Code Climate

ReactPHP 是最有前景的库之一。它将事件驱动、非阻塞 I/O(类似于 NodeJS)的强大概念引入 PHP。有了这种技术,我们能够在 PHP 中编写自己的 HTTP 堆栈,并在请求结束时控制内存而不将其破坏。

ReactPHP 负责建立 TCP-Socket,传输原始 HTTP 请求并发送 HTTP 响应。

PIMF 微框架 负责使用 HTTP 请求方法、验证和处理持久性交互来管理 HTTP 资源。

基准测试结果,每秒完成超过 19500 个请求

[vagrant@gkrsteski php-reactor]$ loadtest http://10.0.49.227:5501/articles/10 -t 20 -c 20 --rps 1000
[Wed Feb 10 2016 12:24:47 GMT+0100 (CET)] INFO Requests: 0, requests per second: 0, mean latency: 0 ms
[Wed Feb 10 2016 12:24:52 GMT+0100 (CET)] INFO Requests: 4516, requests per second: 903, mean latency: 10 ms
[Wed Feb 10 2016 12:24:57 GMT+0100 (CET)] INFO Requests: 9515, requests per second: 1000, mean latency: 0 ms
[Wed Feb 10 2016 12:25:02 GMT+0100 (CET)] INFO Requests: 14515, requests per second: 1000, mean latency: 10 ms
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Target URL:          http://10.0.49.227:5501/articles/10
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Max time (s):        20
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Concurrency level:   20
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Agent:               none
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Requests per second: 1000
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Completed requests:  19512
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Total errors:        0
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Total time:          20.000516531000002 s
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Requests per second: 976
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Total time:          20.000516531000002 s
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO Percentage of the requests served within a certain time
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO   50%      2 ms
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO   90%      9 ms
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO   95%      18 ms
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO   99%      30 ms
[Wed Feb 10 2016 12:25:07 GMT+0100 (CET)] INFO  100%      36 ms (longest request)

安装

  1. 克隆此仓库
  2. 运行: ./init-app.sh

使用 PHP 运行

php run-server.php

或使用 Docker 运行

通过运行以下命令为您的应用程序构建 Docker 镜像

docker build -t="reactphp-pimf-api" .

最后,通过运行以下命令以 Docker 容器运行您的应用程序

docker run -d -p 1337:1337 reactphp-pimf-api

试用 RESTful API

  • http://{reactive.hosts}:{reactive.port}/articles/
  • 使用 HTTP 方法 OPTIONS 查找如何使用 API

资源 /articles 的预期 API 行为

HTTP Verb |  Entire Collection (e.g. /articles)                                          | Specific Item (e.g. /articles/{id})
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
GET       | 400 (Bad Request)                                                            | 200 (OK), single article.
          |                                                                              | 404 (Not Found), if ID not found or 400 (Bad Request) if invalid.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
PUT       | 405 (Method Not Allowed)                                                     | 200 (OK),
          | - unless you want to update/replace every resource in the entire collection. | 404 (Not Found) if ID not found or 400 (Bad Request) if invalid.
          | - 'Location' header with link to API documentation                           |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
POST      | 201 (Created),                                                               | 405 (Method Not Allowed)
          | - 'Location' header with link to /articles/{id} containing new ID.           | - 'Location' header with link to API documentation
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
DELETE    | 404 (Not Found) or 405 (Method NOt Allowed)                                  | 200 (OK)
          | - unless you want to delete the whole collection—not often desirable.        | 404 (Not Found) if ID not found or 400 (Bad Request) if ID invalid.
          | - 'Location' header with link to API documentation                           |


运行测试

在项目根目录安装 Composer

php composer.phar require "codeception/codeception:*"
// or ...
composer require "codeception/codeception:*"

执行测试

php vendor/codeception/codeception/codecept run --colors
// or if want to see steps ...
php vendor/codeception/codeception/codecept run --steps
// or if want to see fancy HTML report page
php vendor/codeception/codeception/codecept run --html