ardeshireshghi / concise
Requires
- php: >7.1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.13
- phpunit/phpunit: ^7.5
This package is not auto-updated.
Last update: 2024-09-20 20:26:24 UTC
README
Concise:PHP的函数式微Web框架
ConcisePHP是一个PHP微框架,它使得创建快速且强大的Web应用和API变得无缝。
先决条件
要使用Concise,您需要PHP版本等于或高于7.1。
PHP >7.1
在Linux上,请确保已安装xml和mbstring PHP模块。
安装
composer require ardeshireshghi/concise:0.4.3
入门(Hello World)
创建app.php文件并添加以下内容
<?php
require './vendor/autoload.php';
use function Concise\app;
use function Concise\Routing\get;
use function Concise\Http\Response\response;
use function Concise\Http\Request\url;
use function Concise\Http\Request\path;
use function Concise\Middleware\Factory\create as createMiddleware;
use function Concise\FP\curry;
use function Concise\FP\ifElse;
function createLogger()
{
$outputFileHandler = fopen('php://stdout', 'w');
return function (string $message, array $context = null) use ($outputFileHandler) {
fwrite($outputFileHandler, $message);
return $context;
};
}
/**
* Curried logger which logs message and returns the context
*
* @param string $message Message to output
* @param array $context to be passed to the next function
* @return mixed Either the curried function or the array context
*/
function logger(...$thisArgs)
{
static $logger = null;
if (!$logger) {
$logger = createLogger();
}
return curry($logger)(...$thisArgs);
}
function loggerMiddleware()
{
return createMiddleware(function (callable $nextRouteHandler, array $middlewareParams = [], array $request) {
return $nextRouteHandler(ifElse(
function($request) {
return $request['meta']['hasRouteMatch'];
},
logger("\n\nRoute with path".path().' matching. Params are: '.implode(',', $request['params'])."\n"),
logger("\nNo route matching for: ".url(). "\n")
)(logger("\n\nRequest: ".json_encode($request))($request)));
});
}
function routes()
{
return [
get('/hello/:name')(function ($request) {
return response('Welcome to Concise, ' . $request['params']['name'], []);
})
];
}
function middlewares()
{
return [
loggerMiddleware()
];
}
logger("\nResponse: ".json_encode(app(routes())(middlewares()))."\n")([]);
并尝试使用PHP服务器运行它
$ php -S localhost:5000 app.php
访问https://:5000/hello/coder将显示“欢迎来到Concise,coder”。
更多资源
文档网站(WIP)
有关更多资源和指南,请参阅DOCUMENTATION网站。
示例:Web API脚本
在/examples/web-api/api.php中也有一个示例应用。在克隆仓库后,您可以使用以下命令运行它
$ composer run-script start:web_api
使用以下命令测试authMiddleware和JSON有效负载的解析
curl -X POST -H 'Authorization: Bearer abcd1234efgh5678' -H 'Content-Type: application/json' --data '{"filename": "test.jpg"}' http://127.0.0.1:5000/api/upload
开发
安装
$ composer install
测试
要执行测试套件,您需要PHPUnit。
$ phpunit --colors
基准测试
基准测试是在1核CPU、2GB RAM的Centos 7.5云服务器上进行的,在设置好默认配置的Nginx和php-fpm(PHP 7.3)之后。性能已与Laravel进行比较,通过在Http/Kernel.php中禁用Laravel中间件,并将主页控制器更改为仅显示“Hello world”。还使用上述代码示例为Concise,同样没有使用日志中间件。测试向每个应用发送500个请求,并发为50个请求。以下是Apache Bench命令的输出
Concise结果
$ ab -n 500 -c 50 https://:82/hello/world
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, https://apache.ac.cn/
Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests
Server Software: nginx/1.12.2
Server Hostname: 127.0.0.1
Server Port: 82
Document Path: /hello/ardi
Document Length: 24 bytes
Concurrency Level: 50
Time taken for tests: 1.154 seconds
Complete requests: 500
Failed requests: 0
Write errors: 0
Total transferred: 151500 bytes
HTML transferred: 12000 bytes
Requests per second: 433.18 [#/sec] (mean)
Time per request: 115.425 [ms] (mean)
Time per request: 2.308 [ms] (mean, across all concurrent requests)
Transfer rate: 128.18 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 2 0.9 2 4
Processing: 7 110 25.8 114 162
Waiting: 5 108 25.4 113 160
Total: 8 111 25.7 117 163
Percentage of the requests served within a certain time (ms)
50% 117
66% 119
75% 122
80% 123
90% 123
95% 163
98% 163
99% 163
100% 163 (longest request)
Laravel结果
$ ab -n 500 -c 50 https://:81/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, https://apache.ac.cn/
Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests
Server Software: nginx/1.12.2
Server Hostname: 127.0.0.1
Server Port: 81
Document Path: /
Document Length: 11 bytes
Concurrency Level: 50
Time taken for tests: 15.738 seconds
Complete requests: 500
Failed requests: 0
Write errors: 0
Total transferred: 122500 bytes
HTML transferred: 5500 bytes
Requests per second: 31.77 [#/sec] (mean)
Time per request: 1573.850 [ms] (mean)
Time per request: 31.477 [ms] (mean, across all concurrent requests)
Transfer rate: 7.60 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.5 0 2
Processing: 202 1535 228.8 1525 2180
Waiting: 200 1535 228.7 1525 2180
Total: 203 1535 228.8 1525 2180
Percentage of the requests served within a certain time (ms)
50% 1525
66% 1574
75% 1648
80% 1690
90% 1837
95% 1913
98% 2048
99% 2102
100% 2180 (longest request)
基准测试结论
Concise的请求处理速度比Laravel快14倍(平均每秒433个请求与Laravel的每秒32个请求)
这可能不是一个很好的比较,因为Laravel是一个框架,其自然较重,而Concise是一个微框架,较轻。但它确实展示了Concise可以肯定是一个构建微服务的良好选择。
贡献
请阅读CONTRIBUTING.md以了解我们的行为准则以及向我们提交拉取请求的过程。
作者
- Ardeshir Eshghi - ardeshireshghi
许可
此项目受MIT许可协议的许可 - 有关详细信息,请参阅LICENSE.md文件
