在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. java中的三元运算符

    格式: 关系表达式 ? 表达式1:表达式2 public class OperatorDemo { public static void main(String[] args){ int a = 10 ...

  2. django “如何”系列10:如何管理静态文件

    django开发者最关心的是web应用中的动态部分-视图函数和模板.但是明显,web应用还有其他需要注意的部分:静态文件(图片,css,javascript等等),那些都是渲染一个完整的页面需要的东西 ...

  3. Disruptor 线程间共享数据无需竞争

    队列的作用是缓冲 缓冲到 队列的空间里.. 线程间共享数据无需竞争 原文 地址  作者  Trisha   译者:李同杰 LMAX Disruptor 是一个开源的并发框架,并获得2011 Duke’ ...

  4. java静态类与非静态类区别

    java静态与非静态区别   这里的静态,指以static关键字修饰的,包括类,方法,块,字段. 非静态,指没有用static 修饰的. 静态有一些特点: 1.全局唯一,任何一次的修改都是全局性的影响 ...

  5. [水煮 ASP.NET Web API2 方法论](1-7)CSRF-Cross-Site Request Forgery

    问题 通过 CSRF(Cross-Site Request Forgery)防护,保护从 MVC 页面提交到ASP.NET Web API 的数据. 解决方案 ASP.NET 已经加入了 CSRF 防 ...

  6. PL/SQL 11.6 注册码

    PL/SQL Developer 下载地址:Download PL/SQL Developer 11.0.6 注册码 Product Code:4t46t6vydkvsxekkvf3fjnpzy5wb ...

  7. Power BI连接至Mogo Altas Connector For BI

    我需要使用Power BI连接至Connector For BI ,现在Connect For BI存放在Mongo Atlas中,详细的来自于官方文档,https://docs.atlas.mong ...

  8. 【javascript】基于javascript的小时钟

    计时事件:通过JavaScript,我们可以设置在一段时间间隔后执行一段代码,而不仅仅是在函数调用后立即执行. 在JavaScript中,使用计时事件是很容易的,主要有两个事件供我们使用 setTim ...

  9. Qt精简编译方法总结

    原文请看:http://blog.csdn.net/loaden/article/details/6061702 Qt如果采取默认编译安装,一般都要占用上G的空间.当初自己不想涉及Qt的一个原因,就是 ...

  10. phpcms 大杂烩

    问题1:栏目页伪静态(不生成HTML)时,URL规则中{$categorydir}{$catdir}仍显示为{$categorydir}{$catdir}解决方法. 第一步:打开phpcms\modu ...