狠狠撸

狠狠撸Share a Scribd company logo
OpenResty 项?目性能优化实践
Alex Zhang
Github: https://github.com/tokers
2017/12/23
OpenResty Meetup 杭州
?常?用性能分析?工具

?基于 OpenResty 的项?目的特点

?基于 OpenResty 的项?目细节优化
常?用性能分析?工具
? top
? pidstat
? iostat
? …
资源分析 负载分析
? perf
? SystemTap
? FlameGraph
? …
Perf
? http://www.brendangregg.com/perf.html

? 多种不不同种类事件,perf list

? 进程级别的事件统计, perf stat -p <pid>
? 函数级别的事件统计,perf report -p <pid> && perf record
# perf stat -e 'context-switches,page-faults,branch-misses' -p 2623564
^C
Performance counter stats for process id '2623564':
3 context-switches
0 page-faults
1,346 branch-misses
3.223158849 seconds time elapsed
# perf record -F 100 -p 2623564 -g -- sleep 5
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.009 MB perf.data (5 samples) ]
# perf report
OpenResty 项目性能优化实践
SystemTap
? 动态追踪 -?自定义探针

? DSL - 简单灵活的脚本语?言

? ?用户态空间追踪和内核态空间追踪

? 调?用栈回溯
? ?非侵?入式
OpenResty 项目性能优化实践
FlameGraph
? 直观性
? 交互性
? https://github.com/openresty/openresty-systemtap-toolkit

? https://github.com/openresty/stapxx

? On-CPU & O?-CPU
O?-CPU
- http://www.brendangregg.com/o?cpuanalysis.html
基于 OpenResty 的项?目有何特点?
? 多 worker 模式

? Nginx 事件循环 + 上层 Lua VM 接管

? 单线程,?一个时刻只有?一个请求在被处理理

? ?一个请求可能会经过多次调度之后才完成

? 分阶段的流?水线处理理(11 个阶段)

? 各阶段的 Lua code 运?行行在不不同的 Lua 协程上
? 阻塞事件循环

? 锁抢占

? ngx.ctx VS ngx.var.VARIABLE
? ?日志
? LuaJIT 的优势
? 编程习惯
阻塞事件循环
引?用了了?一些 Lua/C 第三?方库
怎么解决?
使?用 Cosocket
? ngx.shared.DICT — e.g ngx.shared.DICT.get_keys()

? nginx cache
锁抢占
Benchmark
wrk -d 60s -t 4 -c 192 http://127.0.0.1:7106/t
OpenResty 项目性能优化实践
ngx.ctx VS ngx.var.VARIABLE
? ngx.ctx 是?一个“神奇”的 Lua table,?而?用法和普通 Lua table ?一致

? ngx.var.VARIABLE 利利?用了了 nginx 的变量量系统,同样可以?用于存储信息

? ngx.ctx 拥有?比 ngx.var.VARIABLE 更更好的效率
Why ngx.ctx is better
? nginx 变量量只有字符串串?一种类型

? nginx 变量量需要分配内存?用于存放变量量值信息,且只能在请求结束时被释放

? Lua table 具有?非常?高的查找效率
Benchmark
wrk -d 60s -t 4 -c 128 http://127.0.0.1:7106/test_ngx_var
wrk -d 60s -t 4 -c 128 http://127.0.0.1:7106/test_ngx_ctx
wrk -d 60s -t 4 -c 192 http://127.0.0.1:7106/test_ngx_var
wrk -d 60s -t 4 -c 192 http://127.0.0.1:7106/test_ngx_ctx
ngx.ctx 的不不?足
? 相对昂贵的 metamethod 调?用 - 集中使?用时局部缓存

? ?生命周期局限在?一个 location - https://github.com/tokers/lua-resty-ctxdump
lua-resty-ctxump
?日志
? 合理理设置 access_log 的 bu?er ?大?小 - 避免过多的 write 系统调?用

