Postman:Http请求模拟工具
Apache Bench(AB):Apache附带的工具,测试网站性能
如输入命令:ab -n 1000 -c 50 http://localhost:8081/test
JMeter:Apache组织开发的压力测试工具
安装方法看下文【参考】部分
- 创建线程组:
- 添加Http测试
- 添加测试结果查看监视器
代码:Semaphore、CountDownLatch等
package com.dai.concurrency;
import com.dai.concurrency.annoations.NotThreadSafe;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
/**
* author daioo
* create 2019-04-13 21:26
*/
@Slf4j
public class ConcurrencyTest {
public static int clientTotal = 5000;
public static int threadTotal = 50;
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();
System.out.println(count);
}
private static void add() {
count++;
}
}
输出结果是4989,不是5000,是线程不安全的