请指点 相同的一段代码在不同请求下耗时差距很大

78 views
Skip to first unread message

MadTk4s

unread,
Mar 19, 2020, 12:03:36 PM3/19/20
to openresty
hello, 我现在面临一个问题是 同一份代码在高频请求下耗时差距很大, 

代码均为获取ngx.var 的变量, 请问谁可以解答一下我的问题吗? 谢谢

Dan Liu

unread,
Mar 19, 2020, 12:05:00 PM3/19/20
to openresty
具体现象是怎样的?最好贴一下代码和数据


  此致
祝好

刘丹

 


------------------ Original ------------------
From: "MadTk4s"<fuyul...@gmail.com>;
Date: Fri, Mar 20, 2020 00:03 AM
To: "openresty"<open...@googlegroups.com>;
Subject: [openresty] 请指点 相同的一段代码在不同请求下耗时差距很大

hello, 我现在面临一个问题是 同一份代码在高频请求下耗时差距很大, 

代码均为获取ngx.var 的变量, 请问谁可以解答一下我的问题吗? 谢谢

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/20f7e7cc-5321-4762-a089-9c121e381082%40googlegroups.com

MadTk4s

unread,
Mar 19, 2020, 12:09:24 PM3/19/20
to openresty
    local request_id = ngx_var.request_id
    local uri = request.request.URI()

    -- local unique_id = helper.guid()
    ngx.ctx._unique_id = ngx.md5(_front_ip..request_id)
    ngx.ctx._host = request.request.REMOTE_HOST()
    ngx.ctx._remote_addr = request.request.REMOTE_ADDR()
    ngx.ctx._headers = request.request.REQUEST_HEADERS()
    ngx.ctx._user_agent = request.request.HTTP_USER_AGENT()

抱歉格式不好看,  耗时差距从0.0001s 到 0.002s , 我不太清楚这样的原因是什么?   request.request 就等同于ngx.var.host 

在 2020年3月20日星期五 UTC+8上午12:03:36,MadTk4s写道:

Dan Liu

unread,
Mar 19, 2020, 12:11:25 PM3/19/20
to openresty
没看到你统计耗时的代码


  此致
祝好

刘丹

 


------------------ Original ------------------
From: "MadTk4s"<fuyul...@gmail.com>;
Date: Fri, Mar 20, 2020 00:09 AM
To: "openresty"<open...@googlegroups.com>;
Subject: [openresty] Re: 请指点 相同的一段代码在不同请求下耗时差距很大
--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com

MadTk4s

unread,
Mar 19, 2020, 12:15:06 PM3/19/20
to openresty
local req_start_time = socket.gettime()     
local request_id = ngx_var.request_id
    local uri = request.request.URI()

    -- local unique_id = helper.guid()
    ngx.ctx._unique_id = ngx.md5(_front_ip..request_id)
ngx.ctx._host = request.request.REMOTE_HOST()
    ngx.ctx._remote_addr = request.request.REMOTE_ADDR()
    ngx.ctx._headers = request.request.REQUEST_HEADERS()
    ngx.ctx._user_agent = request.request.HTTP_USER_AGENT()
local req_end_time = socket.gettime()
ngx.ctx._request_use_time = req_end_time - req_start_time

在 2020年3月20日星期五 UTC+8上午12:11:25,Dan Liu写道:
订阅: 请发空白邮件到 open...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 open...@googlegroups.com

归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+unsubscribe@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/aa69abfa-e7c9-4ab3-abf5-35c7d6635565%40googlegroups.com

MadTk4s

unread,
Mar 19, 2020, 12:49:11 PM3/19/20
to openresty
你好? 请问有什么可能会导致这样的情况吗?


在 2020年3月20日星期五 UTC+8上午12:11:25,Dan Liu写道:
没看到你统计耗时的代码
订阅: 请发空白邮件到 open...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 open...@googlegroups.com

归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+unsubscribe@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/aa69abfa-e7c9-4ab3-abf5-35c7d6635565%40googlegroups.com

DeJiang Zhu

unread,
Mar 19, 2020, 8:49:02 PM3/19/20
to open...@googlegroups.com
你的 socket.gettime() 是怎么个实现呢?
是不是这里面有系统调用呢?

这里的 2ms 的延时抖动,有可能是统计代码引入的延时

MadTk4s <fuyul...@gmail.com> 于2020年3月20日周五 上午12:49写道:
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/aa69abfa-e7c9-4ab3-abf5-35c7d6635565%40googlegroups.com

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com

归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/014cf8cb-442b-458d-99f0-2322190ce3f6%40googlegroups.com

timo

