Java基于CountDownLatch的持续并发测试工具

可以设置并发回合,每回合并发随机次数,持续测试并发。点这里查看单回合并发测试

public abstract class AbstractConcurrentControl {

    public AtomicLong longCounter = new AtomicLong(0);

    public static final int DEFAULT_CONCURRENT_CONTROL = 160;

    public Random random = new Random();

    private int times;

    private CountDownLatch latch;

    private ThreadPoolExecutor threadPool;

    private BlockingQueue blockingQueue;

    /**
     * 单位:ms
     */
    private int interval;

    public AbstractConcurrentControl(int times, int interval) {
        this.times = times;
        this.interval = interval;
        blockingQueue = new LinkedBlockingQueue();
        threadPool = new ThreadPoolExecutor(DEFAULT_CONCURRENT_CONTROL, DEFAULT_CONCURRENT_CONTROL, 100000, TimeUnit.MILLISECONDS, blockingQueue);

    }

    public AbstractConcurrentControl(int times) {
        this(times, 1000);
    }

    /**
     * 并发执行线程
     *
     * @Title: process
     * @date 2018年12月26日 上午11:19:20
     * @author yz
     */
    public final void process() {

        // 总计并发执行的执行次数
        int exeTotal = 0;

        // 持续并发次数
        int i = 0;

        while ((times--) > 0) {
            i++;
            int num = random.nextInt(DEFAULT_CONCURRENT_CONTROL);
            System.out.println("当前次数: " + (i) + "  剩余次数: " + (times) + " , 随机推送次数:" + num);
            exeTotal = exeTotal + num;
            latch = new CountDownLatch(num);
            for (int j = 0; j < num; j++) {
                threadPool.submit(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            latch.await();
                            code();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
                latch.countDown();
            }

            try {
                TimeUnit.MILLISECONDS.sleep(interval);
            } catch (InterruptedException e) {

            }
        }
        System.out.println("总次数: " + exeTotal);
        lock();
    }

    /**
     * 并发代码
     *
     * @Title: code
     * @date 2018年12月26日 下午2:05:25
     * @author yz
     */
    protected abstract void code();

    /**
     * 并发数据
     *
     * @return
     * @Title: data
     * @date 2018年12月26日 下午2:06:14
     * @author yz
     */
    protected abstract  T data();

    /**
     * 阻塞主线程,防止JVM关闭,不建议使用Xxx.class.wait,可以使用TimeUnit.Seconds.sleep(200);
     * 如果使用TimeUnit.Seconds.sleep(200),可能会出现异常,因为JVM已经关闭,而测试的线程可能没有执行完成
     *
     * @Title: lock
     * @date 2018年12月26日 下午6:55:03
     * @author yz
     */
    protected void lock() {
        if (this.threadPool == null) {
            return;
        }
        try {
            while (true) {
                if (times <= 0 && this.threadPool.getQueue().size() <= 0) {
                    break;
                }
                TimeUnit.MILLISECONDS.sleep(500);
            }
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println("关闭线程池");
            this.threadPool.shutdown();
        }
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-application.xml"})
public class HttpPushTest extends AbstractConcurrentControl {

    static Random random = new Random();

    static List appId = new ArrayList<>();
    static Map pushTypes = new HashMap<>();

    static {
        appId.add(12301);
        appId.add(12302);
        appId.add(12303);
        pushTypes.put(12301, 0);
        pushTypes.put(12302, 0);
        pushTypes.put(12303, 0);
    }

    public HttpPushTest() {
        super(50);
    }

    @Override
    protected void code() {
        try {
            List data = data();
            httpPushService.delayBatchPushJson(data.get(0), Integer.valueOf(data.get(1)), Integer.valueOf(data.get(2)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected  T data() {
        List data = new ArrayList<>(3);
        String json  = "{\"data\":" + longCounter.getAndIncrement() + "" + "}";
        data.add(json);
        int i = random.nextInt(3);
        data.add(appId.get(i) + "");
        data.add(pushTypes.get(appId.get(i)) + "");
        return (T) data;
    }

    @Override
    public void lock() {

        try {
            int i = random.nextInt(100);
            System.out.println("sleep: " + i);
            TimeUnit.SECONDS.sleep(i);
        } catch (Exception e) {

        }
    }

    @Resource
    private HttpPushService httpPushService;

    @Test
    public void rawPushMyService() throws Exception {
        process();
    }

}

你可能感兴趣的:(java工具,Java,并发编程)