[java] 類比QPS

來源:互聯網
上載者:User

標籤:

在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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.