unread,
Mar 19, 2020, 9:01:42 PM3/19/20
to openresty
用now() 这种时间函数看看,不要用系统调用。

--------------原始邮件--------------
发件人:"DeJiang Zhu "<douji...@gmail.com>;
发送时间:2020年3月20日(星期五) 上午8:48
收件人:"openresty" <open...@googlegroups.com>;
主题:Re: [openresty] Re: 请指点 相同的一段代码在不同请求下耗时差距很大
-----------------------------------

MadTk4s

unread,
Mar 19, 2020, 9:06:26 PM3/19/20
to openresty
local socket = require "socket"

就是luasocket这个包嘛的, 还有一个问题是在ngx.req.read_body()之后, 居然耗时最多高达9s, 有什么办法优化吗?

在 2020年3月20日星期五 UTC+8上午8:49:02,doujiang写道:
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+unsubscribe@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/aa69abfa-e7c9-4ab3-abf5-35c7d6635565%40googlegroups.com

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 open...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 open...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+unsubscribe@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/014cf8cb-442b-458d-99f0-2322190ce3f6%40googlegroups.com

MadTk4s

unread,
Mar 19, 2020, 9:07:16 PM3/19/20
to openresty
好的, 我试试, 还有就是ngx.req.read_body() 这个函数, 也会引起很大的耗时, 不知道有什么好的办法去优化这个耗时呢?

在 2020年3月20日星期五 UTC+8上午9:01:42,timo写道:
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+unsubscribe@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/aa69abfa-e7c9-4ab3-abf5-35c7d6635565%40googlegroups.com

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 open...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 open...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+unsubscribe@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/014cf8cb-442b-458d-99f0-2322190ce3f6%40googlegroups.com

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 open...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 open...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+unsubscribe@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/CAEZxTmnXAtKGOu9RdbMtN7mhEi%3Dc5%2BePGahbS0ihaO6eZje17A%40mail.gmail.com

DeJiang Zhu

unread,
Mar 19, 2020, 10:39:14 PM3/19/20
to open...@googlegroups.com
ngx.req.read_body 这个是要读取请求体的,如果响应体大,并且网络延时大的话,9s 也是完全可能的
这个耗时,大概率是网络上接收请求体

MadTk4s <fuyul...@gmail.com> 于2020年3月20日周五 上午9:07写道:
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/aa69abfa-e7c9-4ab3-abf5-35c7d6635565%40googlegroups.com

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 open...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 open...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/014cf8cb-442b-458d-99f0-2322190ce3f6%40googlegroups.com

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 open...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 open...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/CAEZxTmnXAtKGOu9RdbMtN7mhEi%3Dc5%2BePGahbS0ihaO6eZje17A%40mail.gmail.com

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com

归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/bfecb348-eb22-4ef2-ad85-2934e6ef5e37%40googlegroups.com

MadTk4s

unread,
Mar 19, 2020, 10:41:01 PM3/19/20
to openresty
响应体其实并不大,  content_length 也就140多,  所以有可能是客户网络延迟太大导致的?  

在 2020年3月20日星期五 UTC+8上午10:39:14,doujiang写道:
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+unsubscribe@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/aa69abfa-e7c9-4ab3-abf5-35c7d6635565%40googlegroups.com

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 open...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 open...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+unsubscribe@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/014cf8cb-442b-458d-99f0-2322190ce3f6%40googlegroups.com

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 open...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 open...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+unsubscribe@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/CAEZxTmnXAtKGOu9RdbMtN7mhEi%3Dc5%2BePGahbS0ihaO6eZje17A%40mail.gmail.com

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 open...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 open...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+unsubscribe@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/bfecb348-eb22-4ef2-ad85-2934e6ef5e37%40googlegroups.com

DeJiang Zhu

unread,
Mar 21, 2020, 6:48:22 AM3/21/20
to open...@googlegroups.com
是的,大概率是这个问题

MadTk4s <fuyul...@gmail.com> 于2020年3月20日周五 上午10:41写道:
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/aa69abfa-e7c9-4ab3-abf5-35c7d6635565%40googlegroups.com

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 open...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 open...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/014cf8cb-442b-458d-99f0-2322190ce3f6%40googlegroups.com

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 open...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 open...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/CAEZxTmnXAtKGOu9RdbMtN7mhEi%3Dc5%2BePGahbS0ihaO6eZje17A%40mail.gmail.com

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 open...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 open...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/bfecb348-eb22-4ef2-ad85-2934e6ef5e37%40googlegroups.com

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com

归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/29d2b5d1-dbce-48bb-9285-679bb6ee1d4d%40googlegroups.com
Reply all
Reply to author
Forward
0 new messages