优化lumen加载速度

通过xdebug+QCachegrind分析lumen一个项目里的某一接口

分析每一个时间较长或者调用次数较多的函数
先看了下route这块用到的是nikic/fast-route 其中按照官方文档介绍,动态路由比静态路由慢,所以项目路由设计时我们添加的都是静态路由,而项目自带一个欢迎页面的动态路由。

优化方案,干掉动态路由,静态路由函数优化:找到
vendor/nikic/fast-route/src/DataGenerator/RegexBasedAbstract.php
的addStaticRoute方法
其中为了验证添加的路由是否有效,每一条记录都做了循环校验,这里其实可以在开发环境中启用,线上干掉来提速。
注释代码
/*
if (isset($this->methodToRegexToRoutesMap[$httpMethod])) {

foreach ($this->methodToRegexToRoutesMap[$httpMethod] as $route) {
if ($route->matches($routeStr)) {
throw new BadRouteException(sprintf(
'Static route "%s" is shadowed by previously defined variable route "%s" for method "%s"',
$routeStr, $route->regex, $httpMethod
));
}
}
}

*/

整个请求最重的就是mysql数据库操作了,其中prepare是pdo中做安全验证的必要组件,参考php手册

http://www.php.net/manual/de/pdo.prepare.php


http://stackoverflow.com/questions/1176352/pdo-prepared-inserts-multiple-rows-in-single-query
有说php5.6中array_push 比array_merge快很多