首页 后端开发 php教程 nginx+lua+redis

nginx+lua+redis

Aug 08, 2016 am 09:19 AM
request requests time

最近在使用nginx+lua+redis做一个系统,来支撑高并发高访问量的应用。开发时突然想到golang是不是也可以达到同样的效果。于是写了个简单的代码对比一下。具体就不多做介绍了,网上很多关于nginx+lua+redis构建高并发应用的介绍。我使用的是openresty+lua+redis。先贴下测试结果,机器就是2013年新出的低配air——(1.3 GHz Intel Core i5, 4 GB 1600 MHz DDR3), 命令:ab -n 1000 -c 100 http://localhost:8880/openresty+lua+redis: Concurrency Level: 100 Time taken for tests: 0.458 seconds Complete requests: 1000 Failed requests: 0 Total transferred: 689000 bytes HTML transferred: 533000 bytes Requests per second: 2183.67 [#/sec] (mean) Time per request: 45.794 [ms] (mean) Time per request: 0.458 [ms] (mean, across all concurrent requests) Transfer rate: 1469.29 [Kbytes/sec] received golang+redis: Concurrency Level: 100 Time taken for tests: 0.503 seconds Complete requests: 1000 Failed requests: 0 Total transferred: 650000 bytes HTML transferred: 532000 bytes Requests per second: 1988.22 [#/sec] (mean) Time per request: 50.296 [ms] (mean) Time per request: 0.503 [ms] (mean, across all concurrent requests) Transfer rate: 1262.05 [Kbytes/sec] received lua代码:-- redis 配置localparams={host='127.0.0.1',port=6379,}localred=redis:new()localok,err=red:connect(params.host,params.port)ifnotokthenngx.say("failed to connect: ",err)returnendlocalposition_key=ngx.var.position_keylocalcontent=red:get(position_key)ngx.print(content)golang代码 :packagemainimport("fmt""github.com/garyburd/redigo/redis""log""net/http""time")funcgetConn()(redis.Conn,error){conn,err:=redis.DialTimeout("tcp",":6379",0,1*time.Second,1*time.Second)iferr!=nil{fmt.Println(err)}returnconn,err}funcindexHandler(whttp.ResponseWriter,r*http.Request){conn,err:=getConn()iferr!=nil{http.Error(w,err.Error(),http.StatusInternalServerError)return}result,err:=conn.Do("get","content_1")iferr!=nil{http.Error(w,err.Error(),http.StatusInternalServerError)return}fmt.Fprintf(w,"Hello, %q",result)}funcmain(){http.HandleFunc("/",indexHandler)err:=http.ListenAndServe(":8880",nil)iferr!=nil{log.Fatal("ListenAndServe: ",err.Error())}}经过多次压测之后发现,nginx + lua + redis的组合确实高效,golang + redis的方案其实也差不了多少。相对于整个系统从开发到部署的方式来说,golang可能更合适,更符合开发的习惯,毕竟nginx + lua 这种方案开发和测试都略显别扭。补充连接池的使用和测试结果

上次测试完之后,觉得这个代码还有提高的空间,于是查了下怎么在golang中使用redis连接池(其实就是redigo的使用),还有lua中怎么使用redis连接池(其实就是rest.redis的使用)。

先上结果:

