编写Openresty的HelloWorld并进行测试

Posted by Dayong Chan on 2019-10-27
Words 611 and Reading Time 2 Minutes
Viewed Times

一、编写内容

先创建好基本的目录

script
1
2
3
mkdir ~/work
cd ~/work
mkdir logs/ conf/

logs目录是用来存放日志的,conf目录是用来存放配置文件的

创建并编辑nginx.conf文件

script
1
vim conf/nginx.conf

把下面的内容粘贴到文件里面
script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
worker_processes  1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
location / {
default_type text/html;
content_by_lua_block {
ngx.say("<p>hello, world</p>")
}
}
}
}

二、运行程序

为了确保安装的地址是OK的,先查下openresty的安装地址

script
1
whereis openresty

如果输出跟下面一样的话就是没问题了
1
openresty: /usr/bin/openresty /usr/local/openresty

然我们把openresty内置的nginx添加到我们的环境变量里面去
script
1
2
PATH=/usr/local/openresty/nginx/sbin:$PATH
export PATH

添加完毕后就可以直接通过执行nginx的命令来启动程序了
script
1
nginx -p `pwd`/ -c conf/nginx.conf

启动过程中如果有错误的话会输出到logs/error.log上面,可以去查看下是什么问题导致的。

接下来测试一下是否能访问

script
1
curl http://localhost:8080/

正常的情况下是会返回下面的内容
1
<p>hello, world</p>

三、测试

最后我们用ab工具类测试一下openresty的性能

如果没有ab工具的话,先安装一下

script
1
yum -y install httpd-tools

然后执行下面的命令
script
1
ab -c10 -n50000 http://localhost:8080/

执行了之后会输出一堆的结果,这个就是进行压力测试的结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Completed 50000 requests
Finished 50000 requests


Server Software: openresty/1.15.8.2
Server Hostname: localhost
Server Port: 8080

Document Path: /
Document Length: 20 bytes

Concurrency Level: 10
Time taken for tests: 1.747 seconds
Complete requests: 50000
Failed requests: 0
Write errors: 0
Total transferred: 8400000 bytes
HTML transferred: 1000000 bytes
Requests per second: 28613.94 [#/sec] (mean)
Time per request: 0.349 [ms] (mean)
Time per request: 0.035 [ms] (mean, across all concurrent requests)
Transfer rate: 4694.47 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 1
Processing: 0 0 0.1 0 1
Waiting: 0 0 0.1 0 1
Total: 0 0 0.1 0 1

Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 0
95% 0
98% 0
99% 1
100% 1 (longest request)

上面ab使用10个线程发起了总共50000个请求,全部处理完得到结果花费的时间是1.747秒,这些请求全部都处理成功了。


本站版权使用署名-非商业性使用-禁止演绎 4.0 国际,转载请保留原文链接及作者。