On the Web server side, the daily traffic is huge. In a non-production environment, stress testing of the server is required, typically using a background thread and sleep mode to simulate the stress on the line. Here a simple QPS test code is implemented using Scheduledexecutorservice.
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;/** * If the runable execution time exceeds MAX_QPS/ QPS, the actual QPS will be lower than the actual value. */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 Scheduledexecuto Rservice; } public void Setscheduledexecutorservice (Scheduledexecutorservice scheduledexecutorservice) {This.scheduledExe Cutorservice = Scheduledexecutorservice; } Public long Getqps () {return QPS; } public void Setqps (long QPS) {preconditions.checkargument (QPS < MAX_QPS, "set the QPS above the upper limit value:" + 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, "set task performed"); Long period = (long) Math.floor ((double) max_qps/qps); Logger.info ("interval: {}MS", period); Scheduledexecutorservice = Executors.newscheduledthreadpool (threads); Scheduledexecutorservice.scheduleatfixedrate (runnable, Delay2start, period, TiMeunit.milliseconds); } public void Stop () {preconditions.checknotnull (Scheduledexecutorservice, "task not Started"); Scheduledexecutorservice.shutdown (); }}
Build Tool:
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 (Delay2Tim Ebymillisseconds > 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; }}
Test code:
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 (+). 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] simulating QPS