你好,我是温铭。
今天是我们专栏中的最后一节视频课了,后面内容仍然以图文形式呈现。老规矩,为了更有针对性地学习,在你进行视频学习之前,我想先问你这么几个问题:
- 你测试过 OpenResty 程序的性能吗?如何才能科学地找到性能瓶颈?
- 如何看懂火焰图的信息,并与 Lua 代码相对应呢?
这几个问题,也是今天视频课要解决的核心内容,希望你可以先自己思考一下,并带着问题来学习今天的视频内容。
同时,我会给出相应的文字介绍,方便你在听完视频内容后,及时总结与复习。下面是今天这节课的文字介绍部分。
今日核心
今天的视频课,我会用一个开源的小项目来演示一下,如何通过 wrk 和火焰图来优化代码,这个项目地址为:https://github.com/iresty/lua-performance-demo。
视频中的环境是 Ubuntu 16.04,其中的 systemtap 和 wrk 工具,都是使用 apt-get 来安装的,不推荐你用源码来安装。
这里的demo 有几个不同的版本,我会用 wrk 来压测每一个版本的 qps。同时,在压测过程中,我都会使用 stapxx 来生成火焰图,并用火焰图来指导我们去优化哪一个函数和代码块。
最后的结果是,我们会看到一个性能提升 10 倍以上的版本,当然,这其中的优化方式,都是在专栏前面课程中提到过的。建议你可以 clone 这个 demo 项目,来复现我在视频中的操作,加深对 wrk、火焰图和性能优化的理解。
要知道,性能优化并不是感性和直觉的判断,而是需要科学的数据来做指导的。这里的数据,不仅仅是指 qps 等最终的性能指标,也包括了用数据来定位具体的瓶颈。
课件参考
今天的课件已经上传到了我的GitHub上,你可以自己下载学习。
链接如下:https://github.com/iresty/geektime-slides
如果有不清楚的地方,你可以在留言区提问,另也可以在留言区分享你的学习心得。期待与你的对话,也欢迎你把这篇文章分享给你的同事、朋友,我们一起交流、一起进步。
精选留言
2019-08-21 16:37:19
过早的优化是万恶这源,还要注意优化和代码可读性间的平衡。
2019-08-22 23:32:02
2021-12-18 12:03:15
2019-12-04 22:26:25
Found exact match for libluajit: /usr/local/openresty/luajit/lib/libluajit-5.1.so.2.1.0
In file included from /usr/share/systemtap/runtime/linux/runtime.h:201:0,
from /usr/share/systemtap/runtime/runtime.h:24,
from /tmp/stapdiQN45/stap_b7d4c96a12d824bc289ba86f5d42b8c9_43901_src.c:26:
/usr/share/systemtap/runtime/linux/access_process_vm.h: In function ‘__access_process_vm_’:
/usr/share/systemtap/runtime/linux/access_process_vm.h:24:8: error: implicit declaration of function ‘get_task_mm’ [-Werror=implicit-function-declaration]
mm = get_task_mm (tsk);
^~~~~~~~~~~
/usr/share/systemtap/runtime/linux/access_process_vm.h:24:6: error: assignment makes pointer from integer without a cast [-Werror=int-conversion]
mm = get_task_mm (tsk);
^
/usr/share/systemtap/runtime/linux/access_process_vm.h:35:29: error: passing argument 1 of ‘get_user_pages’ makes integer from pointer without a cast [-Werror=int-conversion]
ret = get_user_pages (tsk, mm, addr, 1, write, 1, &page, &vma);
^~~
In file included from ./include/linux/pid_namespace.h:7:0,
from ./include/linux/ptrace.h:10,
from ./include/linux/ftrace.h:14,
from ./include/linux/kprobes.h:42,
from /usr/share/systemtap/runtime/linux/runtime.h:21,
2019-12-04 22:23:37
2019-09-15 22:51:03
报错信息:
Found exact match for libluajit: /usr/local/openresty/luajit/lib/libluajit-5.1.so.2.1.0
WARNING: cannot find module /usr/local/openresty/nginx/sbin/nginx debuginfo: No DWARF information found [man warning::debuginfo]
WARNING: cannot find module /usr/local/openresty/luajit/lib/libluajit-5.1.so.2.1.0 debuginfo: No DWARF information found [man warning::debuginfo]
semantic error: while processing function luajit_G
semantic error: type definition 'lua_State' not found in '/usr/local/openresty/luajit/lib/libluajit-5.1.so.2.1.0': operator '@cast' at stapxx-0fF6qb3V/luajit.stp:162:12
source: return @cast(L, "lua_State", "/usr/local/openresty/luajit/lib/libluajit-5.1.so.2.1.0")->glref->ptr32
^
Pass 2: analysis failed. [man error::pass2]
Number of similar warning messages suppressed: 634.
Rerun with -v to see them.
Tip: /usr/share/doc/systemtap/README.Debian should help you get started.
ERROR: No stack counts found
环境:
Ubuntu 16.04.6
openresty/1.15.8.2