[Java] simulating QPS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.