利用accesslog统计QPS

2022-08-12 20:47:47 浏览数 (1)

这篇文章很水很水,但是很实用。

什么是QPS? QPS(Query Per Second):每秒请求数,就是说服务器在一秒的时间内处理了多少个请求。

代码语言:javascript复制
tomcat的accesslog的格式 
119.250.238.218 - - [09/Oct/2019:03:32:46  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10959 
当我们访问articleId=145这个文章的时候这个日志文件就会产生对应一条记录,那样我们就可以根据这个日志文件来统计1s内有几个访问,就可以知道对应得QPS 
[root@iZwz9278r1bks3b80puk6fZ logs]# cat localhost_access_log.2019-10-09.txt | grep 'articleId=145' 
119.250.238.218 - - [09/Oct/2019:03:32:46  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10959 
27.202.249.24 - - [09/Oct/2019:03:34:04  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10959 
115.210.28.171 - - [09/Oct/2019:03:35:24  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10959 
115.209.119.49 - - [09/Oct/2019:03:36:42  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10959 
123.131.123.179 - - [09/Oct/2019:03:38:02  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10959 
144.123.71.235 - - [09/Oct/2019:03:39:21  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10959 
175.4.114.136 - - [09/Oct/2019:03:40:42  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10959 
115.209.125.149 - - [09/Oct/2019:03:42:02  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10959 
112.194.70.154 - - [09/Oct/2019:03:43:23  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
36.33.54.7 - - [09/Oct/2019:03:44:40  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
115.148.70.51 - - [09/Oct/2019:03:45:57  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
183.188.243.35 - - [09/Oct/2019:03:47:14  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
113.73.169.139 - - [09/Oct/2019:03:48:33  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
49.71.139.184 - - [09/Oct/2019:03:49:51  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
113.239.250.255 - - [09/Oct/2019:03:51:00  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
180.123.169.120 - - [09/Oct/2019:03:52:09  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
223.72.96.183 - - [09/Oct/2019:03:53:28  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
180.118.234.162 - - [09/Oct/2019:03:54:46  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
175.44.156.161 - - [09/Oct/2019:03:56:05  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
223.72.100.175 - - [09/Oct/2019:03:57:21  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
218.85.247.86 - - [09/Oct/2019:03:58:38  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
220.186.84.151 - - [09/Oct/2019:03:59:59  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
117.30.53.211 - - [09/Oct/2019:08:56:58  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
117.30.53.211 - - [09/Oct/2019:08:59:15  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
117.30.53.211 - - [09/Oct/2019:09:04:07  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
117.30.53.211 - - [09/Oct/2019:09:07:28  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
117.30.53.211 - - [09/Oct/2019:09:07:29  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
117.30.53.211 - - [09/Oct/2019:09:09:31  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
117.30.53.211 - - [09/Oct/2019:09:09:31  0800] "GET /article/view.do?articleId=145 HTTP/1.1" 200 10960 
[root@iZwz9278r1bks3b80puk6fZ logs]# cat localhost_access_log.2019-10-09.txt | grep 'articleId=145' |cut -d ' ' -f4|uniq -c|sort -n -r 
2 [09/Oct/2019:09:09:31 
1 [09/Oct/2019:09:07:29 
1 [09/Oct/2019:09:07:28 
1 [09/Oct/2019:09:04:07 
...... 
这样就可以看出2019:09:09:31有两个访问量即可得 QPS=2 
现在,来讲一下命令什么意思! 
cat xxx.log:读文件内容 
grep 'articleId=145':将文件内容按照GET /mvc2进行过滤 
cut -d ' ' -f4:过滤出来的内容按照空格进行分割,取第四列内容 
uniq -c:每列旁边显示该行重复出现的次数 
sort -n -r:依照数值的大小排序 

注意 上面的统计是单机版的,那假设我们的集群部署了4台机器,并且刚好是轮询的,这样乘4就行了。

0 人点赞