在WEB服务器端,每日的访问量巨大。在非生产环境需要对服务器进行压力测试,一般使用后台线程和Sleep方式来模拟线上的压力。这里使用ScheduledExecutorService实现一种简单的QPS测试代码。

QpsProxy:

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; /**
* 若Runable执行的时间超过 MAX_QPS / qps,则实际的QPS会低于实际的值。
*/
public class QpsProxy { private final Logger logger = LoggerFactory.getLogger(QpsProxy.class); private final static int MAX_QPS = 1000; private ScheduledExecutorService scheduledExecutorService; private long qps = NumberUtils.LONG_ONE; private Runnable runnable; private long delay2Start = NumberUtils.INTEGER_ZERO; private int threads = 10; public QpsProxy() {
} public ScheduledExecutorService getScheduledExecutorService() {
return scheduledExecutorService;
} public void setScheduledExecutorService(ScheduledExecutorService scheduledExecutorService) {
this.scheduledExecutorService = scheduledExecutorService;
} public long getQps() {
return qps;
} public void setQps(long qps) {
Preconditions.checkArgument(qps < MAX_QPS , "设置的qps超过上限值:" + MAX_QPS);
this.qps = qps;
} public Runnable getRunnable() {
return runnable;
} public void setRunnable(Runnable runnable) {
this.runnable = runnable;
} public long getDelay2Start() {
return delay2Start;
} public void setDelay2Start(long delay2Start) {
this.delay2Start = delay2Start;
} public int getThreads() {
return threads;
} public void setThreads(int threads) {
this.threads = threads;
} public void start() {
Preconditions.checkNotNull(runnable, "请设置执行的任务");
long period = (long) Math.floor((double) MAX_QPS / qps);
logger.info("间隔周期:{}ms", period);
scheduledExecutorService = Executors.newScheduledThreadPool(threads);
scheduledExecutorService.scheduleAtFixedRate(runnable, delay2Start,
period, TimeUnit.MILLISECONDS);
} public void stop() {
Preconditions.checkNotNull(scheduledExecutorService, "任务未开始");
scheduledExecutorService.shutdown();
}
}

  构建工具:

import com.google.common.base.Preconditions;

public class QpsProxyBuilder {

    private QpsProxy qpsProxy = new QpsProxy();

    public static QpsProxyBuilder newBuilder() {
return new QpsProxyBuilder();
} public QpsProxyBuilder withDelay2Start(long delay2TimeByMillisSeconds) {
Preconditions.checkArgument(delay2TimeByMillisSeconds > 0);
qpsProxy.setDelay2Start(delay2TimeByMillisSeconds);
return this;
} public QpsProxyBuilder withQps(long qps) {
Preconditions.checkArgument(qps > 0);
qpsProxy.setQps(qps);
return this;
} public QpsProxyBuilder withRunnable(Runnable runnable) {
Preconditions.checkNotNull(runnable);
qpsProxy.setRunnable(runnable);
return this;
} public QpsProxyBuilder withThreads(int threads) {
Preconditions.checkNotNull(threads > 0);
qpsProxy.setThreads(threads);
return this;
} public QpsProxy build() {
return qpsProxy;
}
}

  测试代码:

import com.qunar.hotel.qps.QpsProxy;
import com.qunar.hotel.qps.QpsProxyBuilder;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class qps { private final Logger logger = LoggerFactory.getLogger(getClass()); @Test
public void testBasic() throws InterruptedException {
QpsProxy proxy = QpsProxyBuilder.newBuilder().withQps(100).withThreads(1).withDelay2Start(4000).withRunnable(new Runnable() {
private int counter = 0; @Override
public void run() {
logger.info("{}-{}:{}", Thread.currentThread().getName(), System.nanoTime(), counter++);
}
}).build(); proxy.start();
Thread.currentThread().sleep(5020L);
proxy.stop();
}
}

  

