希望长大对我而言,是可以做更多想做的事,而不是被迫做更多不想做的事...... 首页 利用accesslog统计QPS 丁D 学无止境 2019-10-09 09:11 100850已阅读 QPS 摘要本文将介绍如何使用tomcat的accesslog日志来统计QPS。 这篇文章很水很水,但是很实用。 >**什么是QPS?** >QPS(Query Per Second):每秒请求数,就是说服务器在一秒的时间内处理了多少个请求。 ```js 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就行了。 很赞哦! (1) 上一篇:设计模式之观察者模式 下一篇:JVM垃圾回收器 目录 点击排行 Elasticsearch6.3.2之x-pack redis哨兵 2019-07-09 22:05 Redis+Twemproxy+HAProxy+Keepalived 2019-07-12 17:20 GC优化策略和相关实践案例 2019-10-10 10:54 JVM垃圾回收器 2019-10-10 10:23 标签云 Java Spring MVC Mybatis Ansible Elasticsearch Redis Hive Docker Kubernetes RocketMQ Jenkins Nginx 友情链接 郑晓博客 佛布朗斯基 凉风有信 MarkHoo's Blog 冰洛博客 南实博客 Rui | 丁D Java研发工程师 生活可以用「没办法」三个字概括。但别人的没办法是「腿长,没办法」、「长得好看,没办法」、「有才华,没办法」。而你的没办法,是真的没办法。 请作者喝咖啡