openresty + lua + redis Concurrency Level: 100 Time taken for tests: 0.284 seconds Complete requests: 1000 Failed requests: 0 Total transferred: 687000 bytes HTML transferred: 531000 bytes Requests per second: 3522.03 [#/sec] (mean) Time per request: 28.393 [ms] (mean) Time per request: 0.284 [ms] (mean, across all concurrent requests) Transfer rate: 2362.93 [Kbytes/sec] received

再看golang:

golang + redis Concurrency Level: 100 Time taken for tests: 0.327 seconds Complete requests: 1000 Failed requests: 0 Total transferred: 650000 bytes HTML transferred: 532000 bytes Requests per second: 3058.52 [#/sec] (mean) Time per request: 32.696 [ms] (mean) Time per request: 0.327 [ms] (mean, across all concurrent requests) Transfer rate: 1941.44 [Kbytes/sec] received

lua代码:

-- redis 配置localparams={host='127.0.0.1',port=6379,}localred=redis:new()localok,err=red:connect(params.host,params.port)ifnotokthenngx.say("failed to connect: ",err)returnendlocalposition_key=ngx.var.position_keylocalcontent=red:get(position_key)ngx.print(content)localok,err=red:set_keepalive(10000,100)ifnotokthenngx.say("failed to set keepalive: ",err)returnend

golang代码:

packagemainimport("flag""fmt""github.com/garyburd/redigo/redis""log""net/http""runtime""time")var(pool*redis.PoolredisServer=flag.String("redisServer",":6379",""))funcindexHandler(whttp.ResponseWriter,r*http.Request){t0:=time.Now()conn:=pool.Get()t1:=time.Now()fmt.Printf("The call took %v to run.\n",t1.Sub(t0))deferconn.Close()result,err:=conn.Do("get","content_1")iferr!=nil{http.Error(w,err.Error(),http.StatusInternalServerError)return}fmt.Fprintf(w,"Hello, %q",result)}funcnewPool(serverstring)*redis.Pool{return&redis.Pool{MaxIdle:3,IdleTimeout:240*time.Second,Dial:func()(redis.Conn,error){c,err:=redis.Dial("tcp",server)iferr!=nil{returnnil,err}returnc,err},TestOnBorrow:func(credis.Conn,ttime.Time)error{_,err:=c.Do("PING")returnerr},}}funcmain(){runtime.GOMAXPROCS(runtime.NumCPU())flag.Parse()pool=newPool(*redisServer)http.HandleFunc("/",indexHandler)err:=http.ListenAndServe(":8880",nil)iferr!=nil{log.Fatal("ListenAndServe: ",err.Error())}}

golang中除了添加了线程池,还设置了cpu核数。

不过这个测试并不十分严谨,redis,nginx,golang http server,ab压测都在一台机器,相互之间会有影响。有兴趣的可以自己分开部署测试下。

以上就介绍了nginx+lua+redis,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

python中CURL和python requests的相互转换如何实现 python中CURL和python requests的相互转换如何实现 May 03, 2023 pm 12:49 PM

curl和Pythonrequests都是发送HTTP请求的强大工具。虽然curl是一种命令行工具,可让您直接从终端发送请求,但Python的请求库提供了一种更具编程性的方式来从Python代码中发送请求。将curl转换为Pythonrequestscurl命令的基本语法如下所示:curl[OPTIONS]URL将curl命令转换为Python请求时,我们需要将选项和URL转换为Python代码。这是一个示例curlPOST命令:curl-XPOSThttps://example.com/api

Python爬虫Requests库怎么使用 Python爬虫Requests库怎么使用 May 16, 2023 am 11:46 AM

1、安装requests库因为学习过程使用的是Python语言,需要提前安装Python,我安装的是Python3.8,可以通过命令python--version查看自己安装的Python版本,建议安装Python3.X以上的版本。安装好Python以后可以直接通过以下命令安装requests库。pipinstallrequestsPs:可以切换到国内的pip源,例如阿里、豆瓣,速度快为了演示功能,我这里使用nginx模拟了一个简单网站。下载好了以后,直接运行根目录下的nginx.exe程序就可

php request什么意思 php request什么意思 Jul 07, 2021 pm 01:49 PM

request的中文意思为“请求”,是php中的一个全局变量,是一个包含了“$_POST”、“$_GET”和“$_COOKIE”的数组。“$_REQUEST”变量可以获取POST或GET方式提交的数据、COOKIE信息。

time包的单调时钟处理 time包的单调时钟处理 Aug 04, 2023 pm 05:45 PM

我们今天主要是来看一看golang time 包的时间应用方式。两者的一般规则是「wall time」用于告知时间,而「monotonic clock」用于测量时间;除外还有其他的时钟处理方式。

Python如何使用Requests请求网页 Python如何使用Requests请求网页 Apr 25, 2023 am 09:29 AM

Requests继承了urllib2的所有特性。Requests支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动确定响应内容的编码,支持国际化的URL和POST数据自动编码。安装方式利用pip安装$pipinstallrequestsGET请求基本GET请求(headers参数和parmas参数)1.最基本的GET请求可以直接用get方法'response=requests.get("http://www.baidu.com/&quot

Python 3.x 中如何使用urllib.request.urlopen()函数发送GET请求 Python 3.x 中如何使用urllib.request.urlopen()函数发送GET请求 Jul 30, 2023 am 11:28 AM

Python3.x中如何使用urllib.request.urlopen()函数发送GET请求在网络编程中,我们经常需要通过发送HTTP请求来获取远程服务器的数据。在Python中,我们可以使用urllib模块中的urllib.request.urlopen()函数来发送HTTP请求,并获取服务器返回的响应。本文将介绍如何使用

python requests post如何使用 python requests post如何使用 Apr 29, 2023 pm 04:52 PM

python模拟浏览器发送post请求importrequests格式request.postrequest.post(url,data,json,kwargs)#post请求格式request.get(url,params,kwargs)#对比get请求发送post请求传参分为表单(x-www-form-urlencoded)json(application/json)data参数支持字典格式和字符串格式,字典格式用json.dumps()方法把data转换为合法的json格式字符串次方法需要

使用Python的Requests和BeautifulSoup下载PDF文件 使用Python的Requests和BeautifulSoup下载PDF文件 Aug 30, 2023 pm 03:25 PM

Request和BeautifulSoup是可以在线下载任何文件或PDF的Python库。请求库用于发送HTTP请求和接收响应。BeautifulSoup库用于解析响应中收到的HTML并获取可下载的pdf链接。在本文中,我们将了解如何在Python中使用Request和BeautifulSoup下载PDF。安装依赖项在Python中使用BeautifulSoup和Request库之前,我们需要使用pip命令在系统中安装这些库。要安装request以及BeautifulSoup和Request库,

See all articles