Operations 8 min read

Speed Up Load Test Preparation with Java CyclicBarrier

This article explains how to use Java's CyclicBarrier and related concurrency utilities to parallelize user login preparation for load testing, reducing setup time and synchronizing multi‑stage performance tests with concrete code examples and sample console output.

FunTester
FunTester
FunTester
Speed Up Load Test Preparation with Java CyclicBarrier

During performance testing, certain pre‑conditions—such as logging in a large number of users—must be satisfied before the actual load can start. The author originally logged in 200 students sequentially, which consumed unnecessary time.

After learning about the CyclicBarrier class, the author decided to compress the preparation phase by executing logins concurrently and releasing all threads at a single synchronization point, enabling multi‑stage load testing.

Approach

When creating ThreadBase objects, an AtomicInteger is used to assign a unique user account and password to each thread. Two barrier points are configured:

before() : waits for all user base objects ( OkayBase) and business module objects ( OKclass) to finish initialization.

after() : demonstrates a second barrier using CountDownLatch inside ThreadBase to wait for all thread tasks to complete, illustrating multi‑stage synchronization with CyclicBarrier.

Demo Implementation

The after() method of ThreadBase simply counts down the latch:

/**
 * 运行待测方法后的处理
 */
protected void after() {
    if (countDownLatch != null)
        countDownLatch.countDown();
}

Full Example

package com.okayqa.composer.performance.master1_0;

import com.fun.base.constaint.ThreadLimitTimesCount;
import com.fun.frame.execute.Concurrent;
import com.fun.frame.httpclient.ClientManage;
import com.fun.utils.ArgsUtil;
import com.okayqa.composer.base.OkayBase;
import com.okayqa.composer.function.OKClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class BothCollectCopy extends OkayBase {
    private static Logger logger = LoggerFactory.getLogger(BothCollectCopy.class);
    static AtomicInteger u = new AtomicInteger(0);
    static int times = 0;
    static int thread;
    static CyclicBarrier cyclicBarrier;

    public static void main(String[] args) {
        ClientManage.init(5, 1, 0, "", 0);
        ArgsUtil util = new ArgsUtil(args);
        thread = util.getIntOrdefault(0, 5);
        times = util.getIntOrdefault(1, 2);
        cyclicBarrier = new CyclicBarrier(thread, new Runnable() {
            @Override
            public void run() {
                logger.info("到达人数 {} ", cyclicBarrier.getParties());
            }
        });
        def funs = [];
        thread.times {
            funs << new Fun();
        }
        new Concurrent(funs, "收藏和取消收藏").start();
        allOver();
    }

    static int getTimes() { return times; }

    static class Fun extends ThreadLimitTimesCount {
        OkayBase okayBase;
        OKClass driver;
        public Fun() { super(null, getTimes(), null); }
        @Override
        void before() {
            super.before();
            okayBase = getBase(u.getAndIncrement());
            driver = new OKClass(okayBase);
            cyclicBarrier.await(10, TimeUnit.SECONDS);
        }
        @Override
        protected void after() {
            super.after();
            cyclicBarrier.await(100, TimeUnit.SECONDS);
        }
        @Override
        protected void doing() throws Exception {
            // Simulated work
            sleep(1.0);
            logger.info(this.threadName);
            sleep(1.0);
        }
    }
}

Console Output Example

INFO-> 当前用户:fv,IP:10.60.192.21,工作目录:/Users/fv/Documents/workspace/okay_test/,系统编码格式:UTF-8,系统Mac OS X版本:10.16
INFO-> requestid: Fdev16093173246753
... (login logs for each user) ...
INFO-> 到达人数 5 
INFO-> 收藏和取消收藏0
... (per‑thread statistics) ...
INFO-> 总计5个线程,共用时:4.925 s,执行总数:10,错误数:0,失败数:0
INFO-> 数据保存成功!文件名:/Users/fv/Documents/workspace/okay_test/long/data/5收藏和取消收藏20201230163524

The run demonstrates that all 200 user logins are performed in parallel, the barrier releases the threads simultaneously, and the final metrics (QPS, total requests, error rate) are collected efficiently.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaconcurrencyPerformance TestingLoad TestingmultithreadingCyclicBarrier
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.