php框架的功能通用的路由,autoload。服务端mysql封装,日志组件。前端的页面渲染(smarty封装个)。在工做时据说别的部门换框架性能提高,因此调研了下常见的框架,包含ci,laravel,yii,yaf,会介绍下功能,另外给出号称最快框架yaf和经常使用yii和裸写框架的性能差。另外想实现rpc并发,http通常用过multi_curl能够,公司用的thrift没有实现并发,因此研究了下php协程,curl_multi,swoole异步,rpc中并发实现,corotine等。php
laravel https://laravel-china.org/doc...
路由
中间件(前置后置)
配置区分环境,本地和线上密码不放其中
数据库 建立表,编辑,删除,迁移,回滚,软删除和恢复(标记删除位)。ORM链式操做
依赖注入,依赖自动发现。html
IOC 平时的if new 这种工厂模式,IoC模式看做工厂模式的升华,之前在工厂模式里写死了的对象,IoC模式 改成配置XML文件,这就把工厂和要生成的对象二者隔离 类(DatabaseQueue,queue,QueueContract),serviceprovider(外部调这个)=>bind(将类绑定到容器)。调用Queue::xx。依赖注入能够直接调用$类->method。经过门面能够类::method 【https://www.cnblogs.com/shiwenhu/p/6882340.html】
事件前端
事件映射protected $listen = [ 'App\Events\OrderShipped' => [ 'App\Listeners\SendShipmentNotification', ],]; 写事件OrderShipped,写监听SendShipmentNotification 能够继承队列 分发事件:public function ship($orderId) { order=Order::findOrFail(orderId); // 订单的发货逻辑... event(new OrderShipped($order)); }
队列 ,redis,db, 广播
js监听
任务调度 只是cronmysql
yii https://www.yiichina.com/doc/...
功能全面读介于ci和laravel之间,前端支持功能丰富。组件和行为是它的特点
行为laravel
要定义行为,经过继承 yii\base\Behavior 。覆盖其中的events方法, public function events() { return [ ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate', ]; } public function beforeValidate($event) { // 处理器方法逻辑 }, 附加行为:lass User extends ActiveRecord { public function behaviors() { return [ [ 'class' => MyBehavior::className(), 'prop1' => 'value1', 'prop2' => 'value2', ]]}}或者attach组件就能够使用行为了。
直观感觉下git
裸写在CPU占用方面和YAF相近,略高,在内存方面更节省。在吞吐量方面和YAF相近,略好。
CI框架会比YAF在CPU方面多耗费10%。吞吐量要差75%github
yafweb
index.php <?php define('APPLICATION_PATH', dirname(__FILE__)); $performance = getrusage(); $globalPerformStatics['cpu_time_start'] = $performance['ru_utime.tv_sec'] *1000000+ $performance['ru_utime.tv_usec']+$performance['ru_stime.tv_sec'] *1000000+$performance['ru_stime.tv_usec']; $application = new Yaf_Application( APPLICATION_PATH . "/conf/application.ini"); $application->bootstrap()->run(); $performance = getrusage(); $globalPerformStatics['cpu_time_end'] = $performance['ru_utime.tv_sec'] *1000000+ $performance['ru_utime.tv_usec']+$performance['ru_stime.tv_sec'] *1000000+$performance['ru_stime.tv_usec']; var_dump($globalPerformStatics['cpu_time_end']-$globalPerformStatics['cpu_time_start']); ?> ———————————————————————————————— Bootstrap.php <?php class Bootstrap extends Yaf_Bootstrap_Abstract { public function _initRoute(Yaf_Dispatcher $dispatcher) { $router = Yaf_Dispatcher::getInstance()->getRouter(); $route = new Yaf_Route_Rewrite( 'order/base/passenger/getinfo', array( 'controller' => 'Base_Passenger_Getinfo', ) ); $router->addRoute('html1asdfd', $route); } public function _initView(Yaf_Dispatcher $dispatcher) { Yaf_Dispatcher::getInstance()->autoRender(false); } } ———————————————————————————————— 建立controller/Base/Passenger/Getinfo.php <?php class Base_Passenger_GetInfoController extends Yaf_Controller_Abstract { public function indexAction($name = "Stranger") { echo "hello"; $orderId = $this->getRequest()->getQuery("order_id"); $model = new SampleModel(); $orderInfo = $model->selectSample($orderId); var_dump($orderInfo); return TRUE; } } ———————————————————————————————— 建立Model <?php /** * @name SampleModel * @desc sample数据获取类, 能够访问数据库,文件,其它系统等 * @author */ class SampleModel { public function __construct() { } public function selectSample($id) { $a = $id / 5.314; // 取反正切 0-5.314的变化区间 $b = 1000 / 1.520837931073; return intval((100 / $a) * $b); } public function insertSample($arrInfo) { return true; } }
裸写redis
index.php <?php define('FRAMEPATH', '/home/project/phputil/'); define('APPPATH', '/home/project/order/'); $performance = getrusage();echo 1; $globalPerformStatics['cpu_time_start'] = $performance['ru_utime.tv_sec'] *1000000+ $performance['ru_utime.tv_usec']+$performance['ru_stime.tv_sec'] *1000000+$performance['ru_stime.tv_usec']; $appNameSpace = 'Project\Order'; $_GET = array_merge($_GET, $_POST);//fix Android端把$_GET改为$_POST问题 require_once(FRAMEPATH . '/autoload/autoloader.php'); $loader = Project\Autoload\Autoloader::getLoader(); $loader->addPsr4('Project\Order\\', APPPATH); require_once(FRAMEPATH . '/framework.php'); $performance = getrusage(); $globalPerformStatics['cpu_time_end'] = $performance['ru_utime.tv_sec'] *1000000+ $performance['ru_utime.tv_usec']+$performance['ru_stime.tv_sec'] *1000000+$performance['ru_stime.tv_usec']; var_dump($globalPerformStatics['cpu_time_end']-$globalPerformStatics['cpu_time_start']); ?> ———————————————————————————————— config/route.php <?php namespace Project\Order\Config; class Route { public static $routes = array( 'order/(.+)' => '$1', ); } ———————————————————————————————— helper/order.php <?php namespace Project\Order\Helper; class Order{ public static function selectSample($id) { $a = $id / 5.314; // 取反正切 0-5.314的变化区间 $b = 1000 / 1.520837931073; return intval((100 / $a) * $b); } } ———————————————————————————————— phputil <?php $loader->addPsr4('Project\Framework\\', FRAMEPATH . ''); //全局变量定义(暂时作法,限制性使用) $__uid = 0; $_startTime = ''; $_redisCount = 0; $_mysqlCount = 0; $_httpRpcCount = 0; $_thriftRpcCount = 0; try { $params = array('get' => $_GET, 'post' => $_POST); $routerConfigPath = $appNameSpace . '\Config\Route'; $routerConfig = array(); $errorPageConfig = false; if(class_exists($routerConfigPath)){ if(!empty($routerConfigPath::$routes) && is_array($routerConfigPath::$routes)){ $routerConfig = $routerConfigPath::$routes; } if(!empty($routerConfigPath::$showErrorPage)){ $errorPageConfig = $routerConfigPath::$showErrorPage; } } $_startTime = microtime(true); $router = new \Project\Framework\Base\Router($_SERVER['REQUEST_URI'], $routerConfig,$errorPageConfig); $router->setRoute(); $router->run($params); } catch (\InvalidArgumentException $ex) { $errNo = -1; $errMsg = strlen($ex->getMessage()) ? $ex->getMessage() : 'system error'; var_dump(array('errno' => $errNo, 'errmsg' => $errMsg)); } catch (\Exception $ex) { $errNo = $ex->getCode(); $errMsg = $ex->getMessage(); var_dump(array('errno' => $errNo, 'errmsg' => $errMsg)); }
cisql
config/route.php CI多级目录支持须要本身开发,粗暴的把路由直接打到welcome.php $route['(.+)'] = 'welcome'; ———————————————————————————————— controller/Welcome.php <?php class Welcome extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('SampleModel'); } public function index() { echo "hello"; $orderId=$this->input->get('order_id'); $orderInfo = $this->SampleModel->selectSample($orderId); var_dump($orderInfo); return 1; } } ———————————————————————————————— model/SampleModel.php <?php class SampleModel extends CI_Model { public function selectSample($id) { $a = $id / 5.314; // 取反正切 0-5.314的变化区间 $b = 1000 / 1.520837931073; return intval((100 / $a) * $b); } public function insertSample($arrInfo) { return true; } }
php为什么不用多线程:
pthreads v3 is restricted to operating in CLI only: I have spent many years trying to explain that threads in a web server just don't make sense, after 1,111 commits to pthreads I have realised that, my advice is going unheeded.
So I'm promoting the advice to hard and fast fact: you can't use pthreads safely and sensibly anywhere but CLI.
Thanks for listening ;)
设为非阻塞的socket,调用libcurl的方法。也是select后执行,跟咱们php协程处理方式差很少
curl_multi_init
curl_multi_add_handle
curl_multi_select
select有结果后curl_multi_perform // multi_runsingle.一个一个进行,状态流转,从任何一个状态均可以继续执行
当select结束后curl_multi_info_read
curl_multi_remove_handle,curl_multi_cleanup
swoole:https://wiki.swoole.com/wiki/...
协程原理:跳堆栈的原理
异步:客户端/mysql/redis/http等。这些只能在cli下调用。不能在fpm中, 异步就是设为非阻塞的socket, 而后设置回调。epoll。一个进程就好了。
http协议封装libcurl.其余协议 状态机+epoll
须要分开提供发送和读取两个接口。对发送和读取两个步骤实现异步。先发送一批再读取,不是发完了等待接收再下一个。