这里有新鲜出炉的精品教程,程序狗速度看过来!
修复使用 CSpeed 框架进行 API 项目时的高并发情况下的PHP崩溃的情况。
- $app = new App::getApp(); $app->get('/', function(){ echo "hello cspeed"; });
此情况见于 2.0.0 版本。已在新版本中予以修复。
新版本中 getApp() 方法是一个单例模式控制器获取方法;不再适应于创建一个 CsApp 对象, 新版本中全部更新为 构造函数形式。可以不传入任何的参数来生成一个 零 IO 消耗的 application 对象 。
控制器全部更新为命名空间形式,如路由:
- http://www.supjos.com/hello/cspeed/news
导向到的是 hello 模块中的 Cspeed 控制器中的 newsAction 方法:
控制器的格式如下:
左右的控制器都必须继承自 CsmvcController 类:
- <?php namespace appmoduleshellocontrollers; class Cspeed extends CsmvcControllers { /** * 初始化方法,如果有父类的话,则会先从顶级父类开始执行到本方法 * 来完成初始化 */ function initialise() { } /** * 执行具体的方法之前先执行的方法 * 不会执行父类的本方法 */ function __beforeActoin() { } /** * 具体的方法 */ function newsActon() { } /** * 执行具体的方法之后执行的方法 * 不会执行父类的本方法 */ function __afterActon() { } }
CSpeed扩展目前在 Github 与 码云 平台均有代码存储库,用户只需下载源码然后按照如下方法安装即可:
Github:
- https://github.com/liqiongfan/cspeed
码云:
- https://gitee.com/josinli/cspeed
安装步骤:
- 1、 / usr / local / php_to_path / bin / phpize2、. / configure --with - php - config = /usr/local / php_to_path / bin / php - config3、make install
编译完成后在php.ini配置文件添加如下内容:
- extension_dir = "/usr/local/php-7.1.8-nts/lib/php/extensions/no-debug-non-zts-20160303/"extension = cspeed.so
然后重启 Nginx 的 PHP-FPM 或者 Apache:
- 4、systemctl restart php - fpm 或者 systemctl restart httpd
测试命令:
- /usr/local/httpd-2.4.29/bin/ab -c100 -n100000 http://www.supjos.com/hello/cspeed/
测试机器[特意开启旧的笔记本进行测试,未开启工作站]:
- CPU: Intel(R) Core(TM) i5 - 2430M CPU @ 2.40GHz硬盘:HDD 750GB 2009年内存:4G Sansung
测试环境:
- nginx 1.12.1 php 7.1.5 php - fpm
测试结果:
- Concurrency Level: 100 Time taken for tests: 9.781 seconds Complete requests: 100000 Failed requests: 0 Total transferred: 18500000 bytes html transferred: 0 bytes Requests per second: 10223.94 [#/sec] (mean) Time per request: 9.781 [ms] (mean) Time per request: 0.098 [ms] (mean, across all concurrent requests) Transfer rate: 1847.10 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 1 0.9 0 9 Processing: 1 9 1.8 9 25 Waiting: 1 9 1.8 9 24 Total: 4 10 1.3 10 28 WARNING: The median and mean for the initial connection time are not within a normal deviation These results are probably not that reliable. Percentage of the requests served within a certain time (ms) 50% 10 66% 10 75% 10 80% 10 90% 11 95% 12 98% 13 99% 13 100% 28 (longest request)
测试后单次,不产生任何IO的情况下可以达到 10223reqs/s,持平于1万左右吞吐量。解析ini文件的情况下,可达到将近9千的吞吐量.
- + --cspeed 入口文件 + --app 项目目录 + ---config 配置目录 | ---core.ini 配置文件 + ---models 通用模型目录 | ---User.php User模型 | ---Goods.php Goods模型 + ---modules 模块目录 + ---backend 后台模块 + ---controllers backend模块控制器目录 | ---Index.php Index控制器 | ---About.php About控制器 + ---views 视图目录 + ---index Index控制器的视图目录 | ---index.phtml Index控制器的index方法的视图文件 + ---about About控制器的视图目录 | ---index.phtml About控制器的index方法的视图文件 + ---home Home模块 + ---controllers Home模块控制器目录 | ---Index.php Index控制器 + ---views 视图目录 + ---index Index控制器的视图目录 | ---index.phtml Index控制器的index方法的视图文件 + ---bootstrap.php 框架的初始化类Bootstrap + --public 入口目录 | ---index.php 入口文件 + ---assets 资源目录 + ---CSS CSS资源 + ---js JS资源 + ---img 图片资源
1、web示例
- $app = new CsApp("../app/config/core.ini", "dev"); $app->bootstrap()->run();
CsApp类的构造函数支持传入绝对路径或者相对路径的INI文件,第二个参数是INI配置文件的解析节点,构造函数的两个参数都可以省略来进行API框架的配置达到减少IO操作的目的.
一个复杂的WEB应用就只需以上两行代码就可搞定。
2、API示例
- $app = new CsApp(); $app->get('/hello/cspeed/:any:', function($any){ echo "<div style='text-align:center;'>Hello CSpeed User, The any value is : $any.</div>"; });
RESTful API 项目只需要添加对应的请求方法即可。
- [core] core.application = '../app' ; WEB目录 core.bootstrap = '../app/bootstrap.php' ; 指定bootstrap 类目录 core.bootstrap.method.string = '__init' ; 指定Bootstrap类的初始化方法的前缀 core.router.modules = index,home,back ; 注册多模块 core.router.default.module = index ; 默认模块 core.router.default.controller = Index ; 默认控制器 core.router.default.action = index ; 默认方法 core.view.ext = phtml ; 视图文件后缀 core.view.auto.render = 0 ; 是否自动渲染视图,1:自动渲染、0:不渲染 [db] db.master.type = mysql ; 数据库类型,默认:mysql db.master.host = localhost ; 数据库主机地址 db.master.port = 3306 ; 数据库端口 db.master.dbname = supjos ; 数据库名称 db.master.username = root ; 数据库用户名 db.master.password = 3333 ; 数据库密码 [dev:core] core.application = '../app' ; WEB目录 core.bootstrap = '../app/bootstrap.php' ; 指定bootstrap 类目录 core.bootstrap.method.string = '__init' ; 指定Bootstrap类的初始化方法的前缀 core.router.modules = index,home ; 注册多模块 core.router.default.module = index ; 默认模块 core.router.default.controller = Index ; 默认控制器 core.router.default.action = index ; 默认方法 core.view.ext = xhtml ; 视图文件后缀
- class Bootstrap implements CsBootstrap { /* 初始化路由与视图 */ function __initRouter($di, $router) { $di->set('view', function(){ return new CsmvcView(); }); $router->add( '/back/:action:/:id:', '/shop/list/$1' ); $router->add( '/shop/:controller:/:action:/:any:', '/get/1/2/3' ); } /* 初始化数据库连接 */ function __initDb($di, $router) { $di->set('db', function(){ return new CsdbpdoAdapter(); }); } }
- <?php class Site extends CsmvcController { /** * 本方法在每个控制器方法调用前进行调用 */ function __beforeAction() { } /** * 需要执行的方法 */ function indexAction() { // 如果开启自动渲染视图,可以使用 $this->view->setVar()方法向模板传输数据 } /** * 在每个控制器方法调用执行后进行调用 */ function __afterAction() { } }
- <?php namespace appmodels; class User extends CsmvcModel { function tableName() { return "www_product"; } }
来源: http://www.phperz.com/article/17/1031/352076.html