java concurrent programming notes (b) - concurrent Tools

java concurrent programming notes (b) - concurrent Tools

tool:

  • Postman: http request simulation tools
  • Apache Bench (AB): Apache comes with a tool to test site performance
  • JMeter: Apache organization to develop stress testing tool
  • Code: Semaphone, CountDownLatch etc.

PostMan:

Apache Bench(AB)

ab -n [请求总数] -c [本次请求的并发数是50] [url]
例如:ab -1000 -c 50 http://www.baidu.com

JMeter

A graphical tool, very powerful

Code simulate concurrent test

CountDownLatch

T1, T2, T3 are executed each time the counter is decremented by 1, when the thread A counter is decremented to zero, will execute.

To ensure that the needs of a particular thread after the other thread executing the re-execution of

Semaphore (semaphore)

The same time the number of threads in parallel

Mainly used to control the number of concurrent

Concurrent scene simulation

public class ConcurrencyTest {

    // 请求总数
    public static int clientTotal = 5000;

    // 同时并发执行的线程数
    public static int threadTotal = 200;

    public static int count = 0;

    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(threadTotal);
        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
        for (int i = 0; i < clientTotal ; i++) {
            executorService.execute(() -> {
                try {
                    semaphore.acquire();
                    add();
                    semaphore.release();
                } catch (Exception e) {
                    log.error("exception", e);
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        log.info("count:{}", count);
    }

    private static void add() {
        count++;
    }
}

Guess you like

Origin www.cnblogs.com/xujie09/p/11694108.html