? 关闭 access_log 和拦截 error_log,经过?网络传输到外部组件
利利?用 LuaJIT 的优势
? 引?入 lua-resty-core(https://github.com/openresty/lua-resty-core)

? 使?用可被 JIT 编译器?编译的函数(http://wiki.luajit.org/NYI)

? 尽量量避免 table resize(table.new)
良好的编程习惯
? https://blog.codingnow.com/cloud/LuaTips

? 避免滥?用全局变量量

? 避免低效率的字符串串拼接 - table.concat
upyun-resty
? https://github.com/upyun/upyun-resty

? Tech Talks

? Nginx Modules

? Lua-Resty Libraries

? Projects
Thanks

More Related Content

Similar to OpenResty 项目性能优化实践 (20)

奥颈苍诲产驳入门
奥颈苍诲产驳入门奥颈苍诲产驳入门
奥颈苍诲产驳入门
晓锋 陈
?
Performance Monitoring With AOP
Performance Monitoring With AOPPerformance Monitoring With AOP
Performance Monitoring With AOP
ivannotes
?
纵览尝辞补诲谤耻苍苍别谤核心功能
纵览尝辞补诲谤耻苍苍别谤核心功能纵览尝辞补诲谤耻苍苍别谤核心功能
纵览尝辞补诲谤耻苍苍别谤核心功能
beiyu95
?
Pptv lb日志实时分析平台
Pptv lb日志实时分析平台Pptv lb日志实时分析平台
Pptv lb日志实时分析平台
drewz lin
?
广告技术部自动化测试介绍.辫诲蹿
广告技术部自动化测试介绍.辫诲蹿广告技术部自动化测试介绍.辫诲蹿
广告技术部自动化测试介绍.辫诲蹿
bj_qa
?
线上埋码资料收集实作
线上埋码资料收集实作线上埋码资料收集实作
线上埋码资料收集实作
FEG
?
2012 淘宝技术沙龙 PPT
2012 淘宝技术沙龙 PPT2012 淘宝技术沙龙 PPT
2012 淘宝技术沙龙 PPT
lhqunar
?
2012 淘宝技术沙龙 PPT
2012 淘宝技术沙龙 PPT2012 淘宝技术沙龙 PPT
2012 淘宝技术沙龙 PPT
lhqunar
?
Python 于 webgame 的应用
Python 于 webgame 的应用Python 于 webgame 的应用
Python 于 webgame 的应用
勇浩 赖
?
Software Engineer Talk
Software Engineer TalkSoftware Engineer Talk
Software Engineer Talk
Larry Cai
?
Django step0
Django step0Django step0
Django step0
永昇 陳
?
Data Analyse Black Horse - ClickHouse
Data Analyse Black Horse - ClickHouseData Analyse Black Horse - ClickHouse
Data Analyse Black Horse - ClickHouse
Jack Gao
?
02.辫测迟丑辞苍.开发最佳实践
02.辫测迟丑辞苍.开发最佳实践02.辫测迟丑辞苍.开发最佳实践
02.辫测迟丑辞苍.开发最佳实践
Na Lee
?
4. Go 工程化实践-0124-v2.pdf
4. Go 工程化实践-0124-v2.pdf4. Go 工程化实践-0124-v2.pdf
4. Go 工程化实践-0124-v2.pdf
ssuserd6c7621
?
2012 淘宝D2技术沙龙
2012 淘宝D2技术沙龙2012 淘宝D2技术沙龙
2012 淘宝D2技术沙龙
lhqunar
?
陈桂鸿 Ak--手机qq hybrid-app优化新思路
陈桂鸿 Ak--手机qq hybrid-app优化新思路陈桂鸿 Ak--手机qq hybrid-app优化新思路
陈桂鸿 Ak--手机qq hybrid-app优化新思路
yingganfei
?
Ops as Code using Serverless
Ops as Code using Serverless Ops as Code using Serverless
Ops as Code using Serverless
Rick Hwang
?
PHP Optimization for Millions Visits Level
PHP Optimization for Millions Visits LevelPHP Optimization for Millions Visits Level
PHP Optimization for Millions Visits Level
Ho Kim
?
奥颈苍诲产驳入门
奥颈苍诲产驳入门奥颈苍诲产驳入门
奥颈苍诲产驳入门
晓锋 陈
?
Performance Monitoring With AOP
Performance Monitoring With AOPPerformance Monitoring With AOP
Performance Monitoring With AOP
ivannotes
?
纵览尝辞补诲谤耻苍苍别谤核心功能
纵览尝辞补诲谤耻苍苍别谤核心功能纵览尝辞补诲谤耻苍苍别谤核心功能
纵览尝辞补诲谤耻苍苍别谤核心功能
beiyu95
?
Pptv lb日志实时分析平台
Pptv lb日志实时分析平台Pptv lb日志实时分析平台
Pptv lb日志实时分析平台
drewz lin
?
广告技术部自动化测试介绍.辫诲蹿
广告技术部自动化测试介绍.辫诲蹿广告技术部自动化测试介绍.辫诲蹿
广告技术部自动化测试介绍.辫诲蹿
bj_qa
?
线上埋码资料收集实作
线上埋码资料收集实作线上埋码资料收集实作
线上埋码资料收集实作
FEG
?
2012 淘宝技术沙龙 PPT
2012 淘宝技术沙龙 PPT2012 淘宝技术沙龙 PPT
2012 淘宝技术沙龙 PPT
lhqunar
?
2012 淘宝技术沙龙 PPT
2012 淘宝技术沙龙 PPT2012 淘宝技术沙龙 PPT
2012 淘宝技术沙龙 PPT
lhqunar
?
Python 于 webgame 的应用
Python 于 webgame 的应用Python 于 webgame 的应用
Python 于 webgame 的应用
勇浩 赖
?
Software Engineer Talk
Software Engineer TalkSoftware Engineer Talk
Software Engineer Talk
Larry Cai
?
Data Analyse Black Horse - ClickHouse
Data Analyse Black Horse - ClickHouseData Analyse Black Horse - ClickHouse
Data Analyse Black Horse - ClickHouse
Jack Gao
?
02.辫测迟丑辞苍.开发最佳实践
02.辫测迟丑辞苍.开发最佳实践02.辫测迟丑辞苍.开发最佳实践
02.辫测迟丑辞苍.开发最佳实践
Na Lee
?
4. Go 工程化实践-0124-v2.pdf
4. Go 工程化实践-0124-v2.pdf4. Go 工程化实践-0124-v2.pdf
4. Go 工程化实践-0124-v2.pdf
ssuserd6c7621
?
2012 淘宝D2技术沙龙
2012 淘宝D2技术沙龙2012 淘宝D2技术沙龙
2012 淘宝D2技术沙龙
lhqunar
?
陈桂鸿 Ak--手机qq hybrid-app优化新思路
陈桂鸿 Ak--手机qq hybrid-app优化新思路陈桂鸿 Ak--手机qq hybrid-app优化新思路
陈桂鸿 Ak--手机qq hybrid-app优化新思路
yingganfei
?
Ops as Code using Serverless
Ops as Code using Serverless Ops as Code using Serverless
Ops as Code using Serverless
Rick Hwang
?
PHP Optimization for Millions Visits Level
PHP Optimization for Millions Visits LevelPHP Optimization for Millions Visits Level
PHP Optimization for Millions Visits Level
Ho Kim
?

OpenResty 项目性能优化实践