如何提高lua-resty-http的访问效率?

259 views
Skip to first unread message

lhmwzy

unread,
Dec 3, 2013, 12:28:03 AM12/3/13
to open...@googlegroups.com
有如下代码:

local http = require "resty.http"
local hc=http:new()
local m=0
local args = ngx.req.get_post_args()
local hphm=args.hphm
hphm=string.upper(hphm)
for i=1000,999999 do
local m= string.format("%06d",i)
local ok,code,headers,status,body=hc:request({
url="http://www.dygajj.gov.cn:9080/wscgsxxcx/jdcwfcx.do",
method="POST",
headers = { ["Content-Type"] = "application/x-www-form-urlencoded" },
body="fzjg=E&hphm="..hphm.."&hpzl=02&type=wfcx&clsbdh="..m,
})
local ma,err=ngx.re.match(body,'(.*)<input type="hidden" name="state"
value="输入的车辆识别代号不正确>,请重新输入">(.*)',
if not ma then
body = ngx.re.gsub(body,'(.*?)<img src="(.*?)" width=".*>(.*?)',"$1$3","mj");
body = ngx.re.gsub(body,'(.*?)<img border="0" src="(.*?)"
width=".*>(.*?)',"$1$3","mj");
body = ngx.re.gsub(body,'(.*?) background="(.*?)" (.*?)',"$1 $3","mj");
ngx.say(body)
ngx.say(m)
ngx.exit(ngx.HTTP_OK)
end
end



如何提高效率?

Lance

unread,
Dec 3, 2013, 12:31:16 AM12/3/13
to open...@googlegroups.com
生试啊......  只能并行试吧,不知道对面有没有封ip的机制

Lance



--
--
邮件来自列表“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

lhmwzy

unread,
Dec 3, 2013, 12:37:14 AM12/3/13
to open...@googlegroups.com
只能生试了。。

2013/12/3 Lance <lance...@gmail.com>:

lhmwzy

unread,
Dec 3, 2013, 12:38:00 AM12/3/13
to open...@googlegroups.com
如何并行?

2013/12/3 lhmwzy <lhm...@gmail.com>:

Lance

unread,
Dec 3, 2013, 2:18:48 AM12/3/13
to open...@googlegroups.com
多起几份每个管一段,或者弄个池子从里面取闲的


Lance


2013/12/3 lhmwzy <lhm...@gmail.com>

wgm

unread,
Dec 3, 2013, 8:07:16 AM12/3/13
to open...@googlegroups.com
呵呵,看到你的代码很亲切阿,你也是做交通和公安的项目吧,lua-resty-http 怎么样?稳定吗?
你为什么不在前面做一个socket connct,的连接测试一下对方的端口能是否正常打开,如果可以正常打开的话,你再考虑用多线程发送HTTP请求。
我现在的项目就是这样做的,不知道我的方法是否是最优化的?可以看看春哥他们的建议。
不过你的代码和我一样没有用到长连接,不知道lua-resty-http是否支持长连接,我现在给常把后端的接收机压垮了。
你可以看看春哥给我的建议(Re: [openresty] 关于在openresty协程中调用socket的问题),应该对你有一定帮助。


Yichun Zhang (agentzh)

unread,
Dec 3, 2013, 1:53:24 PM12/3/13
to openresty
Hello!

On Mon, Dec 2, 2013 at 9:28 PM, lhmwzy wrote:
> 有如下代码:
>
> local http = require "resty.http"
>
[...]
> 如何提高效率?
>

我觉得你需要首先把你的 nginx 本身的效率和你访问的 http 后端服务 的效率区分开。如果你的 http 后端服务是瓶颈,那么你在
nginx 一侧做的任何优化都是徒劳的。一个最简单的判断标准是观察你的 nginx worker 进程的 CPU 使用率(如果你的
nginx 没有阻塞在什么东西上面的话),如果 CPU 接近 100%,则说明你的 nginx 一侧很可能是瓶颈,此时我们再接下来谈如何在
nginx 一侧进行优化(比如使用 on-CPU 火焰图工具进行分析)。

当然,如果你的 http 后端服务的吞吐能力很强,只是响应延时较长,同时你又有多个彼此独立的 http 请求需要发起,则你可以使用
ngx_lua 的轻量级线程同时发起多个到后端的 http 请求。不过,如果你的 http
后端服务的吞吐量已经达到或者接近极限了,则提高后端的并发度只会让性能进一步恶化。

Best regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages