38 | [视频]巧用wrk和火焰图,科学定位性能瓶颈

你好,我是温铭。

今天是我们专栏中的最后一节视频课了,后面内容仍然以图文形式呈现。老规矩,为了更有针对性地学习,在你进行视频学习之前,我想先问你这么几个问题:

  • 你测试过 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

    老师这个案例还是挺不错的,手把手带你用火焰图做性能优化。
    过早的优化是万恶这源,还要注意优化和代码可读性间的平衡。
    作者回复

    是的,OpenResty 代码的优化做到极致就容易影响可读性。实际的项目,一般会在上面多做一层封装,把优化的细节隐藏下。

    2019-08-27 09:25:25

  • wusiration

    2019-08-22 23:32:02

    老师的这个案例,基本上把整个性能分析的流程给讲清楚了,周末搭一下环境尝试性能分析一下
    作者回复

    多动手:)

    2019-08-27 09:24:31

  • 小侠

    2021-12-18 12:03:15

    火焰图很直观
  • 北冥Master

    2019-12-04 22:26:25

    ./samples/lj-lua-stacks.sxx --arg time=5 --skip-badvars -x 2871915 > ~/perf.bt
    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,
  • 北冥Master

    2019-12-04 22:23:37

    我的环境执行lj-lua-stackxx 报错,debian9环境,systemtap应该装什么版本?我装的2.6
  • Witt

    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
    作者回复

    OpenResty 的 1.15.8 中开启了 LuaJIT64 模式,火焰图的工具一直没有跟着升级,所以存在不兼容的问题。有两个方法可以解决:
    1. 使用 OpenResty 1.13 的版本;
    2. 自己编译 OpenResty 1.15.8,把LuaJIT 64 模式关闭。

    2019-09-16 15:16:25