[java] 模拟QPS的更多相关文章

  1. java模拟post请求发送json

    java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: package main ...

  2. java 模拟qq源码

    java 模拟qq源码: http://files.cnblogs.com/files/hujunzheng/QQ--hjzgg.zip

  3. java模拟开锁

    java模拟开锁 service qq:928900200 Introduction to Computer Science II: CSCI142Fall 2014Lab #1Instructor: ...

  4. Jsoup实现java模拟登陆

    Jsoup实现java模拟登陆 2013-10-29 14:52:05|  分类: web开发|举报|字号 订阅     下载LOFTER我的照片书  |     1:如何获取cookies. 1.1 ...

  5. [Java] 模拟HTTP的Get和Post请求

    在之前,写了篇Java模拟HTTP的Get和Post请求的文章,这篇文章起源与和一个朋友砍飞信诈骗网站的问题,于是动用了Apache的comments-net包,也实现了get和post的http请求 ...

  6. Java模拟登录系统抓取内容【转载】

    没有看考勤的习惯,导致我的一天班白上了,都是钱啊,系统也不发个邮件通知下....     为了避免以后还有类似状况特别写了个java模拟登录抓取考勤内容的方法(部分代码来自网络),希望有人修改后也可以 ...

  7. Java模拟登陆02【转载】

    在使用java访问URL时,如果该URL需要身份验证,那么就不能够直接访问,因为没有登陆.那么,如何解决这个问题呢?     方法是使用java模拟登陆,登陆后记录下cookie信息,在下次发起请求时 ...

  8. java模拟浏览器包selenium整合了htmlunit,火狐浏览器,IE浏览器,opare浏览器驱

    //如果网页源码中有些内容是js渲染过来的,那你通过HttpClient直接取肯定取不到,但是这些数据一般都是通过异步请求传过来的(一般都是通过ajax的get或者post方式).那么你可以通过火狐浏 ...

  9. 上curl java 模拟http请求

    最近,我的项目要求java模拟http请求,获得dns解决 tcp处理过的信息特定的连接. java api提供urlConnection apache提供的httpClient都不能胜任该需求,二次 ...

随机推荐

  1. mui 怎样监听scroll事件的滚动距离

    var scroll = mui('.mui-scroll-wrapper').scroll(); document.querySelector('.mui-scroll-wrapper' ).add ...

  2. Leetcode 之Length of Last Word(37)

    扫描每个WORD的长度并记录即可. int lengthOfLast(const char *s) { //扫描统计每个word的长度 ; while (*s) { if (*s++ != ' ')/ ...

  3. Nginx web proxy NFS服务

    1.nginx web 安装 配置 #systemctl stop firewalld #systemctl disabled firewalld #wget -O /etc/yum.repos.d/ ...

  4. MapReduce案例一:天气温度

    1.需求 2.思路 3.代码实现 3.1MyWeather 类代码: 这个类主要是用来定义hadoop的配置,在执行计算程序时所需加载的一些类. package com.hadoop.mr.weath ...

  5. Letter Combinations of a Phone Number——简单的回溯算法

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  6. 【hdoj_2187】老人是真饿了

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2187 题意:由很多种价格的大米,在经费一定的情况下,买重量更多的大米,并且题目假设经费买不光所有的大米. ...

  7. hdu多校第三场

    Problem D. Euler Function 思路:打表找找规律. #include<bits/stdc++.h> #define LL long long #define fi f ...

  8. thinkphp下实现ajax无刷新分页

    1.前言 作为一名php程序员,我们开发网站主要就是为了客户从客户端进行体验,在这里,thinkphp框架自带的分页类是每次翻页都要刷新一下整个页面,这种翻页的用户体验显然是不太理想的,我们希望每次翻 ...

  9. POJ 1860 Currency Exchange【SPFA判环】

    Several currency exchange points are working in our city. Let us suppose that each point specializes ...

  10. 洛谷——P3905 道路重建

    P3905 道路重建 题目描述 从前,在一个王国中,在n个城市间有m条道路连接,而且任意两个城市之间至多有一条道路直接相连.在经过一次严重的战争之后,有d条道路被破坏了.国王想要修复国家的道路系统,现 ...